/usr/share/perl5/Sys/Statistics/Linux/Compilation.pm is in libsys-statistics-linux-perl 0.66-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | =head1 NAME
Sys::Statistics::Linux::Compilation - Statistics compilation.
=head1 SYNOPSIS
use Sys::Statistics::Linux;
my $lxs = Sys::Statistics::Linux->new( loadavg => 1 );
my $stat = $lxs->get;
foreach my $key ($stat->loadavg) {
print $key, " ", $stat->loadavg($key), "\n";
}
# or
use Sys::Statistics::Linux::LoadAVG;
use Sys::Statistics::Linux::Compilation;
my $lxs = Sys::Statistics::Linux::LoadAVG->new();
my $load = $lxs->get;
my $stat = Sys::Statistics::Linux::Compilation->new({ loadavg => $load });
foreach my $key ($stat->loadavg) {
print $key, " ", $stat->loadavg($key), "\n";
}
# or
foreach my $key ($stat->loadavg) {
print $key, " ", $stat->loadavg->{$key}, "\n";
}
=head1 DESCRIPTION
This module provides different methods to access and filter the statistics compilation.
=head1 METHODS
=head2 new()
Create a new C<Sys::Statistics::Linux::Compilation> object. This creator is only useful if you
don't call C<get()> of C<Sys::Statistics::Linux>. You can create a new object with:
my $lxs = Sys::Statistics::Linux::LoadAVG->new();
my $load = $lxs->get;
my $stat = Sys::Statistics::Linux::Compilation->new({ loadavg => $load });
=head2 Statistic methods
=over 4
=item sysinfo()
=item cpustats()
=item procstats()
=item memstats()
=item pgswstats()
=item netstats()
=item netinfo()
C<netinfo()> provides raw data - no deltas.
=item sockstats()
=item diskstats()
=item diskusage()
=item loadavg()
=item filestats()
=item processes()
=back
All methods returns the statistics as a hash reference in scalar context. In list all methods
returns the first level keys of the statistics. Example:
my $net = $stat->netstats; # netstats as a hash reference
my @dev = $stat->netstats; # the devices eth0, eth1, ...
my $eth0 = $stat->netstats('eth0'); # eth0 statistics as a hash reference
my @keys = $stat->netstats('eth0'); # the statistic keys
my @vals = $stat->netstats('eth0', @keys); # the values for the passed device and @keys
my $val = $stat->netstats('eth0', $key); # the value for the passed device and key
Sorted ...
my @dev = sort $stat->netstats;
my @keys = sort $stat->netstats('eth0');
=head2 pstop()
This method is looking for top processes and returns a sorted list of PIDs as an array or
array reference depending on the context. It expected two values: a key name and the number
of top processes to return.
As example you want to get the top 5 processes with the highest cpu usage:
my @top5 = $stat->pstop( ttime => 5 );
# or as a reference
my $top5 = $stat->pstop( ttime => 5 );
If you want to get all processes:
my @top_all = $stat->pstop( ttime => $FALSE );
# or just
my @top_all = $stat->pstop( 'ttime' );
=head2 search(), psfind()
Both methods provides a simple scan engine to find special statistics. Both methods except a filter
as a hash reference. It's possible to pass the statistics as second argument if the data is not stored
in the object.
The method C<search()> scans for statistics and rebuilds the hash tree until that keys that matched
your filter and returns the hits as a hash reference.
my $hits = $stat->search({
processes => {
cmd => qr/\[su\]/,
owner => qr/root/
},
cpustats => {
idle => 'lt:10',
iowait => 'gt:10'
},
diskusage => {
'/dev/sda1' => {
usageper => 'gt:80'
}
}
});
This would return the following matches:
* processes with the command "[su]"
* processes with the owner "root"
* all cpu where "idle" is less than 50
* all cpu where "iowait" is grather than 10
* only disk '/dev/sda1' if "usageper" is grather than 80
The method C<psfind()> scans for processes only and returns a array reference with all process
IDs that matched the filter. Example:
my $pids = $stat->psfind({ cmd => qr/init/, owner => 'eq:apache' });
This would return the following process ids:
* processes that matched the command "init"
* processes with the owner "apache"
There are different match operators available:
gt - grather than
lt - less than
eq - is equal
ne - is not equal
Notation examples:
gt:50
lt:50
eq:50
ne:50
Both argumnents have to be set as a hash reference.
Note: the operators < > = ! are not available any more. It's possible that in further releases
could be different changes for C<search()> and C<psfind()>. So please take a look to the
documentation if you use it.
=head1 EXPORTS
No exports.
=head1 TODOS
* Are there any wishs from your side? Send me a mail!
=head1 REPORTING BUGS
Please report all bugs to <jschulz.cpan(at)bloonix.de>.
=head1 AUTHOR
Jonny Schulz <jschulz.cpan(at)bloonix.de>.
Thanks to Moritz Lenz for his suggestion for the name of this module.
=head1 COPYRIGHT
Copyright (c) 2006, 2007 by Jonny Schulz. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut
package Sys::Statistics::Linux::Compilation;
use strict;
use warnings;
use Carp qw(croak);
our $VERSION = '0.10';
# Creating the statistics accessors
BEGIN {
foreach my $stat (qw/sysinfo procstats memstats sockstats loadavg filestats/) {
no strict 'refs';
*{$stat} = sub {
use strict 'refs';
my ($self, @keys) = @_;
return () unless $self->{$stat};
if (@keys) {
return @{$self->{$stat}}{@keys};
}
return wantarray ? keys %{$self->{$stat}} : $self->{$stat};
};
}
foreach my $stat (qw/cpustats pgswstats netstats netinfo diskstats diskusage processes/) {
no strict 'refs';
*{$stat} = sub {
use strict 'refs';
my ($self, $sub, @keys) = @_;
return () unless $self->{$stat};
if ($sub) {
my $ref = $self->{$stat};
return () unless exists $ref->{$sub};
if (@keys) {
return @{$ref->{$sub}}{@keys};
} else {
return wantarray ? keys %{$ref->{$sub}} : $ref->{$sub};
}
}
return wantarray ? keys %{$self->{$stat}} : $self->{$stat};
};
}
}
sub new {
my ($class, $stats) = @_;
unless (ref($stats) eq 'HASH') {
croak 'Usage: $class->new( \%statistics )';
}
return bless $stats, $class;
}
sub search {
my $self = shift;
my $filter = ref($_[0]) eq 'HASH' ? shift : {@_};
my $class = ref($self);
my %hits = ();
foreach my $opt (keys %{$filter}) {
unless (ref($filter->{$opt}) eq 'HASH') {
croak "$class: not a hash ref opt '$opt'";
}
# next if the object isn't loaded
next unless exists $self->{$opt};
my $fref = $filter->{$opt};
my $proc = $self->{$opt};
my $subref;
# we search for matches for each key that is defined
# in %filter and rebuild the tree until that key that
# matched the searched string
foreach my $x (keys %{$fref}) {
if (ref($fref->{$x}) eq 'HASH') {
# if the key $proc->{eth0} doesn't exists
# then we continue with the next defined filter
next unless exists $proc->{$x};
$subref = $proc->{$x};
while ( my ($name, $value) = each %{$fref->{$x}} ) {
if (exists $subref->{$name} && $self->_compare($subref->{$name}, $value)) {
$hits{$opt}{$x}{$name} = $subref->{$name};
}
}
} else {
foreach my $key (keys %{$proc}) {
if (ref($proc->{$key}) eq 'HASH') {
$subref = $proc->{$key};
if (ref $subref->{$x} eq 'HASH') {
foreach my $y (keys %{$subref->{$x}}) {
if ($self->_compare($subref->{$x}->{$y}, $fref->{$x})) {
$hits{$opt}{$key}{$x}{$y} = $subref->{$x}->{$y};
}
}
} elsif (defined $subref->{$x} && $self->_compare($subref->{$x}, $fref->{$x})) {
$hits{$opt}{$key}{$x} = $subref->{$x};
}
} else { # must be a scalar now
if (defined $proc->{$x} && $self->_compare($proc->{$x}, $fref->{$x})) {
$hits{$opt}{$x} = $proc->{$x}
}
last;
}
}
}
}
}
return wantarray ? %hits : \%hits;
}
sub psfind {
my $self = shift;
my $filter = ref($_[0]) eq 'HASH' ? shift : {@_};
my $proc = $self->{processes} or return undef;
my @hits = ();
PID: foreach my $pid (keys %{$proc}) {
my $proc = $proc->{$pid};
while ( my ($key, $value) = each %{$filter} ) {
if (exists $proc->{$key}) {
if (ref $proc->{$key} eq 'HASH') {
foreach my $v (values %{$proc->{$key}}) {
if ($self->_compare($v, $value)) {
push @hits, $pid;
next PID;
}
}
} elsif ($self->_compare($proc->{$key}, $value)) {
push @hits, $pid;
next PID;
}
}
}
}
return wantarray ? @hits : \@hits;
}
sub pstop {
my ($self, $key, $count) = @_;
unless ($key) {
croak 'Usage: pstop( $key => $count )';
}
my $proc = $self->{processes};
my @top = (
map { $_->[0] }
reverse sort { $a->[1] <=> $b->[1] }
map { [ $_, $proc->{$_}->{$key} ] } keys %{$proc}
);
if ($count) {
@top = @top[0..--$count];
}
return wantarray ? @top : \@top;
}
#
# private stuff
#
sub _compare {
my ($self, $x, $y) = @_;
if (ref($y) eq 'Regexp') {
return $x =~ $y;
} elsif ($y =~ s/^eq://) {
return $x eq $y;
} elsif ($y =~ s/^ne://) {
return $x ne $y;
} elsif ($y =~ s/^gt://) {
return $x > $y;
} elsif ($y =~ s/^lt://) {
return $x < $y;
} else {
croak ref($self).": bad search() / psfind() operator '$y'";
}
return undef;
}
1;
|