Skip to content

Instantly share code, notes, and snippets.

@brettwooldridge
Created May 4, 2020 14:23
Show Gist options
  • Save brettwooldridge/409330b874a0e86d3f101ae3b1600215 to your computer and use it in GitHub Desktop.
Save brettwooldridge/409330b874a0e86d3f101ae3b1600215 to your computer and use it in GitHub Desktop.
ZFS and Smartctl status email script
#!/bin/env perl
use feature qw(say);
our %smartctl_raw;
say '<html><body><pre style="font-size: smaller">';
_build_smartctl(18);
_build_smartctl(34);
_print_zpool();
_print_smartctl_summary();
_print_zfs_list();
_print_zpool_iostat();
_print_arc_summary();
_print_smartctl_detail();
say "</pre></body></htnl>";
sub _print_zpool {
say ('=' x 75);
say ' zpool status';
say ('-' x 75);
say `/usr/sbin/zpool status`;
}
sub _print_zfs_list {
say "\n" . ('=' x 75);
say ' ZFS Volume List';
say ('-' x 75);
say `/usr/sbin/zfs list`;
}
sub _print_zpool_iostat {
say ('=' x 75);
say ' zpool iostats';
say ('-' x 75);
say `/usr/sbin/zpool iostat -vl`;
}
sub _print_arc_summary {
say ('=' x 72);
print ' zpool ARC summary';
my $arc_summary = `/usr/bin/arc_summary`;
$arc_summary =~ /(.*?)\s+ZFS Tunables/gsm;
say $1;
}
sub _build_smartctl {
my $idLen = shift;
my @devs = split /^/, `ls -1 /dev/disk/by-id | grep -E "wwn-.{$idLen}\$"`;
my $megacount = ($idLen == 18) ? 0 : 1;
foreach $dev (@devs) {
chomp($dev);
for $drive (0..$megacount) {
my $megaraid = "-d megaraid,$drive" if $idLen == 34;
my $suffix = ($idLen == 18) ? '' : "-$drive";
$smartctl_raw{"$dev$suffix"} = `/usr/sbin/smartctl $megaraid -x /dev/disk/by-id/$dev`;
}
}
}
sub _print_smartctl_summary {
say ('=' x 75);
say ' SMARTCTL Disk Summary';
say ('-' x 75);
my @concerns = (
"SMART overall-health",
"SMART Health Status",
);
my $concernsRegex = (join "|", @concerns);
foreach $dev (keys %smartctl_raw) {
my @smartctl = split /^/, $smartctl_raw{$dev};
foreach $line (@smartctl) {
if ($line =~ /$concernsRegex/i) {
chomp($line);
$line =~ /^.+:\s+(.+)$/;
my $status = $1;
say "$dev: $status";
}
}
}
}
sub _print_smartctl_detail {
say "\n" . ('=' x 75);
say ' SMARTCTL Disk Detail';
say ('-' x 75);
my @concerns = (
"^Vendor:",
"^Product:",
"Device Model",
"Serial Number",
"User Capacity",
"Rotation Rate",
"Power_On_Hours",
"Wear_Leveling_Count",
"Used_Rsvd_Blk_Cnt_Tot",
"Percent_Lifetime_Remain",
"Test_Description",
"# 1 Vendor",
"No self-tests",
"algorithm",
"invocations",
"^read:",
"^write:",
"^verify:"
);
my @concernsWithNewline = (
"ATTRIBUTE_NAME",
"SMART overall-health",
"SMART Extended Self-test",
"SMART Health Status",
"Accumulated power",
"Errors Corrected",
"Non-medium error count",
);
my $concernsWithNewlineRegex = (join "|", @concernsWithNewline);
my $concernsRegex = (join "|", @concerns) . '|' . $concernsWithNewlineRegex;
foreach $dev (keys %smartctl_raw) {
my @smartctl = split /^/, $smartctl_raw{$dev};
say "Device by-id:" . (' ' x (length($dev) == 22 ? 5 : 9)) . "$dev " . (length($dev) > 22 ? "(raid disk $drive)" : "");
foreach $line (@smartctl) {
if ($line =~ /$concernsRegex/i) {
if ($line =~ /$concernsWithNewlineRegex/i) {
say '';
}
chomp($line);
say substr($line, 0, 75);
}
}
say "\n" . ('-' x 75);
}
}
@brettwooldridge
Copy link
Author

Used in crontab like this:

0 8 * * 1,4 /usr/bin/perl /root/zfs-report.pl | /usr/bin/mutt -e 'set content_type=text/html' -s "devstorage server report" dev@yourdomain.com

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