From 2bfe6dcac9b6e6c235ec3bd9502a1aa057341b1d Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Sun, 15 Dec 2013 18:16:40 +0100 Subject: [PATCH] + Work on freebsd SNMP + Add type in storage for freebsd + Quit if no cpu information --- centreon-plugins/centreon_plugins.pl | 3 +- .../os/freebsd/mode/liststorages.pm | 268 +++++++++++++ centreon-plugins/os/freebsd/mode/storage.pm | 375 ++++++++++++++++++ centreon-plugins/os/freebsd/plugin.pm | 72 ++++ centreon-plugins/snmp_standard/mode/cpu.pm | 2 +- .../snmp_standard/mode/listinterfaces.pm | 6 +- .../snmp_standard/mode/liststorages.pm | 18 +- .../snmp_standard/mode/packeterrors.pm | 6 +- .../snmp_standard/mode/storage.pm | 15 +- .../snmp_standard/mode/traffic.pm | 6 +- 10 files changed, 757 insertions(+), 14 deletions(-) create mode 100644 centreon-plugins/os/freebsd/mode/liststorages.pm create mode 100644 centreon-plugins/os/freebsd/mode/storage.pm create mode 100644 centreon-plugins/os/freebsd/plugin.pm diff --git a/centreon-plugins/centreon_plugins.pl b/centreon-plugins/centreon_plugins.pl index 9b6fb19d1..586c4e5e8 100644 --- a/centreon-plugins/centreon_plugins.pl +++ b/centreon-plugins/centreon_plugins.pl @@ -36,10 +36,11 @@ use strict; use warnings; -use centreon::plugins::script; # Not perl embedded compliant at all use FindBin; use lib "$FindBin::Bin"; # use lib '/usr/lib/nagios/plugins/'; +use centreon::plugins::script; + centreon::plugins::script->new()->run(); diff --git a/centreon-plugins/os/freebsd/mode/liststorages.pm b/centreon-plugins/os/freebsd/mode/liststorages.pm new file mode 100644 index 000000000..aef7bb9d6 --- /dev/null +++ b/centreon-plugins/os/freebsd/mode/liststorages.pm @@ -0,0 +1,268 @@ +################################################################################ +# Copyright 2005-2013 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 snmp_standard::mode::liststorages; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +my %oids_hrStorageTable = ( + 'hrstoragedescr' => '.1.3.6.1.2.1.25.2.3.1.3', + 'hrfsmountpoint' => '.1.3.6.1.2.1.25.3.8.1.2', + 'hrstoragetype' => '.1.3.6.1.2.1.25.2.3.1.2', +); + +my $oid_hrStorageAllocationUnits = '.1.3.6.1.2.1.25.2.3.1.4'; +my $oid_hrStorageSize = '.1.3.6.1.2.1.25.2.3.1.5'; +my $oid_hrStorageType = '.1.3.6.1.2.1.25.2.3.1.2'; +my $oid_hrStorageFixedDisk = '.1.3.6.1.2.1.25.2.1.4'; +my $oid_hrStorageNetworkDisk = '.1.3.6.1.2.1.25.2.1.10'; + +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 => + { + "storage:s" => { name => 'storage' }, + "name" => { name => 'use_name' }, + "regexp" => { name => 'use_regexp' }, + "regexp-isensitive" => { name => 'use_regexpi' }, + "oid-filter:s" => { name => 'oid_filter', default => 'hrStorageDescr'}, + "oid-display:s" => { name => 'oid_display', default => 'hrStorageDescr'}, + "display-transform-src:s" => { name => 'display_transform_src' }, + "display-transform-dst:s" => { name => 'display_transform_dst' }, + }); + + $self->{storage_id_selected} = []; + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + $self->{option_results}->{oid_filter} = lc($self->{option_results}->{oid_filter}); + if ($self->{option_results}->{oid_filter} !~ /^(hrstoragedescr|hrfsmountpoint)$/) { + $self->{output}->add_option_msg(short_msg => "Unsupported --oid-filter option."); + $self->{output}->option_exit(); + } + $self->{option_results}->{oid_display} = lc($self->{option_results}->{oid_display}); + if ($self->{option_results}->{oid_display} !~ /^(hrstoragedescr|hrfsmountpoint)$/) { + $self->{output}->add_option_msg(short_msg => "Unsupported --oid-display option."); + $self->{output}->option_exit(); + } +} + +sub run { + my ($self, %options) = @_; + # $options{snmp} = snmp object + $self->{snmp} = $options{snmp}; + + $self->manage_selection(); + my $result = $self->get_additional_information(); + + my $storage_display = ''; + my $storage_display_append = ''; + foreach (sort @{$self->{storage_id_selected}}) { + my $display_value = $self->get_display_value(id => $_); + my $storage_type = $result->{$oid_hrStorageType . "." . $_}; + next if (!defined($storage_type) || ($storage_type ne $oid_hrStorageFixedDisk && $storage_type ne $oid_hrStorageNetworkDisk)); + + $storage_display .= $storage_display_append . "name = $display_value [size = " . $result->{$oid_hrStorageSize . "." . $_} * $result->{$oid_hrStorageAllocationUnits . "." . $_} . "B, id = $_]"; + $storage_display_append = ', '; + } + + $self->{output}->output_add(severity => 'OK', + short_msg => 'List storage: ' . $storage_display); + $self->{output}->display(nolabel => 1); + $self->{output}->exit(); +} + +sub get_additional_information { + my ($self, %options) = @_; + + $self->{snmp}->load(oids => [$oid_hrStorageType, $oid_hrStorageAllocationUnits, $oid_hrStorageSize], instances => $self->{storage_id_selected}); + return $self->{snmp}->get_leef(); +} + +sub get_display_value { + my ($self, %options) = @_; + my $value = $self->{datas}->{$self->{option_results}->{oid_display} . "_" . $options{id}}; + + if (defined($self->{option_results}->{display_transform_src})) { + $self->{option_results}->{display_transform_dst} = '' if (!defined($self->{option_results}->{display_transform_dst})); + eval "\$value =~ s{$self->{option_results}->{display_transform_src}}{$self->{option_results}->{display_transform_dst}}"; + } + return $value; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{datas} = {}; + $self->{datas}->{oid_filter} = $self->{option_results}->{oid_filter}; + $self->{datas}->{oid_display} = $self->{option_results}->{oid_display}; + my $result = $self->{snmp}->get_table(oid => $oids_hrStorageTable{$self->{option_results}->{oid_filter}}); + my $total_storage = 0; + foreach my $key ($self->{snmp}->oid_lex_sort(keys %$result)) { + next if ($key !~ /\.([0-9]+)$/); + $self->{datas}->{$self->{option_results}->{oid_filter} . "_" . $1} = $self->{output}->to_utf8($result->{$key}); + $total_storage = $1; + } + + if (scalar(keys %{$self->{datas}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "Can't get storages..."); + $self->{output}->option_exit(); + } + + if ($self->{option_results}->{oid_filter} ne $self->{option_results}->{oid_display}) { + $result = $self->{snmp}->get_table(oid => $oids_hrStorageTable{$self->{option_results}->{oid_display}}); + foreach my $key ($self->{snmp}->oid_lex_sort(keys %$result)) { + next if ($key !~ /\.([0-9]+)$/); + $self->{datas}->{$self->{option_results}->{oid_display} . "_" . $1} = $self->{output}->to_utf8($result->{$key}); + } + } + + if (!defined($self->{option_results}->{use_name}) && defined($self->{option_results}->{storage})) { + # get by ID + push @{$self->{storage_id_selected}}, $self->{option_results}->{storage}; + my $name = $self->{datas}->{$self->{option_results}->{oid_display} . "_" . $self->{option_results}->{storage}}; + if (!defined($name)) { + $self->{output}->add_option_msg(short_msg => "No storage found for id '" . $self->{option_results}->{storage} . "'."); + $self->{output}->option_exit(); + } + } else { + for (my $i = 0; $i <= $total_storage; $i++) { + my $filter_name = $self->{datas}->{$self->{option_results}->{oid_filter} . "_" . $i}; + next if (!defined($filter_name)); + if (!defined($self->{option_results}->{storage})) { + push @{$self->{storage_id_selected}}, $i; + next; + } + if (defined($self->{option_results}->{use_regexp}) && defined($self->{option_results}->{use_regexpi}) && $filter_name =~ /$self->{option_results}->{storage}/i) { + push @{$self->{storage_id_selected}}, $i; + } + if (defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $filter_name =~ /$self->{option_results}->{storage}/) { + push @{$self->{storage_id_selected}}, $i; + } + if (!defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $filter_name eq $self->{option_results}->{storage}) { + push @{$self->{storage_id_selected}}, $i; + } + } + + if (scalar(@{$self->{storage_id_selected}}) <= 0) { + if (defined($self->{option_results}->{storage})) { + $self->{output}->add_option_msg(short_msg => "No storage found for name '" . $self->{option_results}->{storage} . "'."); + } else { + $self->{output}->add_option_msg(short_msg => "No storage found."); + } + $self->{output}->option_exit(); + } + } +} + +sub disco_format { + my ($self, %options) = @_; + + $self->{output}->add_disco_format(elements => ['name', 'total', 'storageid']); +} + +sub disco_show { + my ($self, %options) = @_; + # $options{snmp} = snmp object + $self->{snmp} = $options{snmp}; + + $self->manage_selection(); + my $result = $self->get_additional_information(); + foreach (sort @{$self->{storage_id_selected}}) { + my $display_value = $self->get_display_value(id => $_); + my $storage_type = $result->{$oid_hrStorageType . "." . $_}; + next if (!defined($storage_type) || ($storage_type ne $oid_hrStorageFixedDisk && $storage_type ne $oid_hrStorageNetworkDisk)); + + $self->{output}->add_disco_entry(name => $display_value, + total => $result->{$oid_hrStorageSize . "." . $_} * $result->{$oid_hrStorageAllocationUnits . "." . $_}, + storageid => $_); + } +} + +1; + +__END__ + +=head1 MODE + +=over 8 + +=item B<--storage> + +Set the storage (number expected) ex: 1, 2,... (empty means 'check all storage'). + +=item B<--name> + +Allows to use storage name with option --storage instead of storage oid index. + +=item B<--regexp> + +Allows to use regexp to filter storage (with option --name). + +=item B<--regexp-isensitive> + +Allows to use regexp non case-sensitive (with --regexp). + +=item B<--oid-filter> + +Choose OID used to filter storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSRemoteMountPoint). + +=item B<--oid-display> + +Choose OID used to display storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSRemoteMountPoint). + +=item B<--display-transform-src> + +Regexp src to transform display value. (security risk!!!) + +=item B<--display-transform-dst> + +Regexp dst to transform display value. (security risk!!!) + +=back + +=cut diff --git a/centreon-plugins/os/freebsd/mode/storage.pm b/centreon-plugins/os/freebsd/mode/storage.pm new file mode 100644 index 000000000..c4f444f6e --- /dev/null +++ b/centreon-plugins/os/freebsd/mode/storage.pm @@ -0,0 +1,375 @@ +################################################################################ +# Copyright 2005-2013 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 snmp_standard::mode::storage; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use centreon::plugins::statefile; +use Digest::MD5 qw(md5_hex); + +my %oids_hrStorageTable = ( + 'hrstoragedescr' => '.1.3.6.1.2.1.25.2.3.1.3', + 'hrfsmountpoint' => '.1.3.6.1.2.1.25.3.8.1.2', + 'hrstoragetype' => '.1.3.6.1.2.1.25.2.3.1.2', +); + +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 => + { + "warning:s" => { name => 'warning' }, + "critical:s" => { name => 'critical' }, + "units:s" => { name => 'units', default => '%' }, + "free" => { name => 'free' }, + "reload-cache-time:s" => { name => 'reload_cache_time' }, + "name" => { name => 'use_name' }, + "storage:s" => { name => 'storage' }, + "regexp" => { name => 'use_regexp' }, + "regexp-isensitive" => { name => 'use_regexpi' }, + "oid-filter:s" => { name => 'oid_filter', default => 'hrStorageDescr'}, + "oid-display:s" => { name => 'oid_display', default => 'hrStorageDescr'}, + "display-transform-src:s" => { name => 'display_transform_src' }, + "display-transform-dst:s" => { name => 'display_transform_dst' }, + "show-cache" => { name => 'show_cache' }, + }); + + $self->{storage_id_selected} = []; + $self->{statefile_cache} = centreon::plugins::statefile->new(%options); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); + $self->{output}->option_exit(); + } + $self->{option_results}->{oid_filter} = lc($self->{option_results}->{oid_filter}); + if ($self->{option_results}->{oid_filter} !~ /^(hrstoragedescr|hrfsmountpoint)$/) { + $self->{output}->add_option_msg(short_msg => "Unsupported --oid-filter option."); + $self->{output}->option_exit(); + } + $self->{option_results}->{oid_display} = lc($self->{option_results}->{oid_display}); + if ($self->{option_results}->{oid_display} !~ /^(hrstoragedescr|hrfsmountpoint)$/) { + $self->{output}->add_option_msg(short_msg => "Unsupported --oid-display option."); + $self->{output}->option_exit(); + } + + $self->{statefile_cache}->check_options(%options); +} + +sub run { + my ($self, %options) = @_; + # $options{snmp} = snmp object + $self->{snmp} = $options{snmp}; + $self->{hostname} = $self->{snmp}->get_hostname(); + + $self->manage_selection(); + + my $oid_hrStorageAllocationUnits = '.1.3.6.1.2.1.25.2.3.1.4'; + my $oid_hrStorageSize = '.1.3.6.1.2.1.25.2.3.1.5'; + my $oid_hrStorageUsed = '.1.3.6.1.2.1.25.2.3.1.6'; + my $oid_hrStorageType = '.1.3.6.1.2.1.25.2.3.1.2'; + my $oid_hrStorageFixedDisk = '.1.3.6.1.2.1.25.2.1.4'; + my $oid_hrStorageNetworkDisk = '.1.3.6.1.2.1.25.2.1.10'; + + $self->{snmp}->load(oids => [$oid_hrStorageAllocationUnits, $oid_hrStorageSize, $oid_hrStorageUsed], instances => $self->{storage_id_selected}); + my $result = $self->{snmp}->get_leef(); + + if (!defined($self->{option_results}->{storage}) || defined($self->{option_results}->{use_regexp})) { + $self->{output}->output_add(severity => 'OK', + short_msg => 'All storages are ok.'); + } + + my $num_disk_check = 0; + foreach (sort @{$self->{storage_id_selected}}) { + # Skipped disks + my $storage_type = $self->{statefile_cache}->get(name => "type_" . $_); + next if (!defined($storage_type) || ($storage_type ne $oid_hrStorageFixedDisk && $storage_type ne $oid_hrStorageNetworkDisk)); + + my $name_storage = $self->get_display_value(id => $_); + $num_disk_check++; + + # in bytes hrStorageAllocationUnits + my $total_size = $result->{$oid_hrStorageSize . "." . $_} * $result->{$oid_hrStorageAllocationUnits . "." . $_}; + next if ($total_size == 0); + my $total_used = $result->{$oid_hrStorageUsed . "." . $_} * $result->{$oid_hrStorageAllocationUnits . "." . $_}; + my $total_free = $total_size - $total_used; + my $prct_used = $total_used * 100 / $total_size; + my $prct_free = 100 - $prct_used; + + my ($exit, $threshold_value); + + $threshold_value = $total_used; + $threshold_value = $total_free if (defined($self->{option_results}->{free})); + if ($self->{option_results}->{units} eq '%') { + $threshold_value = $prct_used; + $threshold_value = $prct_free if (defined($self->{option_results}->{free})); + } + $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); + + my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $total_size); + my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $total_used); + my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => ($total_size - $total_used)); + + $self->{output}->output_add(long_msg => sprintf("Storage '%s' Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $name_storage, + $total_size_value . " " . $total_size_unit, + $total_used_value . " " . $total_used_unit, $prct_used, + $total_free_value . " " . $total_free_unit, $prct_free)); + if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1) || (defined($self->{option_results}->{storage}) && !defined($self->{option_results}->{use_regexp}))) { + $self->{output}->output_add(severity => $exit, + short_msg => sprintf("Storage '%s' Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $name_storage, + $total_size_value . " " . $total_size_unit, + $total_used_value . " " . $total_used_unit, $prct_used, + $total_free_value . " " . $total_free_unit, $prct_free)); + } + + my $label = 'used'; + my $value_perf = $total_used; + if (defined($self->{option_results}->{free})) { + $label = 'free'; + $value_perf = $total_free; + } + my $extra_label = ''; + $extra_label = '_' . $name_storage if (!defined($self->{option_results}->{storage}) || defined($self->{option_results}->{use_regexp})); + my %total_options = (); + if ($self->{option_results}->{units} eq '%') { + $total_options{total} = $total_size; + $total_options{cast_int} = 1; + } + $self->{output}->perfdata_add(label => $label . $extra_label, unit => 'o', + value => $value_perf, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning', %total_options), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical', %total_options), + min => 0, max => $total_size); + } + + if ($num_disk_check == 0) { + $self->{output}->add_option_msg(short_msg => "Not a disk with a good 'type'."); + $self->{output}->option_exit(); + } + + $self->{output}->display(); + $self->{output}->exit(); +} + +sub reload_cache { + my ($self) = @_; + my $datas = {}; + + $datas->{oid_filter} = $self->{option_results}->{oid_filter}; + $datas->{oid_display} = $self->{option_results}->{oid_display}; + my $result = $self->{snmp}->get_table(oid => $oids_hrStorageTable{$self->{option_results}->{oid_filter}}); + my $last_num = 0; + foreach my $key ($self->{snmp}->oid_lex_sort(keys %$result)) { + next if ($key !~ /\.([0-9]+)$/); + $datas->{$self->{option_results}->{oid_filter} . "_" . $1} = $self->{output}->to_utf8($result->{$key}); + $last_num = $1; + } + + if (scalar(keys %$datas) <= 0) { + $self->{output}->add_option_msg(short_msg => "Can't construct cache..."); + $self->{output}->option_exit(); + } + + if ($self->{option_results}->{oid_filter} ne $self->{option_results}->{oid_display}) { + $result = $self->{snmp}->get_table(oid => $oids_hrStorageTable{$self->{option_results}->{oid_display}}); + foreach my $key ($self->{snmp}->oid_lex_sort(keys %$result)) { + next if ($key !~ /\.([0-9]+)$/); + $datas->{$self->{option_results}->{oid_display} . "_" . $1} = $self->{output}->to_utf8($result->{$key}); + } + } + + $result = $self->{snmp}->get_table(oid => $oids_hrStorageTable{hrstoragetype}); + foreach my $key ($self->{snmp}->oid_lex_sort(keys %$result)) { + next if ($key !~ /\.([0-9]+)$/); + $datas->{"type_" . $1} = $result->{$key}; + } + + $datas->{total_storage} = $last_num; + $self->{statefile_cache}->write(data => $datas); +} + +sub manage_selection { + my ($self, %options) = @_; + + # init cache file + my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_snmpstandard_' . $self->{hostname} . '_' . $self->{mode}); + if (defined($self->{option_results}->{show_cache})) { + $self->{output}->add_option_msg(long_msg => $self->{statefile_cache}->get_string_content()); + $self->{output}->option_exit(); + } + + my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp'); + my $oid_display = $self->{statefile_cache}->get(name => 'oid_display'); + my $oid_filter = $self->{statefile_cache}->get(name => 'oid_filter'); + if ($has_cache_file == 0 || + ($self->{option_results}->{oid_display} !~ /^($oid_display|$oid_filter)$/i || $self->{option_results}->{oid_filter} !~ /^($oid_display|$oid_filter)$/i) || + (defined($timestamp_cache) && (time() - $timestamp_cache) > (($self->{option_results}->{reload_cache_time}) * 60))) { + $self->reload_cache(); + $self->{statefile_cache}->read(); + } + + my $total_storage = $self->{statefile_cache}->get(name => 'total_storage'); + if (!defined($self->{option_results}->{use_name}) && defined($self->{option_results}->{storage})) { + # get by ID + push @{$self->{storage_id_selected}}, $self->{option_results}->{storage}; + my $name = $self->{statefile_cache}->get(name => $self->{option_results}->{oid_display} . "_" . $self->{option_results}->{storage}); + if (!defined($name)) { + $self->{output}->add_option_msg(short_msg => "No storage found for id '" . $self->{option_results}->{storage} . "'."); + $self->{output}->option_exit(); + } + } else { + for (my $i = 0; $i <= $total_storage; $i++) { + my $filter_name = $self->{statefile_cache}->get(name => $self->{option_results}->{oid_filter} . "_" . $i); + next if (!defined($filter_name)); + if (!defined($self->{option_results}->{storage})) { + push @{$self->{storage_id_selected}}, $i; + next; + } + if (defined($self->{option_results}->{use_regexp}) && defined($self->{option_results}->{use_regexpi}) && $filter_name =~ /$self->{option_results}->{storage}/i) { + push @{$self->{storage_id_selected}}, $i; + } + if (defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $filter_name =~ /$self->{option_results}->{storage}/) { + push @{$self->{storage_id_selected}}, $i; + } + if (!defined($self->{option_results}->{use_regexp}) && !defined($self->{option_results}->{use_regexpi}) && $filter_name eq $self->{option_results}->{storage}) { + push @{$self->{storage_id_selected}}, $i; + } + } + + if (scalar(@{$self->{storage_id_selected}}) <= 0) { + if (defined($self->{option_results}->{storage})) { + $self->{output}->add_option_msg(short_msg => "No storage found for name '" . $self->{option_results}->{storage} . "' (maybe you should reload cache file)."); + } else { + $self->{output}->add_option_msg(short_msg => "No storage found (maybe you should reload cache file)."); + } + $self->{output}->option_exit(); + } + } +} + +sub get_display_value { + my ($self, %options) = @_; + my $value = $self->{statefile_cache}->get(name => $self->{option_results}->{oid_display} . "_" . $options{id}); + + if (defined($self->{option_results}->{display_transform_src})) { + $self->{option_results}->{display_transform_dst} = '' if (!defined($self->{option_results}->{display_transform_dst})); + eval "\$value =~ s{$self->{option_results}->{display_transform_src}}{$self->{option_results}->{display_transform_dst}}"; + } + return $value; +} + +1; + +__END__ + +=head1 MODE + +=over 8 + +=item B<--warning> + +Threshold warning. + +=item B<--critical> + +Threshold critical. + +=item B<--units> + +Units of thresholds (Default: '%') ('%', 'B'). + +=item B<--free> + +Thresholds are on free space left. + +=item B<--storage> + +Set the storage (number expected) ex: 1, 2,... (empty means 'check all storage'). + +=item B<--name> + +Allows to use storage name with option --storage instead of storage oid index. + +=item B<--regexp> + +Allows to use regexp to filter storage (with option --name). + +=item B<--regexp-isensitive> + +Allows to use regexp non case-sensitive (with --regexp). + +=item B<--reload-cache-time> + +Time in seconds before reloading cache file (default: 180). + +=item B<--oid-filter> + +Choose OID used to filter storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSRemoteMountPoint). + +=item B<--oid-display> + +Choose OID used to display storage (default: hrStorageDescr) (values: hrStorageDescr, hrFSRemoteMountPoint). + +=item B<--display-transform-src> + +Regexp src to transform display value. (security risk!!!) + +=item B<--display-transform-dst> + +Regexp dst to transform display value. (security risk!!!) + +=item B<--show-cache> + +Display cache storage datas. + +=back + +=cut diff --git a/centreon-plugins/os/freebsd/plugin.pm b/centreon-plugins/os/freebsd/plugin.pm new file mode 100644 index 000000000..bfd2df247 --- /dev/null +++ b/centreon-plugins/os/freebsd/plugin.pm @@ -0,0 +1,72 @@ +################################################################################ +# Copyright 2005-2013 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 os::freebsd::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_snmp); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + # $options->{options} = options object + + $self->{version} = '0.1'; + %{$self->{modes}} = ( + 'cpu' => 'snmp_standard::mode::cpu', + 'load' => 'snmp_standard::mode::loadaverage', + 'list-interfaces' => 'snmp_standard::mode::listinterfaces', + 'list-storages' => 'snmp_standard::mode::liststorages', + 'packet-errors' => 'snmp_standard::mode::packeterrors', + 'storage' => 'snmp_standard::mode::storage', + 'traffic' => 'snmp_standard::mode::traffic', + 'uptime' => 'snmp_standard::mode::uptime', + ); + + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Freebsd operating systems in SNMP. +Some modes ('cpu', 'load') needs 'bsnmp-ucd'. + +=cut diff --git a/centreon-plugins/snmp_standard/mode/cpu.pm b/centreon-plugins/snmp_standard/mode/cpu.pm index 0a6543236..4bf1c33bf 100644 --- a/centreon-plugins/snmp_standard/mode/cpu.pm +++ b/centreon-plugins/snmp_standard/mode/cpu.pm @@ -75,7 +75,7 @@ sub run { $self->{snmp} = $options{snmp}; my $oid_cputable = '.1.3.6.1.2.1.25.3.3.1.2'; - my $result = $self->{snmp}->get_table(oid => $oid_cputable); + my $result = $self->{snmp}->get_table(oid => $oid_cputable, nothing_quit => 1); my $cpu = 0; my $i = 0; diff --git a/centreon-plugins/snmp_standard/mode/listinterfaces.pm b/centreon-plugins/snmp_standard/mode/listinterfaces.pm index 7750b2180..7128e263a 100644 --- a/centreon-plugins/snmp_standard/mode/listinterfaces.pm +++ b/centreon-plugins/snmp_standard/mode/listinterfaces.pm @@ -206,7 +206,11 @@ sub manage_selection { } if (scalar(@{$self->{interface_id_selected}}) <= 0) { - $self->{output}->add_option_msg(short_msg => "No interface found for name '" . $self->{option_results}->{interface} . "'."); + if (defined($self->{option_results}->{interface})) { + $self->{output}->add_option_msg(short_msg => "No interface found for name '" . $self->{option_results}->{interface} . "'."); + } else { + $self->{output}->add_option_msg(short_msg => "No interface found."); + } $self->{output}->option_exit(); } } diff --git a/centreon-plugins/snmp_standard/mode/liststorages.pm b/centreon-plugins/snmp_standard/mode/liststorages.pm index 4408c0cfe..55e3d3f3d 100644 --- a/centreon-plugins/snmp_standard/mode/liststorages.pm +++ b/centreon-plugins/snmp_standard/mode/liststorages.pm @@ -49,8 +49,12 @@ my %oids_hrStorageTable = ( my $oid_hrStorageAllocationUnits = '.1.3.6.1.2.1.25.2.3.1.4'; my $oid_hrStorageSize = '.1.3.6.1.2.1.25.2.3.1.5'; my $oid_hrStorageType = '.1.3.6.1.2.1.25.2.3.1.2'; -my $oid_hrStorageFixedDisk = '.1.3.6.1.2.1.25.2.1.4'; -my $oid_hrStorageNetworkDisk = '.1.3.6.1.2.1.25.2.1.10'; + +my %storage_types_manage = ( + '.1.3.6.1.2.1.25.2.1.4' => 'hrStorageFixedDisk', + '.1.3.6.1.2.1.25.2.1.10' => 'hrStorageNetworkDisk', + '.1.3.6.1.2.1.25.3.9.3' => 'hrFSBerkeleyFFS' # For Freebsd +); sub new { my ($class, %options) = @_; @@ -104,7 +108,7 @@ sub run { foreach (sort @{$self->{storage_id_selected}}) { my $display_value = $self->get_display_value(id => $_); my $storage_type = $result->{$oid_hrStorageType . "." . $_}; - next if (!defined($storage_type) || ($storage_type ne $oid_hrStorageFixedDisk && $storage_type ne $oid_hrStorageNetworkDisk)); + next if (!defined($storage_type) || (!defined($storage_types_manage{$storage_type}))); $storage_display .= $storage_display_append . "name = $display_value [size = " . $result->{$oid_hrStorageSize . "." . $_} * $result->{$oid_hrStorageAllocationUnits . "." . $_} . "B, id = $_]"; $storage_display_append = ', '; @@ -189,7 +193,11 @@ sub manage_selection { } if (scalar(@{$self->{storage_id_selected}}) <= 0) { - $self->{output}->add_option_msg(short_msg => "No storage found for name '" . $self->{option_results}->{storage} . "'."); + if (defined($self->{option_results}->{storage})) { + $self->{output}->add_option_msg(short_msg => "No storage found for name '" . $self->{option_results}->{storage} . "'."); + } else { + $self->{output}->add_option_msg(short_msg => "No storage found."); + } $self->{output}->option_exit(); } } @@ -211,7 +219,7 @@ sub disco_show { foreach (sort @{$self->{storage_id_selected}}) { my $display_value = $self->get_display_value(id => $_); my $storage_type = $result->{$oid_hrStorageType . "." . $_}; - next if (!defined($storage_type) || ($storage_type ne $oid_hrStorageFixedDisk && $storage_type ne $oid_hrStorageNetworkDisk)); + next if (!defined($storage_type) || (!defined($storage_types_manage{$storage_type}))); $self->{output}->add_disco_entry(name => $display_value, total => $result->{$oid_hrStorageSize . "." . $_} * $result->{$oid_hrStorageAllocationUnits . "." . $_}, diff --git a/centreon-plugins/snmp_standard/mode/packeterrors.pm b/centreon-plugins/snmp_standard/mode/packeterrors.pm index a19d78f90..7dda32952 100644 --- a/centreon-plugins/snmp_standard/mode/packeterrors.pm +++ b/centreon-plugins/snmp_standard/mode/packeterrors.pm @@ -429,7 +429,11 @@ sub manage_selection { } if (scalar(@{$self->{interface_id_selected}}) <= 0) { - $self->{output}->add_option_msg(short_msg => "No interface found for name '" . $self->{option_results}->{interface} . "' (maybe you should reload cache file)."); + if (defined($self->{option_results}->{interface})) { + $self->{output}->add_option_msg(short_msg => "No interface found for name '" . $self->{option_results}->{interface} . "' (maybe you should reload cache file)."); + } else { + $self->{output}->add_option_msg(short_msg => "No interface found (maybe you should reload cache file)."); + } $self->{output}->option_exit(); } } diff --git a/centreon-plugins/snmp_standard/mode/storage.pm b/centreon-plugins/snmp_standard/mode/storage.pm index 3016f3bfc..1b7dc3072 100644 --- a/centreon-plugins/snmp_standard/mode/storage.pm +++ b/centreon-plugins/snmp_standard/mode/storage.pm @@ -47,6 +47,11 @@ my %oids_hrStorageTable = ( 'hrfsmountpoint' => '.1.3.6.1.2.1.25.3.8.1.2', 'hrstoragetype' => '.1.3.6.1.2.1.25.2.3.1.2', ); +my %storage_types_manage = ( + '.1.3.6.1.2.1.25.2.1.4' => 'hrStorageFixedDisk', + '.1.3.6.1.2.1.25.2.1.10' => 'hrStorageNetworkDisk', + '.1.3.6.1.2.1.25.3.9.3' => 'hrFSBerkeleyFFS' # For Freebsd +); sub new { my ($class, %options) = @_; @@ -116,8 +121,6 @@ sub run { my $oid_hrStorageSize = '.1.3.6.1.2.1.25.2.3.1.5'; my $oid_hrStorageUsed = '.1.3.6.1.2.1.25.2.3.1.6'; my $oid_hrStorageType = '.1.3.6.1.2.1.25.2.3.1.2'; - my $oid_hrStorageFixedDisk = '.1.3.6.1.2.1.25.2.1.4'; - my $oid_hrStorageNetworkDisk = '.1.3.6.1.2.1.25.2.1.10'; $self->{snmp}->load(oids => [$oid_hrStorageAllocationUnits, $oid_hrStorageSize, $oid_hrStorageUsed], instances => $self->{storage_id_selected}); my $result = $self->{snmp}->get_leef(); @@ -131,7 +134,7 @@ sub run { foreach (sort @{$self->{storage_id_selected}}) { # Skipped disks my $storage_type = $self->{statefile_cache}->get(name => "type_" . $_); - next if (!defined($storage_type) || ($storage_type ne $oid_hrStorageFixedDisk && $storage_type ne $oid_hrStorageNetworkDisk)); + next if (!defined($storage_type) || (!defined($storage_types_manage{$storage_type}))); my $name_storage = $self->get_display_value(id => $_); $num_disk_check++; @@ -285,7 +288,11 @@ sub manage_selection { } if (scalar(@{$self->{storage_id_selected}}) <= 0) { - $self->{output}->add_option_msg(short_msg => "No storage found for name '" . $self->{option_results}->{storage} . "' (maybe you should reload cache file)."); + if (defined($self->{option_results}->{storage})) { + $self->{output}->add_option_msg(short_msg => "No storage found for name '" . $self->{option_results}->{storage} . "' (maybe you should reload cache file)."); + } else { + $self->{output}->add_option_msg(short_msg => "No storage found (maybe you should reload cache file)."); + } $self->{output}->option_exit(); } } diff --git a/centreon-plugins/snmp_standard/mode/traffic.pm b/centreon-plugins/snmp_standard/mode/traffic.pm index eeb7751f8..5ea375dae 100644 --- a/centreon-plugins/snmp_standard/mode/traffic.pm +++ b/centreon-plugins/snmp_standard/mode/traffic.pm @@ -372,7 +372,11 @@ sub manage_selection { } if (scalar(@{$self->{interface_id_selected}}) <= 0) { - $self->{output}->add_option_msg(short_msg => "No interface found for name '" . $self->{option_results}->{interface} . "' (maybe you should reload cache file)."); + if (defined($self->{option_results}->{interface})) { + $self->{output}->add_option_msg(short_msg => "No interface found for name '" . $self->{option_results}->{interface} . "' (maybe you should reload cache file)."); + } else { + $self->{output}->add_option_msg(short_msg => "No interface found (maybe you should reload cache file)."); + } $self->{output}->option_exit(); } }