Quick and dirty script to add devices to cacti

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

Moderators: Developers, Moderators

Post Reply
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Wow, cool. I'm not a MRTG user, but sth like this was asked often. I'll surely link to this post from time to time :lol:
Reinhard
c1155499
Posts: 1
Joined: Wed Nov 29, 2006 3:12 pm
Location: Ohio

add_tree.php

Post by c1155499 »

I have added close to 300 devices and 1500 graphs, but I am having problems with the add_tree.php.

What I would like to do is make new Tree Item underneath an already made Graph Tree under the root tree, and then add the specific location graphs to that tree. Can anyone help me with the command?

Example:

Remote Sites -> Location 304 -> Location 304 Graphs

Also, is there a later version of the add_tree with the new features listed below:

Other Options: --list-hosts
--list-trees *
--tree-id [ID] --list-nodes *
--list-rras *
--host-id [ID] --list-graphs *

Thanks,
Jamie
sini
Cacti User
Posts: 91
Joined: Mon Nov 24, 2003 10:22 am
Location: Hungary

Post by sini »

Hi,


I upgraded cacti from 0.8.6.g > 0.8.6i and the add_graph.php script does not work anymore...
Every time I execute the script its gives me the help output, although I use the same parameters like I used with 0.8.6g.
Does anyone use this script with success on 0.8.6i ??
Sini
melvinshaw
Posts: 6
Joined: Tue Feb 13, 2007 5:14 pm
Location: Columbus, OH

Post by melvinshaw »

It's been a little quiet around here but thought I'd ask anyway.

These scripts run wonderfully for my on 0.8.6i but on 0.8.6j they seem to run REALLY slow, and somewhat unreliably. I have a PERL script wrapper thing that I use to call the add_device.php script. It can take upwards of 5 or 6 minutes to add some hosts. Others seem to fail altogether. This took seconds on the previous versions.

When I run the command directly on the console, I get the same symptoms so I'm fairly confident it has nothing to do with my usage via my wrapper. I also don't suspect it's the host I'm running it on as it's a fairly powerful HP ProLiant with a ton of RAM and really not much else to be working on.

Before I get started digging in, figured I check if anyone had similar issues, tips, or fixes they'd like to share.

-melvin
melvinshaw
Posts: 6
Joined: Tue Feb 13, 2007 5:14 pm
Location: Columbus, OH

Post by melvinshaw »

Solved my previously posted issue somewhat - I found I was missing a PHP memory error. Upped that a bit in /etc/php5/cli/php.cfg and seems to be completing now, though slower than I would like.

score one for the good guys

-melvin
glista
Posts: 10
Joined: Mon Mar 19, 2007 12:43 am

Post by glista »

We needed a mechanism for automaticly (using scripts) adding alot of data sources and graphs to cacti. So I've implemented an additional data input method (data_input.php). The changes also add data validation in database functions. The mechanism works like a black-box but requires PHP4. It works with any GUI (is not tied into a specific GUI). This way one can easily write a data input tool, it can be a command line, SOAP, Cacti GUI, .... One only needs to use the following functions to implement data input:
-method_add, method_edit, mehod_delete,
-input_add, input_edit, input_delete,
-output_add, output_edit, output_delete.

Methods return values or throw exceptions. Exceptions should be catched by "if ($throwExceptionArray)"

There are 4 files in cati-data_input.zip:
data_input_cmd.php // for command line calls
data_input_function.php // implemented functions from cacti
data_input_validate.php // input validations, database validations
error_Exception.php // error exception

Knowing these are quite extensive changes I would welcome your input. Hopefully I've also managed to make code a bit more readable.
Attachments
cacti-data_input.zip
(7.08 KiB) Downloaded 665 times
Last edited by glista on Wed Apr 11, 2007 4:59 am, edited 1 time in total.
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Hey, interesting.
Would you please be so kind to post some application examples?
Reinhard
glista
Posts: 10
Joined: Mon Mar 19, 2007 12:43 am

Post by glista »

Here is sample to use command line data_input_cmd.php UI:

C:\cacti>php data_input_cmd.php --type method --do add --name TestMethod --inputType scriptcommand --inputString c:/phptest/random2.pl
Method Created - method-id: (33)

#set --methodID 33
C:\cacti>php data_input_cmd.php --type output --do add --name OutputField --methodID 33 --dataName friendlyName --updateRRA
Output Created - output-id: (67)


php data_input_cmd.php --type method --do show
php data_input_cmd.php --type output --do show
The changes should be visible in cacti GUI "data input methods"
danathane
Cacti User
Posts: 127
Joined: Thu May 03, 2007 9:03 am

Post by danathane »

peolple hie!!!

I have read all the topic and I think that the solution to my problem is there. But I am still asking myself this question : how can i run the commands like ./add_device.php? I think that thoose commands are Linux ones. So do thoose scripts work one windows. And if the answer is yes how can I run thoose script?

Thanks for all
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

