Project

General

Profile

Download (13.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_smart.php
4
	Part of pfSense
5

    
6
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
7
	All rights reserved
8

    
9
	Some modifications:
10
	Copyright (C) 2010 - Jim Pingle
11

    
12
	Copyright (C) 2006, Eric Friesen
13
	All rights reserved
14

    
15
*/
16

    
17
require("guiconfig.inc");
18

    
19
$pgtitle = array(gettext("Diagnostics"), gettext("S.M.A.R.T. Monitor Tools"));
20
$smartctl = "/usr/local/sbin/smartctl";
21
$smartd = "/usr/local/sbin/smartd";
22
$start_script = "/usr/local/etc/rc.d/smartd.sh";
23

    
24
$valid_test_types = array("offline", "short", "long", "conveyance");
25
$valid_info_types = array("i", "H", "c", "A", "a");
26
$valid_log_types = array("error", "selftest");
27

    
28
$closehead = false;
29
include("head.inc");
30
?>
31

    
32
<style type="text/css">
33
/*<![CDATA[*/
34

    
35
input {
36
	font-family: courier new, courier;
37
	font-weight: normal;
38
	font-size: 9pt;
39
}
40

    
41
pre {
42
	border: 2px solid #435370;
43
	background: #F0F0F0;
44
	padding: 1em;
45
	font-family: courier new, courier;
46
	white-space: pre;
47
	line-height: 10pt;
48
	font-size: 10pt;
49
}
50

    
51
.label {
52
	font-family: tahoma, verdana, arial, helvetica;
53
	font-size: 11px;
54
	font-weight: bold;
55
}
56

    
57
.button {
58
	font-family: tahoma, verdana, arial, helvetica;
59
	font-weight: bold;
60
	font-size: 11px;
61
}
62

    
63
/*]]>*/
64
</style>
65
</head>
66
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
67

    
68
<?php
69
include("fbegin.inc");
70

    
71
// Highlights the words "PASSED", "FAILED", and "WARNING".
72
function add_colors($string)
73
{
74
	// To add words keep arrays matched by numbers
75
	$patterns[0] = '/PASSED/';
76
	$patterns[1] = '/FAILED/';
77
	$patterns[2] = '/Warning/';
78
	$replacements[0] = '<b><font color="#00ff00">' . gettext("PASSED") . '</font></b>';
79
	$replacements[1] = '<b><font color="#ff0000">' . gettext("FAILED") . '</font></b>';
80
	$replacements[2] = '<font color="#ff0000">' . gettext("Warning") . '</font>';
81
	ksort($patterns);
82
	ksort($replacements);
83
	return preg_replace($patterns, $replacements, $string);
84
}
85

    
86
// Edits smartd.conf file, adds or removes email for failed disk reporting
87
function update_email($email)
88
{
89
	// Did they pass an email?
90
	if (!empty($email)) {
91
		// Put it in the smartd.conf file
92
		shell_exec("/usr/bin/sed -i old 's/^DEVICESCAN.*/DEVICESCAN -H -m " . escapeshellarg($email) . "/' /usr/local/etc/smartd.conf");
93
	} else {
94
		// Remove email flags in smartd.conf
95
		shell_exec("/usr/bin/sed -i old 's/^DEVICESCAN.*/DEVICESCAN/' /usr/local/etc/smartd.conf");
96
	}
97
}
98

    
99
function smartmonctl($action)
100
{
101
	global $start_script;
102
	shell_exec($start_script . escapeshellarg($action));
103
}
104

    
105
// What page, aka. action is being wanted
106
// If they "get" a page but don't pass all arguments, smartctl will throw an error
107
$action = (isset($_POST['action']) ? $_POST['action'] : $_GET['action']);
108
$targetdev = basename($_POST['device']);
109
if (!file_exists('/dev/' . $targetdev)) {
110
	echo "Device does not exist, bailing.";
111
	return;
112
}
113
switch ($action) {
114
	// Testing devices
115
	case 'test':
116
	{
117
		$test = $_POST['testType'];
118
		if (!in_array($test, $valid_test_types)) {
119
			echo "Invalid test type, bailing.";
120
			return;
121
		}
122
		$output = add_colors(shell_exec($smartctl . " -t " . escapeshellarg($test) . " /dev/" . escapeshellarg($targetdev)));
123
		echo '<pre>' . $output . '
124
		<form action="diag_smart.php" method="post" name="abort">
125
		<input type="hidden" name="device" value="' . $targetdev . '" />
126
		<input type="hidden" name="action" value="abort" />
127
		<input type="submit" name="submit" value="' . gettext("Abort") . '" />
128
		</form>
129
		</pre>';
130
		break;
131
	}
132

    
133
	// Info on devices
134
	case 'info':
135
	{
136
		$type = $_POST['type'];
137
		if (!in_array($type, $valid_info_types)) {
138
			echo "Invalid info type, bailing.";
139
			return;
140
		}
141
		$output = add_colors(shell_exec($smartctl . " -" . escapeshellarg($type) . " /dev/" . escapeshellarg($targetdev)));
142
		echo "<pre>$output</pre>";
143
		break;
144
	}
145

    
146
	// View logs
147
	case 'logs':
148
	{
149
		$type = $_POST['type'];
150
		if (!in_array($type, $valid_log_types)) {
151
			echo "Invalid log type, bailing.";
152
			return;
153
		}
154
		$output = add_colors(shell_exec($smartctl . " -l " . escapeshellarg($type) . " /dev/" . escapeshellarg($targetdev)));
155
		echo "<pre>$output</pre>";
156
		break;
157
	}
158

    
159
	// Abort tests
160
	case 'abort':
161
	{
162
		$output = shell_exec($smartctl . " -X /dev/" . escapeshellarg($targetdev));
163
		echo "<pre>$output</pre>";
164
		break;
165
	}
166

    
167
	// Config changes, users email in xml config and write changes to smartd.conf
168
	case 'config':
169
	{
170
		if (isset($_POST['submit'])) {
171
			// DOES NOT WORK YET...
172
			if ($_POST['testemail']) {
173
// FIXME				shell_exec($smartd . " -M test -m " . $config['system']['smartmonemail']);
174
				$savemsg = sprintf(gettext("Email sent to %s"), $config['system']['smartmonemail']);
175
				smartmonctl("stop");
176
				smartmonctl("start");
177
			} else {
178
				$config['system']['smartmonemail'] = $_POST['smartmonemail'];
179
				write_config();
180

    
181
				// Don't know what all this means, but it adds the config changed header when config is saved
182
				$retval = 0;
183
				config_lock();
184
				if (stristr($retval, "error") <> true) {
185
					$savemsg = get_std_save_message($retval);
186
				} else {
187
					$savemsg = $retval;
188
				}
189
				config_unlock();
190

    
191
				if ($_POST['email']) {
192
					// Write the changes to the smartd.conf file
193
					update_email($_POST['smartmonemail']);
194
				}
195

    
196
				// Send sig HUP to smartd, rereads the config file
197
				shell_exec("/usr/bin/killall -HUP smartd");
198
			}
199
		}
200
		// Was the config changed? if so , print the message
201
		if ($savemsg) {
202
			print_info_box($savemsg);
203
		}
204
		// Get users email from the xml file
205
		$pconfig['smartmonemail'] = $config['system']['smartmonemail'];
206

    
207
		?>
208
		<!-- Print the tabs across the top -->
209
		<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="tabs">
210
			<tr>
211
				<td>
212
					<?php
213
					$tab_array = array();
214
					$tab_array[0] = array(gettext("Information/Tests"), false, $_SERVER['PHP_SELF'] . "?action=default");
215
					$tab_array[1] = array(gettext("Config"), true, $_SERVER['PHP_SELF'] . "?action=config");
216
					display_top_tabs($tab_array);
217
					?>
218
				</td>
219
			</tr>
220
		</table>
221
<!-- user email address -->
222
		<form action="<?= $_SERVER['PHP_SELF']?>" method="post" name="config">
223
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="e-mail">
224
			<tbody>
225
				<tr>
226
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Config"); ?></td>
227
				</tr>
228
				<tr>
229
					<td width="22%" valign="top" class="vncell"><?=gettext("Email Address"); ?></td>
230
					<td width="78%" class="vtable">
231
						<input type="text" name="smartmonemail" value="<?=htmlspecialchars($pconfig['smartmonemail'])?>"/>
232
					</td>
233
				</tr>
234
				<tr>
235
					<td width="22%" valign="top">&nbsp;</td>
236
					<td width="78%">
237
						<input type="hidden" name="action" value="config" />
238
						<input type="hidden" name="email" value="true" />
239
						<input type="submit" name="submit" value="<?=gettext("Save"); ?>" class="formbtn" />
240
					</td>
241
				</tr>
242
			</tbody>
243
		</table>
244
		</form>
245

    
246
<!-- test email -->
247
		<form action="<?= $_SERVER['PHP_SELF']?>" method="post" name="config">
248
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="test e-mail">
249
			<tbody>
250
				<tr>
251
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Test email"); ?></td>
252
				</tr>
253
				<tr>
254
					<td width="22%" valign="top" class="vncell">&nbsp;</td>
255
					<td width="78%" class="vtable">
256
						<?php printf(gettext("Send test email to %s"), $config['system']['smartmonemail']); ?>
257
					</td>
258
				</tr>
259
				<tr>
260
					<td width="22%" valign="top">&nbsp;</td>
261
					<td width="78%">
262
						<input type="hidden" name="action" value="config" />
263
						<input type="hidden" name="testemail" value="true" />
264
						<input type="submit" name="submit" value="<?=gettext("Send"); ?>" class="formbtn" />
265
					</td>
266
				</tr>
267
			</tbody>
268
		</table>
269
		</form>
270

    
271
		<?php
272
		break;
273
	}
274

    
275
	// Default page, prints the forms to view info, test, etc...
276
	default:
277
	{
278
		$devs = get_smart_drive_list();
279
		?>
280
		<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="default page">
281
			<tr>
282
				<td>
283
					<?php
284
					$tab_array = array();
285
					$tab_array[0] = array(gettext("Information/Tests"), true, $_SERVER['PHP_SELF']);
286
					//$tab_array[1] = array("Config", false, $_SERVER['PHP_SELF'] . "?action=config");
287
					display_top_tabs($tab_array);
288
					?>
289
				</td>
290
			</tr>
291
		</table>
292
<!--INFO-->
293
		<form action="<?= $_SERVER['PHP_SELF']?>" method="post" name="info">
294
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="info">
295
			<tbody>
296
				<tr>
297
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Info"); ?></td>
298
				</tr>
299
				<tr>
300
					<td width="22%" valign="top" class="vncell"><?=gettext("Info type"); ?></td>
301
					<td width="78%" class="vtable">
302
						<input type="radio" name="type" value="i" /><?=gettext("Info"); ?><br />
303
						<input type="radio" name="type" value="H" checked="checked" /><?=gettext("Health"); ?><br />
304
						<input type="radio" name="type" value="c" /><?=gettext("SMART Capabilities"); ?><br />
305
						<input type="radio" name="type" value="A" /><?=gettext("Attributes"); ?><br />
306
						<input type="radio" name="type" value="a" /><?=gettext("All"); ?><br />
307
					</td>
308
				</tr>
309
				<tr>
310
					<td width="22%" valign="top" class="vncell"><?=gettext("Device: /dev/"); ?></td>
311
					<td width="78%" class="vtable">
312
						<select name="device">
313
						<?php
314
						foreach ($devs as $dev) {
315
							echo "<option value=\"" . $dev . "\">" . $dev . "</option>";
316
						}
317
						?>
318
						</select>
319
					</td>
320
				</tr>
321
				<tr>
322
					<td width="22%" valign="top">&nbsp;</td>
323
					<td width="78%">
324
						<input type="hidden" name="action" value="info" />
325
						<input type="submit" name="submit" value="<?=gettext("View"); ?>" class="formbtn" />
326
					</td>
327
				</tr>
328
			</tbody>
329
		</table>
330
		</form>
331
<!--TESTS-->
332
		<form action="<?= $_SERVER['PHP_SELF']?>" method="post" name="tests">
333
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="tests">
334
			<tbody>
335
				<tr>
336
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Perform Self-tests"); ?></td>
337
				</tr>
338
				<tr>
339
					<td width="22%" valign="top" class="vncell"><?=gettext("Test type"); ?></td>
340
					<td width="78%" class="vtable">
341
						<input type="radio" name="testType" value="offline" /><?=gettext("Offline"); ?><br />
342
						<input type="radio" name="testType" value="short" checked="checked" /><?=gettext("Short"); ?><br />
343
						<input type="radio" name="testType" value="long" /><?=gettext("Long"); ?><br />
344
						<input type="radio" name="testType" value="conveyance" /><?=gettext("Conveyance (ATA Disks Only)"); ?><br />
345
					</td>
346
				</tr>
347
				<tr>
348
					<td width="22%" valign="top" class="vncell"><?=gettext("Device: /dev/"); ?></td>
349
					<td width="78%" class="vtable">
350
						<select name="device">
351
						<?php
352
						foreach ($devs as $dev) {
353
							echo "<option value=\"" . $dev . "\">" . $dev . "</option>";
354
						}
355
						?>
356
						</select>
357
					</td>
358
				</tr>
359
				<tr>
360
					<td width="22%" valign="top">&nbsp;</td>
361
					<td width="78%">
362
						<input type="hidden" name="action" value="test" />
363
						<input type="submit" name="submit" value="<?=gettext("Test"); ?>" class="formbtn" />
364
					</td>
365
				</tr>
366
			</tbody>
367
		</table>
368
		</form>
369
<!--LOGS-->
370
		<form action="<?= $_SERVER['PHP_SELF']?>" method="post" name="logs">
371
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="logs">
372
			<tbody>
373
				<tr>
374
					<td colspan="2" valign="top" class="listtopic"><?=gettext("View Logs"); ?></td>
375
				</tr>
376
				<tr>
377
					<td width="22%" valign="top" class="vncell"><?=gettext("Log type"); ?></td>
378
					<td width="78%" class="vtable">
379
						<input type="radio" name="type" value="error" checked="checked" /><?=gettext("Error"); ?><br />
380
						<input type="radio" name="type" value="selftest" /><?=gettext("Self-test"); ?><br />
381
					</td>
382
				</tr>
383
				<tr>
384
					<td width="22%" valign="top" class="vncell"><?=gettext("Device: /dev/"); ?></td>
385
					<td width="78%" class="vtable">
386
						<select name="device">
387
						<?php
388
						foreach ($devs as $dev) {
389
							echo "<option value=\"" . $dev . "\">" . $dev . "</option>";
390
						}
391
						?>
392
						</select>
393
					</td>
394
				</tr>
395
				<tr>
396
					<td width="22%" valign="top">&nbsp;</td>
397
					<td width="78%">
398
						<input type="hidden" name="action" value="logs" />
399
						<input type="submit" name="submit" value="<?=gettext("View"); ?>" class="formbtn" />
400
					</td>
401
				</tr>
402
			</tbody>
403
		</table>
404
		</form>
405
<!--ABORT-->
406
		<form action="<?= $_SERVER['PHP_SELF']?>" method="post" name="abort">
407
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="abort">
408
			<tbody>
409
				<tr>
410
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Abort tests"); ?></td>
411
				</tr>
412
				<tr>
413
					<td width="22%" valign="top" class="vncell"><?=gettext("Device: /dev/"); ?></td>
414
					<td width="78%" class="vtable">
415
						<select name="device">
416
						<?php
417
						foreach ($devs as $dev) {
418
							echo "<option value=\"" . $dev . "\">" . $dev . "</option>";
419
						}
420
						?>
421
						</select>
422
					</td>
423
				</tr>
424
				<tr>
425
					<td width="22%" valign="top">&nbsp;</td>
426
					<td width="78%">
427
						<input type="hidden" name="action" value="abort" />
428
						<input type="submit" name="submit" value="<?=gettext("Abort"); ?>" class="formbtn" onclick="return confirm('<?=gettext("Do you really want to abort the test?"); ?>')" />
429
					</td>
430
				</tr>
431
			</tbody>
432
		</table>
433
		</form>
434

    
435
		<?php
436
		break;
437
	}
438
}
439

    
440
// print back button on pages
441
if (isset($_POST['submit']) && $_POST['submit'] != "Save") {
442
?>
443
	<input type="button" class="formbtn" value="<?=gettext("Back");?>" onclick="window.location.href='<?=$_SERVER['PHP_SELF'];?>'" />
444
<?php
445
}
446
?>
447
<br />
448
<?php
449
if ($ulmsg) {
450
	echo "<p><strong>" . $ulmsg . "</strong></p>\n";
451
}
452
?>
453

    
454
<?php include("fend.inc"); ?>
455
</body>
456
</html>
(46-46/256)