Project

General

Profile

Download (13.9 KB) Statistics
| Branch: | Tag: | Revision:
1 21f0f60d jim-p
<?php
2
/*
3 aaec5634 Renato Botelho
 * diag_smart.php
4 fd9ebcd5 Stephen Beaver
 *
5 aaec5634 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Electric Sheep Fencing, LLC
7
 * Copyright (c) 2006 Eric Friesen
8
 * All rights reserved.
9 fd9ebcd5 Stephen Beaver
 *
10 aaec5634 Renato Botelho
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions are met:
12 fd9ebcd5 Stephen Beaver
 *
13 aaec5634 Renato Botelho
 * 1. Redistributions of source code must retain the above copyright notice,
14
 *    this list of conditions and the following disclaimer.
15 fd9ebcd5 Stephen Beaver
 *
16 aaec5634 Renato Botelho
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in
18
 *    the documentation and/or other materials provided with the
19
 *    distribution.
20 fd9ebcd5 Stephen Beaver
 *
21 aaec5634 Renato Botelho
 * 3. All advertising materials mentioning features or use of this software
22
 *    must display the following acknowledgment:
23
 *    "This product includes software developed by the pfSense Project
24
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
25 fd9ebcd5 Stephen Beaver
 *
26 aaec5634 Renato Botelho
 * 4. The names "pfSense" and "pfSense Project" must not be used to
27
 *    endorse or promote products derived from this software without
28
 *    prior written permission. For written permission, please contact
29
 *    coreteam@pfsense.org.
30 fd9ebcd5 Stephen Beaver
 *
31 aaec5634 Renato Botelho
 * 5. Products derived from this software may not be called "pfSense"
32
 *    nor may "pfSense" appear in their names without prior written
33
 *    permission of the Electric Sheep Fencing, LLC.
34 fd9ebcd5 Stephen Beaver
 *
35 aaec5634 Renato Botelho
 * 6. Redistributions of any form whatsoever must retain the following
36
 *    acknowledgment:
37 0e834fc5 Stephen Beaver
 *
38 aaec5634 Renato Botelho
 * "This product includes software developed by the pfSense Project
39
 * for use in the pfSense software distribution (http://www.pfsense.org/).
40 fd9ebcd5 Stephen Beaver
 *
41 aaec5634 Renato Botelho
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 fd9ebcd5 Stephen Beaver
 */
