-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsenhub-importer.py
76 lines (75 loc) · 2.07 KB
/
senhub-importer.py
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
#!/usr/bin/python3
"""
.SYNOPSIS
Requests SenHub API
.DESCRIPTION
This sensor use the built in python libraries
Output: 0 OK
1 WARNING
2 CRITICAL
3 UNKNOWN
.PARAMETER
URL : SenHub URL between ''
.NOTES
1.1
First version
29/09/2021
Alexis
1.2
Check args
19/10/2024
Yann
"""
from urllib import request
import sys
def http_request(url):
try:
# Try to reach the endpoint given by the url
result = (request.urlopen(url)).read()
result = result.decode('utf-8')
except Exception as e:
# If something is wrong return an error message
print(e)
sys.exit(3)
else:
# if everything works fine check the status of the return string
if result.startswith('OK'):
print(result)
sys.exit(0)
elif result.startswith('CRITICAL'):
print(result)
sys.exit(2)
elif result.startswith('UNKNOWN'):
print(result)
sys.exit(3)
elif result.startswith('WARNING'):
print(result)
sys.exit(1)
else:
print(result)
sys.exit(3)
try:
if len(sys.argv) != 2:
raise IndexError('Exactly one URL parameter is required')
url = sys.argv[1]
if not (url.startswith('http://') or url.startswith('https://')):
raise ValueError('URL must start with http:// or https://')
if 'senhub.io' not in url:
raise ValueError('URL must contain senhub.io')
if 'format=nagios' not in url:
raise ValueError('URL must contain format=nagios')
http_request(url)
except IndexError as e:
# If no url is given or more than one argument is given, return an error
print(e)
sys.exit(2)
except ValueError as e:
# If the URL does not meet the required conditions, return an error
print(e)
sys.exit(2)
except Exception as e:
# If something else goes wrong, return an error message
print(e)
sys.exit(2)