-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.php
378 lines (355 loc) · 10.7 KB
/
model.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/* Eregansu Clustering
*
* Copyright 2009 Mo McRoberts.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the author(s) of this software may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
uses('model');
if(!defined('INSTANCE_NAME')) define('INSTANCE_NAME', php_uname('n'));
if(!defined('CLUSTER_HEARTBEAT_THRESHOLD')) define('CLUSTER_HEARTBEAT_THRESHOLD', 60);
if(!defined('CLUSTER_IRI')) define('CLUSTER_IRI', null);
class ClusterModel extends Model
{
public $writeable = true;
protected $heartbeat;
public $instanceName;
public $clusterName;
public $hostName;
public static function getInstance($args = null, $className = null, $defaultDbIri = null)
{
if(null === $args) $args = array();
if(!isset($args['db'])) $args['db'] = ($defaultDbIri ? $defaultDbIri : CLUSTER_IRI);
if(null === $className)
{
$className = 'ClusterModel';
if(!strncmp($args['db'], 'file:', 5))
{
$className = 'ClusterFileModel';
}
}
return Model::getInstance($args, $className, null);
}
public function clusterStatus($cluster)
{
$fs = array();
if(!defined('HEARTBEAT_IRI'))
{
return array('tag' => 'unknown', 'description' => 'Unknown', 'monfs' => $fs);
}
require_once(APPS_ROOT . 'heartbeat/model.php');
if(!$this->heartbeat) $this->heartbeat = HeartbeatModel::getInstance();
$instances = $this->instancesInCluster($cluster);
$online = 0;
$offline = 0;
$alert = 0;
$now = time();
foreach($instances as $inst)
{
$status = $this->instanceStatus($inst['name'], $cluster);
if(!$status) continue;
if($status['tag'] == 'offline')
{
$offline++;
}
else if($status['tag'] == 'online')
{
$online++;
}
else if($status['tag'] == 'alert')
{
$alert++;
}
if(isset($status['monfs']))
{
foreach($status['monfs'] as $k => $mfs)
{
$fs[$k] = $mfs;
}
}
/* $host = $this->host($inst['host']);
$status = $this->heartbeat->lastPulse($inst['host']);
if(!$status) continue;
if($status['unixtime'] + CLUSTER_HEARTBEAT_THRESHOLD < $now) continue;
$online++; */
}
if(!$online)
{
return array('tag' => 'offline', 'description' => 'No instances are online', 'monfs' => $fs);
}
if($online == count($instances))
{
return array('tag' => 'online', 'description' => 'All instances are online', 'monfs' => $fs);
}
return array('tag' => 'alert', 'description' => 'Some instances are offline', 'monfs' => $fs);
}
public function instanceStatus($instance, $clusterName = null)
{
if(!defined('HEARTBEAT_IRI'))
{
return array('tag' => 'unknown', 'status' => 'Unknown', 'timestamp' => null, 'unixtime' => null);
}
require_once(APPS_ROOT . 'heartbeat/model.php');
if(!$this->heartbeat) $this->heartbeat = HeartbeatModel::getInstance();
$inst = $this->instanceInCluster($instance, $clusterName);
$info = $this->heartbeat->lastPulse($inst['host']);
$host = $this->host($inst['host']);
if(!$info || $info['unixtime'] + CLUSTER_HEARTBEAT_THRESHOLD < time())
{
$info['tag'] = 'offline';
$info['status'] = 'This instance is currently offline';
}
else
{
$info['tag'] = 'online';
$info['status'] = 'Online';
}
if(!isset($info['timestamp']))
{
$info['timestamp'] = $info['unixtime'] = null;
}
$info['monfs'] = array();
if(isset($info['mib']['fs']))
{
foreach($host['fs'] as $mfs)
{
foreach($info['mib']['fs'] as $hfs)
{
if(isset($mfs['mountpoint']) && !strcmp($mfs['mountpoint'], $hfs['mountpoint']))
{
$hfs['host'] = $inst['host'];
$info['monfs'][$inst['host'] . ':' . $hfs['mountpoint']] = $hfs;
}
}
}
}
return $info;
}
public function hostStatus($host)
{
if(!defined('HEARTBEAT_IRI'))
{
return array('tag' => 'unknown', 'status' => 'Unknown', 'timestamp' => null, 'unixtime' => null);
}
require_once(APPS_ROOT . 'heartbeat/model.php');
if(!$this->heartbeat) $this->heartbeat = HeartbeatModel::getInstance();
$info = $this->heartbeat->lastPulse($host);
if(!$info || $info['unixtime'] + CLUSTER_HEARTBEAT_THRESHOLD < time())
{
$info['tag'] = 'offline';
$info['status'] = 'This instance is currently offline';
}
else
{
$info['tag'] = 'online';
$info['status'] = 'Online';
}
if(!isset($info['timestamp']))
{
$info['timestamp'] = $info['unixtime'] = null;
}
return $info;
}
public function clusters()
{
return array();
}
public function hosts()
{
return array();
}
}
class ClusterFileModel extends ClusterModel
{
protected $clusters = array();
protected $inst = array();
protected $hosts = array();
public function __construct($args)
{
$this->writeable = false;
$path = $args['db'];
if(strncmp($args['db'], 'file:', 5))
{
trigger_error('Invalid file: IRI passed to ClusterFileModel::__construct()', E_USER_ERROR);
return;
}
$path = substr($args['db'], 5);
while(substr($path, 0, 2) == '//') $path = substr($path, 1);
if(!strlen($path))
{
trigger_error('Empty file: IRI passed to ClusterFileModel::__construct()', E_USER_ERROR);
return;
}
if($path[0] != '/') $path = CONFIG_ROOT . $path;
$this->readClustersFromFile($path);
$this->instanceName = INSTANCE_NAME;
if(!isset($this->inst[INSTANCE_NAME]))
{
trigger_error('Current instance ' . INSTANCE_NAME . ' is not defined in any cluster (define INSTANCE_NAME to override the default or modify ' . $path . ' to add it to a cluster)', E_USER_ERROR);
}
$this->clusterName = $this->inst[INSTANCE_NAME]['cluster'];
$this->hostName = $this->inst[INSTANCE_NAME]['host'];
}
public function hosts()
{
return $this->hosts;
}
public function instancesOnHost($host)
{
if(isset($this->hosts[$host])) return $this->hosts[$host]['instances'];
return null;
}
public function host($name)
{
if(!isset($this->hosts[$name])) return null;
return $this->hosts[$name];
}
public function clusters()
{
return $this->clusters;
}
public function cluster($name)
{
if(isset($this->clusters[$name])) return $this->clusters[$name];
return null;
}
public function clusterNameOfInstance($inst)
{
if(isset($this->inst[$inst]))
{
return $this->inst[$inst]['cluster'];
}
return null;
}
public function instancesInCluster($clusterName = null)
{
if(!strlen($clusterName)) $clusterName = $this->clusterName;
$list = array();
if(!isset($this->clusters[$clusterName])) return null;
foreach($this->clusters[$clusterName]['instances'] as $inst)
{
$list[$inst] = $this->inst[$inst];
}
return $list;
}
public function instanceInCluster($instanceName, $clusterName = null)
{
if(!strlen($clusterName)) $clusterName = $this->clusterName;
if(!isset($this->clusters[$clusterName])) return null;
if(!in_array($instanceName, $this->clusters[$clusterName]['instances'])) return null;
return $this->inst[$instanceName];
}
protected function readClustersFromFile($path)
{
$xml = simplexml_load_file($path);
foreach($xml->host as $host)
{
$info = array();
$attrs = $host->attributes();
foreach($attrs as $a)
{
$k = $a->getName();
$v = trim($a);
$info[$k] = $v;
}
$info['fs'] = array();
foreach($host->fs as $fs)
{
$fsinfo = array();
$attrs = $fs->attributes();
foreach($attrs as $a)
{
$k = $a->getName();
$v = trim($a);
$fsinfo[$k] = $v;
}
$info['fs'][] = $fsinfo;
}
if(!isset($info['name']))
{
trigger_error('Skipping defined host with no name', E_USER_WARNING);
continue;
}
$info['instances'] = array();
if(!isset($info['title'])) $info['title'] = $info['name'];
if(isset($this->hosts[$info['name']]))
{
trigger_error('Cluster ' . $info['name'] . ' is defined more than once; the most recent definition will be used', E_USER_NOTICE);
}
$this->hosts[$info['name']] = $info;
}
foreach($xml->cluster as $cluster)
{
$info = array();
$attrs = $cluster->attributes();
foreach($attrs as $a)
{
$k = $a->getName();
$v = trim($a);
$info[$k] = $v;
}
if(!isset($info['name']))
{
trigger_error('Skipping defined cluster with no name', E_USER_WARNING);
continue;
}
if(!isset($info['title'])) $info['title'] = $info['name'];
if(isset($this->cluster[$info['name']]))
{
trigger_error('Cluster ' . $info['name'] . ' is defined more than once; the most recent definition will be used', E_USER_NOTICE);
}
$ilist = array();
foreach($cluster->instance as $inst)
{
$iinfo = array();
$attrs = $inst->attributes();
foreach($attrs as $a)
{
$k = $a->getName();
$v = trim($a);
$iinfo[$k] = $v;
}
if(!isset($iinfo['name']))
{
trigger_error('Skipping defined instance with no name', E_USER_WARNING);
continue;
}
if(!isset($iinfo['title'])) $iinfo['title'] = $iinfo['name'];
if(isset($this->inst[$iinfo['name']]))
{
trigger_error('Instance ' . $iinfo['name'] . ' is defined more than once; the most recent definition will be used', E_USER_NOTICE);
}
if(!isset($iinfo['host'])) $iinfo['host'] = $iinfo['name'];
if(!isset($this->hosts[$iinfo['host']])) $this->hosts[$iinfo['host']] = array('name' => $iinfo['host'], 'title' => $iinfo['host'], 'instances' => array(), 'fs' => array());
$this->hosts[$iinfo['host']]['instances'][] = $iinfo['name'];
$iinfo['cluster'] = $info['name'];
$this->inst[$iinfo['name']] = $iinfo;
$ilist[$iinfo['name']] = $iinfo['name'];
}
$info['instances'] = array_values($ilist);
$this->clusters[$info['name']] = $info;
}
}
}