Quick and dirty script to add devices to cacti

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

Moderators: Developers, Moderators

Post Reply
Floppe
Posts: 6
Joined: Wed Apr 20, 2005 4:03 am

Post by Floppe »

I'm searching for something like this.

What I need is a script that adds data sources that uses a script (data input method) and then the graphs for the data source.
abadaba
Cacti User
Posts: 52
Joined: Wed Nov 23, 2005 10:43 pm

Post by abadaba »

Ok ive been picking my brain apart for hours trying too figure out how too add 2,400 cable modems using this...What i want is too be able too add all of them on one run like

Code: Select all

php add_device.php 1 SimpleTest 10.*.*.0/10.*.*.254 2 0 [code]

Or just being able too add the whole group of IP's in one shot...And then adding the graphs too go along with them...I know i can figur ethis out after hours of hammering away...But has anyone attempted this, and can maybe share some insight?
| Cacti 0.8.6h | MySQL 5.0.22 | PHP 5.1.2
|Apache2.20 | RRDtool 1.2.13 | CentOS 4.3 | P4 3200Mhz | 2.5GB |
User avatar
timi
Cacti User
Posts: 240
Joined: Wed Nov 09, 2005 8:35 am
Location: Timisoara, Romania
Contact:

Post by timi »

hi, i have a problem when i add a device, in log file i get this error

11/14/2006 03:04:41 PM - CMDPHP: Poller[0] ERROR: SQL Exec Failed "update data_template_data set rrd_maximum='|query_ifSpeed|' where local_data_id=11366" the data_template_data should be data_template_rrd, how can i change this ? becouse all gigabit interface have maximum value for traffic set to 1000000000 instead of 10000000000

i'm using cacti 0.8.6h
abadaba
Cacti User
Posts: 52
Joined: Wed Nov 23, 2005 10:43 pm

Post by abadaba »

When i run php add_device.php 1 test21 10.201.*.* public 1 I dont get any errors. But when i check the devices, it didnt add anything at all...What could be the problem?
| Cacti 0.8.6h | MySQL 5.0.22 | PHP 5.1.2
|Apache2.20 | RRDtool 1.2.13 | CentOS 4.3 | P4 3200Mhz | 2.5GB |
Exo7
Cacti User
Posts: 136
Joined: Wed Jul 13, 2005 4:50 pm

Post by Exo7 »

When i run php add_device.php 1 test21 10.201.*.* public 1 I dont get any errors. But when i check the devices, it didnt add anything at all...What could be the problem?
The script is not supposed to handle wildcard.
You should add a 0 at the end to enable the host

Code: Select all

Usage:
add_device.php templateid description IP snmp_community snmp_version disable

Where:
	- templateid is a number (read below to get a list of templates)
	- description: the name that will be displayed by Cacti in the graphs
	- IP: self explanatory (can also be a FQDN)
	- snmp_community: community string
	- snmp_version: 1/2
	- disable: 1 to add this host but to disable checks and 0 to enable it
abadaba
Cacti User
Posts: 52
Joined: Wed Nov 23, 2005 10:43 pm

Post by abadaba »

so i would have too add each device by hand? The problem is, i need too add now 3,200 cable modems lol
| Cacti 0.8.6h | MySQL 5.0.22 | PHP 5.1.2
|Apache2.20 | RRDtool 1.2.13 | CentOS 4.3 | P4 3200Mhz | 2.5GB |
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

abadaba wrote:so i would have too add each device by hand? The problem is, i need too add now 3,200 cable modems lol
Personally, I created a list of all needed ip addresses and built a shell script, each line with a new address. So your shell script would hold 3200 entries. Not that big of a deal ...
Reinhard
abadaba
Cacti User
Posts: 52
Joined: Wed Nov 23, 2005 10:43 pm

Post by abadaba »

Yeah i seen the shell script thingy...Its just a way of getting them all by hand into the csv that would kill me lol...
| Cacti 0.8.6h | MySQL 5.0.22 | PHP 5.1.2
|Apache2.20 | RRDtool 1.2.13 | CentOS 4.3 | P4 3200Mhz | 2.5GB |
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

By hand, why? I dumped my IP@ to some file and performed some "change all" magic to them. It's a one-liner
Reinhard
Last edited by gandalf on Thu Nov 16, 2006 1:24 pm, edited 1 time in total.
abadaba
Cacti User
Posts: 52
Joined: Wed Nov 23, 2005 10:43 pm

Post by abadaba »

you did this within the CMTS?
| Cacti 0.8.6h | MySQL 5.0.22 | PHP 5.1.2
|Apache2.20 | RRDtool 1.2.13 | CentOS 4.3 | P4 3200Mhz | 2.5GB |
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

The dump was done on our "company wide configuration management database"
Reinhard
abadaba
Cacti User
Posts: 52
Joined: Wed Nov 23, 2005 10:43 pm

Post by abadaba »

Ive figured out how too dump all of the IP's in the CMTS. Now ive got that bash script, and it really cornfuses the mess outaa me. Im really not good at scripting at all. So does anyone have an idea of how it works? A lil simple how too would go a long way for me. = }
| Cacti 0.8.6h | MySQL 5.0.22 | PHP 5.1.2
|Apache2.20 | RRDtool 1.2.13 | CentOS 4.3 | P4 3200Mhz | 2.5GB |
Exo7
Cacti User
Posts: 136
Joined: Wed Jul 13, 2005 4:50 pm

Post by Exo7 »

for each device in your file, call the php script with the device as argument.

in Perl, it is something like this (not tested, you should modify to your conveniance. My script is far more complicated to create a lot of different graphs).

Code: Select all

#!/usr/bin/perl

