Wednesday, 7 August 2013

NagiosGraph – Graphs in Nagios on CentOS/RHEL 6.3

Nagiosgraph is a Nagios Addon that parses output and performance data from Nagios plugins and stores the data in RRD files. Nagiosgraph creates graphs and generates HTML pages with graphic reports from the data. This tutorial is tested on CentOS/RHEL 6.3, and have following Nagios core and plugin package:
   Nagios Core: nagios-4.0.0
   Nagios Plugin: nagios-plugins-1.4.16
To install Nagios please see my Nagios Installation Docs.

Installation Prerequisite

  • Nagios server in working condition.
  • We require following packages as well:
   yum -y install perl-GD php-gd rrdtool-perl rrdtool-php rrdtool perl-CGI perl-Time-HiRes

Installation

*) Create a directory where you would download nagios software:
   mkdir /usr/local/src
   cd /usr/local/src
*) Download nagiosgraph package/Untar it and navigate it to that directory
   wget http://downloads.sourceforge.net/project/nagiosgraph/nagiosgraph/1.4.4/nagiosgraph-1.4.4.tar.gz
   tar -xzvf nagiosgraph-1.4.4.tar.gz
   cd nagiosgraph-1.4.4
*) Check for any other Nagiosgraph dependency using "install.pl" script with "check-prereq" options:
   ./install.pl --check-prereq
   checking required PERL modules
     Carp...1.11
     CGI...3.51
     Data::Dumper...2.124
     File::Basename...2.77
     File::Find...1.14
     MIME::Base64...3.08
     POSIX...1.17
     RRDs...1.3008
     Time::HiRes...1.9721
   checking optional PERL modules
     GD...2.44
   checking nagios installation
     found nagios at /usr/local/nagios/bin/nagios
   checking web server installation
     found apache at /usr/sbin/httpd
Note: *) In case any dependency is missing we need to install it using cpan or yum command. *) You need to be root to run this script.

There are two ways to install nagiosgraph either automatically or manually.

1) Automated way which install file and set permission automatically
   ./install.pl --layout overlay --prefix /usr/local/nagios
   checking required PERL modules
     Carp...1.11
     CGI...3.51
     Data::Dumper...2.124
     File::Basename...2.77
     File::Find...1.14
     MIME::Base64...3.08
     POSIX...1.17
     RRDs...1.3008
     Time::HiRes... ***FAIL***
   checking optional PERL modules
     GD...2.44
   checking nagios installation
     found nagios at /usr/local/nagios/bin/nagios
   checking web server installation
     found apache at /usr/sbin/httpd
   Destination directory (prefix)? [/usr/local/nagios] 
   Location of configuration files (etc-dir)? [/usr/local/nagios/etc/nagiosgraph] 
   Location of executables? [/usr/local/nagios/libexec] 
   Location of CGI scripts? [/usr/local/nagios/sbin] 
   Location of documentation (doc-dir)? [/usr/local/nagios/docs/nagiosgraph] 
   Location of examples? [/usr/local/nagios/docs/nagiosgraph/examples] 
   Location of CSS and JavaScript files? [/usr/local/nagios/share] 
   Location of utilities? [/usr/local/nagios/docs/nagiosgraph/util] 
   Location of state files (var-dir)? [/var/nagios] 
   Location of RRD files? [/var/nagios/rrd] 
   Location of log files (log-dir)? [/var/nagios] 
   Path of log file? [/var/nagios/nagiosgraph.log] 
   Path of CGI log file? [/var/nagios/nagiosgraph-cgi.log] 
   URL of CGI scripts? [/nagios/cgi-bin] 
   URL of CSS file? [/nagios/nagiosgraph.css] 
   URL of JavaScript file? [/nagios/nagiosgraph.js] 
   Path of Nagios performance data file? [/tmp/perfdata.log] 
   URL of Nagios CGI scripts? [/nagios/cgi-bin] 
   username or userid of Nagios user? [nagios] 
   username or userid of web server user? [apache] 
   Modify the Nagios configuration? [n] 
   Modify the Apache configuration? [n] 
   configuration:
     ng_layout            overlay
     ng_prefix            /usr/local/nagios
     ng_etc_dir           /usr/local/nagios/etc/nagiosgraph
     ng_bin_dir           /usr/local/nagios/libexec
     ng_cgi_dir           /usr/local/nagios/sbin
     ng_doc_dir           /usr/local/nagios/docs/nagiosgraph
     ng_examples_dir      /usr/local/nagios/docs/nagiosgraph/examples
     ng_www_dir           /usr/local/nagios/share
     ng_util_dir          /usr/local/nagios/docs/nagiosgraph/util
     ng_var_dir           /var/nagios
     ng_rrd_dir           /var/nagios/rrd
     ng_log_dir           /var/nagios
     ng_log_file          /var/nagios/nagiosgraph.log
     ng_cgilog_file       /var/nagios/nagiosgraph-cgi.log
     ng_url               /nagios
     ng_cgi_url           /nagios/cgi-bin
     ng_css_url           /nagios/nagiosgraph.css
     ng_js_url            /nagios/nagiosgraph.js
     nagios_cgi_url       /nagios/cgi-bin
     nagios_perfdata_file /tmp/perfdata.log
     nagios_user          nagios
     www_user             apache
     modify_nagios_config n
     nagios_config_file   /usr/local/nagios/etc/nagios.cfg
     nagios_commands_file /usr/local/nagios/var/rw/nagios.cmd 
     modify_apache_config n
     apache_config_dir    /etc/httpd/conf.d
     apache_config_file   
   Continue with this configuration? [y] 
