-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZpoolStatusCheck.php
160 lines (141 loc) · 4.7 KB
/
ZpoolStatusCheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Check if zpool is still healthy.
*
* @author Stefan Konig <[email protected]>
* @package seoSepa\PHP-ZFSHealthCheck
*/
class ZpoolStatusCheck
{
/**
* @var $zpoolStatusData array
*/
protected $zpoolStatusData = array();
/**
* Perform zpool status check
*/
public function run()
{
$this->debugLog('>> Starting zpool status check');
// return on fail
if (!$this->getZpoolStatus()) {
return;
}
$failArray = $this->parseZpoolStatus();
foreach ($failArray as $failureMessage) {
$this->sendFail($failureMessage['message'], $failureMessage['verbose']);
}
$this->debugLog('Finished zpool capacity check, ' . count($failArray) . ' failures send');
}
/**
* send message to failHandler
*
* @param string $message
* @param string $verboseMessage
*/
protected function sendFail($message, $verboseMessage)
{
$failureHandler = new FailHandler();
$failureHandler->sendNotice($message, $verboseMessage);
}
/**
* send debugLog to central log function in FailHandler
*
* @param string $log
*/
protected function debugLog($log)
{
$failureHandler = new FailHandler();
$failureHandler->debugLog($log);
}
/**
* Use proc_open in case the command zpool status hangs.
*/
protected function getZpoolStatus()
{
$descriptorspec = array(
0 => STDIN,
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
$this->debugLog('proc_open (/sbin/zpool status)');
$process = proc_open('/sbin/zpool status 2>&1', $descriptorspec, $pipes);
// poll status of the process 5 times
for ($i = 0; $i < 5; $i++) {
sleep(1);
$this->debugLog('polling process status');
$status = proc_get_status($process);
if ($status['running'] == false) {
break;
}
}
$status = proc_get_status($process);
$this->debugLog('process still running: ' . var_export($status['running'],true));
// this indicates a problem with the zpool
if ($status['running'] == true) {
$this->sendFail('zpool status command timeout', 'process status: ' . print_r($status, true));
// force the process to stop
proc_terminate($process, 9);
return false;
}
$this->debugLog('zpool status output:');
while (($line = fgets($pipes[1])) !== false) {
$this->zpoolStatusData[] = $line;
$this->debugLog(' ' . trim($line));
}
proc_close($process);
$this->debugLog('process closed');
return true;
}
/**
* parse the zpool status output
*/
protected function parseZpoolStatus()
{
$failArray = array();
$zpoolName = '';
foreach ($this->zpoolStatusData as $output) {
// Check if output is the zpool name, if so, set it.
if (preg_match('/^\s+pool:\s+(.+)$/', $output, $matches)) {
$zpoolName = $matches[1];
continue;
}
// Check state
$zpoolState = '';
if (preg_match('/^\s+state:\s+(.+)$/', $output, $matches)) {
$zpoolState = $matches[1];
$this->debugLog("zpool '{$zpoolName}' is currently {$zpoolState}");
}
$zpoolStatus = '';
if (preg_match('/^\s+status:\s+(.+)$/', $output, $matches)) {
$zpoolStatus = $matches[1];
}
if ($zpoolState != 'ONLINE' && $zpoolState != '') {
$failArray[] = array(
'message' => "zpool {$zpoolName} is currently {$zpoolState}",
'verbose' => "Complete zpool data: " . print_r(
$this->zpoolStatusData,
true
),
);
}
// this usually means there is action required for this zpool
if ($zpoolStatus != '') {
$failArray[] = array(
'message' => "zpool {$zpoolName} has state: {$zpoolState}",
'verbose' => "Complete zpool data: " . print_r(
$this->zpoolStatusData,
true
),
);
}
}
return $failArray;
}
}
// if wrapperClass does not exist, assume this script is run standalone
if (!class_exists('CheckAllWrapper')) {
require_once('FailHandler.php');
$zpoolStatusCheck = new ZpoolStatusCheck();
$zpoolStatusCheck->run();
}