Quick and dirty script to add devices to cacti

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

Moderators: Developers, Moderators

Post Reply
mshook
Posts: 46
Joined: Mon May 02, 2005 3:33 pm
Location: New York, NY

Quick and dirty script to add devices to cacti

Post by mshook »

Hi all,

I just wrote this script that i'm going to be using to add a bunch of new devices (I also have a wrapper to call it multiple times with different hosts/addresses).

Put the file in your cacti directory and call it like this:
./add_device.php profilenumber somehostname somedescriptionforthehost community snmpversion disable

You can get the complete syntax by using --help or by reading the code! ;-)

The script uses api_device_add to create the new device so it should be safe.

I'm now trying to write a script to automate graph creation but it doesn't look like it's going to be a piece of cake (been reading graphs_new.php and template.php, most notably these functions: host_new_graphs, host_new_graphs_save and create_complete_graph_from_template).

Any help, comment or patches are welcome!
moshesharon
Posts: 17
Joined: Sat Dec 18, 2004 5:38 pm

suggestions

Post by moshesharon »

hi

this is a very important addon. perhapse you can post the script so we can test.

different approach then digging in the php code. is to trace the sql queries.

Good luck

Moshe
mshook
Posts: 46
Joined: Mon May 02, 2005 3:33 pm
Location: New York, NY

Post by mshook »

I thought I had attached the script, guess not, sorry about that! :-D
Attachments
add_device.php.txt
(4.86 KiB) Downloaded 9847 times
mshook
Posts: 46
Joined: Mon May 02, 2005 3:33 pm
Location: New York, NY

Re: suggestions

Post by mshook »

moshesharon wrote:different approach then digging in the php code. is to trace the sql queries.
This issue I'm concerned with this approach ('cause I thought about it) is if you're adding a set of devices that are not homogeneous, you might end up inserting the wrong data in the database.

IE: say you want to add hosts A & B. A has 2 interfaces, 3 drives and B 3 interfaces and 2 drives but let's assume you don't know about it and you've decided to use host A as the template for your SQL queries.
If you use the SQL queries you derived from A to create these devices, as you really don't query the device to see what snmp exports, you're going to insert a lot of mess in the database, though I'm not sure how bad it is.

The good thing about using the api, is when you create the device, it actually does all the snmp queries and populates all the appropriate tables. Drawback is that it takes longer as there's more processing involved.

Another problem with the API approach is that creating the actual graphs seems way trickier than just calling one simple function. I first need to create a function that actually gets the available things that can be graphed for a device and then, based on some parameters one would pass to the script, decide which one to graph.
Issue is for some of the graphs, you need to specify things like colors and so on meaning I need to recreate all the arrays that are passed to create_complete_graph_from_template.
I've dumped the actual args that are passed to it and well it looks really discouraging!!! :-D

Does this make sense?
moshesharon
Posts: 17
Joined: Sat Dec 18, 2004 5:38 pm

Post by moshesharon »

i guess you right about this approach if you adding complex device settings. so another option can be to create host + device + snmp. wait till the api finish and then creates the graph. maybe running the script twcie with --device flag and --graph flags. i currently working on perl script to add / import device + rrd file + graphs into cacti. this is usefull
if you have other programs like nagios creating the rrd file for you and just want to display / report it in cacti. but its still in its alpha stages. and im not ready to post ( hopefully soon ) in this script i traced the sql queries.

Moshe
mshook
Posts: 46
Joined: Mon May 02, 2005 3:33 pm
Location: New York, NY

Post by mshook »

