| 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::pendingreboot; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::misc; |
| 28 |
use centreon::common::powershell::windows::pendingreboot; |
| 29 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 30 |
use JSON::XS; |
| 31 |
|
| 32 |
sub custom_status_output { |
| 33 |
my ($self, %options) = @_; |
| 34 |
|
| 35 |
return sprintf( |
| 36 |
"'%s': reboot pending is %s [Windows Update: %s][Component Based Servicing: %s][SCCM Client: %s][File Rename Operations: %s][Computer Name Change: %s]", |
| 37 |
$self->{result_values}->{WindowsVersion}, |
| 38 |
$self->{result_values}->{RebootPending}, |
| 39 |
$self->{result_values}->{WindowsUpdate}, |
| 40 |
$self->{result_values}->{CBServicing}, |
| 41 |
$self->{result_values}->{CCMClientSDK}, |
| 42 |
$self->{result_values}->{PendFileRename}, |
| 43 |
$self->{result_values}->{PendComputerRename} |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
sub set_counters { |
| 48 |
my ($self, %options) = @_; |
| 49 |
|
| 50 |
$self->{maps_counters_type} = [ |
| 51 |
{ name => 'pendingreboot', type => 0 }, |
| 52 |
]; |
| 53 |
|
| 54 |
$self->{maps_counters}->{pendingreboot} = [ |
| 55 |
{ label => 'status', type => 2, warning_status => '%{RebootPending} =~ /true/i', set => { |
| 56 |
key_values => [ |
| 57 |
{ name => 'WindowsVersion' }, { name => 'CBServicing' }, { name => 'RebootPending' }, { name => 'WindowsUpdate' }, |
| 58 |
{ name => 'CCMClientSDK' }, { name => 'PendComputerRename' }, { name => 'PendFileRename' } |
| 59 |
], |
| 60 |
closure_custom_output => $self->can('custom_status_output'), |
| 61 |
closure_custom_perfdata => sub { return 0; }, |
| 62 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 63 |
} |
| 64 |
} |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
sub new { |
| 69 |
my ($class, %options) = @_; |
| 70 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 71 |
bless $self, $class; |
| 72 |
|
| 73 |
$options{options}->add_options(arguments => { |
| 74 |
'ps-exec-only' => { name => 'ps_exec_only' }, |
| 75 |
'ps-display' => { name => 'ps_display' } |
| 76 |
}); |
| 77 |
|
| 78 |
return $self; |
| 79 |
} |
| 80 |
|
| 81 |
sub manage_selection { |
| 82 |
my ($self, %options) = @_; |
| 83 |
|
| 84 |
my $ps = centreon::common::powershell::windows::pendingreboot::get_powershell(); |
| 85 |
if (defined($self->{option_results}->{ps_display})) { |
| 86 |
$self->{output}->output_add( |
| 87 |
severity => 'OK', |
| 88 |
short_msg => $ps |
| 89 |
); |
| 90 |
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); |
| 91 |
$self->{output}->exit(); |
| 92 |
} |
| 93 |
|
| 94 |
my $result = $options{wsman}->execute_powershell( |
| 95 |
label => 'pendingreboot', |
| 96 |
content => centreon::plugins::misc::powershell_encoded($ps) |
| 97 |
); |
| 98 |
|
| 99 |
if (defined($self->{option_results}->{ps_exec_only})) { |
| 100 |
$self->{output}->output_add( |
| 101 |
severity => 'OK', |
| 102 |
short_msg => $result->{pendingreboot}->{stdout} |
| 103 |
); |
| 104 |
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); |
| 105 |
$self->{output}->exit(); |
| 106 |
} |
| 107 |
|
| 108 |
my $decoded; |
| 109 |
eval { |
| 110 |
$decoded = JSON::XS->new->decode($self->{output}->decode($result->{pendingreboot}->{stdout})); |
| 111 |
}; |
| 112 |
if ($@) { |
| 113 |
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); |
| 114 |
$self->{output}->option_exit(); |
| 115 |
} |
| 116 |
|
| 117 |
#{ WindowsVersion: "Microsoft Windows 2003 Server", CBServicing: false, WindowsUpdate: false, CCMClientSDK: null, PendComputerRename: false, PendFileRename: false, PendFileRenVal: null, RebootPending: false } |
| 118 |
foreach (keys %$decoded) { |
| 119 |
$decoded->{$_} = '-' if (!defined($decoded->{$_})); |
| 120 |
$decoded->{$_} = 'true' if ($decoded->{$_} =~ /^(?:true|1)$/i); |
| 121 |
$decoded->{$_} = 'false' if ($decoded->{$_} =~ /^(?:false|0)$/i); |
| 122 |
} |
| 123 |
|
| 124 |
$self->{pendingreboot} = $decoded; |
| 125 |
} |
| 126 |
|
| 127 |
1; |
| 128 |
|
| 129 |
__END__ |
| 130 |
|
| 131 |
=head1 MODE |
| 132 |
|
| 133 |
Check pending Windows reboot. |
| 134 |
|
| 135 |
=over 8 |
| 136 |
|
| 137 |
=item B<--ps-display> |
| 138 |
|
| 139 |
Display powershell script. |
| 140 |
|
| 141 |
=item B<--ps-exec-only> |
| 142 |
|
| 143 |
Print powershell output. |
| 144 |
|
| 145 |
=item B<--warning-status> |
| 146 |
|
| 147 |
Define the conditions to match for the status to be WARNING (default: '%{RebootPending} =~ /true/i'). |
| 148 |
You can use the following variables: %{RebootPending}, %{WindowsUpdate}, %{CBServicing}, %{CCMClientSDK}, |
| 149 |
%{PendFileRename}, %{PendComputerRename}. |
| 150 |
|
| 151 |
=item B<--critical-status> |
| 152 |
|
| 153 |
Define the conditions to match for the status to be CRITICAL (default: ''). |
| 154 |
You can use the following variables: %{RebootPending}, %{WindowsUpdate}, %{CBServicing}, %{CCMClientSDK}, |
| 155 |
%{PendFileRename}, %{PendComputerRename}. |
| 156 |
|
| 157 |
=back |
| 158 |
|
| 159 |
=cut |