root/lib/Core/Pgrep.pm

Revision 92cfa46746b7b066236f784998687a05e94f9e24, 2.3 kB (checked in by Robert Williams <rjwill@icgroup.com>, 4 years ago)

added space after zonename arg, changed run_command into a string

  • Property mode set to 100644
Line 
1 package Core::Pgrep;
2
3 use strict;
4 use warnings;
5
6 use base 'Resmon::Module';
7
8 use Resmon::ExtComm qw(run_command);
9
10 =pod
11
12 =head1 NAME
13
14 Core::Pgrep - Check for running processes using pgrep
15
16 =head1 SYNOPSIS
17
18  Core::Pgrep {
19      myproc : pattern => foo.sh -c foo, full => 1
20  }
21
22  Core::Pgrep {
23      myproc : pgrep_path => /bin/pgrep, pattern => foo.sh
24
25 =head1 DESCRIPTION
26
27 This module is a sample resmon module that demonstrates how to write and use a
28 resmon module, as well as exposing some features modules can use.
29 Documentation for a module should be done using pod (see B<perldoc perlpod>).
30
31 To read the documenation, use B<perldoc Pgrep.pm>.
32
33 To verify the documentatioe, use B<podchecker Pgrep.pm>.
34
35 =head1 CONFIGURATION
36
37 =over
38
39 =item check_name
40
41 The check name is descriptive only in this check. It is not used for anything.
42
43 =item pattern
44
45 The name of the process (or command line if full is used) to match against.
46
47 =item full
48
49 Set this to 1 to match on the full command line. This passes the '-f' option
50 to pgrep.
51
52 =item pgrep_path
53
54 Specify an alternate path to pgrep. Optional.
55
56 =back
57
58 =head1 METRICS
59
60 =over
61
62 =item process_count
63
64 How many matching processes are running.
65
66 =back
67
68 =cut
69
70 sub handler {
71     my $self = shift;
72     my $config = $self->{config}; # All configuration is in here
73     my $pgrep_path = $config->{pgrep_path} || 'pgrep';
74     my $full = $config->{full} ? "f" : "";
75
76     my $args;
77     my $count;
78     my @count;
79
80     if ( $^O eq "solaris" ) {
81       my $zonename = `zonename`;
82       chomp $zonename;
83       $args .= "-z $zonename ";
84     }
85
86     if ( $full ) {
87       $args .= "-f";
88     }
89
90     # run_command acts oddly if $args is empty. pgrep throws an error.
91     if ( $args ) {
92       @count = split(/\n/, (run_command("$pgrep_path $args \'$config->{pattern}\'")) );
93
94     }
95     else {
96       @count = split(/\n/, (run_command("$pgrep_path", $config->{pattern})) );
97     }
98     $count = scalar(@count);
99
100     die "Unable to run pgrep command\n" if (!defined($count));
101     chomp $count;
102
103     if ($count =~ /^\d+$/) {
104         return {
105             "process_count" => [$count, "i"]
106         };
107     } else {
108         # We didn't get a count as expected. This can happen if you didn't
109         # provide a pattern or something else went wrong.
110         die "Pgrep gave unexpected output: $count\n";
111     };
112 };
113
114 1;
Note: See TracBrowser for help on using the browser.