Send nagios report as pdf file via email

The tutorial will give you idea, how you can send Nagios Report as pdf file via email. We will achieve this requirement, with the help of script and using wkhtmltopdf command line tool. We will set the Nagios Report script in crontab and send it via email to receiver person/team.

In our previous post we shared, how we can convert website page into pdf. Logically we will use same thing here.

But the main part of script is how we can get the Nagios report URL. It is quite easy to get any Nagios Report URL from Web browser. You have to simply Right Click on your desired Report Section of Nagios Dashbaord, then from right click mouse menu ,select new tab or window. Now check address bar of newly opened tab in web browser , you will see long Nagios Report URL.

nagios-report

Steps to setup the Nagios Report script

1. Install wkhtmltopdf :

We will suggest you to follow the our previous post to install wkhtmltopdf command in your CentOS/Debian/Ubuntu system.

2. Create nagios_report script :

Once you install wkhtmltopdf . Create a script to send Nagios report pdf file through email.

In this script, we will get Nagios Event Log report in the form of pdf file, which is one day old.

(a) Install mutt :

With the help of mutt we will send the email .Hence install mutt in your system first.

   For CentOS / RHEL
    sudo yum install mutt

    For Ubuntu / Debian
    sudo apt-get install mutt

(b) Create a file nagios_report.sh

vi /opt/nagios_report.sh

(c) In nagios_report.sh file, write the following content. You can also do copy paste from below given script content.

Change the variable value like _USER, _PASSWORD,_TO_EMAIL etc. as per your requirement or information.
Here, _URL is the Nagios Report URL.

#!/bin/bash
#
# Author : Sharad Kumar Chhetri
# Date : 21-aug-2015
# Version : 1.0
# Description : The script will fetch the nagios eventlog report one day old.
#
## 

_URL="http://localhost/nagios/cgi-bin/showlog.cgi?archive=1"
_USER=nagios_username
_PASSWORD=Your_Nagios_password
_REPORT_PATH=/opt/NagiosReport
_FILE_NAME=Nagios-EventLog-`date +%F --date="yesterday"`.pdf
_TO_EMAIL=your@email.com
_WKHTMLTOPDF=`which wkhtmltopdf`

if [ -d $_REPORT_PATH ]
then
echo "NagiosReport directory already exist in /opt "
else
mkdir -p $_REPORT_PATH
echo $(ls -ld $_REPORT_PATH)
echo "$_REPORT_PATH directory created"
fi

### Create pdf file of Nagios Event Log , Set the --page-height and --page-width as per your requirement.
$_WKHTMLTOPDF --username $_USER --password $_PASSWORD   --page-height 1200 --page-width 900 "$_URL" "$_REPORT_PATH/$_FILE_NAME"

### Send Email with attachment
echo -e "Hello Team,nFind Nagios report dated of $(date +%F --date=yesterday), attachment is enclosed in this emailnBest RegardsnNagios Admin"|mutt -a "$_REPORT_PATH/$_FILE_NAME" -s "Report: Nagios Event Log  $(date +%F --date=yesterday)" -- $_TO_EMAIL

(d) Give executable permission nagios_report.sh script :

chmod 700 /opt/nagios_report.sh

Run the script manually from terminal to check.

sh /opt/nagios_report.sh

(e) Crontab setting :

As per your requirement now set the crontab for script. We are setting the script to be executed daily at 12:05 AM .

crontab -e
05 00 * * * /opt/nagios_report.sh

You can use this method for all types of report you generate on Nagios Dashboard. Logic is very simple, we just convert the website page into pdf file and send it through email.

10 thoughts on “Send nagios report as pdf file via email”

  1. Hello,

    Here is my error:

    [root@central-nagios opt]# ./nagios_report.sh
    NagiosReport directory already exist in /opt
    ./nagios_report.sh: line 29: You: command not found
    Can’t stat /opt/NagiosReport/Nagios-EventLog-2022-12-27.pdf: No such file or directory
    /opt/NagiosReport/Nagios-EventLog-2022-12-27.pdf: unable to attach file.

    The nagios_report.sh file is as shown below.
    =====================================================
    # Description : The script will fetch the nagios eventlog report one day old.
    #
    ##

    _URL=”http://localhost/nagios/cgi-bin/showlog.cgi?archive=1″
    _USER=nagiosadmin
    _PASSWORD=H3ll0#123
    _REPORT_PATH=/opt/NagiosReport
    _FILE_NAME=Nagios-EventLog-`date +%F –date=”yesterday”`.pdf
    _TO_EMAIL=nvblue@xyz.com
    #_WKHTMLTOPDF=`which wkhtmltopdf`
    _WKHTMLTOPDF=`xvfb-run /usr/bin/wkhtmltopdf`

    if [ -d $_REPORT_PATH ]
    then
    echo “NagiosReport directory already exist in /opt ”
    else
    mkdir -p $_REPORT_PATH
    echo $(ls -ld $_REPORT_PATH)
    echo “$_REPORT_PATH directory created”
    fi

    ### Create pdf file of Nagios Event Log , Set the –page-height and –page-width as per your requirement.
    $_WKHTMLTOPDF –username $_USER –password $_PASSWORD –page-height 1200 –page-width 900 “$_URL” “$_REPORT_PATH/$_FILE_NAME”

    ### Send Email with attachment
    echo -e “Hello Team,nFind Nagios report dated of $(date +%F –date=yesterday), attachment is enclosed in this emailnBest RegardsnNagios Admin”|mutt -a “$_REPORT_PATH/$_FILE_NAME” -s “Report: Nagios Event Log $(date +%F –date=yesterday)” — $_TO_EMAIL

    =====================================================

    Please Help…

  2. I followed the steps. reports seems to be fine but mails i not getting triggered.

    error:
    Dec 6 16:12:49 nagios postfix/pickup[1345779]: 1E2956005D: uid=0 from=
    Dec 6 16:12:49 nagios postfix/cleanup[1351376]: 1E2956005D: message-id=
    Dec 6 16:12:49 nagios postfix/qmgr[1772]: 1E2956005D: from=, size=67572, nrcpt=1 (queue active)
    nagios postfix/error[1351382]: 1E2956005D: to=****************
    postfix/bounce[1351383]: 1E2956005D: sender non-delivery notification: 244E466463
    Dec 6 16:12:49 nagios postfix/qmgr[1772]: 244E466463: from=, size=2235, nrcpt=1 (queue active)
    Dec 6 16:12:49 nagios postfix/qmgr[1772]: 1E2956005D: removed
    Dec 6 16:12:49 nagios postfix/local[1351384]: 244E466463: to=, relay=local, delay=0.01, delays=0/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
    Dec 6 16:12:49 nagios postfix/qmgr[1772]: 244E466463: removed

  3. Error getting when using nagios report script in crontab

    NagiosReport directory already exist in /opt
    /opt/test.sh: line 28: –username: command not found
    Mar 15 16:17:01 uhcindia sendEmail[33425]: ERROR => The attachment [/opt/NagiosReport/Nagios-EventLog-2017-03-14.pdf] doesn’t exist!
    Mar 15 16:17:01 uhcindia sendEmail[33425]: HINT => Try specifying the full path to the file or reading extended help with “–help message”

    • Hi Rajendra,

      Change the variable value like _USER, _PASSWORD,_TO_EMAIL etc. as per your requirement or information in script.
      Here, _URL is the Nagios Report URL.

      Regards
      sharad

  4. Great work !!! Worked…

    Could you please let me know, how can I configure to sent a mail to internal DL (Group mail)..

Comments are closed.