| 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::components::sensor; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use centreon::plugins::misc; |
| 26 |
|
| 27 |
my %map_sensor_status = ( |
| 28 |
1 => 'ok', |
| 29 |
2 => 'unavailable', |
| 30 |
3 => 'nonoperational', |
| 31 |
); |
| 32 |
my %map_sensor_type = ( |
| 33 |
1 => 'other', |
| 34 |
2 => 'unknown', |
| 35 |
3 => 'voltsAC', |
| 36 |
4 => 'voltsDC', |
| 37 |
5 => 'amperes', |
| 38 |
6 => 'watts', |
| 39 |
7 => 'hertz', |
| 40 |
8 => 'celsius', |
| 41 |
9 => 'percentRH', |
| 42 |
10 => 'rpm', |
| 43 |
11 => 'cmm', |
| 44 |
12 => 'truthvalue', |
| 45 |
13 => 'specialEnum', |
| 46 |
14 => 'dBm' |
| 47 |
); |
| 48 |
my %map_scale = ( |
| 49 |
1 => -24, # yocto, |
| 50 |
2 => -21, # zepto |
| 51 |
3 => -18, # atto |
| 52 |
4 => -15, # femto |
| 53 |
5 => -12, # pico |
| 54 |
6 => -9, # nano |
| 55 |
7 => -6, # micro |
| 56 |
8 => -3, # milli |
| 57 |
9 => 0, #units |
| 58 |
10 => 3, #kilo |
| 59 |
11 => 6, #mega |
| 60 |
12 => 9, #giga |
| 61 |
13 => 12, #tera |
| 62 |
14 => 18, #exa |
| 63 |
15 => 15, #peta |
| 64 |
16 => 21, #zetta |
| 65 |
17 => 24 #yotta |
| 66 |
); |
| 67 |
my %map_severity = ( |
| 68 |
1 => 'other', |
| 69 |
10 => 'minor', |
| 70 |
20 => 'major', |
| 71 |
30 => 'critical' |
| 72 |
); |
| 73 |
my %map_relation = ( |
| 74 |
1 => 'lessThan', |
| 75 |
2 => 'lessOrEqual', |
| 76 |
3 => 'greaterThan', |
| 77 |
4 => 'greaterOrEqual', |
| 78 |
5 => 'equalTo', |
| 79 |
6 => 'notEqualTo' |
| 80 |
); |
| 81 |
my %perfdata_unit = ( |
| 82 |
'other' => '', |
| 83 |
'unknown' => '', |
| 84 |
'voltsAC' => 'V', |
| 85 |
'voltsDC' => 'V', |
| 86 |
'amperes' => 'A', |
| 87 |
'watts' => 'W', |
| 88 |
'hertz' => 'Hz', |
| 89 |
'celsius' => 'C', |
| 90 |
'percentRH' => '%', |
| 91 |
'rpm' => 'rpm', |
| 92 |
'cmm' => '', |
| 93 |
'truthvalue' => '', |
| 94 |
'specialEnum' => '', |
| 95 |
'dBm' => 'dBm' |
| 96 |
); |
| 97 |
|
| 98 |
# In MIB 'CISCO-ENTITY-SENSOR-MIB' |
| 99 |
my $mapping = { |
| 100 |
entSensorType => { oid => '.1.3.6.1.4.1.9.9.91.1.1.1.1.1', map => \%map_sensor_type }, |
| 101 |
entSensorScale => { oid => '.1.3.6.1.4.1.9.9.91.1.1.1.1.2', map => \%map_scale }, |
| 102 |
entSensorPrecision => { oid => '.1.3.6.1.4.1.9.9.91.1.1.1.1.3' }, |
| 103 |
entSensorValue => { oid => '.1.3.6.1.4.1.9.9.91.1.1.1.1.4' }, |
| 104 |
entSensorStatus => { oid => '.1.3.6.1.4.1.9.9.91.1.1.1.1.5', map => \%map_sensor_status }, |
| 105 |
entSensorValueUpdateRate => { oid => '.1.3.6.1.4.1.9.9.91.1.1.1.1.7' } # 0 means no threshold check |
| 106 |
}; |
| 107 |
my $mapping2 = { |
| 108 |
entSensorThresholdSeverity => { oid => '.1.3.6.1.4.1.9.9.91.1.2.1.1.2', map => \%map_severity }, |
| 109 |
entSensorThresholdRelation => { oid => '.1.3.6.1.4.1.9.9.91.1.2.1.1.3', map => \%map_relation }, |
| 110 |
entSensorThresholdValue => { oid => '.1.3.6.1.4.1.9.9.91.1.2.1.1.4' } |
| 111 |
}; |
| 112 |
my $oid_entSensorValueEntry = '.1.3.6.1.4.1.9.9.91.1.1.1.1'; |
| 113 |
my $oid_entSensorThresholdEntry = '.1.3.6.1.4.1.9.9.91.1.2.1.1'; |
| 114 |
|
| 115 |
sub load { |
| 116 |
my ($self) = @_; |
| 117 |
|
| 118 |
push @{$self->{request}}, { oid => $oid_entSensorValueEntry, end => $mapping->{entSensorStatus}->{oid} }, |
| 119 |
{ oid => $oid_entSensorThresholdEntry, end => $mapping2->{entSensorThresholdValue}->{oid} }; |
| 120 |
} |
| 121 |
|
| 122 |
sub get_default_warning_threshold { |
| 123 |
my ($self, %options) = @_; |
| 124 |
my ($high_th, $low_th); |
| 125 |
|
| 126 |
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_entSensorThresholdEntry}})) { |
| 127 |
next if ($oid !~ /^$mapping2->{entSensorThresholdSeverity}->{oid}\.$options{instance}\.(.*)$/); |
| 128 |
my $instance = $1; |
| 129 |
my $result = $self->{snmp}->map_instance(mapping => $mapping2, results => $self->{results}->{$oid_entSensorThresholdEntry}, instance => $options{instance} . '.' . $instance); |
| 130 |
next if ($result->{entSensorThresholdSeverity} ne 'minor'); |
| 131 |
|
| 132 |
my $value = $result->{entSensorThresholdValue} * (10 ** ($options{result}->{entSensorScale}) * (10 ** -($options{result}->{entSensorPrecision}))); |
| 133 |
if ($result->{entSensorThresholdRelation} eq 'greaterOrEqual') { |
| 134 |
$high_th = $value - (1 * (10 ** ($options{result}->{entSensorScale}) * (10 ** -($options{result}->{entSensorPrecision})))); |
| 135 |
} elsif ($result->{entSensorThresholdRelation} eq 'greaterThan') { |
| 136 |
$high_th = $value; |
| 137 |
} elsif ($result->{entSensorThresholdRelation} eq 'lessOrEqual') { |
| 138 |
$low_th = $value + (1 * (10 ** ($options{result}->{entSensorScale}) * (10 ** -($options{result}->{entSensorPrecision})))); |
| 139 |
} elsif ($result->{entSensorThresholdRelation} eq 'lessThan') { |
| 140 |
$low_th = $value; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
# when it's the same value. Means no threshold. |
| 145 |
if (defined($low_th) && defined($high_th) && $high_th <= $low_th) { |
| 146 |
return ''; |
| 147 |
} |
| 148 |
my $th = ''; |
| 149 |
$th = centreon::plugins::misc::expand_exponential(value => $low_th) . ':' if (defined($low_th)); |
| 150 |
$th .= centreon::plugins::misc::expand_exponential(value => $high_th) if (defined($high_th)); |
| 151 |
$th = '~:' . $high_th if (defined($high_th) && !defined($low_th)); |
| 152 |
|
| 153 |
return $th; |
| 154 |
} |
| 155 |
|
| 156 |
sub get_default_critical_threshold { |
| 157 |
my ($self, %options) = @_; |
| 158 |
my ($high_th, $low_th); |
| 159 |
|
| 160 |
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_entSensorThresholdEntry}})) { |
| 161 |
next if ($oid !~ /^$mapping2->{entSensorThresholdSeverity}->{oid}\.$options{instance}\.(.*)$/); |
| 162 |
my $instance = $1; |
| 163 |
my $result = $self->{snmp}->map_instance(mapping => $mapping2, results => $self->{results}->{$oid_entSensorThresholdEntry}, instance => $options{instance} . '.' . $instance); |
| 164 |
next if ($result->{entSensorThresholdSeverity} !~ /major|critical/); |
| 165 |
|
| 166 |
my $value = $result->{entSensorThresholdValue} * (10 ** ($options{result}->{entSensorScale}) * (10 ** -($options{result}->{entSensorPrecision}))); |
| 167 |
if ($result->{entSensorThresholdRelation} eq 'greaterOrEqual') { |
| 168 |
$high_th = $value - (1 * (10 ** ($options{result}->{entSensorScale}) * (10 ** -($options{result}->{entSensorPrecision})))); |
| 169 |
} elsif ($result->{entSensorThresholdRelation} eq 'greaterThan') { |
| 170 |
$high_th = $value; |
| 171 |
} elsif ($result->{entSensorThresholdRelation} eq 'lessOrEqual') { |
| 172 |
$low_th = $value + (1 * (10 ** ($options{result}->{entSensorScale}) * (10 ** -($options{result}->{entSensorPrecision})))); |
| 173 |
} elsif ($result->{entSensorThresholdRelation} eq 'lessThan') { |
| 174 |
$low_th = $value; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
# when it's the same value. Means no threshold. |
| 179 |
if (defined($low_th) && defined($high_th) && $high_th <= $low_th) { |
| 180 |
return ''; |
| 181 |
} |
| 182 |
my $th = ''; |
| 183 |
$th = centreon::plugins::misc::expand_exponential(value => $low_th) . ':' if (defined($low_th)); |
| 184 |
$th .= centreon::plugins::misc::expand_exponential(value => $high_th) if (defined($high_th)); |
| 185 |
$th = '~:' . $high_th if (defined($high_th) && !defined($low_th)); |
| 186 |
|
| 187 |
return $th; |
| 188 |
} |
| 189 |
|
| 190 |
sub check { |
| 191 |
my ($self) = @_; |
| 192 |
|
| 193 |
$self->{output}->output_add(long_msg => "Checking sensors"); |
| 194 |
$self->{components}->{sensor} = {name => 'sensors', total => 0, skip => 0}; |
| 195 |
return if ($self->check_filter(section => 'sensor')); |
| 196 |
|
| 197 |
my $verify_th_update = {}; |
| 198 |
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$oid_entSensorValueEntry}})) { |
| 199 |
next if ($oid !~ /^$mapping->{entSensorStatus}->{oid}\.(.*)$/); |
| 200 |
my $instance = $1; |
| 201 |
my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$oid_entSensorValueEntry}, instance => $instance); |
| 202 |
|
| 203 |
next if (!defined($self->{results}->{ $self->{physical_name} }->{ $self->{physical_name} . '.' . $instance })); |
| 204 |
my $sensor_descr = $self->{results}->{ $self->{physical_name} }->{ $self->{physical_name} . '.' . $instance }; |
| 205 |
|
| 206 |
next if ($self->check_filter(section => 'sensor', instance => $result->{entSensorType} . '.' . $instance, name => $sensor_descr)); |
| 207 |
$self->{components}->{sensor}->{total}++; |
| 208 |
|
| 209 |
$result->{entSensorValue} = defined($result->{entSensorValue}) ? |
| 210 |
$result->{entSensorValue} * (10 ** ($result->{entSensorScale}) * (10 ** -($result->{entSensorPrecision}))) : undef; |
| 211 |
|
| 212 |
$self->{output}->output_add( |
| 213 |
long_msg => sprintf( |
| 214 |
"sensor '%s' status is '%s' [instance: %s] [value: %s %s]", |
| 215 |
$sensor_descr, $result->{entSensorStatus}, |
| 216 |
$instance, |
| 217 |
defined($result->{entSensorValue}) ? $result->{entSensorValue} : '-', |
| 218 |
$result->{entSensorType} |
| 219 |
) |
| 220 |
); |
| 221 |
my $exit = $self->get_severity(section => $result->{entSensorType}, label => 'sensor', value => $result->{entSensorStatus}); |
| 222 |
if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { |
| 223 |
$self->{output}->output_add( |
| 224 |
severity => $exit, |
| 225 |
short_msg => sprintf( |
| 226 |
"Sensor '%s/%s' status is '%s'", |
| 227 |
$sensor_descr, $instance, $result->{entSensorStatus} |
| 228 |
) |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
next if (!defined($result->{entSensorValue}) || $result->{entSensorValue} !~ /[0-9]/); |
| 233 |
|
| 234 |
my $component = 'sensor.' . $result->{entSensorType}; |
| 235 |
my ($exit2, $warn, $crit, $checked) = $self->get_severity_numeric( |
| 236 |
section => $component, |
| 237 |
instance => $instance, |
| 238 |
name => $sensor_descr, |
| 239 |
value => $result->{entSensorValue} |
| 240 |
); |
| 241 |
if ($checked == 0) { |
| 242 |
my $warn_th = get_default_warning_threshold($self, instance => $instance, result => $result); |
| 243 |
my $crit_th = get_default_critical_threshold($self, instance => $instance, result => $result); |
| 244 |
$self->{perfdata}->threshold_validate(label => 'warning-' . $component . '-instance-' . $instance, value => $warn_th); |
| 245 |
$self->{perfdata}->threshold_validate(label => 'critical-' . $component . '-instance-' . $instance, value => $crit_th); |
| 246 |
$warn = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $component . '-instance-' . $instance); |
| 247 |
$crit = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $component . '-instance-' . $instance); |
| 248 |
$exit2 = $self->{perfdata}->threshold_check( |
| 249 |
value => $result->{entSensorValue}, |
| 250 |
threshold => [ |
| 251 |
{ label => 'critical-' . $component . '-instance-' . $instance, exit_litteral => 'critical' }, |
| 252 |
{ label => 'warning-' . $component . '-instance-' . $instance, exit_litteral => 'warning' } |
| 253 |
] |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
if (!$self->{output}->is_status(value => $exit2, compare => 'ok', litteral => 1)) { |
| 258 |
# we verify if it's a entSensorValueUpdateRate is not zero |
| 259 |
if ($checked == 0) { |
| 260 |
$verify_th_update->{$instance} = { result => $result, warn => $warn, crit => $crit, sensor_descr => $sensor_descr, status => $exit2 }; |
| 261 |
next; |
| 262 |
} |
| 263 |
$self->{output}->output_add( |
| 264 |
severity => $exit2, |
| 265 |
short_msg => sprintf( |
| 266 |
"Sensor '%s/%s' is %s %s", |
| 267 |
$sensor_descr, $instance, $result->{entSensorValue}, $perfdata_unit{$result->{entSensorType}} |
| 268 |
) |
| 269 |
); |
| 270 |
} |
| 271 |
|
| 272 |
$self->{output}->perfdata_add( |
| 273 |
label => $component, unit => $perfdata_unit{$result->{entSensorType}}, |
| 274 |
nlabel => 'hardware.' . $component, |
| 275 |
instances => $sensor_descr, |
| 276 |
value => $result->{entSensorValue}, |
| 277 |
warning => $warn, |
| 278 |
critical => $crit |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
return if (scalar(keys %$verify_th_update) <= 0); |
| 283 |
my $snmp_result = $self->{snmp}->get_leef(oids => [map($mapping->{entSensorValueUpdateRate}->{oid} . '.' . $_, keys(%$verify_th_update))]); |
| 284 |
foreach (keys %$verify_th_update) { |
| 285 |
my ($warn, $crit) = ($verify_th_update->{$_}->{warn}, $verify_th_update->{$_}->{crit}); |
| 286 |
if (defined($snmp_result->{$mapping->{entSensorValueUpdateRate}->{oid} . '.' . $_}) && |
| 287 |
$snmp_result->{$mapping->{entSensorValueUpdateRate}->{oid} . '.' . $_} == 0) { |
| 288 |
($warn, $crit) = ('', ''); |
| 289 |
$verify_th_update->{$_}->{status} = 'ok'; |
| 290 |
} |
| 291 |
|
| 292 |
if (!$self->{output}->is_status(value => $verify_th_update->{$_}->{status}, compare => 'ok', litteral => 1)) { |
| 293 |
$self->{output}->output_add( |
| 294 |
severity => $verify_th_update->{$_}->{status}, |
| 295 |
short_msg => sprintf( |
| 296 |
"Sensor '%s/%s' is %s %s", |
| 297 |
$verify_th_update->{$_}->{sensor_descr}, $_, |
| 298 |
$verify_th_update->{$_}->{result}->{entSensorValue}, |
| 299 |
$perfdata_unit{ $verify_th_update->{$_}->{result}->{entSensorType} } |
| 300 |
) |
| 301 |
); |
| 302 |
} |
| 303 |
|
| 304 |
$self->{output}->perfdata_add( |
| 305 |
label => 'sensor.' . $verify_th_update->{$_}->{result}->{entSensorType}, |
| 306 |
unit => $perfdata_unit{ $verify_th_update->{$_}->{result}->{entSensorType} }, |
| 307 |
nlabel => 'hardware.sensor.' . $verify_th_update->{$_}->{result}->{entSensorType}, |
| 308 |
instances => $verify_th_update->{$_}->{sensor_descr}, |
| 309 |
value => $verify_th_update->{$_}->{result}->{entSensorValue}, |
| 310 |
warning => $warn, |
| 311 |
critical => $crit |
| 312 |
); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
1; |