File Coverage

blib/lib/App/RecordStream/Operation/fromps.pm
Criterion Covered Total %
statement 12 58 20.6
branch 0 8 0.0
condition 0 5 0.0
subroutine 4 14 28.5
pod n/a
total 16 85 18.8


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::fromps;
2              
3             our $VERSION = "4.0.23";
4              
5 1     1   442 use strict;
  1         2  
  1         23  
6 1     1   4 use warnings;
  1         2  
  1         22  
7              
8 1     1   4 use base qw(App::RecordStream::Operation);
  1         2  
  1         65  
9              
10 1     1   11 use App::RecordStream::OptionalRequire qw(Proc::ProcessTable);
  1         3  
  1         8  
11             App::RecordStream::OptionalRequire::require_done();
12              
13             sub init {
14 0     0     my $this = shift;
15 0           my $args = shift;
16              
17 0           my $process_table = $this->get_process_table();
18              
19 0           my @fields;
20              
21             my $spec = {
22 0     0     'key|k|field|f=s' => sub { push @fields, split(',', $_[1]) },
23 0           };
24              
25 0           $this->parse_options($args, $spec);
26              
27 0 0         if ( scalar @fields < 1 ) {
28             # Silly Proc::ProcessTable has been known to give undefs sometimes for some
29             # reason. We throw them on the ground.
30 0           @fields = grep { defined($_) } $process_table->fields();
  0            
31             }
32              
33 0           $this->{'FIELDS'} = \@fields;
34             }
35              
36             sub get_process_table {
37 0     0     my $this = shift;
38 0   0       $this->{'PROCESS_TABLE'} ||= Proc::ProcessTable->new();
39 0           return $this->{'PROCESS_TABLE'}
40             }
41              
42             sub set_process_table {
43 0     0     my $this = shift;
44 0           my $table = shift;
45 0           $this->{'PROCESS_TABLE'} = $table;
46             }
47              
48             sub set_converter {
49 0     0     my $this = shift;
50 0           my $func = shift;
51 0           $this->{'CONVERTER'} = $func;
52             }
53              
54             sub get_converter {
55 0     0     my $this = shift;
56 0   0 0     $this->{'CONVERTER'} ||= sub { return (getpwuid($_[0]))[0] };
  0            
57 0           return $this->{'CONVERTER'};
58             }
59              
60             sub wants_input {
61 0     0     return 0;
62             }
63              
64             sub stream_done {
65 0     0     my $this = shift;
66              
67 0           my $table = $this->get_process_table();
68 0           my $fields = $this->{'FIELDS'};
69              
70 0           foreach my $proc (@{$table->table()}) {
  0            
71 0           my $record = App::RecordStream::Record->new();
72 0           foreach my $field (@$fields) {
73 0           my $value = $proc->{$field};
74              
75 0 0         if ( $field eq 'uid' ) {
76 0           $value = $this->get_converter()->($value);
77             }
78              
79 0 0         $record->{$field} = $value if ( defined $value );
80             }
81              
82 0           $this->push_record($record);
83             }
84             }
85              
86             sub usage {
87 0     0     my $this = shift;
88              
89 0           my @fields = Proc::ProcessTable->new()->fields();
90 0           my $all_fields = join (', ', grep { defined } @fields);
  0            
91              
92 0           my $options = [
93             [ 'keys ', 'Fields to output. May be specified multiple times, may be comma separated. Default to all fields These are Proc::ProcessTable keys, and thus may not be keyspecs or groups'],
94             ];
95              
96 0           my $args_string = $this->options_string($options);
97 0 0         my $default_fields = $ENV{GENERATING_STATIC_DOC} ? <
98             Default fields for Linux:
99             __FORMAT_TEXT__
100             uid, gid, pid, fname, ppid, pgrp, sess, ttynum, flags, minflt, cminflt,
101             majflt, cmajflt, utime, stime, cutime, cstime, priority, start, size, rss,
102             wchan, time, ctime, state, euid, suid, fuid, egid, sgid, fgid, pctcpu,
103             pctmem, cmndline, exec, cwd
104             __FORMAT_TEXT__
105              
106             Default fields for OS X:
107             __FORMAT_TEXT__
108             pid, ppid, pgrp, uid, gid, euid, egid, suid, sgid, priority, size, rss,
109             flags, nice, sess, time, stime, utime, start, wchan, ttydev, ttynum, pctcpu,
110             pctmem, state, cmndline, fname
111             __FORMAT_TEXT__
112             STATIC
113             Default fields:
114             __FORMAT_TEXT__
115             $all_fields
116             __FORMAT_TEXT__
117             DYNAMIC
118 0           chomp $default_fields;
119              
120 0           return <
121             Usage: recs-fromps
122             __FORMAT_TEXT__
123             Prints out JSON records converted from the process table.
124             __FORMAT_TEXT__
125              
126             $args_string
127              
128             $default_fields
129              
130             Examples:
131             Get records for the process table
132             recs-fromps
133             Only get uid and pid
134             recs-fromps --keys uid,pid
135             USAGE
136             }
137              
138             1;