| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package os::windows::wsman::mode::processes; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub new { |
| 29 |
my ($class, %options) = @_; |
| 30 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 31 |
bless $self, $class; |
| 32 |
|
| 33 |
$options{options}->add_options(arguments => { |
| 34 |
'process-status:s' => { name => 'process_status', default => 'running' }, |
| 35 |
'process-name:s' => { name => 'process_name' }, |
| 36 |
'regexp-name' => { name => 'regexp_name' }, |
| 37 |
'warning:s' => { name => 'warning' }, |
| 38 |
'critical:s' => { name => 'critical' } |
| 39 |
}); |
| 40 |
|
| 41 |
return $self; |
| 42 |
} |
| 43 |
|
| 44 |
sub check_options { |
| 45 |
my ($self, %options) = @_; |
| 46 |
$self->SUPER::init(%options); |
| 47 |
|
| 48 |
if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { |
| 49 |
$self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); |
| 50 |
$self->{output}->option_exit(); |
| 51 |
} |
| 52 |
if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { |
| 53 |
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); |
| 54 |
$self->{output}->option_exit(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
my %map_process_status = ( |
| 59 |
0 => 'running', |
| 60 |
1 => 'other', |
| 61 |
2 => 'ready', |
| 62 |
3 => 'running', |
| 63 |
4 => 'blocked' |
| 64 |
); |
| 65 |
|
| 66 |
sub run { |
| 67 |
my ($self, %options) = @_; |
| 68 |
|
| 69 |
my $WQL = 'select Name,ExecutionState,CommandLine,ExecutablePath,Handle from Win32_Process'; |
| 70 |
if (defined($self->{option_results}->{process_name}) && $self->{option_results}->{process_name} ne '') { |
| 71 |
if (defined($self->{option_results}->{regexp_name})) { |
| 72 |
$WQL .= ' where Name like "' . $self->{option_results}->{process_name} . '"'; |
| 73 |
} else { |
| 74 |
$WQL .= ' where Name = "' . $self->{option_results}->{process_name} . '"'; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
my $results = $options{wsman}->request( |
| 79 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 80 |
wql_filter => $WQL, |
| 81 |
result_type => 'array' |
| 82 |
); |
| 83 |
|
| 84 |
# |
| 85 |
#CLASS: Win32_Process |
| 86 |
#CommandLine;ExecutablePath;ExecutionState;Handle;Name |
| 87 |
#C:\\Windows\\system32\\svchost.exe -k DcomLaunch -p;C:\\Windows\\system32\\svchost.exe;0;864;svchost.exe |
| 88 |
#"fontdrvhost.exe";C:\\Windows\\system32\\fontdrvhost.exe;0;884;fontdrvhost.exe |
| 89 |
#"fontdrvhost.exe";C:\\Windows\\system32\\fontdrvhost.exe;0;892;fontdrvhost.exe |
| 90 |
#C:\\Windows\\system32\\svchost.exe -k RPCSS -p;C:\\Windows\\system32\\svchost.exe;0;964;svchost.exe |
| 91 |
# |
| 92 |
my $detected = 0; |
| 93 |
foreach (@$results) { |
| 94 |
my $status = (!defined($_->{ExecutionState}) || $_->{ExecutionState} eq '') ? 0 : $_->{ExecutionState}; |
| 95 |
$self->{output}->output_add(long_msg => |
| 96 |
sprintf( |
| 97 |
"Process %s [status: %s] [name: %s]", |
| 98 |
$_->{Handle}, |
| 99 |
$map_process_status{$status}, |
| 100 |
$_->{Name} |
| 101 |
) |
| 102 |
); |
| 103 |
$detected++; |
| 104 |
} |
| 105 |
|
| 106 |
my $exit = $self->{perfdata}->threshold_check( |
| 107 |
value => $detected, |
| 108 |
threshold => [ |
| 109 |
{ label => 'critical', exit_litteral => 'critical' }, |
| 110 |
{ label => 'warning', exit_litteral => 'warning' } |
| 111 |
] |
| 112 |
); |
| 113 |
|
| 114 |
$self->{output}->output_add( |
| 115 |
severity => $exit, |
| 116 |
short_msg => "Number of current processes: $detected" |
| 117 |
); |
| 118 |
$self->{output}->perfdata_add( |
| 119 |
nlabel => 'processes.detected.count', |
| 120 |
value => $detected, |
| 121 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), |
| 122 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), |
| 123 |
min => 0 |
| 124 |
); |
| 125 |
|
| 126 |
$self->{output}->display(); |
| 127 |
$self->{output}->exit(); |
| 128 |
} |
| 129 |
|
| 130 |
1; |
| 131 |
|
| 132 |
__END__ |
| 133 |
|
| 134 |
=head1 MODE |
| 135 |
|
| 136 |
Check system number of processes. |
| 137 |
|
| 138 |
=over 8 |
| 139 |
|
| 140 |
=item B<--process-status> |
| 141 |
|
| 142 |
Filter process status. Can be a regexp. |
| 143 |
(default: 'running'). |
| 144 |
|
| 145 |
=item B<--process-name> |
| 146 |
|
| 147 |
Filter process name. |
| 148 |
|
| 149 |
=item B<--regexp-name> |
| 150 |
|
| 151 |
Allows to use WQL wildcard to filter process |
| 152 |
name (with option --process-name). |
| 153 |
|
| 154 |
=item B<--warning> |
| 155 |
|
| 156 |
Warning threshold of matching processes detected. |
| 157 |
|
| 158 |
=item B<--critical> |
| 159 |
|
| 160 |
Critical threshold of matching processes detected. |
| 161 |
|
| 162 |
=back |
| 163 |
|
| 164 |
=cut |