Skip to content

Commit

Permalink
Merge pull request #231 from it-novum/development
Browse files Browse the repository at this point in the history
Final merge before release
  • Loading branch information
cfleck authored Jul 26, 2016
2 parents 6864ad4 + 884b36c commit a0e385a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 83 deletions.
12 changes: 6 additions & 6 deletions app/Config/cmddaemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
$config = [
//Path to the named pipe of the daemon
'pipe' => '/opt/openitc/nagios/var/rw/oitc.cmd',

//Chmod of named pipe
'mode' => 0660,
//Sleep, between the named pipe gets read

//Sleep, between the named pipe gets read in μs (microseconds)
'sleep' => 500000,

//Loglevel
// 0 Disable log
// 1 Log Info
// 2 Log Warning
// 4 Log fatal
// 8 Log debug
//Simply add up to combine different log level
// 1 + 2 + 4 = 7
// 1 + 2 + 4 = 7
'loglevel' => 7,

//Path to the logfile
'logfile' => '/var/log/oitc_cmd.log'
];
103 changes: 52 additions & 51 deletions app/Console/Command/Task/DebugConfigNagiosTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
// confirmation.

class DebugConfigNagiosTask extends AppShell {

public $uses = ['Host', 'Hosttemplate', 'Timeperiod', 'Command', 'Contact', 'Contactgroup', 'Container', 'Customvariable', 'Hostescalation', 'Hostgroup', 'Service', 'Servicetemplate', 'Serviceescalations', 'Servicegroup', 'Hostdependency', 'Servicedependency'];

public function execute() {
//Do some cool stuff
debug($this->Hosttemplate->find('all'));
}

public function setup($conf = []){
$this->conf = $conf;
$this->monitoringLog = Configure::read('nagios.logfilepath').Configure::read('nagios.logfilename');
Expand All @@ -43,7 +43,7 @@ public function setup($conf = []){
$this->_buildUuidCache();
$this->searchUuids();
}

private function _buildUuidCache(){
$this->uuidCache = [];
$Models = ['Host', 'Hosttemplate', 'Timeperiod', 'Command', 'Contact', 'Contactgroup', 'Hostgroup', 'Servicegroup', 'Service', 'Servicetemplate'];
Expand Down Expand Up @@ -109,18 +109,18 @@ private function _buildUuidCache(){
'fields' => ['Servicetemplate.id', 'Servicetemplate.uuid', 'Servicetemplate.name'],
]
];

foreach($Models as $ModelName){
if(!in_array($ModelName, $this->uses)){
$this->loadModel($ModelName);
}

foreach($this->{$ModelName}->find('all', $options[$ModelName]) as $result){
$tmp_result = [];
if(isset($result[$ModelName]['id'])){
$tmp_result['id'] = $result[$ModelName]['id'];
}

if(isset($result[$ModelName]['name'])){
$tmp_result['name'] = $result[$ModelName]['name'];
}else{
Expand All @@ -132,32 +132,32 @@ private function _buildUuidCache(){
}
}
}

if(isset($result[$ModelName]['description'])){
$tmp_result['description'] = $result[$ModelName]['description'];
}

if(isset($result[$ModelName]['container_id'])){
$tmp_result['container_id'] = $result[$ModelName]['container_id'];
}

if(isset($result['Container']['name'])){
$tmp_result['container_name'] = $result['Container']['name'];
}


$tmp_result['ModelName'] = $ModelName;

if(!isset($result[$ModelName]['uuid'])){
debug($result[$ModelName]);
}

$this->uuidCache[$result[$ModelName]['uuid']] = $tmp_result;
unset($tmp_result);
}
}
}

public function debug($ModelName = null, $confName){
if($ModelName !== null && is_array($this->uses)){
$ModelSchema = $this->{$ModelName}->schema();
Expand All @@ -184,7 +184,7 @@ public function debug($ModelName = null, $confName){
$this->out('<error>'.__d('oitc_console', 'No name field for '.$ModelName . ' found in database!').'</error>');
$result = $this->{$ModelName}->find('all');
}

if(sizeof($result)>1){
if(isset($input)){
$this->out(__d('oitc_console', 'I found '.sizeof($result).' results matching to "'. $input . '". Please select one '.$ModelName. ' by typing the number in square brackets'));
Expand Down Expand Up @@ -215,16 +215,16 @@ public function debug($ModelName = null, $confName){
$this->out('<error>No object metching given conditions</error>');
}
}

}else{
echo "Unknown Model !";
exit();
}
}

public function debugByUuid($uuid = null){
if($uuid !== null){

}else{
$input = $this->in(__d('oitc_console', 'Please enter your UUID'));
$result = $this->searchByUuid($input);
Expand All @@ -235,7 +235,7 @@ public function debugByUuid($uuid = null){
}
}
}

public function parseMonitoringLogfile(){
foreach($this->tail() as $line){
$timestamp = null;
Expand All @@ -247,18 +247,18 @@ public function parseMonitoringLogfile(){
}
}
}

private function tail($lines = 100){
$return = [];
$file = fopen($this->monitoringLog, "r");
$flen = filesize($this->monitoringLog);
if( $flen % 4096 != 0 ){
$flen = ((int)($flen / 4096) + 1) * 4096;
}

$newlines = 0;
$startpos = -1;

for( $goto = 0; $goto <= $flen - 4096; $goto += 4096 ){
fseek($file, $flen - $goto - 4096);
$data = fread($file, 4096);
Expand All @@ -273,23 +273,24 @@ private function tail($lines = 100){
}
}
}

if( $startpos != -1 ){
fseek($file, $startpos);
$return[] = fread($file, $flen - $startpos);
$fileContent = fread($file, $flen - $startpos);
$return[] = $this->searchUuids($fileContent);
}
$ret = array();
foreach($return as $r){
$ret = explode("\n", $r);
}
return $ret;
}

