-
Notifications
You must be signed in to change notification settings - Fork 303
new mode "output-banks" for plugin APC-ATS #6206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rmorandell-pgum
wants to merge
4
commits into
centreon:develop
Choose a base branch
from
i-Vertix:hardware-ats-apc-outputbank
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
11372a5
new mode "output-banks"
rmorandell-pgum a245069
Merge branch 'centreon:develop' into hardware-ats-apc-outputbank
rmorandell-pgum 4bd5115
add --include-name & --exclude-name to help
rmorandell-pgum 96c6eec
refactor help filter-counter
rmorandell-pgum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,392 @@ | ||
| # | ||
| # Copyright 2024 Centreon (http://www.centreon.com/) | ||
| # | ||
| # Centreon is a full-fledged industry-strength solution that meets | ||
| # the needs in IT infrastructure and application monitoring for | ||
| # service performance. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| package hardware::ats::apc::snmp::mode::outputbanks; | ||
|
|
||
| use base qw(centreon::plugins::templates::counter); | ||
|
|
||
| use strict; | ||
| use warnings; | ||
| use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); | ||
| use centreon::plugins::constants qw(:counters :values); | ||
| use centreon::plugins::misc qw/is_excluded/; | ||
|
|
||
| sub custom_status_output { | ||
| my ($self, %options) = @_; | ||
| my $msg = 'Status : ' . $self->{result_values}->{bank_state}; | ||
|
|
||
| return $msg; | ||
| } | ||
|
|
||
| sub custom_overall_status_output { | ||
| my ($self, %options) = @_; | ||
| my $msg = 'Overall status : ' . $self->{result_values}->{overall_state}; | ||
|
|
||
| return $msg; | ||
| } | ||
|
|
||
| sub prefix_bank_output { | ||
| my ($self, %options) = @_; | ||
|
|
||
| return "Output bank '" . $options{instance_value}->{display} . "' "; | ||
| } | ||
|
|
||
| sub bank_long_output { | ||
| my ($self, %options) = @_; | ||
|
|
||
| return sprintf( | ||
| "checking bank '%s'", | ||
| $options{instance_value}->{display} | ||
| ); | ||
| } | ||
|
|
||
| sub set_counters { | ||
| my ($self, %options) = @_; | ||
|
|
||
| $self->{maps_counters_type} = [ | ||
| { name => 'global', type => COUNTER_TYPE_GLOBAL }, | ||
| { | ||
| name => 'bank', | ||
| type => COUNTER_TYPE_INSTANCE, | ||
| cb_prefix_output => 'prefix_bank_output', | ||
| cb_long_output => 'bank_long_output', | ||
| indent_long_output => ' ', | ||
| message_multiple => 'All banks are ok', | ||
| skipped_code => { NO_VALUE() => 1 } | ||
| } | ||
| ]; | ||
|
|
||
| $self->{maps_counters}->{global} = [ | ||
| { | ||
| label => 'overall-status', | ||
| type => COUNTER_KIND_TEXT, | ||
| warning_default => '%{overall_state} =~ /nearoverload/', | ||
| critical_default => '%{overall_state} =~ /lowload|overload/', | ||
| set => | ||
| { | ||
| key_values => [ | ||
| { name => 'overall_state' }, | ||
| { name => 'display' } | ||
| ], | ||
| closure_custom_output => $self->can('custom_overall_status_output'), | ||
| closure_custom_perfdata => sub {return 0;}, | ||
| closure_custom_threshold_check => \&catalog_status_threshold_ng | ||
| } | ||
| }, | ||
| { label => 'total-current', nlabel => 'bank.current.ampere', set => { | ||
| key_values => [ { name => 'bank_current' }, { name => 'display' } ], | ||
| output_template => 'Current : %.2f A', | ||
| perfdatas => [ | ||
| { | ||
| label => 'current', | ||
| value => 'bank_current', | ||
| template => '%s', | ||
| unit => 'A', | ||
| label_extra_instance => 1, | ||
| instance_use => 'display' | ||
| } | ||
| ], | ||
| } | ||
| }, | ||
| { label => 'total-power', nlabel => 'bank.power.watt',set => { | ||
| key_values => [ { name => 'bank_power' }, { name => 'display' } ], | ||
| output_template => 'Power : %.2f W', | ||
| perfdatas => [ | ||
| { | ||
| label => 'power', | ||
| value => 'bank_power', | ||
| template => '%s', | ||
| unit => 'W', | ||
| label_extra_instance => 1, | ||
| instance_use => 'display' | ||
| } | ||
| ], | ||
| } | ||
| }, | ||
| { label => 'total-load-capacity', nlabel => 'bank.load.capacity.percent', set => { | ||
| key_values => [ { name => 'bank_load' }, { name => 'display' } ], | ||
| output_template => 'Load capacity : %.2f %%', | ||
| perfdatas => [ | ||
| { | ||
| label => 'load_capacity', | ||
| value => 'bank_load', | ||
| template => '%s', | ||
| unit => '%', | ||
| label_extra_instance => 1, | ||
| instance_use => 'display', | ||
| min => 0, | ||
| max => 100 | ||
| } | ||
| ], | ||
| } | ||
| } | ||
| ]; | ||
|
|
||
| $self->{maps_counters}->{bank} = [ | ||
| { | ||
| label => 'bank-status', | ||
| type => COUNTER_KIND_TEXT, | ||
| warning_default => '%{bank_state} =~ /nearoverload/', | ||
| critical_default => '%{bank_state} =~ /lowload|overload/', | ||
| set => | ||
| { | ||
| key_values => [ | ||
| { name => 'bank_state' }, | ||
| { name => 'display' } | ||
| ], | ||
| closure_custom_output => $self->can('custom_status_output'), | ||
| closure_custom_perfdata => sub {return 0;}, | ||
| closure_custom_threshold_check => \&catalog_status_threshold_ng | ||
| } | ||
| }, | ||
| { label => 'bank-voltage', nlabel => 'bank.voltage.volt', display_ok => 0, set => { | ||
| key_values => [ { name => 'bank_voltage' }, { name => 'display' } ], | ||
| output_template => 'Voltage : %.2f V', | ||
| perfdatas => [ | ||
| { | ||
| label => 'voltage', | ||
| value => 'bank_voltage', | ||
| template => '%s', | ||
| unit => 'V', | ||
| label_extra_instance => 1, | ||
| instance_use => 'display' | ||
| } | ||
| ], | ||
| } | ||
| }, | ||
| { label => 'bank-current', nlabel => 'bank.current.ampere', display_ok => 0, set => { | ||
| key_values => [ { name => 'bank_current' }, { name => 'display' } ], | ||
| output_template => 'Current : %.2f A', | ||
| perfdatas => [ | ||
| { | ||
| label => 'current', | ||
| value => 'bank_current', | ||
| template => '%s', | ||
| unit => 'A', | ||
| label_extra_instance => 1, | ||
| instance_use => 'display' | ||
| } | ||
| ], | ||
| } | ||
| }, | ||
| { label => 'bank-power', nlabel => 'bank.power.watt', display_ok => 0, set => { | ||
| key_values => [ { name => 'bank_power' }, { name => 'display' } ], | ||
| output_template => 'Power : %.2f W', | ||
| perfdatas => [ | ||
| { | ||
| label => 'power', | ||
| value => 'bank_power', | ||
| template => '%s', | ||
| unit => 'W', | ||
| label_extra_instance => 1, | ||
| instance_use => 'display' | ||
| } | ||
| ], | ||
| } | ||
| }, | ||
| { label => 'bank-load-capacity', nlabel => 'bank.load.capacity.percent', display_ok => 0, set => { | ||
| key_values => [ { name => 'bank_load' }, { name => 'display' } ], | ||
| output_template => 'Load capacity : %.2f %%', | ||
| perfdatas => [ | ||
| { | ||
| label => 'load_capacity', | ||
| value => 'bank_load', | ||
| template => '%s', | ||
| unit => '%', | ||
| label_extra_instance => 1, | ||
| instance_use => 'display', | ||
| min => 0, | ||
| max => 100 | ||
| } | ||
| ], | ||
| } | ||
| } | ||
| ]; | ||
| } | ||
|
|
||
| sub new { | ||
| my ($class, %options) = @_; | ||
| my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); | ||
| bless $self, $class; | ||
|
|
||
| $options{options}->add_options(arguments => | ||
| { | ||
| 'include-name:s' => { name => 'include_name' }, | ||
| 'exclude-name:s' => { name => 'exclude_name' } | ||
| }); | ||
|
|
||
| return $self; | ||
| } | ||
|
|
||
| sub check_options { | ||
| my ($self, %options) = @_; | ||
| $self->SUPER::check_options(%options); | ||
|
|
||
| $self->change_macros(macros => [ 'warning_status', 'critical_status' ]); | ||
| } | ||
|
|
||
| my $map_state = { 1 => 'normal', 2 => 'lowload', 3 => 'nearoverload', 4 => 'overload' }; | ||
| my $map_bank = { 1 => 'total', 2 => 'bank1', 3 => 'bank2' }; | ||
|
|
||
| my $mapping = { | ||
| atsOutputBank => { oid => '.1.3.6.1.4.1.318.1.1.8.5.4.5.1.3', map => $map_bank }, | ||
| atsOutputPhase => { oid => '.1.3.6.1.4.1.318.1.1.8.5.4.5.1.2' }, | ||
| atsOutputBankOutputVoltage => { oid => '.1.3.6.1.4.1.318.1.1.8.5.4.5.1.6' }, | ||
| atsOutputBankCurrent => { oid => '.1.3.6.1.4.1.318.1.1.8.5.4.5.1.4' }, | ||
| atsOutputBankPercentPower => { oid => '.1.3.6.1.4.1.318.1.1.8.5.4.5.1.18' }, | ||
| atsOutputBankPower => { oid => '.1.3.6.1.4.1.318.1.1.8.5.4.5.1.15' }, | ||
| atsOutputBankState => { oid => '.1.3.6.1.4.1.318.1.1.8.5.4.5.1.5', map => $map_state }, | ||
| }; | ||
|
|
||
| my $oid_atsOutputBankEntry = '.1.3.6.1.4.1.318.1.1.8.5.4.5.1'; | ||
|
|
||
| sub manage_selection { | ||
| my ($self, %options) = @_; | ||
|
|
||
| my $snmp_result = $options{snmp}->get_table(oid => $oid_atsOutputBankEntry, nothing_quit => 1); | ||
|
|
||
| foreach my $oid (sort keys %$snmp_result) { | ||
| next if ($oid !~ /^$mapping->{atsOutputBank}->{oid}\.(.*)$/); | ||
|
|
||
| my $instance = $1; | ||
| my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance); | ||
|
|
||
| next if $result->{atsOutputBank} ne 'total' && is_excluded( | ||
| $result->{atsOutputBank}, | ||
| $self->{option_results}->{include_name}, | ||
| $self->{option_results}->{exclude_name} | ||
| ); | ||
|
|
||
| my $target = {}; | ||
| $target->{display} = $result->{atsOutputBank}; | ||
| $target->{bank_phase} = $result->{atsOutputPhase}; | ||
| $target->{bank_voltage} = $result->{atsOutputBankOutputVoltage}; | ||
| $target->{bank_current} = $result->{atsOutputBankCurrent} * 0.1; | ||
| $target->{bank_load} = $result->{atsOutputBankPercentPower}; | ||
| $target->{bank_power} = $result->{atsOutputBankPower}; | ||
|
|
||
| if($result->{atsOutputBank} eq 'total') { | ||
| $self->{global} = $target; | ||
| $self->{global}->{overall_state} = $result->{atsOutputBankState}; | ||
| } else { | ||
| $self->{bank}->{$instance} = $target; | ||
| $self->{bank}->{$instance}->{bank_state} = $result->{atsOutputBankState}; | ||
| } | ||
| } | ||
|
|
||
| if (!defined($self->{global}) && scalar(keys %{$self->{bank}}) <= 0) { | ||
| $self->{output}->option_exit(short_msg => "No banks matching with filter found."); | ||
| } | ||
| } | ||
|
|
||
| 1; | ||
|
|
||
| __END__ | ||
|
|
||
| =head1 MODE | ||
|
|
||
| Check output bank metrics. | ||
|
|
||
| =over 8 | ||
|
|
||
| =item B<--filter-counters> | ||
|
|
||
| Only display some counters (regexp can be used). | ||
| Example: --filter-counters='^power$' | ||
|
|
||
| =item B<--warning-overall-status> | ||
|
|
||
| Define the conditions to match for the status to be WARNING (default: '%{overall_state} =~ /nearoverload/'). | ||
| You can use the following variables: C<%{overall_state}>, C<%{display}> | ||
|
|
||
| =item B<--critical-overall-status> | ||
|
|
||
| Define the conditions to match for the status to be CRITICAL (default: '%{overall_state} =~ /lowload|overload/'). | ||
| You can use the following variables: C<%{overall_state}>, C<%{display}> | ||
|
|
||
| =item B<--warning-bank-status> | ||
|
|
||
| Define the conditions to match for the status to be WARNING (default: '%{bank_state} =~ /nearoverload/'). | ||
| You can use the following variables: C<%{bank_state}>, C<%{display}> | ||
|
|
||
| =item B<--critical-bank-status> | ||
|
|
||
| Define the conditions to match for the status to be CRITICAL (default: '%{bank_state} =~ /lowload|overload/'). | ||
| You can use the following variables: C<%{bank_state}>, C<%{display}> | ||
|
|
||
| =item B<--warning-total-current> | ||
|
|
||
| Warning threshold. (A) | ||
|
|
||
| =item B<--critical-total-current> | ||
|
|
||
| Critical threshold. (A) | ||
|
|
||
| =item B<--warning-total-power> | ||
|
|
||
| Warning threshold. (W) | ||
|
|
||
| =item B<--critical-total-power> | ||
|
|
||
| Critical threshold. (W) | ||
|
|
||
| =item B<--warning-total-load-capacity> | ||
|
|
||
| Warning threshold. (%) | ||
|
|
||
| =item B<--critical-total-load-capacity> | ||
|
|
||
| Critical threshold. (%) | ||
|
|
||
| =item B<--warning-bank-voltage> | ||
|
|
||
| Warning threshold. (V) | ||
|
|
||
| =item B<--critical-bank-voltage> | ||
|
|
||
| Critical threshold. (V) | ||
|
|
||
| =item B<--warning-bank-current> | ||
|
|
||
| Warning threshold. (A) | ||
|
|
||
| =item B<--critical-bank-current> | ||
|
|
||
| Critical threshold. (A) | ||
|
|
||
| =item B<--warning-bank-power> | ||
|
|
||
| Warning threshold. (W) | ||
|
|
||
| =item B<--critical-bank-power> | ||
|
|
||
| Critical threshold. (W) | ||
|
|
||
| =item B<--warning-bank-load-capacity> | ||
|
|
||
| Warning threshold. (%) | ||
|
|
||
| =item B<--critical-bank-load-capacity> | ||
|
|
||
| Critical threshold. (%) | ||
|
|
||
| =back | ||
|
|
||
| =cut | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check uses
keys %{$self->{bank}}even when$self->{bank}may be undefined, which can crash instead of reaching the "No banks matching" path.Details
✨ AI Reasoning
The final emptiness check assumes two containers may both be empty, but one of them is not initialized before being dereferenced. If no per-bank entry is stored during selection and the overall entry is also missing, evaluating the hash key count on an undefined reference can fail before the intended "No banks matching" message is emitted.
Reply
@AikidoSec feedback: [FEEDBACK]to get better review comments in the future.Reply
@AikidoSec ignore: [REASON]to ignore this issue.More info