hotsanic |
Subversion Repositories: |
Compare with Previous - Blame - Download
package HotSaNICmod::OSdep;
# return module version
#
sub version {
($VERSION = '$Revision: 1.5 $') =~ 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 idle
sub read_cpu {
my %args=@_;
@top = `top -l2`;
while(@top){
if(/CPU Usage:\s+(\d+\.\d+)\% user,\s+(\d+\.\d+)\% sys,\s+(\d+\.\d+)\%/){
$cpuusr = $1;
$cpusys = $2;
$cpuidl = $3;
last;
}
}
HotSaNICmod::do_rrd("cpu","U",time,$cpuusr,$cpusys,$cpuidl);
}
#####################################################################################
# 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);
@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=@_;
@top = `top -l2`;
#PhysMem: 97.4M wired, 144M active, 567M inactive, 809M used, 727M free
#VM: 3.27G + 3.62M 14904(14904) pageins, 44(44) pageouts
while(@top){
if(/^PhysMem:/){
s/^[0-9\.\sKMG]//g;
foreach( split ){
#convert SI to integer bytes
if(/\dG/){ s/G//; $_ *= (1024**3) }
if(/\dM/){ s/M//; $_ *= (1024**2) }
if(/\dK/){ s/K//; $_ *= 1024 }
$_ = int
}
(undef, $memwire, $memactv, $meminac, $memused, $memfree) = @_;
last;
}
}
#get swap.
# to be done!
open(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,$swpfre,$swpuse);
}
1;