+ update hyperv modes

This commit is contained in:
garnier-quentin 2017-05-23 10:52:07 +02:00
parent ee1b2d3ead
commit 1f57b2301d
8 changed files with 298 additions and 27 deletions

View File

@ -0,0 +1,133 @@
#
# Copyright 2017 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 apps::hyperv::2012::local::mode::listnodevms;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use centreon::plugins::misc;
use centreon::common::powershell::hyperv::2012::listnodevms;
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 =>
{
"timeout:s" => { name => 'timeout', default => 50 },
"command:s" => { name => 'command', default => 'powershell.exe' },
"command-path:s" => { name => 'command_path' },
"command-options:s" => { name => 'command_options', default => '-InputFormat none -NoLogo -EncodedCommand' },
"no-ps" => { name => 'no_ps', },
"ps-exec-only" => { name => 'ps_exec_only', },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
}
sub run {
my ($self, %options) = @_;
my $ps = centreon::common::powershell::hyperv::2012::listnodevms::get_powershell(no_ps => $self->{option_results}->{no_ps});
$self->{option_results}->{command_options} .= " " . $ps;
my ($stdout) = centreon::plugins::misc::execute(output => $self->{output},
options => $self->{option_results},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options});
if (defined($self->{option_results}->{ps_exec_only})) {
$self->{output}->output_add(severity => 'OK',
short_msg => $stdout);
} else {
$self->{output}->output_add(severity => 'OK',
short_msg => 'List Virtual Machines:');
centreon::common::powershell::hyperv::2012::listnodevms::list($self, stdout => $stdout);
}
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['name', 'state', 'status', 'is_clustered', 'note']);
}
sub disco_show {
my ($self, %options) = @_;
my $ps = centreon::common::powershell::hyperv::2012::listnodevms::get_powershell(no_ps => $self->{option_results}->{no_ps});
$self->{option_results}->{command_options} .= " " . $ps;
my ($stdout) = centreon::plugins::misc::execute(output => $self->{output},
options => $self->{option_results},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options});
centreon::common::powershell::hyperv::2012::listnodevms::disco_show($self, stdout => $stdout);
}
1;
__END__
=head1 MODE
List virtual machines on hyper-v node.
=over 8
=item B<--timeout>
Set timeout time for command execution (Default: 50 sec)
=item B<--no-ps>
Don't encode powershell. To be used with --command and 'type' command.
=item B<--command>
Command to get information (Default: 'powershell.exe').
Can be changed if you have output in a file. To be used with --no-ps option!!!
=item B<--command-path>
Command path (Default: none).
=item B<--command-options>
Command options (Default: '-InputFormat none -NoLogo -EncodedCommand').
=item B<--ps-exec-only>
Print powershell output.
=back
=cut

View File

