Skip to content

Commit

Permalink
Various enhancements:
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
omercier committed Feb 5, 2024
1 parent 53c0f2b commit aefe4fa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
49 changes: 25 additions & 24 deletions src/hardware/ups/sputnik/snmp/mode/environment.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
});

Expand All @@ -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' }
];

Expand All @@ -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' }
]
}
Expand All @@ -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' }
]
}
Expand All @@ -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' }
Expand All @@ -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,
Expand All @@ -125,28 +123,31 @@ 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
=item B<--warning-*> B<--critical-*>
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
44 changes: 22 additions & 22 deletions tests/functional/snmp/hardware-ups-sputnik-snmp.robot
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand All @@ -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}

0 comments on commit aefe4fa

Please sign in to comment.