Initial version for new class wsman
Some code cleaning in snmp class
Move snmp windows plugin to a sub-dir (for wsman coming)
This commit is contained in:
garnier-quentin 2014-02-08 23:45:41 +01:00
parent 0876af927e
commit c8671bd80a
7 changed files with 605 additions and 12 deletions

View File

@ -0,0 +1,213 @@
################################################################################
# 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 <http://www.gnu.org/licenses>.
#
# 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 <qgarnier@merethis.com>
#
####################################################################################
package centreon::plugins::script_wsman;
use strict;
use warnings;
use centreon::plugins::wsman;
use centreon::plugins::misc;
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
# $options{package} = parent package caller
# $options{options} = options object
# $options{output} = output object
$self->{options} = $options{options};
$self->{output} = $options{output};
$self->{options}->add_options(
arguments => {
'mode:s' => { name => 'mode' },
'dyn-mode:s' => { name => 'dynmode_name' },
'list-mode' => { name => 'list_mode' },
}
);
$self->{version} = '1.0';
%{$self->{modes}} = ();
$self->{default} = undef;
$self->{options}->parse_options();
$self->{mode_name} = $self->{options}->get_option(argument => 'mode' );
$self->{list_mode} = $self->{options}->get_option(argument => 'list_mode' );
$self->{options}->clean();
$self->{options}->add_help(package => $options{package}, sections => 'PLUGIN DESCRIPTION');
$self->{options}->add_help(package => __PACKAGE__, sections => 'GLOBAL OPTIONS');
return $self;
}
sub init {
my ($self, %options) = @_;
# $options{version} = string version
# $options{help} = string help
if (defined($options{help}) && !defined($self->{mode_name})) {
$self->{options}->display_help();
$self->{output}->option_exit();
}
if (defined($options{version}) && !defined($self->{mode_name})) {
$self->version();
}
if (defined($self->{list_mode})) {
$self->list_mode();
}
# Output HELP
$self->{options}->add_help(package => 'centreon::plugins::output', sections => 'OUTPUT OPTIONS');
# WSMAN
$self->{wsman} = centreon::plugins::wsman->new(options => $self->{options}, output => $self->{output});
# Load mode
if (defined($self->{mode_name}) && $self->{mode_name} ne '') {
$self->is_mode(mode => $self->{mode_name});
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => $self->{modes}{$self->{mode_name}},
error_msg => "Cannot load module --mode.");
$self->{mode} = $self->{modes}{$self->{mode_name}}->new(options => $self->{options}, output => $self->{output}, mode => $self->{mode_name});
} elsif (defined($self->{dynmode_name}) && $self->{dynmode_name} ne '') {
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => $self->{dynmode_name},
error_msg => "Cannot load module --dyn-mode.");
$self->{mode} = $self->{dynmode_name}->new(options => $self->{options}, output => $self->{output}, mode => $self->{mode_name});
} else {
$self->{output}->add_option_msg(short_msg => "Need to specify '--mode' or '--dyn-mode' option.");
$self->{output}->option_exit();
}
if (defined($options{help})) {
$self->{options}->add_help(package => $self->{modes}{$self->{mode_name}}, sections => 'MODE');
$self->{options}->display_help();
$self->{output}->option_exit();
}
if (defined($options{version})) {
$self->{mode}->version();
$self->{output}->option_exit(nolabel => 1);
}
$self->{options}->parse_options();
$self->{option_results} = $self->{options}->get_options();
$self->{wsman}->check_options(option_results => $self->{option_results});
$self->{mode}->check_options(option_results => $self->{option_results}, default => $self->{default});
}
sub run {
my $self = shift;
if ($self->{output}->is_disco_format()) {
$self->{mode}->disco_format();
$self->{output}->display_disco_format();
$self->{output}->exit(exit_litteral => 'ok');
}
$self->{wsman}->connect();
if ($self->{output}->is_disco_show()) {
$self->{mode}->disco_show(wsman => $self->{wsman});
$self->{output}->display_disco_show();
$self->{output}->exit(exit_litteral => 'ok');
} else {
$self->{mode}->run(wsman => $self->{wsman});
}
}
sub is_mode {
my ($self, %options) = @_;
# $options->{mode} = mode
if (!defined($self->{modes}{$options{mode}})) {
$self->{output}->add_option_msg(short_msg => "mode '" . $options{mode} . "' doesn't exist (use --list-mode option to show available modes).");
$self->{output}->option_exit();
}
}
sub version {
my $self = shift;
$self->{output}->add_option_msg(short_msg => "Plugin Version: " . $self->{version});
$self->{output}->option_exit(nolabel => 1);
}
sub list_mode {
my $self = shift;
$self->{options}->display_help();
$self->{output}->add_option_msg(long_msg => "Modes Available:");
foreach (keys %{$self->{modes}}) {
$self->{output}->add_option_msg(long_msg => " " . $_);
}
$self->{output}->option_exit(nolabel => 1);
}
1;
__END__
=head1 NAME
-
=head1 SYNOPSIS
-
=head1 GLOBAL OPTIONS
=over 8
=item B<--mode>
Choose a mode.
=item B<--dyn-mode>
Specify a mode with the path (separated by '::').
=item B<--list-mode>
List available modes.
=item B<--version>
Display plugin version.
=back
=head1 DESCRIPTION
B<>.
=cut

