· 5 years ago · Dec 31, 2020, 02:22 AM
1import urllib
2import requests
3from requests.exceptions import ConnectionError
4import json
5import subprocess
6
7#
8# Sonarr, Radarr, Self: The server runs a cron every 10 minutes and checks the last time the script was ran on your end. If it has been > 10 minutes it will send a notification
9# Network: If anything is sent to the server, it will send a notification
10# If you only want to use specific ones, do not fill in the settings for the ones you dont
11#
12# None of the *arr information in the settings or the /api/v3/system/status is posted to the notifier
13#
14
15settings = {
16 'api':'ffe34443-a313-4540-84ce-1a989c01810b', # discordnotifier.com api key
17 'radarr_api':'595dccecd98e4ad0b0c6cf6ca0848ec4', # API key for Radarr, found in Settings > General
18 'radarr_hostname':'10.1.0.63', # Ex: localhost or 10.1.0.2, this is the hostname for Radarr
19 'radarr_port':'7878',
20 'sonarr_api':'589fedd08fee4821a420a71353ac6bba', # API key for Sonarr, found in Settings > General
21 'sonarr_hostname':'10.1.0.63', # Ex: localhost or 10.1.0.3, this is the hostname for Sonarr
22 'sonarr_port':'8989',
23 'readarr_api':'782d62a46cd0415ab4f4e21ae79eab5a', # API key for Readarr, found in Settings > General
24 'readarr_hostname':'10.1.0.63', # Ex: localhost or 10.1.0.3, this is the hostname for Readarr
25 'readarr_port':'8787',
26 'lidarr_api':'', # API key for Lidarr, found in Settings > General
27 'lidarr_hostname':'10.1.0.63', # Ex: localhost or 10.1.0.3, this is the hostname for Lidarr
28 'lidarr_port':'8686',
29 'bazarr_hostname':'10.1.0.63', # Ex: localhost or 10.1.0.4, this is the hostname for Bazarr
30 'bazarr_port':'6767',
31 'self_upcheck_hostname':'plex', # Ex: plex, this is the name you want to see in the notification
32 'network_upcheck_hostname':'plex,10.1.0.6|torrents,10.1.0.63', # Ex: plex,10.1.0.1|radarr,10.1.0.2|sonarr,10.1.0.3
33}
34
35# Check if Sonarr responds
36if settings['sonarr_api'] and settings['sonarr_hostname'] and settings['sonarr_port']:
37 print('Upcheck: Sonarr')
38 headers = {'X-Api-Key':settings['sonarr_api'], 'X-Requested-With':'XMLHttpRequest'}
39 path = 'http://'+ settings['sonarr_hostname'] +':'+ settings['sonarr_port'] +'/api/v3/system/status'
40 try:
41 print('\tChecking API')
42 req = requests.get(path, headers=headers)
43 response = json.loads(req.text)
44 if len(response['version']) > 0:
45 postData = {'api':settings['api'], 'sonarr_upcheck':'true'}
46 headers = {'content-type':'application/x-www-form-urlencoded'}
47 encoded = urllib.parse.urlencode(postData)
48 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
49 print('\tSent')
50 except ConnectionError:
51 print('\tError - Invalid host:port setting')
52 print('\tFailure')
53 except KeyError:
54 print('\tError - Invalid API key')
55 print('\tFailure')
56 except requests.exceptions.HTTPError as e:
57 print('\tError - '+ e)
58 print('\tFailure')
59
60# Check if Radarr responds
61if settings['radarr_api'] and settings['radarr_hostname'] and settings['radarr_port']:
62 print('Upcheck: Radarr')
63 headers = {'X-Api-Key':settings['radarr_api'], 'X-Requested-With':'XMLHttpRequest'}
64 path = 'http://'+ settings['radarr_hostname'] +':'+ settings['radarr_port'] +'/api/v3/system/status'
65 try:
66 print('\tChecking API')
67 req = requests.get(path, headers=headers)
68 response = json.loads(req.text)
69 if len(response['version']) > 0:
70 postData = {'api':settings['api'], 'radarr_upcheck':'true'}
71 headers = {'content-type':'application/x-www-form-urlencoded'}
72 encoded = urllib.parse.urlencode(postData)
73 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
74 print('\tSent')
75 except ConnectionError:
76 print('\tError - Invalid host:port setting')
77 print('\tFailure')
78 except KeyError:
79 print('\tError - Invalid API key')
80 print('\tFailure')
81 except requests.exceptions.HTTPError as e:
82 print('\tError - '+ e)
83 print('\tFailure')
84
85# Check if Readarr responds
86if settings['readarr_api'] and settings['readarr_hostname'] and settings['readarr_port']:
87 print('Upcheck: Readarr')
88 headers = {'X-Api-Key':settings['readarr_api'], 'X-Requested-With':'XMLHttpRequest'}
89 path = 'http://'+ settings['readarr_hostname'] +':'+ settings['readarr_port'] +'/api/v1/system/status'
90 try:
91 print('\tChecking API')
92 req = requests.get(path, headers=headers)
93 response = json.loads(req.text)
94 if len(response['version']) > 0:
95 postData = {'api':settings['api'], 'readarr_upcheck':'true'}
96 headers = {'content-type':'application/x-www-form-urlencoded'}
97 encoded = urllib.parse.urlencode(postData)
98 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
99 print('\tSent')
100 except ConnectionError:
101 print('\tError - Invalid host:port setting')
102 print('\tFailure')
103 except KeyError:
104 print('\tError - Invalid API key')
105 print('\tFailure')
106 except requests.exceptions.HTTPError as e:
107 print('\tError - '+ e)
108 print('\tFailure')
109
110# Check if Lidarr responds
111if settings['lidarr_api'] and settings['lidarr_hostname'] and settings['lidarr_port']:
112 print('Upcheck: Lidarr')
113 headers = {'X-Api-Key':settings['lidarr_api'], 'X-Requested-With':'XMLHttpRequest'}
114 path = 'http://'+ settings['lidarr_hostname'] +':'+ settings['lidarr_port'] +'/api/v1/system/status'
115 try:
116 print('\tChecking API')
117 req = requests.get(path, headers=headers)
118 response = json.loads(req.text)
119 if len(response['version']) > 0:
120 postData = {'api':settings['api'], 'lidarr_upcheck':'true'}
121 headers = {'content-type':'application/x-www-form-urlencoded'}
122 encoded = urllib.parse.urlencode(postData)
123 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
124 print('\tSent')
125 except ConnectionError:
126 print('\tError - Invalid host:port setting')
127 print('\tFailure')
128 except KeyError:
129 print('\tError - Invalid API key')
130 print('\tFailure')
131 except requests.exceptions.HTTPError as e:
132 print('\tError - '+ e)
133 print('\tFailure')
134
135# Check if Bazarr responds
136if settings['bazarr_hostname'] and settings['bazarr_port']:
137 print('Upcheck: Bazarr')
138 path = 'http://'+ settings['bazarr_hostname'] +':'+ settings['bazarr_port']
139 try:
140 print('\tChecking ping for '+ path)
141 req = requests.get(path)
142 if req.status_code == 200:
143 postData = {'api':settings['api'], 'bazarr_upcheck':'true'}
144 headers = {'content-type':'application/x-www-form-urlencoded'}
145 encoded = urllib.parse.urlencode(postData)
146 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
147 print('\tSent')
148 except requests.exceptions.HTTPError as e:
149 print('\tError - '+ e)
150 print('\tFailure')
151
152# Self upcheck
153if settings['self_upcheck_hostname']:
154 print('Upcheck: Self')
155 postData = {'api':settings['api'], 'hostname':settings['self_upcheck_hostname'], 'self_upcheck':'true'}
156 headers = {'content-type':'application/x-www-form-urlencoded'}
157 encoded = urllib.parse.urlencode(postData)
158 try:
159 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
160 print('\tSent')
161 except requests.exceptions.HTTPError as e:
162 print('\tError - '+ e)
163 print('\tFailure')
164
165# Network upchecker
166if settings['network_upcheck_hostname']:
167 print('Upcheck: Network')
168 hostnames = settings['network_upcheck_hostname'].split('|')
169 for hostname in hostnames:
170 hostnameParts = hostname.split(',')
171 ip = hostnameParts[1]
172 try:
173 down = 0
174 print('\tChecking ping for '+ hostnameParts[0])
175 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
176 for line in response.stdout:
177 if "timed out" in line.decode():
178 down = 1
179 if down == 1:
180 postData = {'api':settings['api'], 'host':ip, 'hostnames':settings['network_upcheck_hostname'], 'upcheck':'true'}
181 headers = {'content-type':'application/x-www-form-urlencoded'}
182 encoded = urllib.parse.urlencode(postData)
183 req = requests.post('https://discordnotifier.com/notifier.php', data=encoded, headers=headers)
184 print('\tSent')
185 else:
186 print('\tHost is up, nothing sent')
187 except requests.exceptions as e:
188 print('\tError - '+ e)
189 print('\tFailure')
190
191exit()