| 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::snmp::mode::service; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 28 |
|
| 29 |
sub custom_status_output { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
my $msg = "state is '" . $self->{result_values}->{operating_state}; |
| 33 |
$msg .= "' [installed state: '" . $self->{result_values}->{installed_state} . "']"; |
| 34 |
return $msg; |
| 35 |
} |
| 36 |
|
| 37 |
sub prefix_services_output { |
| 38 |
my ($self, %options) = @_; |
| 39 |
|
| 40 |
return "Service '" . $options{instance_value}->{name} . "' "; |
| 41 |
} |
| 42 |
|
| 43 |
sub prefix_global_output { |
| 44 |
my ($self, %options) = @_; |
| 45 |
|
| 46 |
return 'Number of services '; |
| 47 |
} |
| 48 |
|
| 49 |
sub set_counters { |
| 50 |
my ($self, %options) = @_; |
| 51 |
|
| 52 |
$self->{maps_counters_type} = [ |
| 53 |
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' }, |
| 54 |
{ name => 'services', type => 1, cb_prefix_output => 'prefix_services_output', |
| 55 |
message_multiple => 'All services are ok', skipped_code => { -10 => 1, -11 => 1 } } |
| 56 |
]; |
| 57 |
|
| 58 |
$self->{maps_counters}->{global} = [ |
| 59 |
{ label => 'total', nlabel => 'services.total.count', display_ok => 0, set => { |
| 60 |
key_values => [ { name => 'total' } ], |
| 61 |
output_template => 'total: %d', |
| 62 |
perfdatas => [ |
| 63 |
{ template => '%d', min => 0 } |
| 64 |
] |
| 65 |
} |
| 66 |
}, |
| 67 |
{ label => 'active', nlabel => 'services.active.count', display_ok => 0, set => { |
| 68 |
key_values => [ { name => 'active' } ], |
| 69 |
output_template => 'active: %d', |
| 70 |
perfdatas => [ |
| 71 |
{ template => '%d', min => 0 } |
| 72 |
] |
| 73 |
} |
| 74 |
}, |
| 75 |
{ label => 'continue-pending', nlabel => 'services.continue.pending.count', display_ok => 0, set => { |
| 76 |
key_values => [ { name => 'continue-pending' } ], |
| 77 |
output_template => 'continue pending: %d', |
| 78 |
perfdatas => [ |
| 79 |
{ template => '%d', min => 0 } |
| 80 |
] |
| 81 |
} |
| 82 |
}, |
| 83 |
{ label => 'pause-pending', nlabel => 'services.pause.pending.count', display_ok => 0, set => { |
| 84 |
key_values => [ { name => 'pause-pending' } ], |
| 85 |
output_template => 'pause pending: %d', |
| 86 |
perfdatas => [ |
| 87 |
{ template => '%d', min => 0 } |
| 88 |
] |
| 89 |
} |
| 90 |
}, |
| 91 |
{ label => 'paused', nlabel => 'services.paused.count', display_ok => 0, set => { |
| 92 |
key_values => [ { name => 'paused' } ], |
| 93 |
output_template => 'Service paused: %d', |
| 94 |
perfdatas => [ |
| 95 |
{ template => '%d', min => 0 } |
| 96 |
] |
| 97 |
} |
| 98 |
} |
| 99 |
]; |
| 100 |
|
| 101 |
$self->{maps_counters}->{services} = [ |
| 102 |
{ label => 'status', type => 2, set => { |
| 103 |
key_values => [ { name => 'operating_state' }, { name => 'installed_state' }, { name => 'name' } ], |
| 104 |
closure_custom_output => $self->can('custom_status_output'), |
| 105 |
closure_custom_perfdata => sub { return 0; }, |
| 106 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 107 |
} |
| 108 |
} |
| 109 |
]; |
| 110 |
} |
| 111 |
|
| 112 |
sub new { |
| 113 |
my ($class, %options) = @_; |
| 114 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 115 |
bless $self, $class; |
| 116 |
|
| 117 |
$options{options}->add_options(arguments => { |
| 118 |
'filter-name:s' => { name => 'filter_name' }, |
| 119 |
'warning:s' => { name => 'warning' }, # deprecated |
| 120 |
'critical:s' => { name => 'critical' }, # deprecated |
| 121 |
'service:s@' => { name => 'service' }, # deprecated |
| 122 |
'regexp' => { name => 'use_regexp' }, # deprecated |
| 123 |
'state:s' => { name => 'state' } # deprecated |
| 124 |
}); |
| 125 |
|
| 126 |
return $self; |
| 127 |
} |
| 128 |
|
| 129 |
sub check_options { |
| 130 |
my ($self, %options) = @_; |
| 131 |
|
| 132 |
# Compatibility for deprecated options |
| 133 |
if (defined($options{option_results}->{warning}) && $options{option_results}->{warning} ne '') { |
| 134 |
$options{option_results}->{'warning-services-active-count'} = $options{option_results}->{warning}; |
| 135 |
} |
| 136 |
if (defined($options{option_results}->{critical}) && $options{option_results}->{critical} ne '') { |
| 137 |
$options{option_results}->{'critical-services-active-count'} = $options{option_results}->{critical}; |
| 138 |
} |
| 139 |
|
| 140 |
my $delimiter = ''; |
| 141 |
if (defined($options{option_results}->{service})) { |
| 142 |
my $filter = ''; |
| 143 |
for my $filter_service (@{$options{option_results}->{service}}) { |
| 144 |
next if ($filter_service eq ''); |
| 145 |
if (defined($options{option_results}->{use_regexp})) { |
| 146 |
$filter.= $delimiter . $filter_service; |
| 147 |
} else { |
| 148 |
$filter .= $delimiter . quotemeta($filter_service); |
| 149 |
} |
| 150 |
|
| 151 |
$delimiter = '|'; |
| 152 |
} |
| 153 |
|
| 154 |
if ($filter ne '' && !defined($options{option_results}->{use_regexp})) { |
| 155 |
$filter = '^(' . $filter . ')$'; |
| 156 |
} |
| 157 |
|
| 158 |
if (!defined($options{option_results}->{'filter_name'}) || $options{option_results}->{'filter_name'} eq '') { |
| 159 |
$options{option_results}->{'filter_name'} = $filter; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (defined($options{option_results}->{state}) && $options{option_results}->{state} ne '') { |
| 164 |
$options{option_results}->{'critical-status'} = "%{operating_state} !~ /$options{option_results}->{state}/"; |
| 165 |
} |
| 166 |
|
| 167 |
$self->SUPER::check_options(%options); |
| 168 |
} |
| 169 |
|
| 170 |
sub manage_selection { |
| 171 |
my ($self, %options) = @_; |
| 172 |
|
| 173 |
my $map_installed_state = { |
| 174 |
1 => 'uninstalled', |
| 175 |
2 => 'install-pending', |
| 176 |
3 => 'uninstall-pending', |
| 177 |
4 => 'installed' |
| 178 |
}; |
| 179 |
|
| 180 |
my $map_operating_state = { |
| 181 |
1 => 'active', |
| 182 |
2 => 'continue-pending', |
| 183 |
3 => 'pause-pending', |
| 184 |
4 => 'paused' |
| 185 |
}; |
| 186 |
|
| 187 |
my $mapping = { |
| 188 |
installed_state => { oid => '.1.3.6.1.4.1.77.1.2.3.1.2', map => $map_installed_state }, # svSvcInstalledState |
| 189 |
operating_state => { oid => '.1.3.6.1.4.1.77.1.2.3.1.3', map => $map_operating_state } # svSvcOperatingState |
| 190 |
}; |
| 191 |
my $table_svSvcEntry = '.1.3.6.1.4.1.77.1.2.3.1'; |
| 192 |
|
| 193 |
my $snmp_result = $options{snmp}->get_table( |
| 194 |
oid => $table_svSvcEntry, |
| 195 |
start => $mapping->{installed_state}->{oid}, |
| 196 |
end => $mapping->{operating_state}->{oid}, |
| 197 |
nothing_quit => 1 |
| 198 |
); |
| 199 |
|
| 200 |
$self->{global} = { |
| 201 |
total => 0, |
| 202 |
active => 0, |
| 203 |
'continue-pending' => 0, |
| 204 |
'pause-pending' => 0, |
| 205 |
paused => 0 |
| 206 |
}; |
| 207 |
|
| 208 |
$self->{services} = {}; |
| 209 |
|
| 210 |
foreach my $oid ($options{snmp}->oid_lex_sort(keys %$snmp_result)) { |
| 211 |
next if ($oid !~ /^$mapping->{operating_state}->{oid}\.(\d+)\.(.*)$/); |
| 212 |
my $instance = $1 . '.' . $2; |
| 213 |
|
| 214 |
my $name = $self->{output}->decode(join('', map(chr($_), split(/\./, $2)))); |
| 215 |
$self->{option_results}->{filter_name} = $self->{output}->decode($self->{option_results}->{filter_name}); |
| 216 |
|
| 217 |
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 218 |
$name !~ /$self->{option_results}->{filter_name}/); |
| 219 |
|
| 220 |
my $result = $options{snmp}->map_instance( |
| 221 |
mapping => $mapping, |
| 222 |
results => $snmp_result, |
| 223 |
instance => $instance |
| 224 |
); |
| 225 |
|
| 226 |
$self->{services}->{$instance} = { |
| 227 |
name => $name, |
| 228 |
operating_state => $result->{operating_state}, |
| 229 |
installed_state => $result->{installed_state} |
| 230 |
}; |
| 231 |
|
| 232 |
$self->{global}->{total}++; |
| 233 |
$self->{global}->{ $result->{operating_state} }++; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
1; |
| 238 |
|
| 239 |
__END__ |
| 240 |
|
| 241 |
=head1 MODE |
| 242 |
|
| 243 |
Check Windows services states |
| 244 |
|
| 245 |
=over 8 |
| 246 |
|
| 247 |
=item B<--filter-name> |
| 248 |
|
| 249 |
Filter by service name (can be a regexp). |
| 250 |
|
| 251 |
=item B<--warning-status> B<--critical-status> |
| 252 |
|
| 253 |
Set WARNING or CRITICAL threshold for status. |
| 254 |
You can use the following variables: %{operating_state}, %{installed_state}. |
| 255 |
|
| 256 |
=item B<--warning-*> B<--critical-*> |
| 257 |
|
| 258 |
Thresholds on services count. |
| 259 |
Can be: 'total', 'active', 'continue-pending', |
| 260 |
'pause-pending', 'paused'. |
| 261 |
|
| 262 |
=item B<--warning> |
| 263 |
|
| 264 |
DEPRECATED. Use --warning-active instead. |
| 265 |
|
| 266 |
=item B<--critical> |
| 267 |
|
| 268 |
DEPRECATED. Use --critical-active instead. |
| 269 |
|
| 270 |
=item B<--service> |
| 271 |
|
| 272 |
DEPRECATED. Use --filter-name instead. |
| 273 |
|
| 274 |
=item B<--regexp> |
| 275 |
|
| 276 |
DEPRECATED. Use --filter-name instead. |
| 277 |
|
| 278 |
=item B<--state> |
| 279 |
|
| 280 |
DEPRECATED. Use --critical/warning-status instead. |
| 281 |
|
| 282 |
=back |
| 283 |
|
| 284 |
=cut |