use strict;

my $infile = "yourfile.csv";  # format should be "ip;hostname;snmp;snmp version;template_number"

open(INFILE, "< $infile") or die "can't open $infile";

while(<INFILE>) {
  next if (/^$/ || /^#/);       #  empty lines and commentary
  chomp();                      #  get rid of trailing \n  
  my @line = split(/;/);        #  read the line, separator is character ';'
  my $ip = $line[0];  
  my $hostname = $line[1];
  my $snmp = $line[2];
  my $snmp_v = $line[3];
  my $t_number = $line[4];
  system("/usr/share/cacti/site/add_device.php $t_number $hostname $ip $snmp $snmp_v 0");
}

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

Post by timi »

for each device from you list something like this...
--- cut ---
$command="./add_tree.php --type node --node-type header --tree-ID $tree_id --parent-node $leaf_id --name '$name'\n";
$test=qx!$command!;
if ($test =~ /.*Added Node node-id: \((\d*)\).*/)
{ $node_id=$1; }
--- cut ---

for the node_id run add_perms for each useres in your table...
--- cut ---
$query = "select * from user_auth";
$sth=$dbh->prepare($query) || &err_msg($dbh->errstr);
$rv=$sth->execute || &err_msg($sth->errstr);
while (@idz=$sth->fetchrow_array) {
$command="./add_perms.php --user-id $idz[0] --item-type tree --item-id $node_id\n";
$test=qx!$command!;
}
--- cut ---

find out the interface you want to monitor ... and create graphs...
--- cut ---
$command="./add_graphs.php --graph-type ds --graph-template-id $id --host-id $host_id --snmp-query-id $query_id --snmp-query-type-id $query_type --snmp-field $snmp_field --list-snmp-values\n";
$test=qx!$command!;
@int=split("\n",$test);
foreach my $interface (@int){
add_graphs.php and add_tree.php here for what do you need....
}
exit;
SeanW
Posts: 26
Joined: Tue Nov 28, 2006 9:17 am
Location: Winnipeg, Canada
Contact:

Automating the web interface

Post by SeanW »

I'm trying to migrate from MRTG to Cacti and found this thread. I started off by writing a script that reads information out of the MRTG config file and then generates the same graphs:

Code: Select all

#!/usr/bin/perl

use strict;

foreach my $file (@ARGV) {
        open IN, "<$file" or next;

        my $data;
        while (<IN>) {

                next unless (/^Target\[(.*?)[\-_]?\d*\]: (.*):(\w+)@([\w\.]*):/);


                my ($name, $oid, $community, $address) = ($1, $2, $3, $4);

                $data->{$name} = {} unless $data->{$name};
                $data->{$name}->{community} = $community unless ($data->{$name}->{community});
                $data->{$name}->{address} = $address unless ($data->{$name}->{address});
                push @{$data->{$name}->{intf}}, $oid;

        }

        foreach my $host (keys %$data) {

                my $string = sprintf("./add_device.php %d %s %s %s %d %d",
                        5,
                        $host,
                        $data->{$host}->{address},
                        $data->{$host}->{community},
                        2,
                        0);
                my $out = `$string`;
                $out =~ /device-id: \((\d+)\)/;
                my $deviceid = $1;
                print "Added as $1\n";
                foreach my $intf (@{$data->{$host}->{intf}}) {
                        my $addgraph = sprintf("./add_graph.php --graph-type %s --graph-template-id %d --host-id %d --snmp-query-id %d --snmp-query-type-id %d --snmp-field %s --snmp-value %d",
                                 "ds",
                                2,
                                $deviceid,
                                1,
                                14,
                                "ifIndex",
                                $intf);
                        my $out =  `$addgraph\n`;
                }

        }

}
Maybe I did something wrong, but the data queries never got associated with the new devices, so the add_graph failed because ifIndex wasn't a valid snmp field.

So, I wrote another script to go through the web interface and add them. I then figured that while I was there I may as well create all the graphs. Here's a script that logs in, iterates through all the devices, and creates all available graphs. I cheated a bit by forcing all the devices to appear on one screen by changing my display settings.

Code: Select all

#!/usr/bin/perl

use strict;
use WWW::Mechanize;
use Data::Dumper;
my $CACTIURL = "ENTER YOUR CACTI URL HERE";
my $ADMINPASSWORD = "ADMIN PASSWORD";


my $mech = WWW::Mechanize->new();
$mech->get($CACTIURL);

$mech->set_visible("admin", $ADMINPASSWORD);
$mech->click;

$mech->follow_link(text => "Devices");



my @links = $mech->find_all_links(url_regex => qr /host.php\?action=edit&id=/);

foreach my $i (@links) {
        $mech->get($i);

        # Set snmp_query_id to SNMP - Interface Statistics and hit the add_dq button
        eval {
                $mech->select(snmp_query_id => "SNMP - Interface Statistics");

                $mech->click_button(name=>"add_dq");
        };

        $mech->follow_link(text => "Create Graphs for this Host");

        $mech->form(2);
        my @inputs = $mech->current_form()->inputs();
        my $submitbutton;
        foreach my $input (@inputs) {
                if ($input->type == "checkbox" && $input->name =~ /[sc]g_\d+/) {
                        $mech->tick($input->name, "on");
                }
                $submitbutton = $input if ($input->type == "image"); # get the last one
        }

        $mech->select(sgg_1 => "In/Out Bits (64-bit Counters)");
        $mech->click_button(input => $submitbutton);

        foreach my $input (@inputs) {
                $submitbutton = $input if ($input->type == "image"); # get the last one
        }

        $mech->click_button(input => $submitbutton);


        # final create
}
Sean
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests