Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for new API_Key requirements #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
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

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
Expand Down
32 changes: 20 additions & 12 deletions check_pihole.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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',
Expand All @@ -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
Expand All @@ -51,20 +55,24 @@ 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
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 - " + \
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"])
"|queries=" + str(url_output["dns_queries_all_types"]) + " blocked=" + str(url_output["ads_blocked_today"]) + " clients=" + str(url_output['unique_clients'])
gtfo(exitcode, message)


Expand Down