View File

@ -99,10 +99,7 @@ sub new {
sub connect {
my ($self, %options) = @_;
# $options{exit_value} = integer
my ($exit_value) = defined($options{exit_value}) ? $options{exit_value} : $self->{global_exit_value};
$self->{snmp_params}->{RetryNoSuch} = $self->{RetryNoSuch};
$self->{snmp_params}->{UseNumeric} = $self->{UseNumeric};
@ -113,8 +110,8 @@ sub connect {
$self->{session} = new SNMP::Session(%{$self->{snmp_params}});
if ($self->{session}->{ErrorNum}) {
$self->{output}->add_option_msg(short_msg => 'UNKNOWN: SNMP Session : ' . $self->{session}->{ErrorStr});
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{snmp_errors_exit});
$self->{output}->add_option_msg(short_msg => 'SNMP Session : ' . $self->{session}->{ErrorStr});
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
}
}
@ -169,7 +166,7 @@ sub get_leef {
return undef;
}
$self->{output}->add_option_msg(short_msg => 'Need to specify OIDs');
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{snmp_errors_exit});
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
}
push @{$options{oids}}, @{$self->{oids_loaded}};
@{$self->{oids_loaded}} = ();
@ -259,7 +256,7 @@ sub get_leef {
if ($dont_quit == 0) {
$self->{output}->add_option_msg(short_msg => $msg);
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{snmp_errors_exit});
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
}
$self->set_error(error_status => -1, error_msg => $msg);
@ -283,7 +280,7 @@ sub get_leef {
if ($nothing_quit == 1 && $total == 0) {
$self->{output}->add_option_msg(short_msg => "SNMP GET Request : Cant get a single value.");
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{snmp_errors_exit});
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
}
return $results;
@ -324,7 +321,7 @@ sub get_table {
return undef;
}
$self->{output}->add_option_msg(short_msg => "Method 'get_table': Wrong OID '" . $options{oid} . "'.");
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{snmp_errors_exit});
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
}
my $main_indice = $1 . "." . $2;
@ -362,7 +359,7 @@ sub get_table {
if ($dont_quit == 0) {
$self->{output}->add_option_msg(short_msg => $msg);
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{snmp_errors_exit});
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
}
$self->set_error(error_status => -1, error_msg => $msg);
@ -392,7 +389,7 @@ sub get_table {
if ($nothing_quit == 1 && scalar(keys %$results) == 0) {
$self->{output}->add_option_msg(short_msg => "SNMP Table Request: Cant get a single value.");
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{snmp_errors_exit});
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
}
return $results;
@ -426,7 +423,7 @@ sub set {
if ($dont_quit == 0) {
$self->{output}->add_option_msg(short_msg => $msg);
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{snmp_errors_exit});
$self->{output}->option_exit(exit_litteral => $self->{snmp_errors_exit});
}
$self->set_error(error_status => -1, error_msg => $msg);

