Project

General

Profile

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