Nagios (http://www.nagios.org) is a great network monitoring tool. Cacti (http://www.cacti.net) is a great network graphing tool. Perfparse (http://perfparse.sourceforge.net/ is a great add-on to Nagios that collects host performance data as part of the Nagios check command. Perfparse is great at collecting performance data when you are running Nagios client software, but short of doing a lot of SNMP MIB and OID tweaking, it is easier to use Cacti to collect performance data on devices where you can’t run Nagios client software (on routers, switches or network appliances for example). But, having three different user interfaces can be annoying. I wanted a way to to link to the Cacti and Perfparse graphs for a host or device directly from Nagios using the Notes URL without having to enter a unique URL for each host or device. I created a PHP script to handle Cacti, and entered the URL for the scripts in the Notes URL in the Nagios configuration (extended information templates). In the extended information templates for Cacti hosts I entered:
http://yourhost/nagtocacti.php?hostip=$HOSTADDRESS$&hostname=$HOSTNAME$
And in the extended information template for Perfparse hosts I entered:
http://yourhost/path-to/perfparse.cgi?select_metric=1&host_name=$HOSTNAME$
This procedure assumes you have Nagios, Perfparse and Cacti installed on the same host, and that you have saved the code below in the web root of that server as nagtocacti.php and modified the dbhost, dbname, dbuser and dbpass values to match your environment.
> nagtocacti.php
<?php
$dbhost = "localhost";
$dbname = "cacti";
$dbuser = "DB username";
$dbpass = "DB user's password";
$hostip = $_REQUEST['hostip'];
$hostname = strtoupper($_REQUEST['hostname']);
$hostid = "";
$link = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Could not connect : " . mysql_error());
mysql_select_db($dbname) or die("Could not select database");
echo "Searching for host by IP...";
$query = "select id from host where hostname='$hostip'";
$result = mysql_query($query)
or die("Query failed : " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$hostid = $row[0];
}
if ($hostid != "") {
$myurl = "<meta content="\\" http-equiv="refresh" />";
echo "$myurl";
die();
} else {
echo "Searching for host by name";
$query = "select id from host where hostname like '$hostname%'";
$result = mysql_query($query)
or die("Query failed : " . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$hostid = $row[0];
}
if ($hostid != "") {
$myurl = "<meta content="\\" http-equiv="refresh" />";
echo "$myurl";
die();
}
echo "Host not found in Cacti...";
}
?>