Fix PVE scripts to Work with new pvesh version

tags/zabbix-agent-addons-0.2.43-1
Daniel Berteaud 6 years ago
parent a991e64c64
commit 28b3ec4c26
  1. 17
      zabbix_scripts/check_pve_sudo
  2. 11
      zabbix_scripts/disco_pve_sudo

@ -21,8 +21,11 @@ GetOptions(
'pretty' => \$pretty 'pretty' => \$pretty
); );
# Are we using the new pvesh for which we have to specify the output format ?
my $pvesh_opt = (system("$pvesh get /version --output-format=json >/dev/null 2>&1") == 0) ? '--output-format=json' : '';
if ($cluster){ if ($cluster){
my $cluster = from_json(qx($pvesh get /cluster/status 2>/dev/null)); my $cluster = from_json(qx($pvesh get /cluster/status $pvesh_opt 2>/dev/null));
# Set default values so monitoring works for single node, without cluster setup # Set default values so monitoring works for single node, without cluster setup
$json->{status} = { $json->{status} = {
all_online => 1, all_online => 1,
@ -51,7 +54,7 @@ if ($cluster){
} }
} }
foreach my $node (@nodes){ foreach my $node (@nodes){
my $n = from_json(qx($pvesh get /nodes/$node/status 2>/dev/null)); my $n = from_json(qx($pvesh get /nodes/$node/status $pvesh_opt 2>/dev/null));
# Here we gather (and sum) some info about individual nodes to get the total number of # Here we gather (and sum) some info about individual nodes to get the total number of
# CPU, the amount of memory etc... # CPU, the amount of memory etc...
$json->{memory}->{$_} += $n->{memory}->{$_} foreach(qw(free total used)); $json->{memory}->{$_} += $n->{memory}->{$_} foreach(qw(free total used));
@ -62,7 +65,7 @@ if ($cluster){
# We want average load avg of the cluster, not the sum of individual loads # 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); $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)); my $guests = from_json(qx($pvesh get /cluster/resources --type=vm $pvesh_opt 2>/dev/null));
foreach my $guest (@{$guests}){ foreach my $guest (@{$guests}){
$json->{network}->{in} += $guest->{netin}; $json->{network}->{in} += $guest->{netin};
$json->{network}->{out} += $guest->{netout}; $json->{network}->{out} += $guest->{netout};
@ -71,10 +74,10 @@ if ($cluster){
} }
} elsif ($node){ } elsif ($node){
foreach my $item (qw(status version subscription)){ foreach my $item (qw(status version subscription)){
$json->{$item} = from_json(qx(pvesh get /nodes/$node/$item 2>/dev/null)); $json->{$item} = from_json(qx(pvesh get /nodes/$node/$item $pvesh_opt 2>/dev/null));
} }
} elsif ($guest){ } elsif ($guest){
my $guests = from_json(qx($pvesh get /cluster/resources --type=vm 2>/dev/null)); my $guests = from_json(qx($pvesh get /cluster/resources --type=vm $pvesh_opt 2>/dev/null));
foreach my $g (@{$guests}){ foreach my $g (@{$guests}){
if ($g->{vmid} eq $guest){ if ($g->{vmid} eq $guest){
$json = $g; $json = $g;
@ -82,7 +85,7 @@ if ($cluster){
} }
} }
} elsif ($pool){ } elsif ($pool){
my $pool = from_json(qx($pvesh get /pools/$pool 2>/dev/null)); my $pool = from_json(qx($pvesh get /pools/$pool $pvesh_opt 2>/dev/null));
$json->{comment} = $pool->{comment}; $json->{comment} = $pool->{comment};
foreach my $type (qw(qemu lxc)){ foreach my $type (qw(qemu lxc)){
$json->{$_}->{$type} = 0 foreach (qw(guests templates)); $json->{$_}->{$type} = 0 foreach (qw(guests templates));
@ -103,7 +106,7 @@ if ($cluster){
$json->{guests}->{cpu} = ($json->{guests}->{maxcpu} == 0) ? 0 : $json->{guests}->{used_cpu} / $json->{guests}->{maxcpu}; $json->{guests}->{cpu} = ($json->{guests}->{maxcpu} == 0) ? 0 : $json->{guests}->{used_cpu} / $json->{guests}->{maxcpu};
} elsif ($storage){ } elsif ($storage){
$json = from_json(qx($pvesh get /storage/$storage 2>/dev/null)); $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)); my $stores = from_json(qx($pvesh get /cluster/resources --type=storage $pvesh_opt 2>/dev/null));
foreach my $s (@{$stores}){ foreach my $s (@{$stores}){
if ($s->{storage} eq $storage){ if ($s->{storage} eq $storage){
$json->{maxdisk} = $s->{maxdisk}; $json->{maxdisk} = $s->{maxdisk};

@ -23,8 +23,11 @@ unless($pvesh){
exit 0; exit 0;
} }
# Are we using the new pvesh for which we have to specify the output format ?
my $pvesh_opt = (system("$pvesh get /version --output-format=json >/dev/null 2>&1") == 0) ? '--output-format=json' : '';
if ($what eq 'nodes'){ if ($what eq 'nodes'){
my $cluster_status = from_json(qx($pvesh get /cluster/status 2>/dev/null)); my $cluster_status = from_json(qx($pvesh get /cluster/status $pvesh_opt 2>/dev/null));
foreach my $item (@{$cluster_status}){ foreach my $item (@{$cluster_status}){
next unless ($item->{type} eq 'node'); next unless ($item->{type} eq 'node');
push @{$json->{data}}, { push @{$json->{data}}, {
@ -35,7 +38,7 @@ if ($what eq 'nodes'){
}; };
} }
} elsif ($what eq 'guests'){ } elsif ($what eq 'guests'){
my $guests = from_json(qx($pvesh get /cluster/resources --type=vm 2>/dev/null)); my $guests = from_json(qx($pvesh get /cluster/resources --type=vm $pvesh_opt 2>/dev/null));
foreach my $guest (@{$guests}){ foreach my $guest (@{$guests}){
push @{$json->{data}}, { push @{$json->{data}}, {
'{#PVE_GUEST_ID}' => $guest->{vmid}, '{#PVE_GUEST_ID}' => $guest->{vmid},
@ -46,7 +49,7 @@ if ($what eq 'nodes'){
}; };
} }
} elsif ($what eq 'storage'){ } elsif ($what eq 'storage'){
my $stores = from_json(qx($pvesh get /storage 2>/dev/null)); my $stores = from_json(qx($pvesh get /storage $pvesh_opt 2>/dev/null));
foreach my $store (@{$stores}){ foreach my $store (@{$stores}){
push @{$json->{data}}, { push @{$json->{data}}, {
'{#PVE_STOR_ID}' => $store->{storage}, '{#PVE_STOR_ID}' => $store->{storage},
@ -57,7 +60,7 @@ if ($what eq 'nodes'){
}; };
} }
} elsif ($what eq 'pools'){ } elsif ($what eq 'pools'){
my $pools = from_json(qx($pvesh get /pools 2>/dev/null)); my $pools = from_json(qx($pvesh get /pools $pvesh_opt 2>/dev/null));
foreach my $pool (@{$pools}){ foreach my $pool (@{$pools}){
push @{$json->{data}}, { push @{$json->{data}}, {
'{#PVE_POOL_ID}' => $pool->{poolid}, '{#PVE_POOL_ID}' => $pool->{poolid},

Loading…
Cancel
Save