jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-jablonecka/] [modules/] [traffic/] [platform/] [syssnmp.pm] - Rev 1

Compare with Previous - Blame - Download


package syssnmp;

use lib "../../../lib";
use HotSaNICsnmp;

sub version {
        ($VERSION = '$Revision: 1.11 $') =~ s/.*(\d+\.\d+).*/$1/;
        return "syssnmp.pm $VERSION";
        }

sub do_snmp {
        my $oid = '.1.3.6.1.2.1.2.2.1';
              # interfaces.ifTable.ifEntry.ifDesc

        my $suf_descr = '.2';
                    # .ifDescr
        my $suf_in    = '.10';
                    # .ifInOctets
        my $suf_out   = '.16';
                    # .ifOutOctets

        my (%targets) = @_;

        foreach ( keys(%targets) ) {
#       print "1 $_\n";
                my ($host, $com) = split /_/;
                my ($res, $lastnic, @nics);     
                
                my $i = 0;
                my $switch = 0;
                my $portnum = "none";
                for (@{$targets{$_}}) {
                    my (undef, $portnum) = split /port/;

                    if ("port$portnum" eq $_) {
#                       print "Je to switch\n";
                        $switch = 1;
                        $port = $portnum;
                        if($i == 0) {
                                $nics{$_} = 0;
                                $lastnic = $port;
                                $i++;
                        } else {
                                $nics{$lastnic} = $_;
                                $i = 0;
                        }
                    } else {
                        if($i == 0) {
                                $nics{$_} = 0;
                                $lastnic = $_;
                                $i++;
                        } else {
                                $nics{$lastnic} = $_;
                                $i = 0;
                        }
                    }
                }
#                   print "2 $_ a $port\n";
#                   print "3 $host a $com b $lastnic c $nics{$dev}\n";

                my @query = ($oid.$suf_descr, $oid.$suf_in, $oid.$suf_out);
#       print "4 @query\n";
                $res = HotSaNICsnmp::snmp_walk($host, $com, @query);
#       print "5 $res\n";
        
                #pokud nejsou vysledky tak se zapise do databaze Undef stav
                if(keys(%$res) == 0) {
#                       print "tady to je kdyz nejni spojeni tak se do rrd da undef";
                        if ($switch == 1) {
                            do_rrd($host."_port".$_, "U", "U", $nics{$_}) foreach keys(%nics);
                        } else {
                            do_rrd($host."_".$_, "U", "U", $nics{$_}) foreach keys(%nics);
                        }
                        return undef;   
                }

                $necodef = defined($$res{$oid.$suf_descr.".".$i});
#               print "5a $necodef\n";  
                #jedem na prohledani vsech ifaces a pokud souhlasi tak se pise do rrd
                for ( my $i=1; defined($$res{$oid.$suf_descr.".".$i}); $i++) {
                        if ($switch == 1) {
                            #toto je cast pro switch a jde se na rrd pokud souhlasi poradi portu
                            #jmeno iface se neda pro rozpoznani pouzit protoze je pro vsechny stejne
                            my $dev = $$res{$oid.$suf_descr.".".$i};
#                           print "5b $dev\n";
                            if(defined($nics{$i})) {
#                           print "5b $nics{$i}\n";
                                do_rrd($host."_port".$i, $$res{$oid.$suf_in.".".$i}, $$res{$oid.$suf_out.".".$i}, $nics{$i});
                            }
                        } else {
                            #zde rozpoznavame podle jmena iface
                            my $dev = $$res{$oid.$suf_descr.".".$i};
                            if(defined($nics{$dev})) {
                                do_rrd($host."_".$dev, $$res{$oid.$suf_in.".".$i}, $$res{$oid.$suf_out.".".$i}, $nics{$dev});
                            }
                        }
                }
        }
}

sub do_rrd {
        my ($name, $in, $out, $max) = @_;
#print "mam tu $max";
        if (! -e "rrd/$name.rrd" ) {
                system("./makerrd","$name","$max")
        }

        RRDs::update "rrd/$name.rrd",time.":".$in.":".$out;
        if ($ERROR = RRDs::error) { print time,": ",$args{MODNAME},": unable to update `$name.rrd': $ERROR\n"; }
}

1;

Powered by WebSVN 2.2.1