jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [trunk/] [modules/] [traffic/] [platform/] [openbsd.pm] - Rev 22 Go to most recent revision

Compare with Previous - Blame - Download


package HotSaNICmod::OSdep;

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

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

sub sample {
  my %args=@_;

  undef my %maxin;
  undef my %maxout;
  undef my %devin;
  undef my %devout;
  undef my %sampletime;
  undef my %snmpinterfaces;
  undef my %snmpmethods;

  foreach (keys(%args)) {
    if ( index($_,"DEV") >= 0) { 
      ($dev,$maxin,$maxout,$description)=split /,/,$args{$_};
      if ( index($dev,"SNMP\:") >= 0) {
        (undef,$host,$interface,$community,$method)=split /:/,$dev;
        if (defined $method) { if ($method eq "walk") { $SNMPWALK{$host}=1 }; }
        ($dev=$host."_".$interface) =~ s/\./_/g;
        if (defined $snmpinterfaces{$host.":".$community}) {
          $snmpinterfaces{$host.":".$community}=$snmpinterfaces{$host.":".$community}." ".$interface;
          }
        else { $snmpinterfaces{$host.":".$community}=$interface; }
        }
      else { $dev =~ s/:/_/g; }
      $maxin{$dev}=$maxin;
      $maxout{$dev}=$maxout;
      $devin{$dev}="U";
      $devout{$dev}="U";
      $sampletime{$dev}=time;
      }
    }

# untested, but very similar to FreeBSD so should work. will test soon.
  open(IN,"netstat -ib | grep Link |");
  while(<IN>) {
    chomp;
    s/[0-9a-f]{2}(:([0-9a-f]{2})){5}//;  # remove mac address
    ($name, $d, $d, $input, $output)=split;
    $devin{$name}=$input;
    $devout{$name}=$output;
    $sampletime{$name}=time;
    }
  close(IN);

  # then query SNMP hosts and check interfaces
  #

  if (%snmpinterfaces) {
#   if ($DEBUGLEVEL >= 6) { print "SNMP defined, checking args...\n"; }
    $SNMPGET_CMD=$args{"SNMPGET"};
    $SNMPWALK_CMD=$args{"SNMPWALK"};
    foreach $combined (sort keys %snmpinterfaces) {
      ($host,$community)=split /:/,$combined;
      $interfaces=$snmpinterfaces{$combined};
      undef @results;
      if ( ! defined $SNMPWALK{$host} ) {
        if ( ($SNMPGET_CMD ne "") && ($SNMPGET_CMD ne "not configured") ) {
          @results=HotSaNICsnmp::snmp_sysbulk($SNMPGET_CMD,$host,$community,".1.3.6.1.2.1.2.2.1.10 .1.3.6.1.2.1.2.2.1.16",$interfaces); }
        else { print time,": ",$args{MODNAME},"Can't query SNMP target $host - SNMPGET not configured in global settings!"; }
        }
      else {
        if ( ($SNMPWALK_CMD ne "") && ($SNMPWALK_CMD ne "not configured") ) {
          @unfiltered=HotSaNICsnmp::snmp_sysquery($SNMPWALK_CMD,$host,$community,"interfaces","ifInOctets","ifOutOctets");
          foreach $line (@unfiltered) {
            # check if interface number is configured
            ($interface)=split / /,$line;
            if (" $interfaces " =~ / $interface /) { push @results,$line; }
            }
          }
        else { print time,": ",$args{MODNAME},"Can't query SNMP target $host - SNMPGET not configured in global settings!"; }
        }
      if (defined @results) {
        foreach $line (@results) {
          $line =~ s/[A-Za-z]*[0-9]*: //g;
          ($interface,$input,$output)=split / /,$line;
          ($nn=$host."_$interface") =~ s/\./_/g;
          $devin{$nn}=$input;
          $devout{$nn}=$output;
          $sampletime{$name}=time;
          }
        }
      }
    }

  foreach $name (keys %maxin) {

    # build new database if needed
    #
    if (! -e "rrd/$name.rrd" ) {
          
      # get max. transmission value
      #
      my $max=$maxin{$name};
      if ($max < $maxout{$name}) { $max=$maxout{$name} };
        
      # if max-transmission is not set, assume 100 MBit.
      #
      if ($max == 0) { $max=12500000; }
      system("./makerrd","$name","$max")
      }
        
    # update database
    RRDs::update "rrd/$name.rrd",$sampletime{$name}.":".$devin{$name}.":".$devout{$name};
    if ($ERROR = RRDs::error) { print time,": ",$args{MODNAME},": unable to update `$name.rrd': $ERROR\n"; }
    }

  }

1;


Powered by WebSVN 2.2.1