Quick and dirty script to add devices to cacti

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

Moderators: Moderators, Developers

Post Reply
Author
Message
glista
Posts: 10
Joined: Mon Mar 19, 2007 12:43 am

SOAP for Cacti Version 0.8.7a

#61 Post by glista » Mon May 28, 2007 6:32 am

Please find attached a sample script for automated
insertion of graphs (via SOAP). Installation instructions::

SERVER:
1. Use PHP 5.x (with SOAP support) and Cacti Version 0.8.7a
2. Extract files from “soap-server.zip”
3. Place “soap_service.php” file into your Cacti installation directory
4. Place “soap” directory with all included files into your Cacti installation directory
5. Open cacti URL (typically “http://<hostname>:<port>/cacti”)
6. Click “User Management”
7. Check “admin”, choose action “duplicate” and click “Go”
8. Enter the following values:
User name: “soap”
Full name: “Soap service”
Password: “soap”

CLIENT:
1. Use PHP 5.x (with SOAP support)
2. Extracts files from “soap-client.zip”
3. Open the command line and go to the directory where you are extracted the files
4. Type:
“php add_graph_cl.php --username=soap --password=soap --urn=<hostname>:<port>/cacti --do=--list-snmp-queries”

Option “--urn” specifies the path where cacti service is running. If you are running cacti service and client in local machine, just add this parameter: “--urn=localhost/cacti” or “--urn=localhost”

5. Press enter to start the client

Sample Client for Perl
For a sample soap client in Perl see the file “samples/soap_client_perl.pl”.
Both SOAP and GUI use common work functions, this way we abstract
functionality from the user input system (GUI, SOAP) and make code more manageable.

I would appreciate any feedback on the code, does this approach seem sane and what is your opinion, should more functionality be added this way, for example the adding of devices, trees...
Attachments
Cacti SOAP service.png
Cacti SOAP service.png (26.24 KiB) Viewed 15610 times
SOAP_Server.zip
Cacti Soap-server
(78.12 KiB) Downloaded 653 times
SOAP Client.zip
Cacti Soap-client for adding graphs, devices, trees.
(8.02 KiB) Downloaded 591 times
Last edited by glista on Tue Jul 01, 2008 6:34 am, edited 13 times in total.

danathane
Cacti User
Posts: 127
Joined: Thu May 03, 2007 9:03 am

#62 Post by danathane » Tue Jun 05, 2007 10:36 am

Hello!!!!

So, i am using the quick and dirty script, and it is wonderfull, when i found how it works.
I have created a little script in php to use those scripts and i have added some 6 device with something like 80 scripts. It tooks me on minutes to create all that. So it is reaaly use full to me.

Thanks you very very very much :d I love Those Dirty Scripts

danathane
Cacti User
Posts: 127
Joined: Thu May 03, 2007 9:03 am

#63 Post by danathane » Thu Jun 07, 2007 3:19 am

Some question about the dirty script :
Can i Add a device which is not already connected on the network?
If it is possible with the script please could you tell me how to do that?

Thkx

User avatar
timi
Cacti User
Posts: 240
Joined: Wed Nov 09, 2005 8:35 am
Location: Timisoara, Romania
Contact:

#64 Post by timi » Thu Jun 07, 2007 4:03 am

danathane wrote:Some question about the dirty script :
Can i Add a device which is not already connected on the network?
If it is possible with the script please could you tell me how to do that?

Thkx
you can not, you need to get the informations from the device ...

danathane
Cacti User
Posts: 127
Joined: Thu May 03, 2007 9:03 am

#65 Post by danathane » Thu Jun 07, 2007 5:01 am

ok thx for answering, and through the web interface? is it possible to add a device which isn't connected?

thx

User avatar
timi
Cacti User
Posts: 240
Joined: Wed Nov 09, 2005 8:35 am
Location: Timisoara, Romania
Contact:

#66 Post by timi » Thu Jun 07, 2007 7:33 am

danathane wrote:ok thx for answering, and through the web interface? is it possible to add a device which isn't connected?

thx
if you dont need any information from that device... just do it...

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

#67 Post by melvinshaw » Thu Jun 07, 2007 9:50 am

It is possible to add a device in disabled mode with the add_devices.php script by passing a 1 as the last argument. just execute the add_devices.php for more explanation of the usage.

I think this will accomplish what you wish, to add devices to Cacti before they are on the network without the poller freaking out too bad.

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

Cacti SOAP Client for adding multiple hosts, graphs, trees

#68 Post by glista » Mon Jun 11, 2007 8:15 am

SOAP adding multiple graphs manual

Before we can use soap client make sure that the soap server for cacti is installed and configured.

Soap client installation instructions:
1. Use PHP 5.x (with SOAP support)
2. Extract files from “cacti-import.zip”
3. Open the command line interface and got to the directory where you've extracted the files. There you will find tools for adding large sets of devices, graphs and trees.

Usage instructions:

Importing multiple hosts:

We need to create a CSV source file “host.csv” (this is just an example; of course you can choose a different name). Every record is a separate row of data. Every row is in the following format:
<fqdn>;<name>;<community>;<snmp_ver>;<disable>;<snmp_username>;<snmp_password>;<snmp_port>;<snmp_timeout>;<template_name>

Description of fields:
<fqdn> - FQDN or IP address of the host,
<name> - device name,
<community> - community name,
<snmp_ver> - snmp version values: 1, 2, 3,
<disable> - host is disabled (value 1) or host is enabled (value 0),
<snmp_username> - username if we use snmp version 3,
<snmp_password> - password if we use snmp version 3,
<snmp_port> - snmp port number. Default value is 161,
<snmp_timeout> - snmp timeout in milliseconds,
<template_name> - This is host template name. Go to cacti web interface. Click “Host Templates”. Select host template for new device. Copy entire field for name (watch out for empty strings) and paste it here.

An example of the “host.csv” file:

“host1”;”hostname1”;”com123”;2;0;;;161;500;”Cisco Router”
“host2”;”hostname2”;”com123”;2;0;;;161;500;”Cisco Router”
“host3”;”hostname3”;”com123”;2;0;;;161;500;”Cisco Router”


After we have the “host.csv” file, we can run the command:

php auto_add_devices_cl.php --urn="<hostname>:<port>/cacti" --username="soap"
--password="soap" --source="C:\\path\\host.csv"

--urn: is the location where cacti service is running. If you are running both cacti service and client in local machine, just type: --urn=localhost/cacti or --urn=localhost
--source: is location of the CSV file. For delimiter between directory and subdirectory use double slash (“\\”).

Script “auto_add_devices_cl.php” will generate two files named after a source file but with added suffix “log.csv” and “err.csv”. First file contains the data that was successfully imported to Cacti service. We can find it in file “host.csv.log.csv” and second file “host.csv.err.csv” contains data which failed to be imported. These files have the same structure as the source file.


Importing graphs and trees

We need to create a CSV source file “graph.csv” (this is just an example; of course you can choose a different name). Every record is a separate row of data. Every row is in the following format:
<fqdn>;<subdescr>;<graphTemplateName>;<snmpQueryName>;<snmpQueryTypeName>;<snmpFieldName>;<snmpValueName>;<page>;<section>

We can create custom graphs (CG) or data sources (DS) graphs. For CG graphs use empty values for snmpQueryName, snmpQueryTypeName, snmpFieldName and snmpValueName.
The flowing example will show how we create data sources graphs:

Description for each column:
<fqdn> - FQDN or IP address of the host.

<subdescr> - sub description of the graphs for this host. We can use defined variables for generating descriptions (|host_description|, |host_name|, |query_inxmlfile| etc.). We can use generated sub descriptions defined in Cacti Data Queries template if we leave this value empty.

<graphTemplateName> - This is a graph template name. Go to cacti web interface. Click “Graph Templates”. Select graph template for new graphs. Copy entire field from name and paste it here. Watch out for empty strings.

<snmpQueryName> - This is a data query template name. Go to cacti web interface. Click “Data Queries”. Select data query for new graphs. Copy entire field from name and paste it here. Watch out for empty strings.

<snmpQueryTypeName> - Go to cacti web service. Click “Data Queries”. Select data query for new graphs. Go to selected record in table “Associated Graph Templates”. Copy entire content from the name field and paste it here.

<snmpFieldName> - Field name for selected device interfaces. Click “Data Queries”. Select data query for new graphs. Here we can find full file path of used xml query. Open this xml file. We can see the list of all query fields for the selected data query template.

<snmpValueName> - It contains value from selected “<snmpFieldName>”. Click “New Graphs”. Select host name and graph types. Now we can see table of all input fields and their values. We can select only value in column that we defined in “<snmpFieldName>”.

<page> - This is the name of tree in root position.
For adding graphs to existing menus; go to cacti web interface. Click “Graph Trees”. Select the record in table option. Copy entire field from name and paste it here. Watch out for empty strings.
Or if we would like to add a new menu just enter a new name. If the value is empty, then the column name section is ignored. The script will put graphs in root of cacti menu,

<section> - This is the name of sub tree. This has a status of child. The left column is parent name a “<page>”. If this value is empty then column section is ignored. The script will put all graphs in parent of Cacti menu,

Sample of “graph.csv” file:

host1”;;”Interface - Traffic (bits/sec)”;” SNMP - Interface Statistics”;” In/Out Bits (64-bit Counters)”;”ifOperStatus”;”Up”;”Page1”;”Section1”

Now we have a graph.csv file. Then we run two commands:

1. Command: For adding graphs:
php auto_add_graphs_cl.php --urn="<hostname>:<port>/cacti" --username="soap" --password="soap" --source="C:\\path\\graph.csv"

2. Command: Adding trees and graphs to the trees:
php auto_add_trees_cl.php --urn="<hostname>:<port>/cacti"--username="soap" --password="soap " --source="C:\\path\\graph.csv"

First command for adding graphs will create all graphs for monitored interfaces traffic for selected host. We will use SNMP version 2 which supports 64bit counters. In the name field we enter: ”ifOperStatus”. This will ask for status for each interface. Interface has a status “Up” or “Down”. We fill filed “<snmpValueName>” with value “Up”. This will select all the interfaces for selected host which status name “ifOperStatus” is “Up” and it will create a traffic graphs for selected interfaces.

Second command for adding trees will put all created graphs for selected host to the menu of cacti tree.
Attachments
SOAP_Cacti_Import.zip
Tools for adding multiple hosts, graphs, trees via Cacti SOAP calls
(67.63 KiB) Downloaded 574 times
Last edited by glista on Tue Jul 01, 2008 6:32 am, edited 6 times in total.

fxef79
Posts: 9
Joined: Fri Jun 08, 2007 11:10 am

#69 Post by fxef79 » Mon Jun 11, 2007 12:16 pm

Any suggestions on how to bulk import hosts using this method if i want to leave the community string empty (without rewriting it the script, I mean?).

I just want to bulk add a large number of remote hosts for which I'm doing nothing but advanced ping statistics - no SNMP - and I don't want cacti to attempt any SNMP contact (because they'll fail, of course!).

Aceldama
Posts: 3
Joined: Tue Jun 05, 2007 2:17 pm

#70 Post by Aceldama » Tue Jun 12, 2007 1:14 pm

I've read through this thread a few times now and there are some great tools here.

I'm currently stuck on a problem and I can't seem to figure out if the solution is here in the thread or not and I could use some help.

Here is what I want to do:

I want to add a graph to cacti from the command line. The graph's data will come from a perl script.

I've been able to use Glista's script to add a perl script to cacti as a Data Input Method.

The question now is: How do I now create a Data Source which uses that input method? And then how do I add a graph to Cacti that uses that Data Source?

It seems all the methods listed here for adding graphs to cacti via the command line revolve around SNMP queries for data, which I'm not using. Or they require a new Data Template for each graph... which doesn't seem to help since I'd have to create a new template for each graph through Cacti's interface (with a new Data Source for each one) therefore defeating the purpose (unless I'm not understanding properly).

Any help or direction would be greatly appreciated.

Cliff notes:

I want to add graphs to cacti from the command line. These graphs are going to use Data Sources that use scripts for input methods (no snmp). I also need to add the Data Sources to cacti through the command line for the graphs to use. how can I do this?

jzondor
Posts: 7
Joined: Wed May 10, 2006 5:16 pm

Re: A wrapper for Bradley's scripts

#71 Post by jzondor » Fri Jun 15, 2007 12:26 pm

stutz wrote:Firstly thanks to Bradley et al for the php import scripts. I have written my first ever script (so don't be to harsh - comments for improvements appreciated!) to successfully transfer 1500 devices from CastleRock SNMPc to Cacti. This can be modified to read in pretty much any CSV file and populate Cacti with the Tree, Devices and Graphs all asociated with the relevant Nodes.

One question I have is when creating a CG graph, is there any way to populate the custom fields?

For example for a graph 'Host Availability' in the 'Data Sources' menu in the 'Supplemental Data Template Data' section is a 'Custom Data' field for 'IP Address'. At the moment I have to manually add the address for each device.
Stutz, I simplified your script to only add devices. Other various (simplification) changes made.
Attachments
import_deviceonly.sh
create file server.txt file with a list of your servers and modify the variables in the script to suite your environment.
(937 Bytes) Downloaded 695 times

cacti4bigy
Posts: 10
Joined: Wed Jul 11, 2007 12:42 pm

Newbie Question for Adding Multiple Hosts in One Shot.

#72 Post by cacti4bigy » Sun Sep 16, 2007 6:50 pm

OK.

I understand that there are several posts about Adding a large amount of devices to Cacti. However, currently my experience with Cacti is very vague. So if someone would be so kind to give me additional info...

My situation:
I have 300 Servers ( All Win2K3, all on the same domain, with same SNMP Community. )
Is the best way to add all these devices with the add_devices.php ?
If so, as painful as it may be for someone, could someone give me additional detail on how to run it? Such questions I have in mind: Where exactly do you need the add_devices.php file located on Cacti Server ? Also, do i Just run the command from a Windows Dos Cmd Promp? Do i need to run it in the Dos window in any specific directory? Do you have to do the same command line, one server after another, or can this be ran by looking at a txt file of some sort that has a list of the servers i would like to add? Also If you could, could you please give me an example of what the command would look like?

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

#73 Post by gandalf » Mon Sep 17, 2007 2:31 pm

Currently, the files are placed into main cacti directory and run from there, only on the cacti server itself.
With 0.8.7, they are moved to the ./cli directory and will be officially supported
Reinhard

rfoust
Posts: 4
Joined: Thu Aug 24, 2006 8:09 pm

wsdl file?

#74 Post by rfoust » Mon Dec 10, 2007 4:12 pm

Hi,

Is a wsdl file available for the soap stuff? I'd like to interface to it in Powershell on windows and that would sure make the job easier.

Thanks,

- Robbie

shinmaki
Posts: 2
Joined: Wed Dec 19, 2007 10:56 am

#75 Post by shinmaki » Wed Dec 19, 2007 11:05 am

Hi everyone,

Thes scripts seem great. But I have an error message when using it.

I changed the following lines from the add_graphs.php file from :

include(dirname(__FILE__)."/include/config.php");
include_once($config["base_path"]."/lib/data_query.php");
include_once($config["base_path"]."/lib/utility.php");
include_once($config["base_path"]."/lib/sort.php");
include_once($config["base_path"]."/lib/template.php");
include_once($config["base_path"]."/lib/api_data_source.php");
include_once($config["base_path"]."/lib/api_graph.php");
include_once($config["base_path"]."/lib/snmp.php");
include_once($config["base_path"]."/lib/data_query.php");
include_once($config["base_path"]."/lib/api_device.php");

To :

include(dirname(__FILE__)."/config.php");
include_once($config["base_path"]."/usr/share/cacti/lib/data_query.php");
include_once($config["base_path"]."/usr/share/cacti/lib/utility.php");
include_once($config["base_path"]."/usr/share/cacti/lib/sort.php");
include_once($config["base_path"]."/usr/share/cacti/lib/template.php");
include_once($config["base_path"]."/usr/share/cacti/lib/api_data_source.php");
include_once($config["base_path"]."/usr/share/cacti/lib/api_graph.php");
include_once($config["base_path"]."/usr/share/cacti/lib/snmp.php");
include_once($config["base_path"]."/usr/share/cacti/lib/data_query.php");
include_once($config["base_path"]."/usr/share/cacti/lib/api_device.php");
include_once($config["base_path"]."/usr/share/cacti/lib/database.php");
include_once($config["base_path"]."/usr/share/cacti/lib/functions.php");

This is what I have when executing the script :

Shell % php add_graphs.php --graph-type ds --graph-template-id 110 --host-id 229 --snmp-query-id 1 --snmp-query-type-id 69 --snmp-field ifIndex --snmp-value 1000

Fatal error: Call to a member function SetFetchMode() on a non-object in /usr/share/cacti/lib/database.php on line 155
zsh: exit 255 php add_graphs.php --graph-type ds --graph-template-id 110 --host-id 229 1

The line 155 is :

$cnn_id->SetFetchMode(ADODB_FETCH_ASSOC);

I am not really good in programing... Cacti is hosted on a Fedora Core 6, PHP5.

Does anyone have any aidea ?

Thanks in advance.

Post Reply