public function tailf(){
$callback = function($timestamp){
return '<comment>'.date('d.m.Y - H:i:s', $timestamp[0]).'</comment>';
};

$options = array(
'cwd' => Configure::read('nagios.logfilepath'),
'env' => array(
Expand All @@ -298,31 +299,31 @@ public function tailf(){
'LC_ALL' => 'C',
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'),
);

$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "r")
);

$process = proc_open('/usr/bin/tail -f -n 100 '.Configure::read('nagios.logfilename'), $descriptorspec, $pipes, $options['cwd'], $options['env']);
while(true){
$status = proc_get_status($process);
if($status['running'] != 1 && $line == ''){
if($status['running'] != 1){
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
break;
}

$string = fgets($pipes[1], 1024);
$string = preg_replace_callback('#\d{10,11}#', $callback, $string);
$string = $this->searchUuids($string);
$this->out($string, 0);
}
}

private function _outFile($uuid, $ModelName, $confName){
if(!$this->conf['minified'] || in_array($ModelName, ['Hostdependency', 'Hostescalation', 'Servicedependency', 'Serviceescalation', 'Servicegroup'])){
// Model that are not saved as minified config files or minified configs asre tournd off
Expand All @@ -342,10 +343,10 @@ private function _outFile($uuid, $ModelName, $confName){
// User want the file of an object, that is inside of an minified file, so we need to parse the minified config
if(file_exists($this->conf['path'].$this->conf[$confName].$confName.'_minified'.$this->conf['suffix'])){
$fileAsArray = file($this->conf['path'].$this->conf[$confName].$confName.'_minified'.$this->conf['suffix']);

$content = [];
$startParsing = false;

$modelToNagios = [
'Command' => 'command_name',
//'Contactgroup' => ,
Expand All @@ -357,11 +358,11 @@ private function _outFile($uuid, $ModelName, $confName){
'Servicetemplate' => 'service_description',
'Timeperiod' => 'timeperiod_name'
];

$needel = $modelToNagios[$ModelName];

$searchForEnd = false;

$configContent = [];
$breakForeach = false;
$state = 'SEARCH_FOR_DEFINITION';
Expand All @@ -373,7 +374,7 @@ private function _outFile($uuid, $ModelName, $confName){
$state = 'SEARCH_FOR_OBJECT_NAME';
}
break;

case 'SEARCH_FOR_OBJECT_NAME': //host_name, command_name, contacnt_name, etc
if(preg_match('/'.$needel.'/', $line)){
$check = explode($needel, $line);
Expand All @@ -388,15 +389,15 @@ private function _outFile($uuid, $ModelName, $confName){
$state = 'SEARCH_FOR_END_OF_DEFENITION_AND_CONTINUE';
}
break;

case 'SEARCH_FOR_END_OF_DEFENITION':
if(trim($line) == '}'){
// We have the complet object now, so we can break out of switch and foreach
$breakForeach = true;
break;
}
break;

case 'SEARCH_FOR_END_OF_DEFENITION_AND_CONTINUE':
// this was the wrong object, so throw everyting away and continue with next definition
if(trim($line) == '}'){
Expand All @@ -405,12 +406,12 @@ private function _outFile($uuid, $ModelName, $confName){
}
break;
}

if($breakForeach === true){
break;
}
}

$this->hr();
$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>');
$this->hr();
Expand All @@ -423,25 +424,25 @@ private function _outFile($uuid, $ModelName, $confName){

}
}

public function searchUuids($string = ''){
if(!isset($this->uuidCache) || empty($this->uuidCache) || !is_array($this->uuidCache)){
App::uses('UUID', 'Lib');
$this->_buildUuidCache();
$this->_buildUuidCache();
}

$string = preg_replace_callback(UUID::regex(), [$this, '_replaceUuid'], $string);
return $string;
}

public function searchByUuid($uuid = null){
if(isset($this->uuidCache[$uuid])){
return $this->uuidCache[$uuid];
}

return [];
}

private function _replaceUuid($matches){
foreach($matches as $match){
if(isset($this->uuidCache[$match])){
Expand All @@ -453,21 +454,21 @@ private function _replaceUuid($matches){
}else{
return "<error>name not found in DB</error><comment>[".$match."]</comment>";
}

}

return "<error>object not found in UUID cache</error><comment>[".$match."]</comment>";
}
}

public function translateStdin(){
$result = null;
do{
$result = $this->stdin->read();
$this->out($this->searchUuids($result));
}while($result !== false);
}

/**
* Unbind all accociations for the next find() call for every model
*
Expand Down
Loading

0 comments on commit a0e385a

Please sign in to comment.