root/lib/Core/Pgrep.pm

Revision 4cff28cf515af466f93c768b7526a7b2929d5993, 1.9 kB (checked in by Mark Harrison <mark@omniti.com>, 5 years ago)

Pgrep module

git-svn-id: https://labs.omniti.com/resmon/branches/resmon2@310 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • 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, fullcmd => 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 $count = run_command("$pgrep_path", "-c$full", "$config->{pattern}");
77     if (!defined($count)) {
78         return {
79             "error" => ["Unable to run pgrep command", "s"]
80         }
81     }
82     chomp $count;
83
84     if ($count =~ /^\d+$/) {
85         return {
86             "process_count" => [$count, "i"]
87         };
88     } else {
89         # We didn't get a count as expected. This can happen if you didn't
90         # provide a pattern or something else went wrong.
91         return {
92             "error" => ["Pgrep gave unexpected output: $count", "s"]
93         };
94     };
95 };
96
97 1;
Note: See TracBrowser for help on using the browser.