383
centreon/plugins/wsman.pm Normal file
View File

@ -0,0 +1,383 @@
################################################################################
# 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 <http://www.gnu.org/licenses>.
#
# 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 <qgarnier@merethis.com>
#
####################################################################################
package centreon::plugins::wsman;
use strict;
use warnings;
use openwsman;
my %auth_method_map = (
noauth => $openwsman::NO_AUTH_STR,
basic => $openwsman::BASIC_AUTH_STR,
digest => $openwsman::DIGEST_AUTH_STR,
pass => $openwsman::PASS_AUTH_STR,
ntml => $openwsman::NTML_AUTH_STR,
gssnegotiate => $openwsman::GSSNEGOTIATE_AUTH_STR,
);
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
# $options{options} = options object
# $options{output} = output object
# $options{exit_value} = integer
if (!defined($options{output})) {
print "Class wsman: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class wsman: Need to specify 'options' argument.");
$options{output}->option_exit();
}
$options{options}->add_options(arguments =>
{ "hostname|host:s" => { name => 'host' },
"wsman-port:s" => { name => 'wsman_port', default => 5985 },
"wsman-path:s" => { name => 'wsman_path', default => '/wsman' },
"wsman-scheme:s" => { name => 'wsman_scheme', default => 'http' },
"wsman-username:s" => { name => 'wsman_username' },
"wsman-password:s" => { name => 'wsman_password' },
"wsman-timeout:s" => { name => 'wsman_timeout', default => 30 },
"wsman-proxy-url:s" => { name => 'wsman_proxy_url', },
"wsman-proxy-username:s" => { name => 'wsman_proxy_username', },
"wsman-proxy-password:s" => { name => 'wsman_proxy_password', },
"wsman-debug" => { name => 'wsman_debug', },
"wsman-auth-method:s" => { name => 'wsman_auth_method', default => 'basic' },
"wsman-errors-exit:s" => { name => 'wsman_errors_exit', default => 'unknown' },
});
$options{options}->add_help(package => __PACKAGE__, sections => 'WSMAN OPTIONS');
#####
$self->{client} = undef;
$self->{output} = $options{output};
$self->{wsman_params} = {};
$self->{error_msg} = undef;
$self->{error_status} = 0;
return $self;
}
sub connect {
my ($self, %options) = @_;
if (!$self->{output}->is_litteral_status(status => $self->{wsman_errors_exit})) {
$self->{output}->add_option_msg(short_msg => "Unknown value '" . $self->{wsman_errors_exit} . "' for --wsman-errors-exit.");
$self->{output}->option_exit(exit_litteral => 'unknown');
}
openwsman::set_debug(1) if (defined($self->{wsman_params}->{wsman_debug});
$self->{client} = new openwsman::Client::($self->{wsman_params}->{host}, $self->{wsman_params}->{wsman_port},
$self->{wsman_params}->{wsman_path}, $self->{wsman_params}->{wsman_scheme},
$self->{wsman_params}->{wsman_username}, $self->{wsman_params}->{wsman_password});
if (!defined($self->{client})) {
$self->{output}->add_option_msg(short_msg => 'Could not create client handler');
$self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit});
}
$self->{client}->transport()->set_auth_method($auth_method_map{$self->{wsman_params}->{wsman_auth_method}});
$self->{client}->transport()->set_timeout($self->{wsman_params}->{wsman_timeout});
if (defined($self->{wsman_params}->{wsman_proxy_url})) {
$self->{client}->transport()->set_proxy($self->{wsman_params}->{wsman_proxy_url});
if (defined($self->{wsman_params}->{wsman_proxy_username}) && defined($self->{wsman_params}->{wsman_proxy_password})) {
$self->{client}->transport()->set_proxyauth($self->{wsman_params}->{wsman_proxy_username} . ':' . $self->{wsman_params}->{wsman_proxy_password});
}
}
}
sub request {
my ($self, %options) = @_;
# $options{nothing_quit} = integer
# $options{dont_quit} = integer
# $options{uri} = string
# $options{wql_fitler} = string
# $options{result_type} = string ('array' or 'hash' with a key)
# $options{hash_key} = string
my ($dont_quit) = (defined($options{dont_quit}) && $options{dont_quit} == 1) ? 1 : 0;
my ($nothing_quit) = (defined($options{nothing_quit}) && $options{nothing_quit} == 1) ? 1 : 0;
my ($result_type) = (defined($options{result_type}) && $options{result_type} =~ /^(array|hash)$/) ? $options{result_type} : 'array';
$self->set_error();
######
# Check options
if (!defined($options{uri}) || !defined($options{wql_fitler})) {
$self->{output}->add_option_msg(short_msg => 'Need to specify wql_filter and uri options');
$self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit});
}
######
# ClientOptions object
my $client_options = new openwsman::ClientOptions::();
if (!defined($client_options)) {
if ($dont_quit == 1) {
$self->set_error(error_status => -1, error_msg => 'Could not create client options handler');
return undef;
}
$self->{output}->add_option_msg(short_msg => 'Could not create client options handler');
$self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit});
}
# Optimization
$client_options->set_flags($openwsman::FLAG_ENUMERATION_OPTIMIZATION);
$client_options->set_max_elements(999);
######
# Filter/Enumerate
my $filter = new openwsman::Filter::();
if (!defined($filter)) {
if ($dont_quit == 1) {
$self->set_error(error_status => -1, error_msg => 'Could not create filter');
return undef;
}
$self->{output}->add_option_msg(short_msg => 'Could not create filter.');
$self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit});
}
$filter->wql($options{wql_fitler});
my $result = $self->{client}->enumerate($client_options, $filter, $options{uri});
unless($result && $result->is_fault eq 0) {
my $fault_string = $self->{client}->fault_string();
my $msg = 'Could not enumerate instances: ' . ((defined($fault_string)) ? $fault_string : 'use debug option to have details');
if ($dont_quit == 1) {
$self->set_error(error_status => -1, error_msg => $msg );
return undef;
}
$self->{output}->add_option_msg(short_msg => $msg);
$self->{output}->option_exit(exit_litteral => $self->{wsman_errors_exit});
}
######
# Fetch values
my ($array_return, $hash_return)
$array_return = [] if ($result_type eq 'array');
$hash_return = {} if ($result_type eq 'hash');
my $context = $result->context();
my $total = 0;
while ($context) {
# Pull from local server.
# (options, filter, resource uri, enum context)
$result = $self->{client}->pull($client_options, $filter, $options{uri}, $context);
next unless($result);
# Get nodes.
# soap body -> PullResponse -> items
my $nodes = $result->body()->find($openwsman::XML_NS_ENUMERATION, "Items");
next unless($nodes);
# Get items.
my $items;
for (my $cnt = 0; ($cnt<$nodes->size()); $cnt++) {
my $row_return = {};
for (my $cnt2 = 0; ($cnt2<$nodes->get($cnt)->size()); $cnt2++) {
$row_return->{$nodes->get($cnt)->get($cnt2)->name()} = $nodes->get($cnt)->get($cnt2)->text();
}
$total++;
push @{$array_return}, $row_return if ($result_type eq 'array');
$hash_return->{$row_return->{$options{hash_key}}} = $row_return if ($result_type eq 'hash');
}
$context = $result->context();
}
# Release context.
$self->{client}->release($client_options, $options{uri}, $context) if($context);
if ($nothing_quit == 1 && $total == 0) {
$self->{output}->add_option_msg(short_msg => "Cant get a single value.");
$self->{output}->option_exit(exit_litteral => $self->{option_results}->{wsman_errors_exit});
}
if ($result_type eq 'array') {
return $array_return;
}
return $hash_return;
}
sub check_options {
my ($self, %options) = @_;
# $options{option_results} = ref to options result
$self->{wsman_errors_exit} = $options{option_results}->{wsman_errors_exit};
if (!defined($options{option_results}->{host})) {
$self->{output}->add_option_msg(short_msg => "Missing parameter --hostname.");
$self->{output}->option_exit();
}
$self->{wsman_params}->{host} = $options{option_results}->{host};
if (!defined($options{option_results}->{wsman_scheme}) || $options{option_results}->{wsman_scheme} !~ /^(http|https)$/) {
$self->{output}->add_option_msg(short_msg => "Wrong scheme parameter. Must be 'http' or 'https'.");
$self->{output}->option_exit();
}
$self->{wsman_params}->{wsman_scheme} = $options{option_results}->{wsman_scheme};
if (!defined($options{option_results}->{wsman_auth_method}) || !defined($auth_method_map{$options{option_results}->{wsman_auth_method}})) {
$self->{output}->add_option_msg(short_msg => "Wrong wsman auth method parameter. Must be 'basic', 'noauth', 'digest', 'pass', 'ntml' or 'gssnegotiate'.");
$self->{output}->option_exit();
}
$self->{wsman_params}->{wsman_auth_method} = $options{option_results}->{wsman_auth_method};
if (!defined($options{option_results}->{wsman_port}) || $options{option_results}->{wsman_port} !~ /^([0-9]+)$/) {
$self->{output}->add_option_msg(short_msg => "Wrong wsman port parameter. Must be an integer.");
$self->{output}->option_exit();
}
$self->{wsman_params}->{wsman_port} = $options{option_results}->{wsman_port};
$self->{wsman_params}->{wsman_path} = $options{option_results}->{wsman_path};
$self->{wsman_params}->{wsman_username} = $options{option_results}->{wsman_username};
$self->{wsman_params}->{wsman_password} = $options{option_results}->{wsman_password};
$self->{wsman_params}->{wsman_timeout} = $options{option_results}->{wsman_timeout};
$self->{wsman_params}->{wsman_proxy_url} = $options{option_results}->{wsman_proxy_url};
$self->{wsman_params}->{wsman_proxy_username} = $options{option_results}->{wsman_proxy_username};
$self->{wsman_params}->{wsman_proxy_password} = $options{option_results}->{wsman_proxy_password};
$self->{wsman_params}->{wsman_debug} = $options{option_results}->{wsman_debug};
}
sub set_error {
my ($self, %options) = @_;
# $options{error_msg} = string error
# $options{error_status} = integer status
$self->{error_status} = defined($options{error_status}) ? $options{error_status} : 0;
$self->{error_msg} = defined($options{error_msg}) ? $options{error_msg} : undef;
}
sub error_status {
my ($self) = @_;
return $self->{error_status};
}
sub error {
my ($self) = @_;
return $self->{error_msg};
}
sub get_hostname {
my ($self) = @_;
my $host = $self->{wsman_params}->{host};
return $host;
}
sub get_port {
my ($self) = @_;
return $self->{wsman_params}->{wsman_port};
}
1;
__END__
=head1 NAME
WSMAN global
=head1 SYNOPSIS
wsman class
=head1 WSMAN OPTIONS
=over 8
Need at least openwsman-perl version >= 2.3.0
=item B<--hostname>
Hostname to query (required).
=item B<--wsman-port>
Port (default: 5985).
=item B<--wsman-path>
Set path of URL (default: '/wsman').
=item B<--wsman-scheme>
Set transport scheme (default: 'http').
=item B<--wsman-username>
Set username for authentification.
=item B<--wsman-password>
Set username password for authentification.
=item B<--wsman-timeout>
Set HTTP Transport Timeout in seconds (default: 30).
=item B<--wsman-auth-method>
Set the authentification method (default: 'basic').
=item B<--wsman-proxy-url>
Set HTTP proxy URL.
=item B<--wsman-proxy-username>
Set the proxy username.
=item B<--wsman-proxy-password>
Set the proxy password.
=item B<--wsman-debug>
Set openwsman debug on (Only for test purpose).
=item B<--wsman-errors-exit>
Exit code for wsman Errors (default: unknown)
=back
=head1 DESCRIPTION
B<wsman>.
=cut