Provisioning from CSV by SFR

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

Moderators: Developers, Moderators

Post Reply
SFRDGRE
Posts: 1
Joined: Wed Jun 08, 2011 10:51 am

Provisioning from CSV by SFR

Post by SFRDGRE »

DESCRIPTION :
This package provide tools to help users creating devices into cacti.
You can for example add a single device with a simple command line or even import a csv file
with a lot of devices.

HOW TO :
1. Configuration
Edit the config file placed in the conf directory and check that the cacti path is correct.
Set the options to the correct data queries.

2. Patch the cli/create_graph.php file
If you want to use the --create-graph-multi option for importing scripts, you have to patch the cacti cli/create_graph.php file.
Simply copy the cli/add_graphs_filter.php in the cacti cli folder.
The patch (and the patched version of the scripts) are in the cli folder.

3. import only one host
Go to the host_add folder (here) and exec the following command :
perl scripts/import_host_to_cacti.pl --host <hostname or IP> --description <host description> --template <cacti host template> --snmp_version <1,2 or 3> --snmp_community <snmp community> [--network <graph tree name>] [--create-graph-multi] [--debug]

4. importing a lot of devices
Create a csv file as in the csv/cacti_example.csv file.
Go to the host_add folder and start the command :
perl scripts/import_csv_file_to_cacti.pl --file <path to the csv file> [--create-graph-multi] [--debug]
Attachments
provisioning_cacti-1.0.tar.gz
(34.73 KiB) Downloaded 359 times
User avatar
Linegod
Developer
Posts: 1626
Joined: Thu Feb 20, 2003 10:16 am
Location: Canada
Contact:

Re: Provisioning from CSV by SFR

Post by Linegod »

If you get a chance, please post this to the scripts area:

http://docs.cacti.net/scripts

http://docs.cacti.net/scripts.guidelines
--
Live fast, die young
You're sucking up my bandwidth.

J.P. Pasnak,CD
CCNA, LPIC-1
http://www.warpedsystems.sk.ca
X-dark
Cacti User
Posts: 111
Joined: Wed Jun 18, 2008 10:21 am
Location: France

Re: Provisioning from CSV by SFR

Post by X-dark »

Cedric Girard
User avatar
Linegod
Developer
Posts: 1626
Joined: Thu Feb 20, 2003 10:16 am
Location: Canada
Contact:

Re: Provisioning from CSV by SFR

Post by Linegod »

Thanks. I've adjusted some of the formatting.
--
Live fast, die young
You're sucking up my bandwidth.

J.P. Pasnak,CD
CCNA, LPIC-1
http://www.warpedsystems.sk.ca
User avatar
Xe03
Posts: 14
Joined: Wed May 09, 2012 10:24 am
Location: Scott Air Force Base

Re: Provisioning from CSV by SFR

Post by Xe03 »

Alternatively ...

I did not have luck with this script... Had php parsing SQL errors

This worked for me like a champ!

http://slaptijack.com/system-administra ... i-in-bulk/

Tested with 300 hosts, added to /tmp/test or wherever you want to save your file; just edit the location

Ip's listed in one column in excel and saved to *nix box

typed on the command line:

for i in `cat /tmp/test`; do
php cli/add_device.php --description=${i} --ip=${i} --community=public --template=5;
done

--template=? -> cli/php add_device.php --list-host-templates

for everything else..just follow the instructions and edit appropriately :)
User avatar
Linegod
Developer
Posts: 1626
Joined: Thu Feb 20, 2003 10:16 am
Location: Canada
Contact:

Re: Provisioning from CSV by SFR

Post by Linegod »

I pretty much do the same thing, but do a quick discovery on the device first:

Code: Select all

 DESCR=$(snmpget -r1 -t1 -c $COM -v1 $HOST sysDescr.0 2> /dev/null| grep -o "Windows\|Cisco IOS\|Cisco PIX\|Cisco Adaptive Security Appliance\|Linux\|HP-UX\|NetScreen-ISG\|Alcatel\|64101\|71922\|SSU-2000\|iPlex\|SSG-\|srx3600\|ex4200-"|head -n1)
