| 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::cpu; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use Digest::MD5 qw(md5_hex); |
| 28 |
|
| 29 |
sub custom_cpu_avg_calc { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
my ($skipped, $buffer) = (1, 1); |
| 33 |
my ($count, $total_cpu) = (0, 0); |
| 34 |
|
| 35 |
foreach (keys %{$options{new_datas}}) { |
| 36 |
if (/^(.*?cpu\d+)_PercentProcessorTime/) { |
| 37 |
my $prefix = $1; |
| 38 |
$skipped = 0; |
| 39 |
next if (!defined($options{old_datas}->{$_})); |
| 40 |
$buffer = 0; |
| 41 |
if($options{old_datas}->{$prefix . '_PercentProcessorTime'} > $options{new_datas}->{$prefix . '_PercentProcessorTime'}) { |
| 42 |
$options{old_datas}->{$prefix . '_PercentProcessorTime'} = 0; |
| 43 |
} |
| 44 |
if($options{old_datas}->{$prefix . '_Timestamp_Sys100NS'} > $options{new_datas}->{$prefix . '_Timestamp_Sys100NS'}) { |
| 45 |
$options{old_datas}->{$prefix . '_Timestamp_Sys100NS'} = 0; |
| 46 |
} |
| 47 |
|
| 48 |
# |
| 49 |
#Cal Method ref: http://technet.microsoft.com/en-us/library/cc757283%28WS.10%29.aspx |
| 50 |
# |
| 51 |
my $cpu_core = (1 - ( $options{new_datas}->{$prefix . '_PercentProcessorTime'} - $options{old_datas}->{$prefix . '_PercentProcessorTime'} ) / |
| 52 |
( $options{new_datas}->{$prefix . '_Timestamp_Sys100NS'} - $options{old_datas}->{$prefix . '_Timestamp_Sys100NS'} ) ) * 100; |
| 53 |
if ($cpu_core > 0) { |
| 54 |
$total_cpu += $cpu_core; |
| 55 |
} |
| 56 |
$count++; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return -10 if ($skipped == 1); |
| 61 |
if ($buffer == 1) { |
| 62 |
$self->{error_msg} = "Buffer creation"; |
| 63 |
return -1; |
| 64 |
} |
| 65 |
|
| 66 |
$self->{result_values}->{prct_used} = $total_cpu / $count; |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
sub custom_cpu_core_calc { |
| 71 |
my ($self, %options) = @_; |
| 72 |
|
| 73 |
# |
| 74 |
#Core Calc: (1 - (270377812500 - 247044062500) / |
| 75 |
# (132846755243261461 - 132846731625406368) ) * 100 = 1.20292504074261 |
| 76 |
# |
| 77 |
my $core_usage = (1 - ( $options{new_datas}->{$self->{instance} . '_PercentProcessorTime'} - $options{old_datas}->{$self->{instance} . '_PercentProcessorTime'} ) / |
| 78 |
( $options{new_datas}->{$self->{instance} . '_Timestamp_Sys100NS'} - $options{old_datas}->{$self->{instance} . '_Timestamp_Sys100NS'} ) ) * 100; |
| 79 |
$self->{result_values}->{prct_used} = $core_usage; |
| 80 |
|
| 81 |
if ($core_usage < 0) { |
| 82 |
$self->{result_values}->{prct_used} = 0; |
| 83 |
} else { |
| 84 |
$self->{result_values}->{prct_used} = $core_usage; |
| 85 |
} |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
sub prefix_cpu_core_output { |
| 91 |
my ($self, %options) = @_; |
| 92 |
|
| 93 |
return "CPU '" . $options{instance_value}->{display} . "' "; |
| 94 |
} |
| 95 |
|
| 96 |
sub set_counters { |
| 97 |
my ($self, %options) = @_; |
| 98 |
|
| 99 |
$self->{maps_counters_type} = [ |
| 100 |
{ name => 'cpu_avg', type => 0 }, |
| 101 |
{ name => 'cpu_core', type => 1, cb_prefix_output => 'prefix_cpu_core_output' } |
| 102 |
]; |
| 103 |
|
| 104 |
$self->{maps_counters}->{cpu_avg} = [ |
| 105 |
{ label => 'average', nlabel => 'cpu.utilization.percentage', set => { |
| 106 |
key_values => [], |
| 107 |
closure_custom_calc => $self->can('custom_cpu_avg_calc'), |
| 108 |
manual_keys => 1, |
| 109 |
output_template => 'CPU(s) average usage is %.2f %%', |
| 110 |
output_use => 'prct_used', threshold_use => 'prct_used', |
| 111 |
perfdatas => [ |
| 112 |
{ value => 'prct_used', template => '%.2f', |
| 113 |
min => 0, max => 100, unit => '%' } |
| 114 |
] |
| 115 |
} |
| 116 |
} |
| 117 |
]; |
| 118 |
|
| 119 |
$self->{maps_counters}->{cpu_core} = [ |
| 120 |
{ label => 'core', nlabel => 'core.cpu.utilization.percentage', set => { |
| 121 |
key_values => [ |
| 122 |
{ name => 'PercentProcessorTime', diff => 1 }, { name => 'Timestamp_Sys100NS', diff => 1 }, { name => 'display' } |
| 123 |
], |
| 124 |
closure_custom_calc => $self->can('custom_cpu_core_calc'), |
| 125 |
output_template => 'usage: %.2f %%', |
| 126 |
output_use => 'prct_used', threshold_use => 'prct_used', |
| 127 |
perfdatas => [ |
| 128 |
{ value => 'prct_used', template => '%.2f', |
| 129 |
min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 130 |
] |
| 131 |
} |
| 132 |
} |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
sub new { |
| 137 |
my ($class, %options) = @_; |
| 138 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); |
| 139 |
bless $self, $class; |
| 140 |
|
| 141 |
$options{options}->add_options(arguments => { |
| 142 |
}); |
| 143 |
|
| 144 |
return $self; |
| 145 |
} |
| 146 |
|
| 147 |
sub manage_selection { |
| 148 |
my ($self, %options) = @_; |
| 149 |
|
| 150 |
my $results = $options{wsman}->request( |
| 151 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 152 |
wql_filter => "select Name,PercentProcessorTime,Timestamp_Sys100NS from Win32_PerfRawData_PerfOS_Processor where Name != '_Total'", |
| 153 |
result_type => 'array' |
| 154 |
); |
| 155 |
|
| 156 |
$self->{cpu_avg} = {}; |
| 157 |
$self->{cpu_core} = {}; |
| 158 |
foreach (@$results) { |
| 159 |
my $cpu_number = $_->{Name}; |
| 160 |
|
| 161 |
$self->{cpu_core}->{$cpu_number} = { |
| 162 |
display => $cpu_number, |
| 163 |
PercentProcessorTime => $_->{PercentProcessorTime}, |
| 164 |
Timestamp_Sys100NS => $_->{Timestamp_Sys100NS} |
| 165 |
}; |
| 166 |
$self->{cpu_avg}->{'cpu' . $cpu_number . '_PercentProcessorTime'} = $_->{PercentProcessorTime}; |
| 167 |
$self->{cpu_avg}->{'cpu' . $cpu_number . '_Timestamp_Sys100NS'} = $_->{Timestamp_Sys100NS}; |
| 168 |
} |
| 169 |
|
| 170 |
$self->{cache_name} = 'windows_wsman_' . $options{wsman}->get_hostname() . '_' . $options{wsman}->get_port . '_' . $self->{mode} . '_' . |
| 171 |
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); |
| 172 |
} |
| 173 |
|
| 174 |
1; |
| 175 |
|
| 176 |
__END__ |
| 177 |
|
| 178 |
=head1 MODE |
| 179 |
|
| 180 |
Monitor the processor usage. |
| 181 |
|
| 182 |
=over 8 |
| 183 |
|
| 184 |
=item B<--warning-average> |
| 185 |
|
| 186 |
Warning threshold average CPU utilization. |
| 187 |
|
| 188 |
=item B<--critical-average> |
| 189 |
|
| 190 |
Critical threshold average CPU utilization. |
| 191 |
|
| 192 |
=item B<--warning-core> |
| 193 |
|
| 194 |
Warning thresholds for each CPU core |
| 195 |
|
| 196 |
=item B<--critical-core> |
| 197 |
|
| 198 |
Critical thresholds for each CPU core |
| 199 |
|
| 200 |
=back |
| 201 |
|
| 202 |
=cut |