Quick and dirty script to add devices to cacti
Moderators: Developers, Moderators
add_tree.php
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
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
-
- Posts: 6
- Joined: Tue Feb 13, 2007 5:14 pm
- Location: Columbus, OH
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
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
-
- Posts: 6
- Joined: Tue Feb 13, 2007 5:14 pm
- Location: Columbus, OH
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.
-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 668 times
Last edited by glista on Wed Apr 11, 2007 4:59 am, edited 1 time in total.
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"
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"
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
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
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
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";
+ }
+}
?>
+
dirty way to add hosts from discover, using this script
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
}
?>
Who is online
Users browsing this forum: No registered users and 1 guest