From aefe4faafa6fe59984ec4e844bd896c65914621c Mon Sep 17 00:00:00 2001 From: omercier Date: Mon, 5 Feb 2024 16:59:28 +0100 Subject: [PATCH] Various enhancements: * removed the default comments * added relevant comments * filled the help * improved some indentations * added a proposition about how to handle empty columns in Robot Examples table --- .../ups/sputnik/snmp/mode/environment.pm | 49 ++++++++++--------- .../snmp/hardware-ups-sputnik-snmp.robot | 44 ++++++++--------- 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/hardware/ups/sputnik/snmp/mode/environment.pm b/src/hardware/ups/sputnik/snmp/mode/environment.pm index 54926dd3e2..9d80a3a97f 100644 --- a/src/hardware/ups/sputnik/snmp/mode/environment.pm +++ b/src/hardware/ups/sputnik/snmp/mode/environment.pm @@ -28,18 +28,11 @@ use warnings; sub new { my ($class, %options) = @_; - # All options/properties of this mode, always add the force_new_perfdata => 1 to enable new metric/performance data naming. - # It also where you can specify that the plugin uses a cache file for example my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; # Declare options $options{options}->add_options(arguments => { - # One the left it's the option name that will be used in the command line. The ':s' at the end is to - # define that this options takes a value. - # On the right, it's the code name for this option, optionnaly you can define a default value so the user - # doesn't have to set it. - # option name => variable name 'filter-id:s' => { name => 'filter_id' } }); @@ -56,9 +49,6 @@ sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - # cpu will receive value for both instances (768 and 769) : the type => 1 explicits that - # You can define a callback (cb) function to manage the output prefix. This function is called - # each time a value is passed to the counter and can be shared across multiple counters. { name => 'sensors', type => 1, cb_prefix_output => 'prefix_sensors_output', message_multiple => 'All sensors are ok' } ]; @@ -67,7 +57,6 @@ sub set_counters { key_values => [ { name => 'temperature' }, { name => 'display' } ], output_template => 'temperature %.2f C', perfdatas => [ - # we add the label_extra_instance option to have one perfdata per instance { label => 'temperature', template => '%.2f', unit => 'C', label_extra_instance => 1, instance_use => 'display' } ] } @@ -76,7 +65,6 @@ sub set_counters { key_values => [ { name => 'humidity' }, { name => 'display' } ], output_template => 'humidity %s %%', perfdatas => [ - # we add the label_extra_instance option to have one perfdata per instance { label => 'humidity', template => '%s', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' } ] } @@ -87,13 +75,14 @@ sub set_counters { sub manage_selection { my ($self, %options) = @_; - # upsEnvSensorCounts is not used but it gives the number of sensors + # FI: upsEnvSensorCounts is not used but it gives the number of sensors #my $oid_upsEnvSensorCounts = '.1.3.6.1.4.1.54661.1.1.1.2.1.0'; my $oid_upsEnvSensors = '.1.3.6.1.4.1.54661.1.1.1.2.2.1'; + # FI: the actual MIB OIDs: #my $oid_upsEnvSensorTemperature = '.1.3.6.1.4.1.54661.1.1.1.2.2.1.2'; #my $oid_upsEnvSensorHumidity = '.1.3.6.1.4.1.54661.1.1.1.2.2.1.3'; - # Each sensor will provide a temperature and a humidity ratio + # Each sensor will provide a temperature (in 100th of degrees) and a humidity percentage my $mapping = { upsEnvSensorTemperature => { oid => $oid_upsEnvSensors.'.2' }, upsEnvSensorHumidity => { oid => $oid_upsEnvSensors.'.3' } @@ -104,19 +93,28 @@ sub manage_selection { ); $self->{sensors} = {}; - foreach my $oid (keys %{$snmp_result}) { + foreach my $oid (sort(keys %{$snmp_result})) { next if ($oid !~ /^$oid_upsEnvSensors\.2\.(.*)$/); + # The index of the sensor will be used in the instance name my $sensor_index = $1; - # skip if a filter is defined, and the current sensor does not match - if (defined($self->{option_results}->{filter_id}) && $sensor_index ne '' && $sensor_index !~ $self->{option_results}->{filter_id} ) { - #FIXME: Log the skip + # Skip if a filter is defined and the current sensor does not match + if (defined($self->{option_results}->{filter_id}) && $sensor_index ne '' && $sensor_index !~ /$self->{option_results}->{filter_id}/ ) { + $self->{output}->output_add( + long_msg => "With filter-id: '$self->{option_results}->{filter_id}' - Skipping sensor '$sensor_index'.", + debug => 1 + ); next; } - my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $sensor_index); + # Get all the metrics for the current instance + my $result = $options{snmp}->map_instance( + mapping => $mapping, + results => $snmp_result, + instance => $sensor_index + ); - # the temperature is given multiplied by 100, so we have to divide it + # The temperature is given multiplied by 100, so we have to divide it by 100 # cf MIB: UNITS "0.01 degrees Centigrade" $self->{sensors}->{$sensor_index} = { display => 'Sensor '.$sensor_index, @@ -125,20 +123,20 @@ sub manage_selection { }; } + # No results is not OK if (scalar(keys %{$self->{sensors}}) <= 0) { $self->{output}->add_option_msg(short_msg => "No sensors found."); $self->{output}->option_exit(); } } - 1; __END__ -=head1 PLUGIN DESCRIPTION +=head1 MODE -Check environment counters. +Monitor temperature and humidity using the device's environment sensors. =over 8 @@ -146,7 +144,10 @@ Check environment counters. Thresholds. Can be: 'humidity' (%), 'temperature' (C). +=item B<--filter-id> + +Define which sensors should be monitored based on their IDs. This option will be treated as a regular expression. + =back =cut - diff --git a/tests/functional/snmp/hardware-ups-sputnik-snmp.robot b/tests/functional/snmp/hardware-ups-sputnik-snmp.robot index 128b33e3a5..3d5b539ae1 100644 --- a/tests/functional/snmp/hardware-ups-sputnik-snmp.robot +++ b/tests/functional/snmp/hardware-ups-sputnik-snmp.robot @@ -14,7 +14,7 @@ ${CENTREON_PLUGINS} ${CURDIR}${/}..${/}..${/}..${/}src${/}centreon_plugi ${CMD} perl ${CENTREON_PLUGINS} --plugin=hardware::ups::sputnik::snmp::plugin *** Test Cases *** -Sputnik UPS - Environment ${tc}/8 +Sputnik UPS - Environment ${tc}/9 [Tags] hardware UPS snmp ${command} Catenate ... ${CMD} @@ -23,26 +23,18 @@ Sputnik UPS - Environment ${tc}/8 ... --snmp-version=2c ... --snmp-port=2024 ... --snmp-community=hardware-ups/hardware-ups-sputnik - ${length} Get Length ${w_temperature} - IF ${length} > 0 - ${command} Catenate ${command} --warning-temperature=${w_temperature} - END - ${length} Get Length ${c_temperature} - IF ${length} > 0 - ${command} Catenate ${command} --critical-temperature=${c_temperature} - END - ${length} Get Length ${w_humidity} - IF ${length} > 0 - ${command} Catenate ${command} --warning-humidity=${w_humidity} - END - ${length} Get Length ${c_humidity} - IF ${length} > 0 - ${command} Catenate ${command} --critical-humidity=${c_humidity} - END - ${length} Get Length ${filter_id} - IF ${length} > 0 - ${command} Catenate ${command} --filter-id=${filter_id} - END + + # Append options to command + ${opt} Append Option --warning-temperature ${w_temperature} + ${command} Catenate ${command} ${opt} + ${opt} Append Option --critical-temperature ${c_temperature} + ${command} Catenate ${command} ${opt} + ${opt} Append Option --warning-humidity ${w_humidity} + ${command} Catenate ${command} ${opt} + ${opt} Append Option --critical-humidity ${c_humidity} + ${command} Catenate ${command} ${opt} + ${opt} Append Option --filter-id ${filter_id} + ${command} Catenate ${command} ${opt} ${output} Run ${command} ${output} Strip String ${output} @@ -60,4 +52,12 @@ Sputnik UPS - Environment ${tc}/8 ... 6 1 10 50 20 70 WARNING: 'Sensor 1': temperature 20.06 C, humidity 33 % | 'Sensor 1#environment.temperature.celsius'=20.06C;0:10;0:50;; 'Sensor 1#environment.humidity.percentage'=33%;0:20;0:70;0;100 ... 7 1 10 20 20 30 CRITICAL: 'Sensor 1': temperature 20.06 C, humidity 33 % | 'Sensor 1#environment.temperature.celsius'=20.06C;0:10;0:20;; 'Sensor 1#environment.humidity.percentage'=33%;0:20;0:30;0;100 ... 8 2 30 50 50 70 UNKNOWN: No sensors found. -# ... 9 1 empty empty empty empty OK: 'Sensor 1': temperature 20.06 C, humidity 33 % | 'Sensor 1#environment.temperature.celsius'=20.06C;0:30;0:50;; 'Sensor 1#environment.humidity.percentage'=33%;0:50;0:70;0;100 + ... 9 1 _empty_ _empty_ _empty_ _empty_ OK: 'Sensor 1': temperature 20.06 C, humidity 33 % | 'Sensor 1#environment.temperature.celsius'=20.06C;;;; 'Sensor 1#environment.humidity.percentage'=33%;;;0;100 + +*** Keywords *** +Append Option + [Documentation] Concatenates the first argument (option) with the second (value) after having replaced the value with "" if its content is '_empty_' + [Arguments] ${option} ${value} + ${value} Set Variable If '${value}' == '_empty_' '' ${value} + [return] ${option}=${value} +