Skip to content

Commit a0e385a

Browse files
authored
Merge pull request #231 from it-novum/development
Final merge before release
2 parents 6864ad4 + 884b36c commit a0e385a

File tree

5 files changed

+85
-83
lines changed

5 files changed

+85
-83
lines changed

app/Config/cmddaemon.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
$config = [
33
//Path to the named pipe of the daemon
44
'pipe' => '/opt/openitc/nagios/var/rw/oitc.cmd',
5-
5+
66
//Chmod of named pipe
77
'mode' => 0660,
8-
9-
//Sleep, between the named pipe gets read
8+
9+
//Sleep, between the named pipe gets read in μs (microseconds)
1010
'sleep' => 500000,
11-
11+
1212
//Loglevel
1313
// 0 Disable log
1414
// 1 Log Info
1515
// 2 Log Warning
1616
// 4 Log fatal
1717
// 8 Log debug
1818
//Simply add up to combine different log level
19-
// 1 + 2 + 4 = 7
19+
// 1 + 2 + 4 = 7
2020
'loglevel' => 7,
21-
21+
2222
//Path to the logfile
2323
'logfile' => '/var/log/oitc_cmd.log'
2424
];

app/Console/Command/Task/DebugConfigNagiosTask.php

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
// confirmation.
2525

