Categories
Linux Tips & Tutorials Monitoring

How to generate and send Nagios hosts and services reports through command line

OBJECTIVE

The objective of this how to is to setup nagios reporting through command line & enabling the cron jobs to send the reports in HTML format to email accounts specified.

PREREQUISITES

  • wget command should be installed
  • Mail server on localhost must be working
  • Nagios Web Interface Username & Password should be known
  • mutt command should be installed in order to send HTML attachments on mail
  • Basic knowledge of report generation through nagios web interface

STEPS

  • After meeting the prerequisites, we can directly go with generating the reports through wget command. In below example we are generating a report of a host service Current Load for last 31 days. It is assumed here that nagios web interface is accessible through HTTPs otherwise there is no need to put –no-check-certificate in below command.
/usr/bin/wget -v --user USERNAME --password 'PASSWORD' --no-check-certificate -O ./`date +%Y%m%d`-report.html https://Nagios-Server-IP/nagios/cgi-bin/avail.cgi?host=HOSTNAME.server&service=Current+Load&show_log_entries&timeperiod=last31days"

Above command will generate report in the format YYYYMMDD-report.html

  • Another example, where we are generating availability report for all services of all hosts. Timeperiod assumption is yesterday here.
/usr/bin/wget -v --user USERNAME --password 'PASSWORD' --no-check-certificate -O ./`date +%Y%m%d`-report.html "https://Nagios-Server-IP/nagios/cgi-bin/avail.cgi?show_log_entries=&host=HOSTNAME&service=all&timeperiod=yesterday&timeperiod=24x7"

NOTE: You need to replace USERNAME, PASSWORD with real Nagios Web Interface Credentials
HOSTNAME is host name of host for which you want to generate Nagios Report

  • Now we can send the generate HTML report as attachment on any email
/usr/bin/mutt -s "[NAGIOS REPORT:HOSTNAME] [email protected] -a YYYYMMDD-report.html < /dev/null

 

LIST OF DEFINED TIMEPERIODS

Most of the times it gets hard to guess the exact URL for generation of a report with our desired time interval because CGI URLs are not visible in browser's address bar when you generate Nagios reports from web interface, so below is a list of defined timeperiods which we can use to customize our reports

* today        => Today
* last24hours  => Last 24 hours
* yesterday    => Yesterday
* thisweek     => This week
* last7days    => Last 7 days
* lastweek     => Last week
* thismonth    => This month
* last31days   => Last 31 days
* lastmonth    => Last month (default)
* thisyear     => This year
* lastyear     => Last year

 

OPTIONAL

Some of us would like to convert the HTML report into PDF format to make it more pleasing to eyes of receiver. So here are the steps to convert this report to PDF format

 

STEPS

  • Download wkhtmltopdf source code
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
  • Untar it & place it in /bin folder with easy name
tar xvf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /bin/html2pdf
  • Now generate HTML page to PDF in Landscape view
html2pdf -O Landscape 20131010-report.html 20131010-report.pdf
  • To see all options supported by wkhtmltopdf
html2pdf -h

By Sushant Chawla

Over the past 11 years I have acquired very good knowledge of:

- Cloud Setup on Amazon, Rackspace with autoscaling, load balancers, etc
- LAMP architecture
- LNMP Architecture
- Apache, Nginx, Lighttpd
- Databases: MySQL and PostgreSQL
- Bash Scripting
- Configuration Management with Puppet
- Monitoring: Nagios, Zabbix, ICINGA
- Mail servers: Sendmail, Postfix, Exim
- Web Accelerators & Caching Setups with Memcache, Varnish & Redis
- Setup and Management of high availability applications clusters
- LDAP, Samba, NFS, FTP, DNS, DHCP, Openfire, Squid Proxy
- Subversion(SVN), GIT
- Control Panels: Plesk, Webmin, cPanel

Apart from Linux, I am a great sports star & regular traveller. I like working out and exploring various places.

Cheers ;-)