Nagios SLA tool

Ulf Karlsson karlsson.ulf at gmail.com
Tue Jun 23 12:10:54 CEST 2009


Hi,

The crude script that I use is attached. I have set up cron to send
reports every Sunday morning. The script picks out some parts of the
Nagios HTML page generated by avail.cgi, i.e. the table with the
availability report, and data like the time span, and puts this into a
new HTML template that it then sends it by email.

I hope there will be a nicer solution available soon integrated with
Merlin and Ninja - that SLA reporting that Andreas pointed to is
really nice but it was not really packaged for use outside op5 last
time I checked the Git repository a week ago or so.

I'd like to thank op5 for all the good work that everyone will profit
from. I hope that we will have the opportunity to give something back
to op5 at some point.

/Ulf

#!/usr/bin/python

from optparse import OptionParser
from string import Template
import smtplib
import sys
import pycurl
import tempfile
from pyquery import PyQuery as pquery
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import tidy


nagios_user = "nagios"
nagios_pass = "yourpassword"

base = "http://nagios.example.com/nagios3/cgi-bin/"

# Email config
bcc_addrs = []
from_addr = "root at nagios.example.com"
smtp_server = "localhost"

url_lastweek_template =
Template("http://localhost/nagios3/cgi-bin/avail.cgi?show_log_entries=&hostgroup=$hostgroup&timeperiod=lastweek")

html_template = Template("""
<html>
<head>
<style type="text/css">
<!--
$style
-->
</style>
<base href="$base" />
</head>
<body class="avail">
$report_range
$report_duration
$data_title
$data
<div>
For any period for when the NMS does not know the state of the object,
it is reported as being in an undetermined state. This may happen when
an object has been added recently. The numbers in parenthesis are
adjusted for when objects have been in an undetermined state.
</div>

</body>
</html>
""")

# Command line parsing
parser = OptionParser()
parser.add_option('--hostgroup')
parser.add_option('--to', dest='to_addr')
parser.add_option('--from', dest='from_addr')
parser.add_option('--bcc', dest='bcc_addrs')
(options, args) = parser.parse_args()

# Making sure all mandatory options appeared.

mandatories = ['hostgroup', 'to_addr']
for m in mandatories:
   if not options.__dict__[m]:
       print "mandatory option is missing\n"
       parser.print_help()
       exit(-1)

to_addr = options.to_addr
hostgroup = options.hostgroup

if options.__dict__['from_addr']:
   from_addr = options.from_addr

if options.__dict__['bcc_addrs']:
   bcc_addrs = options.bcc_addrs.split(':')

url = url_lastweek_template.safe_substitute(dict(hostgroup=options.hostgroup))

file = tempfile.TemporaryFile()

c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.WRITEFUNCTION, file.write)
c.setopt(pycurl.USERPWD, "%s:%s" % (nagios_user, nagios_pass))
c.perform()


file.seek(0)
contents = file.read()

doc = pquery(contents)

# Select the relevant parts of the HTML

report_range = doc('div.reportRange')
report_duration = doc('div.reportDuration')
data_title = doc('div.dataTitle')
data = doc('table.data')

subject = "Report for %s (%s)" % (hostgroup, report_range.text())

# Include CSS

style = ""
css = open('/etc/nagios3/stylesheets/common.css')
style += css.read()
css = open('/etc/nagios3/stylesheets/avail.css')
style += css.read()

msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to_addr
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject

html_subs = dict(hostgroup=hostgroup,
           base=base,
           style=style,
           report_range=report_range,
           report_duration=report_duration,
           data_title=data_title,
           data=data)

html = html_template.safe_substitute(html_subs)

# Tidy HTML to reduce line length etc (postfix has 990 characters line
length limit)

tidy_options = dict(output_xhtml=0,
                   indent=1,
                   tidy_mark=0)

tidy_html = tidy.parseString(html, **tidy_options)

# print tidy_html.get_errors()

msg.attach(MIMEText(str(tidy_html), 'html'))

server = smtplib.SMTP(smtp_server)
server.sendmail(from_addr, to_addr, msg.as_string())
for bcc_addr in bcc_addrs:
   server.sendmail(from_addr, bcc_addr, msg)
server.quit()

file.close()

------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list