You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
4.0 KiB
119 lines
4.0 KiB
#!/usr/bin/perl -w
|
|
|
|
use strict;
|
|
use warnings;
|
|
use JSON;
|
|
use Getopt::Long;
|
|
use File::Which;
|
|
use Data::Dumper;
|
|
|
|
my $pvesh = which('pvesh');
|
|
my $json;
|
|
my $pretty = 0;
|
|
my ($cluster,$guest,$node,$storage,$pool) = undef;
|
|
|
|
GetOptions(
|
|
'cluster' => \$cluster,
|
|
'guest=i' => \$guest,
|
|
'node=s' => \$node,
|
|
'storage=s' => \$storage,
|
|
'pool=s' => \$pool,
|
|
'pretty' => \$pretty
|
|
);
|
|
|
|
if ($cluster){
|
|
my $cluster = from_json(qx($pvesh get /cluster/status 2>/dev/null));
|
|
# Set default values so monitoring works for single node, without cluster setup
|
|
$json->{status} = {
|
|
all_online => 1,
|
|
quorate => 1,
|
|
nodes => 1,
|
|
name => 'default',
|
|
version => 1
|
|
};
|
|
# Set default global stats
|
|
$json->{network} = {
|
|
in => 0,
|
|
out => 0
|
|
};
|
|
$json->{disk} = {
|
|
read => 0,
|
|
write => 0
|
|
};
|
|
my @nodes = ();
|
|
foreach my $item (@{$cluster}){
|
|
if ($item->{type} eq 'cluster'){
|
|
$json->{status}->{$_} = $item->{$_} foreach (qw(quorate nodes name version));
|
|
} elsif ($item->{type} eq 'node' and $item->{online}){
|
|
push @nodes, $item->{name};
|
|
} elsif ($item->{type} eq 'node'){
|
|
$json->{status}->{all_online} = 0;
|
|
}
|
|
}
|
|
foreach my $node (@nodes){
|
|
my $n = from_json(qx($pvesh get /nodes/$node/status 2>/dev/null));
|
|
# Here we gather (and sum) some info about individual nodes to get the total number of
|
|
# CPU, the amount of memory etc...
|
|
$json->{memory}->{$_} += $n->{memory}->{$_} foreach(qw(free total used));
|
|
$json->{ksm}->{$_} += $n->{ksm}->{$_} foreach (qw(shared));
|
|
$json->{cpuinfo}->{$_} += $n->{cpuinfo}->{$_} foreach (qw(cpus sockets));
|
|
$json->{loadavg}[$_] += $n->{loadavg}[$_] foreach (0..2);
|
|
}
|
|
# We want average load avg of the cluster, not the sum of individual loads
|
|
$json->{loadavg}[$_] = sprintf "%.2f", $json->{loadavg}[$_] / $json->{status}->{nodes} foreach (0..2);
|
|
|
|
my $guests = from_json(qx($pvesh get /cluster/resources --type=vm 2>/dev/null));
|
|
foreach my $guest (@{$guests}){
|
|
$json->{network}->{in} += $guest->{netin};
|
|
$json->{network}->{out} += $guest->{netout};
|
|
$json->{disk}->{read} += $guest->{diskread};
|
|
$json->{disk}->{write} += $guest->{diskwrite};
|
|
}
|
|
} elsif ($node){
|
|
foreach my $item (qw(status version subscription)){
|
|
$json->{$item} = from_json(qx(pvesh get /nodes/$node/$item 2>/dev/null));
|
|
}
|
|
} elsif ($guest){
|
|
my $guests = from_json(qx($pvesh get /cluster/resources --type=vm 2>/dev/null));
|
|
foreach my $g (@{$guests}){
|
|
if ($g->{vmid} eq $guest){
|
|
$json = $g;
|
|
last;
|
|
}
|
|
}
|
|
} elsif ($pool){
|
|
my $pool = from_json(qx($pvesh get /pools/$pool 2>/dev/null));
|
|
$json->{comment} = $pool->{comment};
|
|
foreach my $type (qw(qemu lxc)){
|
|
$json->{$_}->{$type} = 0 foreach (qw(guests templates));
|
|
}
|
|
foreach my $item (@{$pool->{members}}){
|
|
if ($item->{type} =~ m/^(qemu|lxc)$/ and !$item->{template}){
|
|
$json->{guests}->{$_} += $item->{$_} foreach (qw(maxcpu diskread diskwrite maxdisk mem maxmem netin netout));
|
|
$json->{guests}->{used_cpu} += $item->{cpu} * $item->{maxcpu};
|
|
$json->{guests}->{$item->{type}}++;
|
|
}
|
|
if ($item->{type} =~ m/^(qemu|lxc)$/ and $item->{template}){
|
|
$json->{templates}->{$_} += $item->{$_} foreach (qw(maxdisk));
|
|
$json->{templates}->{$item->{type}}++;
|
|
}
|
|
}
|
|
$json->{guests}->{$_} //= 0 foreach (qw(cpu maxcpu diskread diskwrite maxdisk mem maxmem netin netout));
|
|
$json->{templates}->{$_} //= 0 foreach (qw(maxdisk));
|
|
$json->{guests}->{cpu} = ($json->{guests}->{maxcpu} == 0) ? 0 : $json->{guests}->{used_cpu} / $json->{guests}->{maxcpu};
|
|
} elsif ($storage){
|
|
$json = from_json(qx($pvesh get /storage/$storage 2>/dev/null));
|
|
my $stores = from_json(qx($pvesh get /cluster/resources --type=storage 2>/dev/null));
|
|
foreach my $s (@{$stores}){
|
|
if ($s->{storage} eq $storage){
|
|
$json->{maxdisk} = $s->{maxdisk};
|
|
$json->{disk} = $s->{disk};
|
|
last;
|
|
}
|
|
}
|
|
} else{
|
|
print 'ZBX_UNSUPORTED';
|
|
exit 0;
|
|
}
|
|
|
|
print to_json($json, { pretty => $pretty }) . "\n";
|
|
|