Skip to content

Automated Nagios Report Generation and Send Mail with Mutt

A Small Project of Automated Report Generation and Send it over mail

In my environment, I have configured Nagios and MRTG monitoring tool on RHEL Server.

Requirement: My requirement is to take out some reports at fixed time form Nagios core and MRTG thrice a day and mailed  in PDF format.

Tools Used: Shell script, mutt and wkhtmltopdf.

1Install the below packages

We have used MRTG and Nagios core to monitor network and system resource utilization. Now the requirement is to send those reports in PDF format. As there is no save as option in MRTG and Nagios, so I chose “wkhtmltopdf” to convert those HTML pages to pdf format.

**. The below command will install the “wkhtmltopdf”, which will convert the html pages to pdf.

#apt-get install wkhtmltopdf

#which wkhtmltopdf
/usr/bin/wkhtmltopdf

**.apt-get install xvfb (virtual framebuffer X server)

** create a base directory for reports
#mkdir /root/Reports

2. Now my next requirement is daily automatic report generation part, for which I am using one small shell script. This script will create a directory daily, convert html pages to pdf and keeps those pdf files on that folder.

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
dName=`date +%d%m%Y`

if [ ! -d "/root/Reports/$dName" ]

then
echo "Directory doesn't exist. Creating Now"
mkdir /root/Reports/$dName
echo "Directory Created"
else
echo "Directory exist"
fi

xvfb-run -a /usr/bin/wkhtmltopdf --username nagiosadmin --password 1234567 https://192.168.0.20/nagios/cgi-bin/status.cgi?host=all /root/Reports/$dName/systemLoad-`date +%d-%b-%y-%T`.pdf
xvfb-run -a /usr/bin/wkhtmltopdf https://192.168.0.20/mrtg/192.168.0.1_4.html /root/Reports/$dName/Application-server.`date +%d-%b-%y-%T`pdf
xvfb-run -a /usr/bin/wkhtmltopdf https://192.168.0.20/mrtg/192.168.0.2_5.html /root/Reports/$dName/DataBase-Server.`date +%d-%b-%y-%T`.pdf

Now if I run the above script manually on, it will create a directory on selected path and place the converted pdf files on that directory.

3. Now my next requirement is to zip that directory and send it to my manager at the end of the day. For this I am using another shell script and mutt email client.

** Install mutt on the client system

#apt-get install mutt

#which mutt

/usr/bin/mutt

**Now for sending mail I am going to configure my gmail address with mutt.

a. Create below directory structure

#mkdir -p /root/.mutt/cache/bodies

#touch /root/.mutt/certificates

b. Create the mutt config file in root:

#pwd

#touch .muttrc

Add the below lines in the file

set imap_user = "your gmail address"
set imap_pass = "App spectfic Password"
set smtp_url = "smtp://<gmail-id>@smtp.gmail.com:587/"
set smtp_pass = "App specific Password"
set from = "your email address"
set realname = "manab saikia"

# Change the following line to a different editor you prefer.
set editor = "nano"

# Basic config, you can leave this as is
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set imap_check_subscribed
set hostname = gmail.com
set mail_check = 120
set timeout = 300
set imap_keepalive = 300
set postponed = "+[GMail]/Drafts"
set record = "+[GMail]/Sent Mail"
set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates
set move = no
set include
set sort = 'threads'
set sort_aux = 'reverse-last-date-received'
set auto_tag = yes

Note: To configure Gmail account in mutt client we need to to complete the following steps:

Enable two step verification

–Generate app specific password (App passwords allow 2-Step Verification users to access their Google Accounts through apps such as Mail on an iPhone or Mac, or Outlook etc)

( Follow google instruction to complete the above steps, and put the “App-specific password” in .muttrc file)

**Now open mutt and send one test mail for testing purpose.

#mutt

Now my next requirement is to zip the directory containing the pdf files and mail to different person. For that I use below shell script:

#!/bin/bash
cTime=`date +%k%M`
dName=`date +%d%m%Y`
baseDir=/root/Reports
if [ $cTime -ge 1820 ]
then
cd $baseDir
zFileName=`zip -r $dName.zip $dName`
echo "zip created"
echo $zFileName
echo "Hi Sir, please find the attached reports " | mutt -s "System Load report--`date +%d-%b-%Y`" abc@gmail.com -c agogoi@yahoo.co.in -c man.sai@abc.in -c sdas@gmail.com -a $baseDir/$dName.zip

else
exit
fi

Note: Next step is to place all the scripts on the CRON job and sit relax. As all the reports will generated automatically and mailed to respective person accordingly.