Then based on the description, add the device:

Code: Select all

if [ "$DESCR" = "Cisco IOS" ]; then
                                TEMPLATE="5"
                        elif [ "$DESCR" = "Cisco PIX" ]; then
                                TEMPLATE="36"
                        elif [ "$DESCR" = "Cisco Adaptive Security Appliance" ]; then
                                TEMPLATE="37"
                        elif.......

Code: Select all

ACTION=`........./cli/add_device.php`
$ACTION --description=$HOST --ip=$HOST --template=$TEMPLATE --avail=snmp --community=$COM --version=2 --notes=\"$NOTES\n$DATE\""
Now our Device Inventory just sends a list of new devices, and the script 'automagically' creates them.
--
Live fast, die young
You're sucking up my bandwidth.

J.P. Pasnak,CD
CCNA, LPIC-1
http://www.warpedsystems.sk.ca
tosage
Cacti User
Posts: 164
Joined: Wed Jul 28, 2010 5:05 am
Location: France

Re: Provisioning from CSV by SFR

Post by tosage »

Hello,

I test your script, i modify the location of the config.php because i have a ubuntu server cacti from Ubuntu repo.
However, the script does not find the values ​​for database_username and database_password
I put print in the Functions.pm after the line 446 and 455 (after the if section) but i don't understand why it does not work :/

xxxxxxx@xxxxxxx:/usr/share/cacti/site/host_add$ sudo perl scripts/import_csv_file_to_cacti.pl --file /usr/share/cacti/site/csv/cacti_example.csv --debug
Script launched on Sun Apr 21 17:23:44 2013
Options :
file : '/usr/share/cacti/site/csv/cacti_example.csv'
config : 'conf/config.ini'
--------------- Creating Host 1.1.1.1 ---------------
DBI connect('cacti:localhost','',...) failed: Access denied for user 'root'@'localhost' (using password: NO) at lib/Functions.pm line 288
Unable to connect to cacti database 'cacti'
Options :
Host : 1.1.1.1
Description : lol1
Network :
Template : Cisco Routeur
reject file : 201304211723_import_host_to_cacti.csv.rej
Reading Config file...
param :'database_username' value :'cacti'
param2 :'database_default'param :'database_password' value :'xxxxxxx'
param2 :'database_default'param :'basepath' value :''
param2 :'database_default'param :'database_default' value :'cacti'
param :'database_hostname' value :'localhost'
param :'database_port' value :'3306'
param2 :'database_username'param :'dbtype' value :'mysql'
param2 :'database_username'Param 'database_username' does not exists in config file '/etc/cacti/debian.php'
Param 'database_password' does not exists in config file '/etc/cacti/debian.php'
Param 'database_port' does not exists in config file '/etc/cacti/debian.php'
Param 'weathermap_position_file' does not exists in config file 'conf/config.ini'
db_name : cacti
db_hostname : localhost
db_user :
db_pass :
db_port :
template : csv/device_template.csv
Cacti Path : /usr/share/cacti/site/
Connecting to MySQL Database...

the contents of the debian.php file :

<?php
##
## database access settings in php format
## automatically generated from /etc/dbconfig-common/cacti.conf
## by /usr/sbin/dbconfig-generate-include
## Thu, 26 Apr 2012 16:40:10 +0200
##
## by default this file is managed via ucf, so you shouldn't have to
## worry about manual changes being silently discarded. *however*,
## you'll probably also want to edit the configuration file mentioned
## above too.
##
$database_username="cacti";
$database_password="xxxxxxx";
$basepath="";
$database_default="cacti";
$database_hostname="localhost";
$database_port="3306";
$dbtype="mysql";

