Include Cacti links in Nagios

Addons for Cacti and discussion about those addons

Moderators: Developers, Moderators

Post Reply
nicolargo
Posts: 6
Joined: Tue Oct 10, 2006 3:37 am

Include Cacti links in Nagios

Post by nicolargo »

Here is a little hack to include link (URL) to Cacti from Nagios:

http://blog.nicolargo.com/2008/06/lier- ... agios.html

Nicolas

ps: in french but easy to understand...)
Dyr
Posts: 23
Joined: Mon Sep 10, 2007 5:21 am

Post by Dyr »

Nice one!

But some small changes in cactiplug.php are available.
At first, URL isn't correct, it generates cacti view for service instead of view for server.
I.e. if you choose "action" for server, named "MyServer", it will open URL with viewing "CPU Load of MyServer" in Cacti. For fix it, just replace string:

Code: Select all

 $action_url = $cactiurl."/raph.php?local_graph_id=".$row["local_graph_id"]."&rra_id=all";
to this one:

Code: Select all

$action_url = $cactiurl."/graph_view.php?action=preview&host_id=".$row["host_id"]."";
And I suppose, including cacti config.php in file instead of direct writing in code much more right way, but I don't know, how to do it correctly - I'm not php coder. :(
Dyr
Posts: 23
Joined: Mon Sep 10, 2007 5:21 am

Post by Dyr »

I changed script to allow symbolic hostnames, I hope, It's be helpful for you.
Patch:

Code: Select all

-- cactiplug.php.orig       2008-06-10 18:18:30.000000000 +0400
+++ cactiplug.php     2008-07-24 18:41:09.000000000 +0400
@@ -3,11 +3,12 @@
 #
 # Include Cacti links into Nagios
 # Author: Nicolas Hennion
+# Modified: Dennis Yusupoff
 #
 # Distributed under the GPL licence
 #
 ##############################################################################
-$version="0.1";
+$version="0.2";

 # Default options (TO BE CONFIGURE)
 $cactiurl="http://localhost/cacti";
@@ -20,22 +21,30 @@
 # Functions
 ###########

-function checkip($ip) {
-       if(is_string($ip) && ereg('^([0-9]{1,3})\.([0-9]{1,3})\.' .'([0-9]{1,3})\.([0-9]{1,3})$', $ip, $part)) {
-       if($part[1] <= 255 && $part[2] <= 255 && $part[3] <= 255 && $part[4] <= 255)
-               return true;
+function checkip($address) {
+       if(is_string($address)) {
+               # Is it IP...?
+               $ip = ip2long($ip);
+               if( $ip != -1 && $ip !== FALSE) {
+                       return $ip;
+               }
+               #...or hostname?
+               elseif(gethostbyname($address) != $address) {
+                       return $address;
+               }
        }
-       return false;
+return false;
 }

 # Main Code
 ###########

-# Get the IP address oh the host (GET method)
-if (isset($_GET["ip"]) and checkip($_GET["ip"])) {
+# Get the IP ip oh the host (GET method)
+if (isset($_GET["ip"]) && checkip($_GET["ip"])) {
        $ip = $_GET["ip"];
+       $address = gethostbyname($ip);  # (We already sanitize it in "checkip" function)
 } else {
-       exit;
+       die("Incorrect IP or hostname");
 }

 # Connect to the Cacti DB
@@ -45,17 +54,22 @@
        or die("Could not select database");

 # Build and execute the SQL request
-$query = "select graph_local.id as local_graph_id, host.id as host_id, host.hostname as hostname from (graph_local, host) where graph_local.host_id=host.id and host.hostname='".$ip."'";
+$query = "SELECT graph_local.id AS local_graph_id, host.id AS host_id, host.hostname AS hostname "
+       ."FROM (graph_local, host) "
+       ."WHERE graph_local.host_id=host.id AND host.hostname LIKE '".$ip."' OR host.hostname LIKE '".$address."'";
 $result = mysql_query($query)
        or die("Query failed");

 # Get the result (the last one)
 if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
-               $action_url = $cactiurl."/graph.php?local_graph_id=".$row["local_graph_id"]."&rra_id=all";
+               $action_url = $cactiurl."/graph_view.php?action=preview&host_id=".$row["host_id"]."";
        }
     header("Location: ".$action_url);
 }
+else {
+    die("Host not found, sorry...");
+}

 # Close the DB session
 mysql_free_result($result);

Whole file:

Code: Select all

<?php
##############################################################################
#
# Include Cacti links into Nagios
# Author: Nicolas Hennion
# Modified: Dennis Yusupoff
#
# Distributed under the GPL licence
#
##############################################################################
$version="0.2";

# Default options (TO BE CONFIGURE)
$cactiurl="http://localhost/cacti";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cactiadmin";
$database_password = "cactipassword";
# End of the options (DO NOT MODIFY THE INFORMATIONS BELLOW)

# Functions
###########

function checkip($address) {
        if(is_string($address)) {
                # Is it IP...?
                $ip = ip2long($ip);
                if( $ip != -1 && $ip !== FALSE) {
                        return $ip;
                }
                #...or hostname?
                elseif(gethostbyname($address) != $address) {
                        return $address;
                }
        }
return false;
}

# Main Code
###########

# Get the IP ip oh the host (GET method)
if (isset($_GET["ip"]) && checkip($_GET["ip"])) {
        $ip = $_GET["ip"];
        $address = gethostbyname($ip);  # (We already sanitize it in "checkip" function)
} else {
        die("Incorrect IP or hostname");
}

# Connect to the Cacti DB
$link = mysql_connect($database_hostname, $database_username, $database_password)
    or die("Error while connecting to the DB server");
mysql_select_db($database_default)
        or die("Could not select database");

# Build and execute the SQL request
$query = "SELECT graph_local.id AS local_graph_id, host.id AS host_id, host.hostname AS hostname "
        ."FROM (graph_local, host) "
        ."WHERE graph_local.host_id=host.id AND host.hostname LIKE '".$ip."' OR host.hostname LIKE '".$address."'";
$result = mysql_query($query)
        or die("Query failed");

# Get the result (the last one)
if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
                $action_url = $cactiurl."/graph_view.php?action=preview&host_id=".$row["host_id"]."";
        }
    header("Location: ".$action_url);
}
else {
    die("Host not found, sorry...");
}

# Close the DB session
mysql_free_result($result);
mysql_close($link);

?>
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests