jablonka.czprosek.czf

hotsanic

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

Compare with Previous - Blame - Download


package HotSaNICmod::OSdep;

use RRDs;

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

  if ( ! -e "rrd/cpu.rrd" ) { system("./makerrd",$cpu); }
  RRDs::update "rrd/cpu.rrd", time.":".$cpuusr.":".$cpusys.":".$cpuidl;
  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,$load1,$load5,$load15)=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=@_;

  @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;
      split;
      foreach(@_){
        #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;

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

1;

Powered by WebSVN 2.2.1