| 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 |
push @args, '-z', $zonename; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
if ( $full ) { |
|---|
| 87 |
push @args, '-f'; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
@count = split(/\n/, (run_command($pgrep_path, @args, $config->{pattern})) ); |
|---|
| 92 |
$count = scalar(@count); |
|---|
| 93 |
|
|---|
| 94 |
die "Unable to run pgrep command\n" if (!defined($count)); |
|---|
| 95 |
chomp $count; |
|---|
| 96 |
|
|---|
| 97 |
if ($count =~ /^\d+$/) { |
|---|
| 98 |
return { |
|---|
| 99 |
"process_count" => [$count, "i"] |
|---|
| 100 |
}; |
|---|
| 101 |
} else { |
|---|
| 102 |
# We didn't get a count as expected. This can happen if you didn't |
|---|
| 103 |
# provide a pattern or something else went wrong. |
|---|
| 104 |
die "Pgrep gave unexpected output: $count\n"; |
|---|
| 105 |
}; |
|---|
| 106 |
}; |
|---|
| 107 |
|
|---|
| 108 |
1; |
|---|