Projet

Général

Profil

Télécharger (13,1 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / diag_smart.php @ e41ab9aa

1
<?php
2
/*
3
	Part of pfSense
4

    
5
	Copyright (C) 2006, Eric Friesen
6
	All rights reserved
7

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

    
12
require("guiconfig.inc");
13

    
14
$pgtitle = array(gettext("Diagnostics"), gettext("S.M.A.R.T. Monitor Tools"));
15
$smartctl = "/usr/local/sbin/smartctl";
16
$smartd = "/usr/local/sbin/smartd";
17
$start_script = "/usr/local/etc/rc.d/smartd.sh";
18

    
19
$valid_test_types = array("offline", "short", "long", "conveyance");
20
$valid_info_types = array("i", "H", "c", "A", "a");
21
$valid_log_types = array("error", "selftest");
22

    
23

    
24
include("head.inc");
25
?>
26

    
27
<style>
28
<!--
29

    
30
input {
31
	font-family: courier new, courier;
32
	font-weight: normal;
33
	font-size: 9pt;
34
}
35

    
36
pre {
37
	border: 2px solid #435370;
38
	background: #F0F0F0;
39
	padding: 1em;
40
	font-family: courier new, courier;
41
	white-space: pre;
42
	line-height: 10pt;
43
	font-size: 10pt;
44
}
45

    
46
.label {
47
	font-family: tahoma, verdana, arial, helvetica;
48
	font-size: 11px;
49
	font-weight: bold;
50
}
51

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

    
58
-->
59
</style>
60
</head>
61
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
62

    
63
<?php 
64
include("fbegin.inc"); 
65

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

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

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

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

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

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

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

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

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

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

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

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

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

    
272
		<?php
273
		break;
274
	}
275

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

    
441
		<?php
442
		break;
443
	}
444
}
445

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

    
455
<?php include("fend.inc"); ?>
456
</body>
457
</html>
(44-44/246)