php -q add_device.php ...
at least I suppose so. It's quite a time that I windozed last
Reinhard
danathane
Cacti User
Posts: 127
Joined: Thu May 03, 2007 9:03 am

Post by danathane »

hello again.

I would like to know where i have to copy paste the files for add_device. Is it in the cacti folder or in a sub-folder like for plug ins?
I am a totaly new user so i don't know anything.

Thanks.
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

I put them into main directory and it worked well for me
Reinhard
User avatar
disirk
Posts: 12
Joined: Mon Sep 11, 2006 3:26 pm
Location: Atlanta, GA

Post by disirk »

Updated the add_tree.php to include the --list-trees and --tree-id [ID] --list-nodes options. I'm not an expert with patching, so not sure if this will work on all systems so please backup your original.

apply by copying the code below to a patch file...for instance add_tree.patch. Then from the directory you have add_tree.php in execute:
patch -p0 < add_tree.patch

Test by trying it out: ./add_tree.php --list-trees

./add_tree.php --list-trees
Known Trees:(id, name)
9 Testing
12 Points of Presence
13 Firewall testing

Code: Select all

--- add_tree.php        Sat May 19 18:36:16 2007
+++ add_tree.php.new    Sat May 19 18:37:35 2007
@@ -44,8 +44,9 @@
             (Host group styles: 1 = Graph template, 2 = Data Query Index)
 
     Other Options: --list-hosts
-                   --list-trees *
-                   --tree-id [ID] --list-nodes *
+                   --list-trees 
+                   --tree-id [ID] --list-nodes 
+                       (--tree-id must be set before --list-nodes) 
                    --list-rras *
                    --host-id [ID] --list-graphs *
 
@@ -121,6 +122,21 @@
             case "--list-hosts":
                 displayHosts($hosts);
                 return 0;
+            case "--list-trees":
+               $trees          = getTrees();
+                displayTrees($trees);
+                return 0;
+           case "--list-nodes":
+               if (isset($treeId))
+               {
+                       $treeNodes = getTreeNodes($treeId);
+                       displayTreeNodes($treeNodes,$treeId);
+               }
+               else 
+               {
+                       usage();
+               }
+                return 0;
             case "--host-group-style":
                 $i++;
                 $hostGroupStyle = $_SERVER["argv"][$i];
@@ -241,6 +257,7 @@
 
 function getHosts()
 {
+    $hosts = array();
     $tmpArray = db_fetch_assoc("select id, hostname from host order by id");
     foreach ($tmpArray as $host)
       $hosts[$host["id"]] = $host["hostname"];
@@ -256,4 +273,40 @@
     }
 }
 
+function getTrees()
+{
+   $trees = array();
+   $tmpArray = db_fetch_assoc("select id, name from graph_tree order by id");
+   foreach ($tmpArray as $tree)
+     $trees[$tree["id"]] = $tree["name"];
+   return $trees;
+}
+
+function displayTrees($trees)
+{
+   echo "Known Trees:(id, name)\n";
+   while (list($id, $tree) = each ($trees))
+   {
+       echo "\t" . $id . "\t" . $tree . "\n";
+   }
+}
+
+function getTreeNodes($treeId) 
+{
+       $nodes = array();
+       $tmpArray = db_fetch_assoc("select id, title from graph_tree_items WHERE graph_tree_id = $treeId AND title != ''");
+       foreach ($tmpArray as $node)
+               $nodes[$node["id"]] = $node["title"];
+       return $nodes;
+}
+
+function displayTreeNodes($treeNodes,$treeId) 
+{
+       echo "Known Tree Nodes for Tree ID $treeId:(id, title)\n";
+       while (list($id,$title) = each ($treeNodes))
+       {
+               echo "\t" . $id . "\t" . $title . "\n";
+       }
+}
 ?>
+
glista
Posts: 10
Joined: Mon Mar 19, 2007 12:43 am

Post by glista »

It would be very nice if this script could make it into official Cacti distribution becouse of the future compatibility. A nice approch might be to seperate the work API functions from the GUI.
robt
Posts: 1
Joined: Sun May 27, 2007 10:42 am

dirty way to add hosts from discover, using this script

Post by robt »

create a php script to loop through the newly added hosts:

Code: Select all

<?
$db = "cacti";
$h = "localhost";
$user = "whoever";
$pass = "cactipass";

$go = mysql_connect($h,$user,$pass);
$SQL = "SELECT * from plugin_discover_hosts where ip like '10.10.%'";
$result = mysql_db_query($db,$SQL,$go);

while($row = mysql_fetch_array($result)) { 
 $ip = $row['ip'];
 $sysname = $row['sysName'];

 echo "found Customer-YourDescription-$sysname with ip $ip\n";
 $addhost = passthru("/usr/bin/php /path/to/add_device.php 9 \"customer-whatever-$sysname\" $ip COMMUNITY 2 0");
 // add add_graph command line here, or whatever else
 } 
?>
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest