Project

General

Profile

Download (9.29 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * diag_smart.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2006 Eric Friesen
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
##|+PRIV
24
##|*IDENT=page-diagnostics-smart
25
##|*NAME=Diagnostics: S.M.A.R.T. Status
26
##|*DESCR=Allow access to the 'Diagnostics: S.M.A.R.T. Status' page.
27
##|*MATCH=diag_smart.php*
28
##|-PRIV
29

    
30
require_once("guiconfig.inc");
31

    
32
// What page, aka. action is being wanted
33
// If they "get" a page but don't pass all arguments, smartctl will throw an error
34
$action = (isset($_POST['action']) ? $_POST['action'] : $_GET['action']);
35

    
36
$pgtitle = array(gettext("Diagnostics"), gettext("S.M.A.R.T. Status"));
37

    
38
if ($action != 'config') {
39
	$pgtitle[] = htmlspecialchars(gettext('Information & Tests'));
40
} else {
41
	$pgtitle[] = gettext('Config');
42
}
43
$smartctl = "/usr/local/sbin/smartctl";
44

    
45
$valid_test_types = array("offline", "short", "long", "conveyance");
46
$valid_info_types = array("i", "H", "c", "A", "a");
47
$valid_log_types = array("error", "selftest");
48

    
49
include("head.inc");
50

    
51
// Highlights the words "PASSED", "FAILED", and "WARNING".
52
function add_colors($string) {
53
	// To add words keep arrays matched by numbers
54
	$patterns[0] = '/PASSED/';
55
	$patterns[1] = '/FAILED/';
56
	$patterns[2] = '/Warning/';
57
	$replacements[0] = '<span class="text-success">' . gettext("PASSED") . '</span>';
58
	$replacements[1] = '<span class="text-alert">' . gettext("FAILED") . '</span>';
59
	$replacements[2] = '<span class="text-warning">' . gettext("Warning") . '</span>';
60
	ksort($patterns);
61
	ksort($replacements);
62
	return preg_replace($patterns, $replacements, $string);
63
}
64

    
65
$targetdev = basename($_POST['device']);
66

    
67
if (!file_exists('/dev/' . $targetdev)) {
68
	echo gettext("Device does not exist, bailing.");
69
	return;
70
}
71

    
72
$specplatform = system_identify_specific_platform();
73
if (($specplatform['name'] == "Hyper-V") || ($specplatform['name'] == "uFW")) {
74
	echo sprintf(gettext("S.M.A.R.T. is not supported on this system (%s)."), $specplatform['descr']);
75
	include("foot.inc");
76
	exit;
77
}
78

    
79
switch ($action) {
80
	// Testing devices
81
	case 'test':
82
	{
83
		$test = $_POST['testType'];
84
		if (!in_array($test, $valid_test_types)) {
85
			echo gettext("Invalid test type, bailing.");
86
			return;
87
		}
88

    
89
		$output = add_colors(shell_exec($smartctl . " -t " . escapeshellarg($test) . " /dev/" . escapeshellarg($targetdev)));
90
?>
91
		<div class="panel  panel-default">
92
			<div class="panel-heading"><h2 class="panel-title"><?=gettext('Test Results')?></h2></div>
93
			<div class="panel-body">
94
				<pre><?=$output?></pre>
95
			</div>
96
		</div>
97

    
98
		<form action="diag_smart.php" method="post" name="abort">
99
			<input type="hidden" name="device" value="<?=$targetdev?>" />
100
			<input type="hidden" name="action" value="abort" />
101
			<nav class="action-buttons">
102
				<button type="submit" name="submit" class="btn btn-danger" value="<?=gettext("Abort")?>">
103
					<i class="fa fa-times icon-embed-btn"></i>
104
					<?=gettext("Abort Test")?>
105
				</button>
106
				<a href="<?=$_SERVER['PHP_SELF']?>" class="btn btn-info">
107
					<i class="fa fa-undo icon-embed-btn"></i>
108
					<?=gettext("Back")?>
109
				</a>
110
			</nav>
111
		</form>
112

    
113
<?php
114
		break;
115
	}
116

    
117
	// Info on devices
118
	case 'info':
119
	{
120
		$type = $_POST['type'];
121

    
122
		if (!in_array($type, $valid_info_types)) {
123
			print_info_box(gettext("Invalid info type, bailing."), 'danger');
124
			return;
125
		}
126

    
127
		$output = add_colors(shell_exec($smartctl . " -" . escapeshellarg($type) . " /dev/" . escapeshellarg($targetdev)));
128
?>
129
		<div class="panel  panel-default">
130
			<div class="panel-heading"><h2 class="panel-title"><?=gettext('Information')?></h2></div>
131
			<div class="panel-body">
132
				<pre><?=$output?></pre>
133
			</div>
134
		</div>
135

    
136
		<nav class="action-buttons">
137
			<a href="<?=$_SERVER['PHP_SELF']?>" class="btn btn-info">
138
				<i class="fa fa-undo icon-embed-btn"></i>
139
				<?=gettext("Back")?>
140
			</a>
141
		</nav>
142
<?php
143
		break;
144
	}
145

    
146
	// View logs
147
	case 'logs':
148
	{
149
		$type = $_POST['type'];
150
		if (!in_array($type, $valid_log_types)) {
151
			print_info_box(gettext("Invalid log type, bailing."), 'danger');
152
			return;
153
		}
154

    
155
		$output = add_colors(shell_exec($smartctl . " -l " . escapeshellarg($type) . " /dev/" . escapeshellarg($targetdev)));
156
?>
157
		<div class="panel  panel-default">
158
			<div class="panel-heading"><h2 class="panel-title"><?=gettext('Logs')?></h2></div>
159
			<div class="panel-body">
160
				<pre><?=$output?></pre>
161
			</div>
162
		</div>
163

    
164
		<nav class="action-buttons">
165
			<a href="<?=$_SERVER['PHP_SELF']?>" class="btn btn-info">
166
				<i class="fa fa-undo icon-embed-btn"></i>
167
				<?=gettext("Back")?>
168
			</a>
169
		</nav>
170
<?php
171
		break;
172
	}
173

    
174
	// Abort tests
175
	case 'abort':
176
	{
177
		$output = shell_exec($smartctl . " -X /dev/" . escapeshellarg($targetdev));
178
?>
179
		<div class="panel  panel-default">
180
			<div class="panel-heading"><h2 class="panel-title"><?=gettext('Abort')?></h2></div>
181
			<div class="panel-body">
182
				<pre><?=$output?></pre>
183
			</div>
184
		</div>
185
<?php
186
		break;
187
	}
188

    
189
	// Default page, prints the forms to view info, test, etc...
190
	default: {
191
// Information
192
		$devs = get_smart_drive_list();
193

    
194
		$form = new Form(false);
195

    
196
		$btnview = new Form_Button(
197
			'submit',
198
			'View',
199
			null,
200
			'fa-file-text-o'
201
		);
202
		$btnview->addClass('btn-primary');
203
		$btnview->setAttribute('id');
204

    
205
		$section = new Form_Section('Information');
206

    
207
		$section->addInput(new Form_Input(
208
			'action',
209
			null,
210
			'hidden',
211
			'info'
212
		))->setAttribute('id');
213

    
214
		$group = new Form_Group('Info type');
215

    
216
		$group->add(new Form_Checkbox(
217
			'type',
218
			null,
219
			'Info',
220
			false,
221
			'i'
222
		))->displayAsRadio();
223

    
224
		$group->add(new Form_Checkbox(
225
			'type',
226
			null,
227
			'Health',
228
			true,
229
			'H'
230
		))->displayAsRadio();
231

    
232
		$group->add(new Form_Checkbox(
233
			'type',
234
			null,
235
			'S.M.A.R.T. Capabilities',
236
			false,
237
			'c'
238
		))->displayAsRadio();
239

    
240
		$group->add(new Form_Checkbox(
241
			'type',
242
			null,
243
			'Attributes',
244
			false,
245
			'A'
246
		))->displayAsRadio();
247

    
248
		$group->add(new Form_Checkbox(
249
			'type',
250
			null,
251
			'All',
252
			false,
253
			'a'
254
		))->displayAsRadio();
255

    
256
		$section->add($group);
257

    
258
		$section->addInput(new Form_Select(
259
			'device',
260
			'Device: /dev/',
261
			false,
262
			array_combine($devs, $devs)
263
		))->setAttribute('id');
264

    
265
		$section->addInput(new Form_StaticText(
266
			'',
267
			$btnview
268
		));
269

    
270
		$form->add($section);
271
		print($form);
272

    
273
// Tests
274
		$form = new Form(false);
275

    
276
		$btntest = new Form_Button(
277
			'submit',
278
			'Test',
279
			null,
280
			'fa-wrench'
281
		);
282
		$btntest->addClass('btn-primary');
283
		$btntest->setAttribute('id');
284

    
285
		$section = new Form_Section('Perform self-tests');
286

    
287
		$section->addInput(new Form_Input(
288
			'action',
289
			null,
290
			'hidden',
291
			'test'
292
		))->setAttribute('id');
293

    
294
		$group = new Form_Group('Test type');
295

    
296
		$group->add(new Form_Checkbox(
297
			'testType',
298
			null,
299
			'Offline',
300
			false,
301
			'offline'
302
		))->displayAsRadio();
303

    
304
		$group->add(new Form_Checkbox(
305
			'testType',
306
			null,
307
			'Short',
308
			true,
309
			'short'
310
		))->displayAsRadio();
311

    
312
		$group->add(new Form_Checkbox(
313
			'testType',
314
			null,
315
			'Long',
316
			false,
317
			'long'
318
		))->displayAsRadio();
319

    
320
		$group->add(new Form_Checkbox(
321
			'testType',
322
			null,
323
			'Conveyance',
324
			false,
325
			'conveyance'
326
		))->displayAsRadio();
327

    
328
		$group->setHelp('Select "Conveyance" for ATA disks only.');
329
		$section->add($group);
330

    
331
		$section->addInput(new Form_Select(
332
			'device',
333
			'Device: /dev/',
334
			false,
335
			array_combine($devs, $devs)
336
		))->setAttribute('id');
337

    
338
		$section->addInput(new Form_StaticText(
339
			'',
340
			$btntest
341
		));
342

    
343
		$form->add($section);
344
		print($form);
345

    
346
// Logs
347
		$form = new Form(false);
348

    
349
		$btnview =  new Form_Button(
350
			'submit',
351
			'View',
352
			null,
353
			'fa-file-text-o'
354
		);
355
		$btnview->addClass('btn-primary');
356
		$btnview->setAttribute('id');
357

    
358
		$section = new Form_Section('View Logs');
359

    
360
		$section->addInput(new Form_Input(
361
			'action',
362
			null,
363
			'hidden',
364
			'logs'
365
		))->setAttribute('id');
366

    
367
		$group = new Form_Group('Log type');
368

    
369
		$group->add(new Form_Checkbox(
370
			'type',
371
			null,
372
			'Error',
373
			true,
374
			'error'
375
		))->displayAsRadio();
376

    
377
		$group->add(new Form_Checkbox(
378
			'type',
379
			null,
380
			'Self-test',
381
			false,
382
			'selftest'
383
		))->displayAsRadio();
384

    
385
		$section->add($group);
386

    
387
		$section->addInput(new Form_Select(
388
			'device',
389
			'Device: /dev/',
390
			false,
391
			array_combine($devs, $devs)
392
		))->setAttribute('id');
393

    
394
		$section->addInput(new Form_StaticText(
395
			'',
396
			$btnview
397
		));
398

    
399
		$form->add($section);
400
		print($form);
401

    
402
// Abort
403
		$btnabort = new Form_Button(
404
			'submit',
405
			'Abort',
406
			null,
407
			'fa-times'
408
		);
409

    
410
		$btnabort->addClass('btn-danger')->setAttribute('id');
411

    
412
		$form = new Form(false);
413

    
414
		$section = new Form_Section('Abort');
415

    
416
		$section->addInput(new Form_Input(
417
			'action',
418
			null,
419
			'hidden',
420
			'abort'
421
		))->setAttribute('id');
422

    
423
		$section->addInput(new Form_Select(
424
			'device',
425
			'Device: /dev/',
426
			false,
427
			array_combine($devs, $devs)
428
		))->setAttribute('id');
429

    
430
		$section->addInput(new Form_StaticText(
431
			'',
432
			$btnabort
433
		));
434

    
435
		$form->add($section);
436
		print($form);
437

    
438
		break;
439
	}
440
}
441

    
442
include("foot.inc");
(25-25/225)