Skip to content

Commit

Permalink
Statusengine using syslog now instead of own logfile #15
Browse files Browse the repository at this point in the history
Statusengine is now using syslog as "statusengine" to resolve the issue #15
so Statusengine dont need to care about log rotating and stuff like this
  • Loading branch information
nook24 committed Jan 19, 2016
1 parent 51db9b6 commit 38b360c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 111 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Check the documentation for the [migration guide](http://statusengine.org/gettin

**1.5.4**
- Resolve issue with orphaned child processes [Issue 14](https://github.com/nook24/statusengine/issues/14)
- Remove /var/log/statusengine.log [LogfileTask.php] and use syslog instead [Issue 15](https://github.com/nook24/statusengine/issues/15)

##Licence

Expand Down
6 changes: 0 additions & 6 deletions cakephp/app/Config/Perfdata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
'perfdata' => [
//Version of Statusengine's Perfdata extension
'version' => '1.1.0',

/* Logfile of Statusengin's ModPerfdata
* Type: String
* Default: /var/log/mod_perfdata.log
*/
'logfile' => '/var/log/mod_perfdata.log',

/* Number of worker processes, ModPerfdata will create
* Type: Integer
Expand Down
3 changes: 1 addition & 2 deletions cakephp/app/Config/Statusengine.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
//Version of Statusengine
'version' => '1.5.4',

//Logfile, where statusengine will log some information
'logfile' => '/var/log/statusengine.log',
//Logfile moved to SysLog!

//max age of service status records in gearman queue
'servicestatus_freshness' => 300,
Expand Down
27 changes: 12 additions & 15 deletions cakephp/app/Console/Command/ModPerfdataShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

class ModPerfdataShell extends AppShell{

public $tasks = ['Logfile', 'Perfdata'];
public $tasks = ['Perfdata'];

//Some class variables
protected $worker = null;
Expand All @@ -73,9 +73,6 @@ public function main(){
Configure::load('Perfdata');
$this->Config = Configure::read('perfdata');

$this->Logfile->init($this->Config['logfile']);
$this->Logfile->welcome();

//Load CakePHP's File class
App::uses('File', 'Utility');

Expand All @@ -91,7 +88,7 @@ public function main(){
//debug($this->Perfdata->parsePerfdataString('active=650;jobs=650;worker=3436;queues=29'));die();
//debug($this->Perfdata->parseCheckCommand('84084403-5c21-4273-835b-d8ac770b4a9f!7.0,6.0,5.0!10.0,7.0,6.0'));die();

$this->Perfdata->init($this->Config, $this->Logfile);
$this->Perfdata->init($this->Config);

$this->forkWorkers();

Expand All @@ -103,10 +100,10 @@ public function forkWorkers(){
declare(ticks = 1);
if($this->Config['worker'] > 1){
for($i = 1; $i < $this->Config['worker']; $i++){
$this->Logfile->stlog('Forking a new worker child');
CakeLog::info('Forking a new worker child');
$pid = pcntl_fork();
if(!$pid){
$this->Logfile->clog('Hey, I\'m a new child');
CakeLog::info('Hey, I\'m a new child');
pcntl_signal(SIGTERM, [$this, 'childSignalHandler']);
//Run while(true) to prevent a forkcalypse
$this->createWorker();
Expand Down Expand Up @@ -134,9 +131,9 @@ public function signalHandler($signo){
switch($signo){
case SIGINT:
case SIGTERM:
$this->Logfile->stlog('Will kill my childs :-(');
CakeLog::info('Will kill my childs :-(');
$this->sendSignal(SIGTERM);
$this->Logfile->stlog('Bye');
CakeLog::info('Bye');
exit(0);
break;
}
Expand All @@ -153,29 +150,29 @@ public function signalHandler($signo){
public function sendSignal($signal){
if($signal !== SIGTERM){
foreach($this->childPids as $cpid){
$this->Logfile->stlog('Send signal to child pid: '.$cpid);
CakeLog::info('Send signal to child pid: '.$cpid);
posix_kill($cpid, $signal);
}
}

if($signal == SIGTERM){
foreach($this->childPids as $cpid){
$this->Logfile->stlog('Will kill pid: '.$cpid);
CakeLog::info('Will kill pid: '.$cpid);
posix_kill($cpid, SIGTERM);
}
foreach($this->childPids as $cpid){
pcntl_waitpid($cpid, $status);
$this->Logfile->stlog('Child ['.$cpid.'] killed successfully');
CakeLog::info('Child ['.$cpid.'] killed successfully');
}
}
}

public function childSignalHandler($signo){
$this->Logfile->clog('Recived signal: '.$signo);
CakeLog::info('Recived signal: '.$signo);
switch($signo){
case SIGTERM:
$this->Logfile->clog('Will kill myself :-(');
$this->Logfile->clog('Unregister all my queues');
CakeLog::info('Will kill myself :-(');
CakeLog::info('Unregister all my queues');
exit(0);
break;
}
Expand Down
74 changes: 36 additions & 38 deletions cakephp/app/Console/Command/StatusengineLegacyShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
**********************************************************************************/

class StatusengineLegacyShell extends AppShell{
public $tasks = ['Logfile', 'Memcached', 'Perfdata'];
public $tasks = ['Memcached', 'Perfdata'];

//Load models out of Plugin/Legacy/Model
public $uses = [
Expand Down Expand Up @@ -184,11 +184,9 @@ public function main(){
$this->instance_id = Configure::read('instance_id');
$this->config_type = Configure::read('config_type');

$this->Logfile->init(Configure::read('logfile'));
$this->parentPid = getmypid();
$this->Logfile->welcome();
$this->parser = $this->getOptionParser();
$this->Logfile->stlog('THIS IS LEGACY MODE!');
CakeLog::info('Starting Statusengine in legacy mode.');
$this->servicestatus_freshness = Configure::read('servicestatus_freshness');

$this->processPerfdata = Configure::read('process_perfdata');
Expand All @@ -197,7 +195,7 @@ public function main(){
if($this->processPerfdata === true){
Configure::load('Perfdata');
$this->PerfdataConfig = Configure::read('perfdata');
$this->Perfdata->init($this->PerfdataConfig, $this->Logfile);
$this->Perfdata->init($this->PerfdataConfig);
}


Expand Down Expand Up @@ -234,7 +232,7 @@ public function main(){
]);

$this->gearmanConnect();
$this->Logfile->stlog('Lets rock!');
CakeLog::info('Lets rock!');
}

}
Expand Down Expand Up @@ -278,7 +276,7 @@ public function dumpObjects($job){

$this->dumpObjects = true;
$this->fakeLastInsertId = 1;
$this->Logfile->stlog('Start dumping objects');
CakeLog::info('Start dumping objects');
$this->disableAll();
//Legacy behavior :(
$truncate = [
Expand Down Expand Up @@ -331,16 +329,16 @@ public function dumpObjects($job){
break;

case FINISH_OBJECT_DUMP:
$this->Logfile->stlog('Finished dumping objects');
CakeLog::info('Finished dumping objects');
$this->buildHoststatusCache();
$this->buildServicestatusCache();
$this->saveParentHosts();
$this->saveParentServices();
//We are done with object dumping and can write parent hosts and services to DB

$this->Logfile->stlog('Start dumping core config '.Configure::read('coreconfig').' to database');
CakeLog::info('Start dumping core config '.Configure::read('coreconfig').' to database');
$this->dumpCoreConfig();
$this->Logfile->stlog('Core config dump finished');
CakeLog::info('Core config dump finished');

if($this->workerMode === true){
$this->sendSignal(SIGUSR1);
Expand Down Expand Up @@ -1479,7 +1477,7 @@ public function processServicechecks($job){
$payload = json_decode($job->workload());
$service_object_id = $this->objectIdFromCache(OBJECT_SERVICE, $payload->servicecheck->host_name, $payload->servicecheck->service_description);
if($service_object_id === null){
//$this->Logfile->clog(var_export($this->objectCache ,true));
//CakeLog::debug(var_export($this->objectCache ,true));
return;
}

Expand Down Expand Up @@ -2424,9 +2422,9 @@ public function objectIdFromCache($objecttype_id, $name1, $name2 = null, $defaul
}

public function objectIdFromCacheDebug($objecttype_id, $name1, $name2 = null, $default = null){
$this->Logfile->clog('name1: '.$name1);
$this->Logfile->clog('name1: '.$name2);
$this->Logfile->clog('isset: '.(int)isset($this->objectCache[$objecttype_id][$name1.$name2]['object_id']));
CakeLog::debug('name1: '.$name1);
CakeLog::debug('name1: '.$name2);
CakeLog::debug('isset: '.(int)isset($this->objectCache[$objecttype_id][$name1.$name2]['object_id']));
if(isset($this->objectCache[$objecttype_id][$name1.$name2]['object_id'])){
return $this->objectCache[$objecttype_id][$name1.$name2]['object_id'];
}
Expand Down Expand Up @@ -2566,7 +2564,7 @@ public function saveParentHosts(){
}

public function saveParentServices(){
$this->Logfile->clog(var_export($this->createParentServices, true));
//CakeLog::debug(var_export($this->createParentServices, true));
foreach($this->createParentServices as $service_id => $servicesArray){
foreach($servicesArray as $serviceArray){
$this->Parentservice->create();
Expand Down Expand Up @@ -2658,11 +2656,11 @@ public function forkWorker(){
$workers = Configure::read('workers');
foreach($workers as $worker){
declare(ticks = 1);
$this->Logfile->stlog('Forking a new worker child');
CakeLog::info('Forking a new worker child');
$pid = pcntl_fork();
if(!$pid){
//We are the child
$this->Logfile->clog('Hey, my queues are: '.implode(',', array_keys($worker['queues'])));
CakeLog::info('Hey, my queues are: '.implode(',', array_keys($worker['queues'])));
$this->bindQueues = true;
$this->queues = $worker['queues'];
$this->work = false;
Expand Down Expand Up @@ -2693,7 +2691,7 @@ public function forkWorker(){
]);

$this->gearmanConnect();
$this->Logfile->stlog('Lets rock!');
CakeLog::info('Lets rock!');
$this->sendSignal(SIGUSR1);
$this->worker->setTimeout(1000);

Expand Down Expand Up @@ -2725,7 +2723,7 @@ public function forkWorker(){
* @return void
*/
public function waitForInstructions(){
$this->Logfile->clog('Ok, i will wait for instructions');
CakeLog::info('Ok, i will wait for instructions');
if($this->bindQueues === true){
$this->worker = new GearmanWorker();

Expand All @@ -2737,41 +2735,41 @@ public function waitForInstructions(){

$this->worker->addServer(Configure::read('server'), Configure::read('port'));
foreach($this->queues as $queueName => $functionName){
$this->Logfile->clog('Queue "'.$queueName.'" will be handled by function "'.$functionName.'"');
CakeLog::info('Queue "'.$queueName.'" will be handled by function "'.$functionName.'"');
$this->worker->addFunction($queueName, [$this, $functionName]);
}
$this->bindQueues = false;
}
while(true){
if($this->work === true){
$this->Logfile->clog('Clear my objects cache');
CakeLog::info('Clear my objects cache');
$this->clearObjectsCache();

$this->Logfile->clog('Build up new objects cache');
CakeLog::info('Build up new objects cache');
$this->buildObjectsCache();
//$this->Logfile->clog(var_export($this->objectCache, true));
//CakeLog::debug(var_export($this->objectCache, true));

$this->Logfile->clog('Build up new hoststatus cache');
CakeLog::info('Build up new hoststatus cache');
$this->buildHoststatusCache();

$this->Logfile->clog('Build up new servicestatus cache');
CakeLog::info('Build up new servicestatus cache');
$this->buildServicestatusCache();

if($this->processPerfdata === true){
if(isset($this->queues['statusngin_servicechecks'])){
$this->Logfile->clog('Build up new process perfdata cache');
CakeLog::info('Build up new process perfdata cache');
$this->buildProcessPerfdataCache();
}
}

$this->Logfile->clog('I will continue my work');
CakeLog::info('I will continue my work');
$this->childWork();

}
pcntl_signal_dispatch();
//Check if the parent process still exists
if($this->parentPid != posix_getppid()){
$this->Logfile->clog('My parent process is gone I guess I am orphaned and will exit now!');
CakeLog::error('My parent process is gone I guess I am orphaned and will exit now!');
exit(3);
}
usleep(250000);
Expand Down Expand Up @@ -2799,19 +2797,19 @@ public function childWork(){

//Check if the parent process still exists
if($this->parentPid != posix_getppid()){
$this->Logfile->clog('My parent process is gone I guess I am orphaned and will exit now!');
CakeLog::error('My parent process is gone I guess I am orphaned and will exit now!');
exit(3);
}
}
}
}

public function childSignalHandler($signo){
$this->Logfile->clog('Recived signal: '.$signo);
CakeLog::info('Recived signal: '.$signo);
switch($signo){
case SIGTERM:
$this->Logfile->clog('Will kill myself :-(');
$this->Logfile->clog('Unregister all my queues');
CakeLog::info('Will kill myself :-(');
CakeLog::info('Unregister all my queues');
exit(0);
break;

Expand Down Expand Up @@ -2840,9 +2838,9 @@ public function signalHandler($signo){
switch($signo){
case SIGINT:
case SIGTERM:
$this->Logfile->stlog('Will kill my childs :-(');
CakeLog::info('Will kill my childs :-(');
$this->sendSignal(SIGTERM);
$this->Logfile->stlog('Bye');
CakeLog::info('Bye');
exit(0);
break;
}
Expand All @@ -2862,19 +2860,19 @@ public function sendSignal($signal){
$gmanClient->addServer(Configure::read('server'), Configure::read('port'));
if($signal !== SIGTERM){
foreach($this->childPids as $cpid){
$this->Logfile->stlog('Send signal to child pid: '.$cpid);
CakeLog::info('Send signal to child pid: '.$cpid);
posix_kill($cpid, $signal);
}
}

if($signal == SIGTERM){
foreach($this->childPids as $cpid){
$this->Logfile->stlog('Will kill pid: '.$cpid);
CakeLog::info('Will kill pid: '.$cpid);
posix_kill($cpid, SIGTERM);
}
foreach($this->childPids as $cpid){
pcntl_waitpid($cpid, $status);
$this->Logfile->stlog('Child ['.$cpid.'] killed successfully');
CakeLog::info('Child ['.$cpid.'] killed successfully');
}
}
}
Expand Down Expand Up @@ -2930,7 +2928,7 @@ public function dumpCoreConfig(){
}
}
}else{
$this->Logfile->stlog('ERROR: Core config '.$configFile.' not found!!!');
CakeLog::info('ERROR: Core config '.$configFile.' not found!!!');
}
}
}
Loading

0 comments on commit 38b360c

Please sign in to comment.