New Zabbix::Agent::Addons::Disks module

tags/zabbix-agent-addons-0.2.20-1
Daniel Berteaud 9 years ago
parent 6e0872bc3e
commit 6f52ff1fab
  1. 47
      lib/Zabbix/Agent/Addons/Disks.pm
  2. 25
      zabbix_scripts/disco_block_devices
  3. 27
      zabbix_scripts/disco_smart_sudo
  4. 28
      zabbix_scripts/util_generate_sensors_ini

@ -0,0 +1,47 @@
package Zabbix::Agent::Addons::Disks;
use strict;
use warnings;
# Return an array of block devices, skip if size == 0
sub list_block_dev {
my @bd = ();
opendir(my $dh, "/sys/block") or die "Couldn't open /sys/block: $!";
my @blocks = grep { $_ !~ m/^\./ } readdir($dh);
closedir($dh);
foreach my $block (@blocks){
my $size = 1;
if ( -e "/sys/block/$block/size"){
open SIZE, "/sys/block/$block/size";
$size = join "", <SIZE>;
close SIZE;
chomp($size);
next if ($size eq '0');
}
push @bd, $block;
}
return @bd;
}
sub list_smart_hdd{
my ($param) = shift || {};
my @shd = ();
if (-x "/usr/sbin/smartctl"){
foreach my $block (list_block_dev()){
# Skip block we already know won't support SMART
next if ($block =~ m/^(ram|loop|md|dm\-)\d+/);
next unless (system("/usr/sbin/smartctl -A /dev/$block >/dev/null 2>&1") == 0);
if ($param->{skip_remouvable} && -e "/sys/block/$block/removable"){
open REMOVABLE, "/sys/block/$block/removable";
$removable = join "", <REMOVABLE>;
close REMOVABLE;
chomp($removable);
next if ($removable eq '1');
}
push @shd, $block;
}
}
return @shd;
}
1;

@ -1,22 +1,21 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
use warnings;
use strict;
use Zabbix::Agent::Addons::Disks;
use JSON; use JSON;
opendir(my $dh, "/sys/block") or die "Couldn't open /sys/block: $!";
my @blocks = grep { $_ !~ m/^\./ } readdir($dh);
closedir($dh);
my $json; my $json;
@{$json->{data}} = (); @{$json->{data}} = ();
foreach my $block (@blocks){ foreach my $block (Zabbix::Agent::Addons::Disks::list_block_dev()){
my $size = 1; my $size = 1;
if ( -e "/sys/block/$block/size"){ if ( -e "/sys/block/$block/size"){
open SIZE, "/sys/block/$block/size"; open SIZE, "/sys/block/$block/size";
$size = join "", <SIZE>; $size = join "", <SIZE>;
close SIZE; close SIZE;
chomp($size); chomp($size);
next if ($size eq '0'); }
} push @{$json->{data}}, { "{#BLOCKDEVICE}" => $block, "{#BLOCKSIZE}" => $size };
push @{$json->{data}}, { "{#BLOCKDEVICE}" => $block, "{#BLOCKSIZE}" => $size };
} }
print to_json($json); print to_json($json);
exit(0); exit(0);

@ -2,35 +2,14 @@
use warnings; use warnings;
use strict; use strict;
use Zabbix::Agent::Addons::Disks;
use JSON; use JSON;
my $json; my $json;
@{$json->{data}} = (); @{$json->{data}} = ();
opendir(my $dh, "/sys/block") or die "Couldn't open /sys/block: $!"; foreach my $block (Zabbix::Agent::Addons::Disks::list_smart_hdd({ skip_remouvable => 1 })){
my @blocks = grep { $_ !~ m/^\./ } readdir($dh); push @{$json->{data}}, { "{#SMARTDRIVE}" => "/dev/$block" };
closedir($dh);
foreach my $block (@blocks){
my $removable = 0;
my $size = 1;
# Skip block we already know they won't support SMART
next if ($block =~ m/^(ram|loop|md|dm\-)\d+/);
if ( -e "/sys/block/$block/removable"){
open REMOVABLE, "/sys/block/$block/removable";
$removable = join "", <REMOVABLE>;
close REMOVABLE;
chomp($removable);
next if ($removable eq '1');
}
if ( -e "/sys/block/$block/size"){
open SIZE, "/sys/block/$block/size";
$size = join "", <SIZE>;
close SIZE;
chomp($size);
next if ($size eq '0');
}
next unless (system("/usr/sbin/smartctl -A /dev/$block >/dev/null 2>&1") == 0);
push @{$json->{data}}, { "{#SMARTDRIVE}" => "/dev/$block" };
} }
print to_json($json); print to_json($json);
exit(0); exit(0);

@ -5,6 +5,8 @@ use warnings;
use Config::Simple '-strict'; use Config::Simple '-strict';
use Getopt::Long; use Getopt::Long;
use File::Basename; use File::Basename;
use Zabbix::Agent::Addons::Disks;
# Output file # Output file
my $output = undef; my $output = undef;
@ -203,30 +205,7 @@ if (-x $ipmitool){
# Now, try to detect smart capable HDD # Now, try to detect smart capable HDD
if (-x $smartctl){ if (-x $smartctl){
# This is copied from disco_smart_sudo, I should create a module to foreach my $block (Zabbix::Agent::Addons::Disks::list_smart_hdd({ skip_remouvable => 1 })){
# consolidate this
opendir(my $dh, "/sys/block") or die "Couldn't open /sys/block: $!";
my @blocks = grep { $_ !~ m/^\./ } readdir($dh);
closedir($dh);
foreach my $block (@blocks){
my $removable = 0;
my $size = 1;
# Skip block we already know they won't support SMART
next if ($block =~ m/^(ram|loop|md|dm\-)\d+/);
if ( -e "/sys/block/$block/removable"){
open REMOVABLE, "/sys/block/$block/removable";
$removable = join "", <REMOVABLE>;
close REMOVABLE;
chomp($removable);
next if ($removable eq '1');
}
if ( -e "/sys/block/$block/size"){
open SIZE, "/sys/block/$block/size";
$size = join "", <SIZE>;
close SIZE;
chomp($size);
next if ($size eq '0');
}
my @lines = qx($smartctl -A /dev/$block); my @lines = qx($smartctl -A /dev/$block);
next if ($? != 0); next if ($? != 0);
foreach my $l (@lines){ foreach my $l (@lines){
@ -270,7 +249,6 @@ if (-x $smartctl){
} }
# TODO: add support for lm sensors, but its ouput is harder to parse # TODO: add support for lm sensors, but its ouput is harder to parse
foreach my $s (keys %$sensors){ foreach my $s (keys %$sensors){
$cfg->set_block($s, $sensors->{$s}); $cfg->set_block($s, $sensors->{$s});
} }

Loading…
Cancel
Save