moshesharon wrote:i guess you right about this approach if you adding complex device settings. so another option can be to create host + device + snmp. wait till the api finish and then creates the graph. maybe running the script twcie with --device flag and --graph flags.
Yea, guess I've got to get the graph part right though! ;-)
i currently working on perl script to add / import device + rrd file + graphs into cacti. this is usefull
if you have other programs like nagios creating the rrd file for you and just want to display / report it in cacti. but its still in its alpha stages. and im not ready to post ( hopefully soon ) in this script i traced the sql queries.
Nice because I could also use this script (as I've a ton of rrds that were created by Orca). Keep us posted! ;-)
kslt
Cacti User
Posts: 85
Joined: Tue Aug 02, 2005 4:12 am

Post by kslt »

any news on this topic ?

add_host.php is not working to me (doesn't do anything) ..
kslt
Cacti User
Posts: 85
Joined: Tue Aug 02, 2005 4:12 am

Post by kslt »

I'm looking for script to add (from command line) a graphs to host.

anybody heard about this?
kslt
Cacti User
Posts: 85
Joined: Tue Aug 02, 2005 4:12 am

Post by kslt »

hello

does any body can tell me how i can create a graph if i know all of the parametrs ?

i'm intersting to make a graph (smoke ping) thru the command (post) line somethink like:
bash#>php graphs_new.php?host_id=50&Title=sdkfjh&UpperLimit=222&Address=123.123.123.123&Number_of_pings=5

who can help me with that and tell me what parametrs i have to put (i can't debug it from the post$) ..

thanks.
hardinw
Posts: 6
Joined: Thu Jan 02, 2003 8:03 am
Location: Minneapolis, MN

Re: suggestions

Post by hardinw »

mshook wrote: ...

Another problem with the API approach is that creating the actual graphs seems way trickier than just calling one simple function. I first need to create a function that actually gets the available things that can be graphed for a device and then, based on some parameters one would pass to the script, decide which one to graph.
Issue is for some of the graphs, you need to specify things like colors and so on meaning I need to recreate all the arrays that are passed to create_complete_graph_from_template.
I've dumped the actual args that are passed to it and well it looks really discouraging!!! :-D

Does this make sense?
I've been muddling around with the code trying to figure out how to "import" a csv of devices and end up with automatically generated graphs. add_device.php works *perfectly* (i have custom code that wraps add_device.php for csv reading) from the device addition perspective. As it calls api_device_save, it appropriately queries the device and stores the results. The only thing I have yet to do is to *automatically* generate all of the graphs (based on graph templates) associated with a particular host template. All I want to do, is to do the programmatical equivalent of checking all the snmp_query check boxes on the "create graphs for this device" page.

As I am using a custom XML for query definition, host templates, and graph templates, I shouldn't have to interact with the process at all.

Have you had any new success since the original posting or thoughts on my problem?
attempting:
Cacti 0.8.6g | Gentoo Linux | rrdtool 1.2.6
700 devices | 55,000 datasources
bradley
Posts: 38
Joined: Fri Dec 02, 2005 7:02 am
Location: United Kingdom
Contact:

Bulk Import Tools/Mass Import

Post by bradley »

Hi all,

I've created some scripts to compliment the add_device.php and together they can all be used to create graphs, plug the graphs into a tree, and assign permissions etc.

I changed the original add_device.php (posted above) so that it outputs the new device ID/Host ID which can then be captured and used to add graphs to that host. If the added device already exists, then it outputs the existing host/device ID instead so as to avoid duplicates.

Likewise, the add_graphs.php outputs the graph-id so that it can be used to add it to a tree, or to assign permissions etc.

Usage for each of the scripts are documented either within each script or if you run it with no parameters. I've successfully used these scripts to automatically add the graphs for over 2500 interfaces (the company I work for, Synetrix, manage a metropolitan-area network for all the schools in London) and to plug them into various trees, each of which have various access rights depending on the user.

The most complex script is the add_graphs.php:

To add a graph of Interface Traffic, you need to know the following in advance:

* The Host ID (I parse it from the output of add_graph.php)
use --list-hosts to see a list of them
* The Graph Template ID (I note these via the front-end and mostly use the same ones)
use --list-graph-templates to see a list of them
* The SNMP Query ID - I use "SNMP - Interface Statistics" which is ID '1' on my system
use --list-snmp-queries to see a list of them
* The SNMP Query Type ID - I use "In/Out Bits (64-bit Counters)" - ID '14' on my system
use --snmp-query-id 1 --list-query-types to see a list of them
* The SNMP Field name that you want to use to identify the interface on the host
(I use 'ifIndex' on my system, but you can use 'ifAlias' or 'ifDescr' too)
use --host-id [ID] --list-snmp-fields to see a list of them
* The SNMP Value that that you want to use to identify the interface on the host
If you use an SNMP field name of 'ifIndex' then the SNMP Value is the index number
of the interface that you want to graph.

Once you have the above info, you can add a graph by running:

Code: Select all

./add_graphs.php --graph-type ds --graph-template-id 2 --host-id [ID] --snmp-query-id 1 --snmp-query-type-id 14 --snmp-field ifIndex --snmp-value 1
The above command would add a graph of interface 1 on the host-ID specified.
It would also output the graphi-ID that it created, and also the RRA ID.

To add this graph to a tree, you can create/get the ID of the tree using:

Code: Select all

./add_tree.php --type tree --name 'Tree Name' --sort-method a
(Outputs the Tree ID)

To then create a sub-node in this tree, use:

Code: Select all

./add_tree.php --type node --node-type header --tree-ID [TREE ID] --parent-node 0 --name 'Node Name'
Outputs the node ID

To then add the graph to the node in that we just created, run:

Code: Select all

./add_tree.php --type node --node-type graph --tree-id [TREE ID] --parent-node [NODE ID] --graph-id [ID] --rra-id [RRA ID]
Outputs the graph node ID

The graph-ID and RRA ID are output from the add_graphs.php script.

To add permissions to the graph ID created, you can use the add_perms.php:

Code: Select all

./add_perms.php --user-id [USER ID] --item-type graph --item-id [GRAPH ID]
To add permissions to the tree, you can use the following:

Code: Select all

./add_perms.php --user-id [USER ID] --item-type tree --item-id [TREE ID]
Unzip the files into your cacti directory (don't worry they have built-in protection against being run by the web-server).

All scripts are smart enough not to add duplicates, but instead to just return the ID's of the existing things in the databases so that they can be used for other operations.

Also, this is the first thing I've ever coded in php, so feedback is would be welcome!!!

Regards
Bradley Kite

(Attachment removed - see post below)
Last edited by bradley on Fri Dec 16, 2005 6:43 am, edited 1 time in total.
Exo7
Cacti User
Posts: 136
Joined: Wed Jul 13, 2005 4:50 pm

Post by Exo7 »

Thanks for this script, it is the kind of tool I need. I will try to test it as soon as I can.
I will try to make a similar script to add data sources, and then add a graph with this data source.

How much time did the script take to add the 2500 interfaces ?
jolonghair
Posts: 8
Joined: Fri Dec 16, 2005 3:19 am

Re: Bulk Import Tools/Mass Import

Post by jolonghair »

Hi Bradley,

from add_tree.php i got error message that it couldn't find api_importTree.php.

Here is the messages:

cacti@myhost:/var/www/cacti$ php add_tree.php

Warning: main(/var/www/cacti-0.8.6f/lib/api_importTree.php): failed to open stream: No such file or directory in /var/www/cacti-0.8.6f/add_tree.php on line 28

Warning: main(): Failed opening '/var/www/cacti-0.8.6f/lib/api_importTree.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/cacti-0.8.6f/add_tree.php on line 28

for what version of cacti is this script made?

in my installed version there is no api_importTree.php.

Regards

Johannes


I
bradley
Posts: 38
Joined: Fri Dec 02, 2005 7:02 am
Location: United Kingdom
Contact:

Re: Bulk Import Tools/Mass Import

Post by bradley »

jolonghair wrote:Hi Bradley,

from add_tree.php i got error message that it couldn't find api_importTree.php.
Hi jolonghair

Sorry, I forgot to include this file in the zip archive. The file is basically the same as
lib/api_tree.php but has some extra checks added to check for duplicates.

Here's the file you need.

It would be nice if the changes i made to this file were incorporated back into api_tree.php.

I kept the changes seperate so that i can upgrade my version of Cacti without breaking my existing stuff.

(ps: Exo7, it takes just over an hour to do everything for the ~2500 interfaces, thats add hosts, graphs, trees and permissions. It takes this long because each invocation of each script has to be parsed by PHP, connect to the DB etc. etc. but I wanted them to be seperate scripts to make them more modular.)

Also, I wrote this scripts for version 0.8.6g of cacti
Attachments
api_importTree.php.txt
Missing Library File
(6.67 KiB) Downloaded 4437 times
bulk_import_tools.zip
Included missing library file
(10.94 KiB) Downloaded 7684 times
User avatar
ScOp3
Cacti User
Posts: 61
Joined: Wed Aug 03, 2005 4:14 am
Location: Cologne Germany
Contact:

Post by ScOp3 »

Thanks!

Just added like 100+ Servers in less than 10 minutes. :D
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests