Subject: | "find"-method to search in the stats |
Hi Jonny,
I would appreciate if you could add a "find" method to
Sys::Statistics::Linux. This would made it easier to search for specific
values.
This is a short proposal:
In Linux.pm:
sub find{
my ($self,$part,%config) = @_;
return unless $part and keys %config > 0;
return unless STATISTICS->{$part};
my $return = {};
my $stats = $self->get->{$part};
for my $key(keys %config){
my $value = $config{$key};
if(ref($value) eq 'Regexp'){
for my $pid(keys %$stats){
if(exists $stats->{$pid}->{$key} and
$stats->{$pid}->{$key} =~ $value){
$return->{$pid} = $stats->{$pid};
}
}
}
elsif(not ref($value)){
for my $pid(keys %$stats){
if(exists $stats->{$pid}->{$key} and
$stats->{$pid}->{$key} eq $value){
$return->{$pid} = $stats->{$pid};
}
}
}
}
return $return;
}
Then you can do thing like this:
/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Sys::Statistics::Linux;
my $lxs = Sys::Statistics::Linux->new(Processes => 1);
sleep 1;
my $stat = $lxs->find('Processes',cmd => qr/\(su\)/);
print Dumper($stat);
to find all Processes that have "(su)" as the value for "cmd"