![]() ![]() |
hotsanic |
Subversion Repositories: |
Compare with Previous - Blame - Download
package HotSaNICmod::OSdep;# return module version#sub version {($VERSION = '$Revision: 1.10 $') =~ 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)}if ($_ eq "load") {read_load(%args)}if ($_ eq "proc") {read_proc(%args)}if ($_ eq "mem") {read_mem(%args)}if ($_ eq "users") {read_users(%args)}}}# users.rrd -> tty pty pts# cpu.rrd -> cpuusr cpunic cpusys cpuint cpuidl# load.rrd -> load15 load5 load1# proc.rrd -> procslp procrun proczmb procstp# mem.rrd (Linux) -> memfre memshr membuf memcac swpfre swpuse###################################################################################### build CPU statistics## We get info from the kern.cp_time sysctl. works on 4.6-4.7-STABLE and# should work at least on single-cpu 5.0 boxes too. Values for SMP systems# are cumulative until I (or anyone else) can get per-cpu info out of# the kernel. Gkrellm can't even do that!!! :(## sysctl order is: user nice sys intr idlesub read_cpu {my %args=@_;$rawcpu=`sysctl kern.cp_time`;chomp $rawcpu; # don't get why this is needed...(undef, $cpu1, $cpu2, $cpu3, $cpu4, $cpu5)=split(/ /,$rawcpu );$cpu1old=0; $cpu2old=0; $cpu3old=0; $cpu4old=0; $cpu5old=0;if ( -e "cpu.dat" ) {open(CPU,"cpu.dat");while (<CPU>) {($cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /;}close(CPU);}open(CPU,">cpu.dat");print CPU $cpu1." ".$cpu2." ".$cpu3." ".$cpu4." ".$cpu5."\n";close(CPU);# deltas$all=($cpu1+$cpu2+$cpu3+$cpu4+$cpu5)-($cpu1old+$cpu2old+$cpu3old+$cpu4old+$cpu5old);$cpuusr=($cpu1-$cpu1old)/$all;$cpunic=($cpu2-$cpu2old)/$all;$cpusys=($cpu3-$cpu3old)/$all;$cpuint=($cpu4-$cpu4old)/$all;$cpuidl=($cpu5-$cpu5old)/$all;HotSaNICmod::do_rrd("cpu","U",time,$cpuusr,$cpunic,$cpusys,$cpuidl,$cpuint);}###################################################################################### here we go with the load-average value ...#sub read_load {my %args=@_;(undef,undef,$load1,$load5,$load15,undef)=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);@states=`/bin/ps axostate`;foreach (@states) {$procslp++ if (/^[SI]/); #Sleeping (S=<20s, I=>20s)$procdsc++ if (/^D/); #Disk wait$procrun++ if (/^R/); #Running$proczmb++ if (/^Z/); #Zombie$procstp++ if (/^T/); #Stopped}HotSaNICmod::do_rrd("proc","U",time,$procslp,$procrun,$proczmb,$procstp,$procdsc);}###################################################################################### read users stats#sub read_users {my %args=@_;# XXX im not shure if this will work. PR@users=`who`;($tty,$pty,$pts)=(0,0,0);foreach (@users) {if (index($_," ttyv") >=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 swap statistics...#sub read_mem {my %args=@_;# FreeBSD sucks for this...(undef, $pagesize) = split(/ /, `sysctl vm.stats.vm.v_page_size`);(undef, $memtotl) = split(/ /, `sysctl vm.stats.vm.v_page_count`); $memtotl *= $pagesize;(undef, $memfree) = split(/ /, `sysctl vm.stats.vm.v_free_count`); $memfree *= $pagesize;(undef, $memwire) = split(/ /, `sysctl vm.stats.vm.v_wire_count`); $memwire *= $pagesize;(undef, $memactv) = split(/ /, `sysctl vm.stats.vm.v_active_count`); $memactv *= $pagesize;(undef, $meminac) = split(/ /, `sysctl vm.stats.vm.v_inactive_count`);$meminac *= $pagesize;(undef, $memcach) = split(/ /, `sysctl vm.stats.vm.v_cache_count`); $memcach *= $pagesize;#get swap.#may be dodgy on systems with multiple swap partitions. however the#total should be printed on the last line, overwriting the varsopen(IN,"/usr/sbin/pstat -sk|");while(<IN>) {(undef, undef, $swpuse, $swpfre) = split;}$swpuse*=1024;$swpfre*=1024;HotSaNICmod::do_rrd("mem","U",time,$memfree,$memwire,$memactv,$meminac,$memcach,$swpfre,$swpuse);}1;