54 21f0f60d jim-p
55 a57d9fa2 jim-p
##|+PRIV
56
##|*IDENT=page-diagnostics-smart
57 9718847b k-paulius
##|*NAME=Diagnostics: S.M.A.R.T. Status
58
##|*DESCR=Allow access to the 'Diagnostics: S.M.A.R.T. Status' page.
59 a57d9fa2 jim-p
##|*MATCH=diag_smart.php*
60
##|-PRIV
61
62 aceaf18c Phil Davis
require_once("guiconfig.inc");
63 21f0f60d jim-p
64 d88908cf k-paulius
// What page, aka. action is being wanted
65
// If they "get" a page but don't pass all arguments, smartctl will throw an error
66
$action = (isset($_POST['action']) ? $_POST['action'] : $_GET['action']);
67
68 9718847b k-paulius
$pgtitle = array(gettext("Diagnostics"), gettext("S.M.A.R.T. Status"));
69 d88908cf k-paulius
70
if ($action != 'config') {
71 d49d6033 k-paulius
	$pgtitle[] = htmlspecialchars(gettext('Information & Tests'));
72 d88908cf k-paulius
} else {
73
	$pgtitle[] = gettext('Config');
74
}
75 21f0f60d jim-p
$smartctl = "/usr/local/sbin/smartctl";
76
$smartd = "/usr/local/sbin/smartd";
77
$start_script = "/usr/local/etc/rc.d/smartd.sh";
78
79 24879bc6 jim-p
$valid_test_types = array("offline", "short", "long", "conveyance");
80
$valid_info_types = array("i", "H", "c", "A", "a");
81
$valid_log_types = array("error", "selftest");
82
83 21f0f60d jim-p
include("head.inc");
84
85 155d9450 sbeaver
// Highlights the words "PASSED", "FAILED", and "WARNING".
86 947141fd Phil Davis
function add_colors($string) {
87 155d9450 sbeaver
	// To add words keep arrays matched by numbers
88 21f0f60d jim-p
	$patterns[0] = '/PASSED/';
89
	$patterns[1] = '/FAILED/';
90
	$patterns[2] = '/Warning/';
91 2eb51b46 Colin Fleming
	$replacements[0] = '<span class="text-success">' . gettext("PASSED") . '</span>';
92
	$replacements[1] = '<span class="text-alert">' . gettext("FAILED") . '</span>';
93
	$replacements[2] = '<span class="text-warning">' . gettext("Warning") . '</span>';
94 21f0f60d jim-p
	ksort($patterns);
95
	ksort($replacements);
96
	return preg_replace($patterns, $replacements, $string);
97
}
98
99
// Edits smartd.conf file, adds or removes email for failed disk reporting
100 699737d9 Phil Davis
function update_email($email) {
101 1e5239d1 jim-p
	/* Bail if an e-mail address is invalid */
102
	if (!empty($email) && (filter_var($email, FILTER_VALIDATE_EMAIL) === false)) {
103
		return;
104
	}
105
106
	if (!file_exists("/usr/local/etc/smartd.conf") && file_exists("/usr/local/etc/smartd.conf.sample")) {
107
		copy("/usr/local/etc/smartd.conf.sample", "/usr/local/etc/smartd.conf");
108
	}
109 21f0f60d jim-p
	// Did they pass an email?
110 5f601060 Phil Davis
	if (!empty($email)) {
111 21f0f60d jim-p
		// Put it in the smartd.conf file
112 5c4b89a4 jim-p
		shell_exec("/usr/bin/sed -i .old " . escapeshellarg("s/^DEVICESCAN.*/DEVICESCAN -H -m {$email}/") . " /usr/local/etc/smartd.conf");
113 5f601060 Phil Davis
	} else {
114 21f0f60d jim-p
		// Remove email flags in smartd.conf
115 1e5239d1 jim-p
		shell_exec("/usr/bin/sed -i .old 's/^DEVICESCAN.*/DEVICESCAN/' /usr/local/etc/smartd.conf");
116 21f0f60d jim-p
	}
117
}
118
119 699737d9 Phil Davis
function smartmonctl($action) {
120 21f0f60d jim-p
	global $start_script;
121 d31ca336 Renato Botelho
	shell_exec($start_script . escapeshellarg($action));
122 21f0f60d jim-p
}
123 24879bc6 jim-p
$targetdev = basename($_POST['device']);
124 155d9450 sbeaver
125 24879bc6 jim-p
if (!file_exists('/dev/' . $targetdev)) {
126 ff30e319 bruno
	echo gettext("Device does not exist, bailing.");
127 24879bc6 jim-p
	return;
128
}
129 155d9450 sbeaver
130
$tab_array = array();
131 d49d6033 k-paulius
$tab_array[0] = array(htmlspecialchars(gettext("Information & Tests")), ($action != 'config'), $_SERVER['PHP_SELF'] . "?action=default");
132 155d9450 sbeaver
$tab_array[1] = array(gettext("Config"), ($action == 'config'), $_SERVER['PHP_SELF'] . "?action=config");
133
display_top_tabs($tab_array);
134
135 df5c2c73 Chris Buechler
$specplatform = system_identify_specific_platform();
136
if ($specplatform['name'] == "Hyper-V") {
137
	echo gettext("S.M.A.R.T. is not supported in Hyper-V guests.");
138
	include("foot.inc");
139
	exit;
140
}
141
142 288a2a0f Phil Davis
switch ($action) {
143 21f0f60d jim-p
	// Testing devices
144
	case 'test':
145
	{
146
		$test = $_POST['testType'];
147 24879bc6 jim-p
		if (!in_array($test, $valid_test_types)) {
148 ff30e319 bruno
			echo gettext("Invalid test type, bailing.");
149 24879bc6 jim-p
			return;
150
		}
151 155d9450 sbeaver
152 24879bc6 jim-p
		$output = add_colors(shell_exec($smartctl . " -t " . escapeshellarg($test) . " /dev/" . escapeshellarg($targetdev)));
153 155d9450 sbeaver
?>
154
		<div class="panel  panel-default">
155 3d7a8696 k-paulius
			<div class="panel-heading"><h2 class="panel-title"><?=gettext('Test Results')?></h2></div>
156 155d9450 sbeaver
			<div class="panel-body">
157
				<pre><?=$output?></pre>
158
			</div>
159
		</div>
160
161 1d2add88 jim-p
		<form action="diag_smart.php" method="post" name="abort">
162 155d9450 sbeaver
			<input type="hidden" name="device" value="<?=$targetdev?>" />
163
			<input type="hidden" name="action" value="abort" />
164
			<nav class="action-buttons">
165 37676f4e jim-p
				<button type="submit" name="submit" class="btn btn-danger" value="<?=gettext("Abort")?>">
166
					<i class="fa fa-times icon-embed-btn"></i>
167
					<?=gettext("Abort Test")?>
168
				</button>
169
				<a href="<?=$_SERVER['PHP_SELF']?>" class="btn btn-info">
170
					<i class="fa fa-undo icon-embed-btn"></i>
171
					<?=gettext("Back")?>
172
				</a>
173 155d9450 sbeaver
			</nav>
174 21f0f60d jim-p
		</form>
175 155d9450 sbeaver
176
<?php
177 21f0f60d jim-p
		break;
178
	}
179
180
	// Info on devices
181
	case 'info':
182
	{
183
		$type = $_POST['type'];
184 155d9450 sbeaver
185 24879bc6 jim-p
		if (!in_array($type, $valid_info_types)) {
186 155d9450 sbeaver
			print_info_box(gettext("Invalid info type, bailing."), 'danger');
187 24879bc6 jim-p
			return;
188
		}
189 155d9450 sbeaver
190 24879bc6 jim-p
		$output = add_colors(shell_exec($smartctl . " -" . escapeshellarg($type) . " /dev/" . escapeshellarg($targetdev)));
191 155d9450 sbeaver
?>
192
		<div class="panel  panel-default">
193 f17594c7 Sjon Hortensius
			<div class="panel-heading"><h2 class="panel-title"><?=gettext('Information')?></h2></div>
194 155d9450 sbeaver
			<div class="panel-body">
195
				<pre><?=$output?></pre>
196
			</div>
197
		</div>
198
199
		<nav class="action-buttons">
200 37676f4e jim-p
			<a href="<?=$_SERVER['PHP_SELF']?>" class="btn btn-info">
201
				<i class="fa fa-undo icon-embed-btn"></i>
202
				<?=gettext("Back")?>
203
			</a>
204 155d9450 sbeaver
		</nav>
205
<?php
206 21f0f60d jim-p
		break;
207
	}
208
209
	// View logs
210
	case 'logs':
211
	{
212
		$type = $_POST['type'];
213 24879bc6 jim-p
		if (!in_array($type, $valid_log_types)) {
214 155d9450 sbeaver
			print_info_box(gettext("Invalid log type, bailing."), 'danger');
215 24879bc6 jim-p
			return;
216
		}
217 155d9450 sbeaver
218 24879bc6 jim-p
		$output = add_colors(shell_exec($smartctl . " -l " . escapeshellarg($type) . " /dev/" . escapeshellarg($targetdev)));
219 155d9450 sbeaver
?>
220
		<div class="panel  panel-default">
221 f17594c7 Sjon Hortensius
			<div class="panel-heading"><h2 class="panel-title"><?=gettext('Logs')?></h2></div>
222 155d9450 sbeaver
			<div class="panel-body">
223
				<pre><?=$output?></pre>
224
			</div>
225
		</div>
226
227
		<nav class="action-buttons">
228 37676f4e jim-p
			<a href="<?=$_SERVER['PHP_SELF']?>" class="btn btn-info">
229
				<i class="fa fa-undo icon-embed-btn"></i>
230
				<?=gettext("Back")?>
231
			</a>
232 155d9450 sbeaver
		</nav>
233
<?php
234 21f0f60d jim-p
		break;
235
	}
236
237
	// Abort tests
238
	case 'abort':
239
	{
240 24879bc6 jim-p
		$output = shell_exec($smartctl . " -X /dev/" . escapeshellarg($targetdev));
241 155d9450 sbeaver
?>
242
		<div class="panel  panel-default">
243 f17594c7 Sjon Hortensius
			<div class="panel-heading"><h2 class="panel-title"><?=gettext('Abort')?></h2></div>
244 155d9450 sbeaver
			<div class="panel-body">
245
				<pre><?=$output?></pre>
246
			</div>
247
		</div>
248
<?php
249 21f0f60d jim-p
		break;
250
	}
251
252
	// Config changes, users email in xml config and write changes to smartd.conf
253
	case 'config':
254
	{
255 288a2a0f Phil Davis
		if (isset($_POST['test'])) {
256 155d9450 sbeaver
257 21f0f60d jim-p
// FIXME				shell_exec($smartd . " -M test -m " . $config['system']['smartmonemail']);
258 155d9450 sbeaver
			$savemsg = sprintf(gettext("Email sent to %s"), $config['system']['smartmonemail']);
259
			smartmonctl("stop");
260
			smartmonctl("start");
261
			$style = 'warning';
262 947141fd Phil Davis
		} else if (isset($_POST['save'])) {
263 1e5239d1 jim-p
			if (!empty($_POST['smartmonemail']) && (filter_var($_POST['smartmonemail'], FILTER_VALIDATE_EMAIL) === false)) {
264
				$savemsg = "The supplied e-mail address is invalid.";
265
				$style = 'danger';
266 947141fd Phil Davis
			} else {
267 1e5239d1 jim-p
				$config['system']['smartmonemail'] = $_POST['smartmonemail'];
268
				write_config();
269
				$retval = 0;
270
				config_lock();
271
				if (stristr($retval, "error") != true) {
272
					$savemsg = get_std_save_message($retval);
273
					$style = 'success';
274
				} else {
275
					$savemsg = $retval;
276
					$style='danger';
277
				}
278
				config_unlock();
279
				// Write the changes to the smartd.conf file
280
				update_email($_POST['smartmonemail']);
281
				// Send sig HUP to smartd, rereads the config file
282
				shell_exec("/usr/bin/killall -HUP smartd");
283 21f0f60d jim-p
			}
284
		}
285
286 288a2a0f Phil Davis
	// Was the config changed? if so, print the message
287 947141fd Phil Davis
	if ($savemsg) {
288 155d9450 sbeaver
		print_info_box($savemsg, $style);
289 947141fd Phil Davis
	}
290 21f0f60d jim-p
291 155d9450 sbeaver
	// Get users email from the xml file
292
	$pconfig['smartmonemail'] = $config['system']['smartmonemail'];
293
294
	$form = new Form();
295
296
	$section = new Form_Section('Configuration');
297
298
	$section->addInput(new Form_Input(
299
		'smartmonemail',
300
		'Email Address',
301
		'text',
302
		$pconfig['smartmonemail']
303
	 ));
304
305
	$form->add($section);
306
307 288a2a0f Phil Davis
	if (!empty($pconfig['smartmonemail'])) {
308 155d9450 sbeaver
		$form->addGlobal(new Form_Button(
309
			'test',
310 faab522f Renato Botelho
			'Send test email',
311 37676f4e jim-p
			null,
312
			'fa-send'
313
		))->addClass('btn-info');
314 155d9450 sbeaver
	}
315
316
	print($form);
317
318
	break;
319 21f0f60d jim-p
	}
320
321
	// Default page, prints the forms to view info, test, etc...
322 155d9450 sbeaver
	default: {
323 0da0d43e Phil Davis
// Information
324 ea20169a jim-p
		$devs = get_smart_drive_list();
325 21f0f60d jim-p
326 d254b99e Stephen Beaver
		$form = new Form(false);
327
328
		$btnview = new Form_Button(
329 155d9450 sbeaver
			'submit',
330 faab522f Renato Botelho
			'View',
331 37676f4e jim-p
			null,
332 84d961c3 jim-p
			'fa-file-text-o'
333 d254b99e Stephen Beaver
		);
334 37676f4e jim-p
		$btnview->addClass('btn-primary');
335 4d3a1005 NOYB
		$btnview->setAttribute('id');
336
337 155d9450 sbeaver
		$section = new Form_Section('Information');
338
339
		$section->addInput(new Form_Input(
340
			'action',
341
			null,
342
			'hidden',
343
			'info'
344 4d3a1005 NOYB
		))->setAttribute('id');
345 155d9450 sbeaver
346
		$group = new Form_Group('Info type');
347
348
		$group->add(new Form_Checkbox(
349
			'type',
350
			null,
351
			'Info',
352
			false,
353
			'i'
354
		))->displayAsRadio();
355
356
		$group->add(new Form_Checkbox(
357
			'type',
358
			null,
359
			'Health',
360
			true,
361
			'H'
362
		))->displayAsRadio();
363
364
		$group->add(new Form_Checkbox(
365
			'type',
366
			null,
367 9718847b k-paulius
			'S.M.A.R.T. Capabilities',
368 155d9450 sbeaver
			false,
369
			'c'
370
		))->displayAsRadio();
371
372
		$group->add(new Form_Checkbox(
373
			'type',
374
			null,
375
			'Attributes',
376
			false,
377
			'A'
378
		))->displayAsRadio();
379
380
		$group->add(new Form_Checkbox(
381
			'type',
382
			null,
383
			'All',
384
			false,
385
			'a'
386
		))->displayAsRadio();
387
388
		$section->add($group);
389
390
		$section->addInput(new Form_Select(
391
			'device',
392
			'Device: /dev/',
393
			false,
394
			array_combine($devs, $devs)
395 4d3a1005 NOYB
		))->setAttribute('id');
396 155d9450 sbeaver
397 d254b99e Stephen Beaver
		$section->addInput(new Form_StaticText(
398
			'',
399
			$btnview
400
		));
401
402 155d9450 sbeaver
		$form->add($section);
403
		print($form);
404
405
// Tests
406 d254b99e Stephen Beaver
		$form = new Form(false);
407
408
		$btntest = new Form_Button(
409 155d9450 sbeaver
			'submit',
410 faab522f Renato Botelho
			'Test',
411 37676f4e jim-p
			null,
412
			'fa-wrench'
413 d254b99e Stephen Beaver
		);
414 37676f4e jim-p
		$btntest->addClass('btn-primary');
415 4d3a1005 NOYB
		$btntest->setAttribute('id');
416
417 155d9450 sbeaver
		$section = new Form_Section('Perform self-tests');
418
419
		$section->addInput(new Form_Input(
420
			'action',
421
			null,
422
			'hidden',
423
			'test'
424 4d3a1005 NOYB
		))->setAttribute('id');
425 155d9450 sbeaver
426
		$group = new Form_Group('Test type');
427
428
		$group->add(new Form_Checkbox(
429
			'testType',
430
			null,
431
			'Offline',
432
			false,
433
			'offline'
434
		))->displayAsRadio();
435
436
		$group->add(new Form_Checkbox(
437
			'testType',
438
			null,
439
			'Short',
440
			true,
441
			'short'
442
		))->displayAsRadio();
443
444
		$group->add(new Form_Checkbox(
445
			'testType',
446
			null,
447
			'Long',
448
			false,
449
			'long'
450
		))->displayAsRadio();
451
452
		$group->add(new Form_Checkbox(
453
			'testType',
454
			null,
455
			'Conveyance',
456
			false,
457
			'conveyance'
458
		))->displayAsRadio();
459
460 c6d73876 NOYB
		$group->setHelp('Select "Conveyance" for ATA disks only.');
461 155d9450 sbeaver
		$section->add($group);
462
463
		$section->addInput(new Form_Select(
464
			'device',
465
			'Device: /dev/',
466
			false,
467
			array_combine($devs, $devs)
468 4d3a1005 NOYB
		))->setAttribute('id');
469 155d9450 sbeaver
470 d254b99e Stephen Beaver
		$section->addInput(new Form_StaticText(
471
			'',
472
			$btntest
473
		));
474
475 155d9450 sbeaver
		$form->add($section);
476
		print($form);
477
478
// Logs
479 d254b99e Stephen Beaver
		$form = new Form(false);
480
481
		$btnview =  new Form_Button(
482 155d9450 sbeaver
			'submit',
483 faab522f Renato Botelho
			'View',
484 37676f4e jim-p
			null,
485 84d961c3 jim-p
			'fa-file-text-o'
486 d254b99e Stephen Beaver
		);
487 37676f4e jim-p
		$btnview->addClass('btn-primary');
488 4d3a1005 NOYB
		$btnview->setAttribute('id');
489
490 5f88f964 k-paulius
		$section = new Form_Section('View Logs');
491 155d9450 sbeaver
492
		$section->addInput(new Form_Input(
493
			'action',
494
			null,
495
			'hidden',
496
			'logs'
497 4d3a1005 NOYB
		))->setAttribute('id');
498 155d9450 sbeaver
499
		$group = new Form_Group('Log type');
500
501
		$group->add(new Form_Checkbox(
502
			'type',
503
			null,
504
			'Error',
505
			true,
506
			'error'
507
		))->displayAsRadio();
508
509
		$group->add(new Form_Checkbox(
510 655c577b Stephen Beaver
			'type',
511 155d9450 sbeaver
			null,
512
			'Self-test',
513
			false,
514
			'selftest'
515
		))->displayAsRadio();
516
517
		$section->add($group);
518
519
		$section->addInput(new Form_Select(
520
			'device',
521
			'Device: /dev/',
522
			false,
523
			array_combine($devs, $devs)
524 4d3a1005 NOYB
		))->setAttribute('id');
525 155d9450 sbeaver
526 d254b99e Stephen Beaver
		$section->addInput(new Form_StaticText(
527
			'',
528
			$btnview
529
		));
530
531 155d9450 sbeaver
		$form->add($section);
532
		print($form);
533
534
// Abort
535
		$btnabort = new Form_Button(
536
			'submit',
537 faab522f Renato Botelho
			'Abort',
538 37676f4e jim-p
			null,
539
			'fa-times'
540 155d9450 sbeaver
		);
541
542 37676f4e jim-p
		$btnabort->addClass('btn-danger')->setAttribute('id');
543 155d9450 sbeaver
544 d254b99e Stephen Beaver
		$form = new Form(false);
545 155d9450 sbeaver
546
		$section = new Form_Section('Abort');
547
548
		$section->addInput(new Form_Input(
549
			'action',
550
			null,
551
			'hidden',
552
			'abort'
553 4d3a1005 NOYB
		))->setAttribute('id');
554 155d9450 sbeaver
555
		$section->addInput(new Form_Select(
556
			'device',
557
			'Device: /dev/',
558
			false,
559
			array_combine($devs, $devs)
560 4d3a1005 NOYB
		))->setAttribute('id');
561 155d9450 sbeaver
562 d254b99e Stephen Beaver
		$section->addInput(new Form_StaticText(
563
			'',
564
			$btnabort
565
		));
566
567 155d9450 sbeaver
		$form->add($section);
568
		print($form);
569
570 21f0f60d jim-p
		break;
571
	}
572
}
573
574 c10cb196 Stephen Beaver
include("foot.inc");