jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [trunk/] [modules/] [system/] [platform/] [syssnmp.pm] - Rev 36 Go to most recent revision

Compare with Previous - Blame - Download


package syssnmp;

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

my ($host,$com);

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

sub do_snmp {
        my ($arg,$vardir) = @_;
        $arg =~ s/SNMP://;
        ($host, $com) = split ":", $arg;

        # Now do the real work ...
        do_cpu($vardir);
        do_load();
        do_proc();
        do_mem();
        do_users();
}

# Gather data from SNMP agent

sub do_cpu {
        my ($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

        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;

        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";
          }

        do_rrd_cpu(%cpu_new);

        #print "CPU: $cpu_new{'user'} $cpu_new{'nice'} $cpu_new{'system'} $cpu_new{'idle'}\n";

}

sub do_load {
        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 ( keys(%$res) ) {
                push @load, $$res{$_} || 0;
        }

        do_rrd_load(@load);

        #print "LOAD: $load[0] $load[1] $load[2]\n";
}

sub do_proc {
        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        { print "unknown state seen.\n"; }
                }
        }

        do_rrd_proc(%proc);

        #print "PROC: $proc{'running'} $proc{'runnable'} $proc{'notRunnable'} $proc{'invalid'}\n";
}

sub do_mem {
        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_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'};

        do_rrd_mem(%mem);

        #print "MEM: ";
        #foreach ( keys(%mem) ) { print "$mem{$_} "; }
        #print "\n";
}

sub do_users {
        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;

        do_rrd_users($$res{$oid});

        #print "USERS: $res\n";
}

# End gather functions 
#
# Start rrd-functions

sub do_rrd_cpu() {
        my %cpu_new = @_;
        my $rrdname = "cpu_".$host;

        if ( ! -e "rrd/".$rrdname.".rrd" ) { 
                system("./makerrd", $rrdname);
        }

        RRDs::update "rrd/".$rrdname.".rrd", time.":".$cpu_new{'user'}.":".$cpu_new{'nice'}.":".$cpu_new{'system'}.":".$cpu_new{'idle'};
        if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }

        #print "Finished RRD update (CPU)\n";
}

sub do_rrd_load() {
        my @load = @_;
        my $rrdname = "load_".$host;

        if ( ! -e "rrd/".$rrdname.".rrd" ) { system("./makerrd", $rrdname) }
        RRDs::update "rrd/".$rrdname.".rrd", time.":".$load[2].":".$load[1].":".$load[0];
        if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }

        #print "Finished RRD update (LOAD)\n";
}

sub do_rrd_proc() {
        my %proc = @_;
        my $rrdname = "proc_".$host;

        if ( ! -e "rrd/".$rrdname.".rrd" ) { system("./makerrd",$rrdname) }
        RRDs::update "rrd/".$rrdname.".rrd", time.":".$proc{'runnable'}.":".$proc{'running'}.":".$proc{'invalid'}.":".$proc{'notRunnable'}.":U";
        if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }

        #print "Finished RRD update (PROC)\n";
}

sub do_rrd_mem() {
        my %mem = @_;
        my $rrdname = "mem_".$host;

        if ( ! -e "rrd/".$rrdname.".rrd" ) { system("./makerrd",$rrdname) }
        RRDs::update "rrd/".$rrdname.".rrd", time.":".$mem{'availReal'}.":".$mem{'shared'}.":".$mem{'buffer'}.":".$mem{'cached'}.":".$mem{'swapFree'}.":".$mem{'swapUsed'};
        if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }

        #print "Finished RRD update (MEM)\n";
}

sub do_rrd_users() {
        my ($users) = @_;
        my $rrdname = "users_".$host;

        if ( ! -e "rrd/".$rrdname.".rrd" ) { system("./makerrd",$rrdname) }
        RRDs::update "rrd/".$rrdname.".rrd", time.":".$users.":U:U";
        if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }

        #print "Finished RRD update (USERS)\n";
}

1;

Powered by WebSVN 2.2.1