Parsing Nagios4 status.dat to JSON

Posted by Michael Palmer on Saturday, August 20, 2022

Background

I have a django app that shows host links to internal employees, wanted to grab status from Nagios without having to go to a bunch of trouble screen scraping. Found this quick script from Alex Wright, and modified it to work where I can call it from a Django function, then I just loop through my models and match the json object to the model.

Steps to deploy

Upload file: status2json.py below in github script link to your Nagios4 server. You can put this in your /usr/lib/cgi-bin/nagios4 directory and chmod +x status2json.py

The above folder is password protected so you would need to authenticate.

You should add a new user to your /etc/nagios4/htdigest.users file. htdigest -c htdigest.users Nagios4 apicall

You might need to add .py to your http.conf cgi handlers if you use apache.

Django Code

Django Function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def getNagiosHostStatus():
    nagiospass = 'you-forgot-about-dre-1999'
    nagiosuser = 'apicall'
    from requests.auth import HTTPDigestAuth
    try:
        r = requests.get('https://nagios.mysite.com/nagios4/cgi-bin/status2json.py', auth=HTTPDigestAuth(nagiosuser,nagiospass))
        data = r.json()
    except:
        data = None

    return(data)

nagios2jason.py on GitHub


comments powered by Disqus