Skip to content

Instantly share code, notes, and snippets.

@DanielBorgesOliveira
Created August 21, 2022 03:50
Show Gist options
  • Save DanielBorgesOliveira/44697868d246fe1a81fec2b3c6e520b7 to your computer and use it in GitHub Desktop.
Save DanielBorgesOliveira/44697868d246fe1a81fec2b3c6e520b7 to your computer and use it in GitHub Desktop.
Nagios plugin to monitor if the zoneminder monitors are online.
#!/usr/bin/python3
# https://zoneminder.readthedocs.io/en/stable/api.html
# https://www.digitalocean.com/community/tutorials/how-to-create-nagios-plugins-with-python-on-ubuntu-12-10
import requests
import json
import sys
import argparse
parser = argparse.ArgumentParser(description='Plugin for monitoring zoneminder status monitors.')
parser.add_argument(
'-H',
'--IP',
help='IP to be used in the API call. E.g. --IP=10.0.1.13',
type=str,
required=True,
)
parser.add_argument(
'-u',
'--USER',
help='User to be used in the API call. E.g. --USER=admin',
type=str,
required=True,
)
parser.add_argument(
'-p,',
'--PASSWORD',
help='Password to be used in the API call. E.g. --PASSWORD=Abcd1234',
type=str,
required=True,
)
args = parser.parse_args()
IP = args.IP
USER = args.USER
PASSWORD = args.PASSWORD
response = requests.post("http://"+str(IP)+"/zm/api/host/login.json", data = {"user": USER, "pass": PASSWORD})
access_token = response.json()['access_token']
response = requests.get("http://"+str(IP)+"/zm/api/monitors.json", params = {"token": access_token})
Monitors = len(response.json()['monitors'])
OnlineMonitors = ""
OfflineMonitors = ""
DisabledMonitors = ""
# Return codes expected by Nagios
# 0 OK
# 1 WARNING
# 2 CRITICAL
# 3 UNKNOWN
ReturnCode = 0
for monitor in response.json()['monitors']:
if not monitor["Monitor_Status"]["Status"] and monitor["Monitor"]["Enabled"] == "1":
OfflineMonitors += str(monitor["Monitor"]["Id"])+", "
ReturnCode = 2
elif not monitor["Monitor_Status"]["Status"] and monitor["Monitor"]["Enabled"] == "0":
DisabledMonitors += str(monitor["Monitor"]["Id"])+", "
elif monitor["Monitor_Status"]["Status"] and monitor["Monitor"]["Enabled"] == "1":
OnlineMonitors += str(monitor["Monitor"]["Id"])+", "
OfflineMonitors = OfflineMonitors[:-2]
DisabledMonitors = DisabledMonitors[:-2]
OnlineMonitors = OnlineMonitors[:-2]
ReturnMessage = ""
if OnlineMonitors:
ReturnMessage += "Online Monitors IDs: "+str(OnlineMonitors)+". "
if OfflineMonitors:
ReturnMessage += "Offline Monitors IDs: "+str(OfflineMonitors)+". "
if DisabledMonitors:
ReturnMessage += "Disabled Monitors IDs: "+str(DisabledMonitors)+". "
if ReturnCode == 0:
ReturnMessage = "OKAY - "+ ReturnMessage
elif ReturnCode == 1:
ReturnMessage = "WARNING - "+ ReturnMessage
elif ReturnCode == 2:
ReturnMessage = "CRITICAL - "+ ReturnMessage
elif ReturnCode == 3:
ReturnMessage = "UKNOWN - "+ ReturnMessage
#print(json.dumps(response.json(), indent=4, sort_keys=True))
print(ReturnMessage)
sys.exit(ReturnCode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment