From 366061fbd8241612e19e5221700e30fdbf36eb4b Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Wed, 12 Feb 2014 13:47:23 +0100 Subject: [PATCH] Fix #5186 Some fix on wsman management --- centreon/plugins/wsman.pm | 24 ++- os/windows/snmp/mode/service.pm | 2 +- os/windows/wsman/mode/listservices.pm | 2 +- os/windows/wsman/mode/service.pm | 220 ++++++++++++++++++++++++++ os/windows/wsman/plugin.pm | 3 +- 5 files changed, 234 insertions(+), 17 deletions(-) create mode 100644 os/windows/wsman/mode/service.pm diff --git a/centreon/plugins/wsman.pm b/centreon/plugins/wsman.pm index 7b6aef03a..7e56d9f0a 100644 --- a/centreon/plugins/wsman.pm +++ b/centreon/plugins/wsman.pm @@ -330,21 +330,12 @@ sub request { $array_return = [] if ($result_type eq 'array'); $hash_return = {} if ($result_type eq 'hash'); - my $context = $result->context(); + my $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); - + while (1) { + my $nodes = $result->body()->find(undef, "Items"); + # Get items. my $items; for (my $cnt = 0; ($cnt<$nodes->size()); $cnt++) { @@ -356,7 +347,12 @@ sub request { 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(); + + $context = $result->context() + or last; + $result = $self->{client}->pull($client_options, $filter, $options{uri}, $context) + or last; + } # Release context. diff --git a/os/windows/snmp/mode/service.pm b/os/windows/snmp/mode/service.pm index ebcba5249..12aaa23f9 100644 --- a/os/windows/snmp/mode/service.pm +++ b/os/windows/snmp/mode/service.pm @@ -103,7 +103,7 @@ sub run { my %services_match = (); $self->{output}->output_add(severity => 'OK', - short_msg => 'All service states is ok'); + short_msg => 'All service states are ok'); foreach my $oid ($self->{snmp}->oid_lex_sort(keys %$result)) { next if ($oid !~ /^$oid_svSvcName/); $oid =~ /^$oid_svSvcName\.([0-9\.]+)$/; diff --git a/os/windows/wsman/mode/listservices.pm b/os/windows/wsman/mode/listservices.pm index 047e12383..6be781286 100644 --- a/os/windows/wsman/mode/listservices.pm +++ b/os/windows/wsman/mode/listservices.pm @@ -89,7 +89,7 @@ sub run { foreach my $name (sort(keys %{$self->{result}})) { $services_display .= $services_display_append . 'name = ' . $name . - '[DisplayName = ' . $self->{output}->to_utf8($self->{result}->{$name}->{DisplayName}) . ',' . + ' [DisplayName = ' . $self->{output}->to_utf8($self->{result}->{$name}->{DisplayName}) . ',' . 'StartMode = ' . $self->{result}->{$name}->{StartMode} . ',' . 'State = ' . $self->{result}->{$name}->{State} . ']'; diff --git a/os/windows/wsman/mode/service.pm b/os/windows/wsman/mode/service.pm new file mode 100644 index 000000000..74532a0b9 --- /dev/null +++ b/os/windows/wsman/mode/service.pm @@ -0,0 +1,220 @@ +################################################################################ +# 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 os::windows::wsman::mode::service; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +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" => { name => 'warning', }, + "critical" => { name => 'critical', }, + "services:s" => { name => 'services', }, + "auto" => { name => 'auto', }, + "exclude:s" => { name => 'exclude', }, + }); + $self->{service_rules} = {}; + $self->{wql_filter} = ''; + $self->{threshold} = 'CRITICAL'; + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (!defined($self->{option_results}->{services}) && !defined($self->{option_results}->{auto})) { + $self->{output}->add_option_msg(short_msg => "Need to specify at least '--services' or '--auto' option."); + $self->{output}->option_exit(); + } + + if (defined($self->{option_results}->{services})) { + my $append = ''; + foreach my $rule (split /,/, $self->{option_results}->{services}) { + if ($rule !~ /^([^\!=]*)(\!=|=){0,1}(.*){0,1}/) { + $self->{output}->add_option_msg(short_msg => "Wrong rule in --services option: " . $rule); + $self->{output}->option_exit(); + } + if (!defined($1) || $1 eq '') { + $self->{output}->add_option_msg(short_msg => "Need service name for rule: " . $rule); + $self->{output}->option_exit(); + } + + my $sname = $1; + my $operator = defined($2) && $2 ne '' ? $2 : '!='; + my $state = defined($3) && $3 ne '' ? lc($3) : 'running'; + + if ($operator !~ /^(=|\!=)$/) { + $self->{output}->add_option_msg(short_msg => "Wrong operator for rule: " . $rule . ". Should be '=' or '!='."); + $self->{output}->option_exit(); + } + + if ($state !~ /^(stopped|start pending|stop pending|running|continue pending|pause pending|paused|unknown)$/i) { + $self->{output}->add_option_msg(short_msg => "Wrong state for rule: " . $rule . ". See help for available state."); + $self->{output}->option_exit(); + } + + $self->{service_rules}->{$sname} = {operator => $operator, state => $state}; + $self->{wql_filter} .= $append . "Name = '" . $sname . "'"; + $append = ' Or '; + } + + if ($self->{wql_filter} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify one rule for --services option."); + $self->{output}->option_exit(); + } + } + + $self->{threshold} = 'WARNING' if (defined($self->{option_results}->{warning})); + $self->{threshold} = 'CRITICAL' if (defined($self->{option_results}->{critical})); +} + +sub check_auto { + my ($self, %options) = @_; + + $self->{result} = $self->{wsman}->request(uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', + wql_filter => "Select Name, State From Win32_Service Where StartMode = 'Auto'", + result_type => 'hash', + hash_key => 'Name'); + foreach my $name (sort(keys %{$self->{result}})) { + if (defined($self->{option_results}->{exclude}) && $self->{result}->{$name}->{Name} =~ /$self->{option_results}->{exclude}/) { + $self->{output}->output_add(long_msg => "Skipping Service '" . $self->{result}->{$name}->{Name} . "'"); + next; + } + + $self->{output}->output_add(long_msg => "Service '" . $self->{result}->{$name}->{Name} . "' state: " . $self->{result}->{$name}->{State}); + if ($self->{result}->{$name}->{State} !~ /^running$/i) { + $self->{output}->output_add(severity => $self->{threshold}, + short_msg => "Service '" . $self->{result}->{$name}->{Name} . "' is " . $self->{result}->{$name}->{State}); + } + } +} + +sub check { + my ($self, %options) = @_; + + $self->{result} = $self->{wsman}->request(uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', + wql_filter => 'Select Name, State From Win32_Service Where ' . $self->{wql_filter}, + result_type => 'hash', + hash_key => 'Name'); + foreach my $name (sort(keys %{$self->{service_rules}})) { + if (!defined($self->{result}->{$name})) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Service '" . $name . "' not found"); + next; + } + + $self->{output}->output_add(long_msg => "Service '" . $name . "' state: " . $self->{result}->{$name}->{State}); + if ($self->{service_rules}->{$name}->{operator} eq '=' && + lc($self->{result}->{$name}->{State}) eq $self->{service_rules}->{$name}->{state}) { + $self->{output}->output_add(severity => $self->{threshold}, + short_msg => "Service '" . $self->{result}->{$name}->{Name} . "' is " . $self->{result}->{$name}->{State}); + } elsif ($self->{service_rules}->{$name}->{operator} eq '!=' && + lc($self->{result}->{$name}->{State}) ne $self->{service_rules}->{$name}->{state}) { + $self->{output}->output_add(severity => $self->{threshold}, + short_msg => "Service '" . $self->{result}->{$name}->{Name} . "' is " . $self->{result}->{$name}->{State}); + } + } +} + +sub run { + my ($self, %options) = @_; + # $options{wsman} = wsman object + $self->{wsman} = $options{wsman}; + + $self->{output}->output_add(severity => 'OK', + short_msg => 'All service states are ok'); + if (defined($self->{option_results}->{auto})) { + $self->check_auto(); + } else { + $self->check(); + } + + $self->{output}->display(); + $self->{output}->exit(); +} + +1; + +__END__ + +=head1 MODE + +Check Windows Services. + +=over 8 + +=item B<--warning> + +Return warning. + +=item B<--critical> + +Return critical. + +=item B<--services> + +Services to monitor. +Syntax: [service_name[[=|!=]state]],... +Available states are: +- Stopped +- Start Pending +- Stop Pending +- Running +- Continue Pending +- Pause Pending +- Paused +- Unknown + +=item B<--auto> + +Return threshold for auto services not running. + +=item B<--exclude> + +Exclude some services for --auto option (Can be a regexp). + +=back + +=cut \ No newline at end of file diff --git a/os/windows/wsman/plugin.pm b/os/windows/wsman/plugin.pm index 2a9150fe8..de41df7e8 100644 --- a/os/windows/wsman/plugin.pm +++ b/os/windows/wsman/plugin.pm @@ -47,7 +47,8 @@ sub new { $self->{version} = '0.1'; %{$self->{modes}} = ( - 'list-services' => 'os::windows::wsman::mode::listservices', + 'list-services' => 'os::windows::wsman::mode::listservices', + 'service' => 'os::windows::wsman::mode::service', ); return $self;