· 5 years ago · Dec 31, 2020, 02:20 AM
1
2# Check if Sonarr responds
3if settings['sonarr_api'] and settings['sonarr_hostname'] and settings['sonarr_port']:
4 print('Upcheck: Sonarr')
5 headers = {'X-Api-Key':settings['sonarr_api'], 'X-Requested-With':'XMLHttpRequest'}
6 path = 'http://'+ settings['sonarr_hostname'] +':'+ settings['sonarr_port'] +'/api/v3/system/status'
7 try:
8 print('\tChecking API')
9 req = requests.get(path, headers=headers)
10 response = json.loads(req.text)
11 if len(response['version']) > 0:
12 postData = {'api':settings['api'], 'sonarr_upcheck':'true'}
13 headers = {'content-type':'application/x-www-form-urlencoded'}
14 encoded = urllib.parse.urlencode(postData)
15 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
16 print('\tSent')
17 except ConnectionError:
18 print('\tError - Invalid host:port setting')
19 print('\tFailure')
20 except KeyError:
21 print('\tError - Invalid API key')
22 print('\tFailure')
23 except requests.exceptions.HTTPError as e:
24 print('\tError - '+ e)
25 print('\tFailure')
26
27# Check if Radarr responds
28if settings['radarr_api'] and settings['radarr_hostname'] and settings['radarr_port']:
29 print('Upcheck: Radarr')
30 headers = {'X-Api-Key':settings['radarr_api'], 'X-Requested-With':'XMLHttpRequest'}
31 path = 'http://'+ settings['radarr_hostname'] +':'+ settings['radarr_port'] +'/api/v3/system/status'
32 try:
33 print('\tChecking API')
34 req = requests.get(path, headers=headers)
35 response = json.loads(req.text)
36 if len(response['version']) > 0:
37 postData = {'api':settings['api'], 'sonarr_upcheck':'true'}
38 headers = {'content-type':'application/x-www-form-urlencoded'}
39 encoded = urllib.parse.urlencode(postData)
40 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
41 print('\tSent')
42 except ConnectionError:
43 print('\tError - Invalid host:port setting')
44 print('\tFailure')
45 except KeyError:
46 print('\tError - Invalid API key')
47 print('\tFailure')
48 except requests.exceptions.HTTPError as e:
49 print('\tError - '+ e)
50 print('\tFailure')
51
52# Check if Readarr responds
53if settings['readarr_api'] and settings['readarr_hostname'] and settings['readarr_port']:
54 print('Upcheck: Readarr')
55 headers = {'X-Api-Key':settings['readarr_api'], 'X-Requested-With':'XMLHttpRequest'}
56 path = 'http://'+ settings['readarr_hostname'] +':'+ settings['readarr_port'] +'/api/v1/system/status'
57 try:
58 print('\tChecking API')
59 req = requests.get(path, headers=headers)
60 response = json.loads(req.text)
61 if len(response['version']) > 0:
62 postData = {'api':settings['api'], 'sonarr_upcheck':'true'}
63 headers = {'content-type':'application/x-www-form-urlencoded'}
64 encoded = urllib.parse.urlencode(postData)
65 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
66 print('\tSent')
67 except ConnectionError:
68 print('\tError - Invalid host:port setting')
69 print('\tFailure')
70 except KeyError:
71 print('\tError - Invalid API key')
72 print('\tFailure')
73 except requests.exceptions.HTTPError as e:
74 print('\tError - '+ e)
75 print('\tFailure')
76
77# Check if Lidarr responds
78if settings['lidarr_api'] and settings['lidarr_hostname'] and settings['lidarr_port']:
79 print('Upcheck: Lidarr')
80 headers = {'X-Api-Key':settings['lidarr_api'], 'X-Requested-With':'XMLHttpRequest'}
81 path = 'http://'+ settings['lidarr_hostname'] +':'+ settings['lidarr_port'] +'/api/v1/system/status'
82 try:
83 print('\tChecking API')
84 req = requests.get(path, headers=headers)
85 response = json.loads(req.text)
86 if len(response['version']) > 0:
87 postData = {'api':settings['api'], 'sonarr_upcheck':'true'}
88 headers = {'content-type':'application/x-www-form-urlencoded'}
89 encoded = urllib.parse.urlencode(postData)
90 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
91 print('\tSent')
92 except ConnectionError:
93 print('\tError - Invalid host:port setting')
94 print('\tFailure')
95 except KeyError:
96 print('\tError - Invalid API key')
97 print('\tFailure')
98 except requests.exceptions.HTTPError as e:
99 print('\tError - '+ e)
100 print('\tFailure')
101
102# Check if Bazarr responds
103if settings['bazarr_hostname'] and settings['bazarr_port']:
104 print('Upcheck: Bazarr')
105 path = 'http://'+ settings['bazarr_hostname'] +':'+ settings['bazarr_port']
106 try:
107 print('\tChecking ping for '+ path)
108 req = requests.get(path)
109 if req.status_code == 200:
110 postData = {'api':settings['api'], 'bazarr_upcheck':'true'}
111 headers = {'content-type':'application/x-www-form-urlencoded'}
112 encoded = urllib.parse.urlencode(postData)
113 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
114 print('\tSent')
115 except requests.exceptions.HTTPError as e:
116 print('\tError - '+ e)
117 print('\tFailure')
118
119# Self upcheck
120if settings['self_upcheck_hostname']:
121 print('Upcheck: Self')
122 postData = {'api':settings['api'], 'hostname':settings['self_upcheck_hostname'], 'self_upcheck':'true'}
123 headers = {'content-type':'application/x-www-form-urlencoded'}
124 encoded = urllib.parse.urlencode(postData)
125 try:
126 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
127 print('\tSent')
128 except requests.exceptions.HTTPError as e:
129 print('\tError - '+ e)
130 print('\tFailure')
131
132# Network upchecker
133if settings['network_upcheck_hostname']:
134 print('Upcheck: Network')
135 hostnames = settings['network_upcheck_hostname'].split('|')
136 for hostname in hostnames:
137 hostnameParts = hostname.split(',')
138 ip = hostnameParts[1]
139 try:
140 down = 0
141 print('\tChecking ping for '+ hostnameParts[0])
142 response = subprocess.Popen('ping -4 -n 1 -w 5 ' + ip, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # -4 IPV4, -n run X times, -w MS waittime
143 for line in response.stdout:
144 if "timed out" in line.decode():
145 down = 1
146 if down == 1:
147 postData = {'api':settings['api'], 'host':ip, 'hostnames':settings['network_upcheck_hostname'], 'upcheck':'true'}
148 headers = {'content-type':'application/x-www-form-urlencoded'}
149 encoded = urllib.parse.urlencode(postData)
150 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
151 print('\tSent')
152 else:
153 print('\tHost is up, nothing sent')
154 except requests.exceptions as e:
155 print('\tError - '+ e)
156 print('\tFailure')
157
158exit()