![]() ![]() |
hotsanic |
Subversion Repositories: |
Compare with Previous - Blame - Download
package HotSaNICmod::OSdep;# return module version#sub version {($VERSION = '$Revision: 1.20 $') =~ s/.*(\d+\.\d+).*/$1/;return "$^O.pm $VERSION";}# sample data#sub sample {my %args=@_;@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 "irq") {read_interrupts(%args)}elsif ($_ eq "uptime") {read_uptime(%args)}}}# users.rrd -> tty pty pts# cpu.rrd -> cpuusr cpusys cpunic 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;# read actual state# assumed order is: user nice sys intr idle$cpuraw = `/sbin/sysctl kern.cp_time`;$cpuraw =~ /^.* user = (\d+), nice = (\d+), sys = (\d+), intr = (\d+), idle = (\d+)$/;$cpu="cpu";$cpu1{$cpu}=$1;$cpu2{$cpu}=$2;$cpu3{$cpu}=$3;$cpu4{$cpu}=$5;$cpu5{$cpu}=$4;$cpu1old{$cpu}=0;$cpu2old{$cpu}=0;$cpu3old{$cpu}=0;$cpu4old{$cpu}=0;$cpu5old{$cpu}=0;# read last state and store actual state# usr nice sys idle intif ( -e "cpu.dat" ) {open(CPU,"cpu.dat");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,">cpu.dat");foreach $cpu (sort 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};HotSaNICmod::do_rrd($cpu,"U",time,$cpuusr{$cpu},$cpunic{$cpu},$cpusys{$cpu},$cpuidl{$cpu},$cpuint{$cpu});# 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=@_;(undef,$load1,$load5,$load15)=split(/ /,`/sbin/sysctl vm.loadavg`);HotSaNICmod::do_rrd("load","U",time,$load15,$load5,$load1);}###################################################################################### 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,$procdsc)=(0,0,0,0,0);# ok, this needs work doing to it. Unfortunately I'm not quite# familiar enough with FreeBSD to do this properly - any takers? - MJB$procslp=$procrun=$proczmb=$procstp=$procdsc=0;open FILE,"/bin/ps axostate|";while (<FILE>) {$procslp++ if (/^S/);$procdsc++ if (/^D/);$procrun++ if (/^R/);$proczmb++ if (/^Z/);$procstp++ if (/^T/);}close FILE;HotSaNICmod::do_rrd("proc","U",time,$procslp,$procrun,$proczmb,$procstp,$procdsc);}###################################################################################### read users stats#sub read_users {my %args=@_;@users=`who`;($tty,$pty,$pts)=(0,0,0);foreach (@users) {if (index($_," ttyE") >=0 ) { $tty++; }elsif (index($_," ttyp") >=0 ) { $pty++; }elsif (index($_," pts") >=0 ) { $pts++; }}HotSaNICmod::do_rrd("users","U",time,$tty,$pty,$pts);}##################################################################################### here we go with the memory and swapfile statistics...#sub read_mem {my %args=@_;my ($line, $bpp, $totalmem, $free, $active, $inactive, $fcache, $ecache, $totalswap, $swapused);my @vmstat = `/usr/bin/vmstat -s`;foreach $line ( @vmstat ) {chomp $line;($bpp = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / bytes per page$/);($totalmem = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages managed$/);($free = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages free$/);($active = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages active$/);($inactive = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages inactive$/);($wired = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages wired$/);($fcache = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / cached file pages$/);($ecache = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / cached executable pages$/);($totalswap = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / swap pages$/);($swapused = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / swap pages in use$/);}my $memfree = $free * $bpp ;my $meminac = $inactive * $bpp ;my $memactv = $active * $bpp ;my $memwire = $wired * $bpp ;my $memcach = ($fcache + $ecache) * $bpp ;my $swpfree = ($totalswap - $swapused) *$bpp ;my $swpused = $swapused * $bpp ;HotSaNICmod::do_rrd("mem","U",time,$memfree,$meminac,$memactv,$memwire,$memcach,$swpfree,$swpused);}##################################################################################### here we go with the interrupts...#sub read_interrupts {my %args=@_;my @stats=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);my $i;open FILE,"/usr/bin/vmstat -i |";while (<FILE>) {next if (/interrupt/);chomp;($irq,$total,) = split;($i = $irq ) =~ s/irq(\d+)/$1/;$i =~ s/Total/-1/;$stats[$i+1] = $total ;}close FILE;# need first 17 values from arrayHotSaNICmod::do_rrd("irq","U",time,@stats[0..16]);}###################################################################################### here we go with the load-average value ...#sub read_uptime {my %args=@_;open(IN,"/usr/bin/uptime |"); ($up=<IN>) =~ s/.* up (\d+) day.+/$1/; close(IN);$idle=0;HotSaNICmod::do_rrd("uptime","U",time,$up,$idle);}1;