#!/usr/bin/perl ###### # ssupdate: update counters to the current traffic (crontab every 5 mins) # Author: Jimmy Scott # Nick: Sick` # Mail: jimmy *at* inet-solutions.be # Address: Jimmy Scott # Goordijk 66 # 2930 BRASSCHAAT # BELGIUM # source: http://pub.devbox.be ### # # Copyright (C) 2003 Jimmy Scott # # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # 3. The names of the authors may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF # THE POSSIBILITY OF SUCH DAMAGE. # ###### use Net::SNMP; use Fcntl qw(:DEFAULT :flock); ###### # config options ### $basedir="/home/jimmy/snmpdev/ss"; $hostname="10.0.0.1"; $community="public"; $version=1; $instring="1.3.6.1.2.1.2.2.1.10."; $outstring="1.3.6.1.2.1.2.2.1.16."; $max=4294967296; ###### # checking and building hier ### sub newfile { my $file = shift || die "wrong usage of the function &newfile()\n"; open NF, ">$file" || die "could not create $file\n"; print NF "0"; close NF; } # check/create basedir if (! -f "$basedir" ) { mkdir "$basedir" || die "could not create directory $basedir\n"; } # check/create lockfile if (! -f "$basedir/ss.lock" ) { &newfile("$basedir/ss.lock"); } # check/create flow and count lines for ($i=1;$i<=24;$i++) { # current flow if (! -f "$basedir/$i.in.f" ) { &newfile("$basedir/$i.in.f"); } if (! -f "$basedir/$i.out.f" ) { &newfile("$basedir/$i.out.f"); } # current flow cicle count if (! -f "$basedir/$i.in.c" ) { &newfile("$basedir/$i.in.c"); } if (! -f "$basedir/$i.out.c" ) { &newfile("$basedir/$i.out.c"); } # offset files if (! -f "$basedir/$i.in.o" ) { &newfile("$basedir/$i.in.o"); } if (! -f "$basedir/$i.out.o" ) { &newfile("$basedir/$i.out.o"); } } ###### # getting the lock ### open LOCK, "$basedir/ss.lock" || die "can't open lockfile $basedir/ss.lock\n"; flock LOCK, LOCK_EX || die "can't lock lockfile $basedir/ss.lock\n"; ###### # getting connected to the 3com superstack ### ($session, $error) = Net::SNMP->session( -nonblocking => 0, -hostname => $hostname, -community => $community, -version => $version, ); if (! defined($session) ) { die "Error: $error\n"; } ###### # update process ### for ($i=1;$i<=24;$i++) { my ($is,$os,$ir,$or,$if,$of,$ic,$oc); $is = $instring . ($i + 100); $os = $outstring . ($i + 100); ###### # IN SNMP ### $ir = $session->get_request( -varbindlist => [$is], ); if (!defined($ir)) { $error=$session->error; print "Error In: $error\n"; $session->close; exit 1; } $ir=$ir->{$is}; print "DEBUG: IN : $is : $ir\n"; ###### # IN FLOW FILE ### open FLOW, "$basedir/$i.in.f" || die "can't open file $basedir/$i.in.f\n"; $if=; close FLOW; open FLOW, ">$basedir/$i.in.f" || die "can't open file >$basedir/$i.in.f\n"; print FLOW "$ir"; close FLOW; print "DEBUG: IN : $i.in.f : $if\n"; ###### # IN FLOW COUNT ### if ($if > $ir) { open COUNT, "$basedir/$i.in.c" || die "can't open file $basedir/$i.in.c\n"; $ic=; close COUNT; $ic++; open COUNT, ">$basedir/$i.in.c" || die "can't open file >$basedir/$i.in.c\n"; print COUNT "$ic"; close COUNT; print "DEBUG: IN CHANGED COUNT: $i.in.c : $ic\n"; } ###### # OUT SNMP ### $or = $session->get_request( -varbindlist => [$os], ); if (!defined($or)) { $error=$session->error; print "Error In: $error\n"; $session->close; exit 1; } $or=$or->{$os}; print "DEBUG: OUT : $os : $or\n"; ###### # OUT FLOW FILE ### open FLOW, "$basedir/$i.out.f" || die "can't open file $basedir/$i.out.f\n"; $of=; close FLOW; open FLOW, ">$basedir/$i.out.f" || die "can't open file >$basedir/$i.out.f\n"; print FLOW "$or"; close FLOW; print "DEBUG: OUT : $i.out.f : $of\n"; ###### # OUT FLOW COUNT ### if ($of > $or) { open COUNT, "$basedir/$i.out.c" || die "can't open file $basedir/$i.out.c\n"; $oc=; close COUNT; $oc++; open COUNT, ">$basedir/$i.out.c" || die "can't open file >$basedir/$i.out.c\n"; print COUNT "$oc"; close COUNT; print "DEBUG: OUT CHANGED COUNT : $i.out.c : $oc\n"; } } ###### # disconnecting from the superstack ### $session->close; ###### # releasing the lock ### close LOCK; exit;