Skip to content

Instantly share code, notes, and snippets.

@flagranterror
Created April 10, 2013 19:40
Show Gist options
  • Save flagranterror/5357783 to your computer and use it in GitHub Desktop.
Save flagranterror/5357783 to your computer and use it in GitHub Desktop.
Pull alarm stats from nagios web UI.
#!/usr/bin/python
import urllib2
from base64 import b64encode
import bs4
import time
hostname = 'nagioshost.example.tld'
nagios_prefix = '/nagios'
proto = 'http'
user = 'naguser'
password = 'nagpass'
try:
now = time.strftime('%Y-%m-%d - %I:%M:%S', time.localtime())
url = "%s://%s/%s/cgi-bin/status.cgi?host=all" % (proto,hostname,nagios_prefix)
authstring = "%s %s" % ('Basic',b64encode("%s:%s" % (user,password)))
req = urllib2.Request(url)
req.add_header('Authorization', authstring)
res = urllib2.urlopen(req).read()
soup = bs4.BeautifulSoup(res)
hostup = hostdown = hostprob = 0
svcok = svcwarn = svccrit = 0
for td in soup.find_all('td'):
if td.get('class'):
if td.get('class')[0] == u'hostTotalsUP':
hostup = td.get_text()
elif td.get('class')[0] == u'hostTotalsDOWN':
hostdown = td.get_text()
elif td.get('class')[0] == u'hostTotalsPROBLEMS':
hostprob = td.get_text()
if td.get('class')[0] == u'serviceTotalsOK':
svcok = td.get_text()
elif td.get('class')[0] == u'serviceTotalsWARNING':
svcwarn = td.get_text()
elif td.get('class')[0] == u'serviceTotalsCRITICAL':
svccrit = td.get_text()
print """
Nagios:
Hosts:
UP: %s
DOWN: %s
PROBLEM: %s
Services:
OK: %s
WARN: %s
CRIT: %s
As of %s
""" % (hostup,hostdown,hostprob,svcok,svcwarn,svccrit,now)
except urllib2.URLError, e:
print "Could not open URL: %s" % e.reason
except:
print "An unknown error occurred."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment