Pages

Saturday, April 6, 2013

Adding Nagiosgraph to Nagios

This article is an update to my prior released article about installing Nagios in Slackware.
Nagiosgraph allows you to add very nice graphs to your Nagios which is extremely helpful. With these graphs you'll be able to look into the past a little and probably do some trending for the future. You can act before it is too late.
Before you can begin to install Nagiosgraph you need an additional package rrdtool. First create a directory where you can store the sources and change into it: 

# mkdir -p /usr/src/nagios && cd /usr/src/nagios

Then download rrdtool and Nagiosgraph:

# wget -c "http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz"
# wget -c "http://downloads.sourceforge.net/project/nagiosgraph/nagiosgraph/1.4.4/nagiosgraph-1.4.4.tar.gz"


Extract the rrdtool package and change into the new directory:

# tar xf rrdtool-1.4.7.tar.gz
# cd rrdtool-1.4.7


Run configure, make and make install to compile and install rrdtool. If you're on a 32bit system then ignore the libdir option:

# ./configure --prefix=/usr --libdir=/usr/lib64 --with-perl-options='INSTALLDIRS=vendor'
...
# make
...
# make install
...


When rrdtool is installed then run the following perl command to check if the rrdtool perl module was build and installed correct:

# perl -e 'use RRDs; print "OK\n";'
OK


Now you can begin to install Nagiosgraph it self. First extract the source package and change into the new directory:

# cd /usr/src/nagios
# tar xf nagiosgraph-1.4.4.tar.gz
# cd nagiosgraph-1.4.4


Then create the Makefile with perl and run make:

# perl Makefile.PL
...
# make
...


Beofre you continue: note that I've changed some pathes below. These settings are for my Nagios installation. If you use this instruction on your very own Nagios installation then take care of the following settings:

# make install
...
Destination directory (prefix)? [/usr/local/nagiosgraph] /opt/nagios/latest/nagiosgraph
Location of configuration files (etc-dir)? [/opt/nagios/latest/nagiosgraph/etc]
Location of executables? [/opt/nagios/latest/nagiosgraph/bin]
Location of CGI scripts? [/opt/nagios/latest/nagiosgraph/cgi] /opt/nagios/latest/sbin
Location of documentation (doc-dir)? [/opt/nagios/latest/nagiosgraph/doc]
Location of examples? [/opt/nagios/latest/nagiosgraph/examples]
Location of CSS and JavaScript files? [/opt/nagios/latest/nagiosgraph/share] /opt/nagios/latest/share/nagiosgraph
Location of utilities? [/opt/nagios/latest/nagiosgraph/util]
Location of state files (var-dir)? [/opt/nagios/latest/nagiosgraph/var]
Location of RRD files? [/opt/nagios/latest/nagiosgraph/var/rrd]
Location of log files (log-dir)? [/opt/nagios/latest/nagiosgraph/var]
Path of log file? [/opt/nagios/latest/nagiosgraph/var/nagiosgraph.log]
Path of CGI log file? [/opt/nagios/latest/nagiosgraph/var/nagiosgraph-cgi.log]
URL of CGI scripts? [/nagiosgraph/cgi-bin] /nagios/cgi-bin
URL of CSS file? [/nagiosgraph/nagiosgraph.css] /nagios/share/nagiosgraph
URL of JavaScript file? [/nagiosgraph/nagiosgraph.js] /nagios/share/nagiosgraph
Path of Nagios performance data file? [/tmp/perfdata.log] /opt/nagios/latest/var/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]
...
Continue with this configuration? [y]
...


Whan Nagiosgraph is installed then change the user permissions for the nagios user:

# chown -R nagios:nagios /opt/nagios/latest/nagiosgraph

Next configure Nagios to process performance data:

# su - nagios
$ vi /opt/nagios/latest/etc/nagios.cfg
...
#process_performance_data=0
process_performance_data=1
service_perfdata_file=/opt/nagios/latest/var/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
...


Add insert.pl to commands.cfg (remove/comment out prior process-service-perfdata command definition):

$ vi /opt/nagios/latest/etc/objects/commands.cfg
...
# 'process-service-perfdata' command definition
#define command{
#       command_name    process-service-perfdata
#       command_line    /usr/bin/printf "%b" "$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEATTEMPT$\t$SERVICESTATETYPE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SE
RVICEOUTPUT$\t$SERVICEPERFDATA$\n" >> /opt/nagios/3.5.0/var/service-perfdata.out
#       }

define command {
        command_name  process-service-perfdata
        command_line  /opt/nagios/latest/nagiosgraph/bin/insert.pl
}


