24
24
// confirmation.
25
25
26
26
class DebugConfigNagiosTask extends AppShell {
27
-
27
+
28
28
public $ uses = ['Host ' , 'Hosttemplate ' , 'Timeperiod ' , 'Command ' , 'Contact ' , 'Contactgroup ' , 'Container ' , 'Customvariable ' , 'Hostescalation ' , 'Hostgroup ' , 'Service ' , 'Servicetemplate ' , 'Serviceescalations ' , 'Servicegroup ' , 'Hostdependency ' , 'Servicedependency ' ];
29
-
29
+
30
30
public function execute () {
31
31
//Do some cool stuff
32
32
debug ($ this ->Hosttemplate ->find ('all ' ));
33
33
}
34
-
34
+
35
35
public function setup ($ conf = []){
36
36
$ this ->conf = $ conf ;
37
37
$ this ->monitoringLog = Configure::read ('nagios.logfilepath ' ).Configure::read ('nagios.logfilename ' );
@@ -43,7 +43,7 @@ public function setup($conf = []){
43
43
$ this ->_buildUuidCache ();
44
44
$ this ->searchUuids ();
45
45
}
46
-
46
+
47
47
private function _buildUuidCache (){
48
48
$ this ->uuidCache = [];
49
49
$ Models = ['Host ' , 'Hosttemplate ' , 'Timeperiod ' , 'Command ' , 'Contact ' , 'Contactgroup ' , 'Hostgroup ' , 'Servicegroup ' , 'Service ' , 'Servicetemplate ' ];
@@ -109,18 +109,18 @@ private function _buildUuidCache(){
109
109
'fields ' => ['Servicetemplate.id ' , 'Servicetemplate.uuid ' , 'Servicetemplate.name ' ],
110
110
]
111
111
];
112
-
112
+
113
113
foreach ($ Models as $ ModelName ){
114
114
if (!in_array ($ ModelName , $ this ->uses )){
115
115
$ this ->loadModel ($ ModelName );
116
116
}
117
-
117
+
118
118
foreach ($ this ->{$ ModelName }->find ('all ' , $ options [$ ModelName ]) as $ result ){
119
119
$ tmp_result = [];
120
120
if (isset ($ result [$ ModelName ]['id ' ])){
121
121
$ tmp_result ['id ' ] = $ result [$ ModelName ]['id ' ];
122
122
}
123
-
123
+
124
124
if (isset ($ result [$ ModelName ]['name ' ])){
125
125
$ tmp_result ['name ' ] = $ result [$ ModelName ]['name ' ];
126
126
}else {
@@ -132,32 +132,32 @@ private function _buildUuidCache(){
132
132
}
133
133
}
134
134
}
135
-
135
+
136
136
if (isset ($ result [$ ModelName ]['description ' ])){
137
137
$ tmp_result ['description ' ] = $ result [$ ModelName ]['description ' ];
138
138
}
139
-
139
+
140
140
if (isset ($ result [$ ModelName ]['container_id ' ])){
141
141
$ tmp_result ['container_id ' ] = $ result [$ ModelName ]['container_id ' ];
142
142
}
143
-
143
+
144
144
if (isset ($ result ['Container ' ]['name ' ])){
145
145
$ tmp_result ['container_name ' ] = $ result ['Container ' ]['name ' ];
146
146
}
147
-
148
-
147
+
148
+
149
149
$ tmp_result ['ModelName ' ] = $ ModelName ;
150
-
150
+
151
151
if (!isset ($ result [$ ModelName ]['uuid ' ])){
152
152
debug ($ result [$ ModelName ]);
153
153
}
154
-
154
+
155
155
$ this ->uuidCache [$ result [$ ModelName ]['uuid ' ]] = $ tmp_result ;
156
156
unset($ tmp_result );
157
157
}
158
158
}
159
159
}
160
-
160
+
161
161
public function debug ($ ModelName = null , $ confName ){
162
162
if ($ ModelName !== null && is_array ($ this ->uses )){
163
163
$ ModelSchema = $ this ->{$ ModelName }->schema ();
@@ -184,7 +184,7 @@ public function debug($ModelName = null, $confName){
184
184
$ this ->out ('<error> ' .__d ('oitc_console ' , 'No name field for ' .$ ModelName . ' found in database! ' ).'</error> ' );
185
185
$ result = $ this ->{$ ModelName }->find ('all ' );
186
186
}
187
-
187
+
188
188
if (sizeof ($ result )>1 ){
189
189
if (isset ($ input )){
190
190
$ 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){
215
215
$ this ->out ('<error>No object metching given conditions</error> ' );
216
216
}
217
217
}
218
-
218
+
219
219
}else {
220
220
echo "Unknown Model ! " ;
221
221
exit ();
222
222
}
223
223
}
224
-
224
+
225
225
public function debugByUuid ($ uuid = null ){
226
226
if ($ uuid !== null ){
227
-
227
+
228
228
}else {
229
229
$ input = $ this ->in (__d ('oitc_console ' , 'Please enter your UUID ' ));
230
230
$ result = $ this ->searchByUuid ($ input );
@@ -235,7 +235,7 @@ public function debugByUuid($uuid = null){
235
235
}
236
236
}
237
237
}
238
-
238
+
239
239
public function parseMonitoringLogfile (){
240
240
foreach ($ this ->tail () as $ line ){
241
241
$ timestamp = null ;
@@ -247,18 +247,18 @@ public function parseMonitoringLogfile(){
247
247
}
248
248
}
249
249
}
250
-
250
+
251
251
private function tail ($ lines = 100 ){
252
252
$ return = [];
253
253
$ file = fopen ($ this ->monitoringLog , "r " );
254
254
$ flen = filesize ($ this ->monitoringLog );
255
255
if ( $ flen % 4096 != 0 ){
256
256
$ flen = ((int )($ flen / 4096 ) + 1 ) * 4096 ;
257
257
}
258
-
258
+
259
259
$ newlines = 0 ;
260
260
$ startpos = -1 ;
261
-
261
+
262
262
for ( $ goto = 0 ; $ goto <= $ flen - 4096 ; $ goto += 4096 ){
263
263
fseek ($ file , $ flen - $ goto - 4096 );
264
264
$ data = fread ($ file , 4096 );
@@ -273,23 +273,24 @@ private function tail($lines = 100){
273
273
}
274
274
}
275
275
}
276
-
276
+
277
277
if ( $ startpos != -1 ){
278
278
fseek ($ file , $ startpos );
279
- $ return [] = fread ($ file , $ flen - $ startpos );
279
+ $ fileContent = fread ($ file , $ flen - $ startpos );
280
+ $ return [] = $ this ->searchUuids ($ fileContent );
280
281
}
281
282
$ ret = array ();
282
283
foreach ($ return as $ r ){
283
284
$ ret = explode ("\n" , $ r );
284
285
}
285
286
return $ ret ;
286
287
}
287
-
288
+
288
289
public function tailf (){
289
290
$ callback = function ($ timestamp ){
290
291
return '<comment> ' .date ('d.m.Y - H:i:s ' , $ timestamp [0 ]).'</comment> ' ;
291
292
};
292
-
293
+
293
294
$ options = array (
294
295
'cwd ' => Configure::read ('nagios.logfilepath ' ),
295
296
'env ' => array (
@@ -298,31 +299,31 @@ public function tailf(){
298
299
'LC_ALL ' => 'C ' ,
299
300
'PATH ' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin ' ),
300
301
);
301
-
302
+
302
303
$ descriptorspec = array (
303
304
0 => array ("pipe " , "r " ),
304
305
1 => array ("pipe " , "w " ),
305
306
2 => array ("pipe " , "r " )
306
307
);
307
-
308
+
308
309
$ process = proc_open ('/usr/bin/tail -f -n 100 ' .Configure::read ('nagios.logfilename ' ), $ descriptorspec , $ pipes , $ options ['cwd ' ], $ options ['env ' ]);
309
310
while (true ){
310
311
$ status = proc_get_status ($ process );
311
- if ($ status ['running ' ] != 1 && $ line == '' ){
312
+ if ($ status ['running ' ] != 1 ){
312
313
fclose ($ pipes [0 ]);
313
314
fclose ($ pipes [1 ]);
314
315
fclose ($ pipes [2 ]);
315
316
proc_close ($ process );
316
317
break ;
317
318
}
318
-
319
+
319
320
$ string = fgets ($ pipes [1 ], 1024 );
320
321
$ string = preg_replace_callback ('#\d{10,11}# ' , $ callback , $ string );
321
322
$ string = $ this ->searchUuids ($ string );
322
323
$ this ->out ($ string , 0 );
323
324
}
324
325
}
325
-
326
+
326
327
private function _outFile ($ uuid , $ ModelName , $ confName ){
327
328
if (!$ this ->conf ['minified ' ] || in_array ($ ModelName , ['Hostdependency ' , 'Hostescalation ' , 'Servicedependency ' , 'Serviceescalation ' , 'Servicegroup ' ])){
328
329
// 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){
342
343
// User want the file of an object, that is inside of an minified file, so we need to parse the minified config
343
344
if (file_exists ($ this ->conf ['path ' ].$ this ->conf [$ confName ].$ confName .'_minified ' .$ this ->conf ['suffix ' ])){
344
345
$ fileAsArray = file ($ this ->conf ['path ' ].$ this ->conf [$ confName ].$ confName .'_minified ' .$ this ->conf ['suffix ' ]);
345
-
346
+
346
347
$ content = [];
347
348
$ startParsing = false ;
348
-
349
+
349
350
$ modelToNagios = [
350
351
'Command ' => 'command_name ' ,
351
352
//'Contactgroup' => ,
@@ -357,11 +358,11 @@ private function _outFile($uuid, $ModelName, $confName){
357
358
'Servicetemplate ' => 'service_description ' ,
358
359
'Timeperiod ' => 'timeperiod_name '
359
360
];
360
-
361
+
361
362
$ needel = $ modelToNagios [$ ModelName ];
362
-
363
+
363
364
$ searchForEnd = false ;
364
-
365
+
365
366
$ configContent = [];
366
367
$ breakForeach = false ;
367
368
$ state = 'SEARCH_FOR_DEFINITION ' ;
@@ -373,7 +374,7 @@ private function _outFile($uuid, $ModelName, $confName){
373
374
$ state = 'SEARCH_FOR_OBJECT_NAME ' ;
374
375
}
375
376
break ;
376
-
377
+
377
378
case 'SEARCH_FOR_OBJECT_NAME ' : //host_name, command_name, contacnt_name, etc
378
379
if (preg_match ('/ ' .$ needel .'/ ' , $ line )){
379
380
$ check = explode ($ needel , $ line );
@@ -388,15 +389,15 @@ private function _outFile($uuid, $ModelName, $confName){
388
389
$ state = 'SEARCH_FOR_END_OF_DEFENITION_AND_CONTINUE ' ;
389
390
}
390
391
break ;
391
-
392
+
392
393
case 'SEARCH_FOR_END_OF_DEFENITION ' :
393
394
if (trim ($ line ) == '} ' ){
394
395
// We have the complet object now, so we can break out of switch and foreach
395
396
$ breakForeach = true ;
396
397
break ;
397
398
}
398
399
break ;
399
-
400
+
400
401
case 'SEARCH_FOR_END_OF_DEFENITION_AND_CONTINUE ' :
401
402
// this was the wrong object, so throw everyting away and continue with next definition
402
403
if (trim ($ line ) == '} ' ){
@@ -405,12 +406,12 @@ private function _outFile($uuid, $ModelName, $confName){
405
406
}
406
407
break ;
407
408
}
408
-
409
+
409
410
if ($ breakForeach === true ){
410
411
break ;
411
412
}
412
413
}
413
-
414
+
414
415
$ this ->hr ();
415
416
$ 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> ' );
416
417
$ this ->hr ();
@@ -423,25 +424,25 @@ private function _outFile($uuid, $ModelName, $confName){
423
424
424
425
}
425
426
}
426
-
427
+
427
428
public function searchUuids ($ string = '' ){
428
429
if (!isset ($ this ->uuidCache ) || empty ($ this ->uuidCache ) || !is_array ($ this ->uuidCache )){
429
430
App::uses ('UUID ' , 'Lib ' );
430
- $ this ->… _buildUuidCache ();
431
+ $ this ->_buildUuidCache ();
431
432
}
432
-
433
+
433
434
$ string = preg_replace_callback (UUID ::regex (), [$ this , '_replaceUuid ' ], $ string );
434
435
return $ string ;
435
436
}
436
-
437
+
437
438
public function searchByUuid ($ uuid = null ){
438
439
if (isset ($ this ->uuidCache [$ uuid ])){
439
440
return $ this ->uuidCache [$ uuid ];
440
441
}
441
-
442
+
442
443
return [];
443
444
}
444
-
445
+
445
446
private function _replaceUuid ($ matches ){
446
447
foreach ($ matches as $ match ){
447
448
if (isset ($ this ->uuidCache [$ match ])){
@@ -453,21 +454,21 @@ private function _replaceUuid($matches){
453
454
}else {
454
455
return "<error>name not found in DB</error><comment>[ " .$ match ."]</comment> " ;
455
456
}
456
-
457
+
457
458
}
458
459
459
460
return "<error>object not found in UUID cache</error><comment>[ " .$ match ."]</comment> " ;
460
461
}
461
462
}
462
-
463
+
463
464
public function translateStdin (){
464
465
$ result = null ;
465
466
do {
466
467
$ result = $ this ->stdin ->read ();
467
468
$ this ->out ($ this ->searchUuids ($ result ));
468
469
}while ($ result !== false );
469
470
}
470
-
471
+
471
472
/**
472
473
* Unbind all accociations for the next find() call for every model
473
474
*
0 commit comments