From: Robert 'phaylon' Sedlacek Date: Tue, 1 May 2012 22:10:23 +0000 (+0000) Subject: Processes probe for ps output X-Git-Tag: v0.001_001~137 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=19dc45efb42c685a77daa1df7943bf406527b7d1;p=scpubgit%2FSystem-Introspector.git Processes probe for ps output --- diff --git a/lib/System/Introspector/Processes.pm b/lib/System/Introspector/Processes.pm new file mode 100644 index 0000000..0c08b72 --- /dev/null +++ b/lib/System/Introspector/Processes.pm @@ -0,0 +1,65 @@ +package System::Introspector::Processes; +use Moo; + +sub gather { + my ($self) = @_; + my $pipe = $self->_open_ps_pipe; + my $spec = <$pipe>; + $spec =~ s{(?:^\s+|\s+$)}{}g; + my @fields = map lc, split m{\s+}, $spec; + my @rows; + while (defined( my $line = <$pipe> )) { + chomp $line; + $line =~ s{(?:^\s+|\s+$)}{}g; + my @values = split m{\s+}, $line, scalar @fields; + my %row; + @row{ @fields } = @values; + push @rows, \%row; + } + return \@rows; +} + +# args is automatically included, since it has to be last +my @Included = qw( + blocked + c + class + cputime + egid egroup + etime + euid euser + fgid fgroup + flags + fuid fuser + ignored + lwp + nice + nlwp + pgid pgrp + pid ppid + pri + psr + rgid rgroup + rss + ruid ruser + sgid sgroup + sid + size + start_time + stat + suid suser + tid + time + tname + wchan +); + +sub _open_ps_pipe { + my ($self) = @_; + my $command = sprintf 'ps -eo %s', join(',', @Included, 'args'); + open my $pipe, '-|', $command + or die "Unable to open pipe to '$command': $!\n"; + return $pipe; +} + +1;