| 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 centreon::common::cisco::standard::snmp::mode::cpu; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub set_counters { |
| 29 |
my ($self, %options) = @_; |
| 30 |
|
| 31 |
$self->{maps_counters_type} = [ |
| 32 |
{ name => 'cpu_avg', type => 0, cb_prefix_output => 'prefix_cpu_avg_output', message_separator => ' ', skipped_code => { -10 => 1 } }, |
| 33 |
{ name => 'cpu_core', type => 1, cb_prefix_output => 'prefix_cpu_core_output', message_separator => ' ', message_multiple => 'All core cpu are ok', skipped_code => { -10 => 1 } } |
| 34 |
]; |
| 35 |
|
| 36 |
$self->{maps_counters}->{cpu_avg} = [ |
| 37 |
{ label => 'average-5s', nlabel => 'cpu.utilization.5s.percentage', set => { |
| 38 |
key_values => [ { name => 'average_5s' } ], |
| 39 |
output_template => '%.2f %% (5s)', |
| 40 |
perfdatas => [ |
| 41 |
{ label => 'total_cpu_5s_avg', value => 'average_5s', template => '%.2f', |
| 42 |
min => 0, max => 100, unit => '%' }, |
| 43 |
], |
| 44 |
} |
| 45 |
}, |
| 46 |
{ label => 'average-1m', nlabel => 'cpu.utilization.1m.percentage', set => { |
| 47 |
key_values => [ { name => 'average_1m' } ], |
| 48 |
output_template => '%.2f %% (1m)', |
| 49 |
perfdatas => [ |
| 50 |
{ label => 'total_cpu_1m_avg', value => 'average_1m', template => '%.2f', |
| 51 |
min => 0, max => 100, unit => '%' }, |
| 52 |
], |
| 53 |
} |
| 54 |
}, |
| 55 |
{ label => 'average-5m', nlabel => 'cpu.utilization.5m.percentage', set => { |
| 56 |
key_values => [ { name => 'average_5m' } ], |
| 57 |
output_template => '%.2f %% (5m)', |
| 58 |
perfdatas => [ |
| 59 |
{ label => 'total_cpu_5m_avg', value => 'average_5m', template => '%.2f', |
| 60 |
min => 0, max => 100, unit => '%' }, |
| 61 |
], |
| 62 |
} |
| 63 |
}, |
| 64 |
]; |
| 65 |
|
| 66 |
$self->{maps_counters}->{cpu_core} = [ |
| 67 |
{ label => 'core-5s', nlabel => 'core.cpu.utilization.5s.percentage', set => { |
| 68 |
key_values => [ { name => 'cpu_5s' }, { name => 'display' } ], |
| 69 |
output_template => '%.2f %% (5s)', |
| 70 |
perfdatas => [ |
| 71 |
{ label => 'cpu_5s', value => 'cpu_5s', template => '%.2f', |
| 72 |
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' }, |
| 73 |
], |
| 74 |
} |
| 75 |
}, |
| 76 |
{ label => 'core-1m', nlabel => 'core.cpu.utilization.1m.percentage', set => { |
| 77 |
key_values => [ { name => 'cpu_1m' }, { name => 'display' } ], |
| 78 |
output_template => '%.2f %% (1m)', |
| 79 |
perfdatas => [ |
| 80 |
{ label => 'cpu_1m', value => 'cpu_1m', template => '%.2f', |
| 81 |
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' }, |
| 82 |
], |
| 83 |
} |
| 84 |
}, |
| 85 |
{ label => 'core-5m', nlabel => 'core.cpu.utilization.5m.percentage', set => { |
| 86 |
key_values => [ { name => 'cpu_5m' }, { name => 'display' } ], |
| 87 |
output_template => '%.2f %% (5m)', |
| 88 |
perfdatas => [ |
| 89 |
{ label => 'cpu_5m', value => 'cpu_5m', template => '%.2f', |
| 90 |
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' }, |
| 91 |
], |
| 92 |
} |
| 93 |
}, |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
sub prefix_cpu_avg_output { |
| 98 |
my ($self, %options) = @_; |
| 99 |
|
| 100 |
return $self->{cpu_avg}->{count} . " CPU(s) average usage is "; |
| 101 |
} |
| 102 |
|
| 103 |
sub prefix_cpu_core_output { |
| 104 |
my ($self, %options) = @_; |
| 105 |
|
| 106 |
return "CPU '" . $options{instance_value}->{display} . "' usage "; |
| 107 |
} |
| 108 |
|
| 109 |
sub new { |
| 110 |
my ($class, %options) = @_; |
| 111 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 112 |
bless $self, $class; |
| 113 |
|
| 114 |
$options{options}->add_options(arguments => { |
| 115 |
'check-order:s' => { name => 'check_order', default => 'process,old_sys,system_ext' }, |
| 116 |
}); |
| 117 |
|
| 118 |
return $self; |
| 119 |
} |
| 120 |
|
| 121 |
sub check_nexus_cpu { |
| 122 |
my ($self, %options) = @_; |
| 123 |
|
| 124 |
return if (!defined($options{snmp_result}->{$options{oid} . '.0'})); |
| 125 |
|
| 126 |
my $instance = 0; |
| 127 |
$self->{cpu_core}->{$instance} = { |
| 128 |
display => $instance, |
| 129 |
cpu_5m => $options{snmp_result}->{$options{oid} . '.0'}, |
| 130 |
}; |
| 131 |
|
| 132 |
$self->{checked_cpu} = 1; |
| 133 |
} |
| 134 |
|
| 135 |
sub check_table_cpu { |
| 136 |
my ($self, %options) = @_; |
| 137 |
|
| 138 |
return if ($self->{checked_cpu} == 1); |
| 139 |
|
| 140 |
my $instances = {}; |
| 141 |
foreach my $oid (keys %{$options{snmp_result}}) { |
| 142 |
$oid =~ /\.([0-9]+)$/; |
| 143 |
next if (defined($instances->{$1})); |
| 144 |
$instances->{$1} = 1; |
| 145 |
my $instance = $1; |
| 146 |
|
| 147 |
my $cpu5sec = defined($options{snmp_result}->{$options{sec5} . '.' . $instance}) ? $options{snmp_result}->{$options{sec5} . '.' . $instance} : undef; |
| 148 |
my $cpu1min = defined($options{snmp_result}->{$options{min1} . '.' . $instance}) ? $options{snmp_result}->{$options{min1} . '.' . $instance} : undef; |
| 149 |
my $cpu5min = defined($options{snmp_result}->{$options{min5} . '.' . $instance}) ? $options{snmp_result}->{$options{min5} . '.' . $instance} : undef; |
| 150 |
|
| 151 |
# Case that it's maybe other CPU oid in table for datas. |
| 152 |
next if (!defined($cpu5sec) && !defined($cpu1min) && !defined($cpu5min)); |
| 153 |
|
| 154 |
$self->{checked_cpu} = 1; |
| 155 |
$self->{cpu_core}->{$instance} = { |
| 156 |
display => $instance, |
| 157 |
cpu_5s => $cpu5sec, |
| 158 |
cpu_1m => $cpu1min, |
| 159 |
cpu_5m => $cpu5min, |
| 160 |
}; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
sub check_cpu_average { |
| 165 |
my ($self, %options) = @_; |
| 166 |
|
| 167 |
my $count = scalar(keys %{$self->{cpu_core}}); |
| 168 |
my ($avg_5s, $avg_1m, $avg_5m); |
| 169 |
foreach (values %{$self->{cpu_core}}) { |
| 170 |
$avg_5s = defined($avg_5s) ? $avg_5s + $_->{cpu_5s} : $_->{cpu_5s} |
| 171 |
if (defined($_->{cpu_5s})); |
| 172 |
$avg_1m = defined($avg_1m) ? $avg_1m + $_->{cpu_1m} : $_->{cpu_1m} |
| 173 |
if (defined($_->{cpu_1m})); |
| 174 |
$avg_5m = defined($avg_5m) ? $avg_5m + $_->{cpu_5m} : $_->{cpu_5m} |
| 175 |
if (defined($_->{cpu_5m})); |
| 176 |
} |
| 177 |
|
| 178 |
$self->{cpu_avg} = { |
| 179 |
average_5s => defined($avg_5s) ? $avg_5s / $count : undef, |
| 180 |
average_1m => defined($avg_1m) ? $avg_1m / $count : undef, |
| 181 |
average_5m => defined($avg_5m) ? $avg_5m / $count : undef, |
| 182 |
count => $count, |
| 183 |
}; |
| 184 |
} |
| 185 |
|
| 186 |
sub check_cpu_process { |
| 187 |
my ($self, %options) = @_; |
| 188 |
|
| 189 |
return if ($self->{checked_cpu} == 1); |
| 190 |
|
| 191 |
# Cisco IOS Software releases later to 12.0(3)T and prior to 12.2(3.5) |
| 192 |
my $oid_cpmCPUTotalEntry = '.1.3.6.1.4.1.9.9.109.1.1.1.1'; |
| 193 |
my $oid_cpmCPUTotal5sec = '.1.3.6.1.4.1.9.9.109.1.1.1.1.3'; |
| 194 |
my $oid_cpmCPUTotal1min = '.1.3.6.1.4.1.9.9.109.1.1.1.1.4'; |
| 195 |
my $oid_cpmCPUTotal5min = '.1.3.6.1.4.1.9.9.109.1.1.1.1.5'; |
| 196 |
# Cisco IOS Software releases 12.2(3.5) or later |
| 197 |
my $oid_cpmCPUTotal5minRev = '.1.3.6.1.4.1.9.9.109.1.1.1.1.8'; |
| 198 |
my $oid_cpmCPUTotal1minRev = '.1.3.6.1.4.1.9.9.109.1.1.1.1.7'; |
| 199 |
my $oid_cpmCPUTotal5secRev = '.1.3.6.1.4.1.9.9.109.1.1.1.1.6'; |
| 200 |
|
| 201 |
my $snmp_result = $self->{snmp}->get_table( |
| 202 |
oid => $oid_cpmCPUTotalEntry, start => $oid_cpmCPUTotal5sec, end => $oid_cpmCPUTotal5minRev |
| 203 |
); |
| 204 |
$self->check_table_cpu(snmp_result => $snmp_result, sec5 => $oid_cpmCPUTotal5secRev, min1 => $oid_cpmCPUTotal1minRev, min5 => $oid_cpmCPUTotal5minRev); |
| 205 |
$self->check_table_cpu(snmp_result => $snmp_result, sec5 => $oid_cpmCPUTotal5sec, min1 => $oid_cpmCPUTotal1min, min5 => $oid_cpmCPUTotal5min); |
| 206 |
} |
| 207 |
|
| 208 |
sub check_cpu_old_sys { |
| 209 |
my ($self, %options) = @_; |
| 210 |
|
| 211 |
return if ($self->{checked_cpu} == 1); |
| 212 |
|
| 213 |
# Cisco IOS Software releases prior to 12.0(3)T |
| 214 |
my $oid_lcpu = '.1.3.6.1.4.1.9.2.1'; |
| 215 |
my $oid_busyPer = '.1.3.6.1.4.1.9.2.1.56'; # .0 in reality |
| 216 |
my $oid_avgBusy1 = '.1.3.6.1.4.1.9.2.1.57'; # .0 in reality |
| 217 |
my $oid_avgBusy5 = '.1.3.6.1.4.1.9.2.1.58'; # .0 in reality |
| 218 |
|
| 219 |
my $snmp_result = $self->{snmp}->get_table( |
| 220 |
oid => $oid_lcpu, start => $oid_busyPer, end => $oid_avgBusy5 |
| 221 |
); |
| 222 |
$self->check_table_cpu(snmp_result => $snmp_result, sec5 => $oid_busyPer, min1 => $oid_avgBusy1, min5 => $oid_avgBusy5); |
| 223 |
} |
| 224 |
|
| 225 |
sub check_cpu_system_ext { |
| 226 |
my ($self, %options) = @_; |
| 227 |
|
| 228 |
return if ($self->{checked_cpu} == 1); |
| 229 |
|
| 230 |
# Cisco Nexus |
| 231 |
my $oid_cseSysCPUUtilization = '.1.3.6.1.4.1.9.9.305.1.1.1'; # .0 in reality |
| 232 |
my $snmp_result = $self->{snmp}->get_table( |
| 233 |
oid => $oid_cseSysCPUUtilization |
| 234 |
); |
| 235 |
|
| 236 |
$self->check_nexus_cpu(snmp_result => $snmp_result, oid => $oid_cseSysCPUUtilization); |
| 237 |
} |
| 238 |
|
| 239 |
sub manage_selection { |
| 240 |
my ($self, %options) = @_; |
| 241 |
|
| 242 |
$self->{snmp} = $options{snmp}; |
| 243 |
$self->{cpu_avg} = {}; |
| 244 |
$self->{cpu_core} = {}; |
| 245 |
$self->{checked_cpu} = 0; |
| 246 |
|
| 247 |
foreach (split /,/, $self->{option_results}->{check_order}) { |
| 248 |
my $method = $self->can('check_cpu_' . $_); |
| 249 |
if ($method) { |
| 250 |
$self->$method(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
if ($self->{checked_cpu} == 0) { |
| 255 |
$self->{output}->add_option_msg(short_msg => "Cannot find CPU informations"); |
| 256 |
$self->{output}->option_exit(); |
| 257 |
} |
| 258 |
|
| 259 |
$self->check_cpu_average(); |
| 260 |
} |
| 261 |
|
| 262 |
1; |
| 263 |
|
| 264 |
__END__ |
| 265 |
|
| 266 |
=head1 MODE |
| 267 |
|
| 268 |
Check CPU usage (CISCO-PROCESS-MIB and CISCO-SYSTEM-EXT-MIB). |
| 269 |
|
| 270 |
=over 8 |
| 271 |
|
| 272 |
=item B<--check-order> |
| 273 |
|
| 274 |
Check CPU in standard cisco mib. If you have some issue (wrong CPU information in a specific mib), you can change the order |
| 275 |
(default: 'process,old_sys,system_ext'). |
| 276 |
|
| 277 |
=item B<--warning-*> B<--critical-*> |
| 278 |
|
| 279 |
Thresholds. |
| 280 |
Can be: 'core-5s', 'core-1m', 'core-5m', 'average-5s', 'average-1m', 'average-5m'. |
| 281 |
|
| 282 |
=back |
| 283 |
|
| 284 |
=cut |