OR
2) Manual steps to install Nagiosgraph.
2.1) Create NagiosGraph directory and copy its configuration file in it:
   mkdir /usr/local/nagios/etc/nagiosgraph
   cp -vr etc /usr/local/nagios/etc/nagiosgraph/
2.2) Change NagiosGraph path in cgi and perl scripts:
   sed -i "s#/opt/nagiosgraph/etc#/usr/local/nagios/etc/nagiosgraph/#g" cgi/*cgi 
   sed -i "s#/opt/nagiosgraph/etc#/usr/local/nagios/etc/nagiosgraph/#g" lib/insert.pl
2.3) Copy cgi scripts to nagios appropriate directory that will be served by the web-server:
   cp cgi/*.cgi /usr/local/nagios/sbin
2.4) Copy insert.pl to nagios appropriate directory from which it can be executed:
   cp lib/insert.pl /usr/local/nagios/libexec
2.5) Copy CSS and JavaScript files to a nagios directory that would be served by the web server:
   cp share/nagiosgraph.css /usr/local/nagios/share
   cp share/nagiosgraph.js /usr/local/nagios/share
2.6) In NagiosGraph SSI file, set the URL for nagiosgraph.js,copy it to its appropriate location:
   vi share/nagiosgraph.ssi
   src="/nagiosgraph/nagiosgraph.js"   to    src="/nagios/nagiosgraph.js"

   cp share/nagiosgraph.ssi /usr/local/nagios/share/ssi/common-header.ssi
2.7) Replace the Nagios action icon with the nagiosgraph graph icon:
   cp share/graph.gif /usr/local/nagios/share/images/action.gif

Configuration

Edit Nagiosgraph configuration file and set at least the following parameters:
   vi /usr/local/nagios/etc/nagiosgraph/nagiosgraph.conf
    perflog           = /usr/local/nagios/var/perfdata.log
    rrddir            = /usr/local/nagios/var/rrd
    mapfile           = /usr/local/nagios/etc/nagiosgraph/map
    nagiosgraphcgiurl = /nagios/cgi-bin
    javascript        = /nagios/nagiosgraph.js
    stylesheet        = /nagios/nagiosgraph.css
    logfile           = /usr/local/nagios/var/nagiosgraph.log
    cgilogfile        = /usr/local/nagios/var/nagiosgraph-cgi.log

Creating necessary directory and fixing the permissions

Changing the ownership of conf and other necessary file that we have copyied earlier
   chown nagios:nagios /usr/local/nagios/etc/nagiosgraph
   chown nagios:nagios /usr/local/nagios/sbin
   chown nagios:nagios /usr/local/nagios/libexec/insert.pl
   chown nagios:nagios /usr/local/nagios/share/nagiosgraph.css  /usr/local/nagios/share/nagiosgraph.js
   chown nagios:nagios /usr/local/nagios/share/ssi/common-header.ssi
Created RRD directory and set permission of "rrddir" to be writable by *nagios* user and read by *apache* (webserver user).
   mkdir /usr/local/nagios/var/rrd
   chmod nagios:apache /usr/local/nagios/var/rrd
   chmod 755 /usr/local/nagios/var/rrd
Set writeable permissions to user *nagios* for “logfile” config directive:
   touch /usr/local/nagios/var/nagiosgraph.log
   chown nagios /usr/local/nagios/var/nagiosgraph.log
   chmod 644 /usr/local/nagios/var/nagiosgraph.log
Set writeable permissions to user *apache* for “cgilogfile” config directive:
   touch /usr/local/nagios/var/nagiosgraph-cgi.log
   chown apache /usr/local/nagios/var/nagiosgraph-cgi.log
   chmod 644 /usr/local/nagios/var/nagiosgraph-cgi.log
Make sure that the *nagios* user can create and delete perfdata files:

Nagios Configuration

Add the following line at the end of Nagios configuration file:
   vi /usr/local/nagios/etc/nagios.cfg

   # begin nagiosgraph configuration
   # process nagios performance data using nagiosgraph
   process_performance_data=1
   service_perfdata_file=/tmp/perfdata.log
   service_perfdata_file_template=$LASTSERVICECHECK$||$HOSTNAME$||$SERVICEDESC$||$SERVICEOUTPUT$||$SERVICEPERFDATA$
   service_perfdata_file_mode=a
   service_perfdata_file_processing_interval=30
   service_perfdata_file_processing_command=process-service-perfdata-for-nagiosgraph
   # end nagiosgraph configuration
In the Nagios commands file (commands.cfg) define the process-service-perfdata command at the end:
   vi /usr/local/nagios/etc/objects/commands.cfg
   # command to process nagios performance data for nagiosgraph
   define command {
      command_name process-service-perfdata-for-nagiosgraph
      command_line /usr/local/nagios/libexec/insert.pl
   }
Modify the Nagios sidebar (e.g. /usr/local/nagios/share/side.php) by inserting bullets under the 'Trends' heading:
<li><a href="<?php echo $cfg["cgi_base_url"];?>/trends.cgi" target="<?php echo $link_target;?>">Trends</a>
<ul>
<li><a href="<?php echo $cfg["cgi_base_url"];?>/show.cgi" target="<?php echo $link_target;?>">Graphs</a></li>

<li><a href="<?php echo $cfg["cgi_base_url"];?>/showhost.cgi" target="<?php echo $link_target;?>">Graphs by Host</a></li>
<li><a href="<?php echo $cfg["cgi_base_url"];?>/showservice.cgi" target="<?php echo $link_target;?>">Graphs by Service</a></li>

<li><a href="<?php echo $cfg["cgi_base_url"];?>/showgroup.cgi" target="<?php echo $link_target;?>">Graphs by Group</a></li>
</ul>
</li>

Validation and testing.

Verify Nagios configuration and restart it once every thing is ok
   /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
   /etc/init.d/nagios restart
Verify that nagiosgraph configuration is working by running showconfig.cgi:
   http://localhost/nagios/cgi-bin/showconfig.cgi
    OR 
http://Server-IP-Address/nagios/cgi-bin/showconfig.cgi
If everything is working up to this point, try graphing some data by running show.cgi:
   http://localhost/nagios/cgi-bin/show.cgi
    OR 
http://Server-IP-Address/nagios/cgi-bin/show.cgi
Note: If you get a 500 internal error and the issue is with Apache. If you get a “no rrd data found” error, then the configuration of nagiosgraph is incorrect.
To test if you configuration is working you can try to run the script directly:
   cd /usr/local/nagios/libexec
   ./insert.pl
To verify that there is some data in the RRD directory:
   ls /var/nagios/rrd/
   web001    web002 app001 app002 db001 db002

Adding Service to NagiosGraph

Now we need to define an action URL for the services that we want to graph, so that you can click to go directly to the graphs for that service from the Nagios Core web interface.
   define service {
      name  graphed-service
      action_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&period=week&rrdopts=-w+450+-j
    }
Then, you can have the services you want graphed inherit from it, as well as from any other templates they use, by adding "graphed-service" to the value for the use directive:
   define service {
 use generic-service,graphed-service
 host_name web001
 service_description PING
 check_command check_ping!100,10%!200,20%
}
You should do this for all the services for which you want graphing.
Validate the configuration file and reload the Nagios Core server once again:
   /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
   /etc/init.d/nagios reload
With this done, visiting the Service section of the web interface should include action icons after each graphed service.

19 comments:

  1. Hi Sachin,

    I have tried your post but got the below error when tried to run the command ./insert.pl in /usr/local/nagios/libexec/

    Can't locate RRDs.pm in @INC (@INC contains: /usr/local/nagios/etc/nagiosgraph /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at /usr/local/nagios/etc/nagiosgraph/ngshared.pm line 30.
    BEGIN failed--compilation aborted at /usr/local/nagios/etc/nagiosgraph/ngshared.pm line 30.
    Compilation failed in require at ./insert.pl line 35.
    BEGIN failed--compilation aborted at ./insert.pl line 35.

    Can you help me ?

    ReplyDelete
  2. Try installing rrdtool package using and retry it:

    Ubuntu
    sudo apt-get install rrdtool librrds-perl

    For CentOS/RHEL
    yum install rrdtool rrdtool-perl

    OR try to install RRD Module from CPAN site.
    perl -MCPAN -e 'install RRD::Simple'

    ReplyDelete
  3. Dear Sachin

    Thank you for sharing this nice article, after I followed up your instruction I am getting below error
    Can't locate ngshared.pm in @INC (@INC contains: /usr/local/nagios/etc/nagiosgraph/ /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at ./insert.pl line 35.
    BEGIN failed--compilation aborted at ./insert.pl line 35.

    then after run the command yum install rrdtool rrdtool-perl, showing its already installed
    updates/primary_db | 2.0 MB 00:05
    Setting up Install Process
    Package rrdtool-1.3.8-6.el6.x86_64 already installed and latest version
    Package rrdtool-perl-1.3.8-6.el6.x86_64 already installed and latest version
    Nothing to do

    please advice

    ReplyDelete
  4. Haneef,

    Try installing from CPAN.

    / # cpan
    Terminal does not support AddHistory.

    cpan shell -- CPAN exploration and modules installation (v1.9800)
    Enter 'h' for help.

    cpan[1]> install RRD::Simple

    also

    cpan[2]> install GD

    ReplyDelete
  5. Hello all,

    my configuration is running, but i see that the graph scale does not show the real values of the services that it monitors.
    for example it says that the memory being monitored by graph is on the range of KB.
    Any idea how can it be fixed?

    thanks.

    ReplyDelete
  6. I had tried below steps

    Install RRDTOOL using yum

    1- rpm -ivh http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
    2- yum install rrdtool -y

    ReplyDelete
  7. [root@tools nagiosgraph-1.4.4]# perl install.pl --check-prereq
    checking required PERL modules
    Carp...1.11
    CGI...4.04
    Data::Dumper...2.154
    File::Basename...2.77
    File::Find...1.14
    MIME::Base64...3.14
    POSIX...1.17
    RRDs...1.3001
    Time::HiRes...1.9721
    checking optional PERL modules
    GD... ***FAIL***
    checking nagios installation
    found nagios at /usr/local/nagios/bin/nagios
    checking web server installation
    found apache at /usr/sbin/httpd


    [root@tools nagiosgraph-1.4.4]# perl install.pl --check-prereq
    checking required PERL modules
    Carp...1.11
    CGI...4.04
    Data::Dumper...2.154
    File::Basename...2.77
    File::Find...1.14
    MIME::Base64...3.14
    POSIX...1.17
    RRDs...1.3001
    Time::HiRes...1.9721
    checking optional PERL modules
    GD... ***FAIL***
    checking nagios installation
    found nagios at /usr/local/nagios/bin/nagios
    checking web server installation
    found apache at /usr/sbin/httpd


    I am getting an error GD is up to date but I am getting any error GD... ***FAIL*** when I execute the scrip as perl install.pl --check-prereq. Can anyone help me to solve this issue it will highly appreciable. I install all the GD rpm's which are necessary for the GD graph.

    [root@tools nagiosgraph-1.4.4]# rpm -qa|grep gd
    gdm-2.30.4-52.el6.x86_64
    gd-devel-2.0.35-11.el6.x86_64
    eggdbus-0.6-3.el6.x86_64
    gdb-7.2-60.el6_4.1.x86_64
    gd-2.0.35-11.el6.x86_64
    libgdata-0.6.4-2.el6.x86_64
    gdm-user-switch-applet-2.30.4-52.el6.x86_64
    gdb-gdbserver-7.2-60.el6_4.1.x86_64
    plymouth-gdm-hooks-0.8.3-27.el6.x86_64
    gdm-plugin-smartcard-2.30.4-52.el6.x86_64
    gdbm-devel-1.8.0-36.el6.x86_64
    php-gd-5.3.3-26.el6.x86_64
    gdm-libs-2.30.4-52.el6.x86_64
    gdm-plugin-fingerprint-2.30.4-52.el6.x86_64
    gdbm-1.8.0-36.el6.x86_64
    pulseaudio-gdm-hooks-0.9.21-14.el6_3.x86_64
    gdisk-0.8.10-1.el6.x86_64
    [root@tools nagiosgraph-1.4.4]# rpm -qa|grep perl-gd
    [root@tools nagiosgraph-1.4.4]# rpm -qa|grep perl-GD
    perl-GDGraph-1.44-7.el6.noarch
    perl-GD-Barcode-1.15-6.el6.noarch
    perl-GDGraph3d-0.63-12.el6.noarch
    perl-GD-2.44-3.el6.x86_64
    perl-GDTextUtil-0.86-15.el6.noarch
    perl-GD-Graph3d-0.63-3.el6.noarch
    perl-GDGraph-histogram-1.1-1.sdl6.noarch
    [root@tools nagiosgraph-1.4.4]# rpm -qa|grep php-gd
    php-gd-5.3.3-26.el6.x86_64
    [root@tools nagiosgraph-1.4.4]#

    Please help me solve this issue.

    Thanks
    Shahabuddin

    ReplyDelete
  8. RRD is already installed there.

    [root@tools nagiosgraph-1.4.4]# rpm -qa | grep rrdtool
    perl-rrdtool-1.4.7-1.el5.rf.x86_64
    rrdtool-1.4.7-1.el5.rf.x86_64
    rrdtool-devel-1.4.7-1.el5.rf.x86_64
    [root@tools nagiosgraph-1.4.4]# rpm -qa | grep rrdutils
    rrdutils-5.2.1-1.el6.rf.noarch
    [root@tools nagiosgraph-1.4.4]#

    ReplyDelete
  9. GD is already installed by CPAN command.

    [root@tools nagiosgraph-1.4.4]# perl -MCPAN -e shell

    cpan shell -- CPAN exploration and modules installation (v2.10)
    Enter 'h' for help.

    cpan[1]> install GD::Graph
    CPAN: Storable loaded ok (v2.20)
    Reading '/root/.cpan/Metadata'
    Database was generated on Mon, 30 Mar 2015 06:17:02 GMT
    CPAN: Module::CoreList loaded ok (v2.18)
    GD::Graph is up to date (1.49).

    cpan[2]> exit
    Lockfile removed.

    ReplyDelete
  10. insert.pl scrip giving no error on executing.

    [root@tools nagiosgraph-1.4.4]# /usr/local/nagios/libexec/insert.pl
    [root@tools nagiosgraph-1.4.4]#

    ReplyDelete
  11. [root@Nagios1 rrd]# chmod nagios:apache /usr/local/nagios/var/rrd
    chmod: invalid mode: `nagios:apache'
    Try `chmod --help' for more information.

    ReplyDelete
  12. I have added the below to service.cfg file

    define service {
    name graphed-service
    action_url /nagiosgraph/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&period=week&rrdopts=-w+450+-j
    }

    but when i restart the nagios i have below error


    Error: Service has no hosts and/or service_description (config file '/usr/local/nagios/etc/services.cfg', starting on line 315)
    Error processing object config files!

    can you help me here



    ReplyDelete
    Replies
    1. You should add the following lines into templates.cfg file. Bty, you were missing the "register 0".

      define service{
      name nagiosgraph
      action_url /nagios/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagiosgraph/cgi-bin/showgraph.
      cgi?host=$HOSTNAME$&service=$SERVICEDESC$&period=week&rrdopts=-w+450+-j
      register 0
      }

      Hope this helps!

      Delete
  13. hai after adding the service

    (No output on stdout) stderr: i got the error. Kindly suggest

    I enter the service in nagiosql servicetemplates.cfg

    ReplyDelete
  14. Hi all,
    I have install the nagiosgraph in nagios... Its working fine...
    I am able to see the graph of those machines which are added in before the installation of nagiosgraph.
    so the problem is that the it wont add the host and services of the machines which I have deployed after the installation of the nagiosgraph

    ReplyDelete
  15. http://10..100./nagiosgraph/cgi-bin/show.cgi

    not The requested URL /nagiosgraph/cgi-bin/show.cgi was not found on this server.

    Apache/2.2.15 (CentOS) Server at 10.80.100.120 Port 80

    what could be the problem please help

    ReplyDelete
    Replies
    1. logs are generating in rrd tool but am not able to access from web kindly help

      Delete

  16. Well done! It is so well written and interactive. Keep writing such brilliant piece of work. Glad i came across this post. Last night even i saw similar wonderful DevOps tutorial on youtube so you can check that too for more detailed knowledge on DevOps.https://www.youtube.com/watch?v=4AJoRkjm998

    ReplyDelete