First Script Help!

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

Moderators: Developers, Moderators

Post Reply
myconid
Posts: 4
Joined: Fri Jan 06, 2006 4:06 pm

First Script Help!

Post by myconid »

I am making my very first script, and having some problems.

It is a two part script, a data grabber, and the cacti script.

Code: Select all

[cacti@argon bin]$ cat iptables-genstats.pl 
#!/usr/bin/perl
use Data::Dumper; 
my $iptables = '/sbin/iptables';

my @chains = qw(STATS_IN STATS_OUT);

# Gather data from chains.
foreach (@chains){
  $data{$_} = `$iptables -L $_ -v -x -n`;  
  `$iptables -Z $_`;
}


foreach $chain (@chains){
  @parts = split('\n',$data{$chain});
  foreach (@parts){
    $data = ~ /[\W+]?[0-9]+\W+([0-9]+)\W+[A-Z]+\W+[a-z]+\W+([0-9\.\/])+\W+[0-9\.\/]+\W+(.*)/;
    my ($junk, $port) = split(":",$3);
    next if (!$1);
    if (!$port){
      $port = 'none ';
    }
    $output{$port}{$chain} = $1;
  }
}

for (keys(%output)){
  foreach $chain (@chains){
    $printtext = ($_);
    chop($printtext);
    next if (!$output{$_}{$chains[0]});
    print "$printtext," . $output{$_}{$chains[0]} . "," . $output{$_}{$chains[1]} ."\n";
    delete $output{$_};
  }
}
The output looks like this:

Code: Select all

[root@argon bin]# ./iptables-genstats.pl 
143,3417,9343
25,1291,1795
80,498,539
none,563,480
22,360,384
This is run from cron every 5 minutes, and output to /var/log/iptables.stats

I then have a script for cacti:

Code: Select all

[root@argon scripts]# cat iptables.pl 
#!/usr/bin/perl
use Data::Dumper;

$STAT_FILE = "/var/log/iptables.stats";

our $data;

sub process_stats{
        $val = shift();
        # Die unless we can locate the stats file
        if (!open(STATS,$STAT_FILE)) {
                        die "Failed to open $STAT_FILE: $!\n";
        }


        while(<STATS>){
                @incoming = split(',',$_);
                chomp($incoming[2]);
                $in{$incoming[0]} = {in=> $incoming[1], out=> $incoming[2]};
                
        }
        close(STATS);
}


if ($ARGV[0])
{ 
  process_stats($ARGV[0]);
  print "in: 0$in{$ARGV[0]}->{in} out: 0$in{$ARGV[0]}->{out}";
}
else
{
        print "Usage: $0 Chain\n";
}
This seems to work just fine...

Code: Select all

[root@argon scripts]# ./iptables.pl 80
in: 01725 out: 02852[root@argon scripts]# ./iptables.pl none
in: 013416 out: 019357[root@argon scripts]# ./iptables.pl 200
in: 0 out: 0[root@argon scripts]# 
Excuse my little 0 hack :~).

So I made a new data template and such:

Image
------------
Image
------------
Image


The RDD file looks empty( every database line is NAN), and so does the graph..

What am I doing wrong?

This also might have something to do with it:



[/img]

Code: Select all

[cacti@argon rra]$ php /var/www/shtml/monitoring/poller.php 
OK u:0.00 s:0.00 r:1.01
OK u:0.00 s:0.00 r:1.01
OK u:0.00 s:0.00 r:1.01
OK u:0.00 s:0.00 r:1.01
OK u:0.00 s:0.00 r:1.01
ERROR: Not enough arguments
OK u:0.00 s:0.00 r:1.02
OK u:0.00 s:0.00 r:1.02
ERROR: Not enough arguments
OK u:0.00 s:0.00 r:1.02
OK u:0.00 s:0.00 r:1.02
Content-type: text/html
X-Powered-By: PHP/4.3.9
[/code]
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

The error is within

Code: Select all

[root@argon scripts]# ./iptables.pl 80
in: 01725 out: 02852[root@argon scripts]# ./iptables.pl none
in: 013416 out: 019357[root@argon scripts]# ./iptables.pl 200
in: 0 out: 0[root@argon scripts]# 
Simply delete the space between the colon and the value.
Tip: to debug, goto Settings and select DEBUG from the Logging Levels Dropdown. Find all results in cacti.log.
Nice work, btw. Suppose, there are lots of cacti fans interested in this ...
Reinhard
myconid
Posts: 4
Joined: Fri Jan 06, 2006 4:06 pm

Post by myconid »

That seemed to have fixed it, as well as adding a final space.

It doesn't work quite as I expected (traffic seems realllly low).
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

Please pay attention to the ds type selection: COUNTER is for incremental ds only (as traffic), GAUGE is for absolute values (as e.g. number of users logged in).
To debug: Please switch to DEBUG mode (Settings!) to find the raw readings in cacti.log. Then fetch the data from rrd using

Code: Select all

rrdtool fetch <file> AVERAGE
which gives you the data of the last day by default
Reinhard
myconid
Posts: 4
Joined: Fri Jan 06, 2006 4:06 pm

Post by myconid »

My script resets the values from IPTABLES on every read (every 5 minutes)... IPTables resets after 4 gigs of traffic I think.

Shouldnt that be counter?
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Post by gandalf »

No, when data is reset each time, your reading is kinda Temperature or Fan Speed or the like. It is an absolute reading. See http://www.rrdtool.org for more. So you should set the ds's to GAUGE. For existing rrds you may use

Code: Select all

rrdtool tune -d
where
--data-source-type│-d ds-name:DST
               alter the type DST of a data source.
But I suppose your existing readings would no be valueable any more; but I did not try to change the data source type myself.
Reinhard
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests