62 lines
1.3 KiB
Perl
62 lines
1.3 KiB
Perl
#!/usr/bin/perl
|
|
|
|
use strict;
|
|
use JSON;
|
|
use Getopt::Long;
|
|
use Data::Dumper;
|
|
use File::Which;
|
|
|
|
my $pretty = 0;
|
|
|
|
GetOptions(
|
|
'pretty' => \$pretty
|
|
);
|
|
|
|
my $smartctl = which('smartctl');
|
|
my $json = [];
|
|
|
|
sub print_out {
|
|
print to_json($json, { pretty => $pretty });
|
|
}
|
|
|
|
if (not defined $smartctl){
|
|
print_out();
|
|
exit 0;
|
|
}
|
|
|
|
my $smart_scan = from_json(qx($smartctl --scan-open --json=c));
|
|
|
|
if (not defined $smart_scan){
|
|
print_out();
|
|
exit 0;
|
|
}
|
|
|
|
foreach my $device (@{$smart_scan->{devices}}){
|
|
my ($model, $sn, $has_smart) = "";
|
|
my $smart_info = from_json(qx($smartctl -i $device->{name} -d $device->{type} --json=c));
|
|
|
|
if (defined $smart_info){
|
|
$model = $smart_info->{model_name};
|
|
$sn = $smart_info->{serial_number};
|
|
$has_smart = (
|
|
$smart_info->{in_smartctl_database} or (
|
|
defined $smart_info->{smart_support} and
|
|
$smart_info->{smart_support}->{available} and
|
|
$smart_info->{smart_support}->{enabled}
|
|
)
|
|
) ? 1 : 0;
|
|
}
|
|
|
|
push @{$json}, {
|
|
'{#STOR_DEV_NAME}' => $device->{name},
|
|
'{#STOR_DEV_DESC}' => $device->{info_name},
|
|
'{#STOR_DEV_TYPE}' => $device->{type},
|
|
'{#STOR_DEV_PROTO}' => $device->{protocol},
|
|
'{#STOR_DEV_MODEL}' => $model,
|
|
'{#STOR_DEV_SN}' => $sn,
|
|
'{#STOR_DEV_SMART}' => int $has_smart
|
|
};
|
|
}
|
|
|
|
print_out();
|