| 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::services; |
| 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); |
| 31 |
bless $self, $class; |
| 32 |
|
| 33 |
$options{options}->add_options(arguments => { |
| 34 |
'warning' => { name => 'warning' }, |
| 35 |
'critical' => { name => 'critical' }, |
| 36 |
'services:s' => { name => 'services' }, |
| 37 |
'auto' => { name => 'auto' }, |
| 38 |
'exclude:s' => { name => 'exclude' } |
| 39 |
}); |
| 40 |
|
| 41 |
$self->{service_rules} = {}; |
| 42 |
$self->{wql_filter} = ''; |
| 43 |
$self->{threshold} = 'CRITICAL'; |
| 44 |
return $self; |
| 45 |
} |
| 46 |
|
| 47 |
sub check_options { |
| 48 |
my ($self, %options) = @_; |
| 49 |
$self->SUPER::init(%options); |
| 50 |
|
| 51 |
if (!defined($self->{option_results}->{services}) && !defined($self->{option_results}->{auto})) { |
| 52 |
$self->{output}->add_option_msg(short_msg => "Need to specify at least '--services' or '--auto' option."); |
| 53 |
$self->{output}->option_exit(); |
| 54 |
} |
| 55 |
|
| 56 |
if (defined($self->{option_results}->{services})) { |
| 57 |
my $append = ''; |
| 58 |
foreach my $rule (split /,/, $self->{option_results}->{services}) { |
| 59 |
if ($rule !~ /^([^\!=]*)(\!=|=){0,1}(.*){0,1}/) { |
| 60 |
$self->{output}->add_option_msg(short_msg => "Wrong rule in --services option: " . $rule); |
| 61 |
$self->{output}->option_exit(); |
| 62 |
} |
| 63 |
if (!defined($1) || $1 eq '') { |
| 64 |
$self->{output}->add_option_msg(short_msg => "Need service name for rule: " . $rule); |
| 65 |
$self->{output}->option_exit(); |
| 66 |
} |
| 67 |
|
| 68 |
my $sname = $1; |
| 69 |
my $operator = defined($2) && $2 ne '' ? $2 : '!='; |
| 70 |
my $state = defined($3) && $3 ne '' ? lc($3) : 'running'; |
| 71 |
|
| 72 |
if ($operator !~ /^(=|\!=)$/) { |
| 73 |
$self->{output}->add_option_msg(short_msg => "Wrong operator for rule: " . $rule . ". Should be '=' or '!='."); |
| 74 |
$self->{output}->option_exit(); |
| 75 |
} |
| 76 |
|
| 77 |
if ($state !~ /^(stopped|start pending|stop pending|running|continue pending|pause pending|paused|unknown)$/i) { |
| 78 |
$self->{output}->add_option_msg(short_msg => "Wrong state for rule: " . $rule . ". See help for available state."); |
| 79 |
$self->{output}->option_exit(); |
| 80 |
} |
| 81 |
|
| 82 |
$self->{service_rules}->{$sname} = {operator => $operator, state => $state}; |
| 83 |
$self->{wql_filter} .= $append . "Name = '" . $sname . "'"; |
| 84 |
$append = ' Or '; |
| 85 |
} |
| 86 |
|
| 87 |
if ($self->{wql_filter} eq '') { |
| 88 |
$self->{output}->add_option_msg(short_msg => "Need to specify one rule for --services option."); |
| 89 |
$self->{output}->option_exit(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
$self->{threshold} = 'WARNING' if (defined($self->{option_results}->{warning})); |
| 94 |
$self->{threshold} = 'CRITICAL' if (defined($self->{option_results}->{critical})); |
| 95 |
} |
| 96 |
|
| 97 |
sub check_auto { |
| 98 |
my ($self, %options) = @_; |
| 99 |
|
| 100 |
$self->{result} = $self->{wsman}->request( |
| 101 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 102 |
wql_filter => "Select Name, State From Win32_Service Where StartMode = 'Auto'", |
| 103 |
result_type => 'hash', |
| 104 |
hash_key => 'Name' |
| 105 |
); |
| 106 |
foreach my $name (sort(keys %{$self->{result}})) { |
| 107 |
if (defined($self->{option_results}->{exclude}) && $self->{option_results}->{exclude} ne '' && $self->{result}->{$name}->{Name} =~ /$self->{option_results}->{exclude}/) { |
| 108 |
$self->{output}->output_add(long_msg => "Skipping Service '" . $self->{result}->{$name}->{Name} . "'"); |
| 109 |
next; |
| 110 |
} |
| 111 |
|
| 112 |
$self->{output}->output_add(long_msg => "Service '" . $self->{result}->{$name}->{Name} . "' state: " . $self->{result}->{$name}->{State}); |
| 113 |
if ($self->{result}->{$name}->{State} !~ /^running$/i) { |
| 114 |
$self->{output}->output_add( |
| 115 |
severity => $self->{threshold}, |
| 116 |
short_msg => "Service '" . $self->{result}->{$name}->{Name} . "' is " . $self->{result}->{$name}->{State} |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
sub check { |
| 123 |
my ($self, %options) = @_; |
| 124 |
|
| 125 |
$self->{result} = $self->{wsman}->request( |
| 126 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 127 |
wql_filter => 'Select Name, State From Win32_Service Where ' . $self->{wql_filter}, |
| 128 |
result_type => 'hash', |
| 129 |
hash_key => 'Name' |
| 130 |
); |
| 131 |
foreach my $name (sort(keys %{$self->{service_rules}})) { |
| 132 |
if (!defined($self->{result}->{$name})) { |
| 133 |
$self->{output}->output_add( |
| 134 |
severity => 'UNKNOWN', |
| 135 |
short_msg => "Service '" . $name . "' not found" |
| 136 |
); |
| 137 |
next; |
| 138 |
} |
| 139 |
|
| 140 |
$self->{output}->output_add(long_msg => "Service '" . $name . "' state: " . $self->{result}->{$name}->{State}); |
| 141 |
if ($self->{service_rules}->{$name}->{operator} eq '=' && |
| 142 |
lc($self->{result}->{$name}->{State}) eq $self->{service_rules}->{$name}->{state}) { |
| 143 |
$self->{output}->output_add( |
| 144 |
severity => $self->{threshold}, |
| 145 |
short_msg => "Service '" . $self->{result}->{$name}->{Name} . "' is " . $self->{result}->{$name}->{State} |
| 146 |
); |
| 147 |
} elsif ($self->{service_rules}->{$name}->{operator} eq '!=' && |
| 148 |
lc($self->{result}->{$name}->{State}) ne $self->{service_rules}->{$name}->{state}) { |
| 149 |
$self->{output}->output_add( |
| 150 |
severity => $self->{threshold}, |
| 151 |
short_msg => "Service '" . $self->{result}->{$name}->{Name} . "' is " . $self->{result}->{$name}->{State} |
| 152 |
); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
sub run { |
| 158 |
my ($self, %options) = @_; |
| 159 |
$self->{wsman} = $options{wsman}; |
| 160 |
|
| 161 |
$self->{output}->output_add( |
| 162 |
severity => 'OK', |
| 163 |
short_msg => 'All service states are ok' |
| 164 |
); |
| 165 |
if (defined($self->{option_results}->{auto})) { |
| 166 |
$self->check_auto(); |
| 167 |
} else { |
| 168 |
$self->check(); |
| 169 |
} |
| 170 |
|
| 171 |
$self->{output}->display(); |
| 172 |
$self->{output}->exit(); |
| 173 |
} |
| 174 |
|
| 175 |
1; |
| 176 |
|
| 177 |
__END__ |
| 178 |
|
| 179 |
=head1 MODE |
| 180 |
|
| 181 |
Check Windows Services. |
| 182 |
|
| 183 |
=over 8 |
| 184 |
|
| 185 |
=item B<--warning> |
| 186 |
|
| 187 |
Return warning. |
| 188 |
|
| 189 |
=item B<--critical> |
| 190 |
|
| 191 |
Return critical. |
| 192 |
|
| 193 |
=item B<--services> |
| 194 |
|
| 195 |
Services to monitor. |
| 196 |
Syntax: [service_name[[=|!=]state]],... |
| 197 |
Available states are: |
| 198 |
- Stopped |
| 199 |
- Start Pending |
| 200 |
- Stop Pending |
| 201 |
- Running |
| 202 |
- Continue Pending |
| 203 |
- Pause Pending |
| 204 |
- Paused |
| 205 |
- Unknown |
| 206 |
|
| 207 |
=item B<--auto> |
| 208 |
|
| 209 |
Return threshold for auto services not running. |
| 210 |
|
| 211 |
=item B<--exclude> |
| 212 |
|
| 213 |
Exclude some services for --auto option (can be a regexp). |
| 214 |
|
| 215 |
=back |
| 216 |
|
| 217 |
=cut |