Some improvements

git-svn-id: http://svn.merethis.net/centreon-esxd/trunk@87 a5eaa968-4c79-4d68-970d-af6011b5b055
This commit is contained in:
Quentin Garnier 2013-10-07 19:14:39 +00:00
parent bf74e6a0f0
commit 600cfbc200
3 changed files with 50 additions and 11 deletions

View File

@ -183,8 +183,10 @@ sub print_usage () {
print "\n";
print "'countvmhost':\n";
print " -e (--esx-host) Esx Host to check (required)\n";
print " -w (--warning) Warning Threshold (default none)\n";
print " -c (--critical) Critical Threshold (default none)\n";
print " -w (--warning) Warning Threshold if more VMs on (default none)\n";
print " -c (--critical) Critical Threshold if more VMs on (default none)\n";
print " --warning2 Warning Threshold if more VMs not on (default none)\n";
print " --critical2 Critical Threshold if more VMs not on (default none)\n";
print "\n";
print "'uptimehost':\n";
print " -e (--esx-host) Esx Host to check (required)\n";
@ -564,12 +566,18 @@ sub countvmhost_check_arg {
if (!defined($OPTION{critical})) {
$OPTION{critical} = '';
}
if (!defined($OPTION{warning2})) {
$OPTION{warning2} = '';
}
if (!defined($OPTION{critical2})) {
$OPTION{critical2} = '';
}
return 0;
}
sub countvmhost_get_str {
return join($separatorin,
('countvmhost', $OPTION{vsphere}, $OPTION{'esx-host'}, $OPTION{warning}, $OPTION{critical}));
('countvmhost', $OPTION{vsphere}, $OPTION{'esx-host'}, $OPTION{warning}, $OPTION{critical}, $OPTION{warning2}, $OPTION{critical2}));
}
sub uptimehost_check_arg {

View File

@ -23,7 +23,7 @@ sub getCommandName {
sub checkArgs {
my $self = shift;
my ($lhost, $warn, $crit) = @_;
my ($lhost, $warn, $crit, $warn2, $crit2) = @_;
if (!defined($lhost) || $lhost eq "") {
$self->{logger}->writeLogError("ARGS error: need host name");
@ -41,6 +41,18 @@ sub checkArgs {
$self->{logger}->writeLogError("ARGS error: warn threshold must be lower than crit threshold");
return 1;
}
if (defined($warn2) && $warn2 ne "" && $warn2 !~ /^-?(?:\d+\.?|\.\d)\d*\z/) {
$self->{logger}->writeLogError("ARGS error: warn2 threshold must be a positive number");
return 1;
}
if (defined($crit2) && $crit2 ne "" && $crit2 !~ /^-?(?:\d+\.?|\.\d)\d*\z/) {
$self->{logger}->writeLogError("ARGS error: crit2 threshold must be a positive number");
return 1;
}
if (defined($warn2) && defined($crit2) && $warn2 ne "" && $crit2 ne "" && $warn2 > $crit2) {
$self->{logger}->writeLogError("ARGS error: warn2 threshold must be lower than crit2 threshold");
return 1;
}
return 0;
}
@ -49,6 +61,8 @@ sub initArgs {
$self->{lhost} = $_[0];
$self->{warn} = (defined($_[1]) ? $_[1] : '');
$self->{crit} = (defined($_[2]) ? $_[2] : '');
$self->{warn2} = (defined($_[3]) ? $_[3] : '');
$self->{crit2} = (defined($_[4]) ? $_[4] : '');
}
sub run {
@ -78,25 +92,38 @@ sub run {
my $output = '';
my $status = 0; # OK
my $num_poweron = 0;
my $num_poweron = 0;
my $num_powerother = 0;
foreach (@$result) {
my $power_value = lc($_->{'runtime.powerState'}->val);
if ($power_value eq 'poweredon') {
$num_poweron++;
} else {
$num_powerother++;
}
}
if (defined($self->{crit}) && $self->{crit} ne "" && ($num_poweron >= $self->{crit})) {
$output = "CRITICAL: $num_poweron VM running.";
$output = "CRITICAL: $num_poweron VM running";
$status = centreon::esxd::common::errors_mask($status, 'CRITICAL');
} elsif (defined($self->{warn}) && $self->{warn} ne "" && ($num_poweron >= $self->{warn})) {
$output = "WARNING: $num_poweron VM running.";
$output = "WARNING: $num_poweron VM running";
$status = centreon::esxd::common::errors_mask($status, 'WARNING');
} else {
$output = "OK: $num_poweron VM running.";
$output .= "OK: $num_poweron VM running";
}
$self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output|count=$num_poweron\n");
if (defined($self->{crit2}) && $self->{crit2} ne "" && ($num_powerother >= $self->{crit2})) {
$output .= " - CRITICAL: $num_powerother VM not running.";
$status = centreon::esxd::common::errors_mask($status, 'CRITICAL');
} elsif (defined($self->{warn2}) && $self->{warn2} ne "" && ($num_powerother >= $self->{warn2})) {
$output .= " - WARNING: $num_powerother VM not running.";
$status = centreon::esxd::common::errors_mask($status, 'WARNING');
} else {
$output .= " - OK: $num_powerother VM not running.";
}
$self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output|count_on=$num_poweron;$self->{warn};$self->{crit};0; count_not_on=$num_powerother;$self->{warn2};$self->{crit2};0;\n");
}
1;

View File

@ -94,6 +94,7 @@ sub run {
my $output_ok_unit = 'Snapshot(s) OK';
my $consolidate_vms = '';
my $consolidate_vms_append = '';
my ($num_warning, $num_critical) = (0, 0);
foreach my $virtual (@$result) {
if (!centreon::esxd::common::is_connected($virtual->{'runtime.connectionState'}->val)) {
@ -133,16 +134,19 @@ sub run {
centreon::esxd::common::output_add(\$output_critical, \$output_critical_append, ", ",
"[" . $virtual->{'name'}. "]");
$status = centreon::esxd::common::errors_mask($status, 'CRITICAL');
$num_critical++;
last;
} elsif (time() - $create_time >= $self->{warning}) {
centreon::esxd::common::output_add(\$output_warning, \$output_warning_append, ", ",
"[" . $virtual->{'name'}. "]");
$status = centreon::esxd::common::errors_mask($status, 'WARNING');
$num_warning++;
last;
}
}
}
my $perfdata = 'num_warning=' . $num_warning . ' num_critical=' . $num_critical;
if ($output_unknown ne "") {
$output .= $output_append . "UNKNOWN - $output_unknown";
$output_append = ". ";
@ -166,7 +170,7 @@ sub run {
}
}
$self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output\n");
$self->{obj_esxd}->print_response(centreon::esxd::common::get_status($status) . "|$output|$perfdata\n");
}
1;