Thanks for your help if you read my post.
Cacti Version - 0.8.8a
Plugin Architecture - 3.1
Poller Type - spine
Server Info - Linux
Web Server - Apache/2.2.22 (Ubuntu)
PHP - 5.3.10-1ubuntu3.6 with Suhosin-Patch (cli)
MySQL - 5.5.29-0ubuntu0.12.04.2
RRDTool - 1.4.7
X-dark
Cacti User
Posts: 111
Joined: Wed Jun 18, 2008 10:21 am
Location: France

Re: Provisioning from CSV by SFR

Post by X-dark »

Ouch!
The function GetDBParam would need to be properly rewritten. The way it is written, it will only work if the parameters are in the same order as in your config file.
The quick and ugly fix is to reorder the @param array to match the order of your config file.
The proper fix is to do the matching the other way (ie for each config setting found in the config file look into a hash if it is an expected setting and store it).

More generally, a lot of things in this set of scripts need to be rewritten. They mostly work the way they are written now but a lot of things are neither efficient nor elegantly written and thus not easy to build upon for more specific needs. Areas most needing improvements are:
- Config files parsing
- Logging
- DB handling
Cedric Girard
tosage
Cacti User
Posts: 164
Joined: Wed Jul 28, 2010 5:05 am
Location: France

Re: Provisioning from CSV by SFR

Post by tosage »

Ok, that's what I thought :)

Merci !
Cacti Version - 0.8.8a
Plugin Architecture - 3.1
Poller Type - spine
Server Info - Linux
Web Server - Apache/2.2.22 (Ubuntu)
PHP - 5.3.10-1ubuntu3.6 with Suhosin-Patch (cli)
MySQL - 5.5.29-0ubuntu0.12.04.2
RRDTool - 1.4.7
denek
Posts: 2
Joined: Fri Jun 14, 2013 3:23 am

Re: Provisioning from CSV by SFR

Post by denek »

Hi

I want to add 200+ IP addresses as including name of the host. Such as;

Description: xyz Server
Hostname: 192.168.1.1
Host Template: Cisco
Thold Up/Down Email Notification: Global List
Downed Device Detection: Ping
Ping Method: ICMP Ping

How can I configure this options?

adding manually like this takes to much time. Give advise how to handle this issue with script, please !

P.s: my cacti "version 0.8.8a"

http://www.cacti.net/downloads/docs/htm ... evice.html

I want to add "description" and "hostname" from csv file !
tosage
Cacti User
Posts: 164
Joined: Wed Jul 28, 2010 5:05 am
Location: France

Re: Provisioning from CSV by SFR

Post by tosage »

You can open your CSV like this and execute the script for each line

open CSV, '<', $csvfile or log_and_die ("Impossible de lire le fichier CSV, le fichier est-il present ? Probleme de droit ?");
while (<CSV>){
next if ($. == 1);
my $ligne = $_;
chomp ($ligne);
.....
.....
.....
system("php $cacti_path/cli/add_device.php --ip=$datas[0] --description=$datas[1] --template=$host_template_id --version=$datas[3] --community=$datas[4] --avail=$datas[5]");

Extract from a perl script.
Cacti Version - 0.8.8a
Plugin Architecture - 3.1
Poller Type - spine
Server Info - Linux
Web Server - Apache/2.2.22 (Ubuntu)
PHP - 5.3.10-1ubuntu3.6 with Suhosin-Patch (cli)
MySQL - 5.5.29-0ubuntu0.12.04.2
RRDTool - 1.4.7
denek
Posts: 2
Joined: Fri Jun 14, 2013 3:23 am

Re: Provisioning from CSV by SFR

Post by denek »

@tosage,

First of all thanks for getting back to me so quick. But I could not clearly understood how to apply example code to you gave. Please give me more detail.

In A column including Description
In B column including IP addresses
In C column including Host Template: Cisco
In D column includingThold Up/Down Email Notification: Global List
In E column includingDowned Device Detection: Ping
In F column includingPing Method: ICMP Ping

According to these informations would you please create sample, it would be very helpful for me.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests