Better compat with 4.4 vfs.dev.discovery (and use lsblk to get the list of dev if available)

tags/zabbix-agent-addons-0.2.92-1
Daniel Berteaud 5 years ago
parent db939d3e92
commit e30dcda6da
  1. 34
      zabbix_scripts/disco_block_devices

@ -4,18 +4,36 @@ use warnings;
use strict; use strict;
use Zabbix::Agent::Addons::Disks; use Zabbix::Agent::Addons::Disks;
use JSON; use JSON;
use File::Which;
use Getopt::Long;
my $pretty = 0;
GetOptions(
'pretty' => \$pretty
);
my $lsblk = which('lsblk');
my $json; my $json;
@{$json->{data}} = (); @{$json->{data}} = ();
if (defined $lsblk){
foreach my $line (qx($lsblk -o KNAME,TYPE,SIZE -r -n -b)){
my ($block,$type,$size) = split(/\s+/, $line);
push @{$json->{data}}, {
"{#BLOCKDEVICE}" => $block, # Compat with previous zabbix-agent-addons
"{#DEVNAME}" => $block, # New macro name for the native vfs.dev.discovery key in 4.4
"{#DEVTYPE}" => $type,
};
}
} else {
# Fallback if lsblk is not available
foreach my $block (Zabbix::Agent::Addons::Disks::list_block_dev()){ foreach my $block (Zabbix::Agent::Addons::Disks::list_block_dev()){
my $size = 1; push @{$json->{data}}, {
if ( -e "/sys/block/$block/size"){ "{#BLOCKDEVICE}" => $block,
open SIZE, "/sys/block/$block/size"; "{#DEVNAME}" => $block,
$size = join "", <SIZE>; "{#DEVTYPE}" => 'disk'
close SIZE; };
chomp($size);
} }
push @{$json->{data}}, { "{#BLOCKDEVICE}" => $block, "{#BLOCKSIZE}" => $size, "{#DEVNAME}" => $block };
} }
print to_json($json); print to_json($json, { pretty => $pretty });
exit(0); exit(0);

Loading…
Cancel
Save