Subversion Repositories vnag

Rev

Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 daniel-mar 1
<?php /* <ViaThinkSoftSignature>
53 daniel-mar 2
FjPoJmd4Q5+yJRzZt4FvBkVzDOwgeHFuKiqEvpXwJ5MHmkZH2YMsSFb7qKCJRus5Z
3
8PDQ8fm6MhDtXJ7N6hiI/v2tEe2zxr13q0e5ATcg6hDtZrvxks6QNwxa2Qb14q4en
4
tYShvmESYxJAO0IJkZglEFVRyovxH2zm6YgkNbMhf9IReupg2A2TvWdxvyAYXtn4e
5
/L18YW84bH2BaF6e7csex4U66yVbciMZERor6aDDQTtw32Dy1f+4X75jqkcEuYsy+
6
rgmsAhJFOAeghSRRtbA87y26wpP8l/ima9PmmIMc7zsVJ8iuuNd5Px169m23KohpW
7
8WKX8CuIhaaVNfa3UQM2yuM/Kj6cvYCNldouvMtGUpyXyX5ZIOM2b8aaEoDHXAtbC
8
SzSGeTMCDXWFsAoygwQ97fYAoXvzPXnYylIbMpLCZ5EoUyb7raF3x/6asPU7uVIWj
9
y8kXL9VoBb1Dbr04drAmMzqTrH/37/+aymJ/KN7XbPwKtLbayDoBcQ/0Kt+5zVLFC
10
Afe7r9IAKPLb76upMFxzwc4NE2IKe08a/Vis925gKki++Z55XuJtmz1UbBb0NqFdb
11
MtxlFje7sAUHBjw+w5vvfWrKP18vkJlmQU7humm731fzwkqgr/LpFQpTCw6MD3CzW
12
iZtVsLre1Pw4AYe5QkM33u0b1RioOI/KsRKYk6FZodK/nDU1v/EKE5Ixa0JcvEDUw
13
/vPt9Vx3gPF8XUcM5mt5oqzr6R+HnozBbl5mLTCZCAtXExTxpSNhc0JLMfZKLWxwt
14
lHqQrlNBVMXf9KjcI0yRreh3Ir4VTbXjWvkD+8owA+I3edNfj9ZNiT1cC5lwFM35W
15
o0jpqa0nV02pyM+p/anlQtQ8mlPppv4kPv0yCuOL+WbJu7ilqPusyFYmKdVBYN91L
16
zqBHyrddZI0uzf1Ki22GibIrfXSWx24COQFPY9ZtkuHYg6Tuh5yISfOwb2fh6A0zl
17
Aw7J5xDU0ChQh1AWO+sly+ytIG0/dhAqXmyN0IdRnj/GsZddMm9iczdzXM6SAUFI9
18
roosg36lsk2J1Xrl1feRsuPdYdhkJexYhJybAf3Qs0IvbPXbb1UQ9PW8vi1DXXh9G
19
gL7aG6YqLnzTxeSW6uah/tHoc4GfAxWNq2M6OEPte8KWS6hJI3YURLaeKbc7X5zt6
20
5s1JuuwWeLqF76jjaDy2Gq857qT8fBNqx7p1b3X4DbxpNcFdMQiJrXBB8BdygFKEL
21
AcRnNZKmjc7+XBo4HoElAnXc3H9xflbq+F5Y5360R52b9uw17hp4TdelAzut10uOK
22
FFGGSHEVYDau3FAn3iq2dlL4AwDWcSt/myudVuaFNeoVFSpKxMwiBpb+EhFGBRoj4
23
w==
4 daniel-mar 24
</ViaThinkSoftSignature> */ ?>
2 daniel-mar 25
<?php
26
 
27
/*
28
 * VNag - Nagios Framework for PHP
29
 * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
30
 * Licensed under the terms of the Apache 2.0 license
31
 *
49 daniel-mar 32
 * Revision 2022-05-11
2 daniel-mar 33
 */
34
 
35
declare(ticks=1);
36
 
37
class SmartCheck extends VNag {
49 daniel-mar 38
        protected $argType = null;
39
 
2 daniel-mar 40
        public function __construct() {
41
                parent::__construct();
42
 
43
                if ($this->is_http_mode()) {
44
                        // Don't allow the standard arguments via $_REQUEST
45
                        $this->registerExpectedStandardArguments('');
46
                } else {
47
                        $this->registerExpectedStandardArguments('Vhtv');
48
                }
49
 
50
                $this->getHelpManager()->setPluginName('vnag_smart');
51
                $this->getHelpManager()->setVersion('1.0');
52
                $this->getHelpManager()->setShortDescription('This plugin checks the contents of the SMART data and warns when a harddisk has failed.');
53
                $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
49 daniel-mar 54
                $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-T <type>]');
2 daniel-mar 55
                $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
49 daniel-mar 56
 
57
                // TODO: also add a command to check a single drive   [ -d /dev/sda,/dev/sdb ]
58
                // Individual (non-standard) arguments:
50 daniel-mar 59
                $this->addExpectedArgument($this->argType = new VNagArgument('T', 'type', VNagArgument::VALUE_REQUIRED, 'type', 'Explicit drive type e.g. for RAID devices "sat+cciss,0" for drive 0.'));
2 daniel-mar 60
        }
61
 