2626
class DebugConfigNagiosTask extends AppShell {
27-
27+
2828
public $uses = ['Host', 'Hosttemplate', 'Timeperiod', 'Command', 'Contact', 'Contactgroup', 'Container', 'Customvariable', 'Hostescalation', 'Hostgroup', 'Service', 'Servicetemplate', 'Serviceescalations', 'Servicegroup', 'Hostdependency', 'Servicedependency'];
29-
29+
3030
public function execute() {
3131
//Do some cool stuff
3232
debug($this->Hosttemplate->find('all'));
3333
}
34-
34+
3535
public function setup($conf = []){
3636
$this->conf = $conf;
3737
$this->monitoringLog = Configure::read('nagios.logfilepath').Configure::read('nagios.logfilename');
@@ -43,7 +43,7 @@ public function setup($conf = []){
4343
$this->_buildUuidCache();
4444
$this->searchUuids();
4545
}
46-
46+
4747
private function _buildUuidCache(){
4848
$this->uuidCache = [];
4949
$Models = ['Host', 'Hosttemplate', 'Timeperiod', 'Command', 'Contact', 'Contactgroup', 'Hostgroup', 'Servicegroup', 'Service', 'Servicetemplate'];
@@ -109,18 +109,18 @@ private function _buildUuidCache(){
109109
'fields' => ['Servicetemplate.id', 'Servicetemplate.uuid', 'Servicetemplate.name'],
110110
]
111111
];
112-
112+
113113
foreach($Models as $ModelName){
114114
if(!in_array($ModelName, $this->uses)){
115115
$this->loadModel($ModelName);
116116
}
117-
117+
118118
foreach($this->{$ModelName}->find('all', $options[$ModelName]) as $result){
119119
$tmp_result = [];
120120
if(isset($result[$ModelName]['id'])){
121121
$tmp_result['id'] = $result[$ModelName]['id'];
122122
}
123-
123+
124124
if(isset($result[$ModelName]['name'])){
125125
$tmp_result['name'] = $result[$ModelName]['name'];
126126
}else{
@@ -132,32 +132,32 @@ private function _buildUuidCache(){
132132
}
133133
}
134134
}
135-
135+
136136
if(isset($result[$ModelName]['description'])){
137137
$tmp_result['description'] = $result[$ModelName]['description'];
138138
}
139-
139+
140140
if(isset($result[$ModelName]['container_id'])){
141141
$tmp_result['container_id'] = $result[$ModelName]['container_id'];
142142
}
143-
143+
144144
if(isset($result['Container']['name'])){
145145
$tmp_result['container_name'] = $result['Container']['name'];
146146
}
147-
148-
147+
148+
149149
$tmp_result['ModelName'] = $ModelName;
150-
150+
151151
if(!isset($result[$ModelName]['uuid'])){
152152
debug($result[$ModelName]);
153153
}
154-
154+
155155
$this->uuidCache[$result[$ModelName]['uuid']] = $tmp_result;
156156
unset($tmp_result);
157157
}
158158
}
159159
}
160-
160+
161161
public function debug($ModelName = null, $confName){
162162
if($ModelName !== null && is_array($this->uses)){
163163
$ModelSchema = $this->{$ModelName}->schema();
@@ -184,7 +184,7 @@ public function debug($ModelName = null, $confName){
184184
$this->out('<error>'.__d('oitc_console', 'No name field for '.$ModelName . ' found in database!').'</error>');
185185
$result = $this->{$ModelName}->find('all');
186186
}
187-
187+
188188
if(sizeof($result)>1){
189189
if(isset($input)){
190190
$this->out(__d('oitc_console', 'I found '.sizeof($result).' results matching to "'. $input . '". Please select one '.$ModelName. ' by typing the number in square brackets'));
@@ -215,16 +215,16 @@ public function debug($ModelName = null, $confName){
215215
$this->out('<error>No object metching given conditions</error>');
216216
}
217217
}
218-
218+
219219
}else{
220220
echo "Unknown Model !";
221221
exit();
222222
}
223223
}
224-
224+
225225
public function debugByUuid($uuid = null){
226226
if($uuid !== null){
227-
227+
228228
}else{
229229
$input = $this->in(__d('oitc_console', 'Please enter your UUID'));
230230
$result = $this->searchByUuid($input);
@@ -235,7 +235,7 @@ public function debugByUuid($uuid = null){
235235
}
236236
}
237237
}
238-
238+
239239
public function parseMonitoringLogfile(){
240240
foreach($this->tail() as $line){
241241
$timestamp = null;
@@ -247,18 +247,18 @@ public function parseMonitoringLogfile(){
247247
}
248248
}
249249
}
250-
250+
251251
private function tail($lines = 100){
252252
$return = [];
253253
$file = fopen($this->monitoringLog, "r");
254254
$flen = filesize($this->monitoringLog);
255255
if( $flen % 4096 != 0 ){
256256
$flen = ((int)($flen / 4096) + 1) * 4096;
257257
}
258-
258+
259259
$newlines = 0;
260260
$startpos = -1;
261-
261+
262262
for( $goto = 0; $goto <= $flen - 4096; $goto += 4096 ){
263263
fseek($file, $flen - $goto - 4096);
264264
$data = fread($file, 4096);
@@ -273,23 +273,24 @@ private function tail($lines = 100){
273273
}
274274
}
275275
}
276-
276+
277277
if( $startpos != -1 ){
278278
fseek($file, $startpos);
279-
$return[] = fread($file, $flen - $startpos);
279+
$fileContent = fread($file, $flen - $startpos);
280+
$return[] = $this->searchUuids($fileContent);
280281
}
281282
$ret = array();
282283
foreach($return as $r){
283284
$ret = explode("\n", $r);
284285
}
285286
return $ret;
286287
}
287-
288+
288289
public function tailf(){
289290
$callback = function($timestamp){
290291
return '<comment>'.date('d.m.Y - H:i:s', $timestamp[0]).'</comment>';
291292
};
292-
293+
293294
$options = array(
294295
'cwd' => Configure::read('nagios.logfilepath'),
295296
'env' => array(
@@ -298,31 +299,31 @@ public function tailf(){
298299
'LC_ALL' => 'C',
299300
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'),
300301
);
301-
302+
302303
$descriptorspec = array(
303304
0 => array("pipe", "r"),
304305
1 => array("pipe", "w"),
305306
2 => array("pipe", "r")
306307
);
307-
308+
308309
$process = proc_open('/usr/bin/tail -f -n 100 '.Configure::read('nagios.logfilename'), $descriptorspec, $pipes, $options['cwd'], $options['env']);
309310
while(true){
310311
$status = proc_get_status($process);
311-
if($status['running'] != 1 && $line == ''){
312+
if($status['running'] != 1){
312313
fclose($pipes[0]);
313314
fclose($pipes[1]);
314315
fclose($pipes[2]);
315316
proc_close($process);
316317
break;
317318
}
318-
319+
319320
$string = fgets($pipes[1], 1024);
320321
$string = preg_replace_callback('#\d{10,11}#', $callback, $string);
321322
$string = $this->searchUuids($string);
322323
$this->out($string, 0);
323324
}
324325
}
325-
326+
326327
private function _outFile($uuid, $ModelName, $confName){
327328
if(!$this->conf['minified'] || in_array($ModelName, ['Hostdependency', 'Hostescalation', 'Servicedependency', 'Serviceescalation', 'Servicegroup'])){
328329
// Model that are not saved as minified config files or minified configs asre tournd off
@@ -342,10 +343,10 @@ private function _outFile($uuid, $ModelName, $confName){
342343
// User want the file of an object, that is inside of an minified file, so we need to parse the minified config
343344
if(file_exists($this->conf['path'].$this->conf[$confName].$confName.'_minified'.$this->conf['suffix'])){
344345
$fileAsArray = file($this->conf['path'].$this->conf[$confName].$confName.'_minified'.$this->conf['suffix']);
345-
346+
346347
$content = [];
347348
$startParsing = false;
348-
349+
349350
$modelToNagios = [
350351
'Command' => 'command_name',
351352
//'Contactgroup' => ,
@@ -357,11 +358,11 @@ private function _outFile($uuid, $ModelName, $confName){
357358
'Servicetemplate' => 'service_description',
358359
'Timeperiod' => 'timeperiod_name'
359360
];
360-
361+
361362
$needel = $modelToNagios[$ModelName];
362-
363+
363364
$searchForEnd = false;
364-
365+
365366
$configContent = [];
366367
$breakForeach = false;
367368
$state = 'SEARCH_FOR_DEFINITION';
@@ -373,7 +374,7 @@ private function _outFile($uuid, $ModelName, $confName){
373374
$state = 'SEARCH_FOR_OBJECT_NAME';
374375
}
375376
break;
376-
377+
377378
case 'SEARCH_FOR_OBJECT_NAME': //host_name, command_name, contacnt_name, etc
378379
if(preg_match('/'.$needel.'/', $line)){
379380
$check = explode($needel, $line);
@@ -388,15 +389,15 @@ private function _outFile($uuid, $ModelName, $confName){
388389
$state = 'SEARCH_FOR_END_OF_DEFENITION_AND_CONTINUE';
389390
}
390391
break;
391-
392+
392393
case 'SEARCH_FOR_END_OF_DEFENITION':
393394
if(trim($line) == '}'){
394395
// We have the complet object now, so we can break out of switch and foreach
395396
$breakForeach = true;
396397
break;
397398
}
398399
break;
399-
400+
400401
case 'SEARCH_FOR_END_OF_DEFENITION_AND_CONTINUE':
401402
// this was the wrong object, so throw everyting away and continue with next definition
402403
if(trim($line) == '}'){
@@ -405,12 +406,12 @@ private function _outFile($uuid, $ModelName, $confName){
405406
}
406407
break;
407408
}
408-
409+
409410
if($breakForeach === true){
410411
break;
411412
}
412413
}
413-
414+
414415
$this->hr();
415416
$this->out('<info>'.__d('oitc_console', 'Notice: This is not the real nagios configuration file. This is a human readable version of the config.').'</info>');
416417
$this->hr();
@@ -423,25 +424,25 @@ private function _outFile($uuid, $ModelName, $confName){
423424

424425
}
425426
}
426-
427+
427428
public function searchUuids($string = ''){
428429
if(!isset($this->uuidCache) || empty($this->uuidCache) || !is_array($this->uuidCache)){
429430
App::uses('UUID', 'Lib');
430-
$this->_buildUuidCache();
431+
$this->_buildUuidCache();
431432
}
432-
433+
433434
$string = preg_replace_callback(UUID::regex(), [$this, '_replaceUuid'], $string);
434435
return $string;
435436
}
436-
437+
437438
public function searchByUuid($uuid = null){
438439
if(isset($this->uuidCache[$uuid])){
439440
return $this->uuidCache[$uuid];
440441
}
441-
442+
442443
return [];
443444
}
444-
445+
445446
private function _replaceUuid($matches){
446447
foreach($matches as $match){
447448
if(isset($this->uuidCache[$match])){
@@ -453,21 +454,21 @@ private function _replaceUuid($matches){
453454
}else{
454455
return "<error>name not found in DB</error><comment>[".$match."]</comment>";
455456
}
456-
457+
457458
}
458459

459460
return "<error>object not found in UUID cache</error><comment>[".$match."]</comment>";
460461
}
461462
}
462-
463+
463464
public function translateStdin(){
464465
$result = null;
465466
do{
466467
$result = $this->stdin->read();
467468
$this->out($this->searchUuids($result));
468469
}while($result !== false);
469470
}
470-
471+
471472
/**
472473
* Unbind all accociations for the next find() call for every model
473474
*

0 commit comments

Comments
 (0)