[vm] CPU check


git-svn-id: http://svn.merethis.net/centreon-esxd/trunk@16 a5eaa968-4c79-4d68-970d-af6011b5b055
This commit is contained in:
Quentin Garnier 2012-09-20 15:21:45 +00:00
parent 9d0199eac5
commit bfb5688ddb
4 changed files with 108 additions and 2 deletions

View File

@ -34,6 +34,7 @@ GetOptions(
"u|usage=s" => \$OPTION{'usage'},
"e|esx-host=s" => \$OPTION{'esx-host'},
"vm=s" => \$OPTION{'vm'},
"datastore=s" => \$OPTION{'datastore'},
"nic=s" => \$OPTION{'nic'},
@ -110,6 +111,11 @@ sub print_usage () {
print " -w (--warning) Warning Threshold in ms (latency) (default none)\n";
print " -c (--critical) Critical Threshold in ms (latency) (default none)\n";
print "\n";
print "'cpuvm':\n";
print " --vm VM to check (required)\n";
print " -w (--warning) Warning Threshold in percent (default 80)\n";
print " -c (--critical) Critical Threshold in percent (default 90)\n";
print "\n";
print "'listhost':\n";
print " None\n";
print "\n";
@ -330,6 +336,25 @@ sub nethost_get_str {
return "nethost|" . $OPTION{'esx-host'} . "|" . $OPTION{'nic'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
}
sub cpuvm_check_arg {
if (!defined($OPTION{'vm'})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 80;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 90;
}
return 0;
}
sub cpuvm_get_str {
return "cpuvm|" . $OPTION{'vm'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
}
sub listhost_check_arg {
return 0;
}
@ -384,7 +409,7 @@ if (!defined($OPTION{'usage'})) {
print_usage();
exit $ERRORS{'UNKNOWN'};
}
if ($OPTION{'usage'} !~ /^(healthhost|datastore-usage|datastore-io|maintenancehost|statushost|cpuhost|datastoreshost|nethost|memhost|swaphost|listhost|listdatastore|listnichost|getmap)$/) {
if ($OPTION{'usage'} !~ /^(healthhost|datastore-usage|datastore-io|maintenancehost|statushost|cpuhost|datastoreshost|nethost|memhost|swaphost|cpuvm|listhost|listdatastore|listnichost|getmap)$/) {
print "Usage value is unknown\n";
print_usage();
exit $ERRORS{'UNKNOWN'};

View File

@ -30,6 +30,7 @@ use constant {
require '/etc/centreon/centreon_esxd.pm';
require $libpath . '/esxd-common.pm';
require $libpath . '/command-cpuhost.pm';
require $libpath . '/command-cpuvm.pm';
require $libpath . '/command-datastoreio.pm';
require $libpath . '/command-datastoreshost.pm';
require $libpath . '/command-datastoreusage.pm';
@ -83,6 +84,7 @@ our %checks_descr = (
"datastoreshost" => {'arg' => \&datastoreshost_check_args, 'compute' => \&datastoreshost_compute_args, 'exec' => \&datastoreshost_do},
"memhost" => {'arg' => \&memhost_check_args, 'compute' => \&memhost_compute_args, 'exec' => \&memhost_do},
"swaphost" => {'arg' => \&swaphost_check_args, 'compute' => \&swaphost_compute_args, 'exec' => \&swaphost_do},
"cpuvm" => {'arg' => \&cpuvm_check_args, 'compute' => \&cpuvm_compute_args, 'exec' => \&cpuvm_do},
"listhost" => {'arg' => \&listhost_check_args, 'compute' => \&listhost_compute_args, 'exec' => \&listhost_do},
"listdatastore" => {'arg' => \&listdatastore_check_args, 'compute' => \&listdatastore_compute_args, 'exec' => \&listdatastore_do},
"listnichost" => {'arg' => \&listnichost_check_args, 'compute' => \&listnichost_compute_args, 'exec' => \&listnichost_do},

View File

@ -35,7 +35,7 @@ sub cpuhost_do {
}
my %filters = ('name' => $lhost);
my @properties = ('hardware.cpuInfo.numCpuThreads');
my @properties = ('name');
my $result = get_entities_host('HostSystem', \%filters, \@properties);
if (!defined($result)) {
return ;

View File

@ -0,0 +1,79 @@
sub cpuvm_check_args {
my ($vm, $warn, $crit) = @_;
if (!defined($vm) || $vm eq "") {
writeLogFile(LOG_ESXD_ERROR, "ARGS error: need vm hostname\n");
return 1;
}
if (defined($warn) && $warn !~ /^-?(?:\d+\.?|\.\d)\d*\z/) {
writeLogFile(LOG_ESXD_ERROR, "ARGS error: warn threshold must be a positive number\n");
return 1;
}
if (defined($crit) && $crit !~ /^-?(?:\d+\.?|\.\d)\d*\z/) {
writeLogFile(LOG_ESXD_ERROR, "ARGS error: crit threshold must be a positive number\n");
return 1;
}
if (defined($warn) && defined($crit) && $warn > $crit) {
writeLogFile(LOG_ESXD_ERROR, "ARGS error: warn threshold must be lower than crit threshold\n");
return 1;
}
return 0;
}
sub cpuvm_compute_args {
my $lvm = $_[0];
my $warn = (defined($_[1]) ? $_[1] : 80);
my $crit = (defined($_[2]) ? $_[2] : 90);
return ($lvm, $warn, $crit);
}
sub cpuvm_do {
my ($lvm, $warn, $crit) = @_;
if (!($perfcounter_speriod > 0)) {
my $status |= $MYERRORS_MASK{'UNKNOWN'};
print_response($ERRORS{$MYERRORS{$status}} . "|Can't retrieve perf counters.\n");
return ;
}
my %filters = ('name' => $lvm);
my @properties = ('name');
my $result = get_entities_host('VirtualMachine', \%filters, \@properties);
if (!defined($result)) {
return ;
}
my @instances = ('*');
my $values = generic_performance_values_historic($$result[0],
[{'label' => 'cpu.usage.average', 'instances' => \@instances},
{'label' => 'cpu.usagemhz.average', 'instances' => \@instances}],
$perfcounter_speriod);
my $status = 0; # OK
my $output = '';
my $total_cpu_average = simplify_number(convert_number($values->{$perfcounter_cache{'cpu.usage.average'}->{'key'} . ":"}[0] * 0.01));
my $total_cpu_mhz_average = simplify_number(convert_number($values->{$perfcounter_cache{'cpu.usagemhz.average'}->{'key'} . ":"}[0]));
if ($total_cpu_average >= $warn) {
$status |= $MYERRORS_MASK{'WARNING'};
}
if ($total_cpu_average >= $crit) {
$status |= $MYERRORS_MASK{'CRITICAL'};
}
$output = "Total Average CPU usage '$total_cpu_average%', Total Average CPU '" . $total_cpu_mhz_average . "MHz' on last " . ($perfcounter_speriod / 60) . "min | cpu_total=$total_cpu_average%;$warn;$crit;0;100 cpu_total_MHz=" . $total_cpu_mhz_average . "MHz";
foreach my $id (sort { my ($cida, $cia) = split /:/, $a;
my ($cidb, $cib) = split /:/, $b;
$cia = -1 if (!defined($cia) || $cia eq "");
$cib = -1 if (!defined($cib) || $cib eq "");
$cia <=> $cib} keys %$values) {
my ($counter_id, $instance) = split /:/, $id;
if ($instance ne "") {
$output .= " cpu" . $instance . "_MHz=" . simplify_number(convert_number($values->{$id}[0])) . "MHz";
}
}
print_response($ERRORS{$MYERRORS{$status}} . "|$output\n");
}
1;