| 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::updates; |
| 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::updates; |
| 29 |
use JSON::XS; |
| 30 |
|
| 31 |
sub set_counters { |
| 32 |
my ($self, %options) = @_; |
| 33 |
|
| 34 |
$self->{maps_counters_type} = [ |
| 35 |
{ name => 'global', type => 0 } |
| 36 |
]; |
| 37 |
|
| 38 |
$self->{maps_counters}->{global} = [ |
| 39 |
{ label => 'pending-updates', nlabel => 'windows.pending.updates.count', set => { |
| 40 |
key_values => [ { name => 'num' } ], |
| 41 |
output_template => 'pending Windows updates: %s', |
| 42 |
perfdatas => [ |
| 43 |
{ template => '%s', min => 0 } |
| 44 |
] |
| 45 |
} |
| 46 |
} |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
sub new { |
| 51 |
my ($class, %options) = @_; |
| 52 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 53 |
bless $self, $class; |
| 54 |
|
| 55 |
$options{options}->add_options(arguments => { |
| 56 |
'ps-exec-only' => { name => 'ps_exec_only' }, |
| 57 |
'ps-display' => { name => 'ps_display' }, |
| 58 |
'filter-title:s' => { name => 'filter_title' }, |
| 59 |
'exclude-title:s' => { name => 'exclude_title' }, |
| 60 |
'filter-mandatory:s' => { name => 'filter_mandatory' }, |
| 61 |
'display-updates' => { name => 'display_updates' } |
| 62 |
}); |
| 63 |
|
| 64 |
return $self; |
| 65 |
} |
| 66 |
|
| 67 |
sub manage_selection { |
| 68 |
my ($self, %options) = @_; |
| 69 |
|
| 70 |
my $ps = centreon::common::powershell::windows::updates::get_powershell(); |
| 71 |
if (defined($self->{option_results}->{ps_display})) { |
| 72 |
$self->{output}->output_add( |
| 73 |
severity => 'OK', |
| 74 |
short_msg => $ps |
| 75 |
); |
| 76 |
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); |
| 77 |
$self->{output}->exit(); |
| 78 |
} |
| 79 |
|
| 80 |
my $result = $options{wsman}->execute_powershell( |
| 81 |
label => 'pendingupdates', |
| 82 |
content => centreon::plugins::misc::powershell_encoded($ps) |
| 83 |
); |
| 84 |
|
| 85 |
if (defined($self->{option_results}->{ps_exec_only})) { |
| 86 |
$self->{output}->output_add( |
| 87 |
severity => 'OK', |
| 88 |
short_msg => $result->{pendingupdates}->{stdout} |
| 89 |
); |
| 90 |
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); |
| 91 |
$self->{output}->exit(); |
| 92 |
} |
| 93 |
|
| 94 |
my $decoded; |
| 95 |
eval { |
| 96 |
$decoded = JSON::XS->new->decode($self->{output}->decode($result->{pendingupdates}->{stdout})); |
| 97 |
}; |
| 98 |
if ($@) { |
| 99 |
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); |
| 100 |
$self->{output}->option_exit(); |
| 101 |
} |
| 102 |
|
| 103 |
#[ |
| 104 |
# {"isMandatory":false, "title":"Windows Malicious Software Removal Tool x64 - v5.105 (KB890830)"}, |
| 105 |
# {"isMandatory":false,"title":"2022-09 Cumulative Update Preview for .NET Framework 3.5, 4.7.2 and 4.8 for Windows Server 2019 for x64 (KB5017855)"}, |
| 106 |
# {"isMandatory":false,"title":"2022-09 Cumulative Update for Windows Server 2019 (1809) for x64-based Systems (KB5017315)"} |
| 107 |
#] |
| 108 |
|
| 109 |
$self->{global} = { num => 0 }; |
| 110 |
foreach my $update (@$decoded) { |
| 111 |
next if (defined($self->{option_results}->{filter_title}) && $self->{option_results}->{filter_title} ne '' && |
| 112 |
$update->{title} !~ /$self->{option_results}->{filter_title}/); |
| 113 |
next if (defined($self->{option_results}->{exclude_title}) && $self->{option_results}->{exclude_title} ne '' && |
| 114 |
$update->{title} =~ /$self->{option_results}->{exclude_title}/); |
| 115 |
|
| 116 |
$update->{isMandatory} = $update->{isMandatory} =~ /^(?:true|1)$/i ? 'yes' : 'no'; |
| 117 |
next if (defined($self->{option_results}->{filter_mandatory}) && $self->{option_results}->{filter_mandatory} ne '' && |
| 118 |
$update->{isMandatory} !~ /$self->{option_results}->{filter_mandatory}/); |
| 119 |
|
| 120 |
$self->{global}->{num}++; |
| 121 |
|
| 122 |
if (defined($self->{option_results}->{display_updates})) { |
| 123 |
$update->{title} =~ s/\|/ /g; |
| 124 |
$self->{output}->output_add( |
| 125 |
long_msg => sprintf( |
| 126 |
'update: %s [mandatory: %s]', |
| 127 |
$update->{title}, |
| 128 |
$update->{isMandatory} |
| 129 |
) |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
1; |
| 136 |
|
| 137 |
__END__ |
| 138 |
|
| 139 |
=head1 MODE |
| 140 |
|
| 141 |
Check pending Windows updates. |
| 142 |
|
| 143 |
=over 8 |
| 144 |
|
| 145 |
=item B<--ps-display> |
| 146 |
|
| 147 |
Display powershell script. |
| 148 |
|
| 149 |
=item B<--ps-exec-only> |
| 150 |
|
| 151 |
Print powershell output. |
| 152 |
|
| 153 |
=item B<--filter-title> |
| 154 |
|
| 155 |
Filter Windows updates by title (can be a regexp). |
| 156 |
|
| 157 |
=item B<--exclude-title> |
| 158 |
|
| 159 |
Exclude Windows updates by title (regexp can be used). |
| 160 |
|
| 161 |
=item B<--display-updates> |
| 162 |
|
| 163 |
Display updates in verbose output. |
| 164 |
|
| 165 |
=item B<--warning-*> B<--critical-*> |
| 166 |
|
| 167 |
Thresholds. |
| 168 |
Can be: 'pending-updates'. |
| 169 |
|
| 170 |
=back |
| 171 |
|
| 172 |
=cut |