Cacti (home)ForumsRepositoryDocumentation
Cacti: offical forums and support  

 FAQFAQ   SearchSearch   MemberlistMemberlist    RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in    


Include Cacti links in Nagios

 
Post new topic   Reply to topic    Cacti Forum Index -> Addons for Cacti
Author Message
nicolargo



Joined: 10 Oct 2006
Posts: 6

PostPosted: Tue Jun 10, 2008 9:45 am    Post subject: Include Cacti links in Nagios Reply with quote

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

http://blog.nicolargo.com/2008/06/lier-cacti-et-nagios.html

Nicolas

ps: in french but easy to understand...)
Back to top
Dyr



Joined: 10 Sep 2007
Posts: 8

PostPosted: Thu Jul 24, 2008 8:16 am    Post subject: Reply with quote

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:
 $action_url = $cactiurl."/raph.php?local_graph_id=".$row["local_graph_id"]."&rra_id=all";

to this one:
Code:
$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.
Back to top
Dyr



Joined: 10 Sep 2007
Posts: 8

PostPosted: Thu Jul 24, 2008 9:45 am    Post subject: Reply with quote

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

-- 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:

<?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);

?>
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Cacti Forum Index -> Addons for Cacti All times are GMT - 5 Hours
Page 1 of 1

 



Powered by phpBB © 2001, 2005 phpBB Group