Repository navigation
Expand file tree
/
Copy pathcheck_nano.py
More file actions
148 lines (124 loc) · 4.92 KB
/
Copy pathcheck_nano.py
File metadata and controls
148 lines (124 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/bin/env python3
############################################################################
# The MIT License
#
# Copyright (c) 2022 NETWAYS GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
############################################################################
# Author: Andre Paskowski
# GitHub: https://github.com/APaskowski
from urllib.request import urlopen
from urllib.parse import urljoin
from urllib.error import HTTPError, URLError
from xml.etree import ElementTree as ET
import ssl
import sys
import argparse
__version__ = "1.0.0"
# State output
states = ["OK", "WARNING", "CRITICAL", "UNKNOWN"]
def commandline(args):
parser = argparse.ArgumentParser(prog="check_nano")
parser.add_argument('-V', '--version', action='version', version='check_nano' + __version__)
parser.add_argument(
"-H", "--host", type=str, required=True,
help="Nano sensor hostname (Premise)"
)
parser.add_argument('-T', '--timeout', help='Seconds before connection times out (default 10)',
default=10,
type=int)
parser.add_argument('--insecure',
dest='insecure',
action='store_true',
default=False,
help='Allow insecure SSL connections (default False)')
parser.add_argument('--protocol',
choices=["http", "https"],
default='https',
help='HTTP protocol, use one of http or https (default https)')
return parser.parse_args(args)
def get_data(url, timeout, insecure):
"""
Requests the data via HTTP. Basically a wrapper around urllib.
Expected Repsonse:
<response>
<out>00000000</out>
<on>00000000</on>
<in>00000000</in>
<counter1>0</counter1>
<temp1>34.3</temp1>
<mac>00:00:00:00:00:00</mac>
</response>
"""
# Default context for connection
ctx = ssl.create_default_context()
if insecure is True:
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
response = urlopen(url=url, timeout=timeout, context=ctx) # pylint: disable=consider-using-with
if response.getcode() >= 400:
raise RuntimeError("Could not get response")
# Convert UTF-8 to ISO here, device deliver 8859-1 encodings
return response.read().decode(encoding="iso-8859-1")
def main(args):
url = urljoin(f"{args.protocol}://{args.host}", "status.xml")
state = 3
try:
resp = get_data(url=url,
timeout=args.timeout,
insecure=args.insecure) # pylint: disable=consider-using-with
except (HTTPError, URLError) as err:
print(f"check_nano {states[3]}: Could not connect to sensor", err)
return 3
try:
root = ET.fromstring(resp)
except ET.ParseError as err:
print(f"check_nano {states[3]}: Could not parse response", err)
return 3
data = {}
for child in root:
try:
data[child.tag] = float(child.text)
except ValueError:
data[child.tag] = child.text
# Convert alarm attribute value explicitly to boolean
data["on"] = bool(data["on"])
# Output temperature and Alarm state (true/false)
if data["on"] is True:
state = 2
elif data["on"] is False:
state = 0
# Output of temp and alarm.
# The "°C" is a html entity to make it readable in icinga
print(
f"check_nano: {states[state]} temp={data['temp1']}°C," +
f" alarm={data['on']}|'temp'={data['temp1']}"
)
return state
if __name__ == '__main__': # pragma: no cover
try:
ARGS = commandline(sys.argv[1:])
sys.exit(main(ARGS))
except SystemExit:
# Re-throw the exception
raise sys.exc_info()[1].with_traceback(sys.exc_info()[2]) # pylint: disable=raise-missing-from
except:
print("UNKNOWN - Error: %s" % (str(sys.exc_info()[1])))
sys.exit(3)