-
Notifications
You must be signed in to change notification settings - Fork 6
/
check_pihole.py
executable file
·72 lines (58 loc) · 2.55 KB
/
check_pihole.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
#!/usr/bin/env python3
import argparse
import json
import urllib3
__author__ = 'Dmytro Prokhorenkov'
__version__ = '0.2.1'
EXIT_STATUS = {
0: "OK",
1: "WARNING",
2: "CRITICAL",
3: "UNKNOWN"
}
def parse_args():
argp = argparse.ArgumentParser(add_help=True,
description='Check Pi-hole status',
epilog='{0}: v.{1} by {2}'.format('check_pihole.py', __version__, __author__))
argp.add_argument('-H', '--host', type=str, help="Pi-hole ip address or hostname", required=True)
argp.add_argument('-P', '--port', type=int, help="Port number for Pi-Hole web UI", default=80)
argp.add_argument('-C', '--status_critical', dest='pihole_status',
help="Forces CRITICAL when Pi-hole is disabled", action='store_true')
argp.add_argument('-W', '--status_warning', dest='pihole_status',
help="Forces WARNING when Pi-hole is disabled", action='store_false')
argp.add_argument('-t', '--timeout', default=10, type=int, help='Timeout for request. Default 10 seconds')
argp.set_defaults(pihole_status=False)
args = argp.parse_args()
return args
def gtfo(exitcode, message=''):
if message:
print(''.join([EXIT_STATUS[exitcode], ":", " ", message]))
exit(exitcode)
def check_pihole(host, port, _timeout):
status_url = 'http://' + host + ('' if port == 80 else ":"+str(port)) + '/admin/api.php?summaryRaw'
try:
request = urllib3.PoolManager()
content = request.request('GET', status_url, timeout=_timeout)
decoded = json.loads(content.data.decode('utf8'))
return 0, decoded
except Exception:
return 2, "Problems with accessing API: Check if server is running."
def main():
args = parse_args()
exitcode, url_output = check_pihole(args.host, args.port, args.timeout)
message = ""
if exitcode == 2:
message = url_output
else:
if url_output["status"] != "enabled":
if args.pihole_status:
exitcode = 2
else:
exitcode = 1
message = message + "Pi-hole is " + url_output["status"] + ": queries today - " + \
str(url_output["dns_queries_all_types"]) + ", domains blocked: " + str(url_output["ads_blocked_today"]) + \
", percentage blocked: " + str(url_output["ads_percentage_today"]) + \
"|queries=" + str(url_output["dns_queries_all_types"]) + " blocked=" + str(url_output["ads_blocked_today"])
gtfo(exitcode, message)
if __name__ == '__main__':
main()