Generate and send Nagios hosts and services reports using shell

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

http://XXXXX/nagios/cgi-bin/avail.cgi?show_log_entries=&hostgroup=all&timeperiod=lastweek

http://XXX/nagios/cgi-bin/avail.cgi?show_log_entries=&host=Host1&service=all&timeperiod=yesterday&timeperiod=24×7

http://XXXXXXXX/nagios/cgi-bin/avail.cgi?show_log_entries=&host=Host1&service=all&timeperiod=lastweek

.
.
.
– 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 )

PREREQUISITES on server

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
wget-1.11.4-2.el5_4.1
[root@XXXXXXXX bn]#

[root@XXXXXXXX bn]# lsof -i :25
COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
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:1.4.2.2-3.0.2.el5 set to be updated
–> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================
Package                    Arch                         Version                                      Repository                     Size
==========================================================================================================================================
Installing:
mutt                       x86_64                       5:1.4.2.2-3.0.2.el5                          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.4.2.2-3.0.2.el5.x86_64.rpm                                                                                  | 1.2 MB     00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing     : mutt                                                                                                               1/1

Installed:
mutt.x86_64 5:1.4.2.2-3.0.2.el5

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

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

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”

Web_Service_report:-
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
http://XXXXXXXX/nagios//cgi-bin/summary.cgi?report=1&displaytype=4&timeperiod=today&hostgroup=all

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

Leave a Reply