![]() ![]() |
hotsanic |
Subversion Repositories: |
Compare with Previous - Blame - Download
package HotSaNICmod::OSdep;use RRDs;eval { require "platform/syssnmp.pm"; };$NO_SNMP=1 if($@);# return module version#sub version {($VERSION = '$Revision: 1.10 $') =~ s/.*(\d+\.\d+).*/$1/;return "$^O.pm $VERSION";}# sample data#sub sample {my %args=@_;if(!defined($NO_SNMP)) {foreach ( keys(%args) ) {syssnmp::do_snmp($args{$_},$args{VARDIR}) if(/HOST/);}}@sections = split " ", ($args{SECTIONS});foreach (@sections){if ($_ eq "cpu") {read_cpu(%args)}elsif ($_ eq "load") {read_load(%args)}elsif ($_ eq "proc") {read_proc(%args)}elsif ($_ eq "mem") {read_mem(%args)}elsif ($_ eq "users") {read_users(%args)}# elsif ($_ eq "int") {read_interrupts(%args)}# elsif ($_ eq "uptime") {read_uptime(%args)}}}# users.rrd -> tty pty pts# cpu.rrd -> cpuusr cpusys cpuwait cpuidl# load.rrd -> load15 load5 load1# proc.rrd -> procslp procrun proczmb procstp# mem.rrd (Linux) -> memfre memshr membuf memcac swpfre swpuse###################################################################################### build CPU statistics# in /proc/stat there is a CPU entry which provides us with the# number of ticks the CPU was in all states ( user nice system idle )# We simply have to gather this information and calculate the actual percentage :)# To do this we have to remember the last read values of course!#sub read_cpu {my %args=@_;my $multicpu=0;# vmstat -s## 23506934 user cpu# 2794729 system cpu# 44912336 idle cpu# 449606 wait cpu# read actual state@lines=`kstat cpu_stat`;# module: cpu_stat instance: 0# name: cpu_stat0 class: misc# idle 23071073# kernel 1159786# user 11466265# wait 200397#$cpu1{cpu}=0;$cpu2{cpu}=0;$cpu3{cpu}=0;$cpu4{cpu}=0;$cpu5{cpu}=0;foreach (@lines) {($name,$value)=split;if (defined $name) {if (!defined $value) { $value=0; }if ( $name eq "name:") {($cpu=$value) =~ s/_stat//;$cpu1{$cpu}=0;$cpu2{$cpu}=0;$cpu3{$cpu}=0;$cpu4{$cpu}=0;$cpu5{$cpu}=0;$cpu1old{$cpu}=0;$cpu2old{$cpu}=0;$cpu3old{$cpu}=0;$cpu4old{$cpu}=0;$cpu5old{$cpu}=0;if ($cpu eq "cpu1") {$multicpu=1; }}if ($name eq "user") { $cpu1{$cpu}=$value; $cpu1{cpu}+=$value; }if ($name eq "kernel") { $cpu2{$cpu}=$value; $cpu2{cpu}+=$value; }if ($name eq "wait") { $cpu3{$cpu}=$value; $cpu3{cpu}+=$value; }if ($name eq "idle") { $cpu4{$cpu}=$value; $cpu4{cpu}+=$value; }}}# read last state and store actual state# usr nice sys idle intmy $DATFILE=$args{VARDIR}."/cpu.dat";if ( -e $DATFILE ) {open(CPU,$DATFILE);while (<CPU>) {$line=$_." 0 0 0 0 0 0";($cpu, $cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /,$line;if ($cpu !~ /cpu/) {$cpu5old=$cpu4old;$cpu4old=$cpu3old;$cpu3old=$cpu2old;$cpu2old=$cpu1old;$cpu1old=$cpu;$cpu="cpu";}$cpu1old{$cpu}=$cpu1old;$cpu2old{$cpu}=$cpu2old;$cpu3old{$cpu}=$cpu3old;$cpu4old{$cpu}=$cpu4old;$cpu5old{$cpu}=$cpu5old;}close(CPU);}open(CPU,">".$DATFILE);foreach $cpu (keys %cpu1) {print CPU $cpu." ".$cpu1{$cpu}." ".$cpu2{$cpu}." ".$cpu3{$cpu}." ".$cpu4{$cpu}." ".$cpu5{$cpu}."\n";# calculate everything....#$all{$cpu}=($cpu1{$cpu}+$cpu2{$cpu}+$cpu3{$cpu}+$cpu4{$cpu}+$cpu5{$cpu})-($cpu1old{$cpu}+$cpu2old{$cpu}+$cpu3old{$cpu}+$cpu4old{$cpu}+$cpu5old{$cpu});$cpuusr{$cpu}=($cpu1{$cpu}-$cpu1old{$cpu})/$all{$cpu};$cpunic{$cpu}=($cpu2{$cpu}-$cpu2old{$cpu})/$all{$cpu};$cpusys{$cpu}=($cpu3{$cpu}-$cpu3old{$cpu})/$all{$cpu};$cpuidl{$cpu}=($cpu4{$cpu}-$cpu4old{$cpu})/$all{$cpu};$cpuint{$cpu}=($cpu5{$cpu}-$cpu5old{$cpu})/$all{$cpu};# store data#if ( ! -e "rrd/".$cpu.".rrd" ) { system("./makerrd",$cpu); }RRDs::update "rrd/".$cpu.".rrd", time.":".$cpuusr{$cpu}.":".$cpunic{$cpu}.":".$cpusys{$cpu}.":".$cpuidl{$cpu};if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update cpu.rrd: $ERROR\n"; }# on single-cpu machines only the global database has to be updated.#last if ($multicpu==0);}close(CPU);}###################################################################################### here we go with the load-average value ...#sub read_load {my %args=@_;($_)=`uptime`;@values=split;$load15=pop @values;$load5=pop @values;$load1=pop @values;if ( ! -e "rrd/load.rrd" ) { system("./makerrd","load") }RRDs::update "rrd/load.rrd", time.":".$load15.":".$load5.":".$load1;if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update load.rrd: $ERROR\n"; }}###################################################################################### now the processes. We have to check how many processes are sleeping,# running, zombieng spwapped to disc or stopped. To do this we simply# have to walk through the /proc tree, check all "stat" files of each# process-id subdir and increment the according state-counter.#sub read_proc {my %args=@_;($procslp,$procrun,$proczmb,$procstp,$procque)=(0,0,0,0,0);open FILE,"ps -aef -o s|";while (<FILE>) {if (index($_,"S") >=0 ) { $procslp++ }elsif (index($_,"R") >=0 ) { $procque++ }elsif (index($_,"O") >=0 ) { $procrun++ }elsif (index($_,"Z") >=0 ) { $proczmb++ }elsif (index($_,"T") >=0 ) { $procstp++ }}close FILE;if ( ! -e "rrd/proc.rrd" ) { system("./makerrd","proc") }RRDs::update "rrd/proc.rrd", time.":".$procslp.":".$procrun.":".$proczmb.":".$procstp.":".$procque;if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update proc.rrd: $ERROR\n"; }}###################################################################################### read users stats#sub read_users {my %args=@_;@users=`who`;($tty,$pty,$pts)=(0,0,0);foreach (@users) {if (index($_," tty") >=0 ) { $tty++; }elsif (index($_," pty") >=0 ) { $pty++; }elsif (index($_," pts") >=0 ) { $pts++; }}if ( ! -e "rrd/users.rrd" ) { system("./makerrd","users") }RRDs::update "rrd/users.rrd", time.":".$tty.":".$pty.":".$pts;if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update users.rrd: $ERROR\n"; }}##################################################################################### here we go with the memory and swapfile statistics...#sub read_mem {my %args=@_;if ( ! -e "rrd/mem.rrd" ) { system("./makerrd","mem") }# read swap state($_)=`swap -s`;@stats=split;($swpuse=$stats[8]) =~ s/k//;($swpfre=$stats[10]) =~ s/k//;$swpuse*=1024;$swpfre*=1024;# read memory state$memshr=0;$memtot=0;$memfre=0;$membuf=0;$memcac=0;($line)=grep /^Memory:/, `top -b`;# Memory: 4096M real, 154M free, 339M swap in use, 20G swap free@fields=split / /,$line;($memtot=$fields[1]) =~ s/M//g;($memfre=$fields[3]) =~ s/M//g;$memtot*=1048576;$memfre*=1048576;$memshr=$memtot-$memfre;RRDs::update "rrd/mem.rrd", time.":".$memfre.":".$memshr.":".$membuf.":".$memcac.":".$swpfre.":".$swpuse;if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update `mem.rrd': $ERROR\n"; }}sub read_interrupts {my %args=@_;# intr 905974335 490612570 3015 0 0 0 3 3 3 2 71611549 0 145961329 197785844 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0my @stats;open FILE,"/proc/stat";while (<FILE>) { (undef,@stats)=split if /intr/; }close FILE;# need first 17 values from arraymy $result=join(":",@stats[0..16]);if ( ! -e "rrd/irq.rrd" ) { system("./makerrd","irq") }RRDs::update "rrd/irq.rrd",time.":".$result;if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update `irq.rrd': $ERROR\n"; }}###################################################################################### here we go with the load-average value ...#sub read_uptime {my %args=@_;open(IN,"/proc/uptime"); ($up,$idle)=split(/ /,<IN>); close(IN);$up/=86400;$idle/=86400;if ( ! -e "rrd/uptime.rrd" ) { system("./makerrd","uptime") }RRDs::update "rrd/uptime.rrd", time.":".$up.":".$idle;if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update uptime.rrd: $ERROR\n"; }}1;