From d1f22efda6883a47a5fccffcf1f3f38c7bba4b9c Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Wed, 6 Jan 2016 15:09:58 +0100 Subject: [PATCH] + Add a template class for counters (WIP) --- .../common/airespace/snmp/mode/apstatus.pm | 248 ++++---------- centreon/plugins/templates/counter.pm | 313 ++++++++++++++++++ 2 files changed, 380 insertions(+), 181 deletions(-) create mode 100644 centreon/plugins/templates/counter.pm diff --git a/centreon/common/airespace/snmp/mode/apstatus.pm b/centreon/common/airespace/snmp/mode/apstatus.pm index bc1c60839..77e956f72 100644 --- a/centreon/common/airespace/snmp/mode/apstatus.pm +++ b/centreon/common/airespace/snmp/mode/apstatus.pm @@ -20,58 +20,13 @@ package centreon::common::airespace::snmp::mode::apstatus; -use base qw(centreon::plugins::mode); +use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::values; my $instance_mode; -my $maps_counters = { - ap => { - '000_status' => { threshold => 0, - set => { - key_values => [ { name => 'opstatus' }, { name => 'admstatus' }, { name => 'display' } ], - threshold => 0, - closure_custom_calc => \&custom_status_calc, - closure_custom_output => \&custom_status_output, - closure_custom_perfdata => sub { return 0; }, - closure_custom_threshold_check => \&custom_threshold_output, - } - }, - }, - global => { - '000_total' => { set => { - key_values => [ { name => 'total' } ], - output_template => 'Total ap : %s', - perfdatas => [ - { label => 'total', value => 'total_absolute', template => '%s', - min => 0 }, - ], - } - }, - '001_total-associated' => { set => { - key_values => [ { name => 'associated' } ], - output_template => 'Total ap associated : %s', - perfdatas => [ - { label => 'total_associated', value => 'associated_absolute', template => '%s', - min => 0 }, - ], - } - }, - '002_total-disassociating' => { set => { - key_values => [ { name => 'disassociating' } ], - output_template => 'Total ap disassociating : %s', - perfdatas => [ - { label => 'total_disassociating', value => 'disassociating_absolute', template => '%s', - min => 0 }, - ], - } - }, - } -}; - sub custom_threshold_output { my ($self, %options) = @_; my $status = 'ok'; @@ -118,6 +73,55 @@ sub custom_status_calc { return 0; } +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_init => 'skip_global', }, + { name => 'ap', type => 1, cb_prefix_output => 'prefix_ap_output', message_multiple => 'All AP status are ok' } + ]; + $self->{maps_counters}->{global} = [ + { label => 'total', set => { + key_values => [ { name => 'total' } ], + output_template => 'Total ap : %s', + perfdatas => [ + { label => 'total', value => 'total_absolute', template => '%s', + min => 0 }, + ], + } + }, + { label => 'total-associated', set => { + key_values => [ { name => 'associated' } ], + output_template => 'Total ap associated : %s', + perfdatas => [ + { label => 'total_associated', value => 'associated_absolute', template => '%s', + min => 0 }, + ], + } + }, + { label => 'total-disassociating', set => { + key_values => [ { name => 'disassociating' } ], + output_template => 'Total ap disassociating : %s', + perfdatas => [ + { label => 'total_disassociating', value => 'disassociating_absolute', template => '%s', + min => 0 }, + ], + } + }, + ]; + + $self->{maps_counters}->{ap} = [ + { label => 'status', threshold => 0, set => { + key_values => [ { name => 'opstatus' }, { name => 'admstatus' }, { name => 'display' } ], + closure_custom_calc => $self->can('custom_status_calc'), + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_threshold_output'), + } + }, + ]; +} + sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options); @@ -129,22 +133,7 @@ sub new { "filter-name:s" => { name => 'filter_name' }, "warning-status:s" => { name => 'warning_status', default => '' }, "critical-status:s" => { name => 'critical_status', default => '%{admstatus} eq "enable" and %{opstatus} !~ /associated|downloading/' }, - }); - - foreach my $key (('global', 'ap')) { - foreach (keys %{$maps_counters->{$key}}) { - my ($id, $name) = split /_/; - if (!defined($maps_counters->{$key}->{$_}->{threshold}) || $maps_counters->{$key}->{$_}->{threshold} != 0) { - $options{options}->add_options(arguments => { - 'warning-' . $name . ':s' => { name => 'warning-' . $name }, - 'critical-' . $name . ':s' => { name => 'critical-' . $name }, - }); - } - $maps_counters->{$key}->{$_}->{obj} = centreon::plugins::values->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $name); - $maps_counters->{$key}->{$_}->{obj}->set(%{$maps_counters->{$key}->{$_}->{set}}); - } - } + }); return $self; } @@ -152,124 +141,21 @@ sub new { sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); - - foreach my $key (('global', 'ap')) { - foreach (keys %{$maps_counters->{$key}}) { - $maps_counters->{$key}->{$_}->{obj}->init(option_results => $self->{option_results}); - } - } $instance_mode = $self; $self->change_macros(); } -sub run_instance { +sub skip_global { my ($self, %options) = @_; - if ($self->{multiple} == 1) { - $self->{output}->output_add(severity => 'OK', - short_msg => 'All AP status are ok'); - } - - foreach my $id (sort keys %{$self->{ap_selected}}) { - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits = (); - foreach (sort keys %{$maps_counters->{ap}}) { - my $obj = $maps_counters->{ap}->{$_}->{obj}; - $obj->set(instance => $id); - - my ($value_check) = $obj->execute(values => $self->{ap_selected}->{$id}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $obj->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $obj->threshold_check(); - push @exits, $exit2; - - my $output = $obj->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $obj->perfdata(extra_instance => $self->{multiple}); - } - - $self->{output}->output_add(long_msg => "AP '" . $self->{ap_selected}->{$id}->{display} . "' $long_msg"); - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "AP '" . $self->{ap_selected}->{$id}->{display} . "' $short_msg" - ); - } - - if ($self->{multiple} == 0) { - $self->{output}->output_add(short_msg => "AP '" . $self->{ap_selected}->{$id}->{display} . "' $long_msg"); - } - } + scalar(keys %{$self->{ap}}) > 0 ? return(1) : return(0); } -sub run_global { +sub prefix_ap_output { my ($self, %options) = @_; - my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); - my @exits; - foreach (sort keys %{$maps_counters->{global}}) { - my $obj = $maps_counters->{global}->{$_}->{obj}; - - $obj->set(instance => 'global'); - - my ($value_check) = $obj->execute(values => $self->{global}); - - if ($value_check != 0) { - $long_msg .= $long_msg_append . $obj->output_error(); - $long_msg_append = ', '; - next; - } - my $exit2 = $obj->threshold_check(); - push @exits, $exit2; - - my $output = $obj->output(); - $long_msg .= $long_msg_append . $output; - $long_msg_append = ', '; - - if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { - $short_msg .= $short_msg_append . $output; - $short_msg_append = ', '; - } - - $obj->perfdata(); - } - - my $exit = $self->{output}->get_most_critical(status => [ @exits ]); - if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { - $self->{output}->output_add(severity => $exit, - short_msg => "$short_msg" - ); - } else { - $self->{output}->output_add(short_msg => "$long_msg"); - } -} - -sub run { - my ($self, %options) = @_; - $self->{snmp} = $options{snmp}; - - $self->manage_selection(); - - if ($self->{multiple} == 1) { - $self->run_global(); - } - - $self->run_instance(); - - $self->{output}->display(); - $self->{output}->exit(); + return "AP '" . $options{instance_value}->{display} . "' "; } sub change_macros { @@ -305,9 +191,9 @@ my $oid_agentInventoryMachineModel = '.1.3.6.1.4.1.14179.1.1.1.3'; sub manage_selection { my ($self, %options) = @_; - $self->{ap_selected} = {}; + $self->{ap} = {}; $self->{global} = { total => 0, associated => 0, disassociating => 0, downloading => 0 }; - $self->{results} = $self->{snmp}->get_multiple_table(oids => [ { oid => $oid_agentInventoryMachineModel }, + $self->{results} = $options{snmp}->get_multiple_table(oids => [ { oid => $oid_agentInventoryMachineModel }, { oid => $mapping->{bsnAPName}->{oid} }, { oid => $mapping2->{bsnAPOperationStatus}->{oid} }, { oid => $mapping3->{bsnAPAdminStatus}->{oid} }, @@ -317,9 +203,9 @@ sub manage_selection { foreach my $oid (keys %{$self->{results}->{ $mapping->{bsnAPName}->{oid} }}) { $oid =~ /^$mapping->{bsnAPName}->{oid}\.(.*)$/; my $instance = $1; - my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{ $mapping->{bsnAPName}->{oid} }, instance => $instance); - my $result2 = $self->{snmp}->map_instance(mapping => $mapping2, results => $self->{results}->{ $mapping2->{bsnAPOperationStatus}->{oid} }, instance => $instance); - my $result3 = $self->{snmp}->map_instance(mapping => $mapping3, results => $self->{results}->{ $mapping3->{bsnAPAdminStatus}->{oid} }, instance => $instance); + my $result = $options{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{ $mapping->{bsnAPName}->{oid} }, instance => $instance); + my $result2 = $options{snmp}->map_instance(mapping => $mapping2, results => $self->{results}->{ $mapping2->{bsnAPOperationStatus}->{oid} }, instance => $instance); + my $result3 = $options{snmp}->map_instance(mapping => $mapping3, results => $self->{results}->{ $mapping3->{bsnAPAdminStatus}->{oid} }, instance => $instance); if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && $result->{bsnAPName} !~ /$self->{option_results}->{filter_name}/) { $self->{output}->output_add(long_msg => "Skipping '" . $result->{bsnAPName} . "': no matching filter.", debug => 1); @@ -329,19 +215,14 @@ sub manage_selection { $self->{global}->{total}++; $self->{global}->{$result2->{bsnAPOperationStatus}}++; - $self->{ap_selected}->{$instance} = { display => $result->{bsnAPName}, + $self->{ap}->{$instance} = { display => $result->{bsnAPName}, opstatus => $result2->{bsnAPOperationStatus}, admstatus => $result3->{bsnAPAdminStatus}}; } - if (scalar(keys %{$self->{ap_selected}}) <= 0) { + if (scalar(keys %{$self->{ap}}) <= 0) { $self->{output}->output_add(severity => 'OK', short_msg => 'No AP associated (can be: slave wireless controller or your filter)'); } - - $self->{multiple} = 1; - if (scalar(keys %{$self->{ap_selected}}) <= 1) { - $self->{multiple} = 0; - } } 1; @@ -354,6 +235,11 @@ Check AP status. =over 8 +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example to check SSL connections only : --filter-counters='^total-disassociating|total-associated$' + =item B<--filter-name> Filter AP name (can be a regexp). diff --git a/centreon/plugins/templates/counter.pm b/centreon/plugins/templates/counter.pm new file mode 100644 index 000000000..e1b8d6ad0 --- /dev/null +++ b/centreon/plugins/templates/counter.pm @@ -0,0 +1,313 @@ +# +# Copyright 2015 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 centreon::plugins::templates::counter; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::values; +use centreon::plugins::misc; + +sub set_counters { + my ($self, %options) = @_; + + if (!defined($self->{maps_counters})) { + $self->{maps_counters} = {}; + } + + $self->{maps_counters_type} = []; + + # 0 = mode total + # 1 = mode instances + #push @{$self->{maps_counters_type}}, { + # name => 'global', type => 0, message_separator => ', ', cb_prefix_output => undef, cb_init => undef, + #}; + + #$self->{maps_counters}->{global} = [ + # { label => 'client', set => { + # key_values => [ { name => 'client' } ], + # output_template => 'Current client connections : %s', + # perfdatas => [ + # { label => 'Client', value => 'client_absolute', template => '%s', + # min => 0, unit => 'con' }, + # ], + # } + # }, + #]; + + # Example for instances + #push @{$self->{maps_counters_type}}, { + # name => 'cpu', type => 1, message_separator => ', ', cb_prefix_output => undef, cb_init => undef, + # message_multiple => 'All CPU usages are ok', + #}; +} + +sub call_object_callback { + my ($self, %options) = @_; + + if (defined($options{method_name})) { + my $method = $self->can($options{method_name}); + if ($method) { + return $self->$method(%options); + } + } + + return undef; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "filter-counters:s" => { name => 'filter_counters' }, + }); + $self->{statefile_value} = undef; + if (defined($options{statefile}) && $options{statefile}) { + centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'centreon::plugins::statefile', + error_msg => "Cannot load module 'centreon::plugins::statefile'."); + $self->{statefile_value} = centreon::plugins::statefile->new(%options); + } + + $self->{maps_counters} = {} if (!defined($self->{maps_counters})); + $self->set_counters(); + + foreach my $key (keys %{$self->{maps_counters}}) { + foreach (@{$self->{maps_counters}->{$key}}) { + if (!defined($_->{threshold}) || $_->{threshold} != 0) { + $options{options}->add_options(arguments => { + 'warning-' . $_->{label} . ':s' => { name => 'warning-' . $_->{label} }, + 'critical-' . $_->{label} . ':s' => { name => 'critical-' . $_->{label} }, + }); + } + $_->{obj} = centreon::plugins::values->new(statefile => $self->{statefile_value}, + output => $self->{output}, perfdata => $self->{perfdata}, + label => $_->{label}); + $_->{obj}->set(%{$_->{set}}); + } + } + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + foreach my $key (keys %{$self->{maps_counters}}) { + foreach (@{$self->{maps_counters}->{$key}}) { + $_->{obj}->init(option_results => $self->{option_results}); + } + } + + if (defined($self->{statefile_value})) { + $self->{statefile_value}->check_options(%options); + } +} + +sub run_global { + my ($self, %options) = @_; + + return undef if (defined($options{config}->{cb_init}) && $self->call_object_callback(method_name => $options{config}->{cb_init}) == 1); + + my $message_separator = defined($options{config}->{message_separator}) ? + $options{config}->{message_separator}: ', '; + my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); + my @exits; + foreach (@{$self->{maps_counters}->{$options{config}->{name}}}) { + my $obj = $_->{obj}; + + next if (defined($self->{option_results}->{filter_counters}) && $self->{option_results}->{filter_counters} ne '' && + $_->{name} !~ /$self->{option_results}->{filter_counters}/); + + $obj->set(instance => $options{config}->{name}); + + my ($value_check) = $obj->execute(new_datas => $self->{new_datas}, values => $self->{$options{config}->{name}}); + + if ($value_check != 0) { + $long_msg .= $long_msg_append . $obj->output_error(); + $long_msg_append = $message_separator; + next; + } + my $exit2 = $obj->threshold_check(); + push @exits, $exit2; + + my $output = $obj->output(); + $long_msg .= $long_msg_append . $output; + $long_msg_append = $message_separator; + + if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { + $short_msg .= $short_msg_append . $output; + $short_msg_append = $message_separator; + } + + $obj->perfdata(); + } + + my $prefix_output; + $prefix_output = $self->call_object_callback(method_name => $options{config}->{cb_prefix_output}) + if (defined($options{config}->{cb_prefix_output})); + $prefix_output = '' if (defined($prefix_output)); + + my $exit = $self->{output}->get_most_critical(status => [ @exits ]); + if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { + $self->{output}->output_add(severity => $exit, + short_msg => "${prefix_output}$short_msg" + ); + } else { + $self->{output}->output_add(short_msg => "${prefix_output}$long_msg"); + } +} + +sub run_instances { + my ($self, %options) = @_; + + return undef if (defined($options{config}->{cb_init}) && $self->call_object_callback(method_name => $options{config}->{cb_init}) == 1); + + $self->{multiple} = 1; + if (scalar(keys %{$self->{$options{config}->{name}}}) == 1) { + $self->{multiple} = 0; + } + + if ($self->{multiple} == 1) { + $self->{output}->output_add(severity => 'OK', + short_msg => $options{config}->{message_multiple}); + } + + my $message_separator = defined($options{config}->{message_separator}) ? + $options{config}->{message_separator}: ', '; + foreach my $id (sort keys %{$options{config}->{name}}) { + my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); + my @exits = (); + foreach (@{$self->{maps_counters}->{$options{config}->{name}}}) { + my $obj = $_->{obj}; + $obj->set(instance => $id); + + my ($value_check) = $obj->execute(new_datas => $self->{new_datas}, + values => $self->{$options{config}->{name}}->{$id}); + + if ($value_check != 0) { + $long_msg .= $long_msg_append . $obj->output_error(); + $long_msg_append = $message_separator; + next; + } + my $exit2 = $obj->threshold_check(); + push @exits, $exit2; + + my $output = $obj->output(); + $long_msg .= $long_msg_append . $output; + $long_msg_append = $message_separator; + + if (!$self->{output}->is_status(litteral => 1, value => $exit2, compare => 'ok')) { + $short_msg .= $short_msg_append . $output; + $short_msg_append = $message_separator; + } + + $obj->perfdata(extra_instance => $self->{multiple}); + } + + my $prefix_output; + $prefix_output = $self->call_object_callback(method_name => $options{config}->{cb_prefix_output}, instance_value => $self->{$options{config}->{name}}->{$id}) + if (defined($options{config}->{cb_prefix_output})); + $prefix_output = '' if (defined($prefix_output)); + + $self->{output}->output_add(long_msg => "${prefix_output}$long_msg"); + my $exit = $self->{output}->get_most_critical(status => [ @exits ]); + if (!$self->{output}->is_status(litteral => 1, value => $exit, compare => 'ok')) { + $self->{output}->output_add(severity => $exit, + short_msg => "${prefix_output}$short_msg" + ); + } + + if ($self->{multiple} == 0) { + $self->{output}->output_add(short_msg => "${prefix_output}$long_msg"); + } + } +} + +sub run { + my ($self, %options) = @_; + + $self->manage_selection(%options); + + $self->{new_datas} = undef; + if (defined($self->{statefile_value})) { + $self->{new_datas} = {}; + $self->{statefile_value}->read(statefile => $self->{cache_name}) if (defined($self->{cache_name})); + $self->{new_datas}->{last_timestamp} = time(); + } + + foreach my $entry (@{$self->{maps_counters_type}}) { + if ($_->{type} == 0) { + $self->run_global(config => $entry); + } elsif ($_->{type} == 1) { + $self->run_instances(config => $entry); + } + } + + if (defined($self->{statefile_value})) { + $self->{statefile_value}->write(data => $self->{new_datas}); + } + $self->{output}->display(); + $self->{output}->exit(); +} + +sub manage_selection { + my ($self, %options) = @_; + + # example for snmp + #use Digest::MD5 qw(md5_hex); + #$self->{cache_name} = "choose_name_" . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode} . '_' . + # (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); +} + +1; + +__END__ + +=head1 MODE + +Default template for counters. Should be extended. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example to check SSL connections only : --filter-counters='^xxxx|yyyy$' + +=item B<--warning-*> + +Threshold warning. +Can be: 'xxx', 'xxx'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'xxx', 'xxx'. + +=back + +=cut \ No newline at end of file