collectd exec plugin to fetch nvme smart data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env perl | |
use strict; | |
use warnings; | |
use JSON qw/decode_json/; | |
my $hostname = $ENV{COLLECTD_HOSTNAME} // `hostname`; | |
my $interval = $ENV{COLLECTD_INTERVAL} // 60; | |
my $device = $ARGV[0] // "nvme0n1"; | |
chomp $hostname; | |
#LoadPlugin exec | |
#<Plugin exec> | |
# Exec "collectd" "/etc/collectd.d/collectd_nvme.pl" "nvme0n1" | |
#</Plugin> | |
my $map = { | |
temperature => { | |
name => 'smart_temperature', | |
cb => sub { shift() - 273 }, | |
}, | |
power_on_hours => { | |
name => 'smart_poweron', | |
cb => sub { return shift() * 3600 }, | |
}, | |
power_cycles => { | |
name => 'smart_powercycles', | |
}, | |
media_errors => { | |
name => 'smart_badsectors', | |
}, | |
critical_warning => { | |
name => 'smart_critical_warning', | |
}, | |
}; | |
while (1) { | |
my $nvme = nvme_get_data($device); | |
foreach my $k (sort keys %$nvme) { | |
next unless (exists $map->{$k}); | |
my $m = $map->{$k}; | |
my $n = $m->{name}; | |
my $v = $nvme->{$k}; | |
$v = $m->{cb}->($v) if (exists $m->{cb}); | |
print "PUTVAL ${hostname}/smart-${device}/${n} N:${v}\n"; | |
} | |
sleep($interval); | |
} | |
sub nvme_get_data { | |
my $device = shift; | |
open(my $nvme, "sudo nvme --smart-log -o json /dev/${device} |") || die "Can't open nvme: $!"; | |
my $content = do { local $/; <$nvme> }; | |
close ($nvme); | |
return decode_json($content); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment