Skip to content

Instantly share code, notes, and snippets.

@fbeneventi
Forked from alexwright/status2json.py
Created September 8, 2022 20:50
Show Gist options
  • Save fbeneventi/2ca34c5cd3f30c85cfdbfb1ecde90a15 to your computer and use it in GitHub Desktop.
Save fbeneventi/2ca34c5cd3f30c85cfdbfb1ecde90a15 to your computer and use it in GitHub Desktop.
Parse the Nagios status.dat and poop out some JSON
#!/usr/bin/env python
import re
import json
STATUS_FILE_PATH = "/var/cache/nagios3/status.dat"
def read_status():
hosts = {}
services = {}
fh = open(STATUS_FILE_PATH)
status_raw = fh.read()
pattern = re.compile('([\w]+)\s+\{([\S\s]*?)\}',re.DOTALL)
matches = pattern.findall(status_raw)
for def_type, data in matches:
lines = [line.strip() for line in data.split("\n")]
pairs = [line.split("=", 1) for line in lines if line != '']
data = dict(pairs)
if def_type == "servicestatus":
services[data['service_description']] = data
if 'host_name' in data:
hosts[data['host_name']]['services'].append(data)
if def_type == "hoststatus":
data['services'] = []
hosts[data['host_name']] = data
return {
'hosts': hosts,
'services': services,
}
if __name__ == "__main__":
data = read_status()
print "Content-Type: application/json\n" # 1 \n because print does it's own
print json.dumps(data['hosts'], sort_keys=True, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment