Nagios Statistics Templates

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

Kleborp
Posts: 1
Joined: Sat Aug 15, 2009 4:07 am

Nagios Statistics Templates

Post by Kleborp »

Hello Everyone, a few days ago I was looking for some good templates to monitor Nagios in Cacti, and was disappointed when I couldn't find much.

Maybe I didn't look hard enough because a lot of people run both Cacti and Nagios so something must be out there, but I wasn't able to track down something nice and simple.

Eventually I used the script written by crony in this topic and changed it quite a bit to suite my needs.

It can check Nagios servers remotely, both version 2 and 3, graphs hosts, services, latency and check stats (active/passive service/host checks per 5 minutes).

So I'd hereby like to share my scripts and templates with the community. I'm open for suggestions, questions and will try to help troubleshoot any issues you might have.

I'm also planning support for Nagios v1 and might add some more graphs such as scheduling statistics if there's a demand for it.

Code: Select all

Nagios Statistics - Cacti scripts and templates

============
Info
============
Simple system for Cacti to graph Nagios statistics.

It was grown from a script made by 'crony' on the cacti forums: http://forums.cacti.net/post-141347.html#141347
Since then heavily modified, turned into a CGI script and extended with Cacti wrapper for remote checking

With templates created from scratch:
- Nagios Statistics - Check Statistics  - Shows 'per 5 minutes' statistics of amount of checks executed/received
- Nagios Statistics - Host Checks 	- Shows how many hosts are being checked and their status
- Nagios Statistics - Host Problems 	- Shows how many problems there are for hosts
- Nagios Statistics - Latency 		- Shows the average latency for host/service checks
- Nagios Statistics - Service Checks 	- Shows how many services are being checked and their status
- Nagios Statistics - Service Problems	- Shows how many problems there are for services

Created by Kleborp, feel free to use whatever you want for your own purposes

============
Requirements
============
- Cacti 0.8.7d or higher (created with 0.8.7d and also tested with 0.8.7e)
- Nagios v2.xx or v3.x.x (tested with nagios 2.12, 3.1.2 and 3.2.0)
- Perl on both Cacti and Nagios server
- Wget on the Cacti server

============
Files
============
- mrtgstats.cgi - CGI Perl script to be placed on the to be checked Nagios server
- check_nagios.pl - Wrapper Cacti script that passes the output of the CGI script to Cacti
- *.xml - Template files to be imported in Cacti
- README - This file

============
Installation
============
CGI Nagios script:
- Copy 'mrtgstats.cgi' to your nagios server
- Copy it to nagios's cgi-bin directory as root ('/usr/lib/nagios/cgi-bin/' on RHEL/CentOS/Fedora)
- Change ownership and permissions (see ' ls -al' in the cig-bin directory for refence, it should be the same as the rest (root:root or nagios:nagios and +x probably))
- Test it by going to 'http://your-nagios/nagios/cgi-bin/mrtgstats.cgi?type=latency'
- If it shows no/weird output or an internal server error:
  - Make sure the 'nagiostats' binary location is configured properly in mrtgstats.cgi
  - Make sure it didn't accidentally copy as dos text file, which breaks cgi in some http servers like lighttpd (run 'dos2unix mrtgstats.cgi' to be sure)
  - If it still doesn't work, panic, or post on the forums with some details including nagios version, apache/lighttpd access/error logs, etc

Cacti wrapper script:
- Copy 'check_nagios.pl' to your cacti/scripts/ directory
- Make sure it's executable by the user your cacti/spine runs as

Import templates:
- For each .xml file included, import it through the 'Import Templates' bit