62
        private function check_smart($dev) {
63
                if (!`which which`) {
64
                        throw new VNagException("Program 'which' is not installed on your system");
65
                }
66
 
67
                if (!`which smartctl`) {
68
                        throw new VNagException("Program 'smartctl' (usually included in package smartmontools) is not installed on your system");
69
                }
70
 
28 daniel-mar 71
                $code = 0;
72
                $out = array();
49 daniel-mar 73
 
74
                if ($this->argType->getValue() != '') {
53 daniel-mar 75
                        // Note: Requires root
76
                        exec('smartctl --all '.escapeshellarg($dev).' -d '.escapeshellarg($this->argType->getValue()), $out, $code);
49 daniel-mar 77
                } else {
53 daniel-mar 78
                        // Note: Requires root
79
                        exec('smartctl --all '.escapeshellarg($dev), $out, $code);
49 daniel-mar 80
                }
2 daniel-mar 81
                $cont = implode("\n", $out);
82
 
83
                $msg = array();
84
                $status = -1;
85
 
86
                if (stripos($cont, 'device lacks SMART capability') !== false)  {
87
                        // At my system (Debian 9), I get exit code 4 (which is not fully accurate)
88
                        $msg[] = 'Device lacks SMART capability';
89
                        #$status = VNag::STATUS_UNKNOWN;
90
                } else if ($code == 0) {
91
                        $status = VNag::STATUS_OK;
92
                } else {
93
                        if ($code & 1) {
94
                                throw new Exception("smartctl reports 'command line did not parse' (code $code).");
95
                        }
96
                        if ($code & 2) {
97
                                $msg[] = "Device open failed. It is either completely defective, or in low-power mode.";
98
                                $status = max($status, VNag::STATUS_CRITICAL);
99
                        }
100
                        if ($code & 4) {
101
                                $msg[] = "SMART command failed or checksum is wrong.";
102
                                $status = max($status, VNag::STATUS_WARNING);
103
                        }
104
                        if ($code & 8) {
105
                                $msg[] = "SMART status returns 'DISK FAILING'";
106
                                $status = max($status, VNag::STATUS_CRITICAL);
107
                        }
108
                        if ($code & 16) {
109
                                $msg[] = "SMART found prefail attributes below threshold";
110
                                $status = max($status, VNag::STATUS_WARNING);
111
                        }
112
                        if ($code & 32) {
113
                                $msg[] = "SMART status is 'OK' but usage/prefail attributes have been below threshold in the past.";
114
                                $status = max($status, VNag::STATUS_WARNING);
115
                        }
116
                        if ($code & 64) {
117
                                $msg[] = "The device error log contains records of errors.";
118
                                $status = max($status, VNag::STATUS_WARNING);
119
                        }
120
                        if ($code & 128) {
121
                                $msg[] = "The self-test logs contains records of errors.";
122
                                $status = max($status, VNag::STATUS_WARNING);
123
                        }
124
                }
125
 
126
                $messages = implode(", ", $msg);
127
                if ($messages != '') $messages = ": $messages";
128
 
129
                if ($status == VNag::STATUS_CRITICAL) {
130
                        $this->addVerboseMessage("$dev (Critical)$messages", VNag::VERBOSITY_SUMMARY);
131
                } else if ($status == VNag::STATUS_WARNING) {
132
                        $this->addVerboseMessage("$dev (Warning)$messages", VNag::VERBOSITY_SUMMARY);
133
                } else if ($status == VNag::STATUS_OK) {
134
                        $this->addVerboseMessage("$dev (OK)$messages", VNag::VERBOSITY_ADDITIONAL_INFORMATION);
135
                } else {
136
                        $status = VNag::STATUS_UNKNOWN;
137
                        $this->addVerboseMessage("$dev (Unknown)$messages", VNag::VERBOSITY_SUMMARY);
138
                }
139
                $this->setStatus($status);
140
                return $status;
141
        }
142
 
143
        protected function cbRun() {
144
                $devices = array();
145
                $devices = array_merge($devices, glob('/dev/sd?'));
146
                $devices = array_merge($devices, glob('/dev/hd?'));
147
 
49 daniel-mar 148
                if (count($devices) == 0) {
149
                        throw new Exception("No SDx or HDx drives found");
150
                }
151
 
152
                if (strpos($this->argType->getValue(),'cciss') !== false) {
153
                        $devices = array($devices[0]); // we just need a "fake" drive; the drive number is given as parameter to cciss
154
                }
155
 
2 daniel-mar 156
                $count_total = 0;
157
                $count_ok = 0;
158
                $count_warning = 0;
159
                $count_critical = 0;
160
                $count_unknown = 0;
161
                foreach ($devices as $dev) {
162
                        $count_total++;
163
                        switch ($this->check_smart($dev)) {
164
                                case VNag::STATUS_OK:
165
                                        $count_ok++;
166
                                        break;
167
                                case VNag::STATUS_WARNING:
168
                                        $count_warning++;
169
                                        break;
170
                                case VNag::STATUS_CRITICAL:
171
                                        $count_critical++;
172
                                        break;
173
                                case VNag::STATUS_UNKNOWN:
174
                                        $count_unknown++;
175
                                        break;
176
                        }
177
                }
178
 
179
                $this->setHeadline(sprintf('Checked %d drives (%d OK, %d warning, %d critical, %d unknown)', $count_total, $count_ok, $count_warning, $count_critical, $count_unknown));
180
        }
181
}