You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

777 lines
32 KiB

  1. #!/usr/bin/perl -w
  2. # Check SMART status of ATA/SCSI/NVMe drives, returning any usable metrics as perfdata.
  3. # For usage information, run ./check_smart -h
  4. #
  5. # This script was initially created under contract for the US Government and is therefore Public Domain
  6. #
  7. # Official documentation: https://www.claudiokuenzler.com/monitoring-plugins/check_smart.php
  8. #
  9. # Changes and Modifications
  10. # =========================
  11. # Feb 3, 2009: Kurt Yoder - initial version of script (rev 1.0)
  12. # Jul 8, 2013: Claudio Kuenzler - support hardware raids like megaraid (rev 2.0)
  13. # Jul 9, 2013: Claudio Kuenzler - update help output (rev 2.1)
  14. # Oct 11, 2013: Claudio Kuenzler - making the plugin work on FreeBSD (rev 3.0)
  15. # Oct 11, 2013: Claudio Kuenzler - allowing -i sat (SATA on FreeBSD) (rev 3.1)
  16. # Nov 4, 2013: Claudio Kuenzler - works now with CCISS on FreeBSD (rev 3.2)
  17. # Nov 4, 2013: Claudio Kuenzler - elements in grown defect list causes warning (rev 3.3)
  18. # Nov 6, 2013: Claudio Kuenzler - add threshold option "bad" (-b) (rev 4.0)
  19. # Nov 7, 2013: Claudio Kuenzler - modified help (rev 4.0)
  20. # Nov 7, 2013: Claudio Kuenzler - bugfix in threshold logic (rev 4.1)
  21. # Mar 19, 2014: Claudio Kuenzler - bugfix in defect list perfdata (rev 4.2)
  22. # Apr 22, 2014: Jerome Lauret - implemented -g to do a global lookup (rev 5.0)
  23. # Apr 25, 2014: Claudio Kuenzler - cleanup, merge Jeromes code, perfdata output fix (rev 5.1)
  24. # May 5, 2014: Caspar Smit - Fixed output bug in global check / issue #3 (rev 5.2)
  25. # Feb 4, 2015: Caspar Smit and cguadall - Allow detection of more than 26 devices / issue #5 (rev 5.3)
  26. # Feb 5, 2015: Bastian de Groot - Different ATA vs. SCSI lookup (rev 5.4)
  27. # Feb 11, 2015: Josh Behrends - Allow script to run outside of nagios plugins dir / wiki url update (rev 5.5)
  28. # Feb 11, 2015: Claudio Kuenzler - Allow script to run outside of nagios plugins dir for FreeBSD too (rev 5.5)
  29. # Mar 12, 2015: Claudio Kuenzler - Change syntax of -g parameter (glob is now awaited from input) (rev 5.6)
  30. # Feb 6, 2017: Benedikt Heine - Fix Use of uninitialized value $device (rev 5.7)
  31. # Oct 10, 2017: Bobby Jones - Allow multiple devices for interface type megaraid, e.g. "megaraid,[1-5]" (rev 5.8)
  32. # Apr 28, 2018: Pavel Pulec (Inuits) - allow type "auto" (rev 5.9)
  33. # May 5, 2018: Claudio Kuenzler - Check selftest log for errors using new parameter -s (rev 5.10)
  34. # Dec 27, 2018: Claudio Kuenzler - Add exclude list (-e) to ignore certain attributes (5.11)
  35. # Jan 8, 2019: Claudio Kuenzler - Fix 'Use of uninitialized value' warnings (5.11.1)
  36. # Jun 4, 2019: Claudio Kuenzler - Add raw check list (-r) and warning thresholds (-w) (6.0)
  37. # Jun 11, 2019: Claudio Kuenzler - Allow using pseudo bus device /dev/bus/N (6.1)
  38. # Aug 19, 2019: Claudio Kuenzler - Add device model and serial number in output (6.2)
  39. # Oct 1, 2019: Michael Krahe - Allow exclusion from perfdata as well (-E) and by attribute number (6.3)
  40. # Oct 29, 2019: Jesse Becker - Remove dependency on utils.pm, add quiet parameter (6.4)
  41. # Nov 22, 2019: Claudio Kuenzler - Add Reported_Uncorrect and Reallocated_Event_Count to default raw list (6.5)
  42. # Nov 29, 2019: Claudio Kuenzler - Add 3ware and cciss devices for global (-g) check, adjust output (6.6)
  43. # Dec 4, 2019: Ander Punnar - Fix 'deprecation warning on regex with curly brackets' (6.6.1)
  44. # Mar 25, 2020: Claudio Kuenzler - Add support for NVMe devices (6.7.0)
  45. # Jun 2, 2020: Claudio Kuenzler - Bugfix to make --warn work (6.7.1)
  46. use strict;
  47. use Getopt::Long;
  48. use File::Basename qw(basename);
  49. my $basename = basename($0);
  50. my $revision = '6.7.1';
  51. # Standard Nagios return codes
  52. my %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
  53. $ENV{'PATH'}='/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin';
  54. $ENV{'BASH_ENV'}='';
  55. $ENV{'ENV'}='';
  56. use vars qw($opt_b $opt_d $opt_g $opt_debug $opt_h $opt_i $opt_e $opt_E $opt_r $opt_s $opt_v $opt_w $opt_q);
  57. Getopt::Long::Configure('bundling');
  58. GetOptions(
  59. "debug" => \$opt_debug,
  60. "b=i" => \$opt_b, "bad=i" => \$opt_b,
  61. "d=s" => \$opt_d, "device=s" => \$opt_d,
  62. "g=s" => \$opt_g, "global=s" => \$opt_g,
  63. "h" => \$opt_h, "help" => \$opt_h,
  64. "i=s" => \$opt_i, "interface=s" => \$opt_i,
  65. "e=s" => \$opt_e, "exclude=s" => \$opt_e,
  66. "E=s" => \$opt_E, "exclude-all=s" => \$opt_E,
  67. "q" => \$opt_q, "quiet" => \$opt_q,
  68. "r=s" => \$opt_r, "raw=s" => \$opt_r,
  69. "s" => \$opt_s, "selftest" => \$opt_s,
  70. "v" => \$opt_v, "version" => \$opt_v,
  71. "w=s" => \$opt_w, "warn=s" => \$opt_w,
  72. );
  73. if ($opt_v) {
  74. print_revision($basename, $revision);
  75. exit $ERRORS{'OK'};
  76. }
  77. if ($opt_h) {
  78. print_help();
  79. exit $ERRORS{'OK'};
  80. }
  81. my ($device, $interface) = qw// // '';
  82. if ($opt_d || $opt_g ) {
  83. unless($opt_i){
  84. print "must specify an interface for $opt_d using -i/--interface!\n\n";
  85. print_help();
  86. exit $ERRORS{'UNKNOWN'};
  87. }
  88. # list of devices for a loop
  89. my(@dev);
  90. if ( $opt_d ){
  91. # normal mode - push opt_d on the list of devices
  92. push(@dev,$opt_d);
  93. } else {
  94. # glob all devices - try '?' first
  95. @dev =glob($opt_g);
  96. }
  97. foreach my $opt_dl (@dev){
  98. warn "Found $opt_dl\n" if $opt_debug;
  99. if (-b $opt_dl || -c $opt_dl || $opt_dl =~ m/\/dev\/bus\/\d/) {
  100. $device .= $opt_dl.":";
  101. } else {
  102. warn "$opt_dl is not a valid block/character special device!\n\n" if $opt_debug;
  103. }
  104. }
  105. if (!defined($device) || $device eq "") {
  106. print "Could not find any valid block/character special device for ".
  107. ($opt_d?"device $opt_d ":"pattern $opt_g")." !\n\n";
  108. exit $ERRORS{'UNKNOWN'};
  109. }
  110. # Allow all device types currently supported by smartctl
  111. # See http://www.smartmontools.org/wiki/Supported_RAID-Controllers
  112. if ($opt_i =~ m/(ata|scsi|3ware|areca|hpt|cciss|megaraid|sat|auto|nvme)/) {
  113. $interface = $opt_i;
  114. if($interface =~ m/megaraid,\[(\d{1,2})-(\d{1,2})\]/) {
  115. $interface = "";
  116. for(my $k = $1; $k <= $2; $k++) {
  117. $interface .= "megaraid," . $k . ":";
  118. }
  119. }
  120. elsif($interface =~ m/3ware,\[(\d{1,2})-(\d{1,2})\]/) {
  121. $interface = "";
  122. for(my $k = $1; $k <= $2; $k++) {
  123. $interface .= "3ware," . $k . ":";
  124. }
  125. }
  126. elsif($interface =~ m/cciss,\[(\d{1,2})-(\d{1,2})\]/) {
  127. $interface = "";
  128. for(my $k = $1; $k <= $2; $k++) {
  129. $interface .= "cciss," . $k . ":";
  130. }
  131. }
  132. else {
  133. $interface .= ":";
  134. }
  135. } else {
  136. print "invalid interface $opt_i for $opt_d!\n\n";
  137. print_help();
  138. exit $ERRORS{'UNKNOWN'};
  139. }
  140. }
  141. if ($device eq "") {
  142. print "UNKNOWN - Must specify a device!\n\n";
  143. print_help();
  144. exit $ERRORS{'UNKNOWN'};
  145. }
  146. my $smart_command = 'sudo smartctl';
  147. my $exit_status = 'OK';
  148. my $exit_status_local = 'OK';
  149. my $status_string = '';
  150. my $perf_string = '';
  151. my $Terminator = ' --- ';
  152. my $vendor = '';
  153. my $model = '';
  154. my $product = '';
  155. my $serial = '';
  156. # exclude lists
  157. my @exclude_checks = split /,/, $opt_e // '' ;
  158. my @exclude_perfdata = split /,/, $opt_E // '';
  159. push(@exclude_checks, @exclude_perfdata);
  160. # raw check list
  161. my $raw_check_list = $opt_r // 'Current_Pending_Sector,Reallocated_Sector_Ct,Program_Fail_Cnt_Total,Uncorrectable_Error_Cnt,Offline_Uncorrectable,Runtime_Bad_Block,Reported_Uncorrect,Reallocated_Event_Count';
  162. my @raw_check_list = split /,/, $raw_check_list;
  163. # raw check list for nvme
  164. my $raw_check_list_nvme = $opt_r // 'Media_and_Data_Integrity_Errors';
  165. my @raw_check_list_nvme = split /,/, $raw_check_list_nvme;
  166. # warning threshold list (for raw checks)
  167. my $warn_list = $opt_w // '';
  168. my @warn_list = split /,/, $warn_list;
  169. my %warn_list;
  170. my $warn_key;
  171. my $warn_value;
  172. foreach my $warn_element (@warn_list) {
  173. ($warn_key, $warn_value) = split /=/, $warn_element;
  174. $warn_list{ $warn_key } = $warn_value;
  175. }
  176. # For backward compatibility, add -b parameter to warning thresholds
  177. if ($opt_b) {
  178. $warn_list{ 'Current_Pending_Sector' } = $opt_b;
  179. }
  180. my @drives_status_okay;
  181. my @drives_status_not_okay;
  182. my $drive_details;
  183. foreach $device ( split(":",$device) ){
  184. foreach $interface ( split(":",$interface) ){
  185. my @error_messages = qw//;
  186. my($status_string_local)='';
  187. my($tag,$label);
  188. $exit_status_local = 'OK';
  189. if ($opt_g){
  190. # we had a pattern based on $opt_g
  191. $tag = $device;
  192. $tag =~ s/\Q$opt_g\E//;
  193. if($interface =~ qr/(?:megaraid|3ware|cciss)/){
  194. $label = "[$interface] - ";
  195. } else {
  196. $label = "[$device] - ";
  197. }
  198. } else {
  199. # we had a device specified using $opt_d (traditional)
  200. $label = "";
  201. $tag = $device;
  202. }
  203. warn "###########################################################\n" if $opt_debug;
  204. warn "(debug) CHECK 1: getting overall SMART health status for $tag \n" if $opt_debug;
  205. warn "###########################################################\n\n\n" if $opt_debug;
  206. my $full_command = "$smart_command -d $interface -Hi $device";
  207. warn "(debug) executing:\n$full_command\n\n" if $opt_debug;
  208. my @output = `$full_command`;
  209. warn "(debug) output:\n@output\n\n" if $opt_debug;
  210. my $output_mode = "";
  211. # parse ata output, looking for "health status: passed"
  212. my $found_status = 0;
  213. my $line_str_ata = 'SMART overall-health self-assessment test result: '; # ATA SMART line
  214. my $ok_str_ata = 'PASSED'; # ATA SMART OK string
  215. my $line_str_scsi = 'SMART Health Status: '; # SCSI and CCISS SMART line
  216. my $ok_str_scsi = 'OK'; #SCSI and CCISS SMART OK string
  217. my $line_model_ata = 'Device Model: '; # ATA Model including vendor
  218. my $line_model_nvme = 'Model Number: '; # NVMe Model including vendor
  219. my $line_vendor_scsi = 'Vendor: '; # SCSI Vendor
  220. my $line_model_scsi = 'Product: '; # SCSI Model
  221. my $line_serial_ata = 'Serial Number: '; # ATA Drive Serial Number
  222. my $line_serial_scsi = 'Serial number: '; # SCSI Drive Serial Number
  223. foreach my $line (@output){
  224. if($line =~ /$line_str_scsi(.+)/){
  225. $found_status = 1;
  226. $output_mode = "scsi";
  227. warn "(debug) parsing line:\n$line\n\n" if $opt_debug;
  228. if ($1 eq $ok_str_scsi) {
  229. warn "(debug) found string '$ok_str_scsi'; status OK\n\n" if $opt_debug;
  230. }
  231. else {
  232. warn "(debug) no '$ok_str_scsi' status; failing\n\n" if $opt_debug;
  233. push(@error_messages, "Health status: $1");
  234. escalate_status('CRITICAL');
  235. }
  236. }
  237. elsif($line =~ /$line_str_ata(.+)/){
  238. $found_status = 1;
  239. if ($interface eq 'nvme') {
  240. $output_mode = "nvme";
  241. warn "(debug) setting output mode to nvme\n\n" if $opt_debug;
  242. } else {
  243. $output_mode = "ata";
  244. }
  245. warn "(debug) parsing line:\n$line\n\n" if $opt_debug;
  246. if ($1 eq $ok_str_ata) {
  247. warn "(debug) found string '$ok_str_ata'; status OK\n\n" if $opt_debug;
  248. }
  249. else {
  250. warn "(debug) no '$ok_str_ata' status; failing\n\n" if $opt_debug;
  251. push(@error_messages, "Health status: $1");
  252. escalate_status('CRITICAL');
  253. }
  254. }
  255. if($line =~ /$line_model_ata(.+)/){
  256. warn "(debug) parsing line:\n$line\n\n" if $opt_debug;
  257. $model = $1;
  258. $model =~ s/\s{2,}/ /g;
  259. warn "(debug) found model: $model\n\n" if $opt_debug;
  260. }
  261. if($line =~ /$line_model_nvme(.+)/){
  262. warn "(debug) parsing line:\n$line\n\n" if $opt_debug;
  263. $model = $1;
  264. $model =~ s/\s{2,}/ /g;
  265. warn "(debug) found model: $model\n\n" if $opt_debug;
  266. }
  267. if($line =~ /$line_vendor_scsi(.+)/){
  268. warn "(debug) parsing line:\n$line\n\n" if $opt_debug;
  269. $vendor = $1;
  270. warn "(debug) found vendor: $model\n\n" if $opt_debug;
  271. }
  272. if($line =~ /$line_model_scsi(.+)/){
  273. warn "(debug) parsing line:\n$line\n\n" if $opt_debug;
  274. $product = $1;
  275. $model = "$vendor $product";
  276. $model =~ s/\s{2,}/ /g;
  277. warn "(debug) found model: $model\n\n" if $opt_debug;
  278. }
  279. if($line =~ /$line_serial_ata(.+)/){
  280. warn "(debug) parsing line:\n$line\n\n" if $opt_debug;
  281. $serial = $1;
  282. $serial =~ s/^\s+|\s+$//g;
  283. warn "(debug) found serial number $serial\n\n" if $opt_debug;
  284. }
  285. if($line =~ /$line_serial_scsi(.+)/){
  286. warn "(debug) parsing line:\n$line\n\n" if $opt_debug;
  287. $serial = $1;
  288. $serial =~ s/^\s+|\s+$//g;
  289. warn "(debug) found serial number $serial\n\n" if $opt_debug;
  290. }
  291. }
  292. unless ($found_status) {
  293. push(@error_messages, 'No health status line found');
  294. escalate_status('UNKNOWN');
  295. }
  296. warn "###########################################################\n" if $opt_debug;
  297. warn "(debug) CHECK 2: getting silent SMART health check\n" if $opt_debug;
  298. warn "###########################################################\n\n\n" if $opt_debug;
  299. $full_command = "$smart_command -d $interface -q silent -A $device";
  300. warn "(debug) executing:\n$full_command\n\n" if $opt_debug;
  301. system($full_command);
  302. my $return_code = $?;
  303. warn "(debug) exit code:\n$return_code\n\n" if $opt_debug;
  304. if ($return_code & 0x01) {
  305. push(@error_messages, 'Commandline parse failure');
  306. escalate_status('UNKNOWN');
  307. }
  308. if ($return_code & 0x02) {
  309. push(@error_messages, 'Device could not be opened');
  310. escalate_status('UNKNOWN');
  311. }
  312. if ($return_code & 0x04) {
  313. push(@error_messages, 'Checksum failure');
  314. escalate_status('WARNING');
  315. }
  316. if ($return_code & 0x08) {
  317. push(@error_messages, 'Disk is failing');
  318. escalate_status('CRITICAL');
  319. }
  320. if ($return_code & 0x10) {
  321. push(@error_messages, 'Disk is in prefail');
  322. escalate_status('WARNING');
  323. }
  324. if ($return_code & 0x20) {
  325. push(@error_messages, 'Disk may be close to failure');
  326. escalate_status('WARNING');
  327. }
  328. if ($return_code & 0x40) {
  329. push(@error_messages, 'Error log contains errors');
  330. escalate_status('WARNING');
  331. }
  332. if ($return_code & 0x80) {
  333. push(@error_messages, 'Self-test log contains errors');
  334. escalate_status('WARNING');
  335. }
  336. if ($return_code && !$exit_status_local) {
  337. push(@error_messages, 'Unknown return code');
  338. escalate_status('CRITICAL');
  339. }
  340. if ($return_code) {
  341. warn "(debug) non-zero exit code, generating error condition\n\n" if $opt_debug;
  342. } else {
  343. warn "(debug) zero exit code, status OK\n\n" if $opt_debug;
  344. }
  345. if ($opt_s) {
  346. warn "(debug) selftest log check activated\n\n" if $opt_debug;
  347. $full_command = "$smart_command -d $interface -q silent -l selftest $device";
  348. system($full_command);
  349. my $return_code = $?;
  350. warn "(debug) exit code:\n$return_code\n\n" if $opt_debug;
  351. if ($return_code > 0) {
  352. push(@error_messages, 'Self-test log contains errors');
  353. warn "(debug) Self-test log contains errors\n\n" if $opt_debug;
  354. escalate_status('WARNING');
  355. }
  356. if ($return_code) {
  357. warn "(debug) non-zero exit code, generating error condition\n\n" if $opt_debug;
  358. } else {
  359. warn "(debug) zero exit code, status OK\n\n" if $opt_debug;
  360. }
  361. }
  362. warn "###########################################################\n" if $opt_debug;
  363. warn "(debug) CHECK 3: getting detailed statistics from attributes\n" if $opt_debug;
  364. warn "(debug) information contains a few more potential trouble spots\n" if $opt_debug;
  365. warn "(debug) plus, we can also use the information for perfdata/graphing\n" if $opt_debug;
  366. warn "###########################################################\n\n\n" if $opt_debug;
  367. $full_command = "$smart_command -d $interface -A $device";
  368. warn "(debug) executing:\n$full_command\n\n" if $opt_debug;
  369. @output = `$full_command`;
  370. warn "(debug) output:\n@output\n\n" if $opt_debug;
  371. my @perfdata = qw//;
  372. warn "(debug) Raw Check List: $raw_check_list\n" if $opt_debug;
  373. warn "(debug) Exclude List for Checks: ", join(",", @exclude_checks), "\n" if $opt_debug;
  374. warn "(debug) Exclude List for Perfdata: ", join(",", @exclude_perfdata), "\n" if $opt_debug;
  375. warn "(debug) Warning Thresholds:\n" if $opt_debug;
  376. for my $warnpair ( sort keys %warn_list ) { warn "$warnpair=$warn_list{$warnpair}\n" if $opt_debug; }
  377. warn "\n" if $opt_debug;
  378. # separate metric-gathering and output analysis for ATA vs SCSI SMART output
  379. # Yeah - but megaraid is the same output as ata
  380. if ($output_mode =~ "ata") {
  381. foreach my $line(@output){
  382. # get lines that look like this:
  383. # 9 Power_On_Minutes 0x0032 241 241 000 Old_age Always - 113h+12m
  384. next unless $line =~ /^\s*(\d+)\s(\S+)\s+(?:\S+\s+){6}(\S+)\s+(\d+)/;
  385. my ($attribute_number, $attribute_name, $when_failed, $raw_value) = ($1, $2, $3, $4);
  386. if ($when_failed ne '-'){
  387. # Going through exclude list
  388. if (grep {$_ eq $attribute_number || $_ eq $attribute_name || $_ eq $when_failed} @exclude_checks) {
  389. warn "SMART Attribute $attribute_name failed at $when_failed but was set to be ignored\n" if $opt_debug;
  390. } else {
  391. push(@error_messages, "Attribute $attribute_name failed at $when_failed");
  392. escalate_status('WARNING');
  393. warn "(debug) parsed SMART attribute $attribute_name with error condition:\n$when_failed\n\n" if $opt_debug;
  394. }
  395. }
  396. # some attributes produce questionable data; no need to graph them
  397. if (grep {$_ eq $attribute_name} ('Unknown_Attribute', 'Power_On_Minutes') ){
  398. next;
  399. }
  400. if (!grep {$_ eq $attribute_number || $_ eq $attribute_name} @exclude_perfdata) {
  401. push (@perfdata, "$attribute_name=$raw_value") if $opt_d;
  402. }
  403. # skip attribute if it was set to be ignored in exclude_checks
  404. if (grep {$_ eq $attribute_number || $_ eq $attribute_name} @exclude_checks) {
  405. warn "(debug) SMART Attribute $attribute_name was set to be ignored\n\n" if $opt_debug;
  406. next;
  407. } else {
  408. # manual checks on raw values for certain attributes deemed significant
  409. if (grep {$_ eq $attribute_name} @raw_check_list) {
  410. if ($raw_value > 0) {
  411. # Check for warning thresholds
  412. if ( ($warn_list{$attribute_name}) && ($raw_value >= $warn_list{$attribute_name}) ) {
  413. warn "(debug) $attribute_name is non-zero ($raw_value)\n\n" if $opt_debug;
  414. push(@error_messages, "$attribute_name is non-zero ($raw_value)");
  415. escalate_status('WARNING');
  416. } elsif ( ($warn_list{$attribute_name}) && ($raw_value < $warn_list{$attribute_name}) ) {
  417. warn "(debug) $attribute_name is non-zero ($raw_value) but less than $warn_list{$attribute_name}\n\n" if $opt_debug;
  418. push(@error_messages, "$attribute_name is non-zero ($raw_value) (but less than threshold $warn_list{$attribute_name})");
  419. }
  420. else {
  421. warn "(debug) $attribute_name is non-zero ($raw_value)\n\n" if $opt_debug;
  422. push(@error_messages, "$attribute_name is non-zero ($raw_value)");
  423. escalate_status('WARNING');
  424. }
  425. } else {
  426. warn "(debug) $attribute_name is OK ($raw_value)\n\n" if $opt_debug;
  427. }
  428. } else {
  429. warn "(debug) $attribute_name not in raw check list (raw value: $raw_value)\n\n" if $opt_debug;
  430. }
  431. }
  432. }
  433. } elsif ($output_mode =~ "nvme") {
  434. foreach my $line(@output){
  435. next unless $line =~ /(\w.+):\s+(?:(\dx\d(?:\d?\w?)|\d(?:(?:,?\s?\d+,?\s?)?)+))/;
  436. my ($attribute_name, $raw_value) = ($1, $2);
  437. $raw_value =~ s/\s|,//g;
  438. $attribute_name =~ s/\s/_/g;
  439. # some attributes produce irrelevant data; no need to graph them
  440. if (grep {$_ eq $attribute_name} ('Critical_Warning') ){
  441. push (@exclude_perfdata, "$attribute_name");
  442. }
  443. # create performance data unless defined in exclude_perfdata list
  444. if (!grep {$_ eq $attribute_name} @exclude_perfdata) {
  445. push (@perfdata, "$attribute_name=$raw_value") if $opt_d;
  446. }
  447. # skip attribute if it was set to be ignored in exclude_checks
  448. if (grep {$_ eq $attribute_name} @exclude_checks) {
  449. warn "(debug) SMART Attribute $attribute_name was set to be ignored\n\n" if $opt_debug;
  450. next;
  451. }
  452. # Handle Critical_Warning values
  453. if ($attribute_name eq 'Critical_Warning') {
  454. if ($raw_value eq '0x01') {
  455. push(@error_messages, "Available spare below threshold");
  456. escalate_status('WARNING');
  457. }
  458. elsif ($raw_value eq '0x02') {
  459. push(@error_messages, "Temperature is above or below thresholds");
  460. escalate_status('WARNING');
  461. }
  462. elsif ($raw_value eq '0x03') {
  463. push(@error_messages, "Available spare below threshold and temperature is above or below thresholds");
  464. escalate_status('WARNING');
  465. }
  466. elsif ($raw_value eq '0x04') {
  467. push(@error_messages, "NVM subsystem reliability degraded");
  468. escalate_status('WARNING');
  469. }
  470. elsif ($raw_value eq '0x05') {
  471. push(@error_messages, "Available spare below threshold and NVM subsystem reliability degraded");
  472. escalate_status('WARNING');
  473. }
  474. elsif ($raw_value eq '0x06') {
  475. push(@error_messages, "Temperature is above or below thresholds and NVM subsystem reliability degraded");
  476. escalate_status('WARNING');
  477. }
  478. elsif ($raw_value eq '0x07') {
  479. push(@error_messages, "Available spare below threshold and Temperature is above or below thresholds and NVM subsystem reliability degraded");
  480. escalate_status('WARNING');
  481. }
  482. elsif ($raw_value eq '0x08') {
  483. push(@error_messages, "Media in read only mode");
  484. escalate_status('WARNING');
  485. }
  486. elsif ($raw_value eq '0x09') {
  487. push(@error_messages, "Media in read only mode and Available spare below threshold");
  488. escalate_status('WARNING');
  489. }
  490. elsif ($raw_value eq '0x0A') {
  491. push(@error_messages, "Media in read only mode and Temperature is above or below thresholds");
  492. escalate_status('WARNING');
  493. }
  494. elsif ($raw_value eq '0x0B') {
  495. push(@error_messages, "Media in read only mode and Temperature is above or below thresholds and Available spare below threshold");
  496. escalate_status('WARNING');
  497. }
  498. elsif ($raw_value eq '0x0C') {
  499. push(@error_messages, "Media in read only mode and NVM subsystem reliability degraded");
  500. escalate_status('WARNING');
  501. }
  502. elsif ($raw_value eq '0x0D') {
  503. push(@error_messages, "Media in read only mode and NVM subsystem reliability degraded and Available spare below threshold");
  504. escalate_status('WARNING');
  505. }
  506. elsif ($raw_value eq '0x0E') {
  507. push(@error_messages, "Media in read only mode and NVM subsystem reliability degraded and Temperature is above or below thresholds");
  508. escalate_status('WARNING');
  509. }
  510. elsif ($raw_value eq '0x0F') {
  511. push(@error_messages, "Media in read only mode and NVM subsystem reliability degraded and Temperature is above or below thresholds");
  512. escalate_status('WARNING');
  513. }
  514. elsif ($raw_value eq '0x10') {
  515. push(@error_messages, "Volatile memory backup device failed");
  516. escalate_status('WARNING');
  517. }
  518. }
  519. # manual checks on raw values for certain attributes deemed significan
  520. if (grep {$_ eq $attribute_name} @raw_check_list_nvme) {
  521. if ($raw_value > 0) {
  522. # Check for warning thresholds
  523. if ( ($warn_list{$attribute_name}) && ($raw_value >= $warn_list{$attribute_name}) ) {
  524. warn "(debug) $attribute_name is non-zero ($raw_value)\n\n" if $opt_debug;
  525. push(@error_messages, "$attribute_name is non-zero ($raw_value)");
  526. escalate_status('WARNING');
  527. } elsif ( ($warn_list{$attribute_name}) && ($raw_value < $warn_list{$attribute_name}) ) {
  528. warn "(debug) $attribute_name is non-zero ($raw_value) but less than $warn_list{$attribute_name}\n\n" if $opt_debug;
  529. push(@error_messages, "$attribute_name is non-zero ($raw_value) (but less than threshold $warn_list{$attribute_name})");
  530. } else {
  531. warn "(debug) $attribute_name is non-zero ($raw_value)\n\n" if $opt_debug;
  532. push(@error_messages, "$attribute_name is non-zero ($raw_value)");
  533. escalate_status('WARNING');
  534. }
  535. } else {
  536. warn "(debug) $attribute_name is OK ($raw_value)\n\n" if $opt_debug;
  537. }
  538. } else {
  539. warn "(debug) $attribute_name not in raw check list (raw value: $raw_value)\n\n" if $opt_debug;
  540. }
  541. }
  542. } else {
  543. my ($current_temperature, $max_temperature, $current_start_stop, $max_start_stop) = qw//;
  544. foreach my $line(@output){
  545. if ($line =~ /Current Drive Temperature:\s+(\d+)/){
  546. $current_temperature = $1;
  547. }
  548. elsif ($line =~ /Drive Trip Temperature:\s+(\d+)/){
  549. $max_temperature = $1;
  550. }
  551. elsif ($line =~ /Current start stop count:\s+(\d+)/){
  552. $current_start_stop = $1;
  553. }
  554. elsif ($line =~ /Recommended maximum start stop count:\s+(\d+)/){
  555. $max_start_stop = $1;
  556. }
  557. elsif ($line =~ /Elements in grown defect list:\s+(\d+)/){
  558. my $defectlist = $1;
  559. # check for elements in grown defect list
  560. if ($opt_b) {
  561. push (@perfdata, "defect_list=$defectlist;;$opt_b") if $opt_d;
  562. if (($defectlist > 0) && ($defectlist >= $opt_b)) {
  563. push(@error_messages, "$defectlist Elements in grown defect list (threshold $opt_b)");
  564. escalate_status('WARNING');
  565. warn "(debug) Elements in grown defect list is non-zero ($defectlist)\n\n" if $opt_debug;
  566. }
  567. elsif (($defectlist > 0) && ($defectlist < $opt_b)) {
  568. push(@error_messages, "Note: $defectlist Elements in grown defect list");
  569. warn "(debug) Elements in grown defect list is non-zero ($defectlist) but less than $opt_b\n\n" if $opt_debug;
  570. }
  571. }
  572. else {
  573. if ($defectlist > 0) {
  574. push (@perfdata, "defect_list=$defectlist") if $opt_d;
  575. push(@error_messages, "$defectlist Elements in grown defect list");
  576. escalate_status('WARNING');
  577. warn "(debug) Elements in grown defect list is non-zero ($defectlist)\n\n" if $opt_debug;
  578. }
  579. }
  580. }
  581. elsif ($line =~ /Blocks sent to initiator =\s+(\d+)/){
  582. push (@perfdata, "sent_blocks=$1") if $opt_d;
  583. }
  584. }
  585. if($current_temperature){
  586. if($max_temperature){
  587. push (@perfdata, "temperature=$current_temperature;;$max_temperature") if $opt_d;
  588. if($current_temperature > $max_temperature){
  589. warn "(debug) Disk temperature is greater than max ($current_temperature > $max_temperature)\n\n" if $opt_debug;
  590. push(@error_messages, 'Disk temperature is higher than maximum');
  591. escalate_status('CRITICAL');
  592. }
  593. }
  594. else{
  595. push (@perfdata, "temperature=$current_temperature") if $opt_d;
  596. }
  597. }
  598. if($current_start_stop){
  599. if($max_start_stop){
  600. push (@perfdata, "start_stop=$current_start_stop;$max_start_stop") if $opt_d;
  601. if($current_start_stop > $max_start_stop){
  602. warn "(debug) Disk start_stop is greater than max ($current_start_stop > $max_start_stop)\n\n" if $opt_debug;
  603. push(@error_messages, 'Disk start_stop is higher than maximum');
  604. escalate_status('WARNING');
  605. }
  606. }
  607. else{
  608. push (@perfdata, "start_stop=$current_start_stop") if $opt_d;
  609. }
  610. }
  611. }
  612. warn "(debug) gathered perfdata:\n@perfdata\n\n" if $opt_debug;
  613. $perf_string = join(' ', @perfdata);
  614. warn "###########################################################\n" if $opt_debug;
  615. warn "(debug) LOCAL STATUS: $exit_status_local, FINAL STATUS: $exit_status\n" if $opt_debug;
  616. warn "###########################################################\n\n\n" if $opt_debug;
  617. if($exit_status_local ne 'OK'){
  618. if ($opt_g) {
  619. $status_string = $label.join(', ', @error_messages);
  620. }
  621. else {
  622. $drive_details = "Drive $model S/N $serial: ";
  623. $status_string = join(', ', @error_messages);
  624. }
  625. push @drives_status_not_okay, $status_string;
  626. }
  627. else {
  628. if ($opt_g) {
  629. $status_string = $label."Device is clean";
  630. }
  631. else {
  632. $drive_details = "Drive $model S/N $serial: no SMART errors detected. ";
  633. $status_string = join(', ', @error_messages);
  634. }
  635. push @drives_status_okay, $status_string;
  636. }
  637. }
  638. }
  639. warn "(debug) final status/output: $exit_status\n" if $opt_debug;
  640. my @msg_list = ($drive_details) if $drive_details;
  641. if (@drives_status_not_okay) {
  642. push @msg_list, grep { $_ } @drives_status_not_okay;
  643. }
  644. if (@drives_status_not_okay and $opt_q and @drives_status_okay) {
  645. push @msg_list, "Other drives OK";
  646. } else {
  647. push @msg_list, grep { $_ } @drives_status_okay;
  648. }
  649. if ($opt_debug) {
  650. warn "(debug) drives ok: @drives_status_okay\n";
  651. warn "(debug) drives nok: @drives_status_not_okay\n";
  652. warn "(debug) msg_list: ".join('^', @msg_list)."\n\n";
  653. }
  654. $status_string = join( ($opt_g ? $Terminator : ' '), @msg_list);
  655. # Final output: Nagios data and exit code
  656. print "$exit_status: $status_string|$perf_string\n";
  657. exit $ERRORS{$exit_status};
  658. sub print_revision {
  659. ($basename, $revision) = @_;
  660. print "$basename v$revision\n";
  661. print "The monitoring plugins come with ABSOLUTELY NO WARRANTY. You may redistribute\ncopies of the plugins under the terms of the GNU General Public License.\nFor more information about these matters, see the file named COPYING.\n";
  662. }
  663. sub print_help {
  664. print_revision($basename,$revision);
  665. print "\nUsage: $basename {-d=<block device>|-g=<block device glob>} -i=(auto|ata|scsi|3ware,N|areca,N|hpt,L/M/N|cciss,N|megaraid,N) [-r list] [-w list] [-b N] [-e list] [-E list] [--debug]\n\n";
  666. print "At least one of the below. -d supersedes -g\n";
  667. print " -d/--device: a physical block device to be SMART monitored, eg /dev/sda. Pseudo-device /dev/bus/N is allowed.\n";
  668. print " -g/--global: a glob pattern name of physical devices to be SMART monitored\n";
  669. print " Example: '/dev/sd[a-z]' will search for all /dev/sda until /dev/sdz devices and report errors globally.\n";
  670. print " It is also possible to use -g in conjunction with megaraid devices. Example: -i 'megaraid,[0-3]'.\n";
  671. print " Does not output performance data for historical value graphing.\n";
  672. print "Note that -g only works with a fixed interface (e.g. scsi, ata) and megaraid,N.\n";
  673. print "\n";
  674. print "Other options\n";
  675. print " -i/--interface: device's interface type (auto|ata|scsi|nvme|3ware,N|areca,N|hpt,L/M/N|cciss,N|megaraid,N)\n";
  676. print " (See http://www.smartmontools.org/wiki/Supported_RAID-Controllers for interface convention)\n";
  677. print " -r/--raw Comma separated list of ATA or NVMe attributes to check\n";
  678. print " ATA default: Current_Pending_Sector,Reallocated_Sector_Ct,Program_Fail_Cnt_Total,Uncorrectable_Error_Cnt,Offline_Uncorrectable,Runtime_Bad_Block\n";
  679. print " NVMe default: Media_and_Data_Integrity_Errors\n";
  680. print " -b/--bad: Threshold value for Current_Pending_Sector for ATA and 'grown defect list' for SCSI drives\n";
  681. print " -w/--warn Comma separated list of thresholds for ATA drives (e.g. Reallocated_Sector_Ct=10,Current_Pending_Sector=62)\n";
  682. print " -e/--exclude: Comma separated list of SMART attribute names or numbers which should be excluded (=ignored) with regard to checks\n";
  683. print " -E/--exclude-all: Comma separated list of SMART attribute names or numbers which should be completely ignored for checks as well as perfdata\n";
  684. print " -s/--selftest: Enable self-test log check";
  685. print " -h/--help: this help\n";
  686. print " -q/--quiet: When faults detected, only show faulted drive(s) (only affects output when used with -g parameter)\n";
  687. print " --debug: show debugging information\n";
  688. print " -v/--version: Version number\n";
  689. }
  690. # escalate an exit status IFF it's more severe than the previous exit status
  691. sub escalate_status {
  692. my $requested_status = shift;
  693. # no test for 'CRITICAL'; automatically escalates upwards
  694. if ($requested_status eq 'WARNING') {
  695. return if ($exit_status|$exit_status_local) eq 'CRITICAL';
  696. }
  697. if ($requested_status eq 'UNKNOWN') {
  698. return if ($exit_status|$exit_status_local) eq 'WARNING';
  699. return if ($exit_status|$exit_status_local) eq 'CRITICAL';
  700. }
  701. $exit_status = $requested_status;
  702. $exit_status_local = $requested_status;
  703. }