| 1 | 1 | simandl | package HotSaNICmod::OSdep; |
| 2 | | | |
| 3 | | | # return module version |
| 4 | | | # |
| 5 | | | sub version { |
| 6 | | | ($VERSION = '$Revision: 1.15 $') =~ 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 | | | elsif ($_ eq "load") {read_load(%args)} |
| 19 | | | elsif ($_ eq "proc") {read_proc(%args)} |
| 20 | | | elsif ($_ eq "mem") {read_mem(%args)} |
| 21 | | | elsif ($_ eq "users") {read_users(%args)} |
| 22 | | | # elsif ($_ eq "irq") {read_interrupts(%args)} |
| 23 | | | # elsif ($_ eq "uptime") {read_uptime(%args)} |
| 24 | | | } |
| 25 | | | |
| 26 | | | } |
| 27 | | | |
| 28 | | | # users.rrd -> tty pty pts |
| 29 | | | # cpu.rrd -> cpuusr cpusys cpuwait cpuidl |
| 30 | | | # load.rrd -> load15 load5 load1 |
| 31 | | | # proc.rrd -> procslp procrun proczmb procstp |
| 32 | | | # mem.rrd (Linux) -> memfre memshr membuf memcac swpfre swpuse |
| 33 | | | |
| 34 | | | ##################################################################################### |
| 35 | | | # build CPU statistics |
| 36 | | | # in /proc/stat there is a CPU entry which provides us with the |
| 37 | | | # number of ticks the CPU was in all states ( user nice system idle ) |
| 38 | | | # We simply have to gather this information and calculate the actual percentage :) |
| 39 | | | # To do this we have to remember the last read values of course! |
| 40 | | | # |
| 41 | | | sub read_cpu { |
| 42 | | | my %args=@_; |
| 43 | | | my $multicpu=0; |
| 44 | | | |
| 45 | | | # vmstat -s |
| 46 | | | # |
| 47 | | | # 23506934 user cpu |
| 48 | | | # 2794729 system cpu |
| 49 | | | # 44912336 idle cpu |
| 50 | | | # 449606 wait cpu |
| 51 | | | |
| 52 | | | # read actual state |
| 53 | | | @lines=`kstat cpu_stat`; |
| 54 | | | |
| 55 | | | # module: cpu_stat instance: 0 |
| 56 | | | # name: cpu_stat0 class: misc |
| 57 | | | # idle 23071073 |
| 58 | | | # kernel 1159786 |
| 59 | | | # user 11466265 |
| 60 | | | # wait 200397 |
| 61 | | | # |
| 62 | | | |
| 63 | | | $cpu1{cpu}=0; |
| 64 | | | $cpu2{cpu}=0; |
| 65 | | | $cpu3{cpu}=0; |
| 66 | | | $cpu4{cpu}=0; |
| 67 | | | $cpu5{cpu}=0; |
| 68 | | | |
| 69 | | | foreach (@lines) { |
| 70 | | | ($name,$value)=split; |
| 71 | | | if (defined $name) { |
| 72 | | | if (!defined $value) { $value=0; } |
| 73 | | | if ( $name eq "name:") { |
| 74 | | | ($cpu=$value) =~ s/_stat//; |
| 75 | | | $cpu1{$cpu}=0; |
| 76 | | | $cpu2{$cpu}=0; |
| 77 | | | $cpu3{$cpu}=0; |
| 78 | | | $cpu4{$cpu}=0; |
| 79 | | | $cpu5{$cpu}=0; |
| 80 | | | $cpu1old{$cpu}=0; |
| 81 | | | $cpu2old{$cpu}=0; |
| 82 | | | $cpu3old{$cpu}=0; |
| 83 | | | $cpu4old{$cpu}=0; |
| 84 | | | $cpu5old{$cpu}=0; |
| 85 | | | if ($cpu eq "cpu1") {$multicpu=1; } |
| 86 | | | } |
| 87 | | | if ($name eq "user") { $cpu1{$cpu}=$value; $cpu1{cpu}+=$value; } |
| 88 | | | if ($name eq "kernel") { $cpu2{$cpu}=$value; $cpu2{cpu}+=$value; } |
| 89 | | | if ($name eq "wait") { $cpu3{$cpu}=$value; $cpu3{cpu}+=$value; } |
| 90 | | | if ($name eq "idle") { $cpu4{$cpu}=$value; $cpu4{cpu}+=$value; } |
| 91 | | | } |
| 92 | | | } |
| 93 | | | |
| 94 | | | # read last state and store actual state |
| 95 | | | # usr nice sys idle int |
| 96 | | | my $DATFILE=$args{VARDIR}."/cpu.dat"; |
| 97 | | | if ( -e $DATFILE ) { |
| 98 | | | open(CPU,$DATFILE); |
| 99 | | | while (<CPU>) { |
| 100 | | | $line=$_." 0 0 0 0 0 0"; |
| 101 | | | ($cpu, $cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /,$line; |
| 102 | | | |
| 103 | | | if ($cpu !~ /cpu/) { |
| 104 | | | $cpu5old=$cpu4old; |
| 105 | | | $cpu4old=$cpu3old; |
| 106 | | | $cpu3old=$cpu2old; |
| 107 | | | $cpu2old=$cpu1old; |
| 108 | | | $cpu1old=$cpu; |
| 109 | | | $cpu="cpu"; |
| 110 | | | } |
| 111 | | | |
| 112 | | | $cpu1old{$cpu}=$cpu1old; |
| 113 | | | $cpu2old{$cpu}=$cpu2old; |
| 114 | | | $cpu3old{$cpu}=$cpu3old; |
| 115 | | | $cpu4old{$cpu}=$cpu4old; |
| 116 | | | $cpu5old{$cpu}=$cpu5old; |
| 117 | | | } |
| 118 | | | close(CPU); |
| 119 | | | } |
| 120 | | | |
| 121 | | | open(CPU,">".$DATFILE); |
| 122 | | | |
| 123 | | | foreach $cpu (sort keys %cpu1) { |
| 124 | | | print CPU $cpu." ".$cpu1{$cpu}." ".$cpu2{$cpu}." ".$cpu3{$cpu}." ".$cpu4{$cpu}." ".$cpu5{$cpu}."\n"; |
| 125 | | | |
| 126 | | | # calculate everything.... |
| 127 | | | # |
| 128 | | | |
| 129 | | | $all{$cpu}=($cpu1{$cpu}+$cpu2{$cpu}+$cpu3{$cpu}+$cpu4{$cpu}+$cpu5{$cpu})-($cpu1old{$cpu}+$cpu2old{$cpu}+$cpu3old{$cpu}+$cpu4old{$cpu}+$cpu5old{$cpu}); |
| 130 | | | |
| 131 | | | $cpuusr{$cpu}=($cpu1{$cpu}-$cpu1old{$cpu})/$all{$cpu}; |
| 132 | | | $cpunic{$cpu}=($cpu2{$cpu}-$cpu2old{$cpu})/$all{$cpu}; |
| 133 | | | $cpusys{$cpu}=($cpu3{$cpu}-$cpu3old{$cpu})/$all{$cpu}; |
| 134 | | | $cpuidl{$cpu}=($cpu4{$cpu}-$cpu4old{$cpu})/$all{$cpu}; |
| 135 | | | $cpuint{$cpu}=($cpu5{$cpu}-$cpu5old{$cpu})/$all{$cpu}; |
| 136 | | | |
| 137 | | | # store data |
| 138 | | | # |
| 139 | | | |
| 140 | | | HotSaNICmod::do_rrd($cpu,"U",time,$cpuusr{$cpu},$cpunic{$cpu},$cpusys{$cpu},$cpuidl{$cpu}); |
| 141 | | | |
| 142 | | | # on single-cpu machines only the global database has to be updated. |
| 143 | | | # |
| 144 | | | last if ($multicpu==0); |
| 145 | | | |
| 146 | | | } |
| 147 | | | close(CPU); |
| 148 | | | } |
| 149 | | | |
| 150 | | | |
| 151 | | | |
| 152 | | | ##################################################################################### |
| 153 | | | # here we go with the load-average value ... |
| 154 | | | # |
| 155 | | | sub read_load { |
| 156 | | | my %args=@_; |
| 157 | | | ($_)=`uptime`; |
| 158 | | | @values=split; |
| 159 | | | $load15=pop @values; |
| 160 | | | $load5=pop @values; |
| 161 | | | $load1=pop @values; |
| 162 | | | |
| 163 | | | HotSaNICmod::do_rrd("load","U",time,$load15,$load5,$load1); |
| 164 | | | } |
| 165 | | | |
| 166 | | | |
| 167 | | | |
| 168 | | | ##################################################################################### |
| 169 | | | # now the processes. We have to check how many processes are sleeping, |
| 170 | | | # running, zombieng spwapped to disc or stopped. To do this we simply |
| 171 | | | # have to walk through the /proc tree, check all "stat" files of each |
| 172 | | | # process-id subdir and increment the according state-counter. |
| 173 | | | # |
| 174 | | | sub read_proc { |
| 175 | | | my %args=@_; |
| 176 | | | ($procslp,$procrun,$proczmb,$procstp,$procque)=(0,0,0,0,0); |
| 177 | | | |
| 178 | | | open FILE,"ps -aef -o s|"; |
| 179 | | | while (<FILE>) { |
| 180 | | | if (index($_,"S") >=0 ) { $procslp++ } |
| 181 | | | elsif (index($_,"R") >=0 ) { $procque++ } |
| 182 | | | elsif (index($_,"O") >=0 ) { $procrun++ } |
| 183 | | | elsif (index($_,"Z") >=0 ) { $proczmb++ } |
| 184 | | | elsif (index($_,"T") >=0 ) { $procstp++ } |
| 185 | | | } |
| 186 | | | close FILE; |
| 187 | | | |
| 188 | | | HotSaNICmod::do_rrd("proc","U",time,$procslp,$procrun,$proczmb,$procstp,$procque); |
| 189 | | | } |
| 190 | | | |
| 191 | | | ##################################################################################### |
| 192 | | | # read users stats |
| 193 | | | # |
| 194 | | | sub read_users { |
| 195 | | | my %args=@_; |
| 196 | | | @users=`who`; |
| 197 | | | ($tty,$pty,$pts)=(0,0,0); |
| 198 | | | foreach (@users) { |
| 199 | | | if (index($_," tty") >=0 ) { $tty++; } |
| 200 | | | elsif (index($_," pty") >=0 ) { $pty++; } |
| 201 | | | elsif (index($_," pts") >=0 ) { $pts++; } |
| 202 | | | } |
| 203 | | | |
| 204 | | | HotSaNICmod::do_rrd("users","U",time,$tty,$pty,$pts); |
| 205 | | | } |
| 206 | | | |
| 207 | | | #################################################################################### |
| 208 | | | # here we go with the memory and swapfile statistics... |
| 209 | | | # |
| 210 | | | sub read_mem { |
| 211 | | | my %args=@_; |
| 212 | | | |
| 213 | | | # read swap state |
| 214 | | | ($_)=`swap -s`; |
| 215 | | | @stats=split; |
| 216 | | | ($swpuse=$stats[8]) =~ s/k//; |
| 217 | | | ($swpfre=$stats[10]) =~ s/k//; |
| 218 | | | $swpuse*=1024; |
| 219 | | | $swpfre*=1024; |
| 220 | | | |
| 221 | | | # read memory state |
| 222 | | | $memshr=0; |
| 223 | | | $memtot=0; |
| 224 | | | $memfre=0; |
| 225 | | | $membuf=0; |
| 226 | | | $memcac=0; |
| 227 | | | |
| 228 | | | ($line)=grep /^Memory:/, `top -b`; |
| 229 | | | |
| 230 | | | # Memory: 4096M real, 154M free, 339M swap in use, 20G swap free |
| 231 | | | |
| 232 | | | @fields=split / /,$line; |
| 233 | | | ($memtot=$fields[1]) =~ s/M//g; |
| 234 | | | ($memfre=$fields[3]) =~ s/M//g; |
| 235 | | | |
| 236 | | | $memtot*=1048576; |
| 237 | | | $memfre*=1048576; |
| 238 | | | |
| 239 | | | $memshr=$memtot-$memfre; |
| 240 | | | |
| 241 | | | HotSaNICmod::do_rrd("mem","U",time,$memfre,$memshr,$membuf,$memcac,$swpfre,$swpuse); |
| 242 | | | } |
| 243 | | | |
| 244 | | | sub read_interrupts { |
| 245 | | | my %args=@_; |
| 246 | | | |
| 247 | | | # intr 905974335 490612570 3015 0 0 0 3 3 3 2 71611549 0 145961329 197785844 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 248 | | | |
| 249 | | | my @stats; |
| 250 | | | open FILE,"/proc/stat"; |
| 251 | | | while (<FILE>) { (undef,@stats)=split if /intr/; } |
| 252 | | | close FILE; |
| 253 | | | |
| 254 | | | # need first 17 values from array |
| 255 | | | HotSaNICmod::do_rrd("irq","U",time,@stats[0..16]); |
| 256 | | | } |
| 257 | | | |
| 258 | | | |
| 259 | | | |
| 260 | | | ##################################################################################### |
| 261 | | | # here we go with the load-average value ... |
| 262 | | | # |
| 263 | | | sub read_uptime { |
| 264 | | | my %args=@_; |
| 265 | | | open(IN,"/proc/uptime"); ($up,$idle)=split(/ /,<IN>); close(IN); |
| 266 | | | $up/=86400; |
| 267 | | | $idle/=86400; |
| 268 | | | |
| 269 | | | HotSaNICmod::do_rrd("uptime","U",time,$up,$idle); |
| 270 | | | } |
| 271 | | | |
| 272 | | | 1; |
| 273 | | | |