Create pretty graphs:
- Add the Graph Templates to a host (preferably your nagios sever, though it technically doesn't matter as you have to enter the cgi-bin url anyway)
- When creating the graphs for a host it'll show 3 input fields per graph:
 - 'Nagios cgi-bin url' this is the cgi-bin url of nagios, not the full url to mrtgstats.cgi, for example: http://your-nagios/nagios/cgi-bin/
 - 'Web Auth username' Optional field, fill this in if your nagios requires web authentication
 - 'Web Auth password' Optional field, same as above, note that creating an account that doesn't have admin access in nagios itself is preferable (like a view only user)

============
Bugs
============
Some known bugs:
- After reloading the nagios configuration it might show inaccurate data (less hosts/checks/downtime/etc than there actually are), I believe this is a 'bug' in nagiostats
- The 'Service Problems' and 'Host Problems' graphs can use the same data source as 'Service Checks' and 'Host Checks', however by default Cacti will create a data source for each graph created, to 'solve' this, only add the Service Checks and Host Checks graph templates to a hoste, after creating those, manually do 'Add' in 'Graph Management', select your host, and template, and after creating that select the right data source fields.
- Latency times are rounded to 2 decimals, even though nagiostats gives it in microseconds, I cheated by dividing it by 1000 and using 'Load Average' as GPRINT function so I wouldn't have to make my own with 3 decimals

============
Changelog
============

* Sat 15/08/2009 - Intial release
Attachments
nacti.tar.gz
Scripts + Templates
(17.9 KiB) Downloaded 1507 times
Graphs of a Nagios server that receives mostly passive check results
Graphs of a Nagios server that receives mostly passive check results
nacti-1.png (109.98 KiB) Viewed 25368 times
Graphs of a Nagios server that does active checks
Graphs of a Nagios server that does active checks
nacti-2.png (117.89 KiB) Viewed 25368 times
User avatar
BSOD2600
Cacti Moderator
Posts: 12171
Joined: Sat May 08, 2004 12:44 pm
Location: USA

Post by BSOD2600 »

Looks very nice.

Please consider adding them to http://docs.cacti.net/templates
crony
Posts: 9
Joined: Wed Jul 30, 2008 11:08 am

Post by crony »

very cool.
taschi
Posts: 1
Joined: Tue Feb 24, 2009 3:40 am
Location: Austria

Post by taschi »

hi,

this template looks really good, but it doesn't work for me.

when i check the template with browser or cacti-server-cli i get this error:

Nagios version '' not supported..

my nagios-version is 3.0.6

mrtgstats.cgi on the nagiosserver-cli works fine.

any idea's

thx

taschi
Gilles.29
Posts: 15
Joined: Tue Aug 11, 2009 9:27 am

Post by Gilles.29 »

A small evolution to accept https session

check_nagios.pl

Code: Select all

#!/usr/bin/perl -w
use strict;

my $results;
my $url = $ARGV[0];
$url =~ s/\/+$//; # Remove trailing slash if needed 
my $type = $ARGV[1];
my $cmd = "wget --no-check-certificat -qO- ";
if($ARGV[2] && $ARGV[3]) {
        $cmd .= "--http-user=$ARGV[2] --http-passwd=$ARGV[3] ";
}
$cmd .= "$url/mrtgstats.cgi?type=$type";
#print "URL: $cmd\n";
$results = `$cmd`;
$results =~ s/^\s+|\s+$//g;
print "$results\n";
Thanks to Fabrice JSI Lens
hayou
Posts: 5
Joined: Wed Jan 13, 2010 4:13 am
Contact:

thanks

Post by hayou »

thanks Kleborp,
works for me.
nagios 3.2.0 + cacti Version 0.8.7e on the same server
hayou
Posts: 5
Joined: Wed Jan 13, 2010 4:13 am
Contact:

Post by hayou »

@ taschi :
I have the same problem, but only with CentOS.

I have install my linux Debian and I don't have any problems.
eschoeller
Cacti User
Posts: 234
Joined: Mon Dec 13, 2004 3:03 pm

Post by eschoeller »

@ hayou, taschi ... et al.

Check to make sure the user running your apache server also has permission to run the nagiostats binary. On my debian install I had to 'chgrp www-data nagiostats' to get this working.

The code doesn't really indicate that this type of problem is occurring well, afaic. It appropriately checks to make sure the nagiostats binary is there, but doesn't check that the user the webserver is running as can execute it. The mrtgstats.cgi then silently continues until it hits the next exception, where it barfs because it never got the correct version of nagios - hence the reason why the message you get says version '' not supported.

My fix for this:

Code: Select all

# Does the nagiostat binary even exist?
if ( ! -x $nagiostat) {
        print "Specified Nagios binary and/or configuration files does not exist! Or is not executable!\n";
        exit;
}
You get the idea .... a '-x' file check is really the same as a -e except it verifies that it's executable by the user running mrtgstats.cgi.

Hope this helps y'all

Overall, I'm very excited about this plugin. Looks very very good. There is a chance I might port this to an SNMP check. This would avoid having to rely on wget, HTTPS, users etc ... We'd just have an snmp agent that runs on your nagios server and cacti would poll the snmp OIDs.

Good Work!
hayou
Posts: 5
Joined: Wed Jan 13, 2010 4:13 am
Contact:

Post by hayou »

hey eschoeller :

I edit the mrtgstats.cgi and add you few lines command.

Then I type :
# chmod 775 /usr/local/nagios/bin/nagiostats

Finally, It works !

Thanks a lot :D :wink:
locutus233
Posts: 14
Joined: Tue Mar 17, 2009 2:49 pm

Post by locutus233 »

I modified this cgi to support icinga. My changes are mostly just a hack though, but enjoy.

Code: Select all

#!/usr/bin/perl

# CGI stuff, useful for param()
use CGI qw/:standard/;
use strict;

# Needed header printage for CGI scripts
print "Content-type: text/html\n\n";

my $request_type;

# Location of the nagiostat binary
my $nagiostat = '/usr/local/icinga/bin/icingastats';

# Does the nagiostat binary even exist?
if ( ! -e $nagiostat) {
        print "Specified Nagios binary and/or configuration files does not exist!\n";
        exit;
}

# Get nagios version
my $nagiosver = `$nagiostat -d ICINGAVERSION -m`;

# Extract major version (first number of $nagiosver)
my ($nagiosmajorver) = ($nagiosver =~ m/^([0-9])/g);

# Do we have a param?
if(param()) {
        $request_type = param('type');
}

# Param didn't exist or was empty, oh dear!
if($request_type eq "") {
        print "No type parameter given, add '?type=latency' to the URL, for example!\n";
        exit;
}

# Associate graph tyes with fields from nagiostat
my %graphs = (  'latency'       => ['AVGACTSVCLAT', 'AVGACTSVCEXT','AVGACTHSTLAT','AVGACTHSTEXT'],
                'servicechecks' => ['NUMSERVICES','NUMSVCOK','NUMSVCWARN','NUMSVCUNKN','NUMSVCCRIT','NUMSVCPROB','NUMSVCDOWNTIME'],
                'hostchecks'    => ['NUMHOSTS','NUMHSTUP','NUMHSTDOWN','NUMHSTUNR','NUMHSTPROB','NUMHSTDOWNTIME']
 );

# Silly nagios devs thought it'd be a good idea to rename some fields, you know, for compatbility sake *ahem*

if($nagiosmajorver == 1) {
        $graphs{'checkstats'} = ['NUMHSTACTCHK5M','NUMSVCACTCHK5M','NUMHSTPSVCHK5M','NUMSVCPSVCHK5M'];
} elsif($nagiosmajorver == 2) {
        $graphs{'checkstats'} = ['NUMACTHSTCHK5M','NUMACTSVCCHK5M','NUMPSVHSTCHK5M','NUMPSVSVCCHK5M'];
} else {
        print "Nagios version '$nagiosver' not supported..\n";
        exit;
}
# The requested type does not exist in the hash
if (! defined($graphs{$request_type})) {
  print "Type $request_type is not a valid request type!\n";
  exit;
}

# construct parameters for command line
my $params;
foreach my $i (@{$graphs{$request_type}}) {
  if (! defined($params)) {
    $params = $i;
  }
  else {
    $params = $params . ",$i";
  }
}

# execute command and collect results
open(RESULTS, "$nagiostat -d $params -m|");

my $index=0;
while (<RESULTS>) {
        chomp;
        my $tmp = "$graphs{$request_type}[$index]:$_ ";
        # Make nagiostats from nagios 2 and 3 compatible
        $tmp =~ s/ACTHSTCHK/HSTACTCHK/;
        $tmp =~ s/ACTSVCCHK/SVCACTCHK/;
        $tmp =~ s/PSVHSTCHK/HSTPSVCHK/;
        $tmp =~ s/PSVSVCCHK/SVCPSVCHK/;
        print "$tmp";
        $index++;
}
print "\n";

close RESULTS;
axantia
Posts: 1
Joined: Wed Jul 28, 2010 9:05 am

Nagios Core not supported

Post by axantia »

http://my-nagios-server/nagios/cgi-bin/ ... pe=latency

Nagios version '' not supported..

Nagios® Core™
Version 3.2.1
March 09, 2010
jenhek
Posts: 1
Joined: Thu Nov 03, 2011 10:18 am

Re: Nagios Statistics Templates

Post by jenhek »

On my system the nagiostats binary had 770 as permissions:
[root@my-nagios ]# ls -l /usr/local/nagios/bin/
insgesamt 800
-rwxrwxr-- 1 nagios nagios 637024 24. Aug 09:09 nagios
-rwxrwxr-- 1 nagios nagios 41680 24. Aug 09:09 nagiostats
The solution was to set the execute bit for the apache:
[root@my-nagios ]# chmod 751 /usr/local/nagios/bin/nagiostats
tkjode
Posts: 1
Joined: Mon Nov 21, 2011 10:43 am

Re: Nagios Statistics Templates

Post by tkjode »

Noticed that the cgi starts reporting -zero- values for data in some situations (started for me when moving nagios status file and object cache to ram under /dev/shm)

Here's a small fix to resolve this by allowing the piped data to be buffered by 'head' before being parsed by the CGI....

Line 71: Change:

Code: Select all

open(RESULTS, "$nagiostat -d $params -m|");
To:

Code: Select all

open(RESULTS, "$nagiostat -d $params -m|head -10|");
Should help with data drops in all graphs.
User avatar
morgentau
Posts: 43
Joined: Fri Jun 16, 2006 4:05 am
Location: Zurich, Switzerland

Re: Nagios Statistics Templates

Post by morgentau »

Hurray !

The script is working after a bit of trying and correcting... i applied the remedies from eschoeler and hayou and of course followed the setup and installation instructions...This is from/to 2 different Suse Linux Servers, one for the Cacti the other for the Nagios.

For me a great new way to look at Nagios...

:-)
Server: IBM x3650 M3, 1 XEON CPU 3.0 GHz, 4 Core, 8GB RAM,
OS: Suse Linux 11.4 x64
Apache 2.x
MySQL 5.x
PHP 5.x
Plugins: DSStats, Settings, NMID Cereus Reporting, Spikekill
Install Date: May, 2011

Wed, 19 Oct 2011 13:46:36 +0200
Cacti Version 0.8.7g
Cacti OS unix
SNMP Version NET-SNMP version: 5.6.1
RRDTool Version RRDTool 1.4.x
Hosts 57
Graphs 716
Data Sources Script/Command: 15
SNMP: 166
SNMP Query: 310
Script Query: 4
Script - Script Server (PHP): 46
Script Query - Script Server: 319
Total: 860

Last Run Statistics Time:97.9035 Method:cmd.php Processes:10 Threads:N/A Hosts:58 HostsPerProcess:6 DataSources:3040 RRDsProcessed:860
fatbaldneil
Posts: 1
Joined: Mon Feb 02, 2015 4:28 pm

Re: Nagios Statistics Templates

Post by fatbaldneil »

Seems to be working with CentOS 6, Cacti 0.8.8b and Nagios 4.0.8 :D
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest