jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-pre6/] [modules/] [system/] [platform/] [syssnmp.pm] - Rev 17 Go to most recent revision

Compare with Previous - Blame - Download


package HotSaNICmod::syssnmp;

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

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

sub sample {
  %args=@_;

  foreach $entry (@{$args{HOST}}) { 
  my ($host,$community,$item,$dbname,$name,$file,$description,$targets)=HotSaNICmod::common::get_names($entry);
    if ($host ne "") {
      # Now do the real work ...
      if   (index($targets,"cpu"  )>=0) { do_cpu($host,$community,$args{VARDIR}); }
      if   (index($targets,"load" )>=0) { do_load($host,$community); }
      if   (index($targets,"proc" )>=0) { do_proc($host,$community); }
      if ( (index($targets,"mem"  )>=0) or (index($targets,"swap") >=0) ) { do_mem($host,$community); }
      if   (index($targets,"users")>=0) { do_users($host,$community); }
      }
    }
  }

# Gather data from SNMP agent

# uptime-oid: .1.3.6.1.2.1.25.1.1.0

sub do_cpu {
        my ($host,$com,$vardir)=@_;
        my $oid = '.1.3.6.1.4.1.2021.11'; # enterprises.ucdavis.systemStats
        my $suf_user   = '.50.0';         # .ssCpuRawUser.0
        my $suf_nice   = '.51.0';         # .ssCpuRawNice.0
        my $suf_system = '.52.0';         # .ssCpuRawSystem.0
        my $suf_idle   = '.53.0';         # .ssCpuRawIdle.0
        my $suf_intr   = '.56.0';         # .ssCpuRawIdle.0

        undef my %cpu_new;
        my $cpu_all;
        my %cpu_old  = ( user => 0, nice => 0, system => 0, idle => 0 );
        my $filename = $vardir."/cpu_".$host.".dat";

        defined($res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef;
        $cpu_new{'user'}        = $$res{$oid.$suf_user} || 0;
        $cpu_new{'nice'}        = $$res{$oid.$suf_nice} || 0;
        $cpu_new{'system'}      = $$res{$oid.$suf_system} || 0;
        $cpu_new{'idle'}        = $$res{$oid.$suf_idle} || 0;
        $cpu_new{'intr'}        = $$res{$oid.$suf_intr} || 0;

        if( -e $filename ) {
                open(CPUFILE, "$filename");
                my $line = do { local $/; <CPUFILE> };
                ($user,$nice,$system,$idle) = split " ", $line;
                close CPUFILE;

                $cpu_old{'user'}   = $user || 0;
                $cpu_old{'nice'}   = $nice || 0;
                $cpu_old{'system'} = $system || 0;
                $cpu_old{'idle'}   = $idle || 0; 
        }

        $cpu_all = 
        ($cpu_new{'user'}+$cpu_new{'nice'}+$cpu_new{'system'}+$cpu_new{'idle'})
      - ($cpu_old{'user'}+$cpu_old{'nice'}+$cpu_old{'system'}+$cpu_old{'idle'});

        open(CPUFILE, ">$filename"); 
        print CPUFILE "$cpu_new{'user'} $cpu_new{'nice'} $cpu_new{'system'} $cpu_new{'idle'}";
        close CPUFILE;

        if ( $cpu_all > 0 ) {
          $cpu_new{'user'} = ($cpu_new{'user'}-$cpu_old{'user'})/$cpu_all;
          $cpu_new{'nice'} = ($cpu_new{'nice'}-$cpu_old{'nice'})/$cpu_all;
          $cpu_new{'system'} = ($cpu_new{'system'}-$cpu_old{'system'})/$cpu_all;
          $cpu_new{'idle'} = ($cpu_new{'idle'}-$cpu_old{'idle'})/$cpu_all;
          }
        else {
          $cpu_new{'user'} = "U";
          $cpu_new{'nice'} = "U";
          $cpu_new{'system'} = "U";
          $cpu_new{'idle'} = "U";
          }

        my $rrdname = "cpu_".$host;

        if ( $^O =~ /bsd/i ) {
            HotSaNICmod::do_rrd($rrdname,"U",time,$cpu_new{'user'},$cpu_new{'nice'},$cpu_new{'system'},$cpu_new{'idle'},$cpu_new{'intr'});
        } else {
            HotSaNICmod::do_rrd($rrdname,"U",time,$cpu_new{'user'},$cpu_new{'nice'},$cpu_new{'system'},$cpu_new{'idle'});
        }

}

sub do_load {
        my ($host,$com)=@_;
        my $oid = '.1.3.6.1.4.1.2021.10.1.3'; # enterprises.ucdavis.laTable.laEntry.laLoad
                  
        my @load;

        defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef;
        foreach ( sort keys(%$res) ) {
                push @load, $$res{$_} || 0;
        }

        my $rrdname = "load_".$host;

        HotSaNICmod::do_rrd($rrdname,"U",time,$load[2],$load[1],$load[0]);
}

sub do_proc {
        my ($host,$com)=@_;
        my $oid = '.1.3.6.1.2.1.25.4.2.1.7'; # host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunStatus

        my %proc = (    running => 0, 
                        runnable => 0, 
                        notRunnable => 0, 
                        invalid => 0
                   );

        defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef;
        foreach( keys(%$res) ) {
                for ($$res{$_}) {
                        if    (/(1|running)/) { $proc{'running'}++;     }
                        elsif (/(2|runnable)/) { $proc{'runnable'}++;    }
                        elsif (/(3|notRunnable)/) { $proc{'notRunnable'}++; }
                        elsif (/(4|invalid)/) { $proc{'invalid'}++;     }
                        else  { HotSaNIClog::warn("unknown state seen."); }
                }
        }

        my $rrdname = "proc_".$host;

        HotSaNICmod::do_rrd($rrdname,"U",time,$proc{'runnable'},$proc{'running'},$proc{'invalid'},$proc{'notRunnable'},"U");
}

sub do_mem {
        my ($host,$com)=@_;
        my $oid = '.1.3.6.1.4.1.2021.4'; # enterprises.ucdavis.memory
        my $suf_total      = '.5.0';     # .memTotalReal.0
        my $suf_free       = '.6.0';     # .memAvailReal.0
        my $suf_buffer     = '.14.0';    # .memBuffer.0
        my $suf_cache      = '.15.0';    # .memCached.0
        my $suf_inactive   = '0';        #  inactive not readable via snmp

        my $suf_swap_total = '.3.0';     # .memTotalSwap.0
        my $suf_swap_free  = '.4.0';     # .memAvailSwap.0

        my %mem;

        defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef;
        $mem{'totalReal'} = $$res{$oid.$suf_total} * 1024 || 0;
        $mem{'availReal'} = $$res{$oid.$suf_free} * 1024 || 0;
        $mem{'buffer'}    = $$res{$oid.$suf_buffer} * 1024 || 0;
        $mem{'cached'}    = $$res{$oid.$suf_cache} * 1024 || 0;
        $mem{'totalSwap'} = $$res{$oid.$suf_swap_total} * 1024 || 0;
        $mem{'swapFree'}  = $$res{$oid.$suf_swap_free} * 1024 || 0;
        $mem{'swapUsed'}  = $mem{'totalSwap'} - $mem{'swapFree'};
        
        $mem{'shared'} = $mem{'totalReal'}-$mem{'availReal'}-$mem{'buffer'}-$mem{'cached'};

        my $rrdname = "mem_".$host;

        if ( $^O =~ /bsd/ ) {
            HotSaNICmod::do_rrd($rrdname,"U",time,$mem{'availReal'},$mem{'inactive'},$mem{'shared'},$mem{'buffer'},$mem{'cached'},$mem{'swapFree'},$mem{'swapUsed'});
            # HotSaNICmod::do_rrd("mem","U",time,$memfree,$meminac,$memactv,$memwire,$memcach,$swpfree,$swpused);
        } else {
            HotSaNICmod::do_rrd($rrdname,"U",time,$mem{'availReal'},$mem{'shared'},$mem{'buffer'},$mem{'cached'},$mem{'swapFree'},$mem{'swapUsed'});
        }
}

sub do_users {
        my ($host,$com)=@_;
        my $oid = '.1.3.6.1.2.1.25.1.5.0'; # host.hrSystem.hrSystemNumUsers.0

        defined(my $res = HotSaNICsnmp::snmp_get($host, $com, ($oid))) || return undef;

        my $users=$$res{$oid};
        my $rrdname = "users_".$host;

        HotSaNICmod::do_rrd($rrdname,"U",time,$users,"U","U");
}

1;

Powered by WebSVN 2.2.1