Project

General

Profile

Download (13.3 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
// Highlates the words "PASSED", "FAILED", and "WARNING".
72
function add_colors($string)
73
{
74
	// To add words keep arrayes 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
	{
92
		// Put it in the smartd.conf file
93
		shell_exec("/usr/bin/sed -i old 's/^DEVICESCAN.*/DEVICESCAN -H -m " . escapeshellarg($email) . "/' /usr/local/etc/smartd.conf");
94
	}
95
	// Nope
96
	else
97
	{
98
		// Remove email flags in smartd.conf
99
		shell_exec("/usr/bin/sed -i old 's/^DEVICESCAN.*/DEVICESCAN/' /usr/local/etc/smartd.conf");
100
	}
101
}
102

    
103
function smartmonctl($action)
104
{
105
	global $start_script;
106
	shell_exec($start_script . escapeshellarg($action));
107
}
108

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

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

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

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

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

    
189
				// Don't know what all this means, but it addes the config changed header when config is saved
190
				$retval = 0;
191
				config_lock();
192
				if(stristr($retval, "error") <> true)
193
					$savemsg = get_std_save_message($retval);
194
				else
195
					$savemsg = $retval;
196
				config_unlock();
197

    
198
				if($_POST['email'])
199
				{
200
					// Write the changes to the smartd.conf file
201
					update_email($_POST['smartmonemail']);
202
				}
203

    
204
				// Send sig HUP to smartd, rereads the config file
205
				shell_exec("/usr/bin/killall -HUP smartd");
206
			}
207
		}
208
		// Was the config changed? if so , print the message
209
		if ($savemsg) print_info_box($savemsg);
210
		// Get users email from the xml file
211
		$pconfig['smartmonemail'] = $config['system']['smartmonemail'];
212

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

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

    
277
		<?php
278
		break;
279
	}
280

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

    
445
		<?php
446
		break;
447
	}
448
}
449

    
450
// print back button on pages
451
if(isset($_POST['submit']) && $_POST['submit'] != "Save")
452
{
453
	echo '<br /><a href="' . $_SERVER['PHP_SELF'] . '">' . gettext("Back") . '</a>';
454
}
455
?>
456
<br />
457
<?php if ($ulmsg) echo "<p><strong>" . $ulmsg . "</strong></p>\n"; ?>
458

    
459
<?php include("fend.inc"); ?>
460
</body>
461
</html>
(46-46/256)