#!/usr/bin/perl use strict; use DBI; use Getopt::Long qw(:config no_ignore_case); my %opt; GetOptions( "f=s" => \$opt{'f'}, "r=i" => \$opt{'r'}, "i=i" => \$opt{'i'}, "w=s" => \$opt{'w'}, "c=s" => \$opt{'c'} ); unless ($opt{'f'} && $opt{'r'} && $opt{'i'} && $opt{'w'} && $opt{'c'}) { print "Usage: $0 \n"; print "Example: $0 -f /etc/rtg.conf -r 1 -i 2 -w 1024 -c 4096\n"; print " -f\t RTG Config file\n"; print " -r\t RTG Router ID\n"; print " -i\t RTG Interface ID\n"; print " -w\t Warning In/Out rate bits\n"; print " -c\t Critical In/Out rate in bits\n"; exit 3; } if ( ! -f $opt{'f'} ) { print "File ".$opt{'f'}." does not exist\n"; exit 3; } my %RTGCONF; open CFG, "<".$opt{'f'}; while() { chomp; my($d,$v) = split('\s+', $_, 2); $RTGCONF{$d} = $v; } close CFG; my $dbh = DBI->connect("DBI:mysql:".$RTGCONF{'DB_Database'}.":host=".$RTGCONF{'DB_Host'},$RTGCONF{'DB_User'},$RTGCONF{'DB_Pass'}); my $sth = $dbh->prepare("SELECT * FROM ifOutOctets_".$opt{'r'}." WHERE id='".$opt{'i'}."' ORDER BY dtime DESC LIMIT 1"); my $rv = $sth->execute(); my $row_out = $sth->fetchrow_hashref(); my $rate_out = int((($row_out->{'counter'} * 8) / $RTGCONF{'Interval'}) + .5); my $sth = $dbh->prepare("SELECT * FROM ifInOctets_".$opt{'r'}." WHERE id='".$opt{'i'}."' ORDER BY dtime DESC LIMIT 1"); my $rv = $sth->execute(); my $row_in = $sth->fetchrow_hashref(); my $rate_in = int((($row_in->{'counter'} * 8) / $RTGCONF{'Interval'}) + .5); print "In: $rate_in/bps Out: $rate_out/bps\n"; exit 2 if $rate_in > $opt{'c'} or $rate_out > $opt{'c'}; exit 1 if $rate_in > $opt{'w'} or $rate_out > $opt{'w'}; exit 0;