- Timestamp:
- 08/30/07 17:49:33 (17 months ago)
- Location:
- ext
- Files:
-
- 4 added
- 14 modified
-
Dumper (added)
-
Dumper/Makefile.pl (added)
-
Dumper/lib (added)
-
Dumper/lib/Dumper.pm (added)
-
Sys-Statistics-Linux/Makefile.pl (modified) (1 diff)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux.pm (modified) (13 diffs)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/CpuStats.pm (modified) (2 diffs)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/DiskStats.pm (modified) (2 diffs)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/DiskUsage.pm (modified) (1 diff)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/FileStats.pm (modified) (2 diffs)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/LoadAVG.pm (modified) (1 diff)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/MemStats.pm (modified) (1 diff)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/NetStats.pm (modified) (3 diffs)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/PgSwStats.pm (modified) (2 diffs)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/ProcStats.pm (modified) (2 diffs)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/Processes.pm (modified) (5 diffs)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/SockStats.pm (modified) (1 diff)
-
Sys-Statistics-Linux/lib/Sys/Statistics/Linux/SysInfo.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
ext/Sys-Statistics-Linux/Makefile.pl
r17226 r17519 1 use strict;2 1 use lib "../../", "../../inc"; 3 2 use inc::Module::Install prefix => '../../inc'; -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux.pm
r17226 r17519 12 12 13 13 $lxs.set( 14 SysInfo => 1,15 CpuStats => 1,16 ProcStats => 1,17 MemStats => 1,18 PgSwStats => 1,19 NetStats => 1,20 SockStats => 1,21 DiskStats => 1,22 DiskUsage => 1,23 LoadAVG=> 1,24 FileStats => 1,25 Processes => 1,14 sysinfo => 1, 15 cpustats => 1, 16 procstats => 1, 17 memstats => 1, 18 pgswstats => 1, 19 netstats => 1, 20 sockstats => 1, 21 diskstats => 1, 22 diskusage => 1, 23 loadavg => 1, 24 filestats => 1, 25 processes => 1, 26 26 ); 27 27 … … 95 95 In addition it's possible to handoff a process list for option C<Processes>. 96 96 97 my $lxs = Sys::Statistics::Linux ->new(97 my $lxs = Sys::Statistics::Linux.new( 98 98 Processes => { 99 99 init => 1, … … 104 104 To get more informations about the statistics refer the different modules of the distribution. 105 105 106 SysInfo - Collect system informations with Sys::Statistics::Linux::SysInfo.107 CpuStats - Collect cpu statistics with Sys::Statistics::Linux::CpuStats.108 ProcStats - Collect process statistics with Sys::Statistics::Linux::ProcStats.109 MemStats - Collect memory statistics with Sys::Statistics::Linux::MemStats.110 PgSwStats - Collect paging and swapping statistics with Sys::Statistics::Linux::PgSwStats.111 NetStats - Collect net statistics with Sys::Statistics::Linux::NetStats.112 SockStats - Collect socket statistics with Sys::Statistics::Linux::SockStats.113 DiskStats - Collect disk statistics with Sys::Statistics::Linux::DiskStats.114 DiskUsage - Collect the disk usage with Sys::Statistics::Linux::DiskUsage.115 LoadAVG- Collect the load average with Sys::Statistics::Linux::LoadAVG.116 FileStats - Collect inode statistics with Sys::Statistics::Linux::FileStats.117 Processes - Collect process statistics with Sys::Statistics::Linux::Processes.106 sysinfo - Collect system informations with Sys::Statistics::Linux::SysInfo. 107 cpustats - Collect cpu statistics with Sys::Statistics::Linux::CpuStats. 108 procstats - Collect process statistics with Sys::Statistics::Linux::ProcStats. 109 memstats - Collect memory statistics with Sys::Statistics::Linux::MemStats. 110 pgswstats - Collect paging and swapping statistics with Sys::Statistics::Linux::PgSwStats. 111 netstats - Collect net statistics with Sys::Statistics::Linux::NetStats. 112 sockstats - Collect socket statistics with Sys::Statistics::Linux::SockStats. 113 diskstats - Collect disk statistics with Sys::Statistics::Linux::DiskStats. 114 diskusage - Collect the disk usage with Sys::Statistics::Linux::DiskUsage. 115 loadavg - Collect the load average with Sys::Statistics::Linux::LoadAVG. 116 filestats - Collect inode statistics with Sys::Statistics::Linux::FileStats. 117 processes - Collect process statistics with Sys::Statistics::Linux::Processes. 118 118 119 119 =head1 METHODS … … 126 126 Without options 127 127 128 my $lxs = Sys::Statistics::Linux ->new();128 my $lxs = Sys::Statistics::Linux.new; 129 129 130 130 Or with options 131 131 132 my $lxs = Sys::Statistics::Linux ->new(CpuStats => 1);132 my $lxs = Sys::Statistics::Linux.new(CpuStats => 1); 133 133 134 134 Would do nothing 135 135 136 my $lxs = Sys::Statistics::Linux->new(CpuStats => 0); 137 138 It's possible to call C<new()> with a hash reference of options. 139 140 my %options = ( 141 CpuStats => 1, 142 MemStats => 1 143 ); 144 145 my $lxs = Sys::Statistics::Linux->new(\%options); 136 my $lxs = Sys::Statistics::Linux.new(CpuStats => 0); 146 137 147 138 Take a look to C<set()> for more informations. … … 153 144 C<Sys::Statistics::Linux::SysInfo>: 154 145 155 $lxs ->set(146 $lxs.set( 156 147 Processes => 0, # deactivate this statistic 157 148 PgSwStats => 1, # activate the statistic and calls new() and init() if necessary … … 159 150 ); 160 151 161 It's possible to call C<set()> with a hash reference of options.162 163 my %options = (164 CpuStats => 2,165 MemStats => 2166 );167 168 $lxs->set(\%options);169 170 152 =head2 get() 171 153 172 154 Call C<get()> to get the collected statistics. C<get()> returns the statistics as a hash reference. 173 155 174 my $stats = $lxs ->get;156 my $stats = $lxs.get; 175 157 176 158 =head2 init() … … 179 161 higher than 0. 180 162 181 $lxs ->init;163 $lxs.init; 182 164 183 165 =head2 search(), psfind() 184 166 185 Both methods provides a simple scan engine to find special statistics. Both methods except a filter 186 as a hash reference as the first argument. If your data comes from extern - maybe from a client that 187 send his statistics to the server - you can set the statistics as the second argument. The second 188 argument have to be a hash reference as well. 189 190 The method C<search()> scans for statistics and rebuilds the hash tree until that keys that matched 191 your filter and returns the hits as a hash reference. 192 193 my $hits = $lxs->search({ 194 Processes => { 195 cmd => qr/\[su\]/, 196 owner => qr/root/ 197 }, 198 CpuStats => { 199 idle => 'lt:10', 200 iowait => 'gt:10' 201 }, 202 DiskUsage => { 203 '/dev/sda1' => { 204 usageper => 'gt:80' 205 } 206 } 207 }); 208 209 This would return the following matches: 210 211 * processes with the command "[su]" 212 * processes with the owner "root" 213 * all cpu where "idle" is less than 50 214 * all cpu where "iowait" is grather than 10 215 * only disk '/dev/sda1' if "usageper" is grather than 80 216 217 If the statistics are not gathered by the current process then you can handoff statistics as an 218 argument. 219 220 my %stats = ( 221 CpuStats => { 222 cpu => { 223 system => '51.00', 224 total => '51.00', 225 idle => '49.00', 226 nice => '0.00', 227 user => '0.00', 228 iowait => '0.00' 229 } 230 } 231 ); 232 233 my %filter = ( 234 CpuStats => { 235 total => 'gt:50' 236 } 237 ); 238 239 my $hits = $lxs->search(\%filter, \%stats); 240 241 The method C<psfind()> scans for processes only and returns a array reference with all process 242 IDs that matched the filter. Example: 243 244 my $pids = $lxs->psfind({ cmd => qr/init/, owner => 'eq:apache' }); 245 246 You can handoff the statistics as second argument as well. 247 248 my $pids = $lxs->psfind(\%filter, \%stats); 249 250 This would return the following process ids: 251 252 * processes that matched the command "init" 253 * processes with the owner "apache" 254 255 There are different match operators available: 256 257 gt - grather than 258 lt - less than 259 eq - is equal 260 ne - is not equal 261 262 Notation examples: 263 264 gt:50 265 lt:50 266 eq:50 267 ne:50 268 269 Both argumnents have to be set as a hash reference. 270 271 Note: the operators < > = ! are not available any more. It's possible that in further releases 272 could be different changes for C<search()> and C<psfind()>. So please take a look to the 273 documentation if you use it. 167 NOT YET IMPLEMENTED 274 168 275 169 =head2 settime() 276 170 277 Call C<settime()> to define a POSIX formatted time stamp, generated with localtime(). 278 279 $lxs->settime('%Y/%m/%d %H:%M:%S'); 280 281 To get more informations about the formats take a look at C<strftime()> of POSIX.pm 282 or the manpage C<strftime(3)>. 171 NOT YET IMPLEMENTED 283 172 284 173 =head2 gettime() 285 174 286 C<gettime()> returns a POSIX formatted time stamp, @foo in list and $bar in scalar context. 287 If the time format isn't set then the default format "%Y-%m-%d %H:%M:%S" will be set 288 automatically. You can also set a time format with C<gettime()>. 289 290 my $date_time = $lxs->gettime; 291 292 Or 293 294 my ($date, $time) = $lxs->gettime; 295 296 Or 297 298 my ($date, $time) = $lxs->gettime('%Y/%m/%d %H:%M:%S'); 175 NOT YET IMPLEMENTED 299 176 300 177 =head1 EXAMPLES … … 302 179 A very simple perl script could looks like this: 303 180 304 use warnings;305 use strict;306 181 use Sys::Statistics::Linux; 307 182 308 my $lxs = Sys::Statistics::Linux ->new( CpuStats => 1 );183 my $lxs = Sys::Statistics::Linux.new( CpuStats => 1 ); 309 184 sleep(1); 310 my $stats = $lxs->get; 311 my $cpu = $stats->{CpuStats}->{cpu}; 312 313 print "Statistics for CpuStats (all)\n"; 314 print " user $cpu->{user}\n"; 315 print " nice $cpu->{nice}\n"; 316 print " system $cpu->{system}\n"; 317 print " idle $cpu->{idle}\n"; 318 print " ioWait $cpu->{iowait}\n"; 319 print " total $cpu->{total}\n"; 320 321 Set and get a time stamp: 322 323 use warnings; 324 use strict; 325 use Sys::Statistics::Linux; 326 327 my $lxs = Sys::Statistics::Linux->new(); 328 $lxs->settime('%Y/%m/%d %H:%M:%S'); 329 print "$lxs->gettime\n"; 330 331 If you're not sure you can use the the C<Data::Dumper> module to learn more about the hash structure: 332 333 use warnings; 334 use strict; 335 use Sys::Statistics::Linux; 336 use Data::Dumper; 337 338 my $lxs = Sys::Statistics::Linux->new( CpuStats => 1 ); 339 sleep(1); 340 my $stats = $lxs->get; 341 342 print Dumper($stats); 343 344 How to get processes with the highest cpu workload: 345 346 use warnings; 347 use strict; 348 use Sys::Statistics::Linux; 349 350 my $lxs = Sys::Statistics::Linux->new( Processes => 1 ); 351 sleep(1); 352 my $stats = $lxs->get; 353 my $procs = $stats->{Processes}; 354 355 my @top5 = ( 356 map { $_->[0] } 357 reverse sort { $a->[1] <=> $b->[1] } 358 map { [ $_, $procs->{$_}->{ttime} ] } keys %{$procs} 359 )[0..4]; 185 my %stats = $lxs.get; 186 my %cpu := %stats<CpuStats><cpu>; 187 188 say "Statistics for CpuStats (all)\n"; 189 say " user %cpu<user>"; 190 say " nice %cpu<nice>"; 191 say " system %cpu<system>"; 192 say " idle %cpu<idle>"; 193 say " ioWait %cpu<iowait>"; 194 say " total %cpu<total>"; 360 195 361 196 =head1 DEPENDENCIED 362 197 363 UNIVERSAL 364 UNIVERSAL::require 365 Test::More 366 Carp 198 No at the moment. 367 199 368 200 =head1 EXPORTS … … 388 220 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 389 221 390 = end pod222 =cut 391 223 392 224 #package Sys::Statistics::Linux; … … 432 264 #} 433 265 434 has Hash $.inits = { };435 has Hash $.stats = { };436 has Hash $.objects = { };437 has Hash $.regmaps = { };438 has Hash $.options = { };266 has %.inits; 267 has %.stats; 268 has %.objects; 269 has %.regmaps; 270 has %.options; 439 271 440 272 submethod BUILD (*%p) { 273 # alias module name init() 441 274 self.register('sysinfo', 'Sys::Statistics::Linux::SysInfo', 0); 442 275 self.register('cpustats', 'Sys::Statistics::Linux::CpuStats', 1); … … 456 289 method register ($a, $m, $i) { 457 290 self.regmaps{$a} = { class => $m, init => $i }; 458 self.options{$a} = 0;291 self.options{$a} = 0; 459 292 } 460 293 … … 524 357 croak("invalid value for option '$k'"); 525 358 } 526 %options{$k} = $v;527 359 if $v { 528 360 if !%objects.exists($k) { … … 536 368 self.stats{$k}.delete; 537 369 } 370 %options{$k} = $v; 538 371 } 539 372 } -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/CpuStats.pm
r17454 r17519 127 127 #} 128 128 129 has Hash $.files = {};130 has Hash $.inits = {};131 has Hash $.stats = {};129 has %.files; 130 has %.inits; 131 has %.stats; 132 132 133 133 submethod BUILD () { … … 162 162 self.stats = self.load; 163 163 self.deltas(); 164 return self.stats; 164 # "return self.stats" will return a Hash::Const, for this reason 165 # I return %stats, then it returns a Hash 166 my %stats := self.stats; 167 return %stats; 165 168 } 166 169 -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/DiskStats.pm
r17454 r17519 135 135 #} 136 136 137 has Hash $.files = {};138 has Hash $.inits = {};139 has Hash $.stats = {};140 has Int $.uptime;141 has Int $.blocksize = 512;137 has %.files; 138 has %.inits; 139 has %.stats; 140 has Int $.uptime; 141 has Int $.blocksize = 512; 142 142 143 143 submethod BUILD () { … … 176 176 self.stats = self.load; 177 177 self.deltas(); 178 return self.stats; 178 # "return self.stats" will return a Hash::Const, for this reason 179 # I return %stats, then it returns a Hash 180 my %stats := self.stats; 181 return %stats; 179 182 } 180 183 -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/DiskUsage.pm
r17454 r17519 115 115 #} 116 116 117 has Hash $.cmd = {};117 has %.cmd; 118 118 119 119 submethod BUILD () { -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/FileStats.pm
r17454 r17519 120 120 #} 121 121 122 has Hash $.files = {};122 has %.files; 123 123 124 124 submethod BUILD () { … … 157 157 158 158 method get () { 159 my $file_nr = self.files .<file_nr>;160 my $inode_nr = self.files .<inode_nr>;161 my $dentries = self.files .<dentries>;159 my $file_nr = self.files<file_nr>; 160 my $inode_nr = self.files<inode_nr>; 161 my $dentries = self.files<dentries>; 162 162 my (%stats, $line); 163 163 -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/LoadAVG.pm
r17454 r17519 110 110 #} 111 111 112 has Hash $.files = {};112 has %.files; 113 113 114 114 submethod BUILD () { -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/MemStats.pm
r17454 r17519 129 129 #} 130 130 131 has Hash $.files = {};131 has %.files; 132 132 133 133 submethod BUILD () { -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/NetStats.pm
r17454 r17519 141 141 #} 142 142 143 has Hash $.files = {};144 has Hash $.inits = {};145 has Hash $.stats = {};146 has Hash$.uptime;143 has %.files; 144 has %.inits; 145 has %.stats; 146 has Int $.uptime; 147 147 148 148 submethod BUILD () { … … 180 180 self.stats = self.load; 181 181 self.deltas(); 182 return self.stats; 182 # "return self.stats" will return a Hash::Const, for this reason 183 # I return %stats, then it returns a Hash 184 my %stats := self.stats; 185 return %stats; 183 186 } 184 187 … … 308 311 #} 309 312 313 my method uptime () { 314 my $file = self.files<uptime>; 315 my $upfh = open($file, :r) or croak("unable to open $file: $!"); 316 my ($up, $idle) = $upfh.readline.comb; 317 $upfh.close; 318 return $up; 319 } 320 310 321 1; -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/PgSwStats.pm
r17454 r17519 127 127 #} 128 128 129 has Hash $.files = {};130 has Hash $.inits = {};131 has Hash $.stats = {};132 has Int $.uptime;129 has %.files; 130 has %.inits; 131 has %.stats; 132 has Int $.uptime; 133 133 134 134 submethod BUILD () { … … 167 167 self.stats = self.load; 168 168 self.deltas(); 169 return self.stats; 169 # "return self.stats" will return a Hash::Const, for this reason 170 # I return %stats, then it returns a Hash 171 my %stats := self.stats; 172 return %stats; 170 173 } 171 174 -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/ProcStats.pm
r17454 r17519 107 107 sub croak (*@m) { die @m } # waiting for Carp::croak 108 108 109 has Hash $.files = {};110 has Hash $.inits = {};111 has Hash $.stats = {};112 has Int $.uptime;109 has %.files; 110 has %.inits; 111 has %.stats; 112 has Int $.uptime; 113 113 114 114 #sub new { … … 159 159 self.stats = self.load; 160 160 self.deltas(); 161 return self.stats; 161 # "return self.stats" will return a Hash::Const, for this reason 162 # I return %stats, then it returns a Hash 163 my %stats := self.stats; 164 return %stats; 162 165 } 163 166 -
ext/Sys-Statistics-Linux/lib/Sys/Statistics/Linux/Processes.pm
r17453 r17519 136 136 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. 137 137 138 = cut138 =end pod 139 139 140 140 #package Sys::Statistics::Linux::Processes; … … 177 177 #} 178 178 179 has Hash $.files = {}; 180 has Hash $.inits = {}; 181 has Hash $.stats = {}; 179 has %.files; 180 has %.inits; 181 has %.stats; 182 183 #has @.pids is rw; # this doesnt run nice 184 # this doesnt run nice and occurs the error 185 # "Can't modify constant item" 186 # if I try 187 # "self.pids = @p" 188 182 189 has Array $.pids; 183 has Int $.i_uptime; 184 has Int $.s_uptime; 185 186 submethod BUILD (@pids) { 190 has Int $.i_uptime; 191 has Int $.s_uptime; 192 193 multi submethod BUILD () { 194 self.files<basedir uptime p_stat p_statm p_status p_cmdline> = 195 ('/proc', '/proc/uptime', 'stat', 'statm', 'status', 'cmdline'); 196 } 197 198 multi submethod BUILD (@pids) { 187 199 for @pids -> $pid { 188 200 unless $pid ~~ /^\d+$/ { … … 190 202 } 191 203 } 204 192 205 self.files<basedir uptime p_stat p_statm p_status p_cmdline> = 193 206 ('/proc', '/proc/uptime', 'stat', 'statm', 'status', 'cmdline');
