From 9210c5edb552445454110ad62eb28f3237dd2a57 Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Wed, 18 Mar 2015 13:57:09 +1100 Subject: [PATCH] Refs #6207 WIP redback --- network/redback/snmp/mode/cpu.pm | 16 +- network/redback/snmp/mode/disk.pm | 265 ++++++++++++++++++++++++++ network/redback/snmp/mode/hardware.pm | 2 +- network/redback/snmp/mode/memory.pm | 10 +- network/redback/snmp/plugin.pm | 4 + 5 files changed, 281 insertions(+), 16 deletions(-) create mode 100644 network/redback/snmp/mode/disk.pm diff --git a/network/redback/snmp/mode/cpu.pm b/network/redback/snmp/mode/cpu.pm index b2c22be1d..7b12efc9b 100644 --- a/network/redback/snmp/mode/cpu.pm +++ b/network/redback/snmp/mode/cpu.pm @@ -126,25 +126,23 @@ sub run { threshold => [ { label => 'crit5m', exit_litteral => 'critical' }, { label => 'warn5m', exit_litteral => 'warning' } ]); my $exit = $self->{output}->get_most_critical(status => [ $exit1, $exit2, $exit3 ]); - $self->{output}->output_add(long_msg => sprintf("CPU '%s': %.2f%% (5sec), %.2f%% (1min), %.2f%% (5min)", $instance, + $self->{output}->output_add(long_msg => sprintf("CPU: %.2f%% (5sec), %.2f%% (1min), %.2f%% (5min)", $cpu5sec, $cpu1min, $cpu5min)); - if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("CPU '%s': %.2f%% (5sec), %.2f%% (1min), %.2f%% (5min)", $instance, - $cpu5sec, $cpu1min, $cpu5min)); - } + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("CPU: %.2f%% (5sec), %.2f%% (1min), %.2f%% (5min)", + $cpu5sec, $cpu1min, $cpu5min)); - $self->{output}->perfdata_add(label => "cpu_" . $instance . "_5s", + $self->{output}->perfdata_add(label => "cpu_5s", value => $cpu5sec, warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5s'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5s'), min => 0, max => 100); - $self->{output}->perfdata_add(label => "cpu_" . $instance . "_1m", + $self->{output}->perfdata_add(label => "cpu_1m", value => $cpu1min, warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn1m'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit1m'), min => 0, max => 100); - $self->{output}->perfdata_add(label => "cpu_" . $instance . "_5m", + $self->{output}->perfdata_add(label => "cpu_5m", value => $cpu5min, warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5m'), critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5m'), diff --git a/network/redback/snmp/mode/disk.pm b/network/redback/snmp/mode/disk.pm new file mode 100644 index 000000000..d9873b167 --- /dev/null +++ b/network/redback/snmp/mode/disk.pm @@ -0,0 +1,265 @@ +################################################################################ +# Copyright 2005-2014 MERETHIS +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give MERETHIS +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of MERETHIS choice, provided that +# MERETHIS also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Quentin Garnier +# +#################################################################################### + +package network::redback::snmp::mode::disk; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::values; + +my $maps_counters = { + '0_usage' => { class => 'centreon::plugins::values', obj => undef, + set => { + key_values => [ + { name => 'display' }, { name => 'free' }, { name => 'used' }, + ], + closure_custom_calc => \&custom_usage_calc, + closure_custom_output => \&custom_usage_output, + closure_custom_perfdata => \&custom_usage_perfdata, + closure_custom_threshold_check => \&custom_usage_threshold, + } + }, +}; + +sub custom_usage_perfdata { + my ($self, %options) = @_; + + my $extra_label = ''; + if (!defined($options{extra_instance}) || $options{extra_instance} != 0) { + $extra_label .= '_' . $self->{result_values}->{display}; + } + $self->{output}->perfdata_add(label => 'used' . $extra_label, unit => 'B', + value => $self->{result_values}->{used}, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1), + min => 0, max => $self->{result_values}->{total}); +} + +sub custom_usage_threshold { + my ($self, %options) = @_; + + my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{prct_used}, threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{label}, exit_litteral => 'warning' } ]); + return $exit; +} + +sub custom_usage_output { + my ($self, %options) = @_; + + my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); + my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); + + my $msg = sprintf("Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", + $total_size_value . " " . $total_size_unit, + $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, + $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}); + return $msg; +} + +sub custom_usage_calc { + my ($self, %options) = @_; + + $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; + $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'}; + $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'} * $options{new_datas}->{$self->{instance} . '_total'} / 100; + $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used}; + $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total}; + $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total}; + return 0; +} + +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-name" => { name => 'filter_name' }, + }); + + foreach (keys %{$maps_counters}) { + my ($id, $name) = split /_/; + if (!defined($maps_counters->{$_}->{threshold}) || $maps_counters->{$_}->{threshold} != 0) { + $options{options}->add_options(arguments => { + 'warning-' . $name . ':s' => { name => 'warning-' . $name }, + 'critical-' . $name . ':s' => { name => 'critical-' . $name }, + }); + } + my $class = $maps_counters->{$_}->{class}; + $maps_counters->{$_}->{obj} = $class->new(output => $self->{output}, perfdata => $self->{perfdata}, + label => $name); + $maps_counters->{$_}->{obj}->set(%{$maps_counters->{$_}->{set}}); + } + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + foreach (keys %{$maps_counters}) { + $maps_counters->{$_}->{obj}->init(option_results => $self->{option_results}); + } +} + +sub run { + my ($self, %options) = @_; + # $options{snmp} = snmp object + $self->{snmp} = $options{snmp}; + + $self->manage_selection(); + + my $multiple = 1; + if (scalar(keys %{$self->{disk_selected}}) == 1) { + $multiple = 0; + } + + if ($multiple == 1) { + $self->{output}->output_add(severity => 'OK', + short_msg => 'All memory usages are ok'); + } + + foreach my $id (sort keys %{$self->{disk_selected}}) { + my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); + my @exits; + foreach (sort keys %{$maps_counters}) { + $maps_counters->{$_}->{obj}->set(instance => $id); + + my ($value_check) = $maps_counters->{$_}->{obj}->execute(values => $self->{disk_selected}->{$id}); + + if ($value_check != 0) { + $long_msg .= $long_msg_append . $maps_counters->{$_}->{obj}->output_error(); + $long_msg_append = ', '; + next; + } + my $exit2 = $maps_counters->{$_}->{obj}->threshold_check(); + push @exits, $exit2; + + my $output = $maps_counters->{$_}->{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 = ', '; + } + + $maps_counters->{$_}->{obj}->perfdata(extra_instance => $multiple); + } + + $self->{output}->output_add(long_msg => "Disk '" . $self->{disk_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 => "Disk '" . $self->{disk_selected}->{$id}->{display} . "' $short_msg" + ); + } + + if ($multiple == 0) { + $self->{output}->output_add(short_msg => "Disk '" . $self->{disk_selected}->{$id}->{display} . "' $long_msg"); + } + } + + $self->{output}->display(); + $self->{output}->exit(); +} + +my $mapping = { + rbnSRStorageDescr => { oid => '.1.3.6.1.4.1.2352.2.24.1.2.1.1.2' }, + rbnSRStorageSize => { oid => '.1.3.6.1.4.1.2352.2.24.1.2.1.1.5' }, # KB + rbnSRStorageUtilization => { oid => '.1.3.6.1.4.1.2352.2.24.1.2.1.1.6' }, # % +}; + +sub manage_selection { + my ($self, %options) = @_; + + $self->{memory_selected} = {}; + my $oid_rbnSRStorageEntry = '.1.3.6.1.4.1.2352.2.24.1.2.1.1'; + $self->{results} = $self->{snmp}->get_table(oid => $oid_rbnSRStorageEntry, + nothing_quit => 1); + foreach my $oid (keys %{$self->{results}}) { + next if ($oid !~ /^$mapping->{rbnSRStorageSize}->{oid}\.(\d+)/); + my $instance = $1; + my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); + if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && + $result->{rbnSRStorageDescr} !~ /$self->{option_results}->{filter_name}/) { + $self->{output}->output_add(long_msg => "Skipping '" . $result->{rbnSRStorageDescr} . "': no matching filter."); + next; + } + if ($result->{rbnSRStorageSize} == 0) { + $self->{output}->output_add(long_msg => "Skipping '" . $result->{rbnSRStorageDescr} . "': media is removed."); + next; + } + + $self->{memory_selected}->{$instance} = { display => $result->{rbnSRStorageDescr}, + used => $result->{rbnSRStorageUtilization}, total => $result->{rbnSRStorageSize}}; + } + + if (scalar(keys %{$self->{memory_selected}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No entry found."); + $self->{output}->option_exit(); + } +} + +1; + +__END__ + +=head1 MODE + +Check disk usages. + +=over 8 + +=item B<--warning-usage> + +Threshold warning (in percent). + +=item B<--critical-usage> + +Threshold critical (in percent). + +=item B<--filter-name> + +Filter disk name (can be a regexp). + +=back + +=cut diff --git a/network/redback/snmp/mode/hardware.pm b/network/redback/snmp/mode/hardware.pm index 9dc2ccebd..03550c029 100644 --- a/network/redback/snmp/mode/hardware.pm +++ b/network/redback/snmp/mode/hardware.pm @@ -290,7 +290,7 @@ Can be: 'fan', 'psu', 'temperature', 'voltage', 'disk'. =item B<--exclude> Exclude some parts (comma seperated list) (Example: --exclude=fan) -Can also exclude specific instance: --exclude=fan#1#,psu#3] +Can also exclude specific instance: --exclude=fan#1#,psu#3# =item B<--absent-problem> diff --git a/network/redback/snmp/mode/memory.pm b/network/redback/snmp/mode/memory.pm index b41a5ca60..8ef8da31c 100644 --- a/network/redback/snmp/mode/memory.pm +++ b/network/redback/snmp/mode/memory.pm @@ -123,7 +123,7 @@ sub new { } my $class = $maps_counters->{$_}->{class}; $maps_counters->{$_}->{obj} = $class->new(output => $self->{output}, perfdata => $self->{perfdata}, - label => $_); + label => $name); $maps_counters->{$_}->{obj}->set(%{$maps_counters->{$_}->{set}}); } @@ -143,8 +143,6 @@ sub run { my ($self, %options) = @_; # $options{snmp} = snmp object $self->{snmp} = $options{snmp}; - $self->{hostname} = $self->{snmp}->get_hostname(); - $self->{snmp_port} = $self->{snmp}->get_port(); $self->manage_selection(); @@ -158,7 +156,7 @@ sub run { short_msg => 'All memory usages are ok'); } - foreach my $id (sort keys %{$self->{interface_selected}}) { + foreach my $id (sort keys %{$self->{memory_selected}}) { my ($short_msg, $short_msg_append, $long_msg, $long_msg_append) = ('', '', '', ''); my @exits; foreach (sort keys %{$maps_counters}) { @@ -195,7 +193,7 @@ sub run { } if ($multiple == 0) { - $self->{output}->output_add(short_msg => "memory '" . $self->{memory_selected}->{$id}->{display} . "' $long_msg"); + $self->{output}->output_add(short_msg => "Memory '" . $self->{memory_selected}->{$id}->{display} . "' $long_msg"); } } @@ -216,7 +214,7 @@ sub manage_selection { my $oid_rbnMemoryEntry = '.1.3.6.1.4.1.2352.2.16.1.2.1'; $self->{results} = $self->{snmp}->get_table(oid => $oid_rbnMemoryEntry, nothing_quit => 1); - foreach my $oid (keys %{$self->{results}}}) { + foreach my $oid (keys %{$self->{results}}) { next if ($oid !~ /^$mapping->{rbnMemoryModule}->{oid}\.(\d+)/); my $instance = $1; my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}, instance => $instance); diff --git a/network/redback/snmp/plugin.pm b/network/redback/snmp/plugin.pm index be1724d3b..26517e40a 100644 --- a/network/redback/snmp/plugin.pm +++ b/network/redback/snmp/plugin.pm @@ -48,8 +48,12 @@ sub new { $self->{version} = '1.0'; %{$self->{modes}} = ( 'cpu' => 'network::redback::snmp::mode::cpu', + 'disk' => 'network::redback::snmp::mode::disk', 'hardware' => 'network::redback::snmp::mode::hardware', + 'list-interfaces' => 'snmp_standard::mode::listinterfaces', 'memory' => 'network::redback::snmp::mode::memory', + 'packet-errors' => 'snmp_standard::mode::packeterrors', + 'traffic' => 'snmp_standard::mode::traffic', ); return $self;