Next configure Nagiosgraph and correct the path to the perfdata.log file:

$ cd /opt/nagios/latest/nagiosgraph/etc
$ vi nagiosgraph.log
...
perfdata = /opt/nagios/latest/var/perfdata.log
...


You can restart Nagios now:

# /etc/rc.d/rc.nagios restart
...


After a few minutes a new directory should be available for storing the rrd performance data:

# ls /opt/nagios/latest/nagiosgraph/var/rrd
localhost
...
# ls /opt/nagios/latest/nagiosgraph/var/rrd/localhost
...


Before you can access these performance data you need to configure Nagios again. The graphed-service template will add a small graph icon to each service (if enabled):

# su - nagios
$ cd /opt/nagios/latest/etc/objects
$ vi templates.cfg
...
define service {
        name graphed-service
        action_url                      /nagios/cgi-bin/show.cgi?host=$HOSTNAME$&service=$SERVICEDESC$' onMouseOver='showGraphPopup(this)' onMouseOut='hideGraphPopup()' rel='/nagios/cgi-bin/showgraph.cgi?host=$HOSTNAME$&service=$SERVICEDESC$&period=week&rrdopts=-w+450+-j

        register 0
        }


You can enable the graph icon by adding the above template to each service for each host, eg. for the PING service it could look like this:


$ cd /opt/nagios/latest/etc/objects
$ vi localhost.cfg
...
define service{
        use                    local-service,graphed-service
        service_description    PING
...


When you restart Nagios again:

# /etc/rc.d/nagios restart

Then a little graph icon will be available for the PING service for the host localhost. When you click on it then a new page should open with some graphs to watch (it could take some minutes before enough data is gathered).

Some errors I bumped into:

1. Cannot write to nagiosgraph-cgi.log

The error.log for Apache show the following entry:

$ less /var/log/http/error.log
...
Sat Apr  6 12:51:47 2013 showgraph.cgi critical Cannot write to '/opt/nagios/latest/nagiosgraph/var/nagiosgraph-cgi.log', using STDERR instead, referer: http://xbmc/nagios/cgi-bin/show.cgi?host=localhost&service=Current%20Load
...


Change permissions to the nagiosgraph-cgi.log file so that every user in the group nagios can access that file. This implies that the apache user is in the nagios group:

# chmod 664 /opt/nagios/latest/nagiosgraph/var/nagiosgraph-cgi.log
# ls -la /opt/nagios/latest/nagiosgraph/var/nagiosgraph-cgi.log
-rw-rw-r-- 1 nagios nagios 0 Apr  6 11:43 /opt/nagios/latest/nagiosgraph/var/nagiosgraph-cgi.log


2. nagiosgraph.js is not installed or wrong version.

When you open any graph then you might be confronted with the following message:

nagiosgraph.js is not installed or wrong version.

To fix this error check nagiosgraph.conf first:

$ cd /opt/nagios/latest/nagiosgraph/etc
$ less nagiosgraph.conf
...
# JavaScript: URL to the nagiosgraph javascript file.
javascript = /nagios/share/nagiosgraph
# Stylesheet: URL to the nagiosgraph stylesheet.
stylesheet = /nagios/share/nagiosgraph
...


Then check the above path (note that share is the path for you web files), eg:

$ ls -la /opt/nagios/latest/share/nagiosgraph/
...
-rw-r--r--  1 nagios nagios  2656 Apr  6 12:20 nagiosgraph.css
-rw-r--r--  1 nagios nagios 40944 Apr  6 12:20 nagiosgraph.js


Try to open the js or css file in your browser. For me the URL http://mon01/nagios/share/nagiosgraph/nagiosgraph.js failed but the following url http://mon01/nagios/nagiosgraph/nagiosgraph.js worked. Check the apapche logs and the path to these files until you get the correct URL.
When you have found the correct URL then adjust the above path in the nagiosgraph.conf file and add the file to the path for the javascript and css file:

$ cd /opt/nagios/latest/nagiosgraph/etc
$ vi nagiosgraph.conf
...
# JavaScript: URL to the nagiosgraph javascript file.
# javascript = /nagios/share/nagiosgraph
javascript = /nagios/nagiosgraph/nagiosgraph.js
# Stylesheet: URL to the nagiosgraph stylesheet.
# stylesheet = /nagios/share/nagiosgraph
stylesheet = /nagios/nagiosgraph/nagiosgraph.css
...


The error shoudn't appear anymore when you open a page with some graphs.