jablonka.czprosek.czf

hotsanic

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

Compare with Previous - Blame - Download


package HotSaNICmod::OSdep;

use RRDs;

# return module version
#
sub version {
  ($VERSION = '$Revision: 1.8 $') =~ 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=@_;

  $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;

  if ( ! -e "rrd/cpu.rrd" ) { system("./makerrd","cpu"); }
  RRDs::update "rrd/cpu.rrd", time.":".$cpuusr.":".$cpunic.":".$cpusys.":".$cpuidl.":".$cpuint;
  if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update cpu.rrd: $ERROR\n"; }
  
}



#####################################################################################
# here we go with the load-average value ...
#
sub read_load {
  my %args=@_;
  (undef,undef,$load1,$load5,$load15,undef)=split(/ /,`/sbin/sysctl vm.loadavg`);

  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,$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
  }

  if ( ! -e "rrd/proc.rrd" ) { system("./makerrd","proc") }
  RRDs::update "rrd/proc.rrd", time.":".$procslp.":".$procrun.":".$proczmb.":".$procstp.":".$procdsc;
  if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update proc.rrd: $ERROR\n"; }
}


#####################################################################################
# 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) {
    #  print $_;
    if (index($_," ttyv") >=0 )    { $tty++; }
    elsif (index($_," ttyp") >=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 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 vars
  open(IN,"/usr/sbin/pstat -sk|");
  while(<IN>) {
    (undef, undef, $swpuse, $swpfre) = split;
  }

  $swpuse*=1024;
  $swpfre*=1024;

  if ( ! -e "rrd/mem.rrd" ) { system("./makerrd","mem") }
  RRDs::update "rrd/mem.rrd", time.":".$memfree.":".$memwire.":".$memactv.":".$meminac.":".$memcach.":".$swpfre.":".$swpuse;
  if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update `mem.rrd': $ERROR\n"; }
  } 

1;

Powered by WebSVN 2.2.1