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
	// To add words keep arrays matched by numbers
74
	$patterns[0] = '/PASSED/';
75
	$patterns[1] = '/FAILED/';
76
	$patterns[2] = '/Warning/';
77
	$replacements[0] = '<b><font color="#00ff00">' . gettext("PASSED") . '</font></b>';
78
	$replacements[1] = '<b><font color="#ff0000">' . gettext("FAILED") . '</font></b>';
79
	$replacements[2] = '<font color="#ff0000">' . gettext("Warning") . '</font>';
80
	ksort($patterns);
81
	ksort($replacements);
82
	return preg_replace($patterns, $replacements, $string);
83
}
84

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

    
97
function smartmonctl($action) {
98
	global $start_script;
99
	shell_exec($start_script . escapeshellarg($action));
100
}
101

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

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

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

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

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

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

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

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

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

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

    
268
		<?php
269
		break;
270
	}
271

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

    
432
		<?php
433
		break;
434
	}
435
}
436

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

    
451
<?php include("fend.inc"); ?>
452
</body>
453
</html>
(46-46/252)