forked from mairlist/streammonitor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_streammonitor.py
executable file
·66 lines (58 loc) · 3.92 KB
/
check_streammonitor.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
#!/usr/bin/env python3
# author Marcus Aßhauer <[email protected]>
# version: 0.1
# import needed functions
import urllib.request
import datetime
import json
import argparse
# function to get the JSON data from Streammonitor
def getResponse(url):
operUrl = urllib.request.urlopen(url)
if(operUrl.getcode()==200):
data = operUrl.read()
jsonData = json.loads(data)
else:
# UNKNOWN state if Streammonitor is not reachable
print("UNKOWN - Error receiving data", operUrl.getcode())
exit(3)
return jsonData
def main():
# argparser for handling commandline arguments
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--url", help="URL to streammonitor",required=True)
parser.add_argument("-c", "--crit", default=1800, type=int, help="seconds of silence after which silence gets critical. Default=1800",required=False)
parser.add_argument("-w", "--warn", default=0, type=int, help="seconds after reconnect the check should stay in WARN state. Default=0",required=False)
args = parser.parse_args()
jsonData = getResponse(args.url)
# generate more human readable time ranges for online-, offline-, and silenceDuration
# this will be only used in check output
if jsonData["onlineSince"]:
onlineSince=datetime.datetime.strptime(jsonData["onlineSince"], '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=datetime.timezone.utc).astimezone().strftime("%c")
if jsonData["offlineSince"]:
offlineSince=datetime.datetime.strptime(jsonData["offlineSince"], '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=datetime.timezone.utc).astimezone().strftime("%c")
if jsonData["silenceSince"]:
silenceSince=datetime.datetime.strptime(jsonData["silenceSince"], '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=datetime.timezone.utc).astimezone().strftime("%c")
# checking the state
if jsonData["status"] == "OK":
# if stream is silent and not longer than crit value make it WARN state
if jsonData["silenceDuration"] > 0 and jsonData["silenceDuration"] < args.crit:
print(f'WARN - Stream {jsonData["url"]} is connected but silent since {silenceSince} (h:m:s)!| silenceDuration={jsonData["silenceDuration"]}s;;{args.crit};;; onlineDuration={jsonData["onlineDuration"]}s;{args.warn};;;; offlineDuration={jsonData["offlineDuration"]}s;;;;;')
exit(1)
# if stream is silent longer than crit vaule make it CRIT state
elif jsonData["silenceDuration"] > args.crit:
print(f'CRIT - Stream {jsonData["url"]} is connected but silent since {silenceSince} (h:m:s)!| silenceDuration={jsonData["silenceDuration"]}s;;{args.crit};;; onlineDuration={jsonData["onlineDuration"]}s;{args.warn};;;; offlineDuration={jsonData["offlineDuration"]}s;;;;;')
exit(2)
# if stream is newly connected it will be WARN state until args.warn seconds
if jsonData["onlineDuration"] < args.warn:
print(f'WARN - Stream {jsonData["url"]} is connected but less than {args.warn} seconds!| silenceDuration={jsonData["silenceDuration"]}s;;{args.crit};;; onlineDuration={jsonData["onlineDuration"]}s;{args.warn};;;; offlineDuration={jsonData["offlineDuration"]}s;;;;;')
exit(1)
# if status ok and stream is not silent everything is OK
print(f'OK - Stream {jsonData["url"]} is connected and playing since {onlineSince} (h:m:s)| silenceDuration={jsonData["silenceDuration"]}s;;{args.crit};;; onlineDuration={jsonData["onlineDuration"]}s;{args.warn};;;; offlineDuration={jsonData["offlineDuration"]}s;;;;;')
exit(0)
else:
# if stream is not connected make it CRIT state
print(f'CRIT - Stream {jsonData["url"]} is not connected! since {offlineSince} (h:m:s)| silenceDuration={jsonData["silenceDuration"]}s;;{args.crit};;; onlineDuration={jsonData["onlineDuration"]}s;{args.warn};;;; offlineDuration={jsonData["offlineDuration"]}s;;;;;')
exit(2)
if __name__ == '__main__':
main()