| 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::h3c::snmp::mode::cpu; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::statefile; |
| 28 |
|
| 29 |
sub set_counters { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
$self->{maps_counters_type} = [ |
| 33 |
{ name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All CPU usages are ok' } |
| 34 |
]; |
| 35 |
|
| 36 |
$self->{maps_counters}->{cpu} = [ |
| 37 |
{ label => 'usage', nlabel => 'cpu.utilization.percentage', set => { |
| 38 |
key_values => [ { name => 'EntityExtCpuUsage' }, { name => 'num' }, ], |
| 39 |
output_template => ' : %.2f %%', |
| 40 |
perfdatas => [ |
| 41 |
{ label => 'cpu', value => 'EntityExtCpuUsage', template => '%.2f', |
| 42 |
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'num' }, |
| 43 |
], |
| 44 |
} |
| 45 |
}, |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
sub prefix_cpu_output { |
| 50 |
my ($self, %options) = @_; |
| 51 |
|
| 52 |
return "CPU '" . $options{instance_value}->{num} . "' Usage "; |
| 53 |
} |
| 54 |
|
| 55 |
sub new { |
| 56 |
my ($class, %options) = @_; |
| 57 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 58 |
bless $self, $class; |
| 59 |
|
| 60 |
$options{options}->add_options(arguments => { |
| 61 |
"reload-cache-time:s" => { name => 'reload_cache_time', default => 180 }, |
| 62 |
"display-entity-name" => { name => 'display_entity_name' }, |
| 63 |
}); |
| 64 |
|
| 65 |
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); |
| 66 |
return $self; |
| 67 |
} |
| 68 |
|
| 69 |
sub check_options { |
| 70 |
my ($self, %options) = @_; |
| 71 |
$self->SUPER::check_options(%options); |
| 72 |
|
| 73 |
$self->{statefile_cache}->check_options(%options); |
| 74 |
} |
| 75 |
|
| 76 |
my $oid_entPhysicalEntry = '.1.3.6.1.2.1.47.1.1.1.1'; |
| 77 |
my $oid_entPhysicalDescr = '.1.3.6.1.2.1.47.1.1.1.1.2'; |
| 78 |
my $oid_entPhysicalContainedIn = '.1.3.6.1.2.1.47.1.1.1.1.4'; |
| 79 |
my $oid_entPhysicalClass = '.1.3.6.1.2.1.47.1.1.1.1.5'; |
| 80 |
my $oid_entPhysicalName = '.1.3.6.1.2.1.47.1.1.1.1.7'; |
| 81 |
|
| 82 |
sub check_cache { |
| 83 |
my ($self, %options) = @_; |
| 84 |
|
| 85 |
$self->{hostname} = $self->{snmp}->get_hostname(); |
| 86 |
$self->{snmp_port} = $self->{snmp}->get_port(); |
| 87 |
|
| 88 |
# init cache file |
| 89 |
$self->{write_cache} = 0; |
| 90 |
my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_h3c_entity_' . $self->{hostname} . '_' . $self->{snmp_port}); |
| 91 |
my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp'); |
| 92 |
if ($has_cache_file == 0 || |
| 93 |
!defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{option_results}->{reload_cache_time}) * 60))) { |
| 94 |
push @{$self->{snmp_request}}, { oid => $oid_entPhysicalEntry, start => $oid_entPhysicalDescr, end => $oid_entPhysicalName }; |
| 95 |
$self->{write_cache} = 1; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
sub write_cache { |
| 100 |
my ($self, %options) = @_; |
| 101 |
|
| 102 |
if ($self->{write_cache} == 1) { |
| 103 |
my $datas = {}; |
| 104 |
$datas->{last_timestamp} = time(); |
| 105 |
$datas->{oids} = $self->{results}->{$oid_entPhysicalEntry}; |
| 106 |
$self->{statefile_cache}->write(data => $datas); |
| 107 |
} else { |
| 108 |
$self->{results}->{$oid_entPhysicalEntry} = $self->{statefile_cache}->get(name => 'oids'); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
sub get_long_name { |
| 113 |
my ($self, %options) = @_; |
| 114 |
|
| 115 |
my @names = ($self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $options{instance}}); |
| 116 |
my %loop = ($options{instance} => 1); |
| 117 |
my $child = $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalContainedIn . '.' . $options{instance}}; |
| 118 |
while (1) { |
| 119 |
last if (!defined($child) || defined($loop{$child}) || !defined($self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $child})); |
| 120 |
|
| 121 |
unshift @names, $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $child}; |
| 122 |
$loop{$child} = 1; |
| 123 |
$child = $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalContainedIn . '.' . $child}; |
| 124 |
} |
| 125 |
|
| 126 |
return join(' > ', @names); |
| 127 |
} |
| 128 |
|
| 129 |
sub manage_selection { |
| 130 |
my ($self, %options) = @_; |
| 131 |
|
| 132 |
$self->{snmp} = $options{snmp}; |
| 133 |
my $oid_h3cEntityExtCpuUsage = '.1.3.6.1.4.1.2011.10.2.6.1.1.1.1.6'; |
| 134 |
my $oid_hh3cEntityExtCpuUsage = '.1.3.6.1.4.1.25506.2.6.1.1.1.1.6'; |
| 135 |
$self->{snmp_request} = [ |
| 136 |
{ oid => $oid_h3cEntityExtCpuUsage }, |
| 137 |
{ oid => $oid_hh3cEntityExtCpuUsage }, |
| 138 |
]; |
| 139 |
$self->check_cache() if (defined($self->{option_results}->{display_entity_name})); |
| 140 |
|
| 141 |
$self->{results} = $self->{snmp}->get_multiple_table(oids => $self->{snmp_request}, |
| 142 |
nothing_quit => 1); |
| 143 |
$self->write_cache() if (defined($self->{option_results}->{display_entity_name})); |
| 144 |
$self->{branch} = $oid_hh3cEntityExtCpuUsage; |
| 145 |
if (defined($self->{results}->{$oid_h3cEntityExtCpuUsage}) && scalar(keys %{$self->{results}->{$oid_h3cEntityExtCpuUsage}}) > 0) { |
| 146 |
$self->{branch} = $oid_h3cEntityExtCpuUsage; |
| 147 |
} |
| 148 |
|
| 149 |
my $mapping = { |
| 150 |
EntityExtCpuUsage => { oid => $self->{branch} }, |
| 151 |
}; |
| 152 |
|
| 153 |
$self->{cpu} = {}; |
| 154 |
foreach my $oid (keys %{$self->{results}->{$self->{branch}}}) { |
| 155 |
$oid =~ /^$mapping->{EntityExtCpuUsage}->{oid}\.(.*)$/; |
| 156 |
my $instance = $1; |
| 157 |
my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$self->{branch}}, instance => $instance); |
| 158 |
|
| 159 |
if (defined($result->{EntityExtCpuUsage}) && $result->{EntityExtCpuUsage} > 0) { |
| 160 |
my $name = '-'; |
| 161 |
|
| 162 |
if (defined($self->{option_results}->{display_entity_name})) { |
| 163 |
$name = $self->get_long_name(instance => $instance); |
| 164 |
} |
| 165 |
$self->{cpu}->{$instance} = { num => $instance, name => $name, %$result}; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (scalar(keys %{$self->{cpu}}) <= 0) { |
| 170 |
$self->{output}->add_option_msg(short_msg => "No entry found."); |
| 171 |
$self->{output}->option_exit(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
1; |
| 176 |
|
| 177 |
__END__ |
| 178 |
|
| 179 |
=head1 MODE |
| 180 |
|
| 181 |
Check CPU usages. |
| 182 |
|
| 183 |
=over 8 |
| 184 |
|
| 185 |
=item B<--warning-usage> |
| 186 |
|
| 187 |
Warning threshold. |
| 188 |
|
| 189 |
=item B<--critical-usage> |
| 190 |
|
| 191 |
Critical threshold. |
| 192 |
|
| 193 |
=item B<--reload-cache-time> |
| 194 |
|
| 195 |
Time in seconds before reloading cache file (default: 180). |
| 196 |
|
| 197 |
=item B<--display-entity-name> |
| 198 |
|
| 199 |
Display entity name of the component. A cache file will be used. |
| 200 |
|
| 201 |
=back |
| 202 |
|
| 203 |
=cut |