From b3cd056b9d043a5db557ea9a8480ee9edd3f7f13 Mon Sep 17 00:00:00 2001 From: eternalliving <70250520+eternalliving@users.noreply.github.com> Date: Sun, 15 Jan 2023 09:08:13 -0800 Subject: [PATCH 1/5] Pi-hole 5.20 now requires API Token Included auth and ssl args for API calling. SSL is not required but useful if you are making these calls over the internet to prevent your auth key being exposed. Added settings on urllib3 to allow for self-signed certs without having to specify location. --- check_pihole.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/check_pihole.py b/check_pihole.py index 012485e..6bcadee 100755 --- a/check_pihole.py +++ b/check_pihole.py @@ -4,9 +4,10 @@ import json import urllib3 +urllib3.disable_warnings() __author__ = 'Dmytro Prokhorenkov' -__version__ = '0.2.1' +__version__ = '0.3.0' EXIT_STATUS = { 0: "OK", @@ -22,6 +23,8 @@ def parse_args(): 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('-A', '--auth', type=str, help="API Auth Key", required=True) + argp.add_argument('-S', '--secure', help="Use ssl for connection", action='store_true') 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', @@ -38,10 +41,11 @@ def gtfo(exitcode, message=''): exit(exitcode) -def check_pihole(host, port, _timeout): - status_url = 'http://' + host + ('' if port == 80 else ":"+str(port)) + '/admin/api.php?summaryRaw' +def check_pihole(host, port, auth, secure, _timeout): + status_url = 'http' + ('s' if secure else '') + '://' + host + ('' if port == 80 else ":"+str(port)) + '/admin/api.php?summaryRaw&auth=' + auth try: - request = urllib3.PoolManager() + cert_reqs = 'CERT_NONE' if secure else '' + request = urllib3.PoolManager(cert_reqs=cert_reqs) content = request.request('GET', status_url, timeout=_timeout) decoded = json.loads(content.data.decode('utf8')) return 0, decoded @@ -51,7 +55,7 @@ def check_pihole(host, port, _timeout): def main(): args = parse_args() - exitcode, url_output = check_pihole(args.host, args.port, args.timeout) + exitcode, url_output = check_pihole(args.host, args.port, args.auth, args.secure, args.timeout) message = "" if exitcode == 2: message = url_output @@ -64,7 +68,7 @@ def main(): 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"]) + "|queries=" + str(url_output["dns_queries_all_types"]) + " blocked=" + str(url_output["ads_blocked_today"]) + " clients=" + str(url_output['unique_clients']) gtfo(exitcode, message) From 14b6a350f73b8f06e4f94a8361efd33ab77cd0da Mon Sep 17 00:00:00 2001 From: eternalliving <70250520+eternalliving@users.noreply.github.com> Date: Sun, 15 Jan 2023 09:26:06 -0800 Subject: [PATCH 2/5] Update check_pihole.py --- check_pihole.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/check_pihole.py b/check_pihole.py index 6bcadee..58bcfc4 100755 --- a/check_pihole.py +++ b/check_pihole.py @@ -63,6 +63,9 @@ def main(): if url_output["status"] != "enabled": if args.pihole_status: exitcode = 2 + if len(url_output) == 0: + exitcode = 3 + message = "Empty result from Pihole. Wrong or no API token?" else: exitcode = 1 message = message + "Pi-hole is " + url_output["status"] + ": queries today - " + \ From 95ebace8a6b76c1fdfaf5023870ec2dd2fd5b1e4 Mon Sep 17 00:00:00 2001 From: eternalliving <70250520+eternalliving@users.noreply.github.com> Date: Sun, 15 Jan 2023 09:33:57 -0800 Subject: [PATCH 3/5] Fixed my bad commit Tried to add @wopfel patch without fulling reading it. I really need to slow down and look around :S --- check_pihole.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/check_pihole.py b/check_pihole.py index 58bcfc4..6813625 100755 --- a/check_pihole.py +++ b/check_pihole.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3.7 import argparse import json @@ -60,15 +60,16 @@ def main(): if exitcode == 2: message = url_output else: - if url_output["status"] != "enabled": - if args.pihole_status: - exitcode = 2 - if len(url_output) == 0: - exitcode = 3 - message = "Empty result from Pihole. Wrong or no API token?" - else: - exitcode = 1 - message = message + "Pi-hole is " + url_output["status"] + ": queries today - " + \ + if len(url_output) == 0: + exitcode = 3 + message = "Empty result from Pihole. Wrong API token?" + 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"]) + " clients=" + str(url_output['unique_clients']) From 6e472762d9b58eccb1a8ee317c6e270dae7b4c23 Mon Sep 17 00:00:00 2001 From: eternalliving <70250520+eternalliving@users.noreply.github.com> Date: Sun, 15 Jan 2023 09:36:45 -0800 Subject: [PATCH 4/5] Removed .7 for python3 env This is getting embarrassing.... --- check_pihole.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_pihole.py b/check_pihole.py index 6813625..00c3261 100755 --- a/check_pihole.py +++ b/check_pihole.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3.7 +#!/usr/bin/env python3 import argparse import json From 3f71564f598d0b728da180593dd8c8f7ae0d7cc0 Mon Sep 17 00:00:00 2001 From: eternalliving <70250520+eternalliving@users.noreply.github.com> Date: Sun, 15 Jan 2023 09:41:44 -0800 Subject: [PATCH 5/5] Updated readme to reflect new changes --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 45f871b..caa3611 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Icinga2/Nagios plugin to check Pi-hole status. ```bash -usage: check_pihole.py [-h] -H HOST [-P PORT] [-C] [-W] [-t TIMEOUT] +usage: check_pihole.py [-h] -H HOST [-P PORT] [-A API_KEY] [-S] [-C] [-W] [-t TIMEOUT] Check Pi-hole status @@ -11,6 +11,9 @@ optional arguments: -h, --help show this help message and exit -H HOST, --host HOST Pi-hole ip address or hostname -P PORT, --port PORT Port number for Pi-Hole web UI + -A API_KEY, --auth API_KEY API + Token for backend access + -S, --secure Use ssl for API access -C, --status_critical Forces CRITICAL when Pi-hole is disabled -W, --status_warning Forces WARNING when Pi-hole is disabled