Skip to content

Instantly share code, notes, and snippets.

@gwollman
Created December 30, 2018 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwollman/dd64439b9e0e5f29a221bb34d1309f5f to your computer and use it in GitHub Desktop.
Save gwollman/dd64439b9e0e5f29a221bb34d1309f5f to your computer and use it in GitHub Desktop.
An altered verson of Lutz Peter Christoph's hddtemp_smartctl munin plugin to monitor NVMe drives using nvmecontrol
#!/usr/local/bin/perl -w
# -*- perl -*-
use strict;
use warnings;
=head1 NAME
hddtemp_nvmecontrol - Plugin to monitor NVMe SSD temperatures through
SMART
=head1 CONFIGURATION
This plugin needs to run as root or some other user that has access to
the harddrive devices.
The following environment variables are used
nvmecontrol - path to nvmecontrol executable
drives - List drives to monitor. E.g. "env.drives nvme0 nvme1".
If the "nvmecontrol" environment variable is not set the plugin will
search your $PATH, /usr/bin, /usr/sbin, /usr/local/bin and
/usr/local/sbin for a file called "nvmecontrol", and use that.
If the "drives" environment variable is not set, the plugin will
attempt to search for drives to probe.
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf
=head1 AUTHOR
Copyright (c) 2005, Lutz Peter Christoph
All rights reserved.
2016-08-27, Gabriele Pohl (contact@dipohl.de)
Fix for github issue #690
2018-12-30, Garrett Wollman (wollman@csail.mit.edu)
Rip out all the smartctl code and make it work by calling
nvmecontrol(8) instead.
=head1 LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* The name and aliases of Lutz Peter Christoph ("Lupe Christoph",
"Lutz Christoph") may not be used to endorse or promote products
derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
use File::Spec::Functions qw(splitdir);
use lib $ENV{'MUNIN_LIBDIR'};
use Munin::Plugin;
my $DEBUG = $ENV{'MUNIN_DEBUG'} || 0;
my $nvmecontrol;
if (exists $ENV{nvmecontrol}) {
$nvmecontrol = $ENV{nvmecontrol};
if (defined $ARGV[0] and $ARGV[0] eq 'autoconf') {
# The real "autoconf" section follows later. But here we need to check for requirements, too.
if (! -e $nvmecontrol) {
print "no (Predefined nvmecontrol ($nvmecontrol) does not exist)\n";
exit 0;
} elsif (! -x $nvmecontrol) {
print "no (Predefined nvmecontrol ($nvmecontrol) is not executable)\n";
exit 0;
}
} else {
# immediate failure is allowed outside of "autoconf"
die "$nvmecontrol does not exist\n" unless (-e $nvmecontrol);
die "$nvmecontrol is not executable\n" unless (-x $nvmecontrol);
}
} else {
# Not defined in %ENV? Check obvious places
my @dirs = split(':', $ENV{PATH});
push (@dirs, qw(/usr/bin /usr/sbin /usr/local/bin /usr/local/sbin) );
until ($nvmecontrol or @dirs == 0) {
my $dir = shift @dirs;
my $path = $dir.'/nvmecontrol';
$nvmecontrol = $path if -x $path;
}
unless ($nvmecontrol) {
if (defined $ARGV[0] and $ARGV[0] eq 'autoconf') {
print "no ('nvmecontrol' executable not found)\n";
exit 0;
} else {
die "'nvmecontrol' executable not found\n";
}
}
}
my @drives;
if(exists $ENV{drives}) {
@drives = split ' ', $ENV{drives};
} else {
# Try to get a default set of drives
opendir(DEV, '/dev');
@drives = grep /^nvme[0-9]+$/, readdir DEV;
closedir(DEV);
}
# Sort list of drives
@drives = sort @drives;
warn "[DEBUG] Drives: ",join(', ',@drives),"\n" if $DEBUG;
if (defined $ARGV[0]) {
if ($ARGV[0] eq 'autoconf') {
if (@drives) {
my $cmd = command_for_drive_device($drives[0]);
if (`$cmd` =~ /^Temperature/m) {
print "yes\n";
} else {
print "no (first drive not supported, configure the plugin)\n";
}
exit 0;
} else {
print "no (no drives known)\n";
exit 0;
}
} elsif ($ARGV[0] eq 'config') {
print "graph_title HDD temperature\n";
print "graph_vlabel Degrees Celsius\n";
print "graph_category sensors\n";
print "graph_info This graph shows the temperature in degrees Celsius of the NVMe SSDs in the machine.\n";
foreach (@drives) {
my @dirs = splitdir($_);
print clean_fieldname($_) . ".label " . $dirs[-1] . "\n";
print clean_fieldname($_) . ".max 100\n";
print clean_fieldname($_) . ".warning 57\n";
print clean_fieldname($_) . ".critical 60\n";
}
exit 0;
}
}
foreach my $drive (@drives) {
warn "[DEBUG] Processing $drive\n" if $DEBUG;
my $cmd = command_for_drive_device($drive);
warn "[DEBUG] Command for $drive is % $cmd %\n" if $DEBUG;
my $output = `$cmd`;
my $cmd_exit = $?;
if ($cmd_exit != 0) {
print "$drive.value U\n";
if ($cmd_exit == -1) {
warn "[ERROR] Command $cmd on drive $drive failed to execute: $!";
} else {
my $nvmecontrol_exit = $cmd_exit >> 8;
print "$drive.extinfo Command '$cmd' on drive $drive failed with exit($nvmecontrol_exit)\n";
warn "[ERROR] Command $cmd on drive $drive failed with exit($nvmecontrol_exit): $output";
}
next;
}
if ($output =~ /Temperature:\s*(\d+) K, (\S+) C/) {
print "$drive.value $2\n";
} else {
print "$drive.value U\n";
print "$drive.extinfo Temperature not detected in nvmecontrol output\n";
}
}
sub command_for_drive_device {
my ($drive) = @_;
my $cmd = $nvmecontrol.' logpage -p 2 ';
$cmd .= $ENV{'args_'.$drive}.' ' if exists $ENV{'args_'.$drive};
$cmd .= $drive;
}
@gwollman
Copy link
Author

TODO: parse other health information from nvmecontrol(8)'s output, especially the sparing ratio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment