Generate and send Nagios hosts and services reports using shell

– Nagios provide URL based report generating feature so I use this.
For Example




– we can view report using these URL.
To do this we have several steps

Generate report ( Using URL )
Save output ( Wget )
Convert output in PDF format  ( 3rd party utility of any open source )
Send report through mail ( mutt )


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

checked the required applications

[root@XXXXXXXX bn]# rpm -qa |grep wget
[root@XXXXXXXX bn]#

[root@XXXXXXXX bn]# lsof -i :25
master  2390 root   11u  IPv4   6843       TCP localhost.localdomain:smtp (LISTEN)
[root@XXXXXXXX bn]#

[root@XXXXXXXX bn]# /etc/init.d/nagios status
nagios (pid 6855) is running…
[root@XXXXXXXX bn]#

[root@XXXXXXXX bn]# rpm -qa |grep mutt
[root@XXXXXXXX bn]#

– All application installed except ”’MUTT”’ and ”’HTML to PDF converter”’ which is required to send mail with attachment
– Process to install mutt on nagios server

[root@XXXXXXXX bn]# yum install mutt
Loaded plugins: rhnplugin, security
There was an error communicating with RHN.
RHN Satellite or RHN Classic support will be disabled.
Error communicating with server. The message was:
Internal Server Error
epel                                                                                                               | 3.6 kB     00:00
epel/primary_db                                                                                                    | 3.9 MB     00:02
jenkins                                                                                                            |  951 B     00:00
rhel5.2                                                                                                            |  951 B     00:00
rpmforge                                                                                                           | 1.9 kB     00:00
Setting up Install Process
Resolving Dependencies
–> Running transaction check
—> Package mutt.x86_64 5: set to be updated
–> Finished Dependency Resolution

Dependencies Resolved

Package                    Arch                         Version                                      Repository                     Size
mutt                       x86_64                       5:                          rhel5.2                       1.2 M

Transaction Summary
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 1.2 M
Is this ok [y/N]: y
Downloading Packages:
mutt-                                                                                  | 1.2 MB     00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing     : mutt                                                                                                               1/1

mutt.x86_64 5:

[root@XXXXXXXX bn]#
– process to install html to pdf converter ( wkhtmltopdf )

– 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

After meeting the prerequisites, we can directly go with generating the reports through wget command.

wget -v –user nagios_user –password ‘password’ –no-check-certificate -O ./`date +%Y%m%d`-report.html “http://XXXXXXXX/nagios/cgi-bin/avail.cgi?show_log_entries=&hostgroup=all&timeperiod=lastweek”

wget -v –user nagios_user –password ‘password’ –no-check-certificate -O ./`date +%Y%m%d`-Web_Service_report.html  “http://XXXXXXXX/nagios/cgi-bin/avail.cgi?show_log_entries=&servicegroup=web-server-checks&timeperiod=yesterday”

– report download in current directory

-rw-r–r– 1 root root   134397 Dec 27 09:25 20131227-report.html
Now generate HTML page to PDF view

html2pdf -O Landscape 20131227-report.html 20131227-report.pdf

-rw-r–r– 1 root root   134397 Dec 27 09:25 20131227-report.html
-rw-r–r– 1 root root   156017 Dec 27 09:26 20131227-report.pdf

– we have following option for report

* 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

– Send report through mail

/usr/bin/mutt -s “[NAGIOS REPORT:ALL]” bibhuti.naraayan@XXXXXXcom -a `date +%Y%m%d`-report.pdf < /dev/null

mail reached in inbox.

– now create shell script to done all task automatic ( cron job )

To generate more attractive Report  use this URL

Change the value of report to 1,2,3,4 to generate more attractive Report

Leave a Reply