| 1 | 1 | simandl | package HotSaNICmod::OSdep; |
| 2 | | | |
| 3 | | | # return module version |
| 4 | | | # |
| 5 | | | sub version { |
| 6 | | | ($VERSION = '$Revision: 1.10 $') =~ s/.*(\d+\.\d+).*/$1/; |
| 7 | | | return "$^O.pm $VERSION"; |
| 8 | | | } |
| 9 | | | |
| 10 | | | # sample data |
| 11 | | | # |
| 12 | | | sub sample { |
| 13 | | | my %args=@_; |
| 14 | | | |
| 15 | | | @sections = split " ", ($args{SECTIONS}); |
| 16 | | | foreach (@sections){ |
| 17 | | | if ($_ eq "cpu") {read_cpu(%args)} |
| 18 | | | if ($_ eq "load") {read_load(%args)} |
| 19 | | | if ($_ eq "proc") {read_proc(%args)} |
| 20 | | | if ($_ eq "mem") {read_mem(%args)} |
| 21 | | | if ($_ eq "users") {read_users(%args)} |
| 22 | | | } |
| 23 | | | } |
| 24 | | | |
| 25 | | | |
| 26 | | | # users.rrd -> tty pty pts |
| 27 | | | # cpu.rrd -> cpuusr cpunic cpusys cpuint cpuidl |
| 28 | | | # load.rrd -> load15 load5 load1 |
| 29 | | | # proc.rrd -> procslp procrun proczmb procstp |
| 30 | | | # mem.rrd (Linux) -> memfre memshr membuf memcac swpfre swpuse |
| 31 | | | |
| 32 | | | |
| 33 | | | ##################################################################################### |
| 34 | | | # build CPU statistics |
| 35 | | | # |
| 36 | | | # We get info from the kern.cp_time sysctl. works on 4.6-4.7-STABLE and |
| 37 | | | # should work at least on single-cpu 5.0 boxes too. Values for SMP systems |
| 38 | | | # are cumulative until I (or anyone else) can get per-cpu info out of |
| 39 | | | # the kernel. Gkrellm can't even do that!!! :( |
| 40 | | | # |
| 41 | | | # sysctl order is: user nice sys intr idle |
| 42 | | | |
| 43 | | | sub read_cpu { |
| 44 | | | my %args=@_; |
| 45 | | | |
| 46 | | | $rawcpu=`sysctl kern.cp_time`; |
| 47 | | | chomp $rawcpu; # don't get why this is needed... |
| 48 | | | (undef, $cpu1, $cpu2, $cpu3, $cpu4, $cpu5)=split(/ /,$rawcpu ); |
| 49 | | | |
| 50 | | | $cpu1old=0; $cpu2old=0; $cpu3old=0; $cpu4old=0; $cpu5old=0; |
| 51 | | | |
| 52 | | | if ( -e "cpu.dat" ) { |
| 53 | | | open(CPU,"cpu.dat"); |
| 54 | | | while (<CPU>) { |
| 55 | | | ($cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /; |
| 56 | | | } |
| 57 | | | close(CPU); |
| 58 | | | } |
| 59 | | | |
| 60 | | | open(CPU,">cpu.dat"); |
| 61 | | | print CPU $cpu1." ".$cpu2." ".$cpu3." ".$cpu4." ".$cpu5."\n"; |
| 62 | | | close(CPU); |
| 63 | | | |
| 64 | | | # deltas |
| 65 | | | $all=($cpu1+$cpu2+$cpu3+$cpu4+$cpu5)-($cpu1old+$cpu2old+$cpu3old+$cpu4old+$cpu5old); |
| 66 | | | $cpuusr=($cpu1-$cpu1old)/$all; |
| 67 | | | $cpunic=($cpu2-$cpu2old)/$all; |
| 68 | | | $cpusys=($cpu3-$cpu3old)/$all; |
| 69 | | | $cpuint=($cpu4-$cpu4old)/$all; |
| 70 | | | $cpuidl=($cpu5-$cpu5old)/$all; |
| 71 | | | |
| 72 | | | HotSaNICmod::do_rrd("cpu","U",time,$cpuusr,$cpunic,$cpusys,$cpuidl,$cpuint); |
| 73 | | | } |
| 74 | | | |
| 75 | | | |
| 76 | | | |
| 77 | | | ##################################################################################### |
| 78 | | | # here we go with the load-average value ... |
| 79 | | | # |
| 80 | | | sub read_load { |
| 81 | | | my %args=@_; |
| 82 | | | (undef,undef,$load1,$load5,$load15,undef)=split(/ /,`/sbin/sysctl vm.loadavg`); |
| 83 | | | HotSaNICmod::do_rrd("load","U",time,$load15,$load5,$load1); |
| 84 | | | } |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | ##################################################################################### |
| 89 | | | # now the processes. We have to check how many processes are sleeping, |
| 90 | | | # running, zombieng spwapped to disc or stopped. To do this we simply |
| 91 | | | # have to walk through the /proc tree, check all "stat" files of each |
| 92 | | | # process-id subdir and increment the according state-counter. |
| 93 | | | # |
| 94 | | | sub read_proc { |
| 95 | | | my %args=@_; |
| 96 | | | ($procslp,$procrun,$proczmb,$procstp,$procdsc)=(0,0,0,0,0); |
| 97 | | | |
| 98 | | | @states=`/bin/ps axostate`; |
| 99 | | | foreach (@states) { |
| 100 | | | $procslp++ if (/^[SI]/); #Sleeping (S=<20s, I=>20s) |
| 101 | | | $procdsc++ if (/^D/); #Disk wait |
| 102 | | | $procrun++ if (/^R/); #Running |
| 103 | | | $proczmb++ if (/^Z/); #Zombie |
| 104 | | | $procstp++ if (/^T/); #Stopped |
| 105 | | | } |
| 106 | | | HotSaNICmod::do_rrd("proc","U",time,$procslp,$procrun,$proczmb,$procstp,$procdsc); |
| 107 | | | } |
| 108 | | | |
| 109 | | | |
| 110 | | | ##################################################################################### |
| 111 | | | # read users stats |
| 112 | | | # |
| 113 | | | sub read_users { |
| 114 | | | my %args=@_; |
| 115 | | | |
| 116 | | | # XXX im not shure if this will work. PR |
| 117 | | | @users=`who`; |
| 118 | | | ($tty,$pty,$pts)=(0,0,0); |
| 119 | | | foreach (@users) { |
| 120 | | | if (index($_," ttyv") >=0 ) { $tty++; } |
| 121 | | | elsif (index($_," ttyp") >=0 ) { $pty++; } |
| 122 | | | elsif (index($_," pts") >=0 ) { $pts++; } |
| 123 | | | } |
| 124 | | | |
| 125 | | | HotSaNICmod::do_rrd("users","U",time,$tty,$pty,$pts); |
| 126 | | | } |
| 127 | | | |
| 128 | | | |
| 129 | | | #################################################################################### |
| 130 | | | # here we go with the memory and swap statistics... |
| 131 | | | # |
| 132 | | | sub read_mem { |
| 133 | | | my %args=@_; |
| 134 | | | |
| 135 | | | # FreeBSD sucks for this... |
| 136 | | | (undef, $pagesize) = split(/ /, `sysctl vm.stats.vm.v_page_size`); |
| 137 | | | (undef, $memtotl) = split(/ /, `sysctl vm.stats.vm.v_page_count`); $memtotl *= $pagesize; |
| 138 | | | (undef, $memfree) = split(/ /, `sysctl vm.stats.vm.v_free_count`); $memfree *= $pagesize; |
| 139 | | | (undef, $memwire) = split(/ /, `sysctl vm.stats.vm.v_wire_count`); $memwire *= $pagesize; |
| 140 | | | (undef, $memactv) = split(/ /, `sysctl vm.stats.vm.v_active_count`); $memactv *= $pagesize; |
| 141 | | | (undef, $meminac) = split(/ /, `sysctl vm.stats.vm.v_inactive_count`);$meminac *= $pagesize; |
| 142 | | | (undef, $memcach) = split(/ /, `sysctl vm.stats.vm.v_cache_count`); $memcach *= $pagesize; |
| 143 | | | |
| 144 | | | #get swap. |
| 145 | | | #may be dodgy on systems with multiple swap partitions. however the |
| 146 | | | #total should be printed on the last line, overwriting the vars |
| 147 | | | open(IN,"/usr/sbin/pstat -sk|"); |
| 148 | | | while(<IN>) { |
| 149 | | | (undef, undef, $swpuse, $swpfre) = split; |
| 150 | | | } |
| 151 | | | |
| 152 | | | $swpuse*=1024; |
| 153 | | | $swpfre*=1024; |
| 154 | | | |
| 155 | | | HotSaNICmod::do_rrd("mem","U",time,$memfree,$memwire,$memactv,$meminac,$memcach,$swpfre,$swpuse); |
| 156 | | | } |
| 157 | | | |
| 158 | | | 1; |