@ -31,25 +31,38 @@ sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'vm', type => 1, cb_prefix_output => 'prefix_vm_output', message_multiple => 'All VM snapshots are ok' },
{ name => 'vm', type => 1, cb_prefix_output => 'prefix_vm_output', message_multiple => 'All VM snapshots are ok', skipped_code => { -10 => 1 } },
];
$self->{maps_counters}->{vm} = [
{ label => 'snapshot', set => {
key_values => [ { name => 'snapshot' }, { name => 'display' }],
closure_custom_output => $self->can('custom_vm_output'),
closure_custom_output => $self->can('custom_snapshot_output'),
closure_custom_perfdata => sub { return 0; },
}
},
{ label => 'backing', set => {
key_values => [ { name => 'backing' }, { name => 'display' }],
closure_custom_output => $self->can('custom_backing_output'),
closure_custom_perfdata => sub { return 0; },
}
},
];
}
sub custom_vm_output {
sub custom_snapshot_output {
my ($self, %options) = @_;
my $msg = 'checkpoint started since : ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{snapshot_absolute});
return $msg;
}
sub custom_backing_output {
my ($self, %options) = @_;
my $msg = 'backing started since : ' . centreon::plugins::misc::change_seconds(value => $self->{result_values}->{backing_absolute});
return $msg;
}
sub prefix_vm_output {
my ($self, %options) = @_;
@ -96,18 +109,18 @@ sub manage_selection {
}
#[name= ISC1-SV04404 ][state= Running ][note= ]
#[checkpointCreationTime= 1475502921.28734 ]
#[checkpointCreationTime= 1475503073.81975 ]
#[checkpointCreationTime= 1475502921.28734 ][type= snapshot]
#[checkpointCreationTime= 1475503073.81975 ][type= backing]
$self->{vm} = {};
my $id = 1;
while ($stdout =~ /^\[name=\s*(.*?)\s*\]\[state=\s*(.*?)\s*\]\[note=\s*(.*?)\s*\](.*?)(?=\[name=|\z)/msig) {
my ($name, $status, $note, $content) = ($1, $2, $3, $4);
my $chkpt = -1;
while ($content =~ /\[checkpointCreationTime=s*(.*?)\s*\]/msig) {
$chkpt = $1 if ($chkpt == -1 || $chkpt > $1);
my %chkpt = (backing => -1, snapshot => -1);
while ($content =~ /\[checkpointCreationTime=\s*(.*?)\s*\]\[type=\s*(.*?)\s*\]/msig) {
$chkpt{$2} = $1 if ($chkpt{$2} > $1);
}
next if ($chkpt == -1);
next if ($chkpt{backing} == -1 && $chkpt{snapshot} == -1);
if (defined($self->{option_results}->{filter_vm}) && $self->{option_results}->{filter_vm} ne '' &&
$name !~ /$self->{option_results}->{filter_vm}/i) {
@ -125,7 +138,9 @@ sub manage_selection {
next;
}
$self->{vm}->{$id} = { display => $name, snapshot => time() - $chkpt };
$self->{vm}->{$id} = { display => $name,
snapshot => $chkpt{snapshot} > 0 ? $time() - $chkpt{snapshot} : undef,
backing => $chkpt{backing} > 0 ? $time() - $chkpt{backing} : undef };
$id++;
}
}
@ -180,12 +195,12 @@ Filter by VM notes (can be a regexp).
=item B<--warning-*>
Threshold warning.
Can be: 'snapshot' (in seconds).
Can be: 'snapshot' (in seconds), 'backing' (in seconds).
=item B<--critical-*>
Threshold critical.
Can be: 'snapshot' (in seconds).
Can be: 'snapshot' (in seconds), 'backing' (in seconds).
=back

View File

@ -66,6 +66,11 @@ sub custom_status_calc {
$self->{result_values}->{vm} = $options{new_datas}->{$self->{instance} . '_vm'};
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'};
$self->{result_values}->{vmaddition} = $options{new_datas}->{$self->{instance} . '_vmaddition'};
$self->{result_values}->{operatingsystemshutdownenabled} = $options{new_datas}->{$self->{instance} . '_operatingsystemshutdownenabled'};
$self->{result_values}->{timesynchronizationenabled} = $options{new_datas}->{$self->{instance} . '_timesynchronizationenabled'};
$self->{result_values}->{dataexchangeenabled} = $options{new_datas}->{$self->{instance} . '_dataexchangeenabled'};
$self->{result_values}->{heartbeatenabled} = $options{new_datas}->{$self->{instance} . '_heartbeatenabled'};
$self->{result_values}->{backupenabled} = $options{new_datas}->{$self->{instance} . '_backupenabled'};
return 0;
}
@ -77,7 +82,9 @@ sub set_counters {
];
$self->{maps_counters}->{vm} = [
{ label => 'snapshot', set => {
key_values => [ { name => 'vm' }, { name => 'status' }, { name => 'vmaddition' } ],
key_values => [ { name => 'vm' }, { name => 'status' }, { name => 'vmaddition' },
{ name => 'operatingsystemshutdownenabled' }, { name => 'timesynchronizationenabled' },
{ name => 'dataexchangeenabled' }, { name => 'heartbeatenabled' }, { name => 'backupenabled' } ],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
@ -171,16 +178,20 @@ sub manage_selection {
$self->{output}->exit();
}
#[name= test1 ][description= Test Descr - - pp - - aa ][status= Running ][cloud= ][hostgrouppath= All Hosts\CORP\test1 ]][VMAddition= 6.3.9600.16384 ]
#[name= test2 ][description= ][status= HostNotResponding ][cloud= ][hostgrouppath= All Hosts\CORP\test2 ]][VMAddition= Not Detected ]
#[name= test3 ][description= ][status= HostNotResponding ][cloud= ][hostgrouppath= All Hosts\CORP\test3 ]][VMAddition= Not Detected ]
#[name= test4 ][description= ][status= HostNotResponding ][cloud= ][hostgrouppath= All Hosts\CORP\test4 ]][VMAddition= Not Detected ]
#[VM= test1 ][Description= Test Descr - - pp - - aa ][Status= Running ][Cloud= ][HostGroup= All Hosts\CORP\test1 ][VMAddition= 6.3.9600.16384 ]
#[VM= test2 ][Description= ][Status= HostNotResponding ][Cloud= ][HostGroup= All Hosts\CORP\test2 ][VMAddition= Not Detected ]
#[VM= test3 ][Description= ][Status= HostNotResponding ][Cloud= ][HostGroup= All Hosts\CORP\test3 ][VMAddition= Not Detected ]
#[VM= test4 ][Description= ][Status= HostNotResponding ][Cloud= ][HostGroup= All Hosts\CORP\test4 ][VMAddition= Not Detected ]
$self->{vm} = {};
my $id = 1;
while ($stdout =~ /^\[name=\s*(.*?)\s*\]\[description=\s*(.*?)\s*\]\[status=\s*(.*?)\s*\]\[cloud=\s*(.*?)\s*\]\[hostgrouppath=\s*(.*?)\s*\]\[VMAddition=\s*(.*?)\s*\]/msig) {
my %values = (vm => $1, description => $2, status => $3, cloud => $4, hostgroup => $5, vmaddition => $6);
my @lines = split /\n/, $stdout;
foreach my $line (@lines) {
my %values;
while ($line =~ /\[(.*?)=\s*(.*?)\s*\]/g) {
$values{lc($1)} = $2;
}
$values{hostgroup} =~ s/\\/\//g;
my $filtered = 0;
foreach (('vm', 'description', 'status', 'hostgroup')) {

View File

@ -129,7 +129,7 @@ sub manage_selection {
my $content = $6;
my $chkpt = -1;
while ($content =~ /\[checkpointAddedTime=s*(.*?)\s*\]/msig) {
while ($content =~ /\[checkpointAddedTime=\s*(.*?)\s*\]/msig) {
$chkpt = $1 if ($chkpt == -1 || $chkpt > $1);
}
next if ($chkpt == -1);

View File

@ -31,13 +31,14 @@ sub new {
$self->{version} = '0.1';
%{$self->{modes}} = (
'scvmm-integration-service' => 'apps::hyperv::2012::local::mode::scvmmintegrationservice',
'scvmm-snapshot' => 'apps::hyperv::2012::local::mode::scvmmsnapshot',
'scvmm-vm-status' => 'apps::hyperv::2012::local::mode::scvmmvmstatus',
'list-node-vms' => 'apps::hyperv::2012::local::mode::listnodevms',
'node-integration-service' => 'apps::hyperv::2012::local::mode::nodeintegrationservice',
'node-replication' => 'apps::hyperv::2012::local::mode::nodereplication',
'node-snapshot' => 'apps::hyperv::2012::local::mode::nodesnapshot',
'node-vm-status' => 'apps::hyperv::2012::local::mode::nodevmstatus',
'scvmm-integration-service' => 'apps::hyperv::2012::local::mode::scvmmintegrationservice',
'scvmm-snapshot' => 'apps::hyperv::2012::local::mode::scvmmsnapshot',
'scvmm-vm-status' => 'apps::hyperv::2012::local::mode::scvmmvmstatus',
);
return $self;

View File

@ -0,0 +1,101 @@
#
# Copyright 2017 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::common::powershell::hyperv::2012::listnodevms;
use strict;
use warnings;
use centreon::plugins::misc;
sub get_powershell {
my (%options) = @_;
my $no_ps = (defined($options{no_ps})) ? 1 : 0;
return '' if ($no_ps == 1);
my $ps = '
$culture = new-object "System.Globalization.CultureInfo" "en-us"
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
$ProgressPreference = "SilentlyContinue"
Try {
$ErrorActionPreference = "Stop"
$vms = Get-VM
Foreach ($vm in $vms) {
$note = $vm.Notes -replace "\r",""
$note = $note -replace "\n"," - "
Write-Host "[name=" $vm.VMName "][state=" $vm.State "][status=" $vm.Status "][IsClustered=" $vm.IsClustered "][note=" $note "]"
}
} Catch {
Write-Host $Error[0].Exception
exit 1
}
exit 0
';
return centreon::plugins::misc::powershell_encoded($ps);
}
sub list {
my ($self, %options) = @_;
# Following output:
#[name= XXXX1 ][state= Running ][status= Operating normally ][IsClustered= True ][note= ]
#...
foreach my $line (split /\n/, $options{stdout}) {
next if ($line !~ /^\[name=(.*?)\]\[state=(.*?)\]\[status=(.*?)\]\[IsClustered=(.*?)\]\[note=(.*?)\]/);
my ($name, $state, $status, $IsClustered, $note) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2),
centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5));
$self->{output}->output_add(long_msg => "'" . $name . "' [state = $state, status = " . $status . ']');
}
}
sub disco_show {
my ($self, %options) = @_;
# Following output:
#[name= XXXX1 ][state= Running ][status= Operating normally ][IsClustered= True ][note= ]
#...
foreach my $line (split /\n/, $options{stdout}) {
next if ($line !~ /^\[name=(.*?)\]\[state=(.*?)\]\[status=(.*?)\]\[IsClustered=(.*?)\]\[note=(.*?)\]/);
my ($name, $state, $status, $IsClustered, $note) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2),
centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5));
$self->{output}->add_disco_entry(name => $name,
state => $state,
status => $status, is_clustered => $IsClustered, note => $note);
}
}
1;
__END__
=head1 DESCRIPTION
Method to get hyper-v informations.
=cut

View File

@ -51,7 +51,7 @@ Try {
if ($i -eq 0) {
Write-Host "[name=" $vm.VMName "][state=" $vm.State "][note=" $note "]"
}
Write-Host "[checkpointCreationTime=" (get-date -date $snap.CreationTime.ToUniversalTime() -UFormat ' . "'%s'" . ') "]"
Write-Host "[checkpointCreationTime=" (get-date -date $snap.CreationTime.ToUniversalTime() -UFormat ' . "'%s'" . ') "][type= snapshot]"
$i=1
}
}
@ -64,7 +64,7 @@ Try {
if ($i -eq 0) {
Write-Host "[name=" $vm.VMName "][state=" $vm.State "][note=" $note "]"
}
Write-Host "[checkpointCreationTime=" (get-date -date $parent.LastWriteTime.ToUniversalTime() -UFormat ' . "'%s'" . ') "]"
Write-Host "[checkpointCreationTime=" (get-date -date $parent.LastWriteTime.ToUniversalTime() -UFormat ' . "'%s'" . ') "][type= backing]"
$i=1
}
}

View File

@ -49,7 +49,17 @@ Try {
Foreach ($vm in $vms) {
$desc = $vm.description -replace "\r",""
$desc = $desc -replace "\n"," - "
Write-Host "[name=" $vm.Name "][description=" $desc "][status=" $vm.Status "][cloud=" $vm.Cloud "][hostgrouppath=" $vm.HostGroupPath "]][VMAddition=" $vm.VMAddition "]"
Write-Host ("[VM={0}]" -f $vm.Name) -NoNewline
Write-Host ("[Description={0}]" -f $desc) -NoNewline
Write-Host ("[Status={0}]" -f $vm.Status) -NoNewline
Write-Host ("[Cloud={0}]" -f $vm.Cloud) -NoNewline
Write-Host ("[HostGroup={0}]" -f $vm.HostGroupPath) -NoNewline
Write-Host ("[VMAddition={0}]" -f $vm.VMAddition) -NoNewline
Write-Host ("[OperatingSystemShutdownEnabled={0}]" -f $vm.OperatingSystemShutdownEnabled) -NoNewline
Write-Host ("[TimeSynchronizationEnabled={0}]" -f $vm.TimeSynchronizationEnabled) -NoNewline
Write-Host ("[DataExchangeEnabled={0}]" -f $vm.DataExchangeEnabled) -NoNewline
Write-Host ("[HeartbeatEnabled={0}]" -f $vm.HeartbeatEnabled) -NoNewline
Write-Host ("[BackupEnabled={0}]" -f $vm.BackupEnabled)
}
} Catch {
Write-Host $Error[0].Exception