From ba43787dbfdb15b5e2ce4fa7029d1ab45de2a791 Mon Sep 17 00:00:00 2001 From: Daniel Ziegler Date: Thu, 6 Sep 2018 17:51:39 +0200 Subject: [PATCH 1/4] Update copyright notice --- bin/Console.php | 2 +- bin/StatusengineWorker.php | 2 +- bootstrap.php | 2 +- src/BackendSelector.php | 2 +- src/Backends/Crate/Crate.php | 2 +- src/Backends/Crate/CrateModel.php | 2 +- src/Backends/Crate/SqlObjects/CrateHostAcknowledgement.php | 2 +- src/Backends/Crate/SqlObjects/CrateHostDowntimehistory.php | 2 +- src/Backends/Crate/SqlObjects/CrateHostScheduleddowntime.php | 2 +- src/Backends/Crate/SqlObjects/CrateHostcheck.php | 2 +- src/Backends/Crate/SqlObjects/CrateHoststatus.php | 2 +- src/Backends/Crate/SqlObjects/CrateLogentry.php | 2 +- src/Backends/Crate/SqlObjects/CrateNotification.php | 2 +- src/Backends/Crate/SqlObjects/CratePerfdata.php | 2 +- src/Backends/Crate/SqlObjects/CrateServiceAcknowledgement.php | 2 +- src/Backends/Crate/SqlObjects/CrateServiceDowntimehistory.php | 2 +- src/Backends/Crate/SqlObjects/CrateServiceScheduleddowntime.php | 2 +- src/Backends/Crate/SqlObjects/CrateServicecheck.php | 2 +- src/Backends/Crate/SqlObjects/CrateServicestatus.php | 2 +- src/Backends/Crate/SqlObjects/CrateStatechange.php | 2 +- src/Backends/Crate/SqlObjects/CrateTask.php | 2 +- src/Backends/MySQL/MySQL.php | 2 +- src/Backends/MySQL/MysqlModel.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlHostAcknowledgement.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlHostDowntimehistory.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlHostScheduleddowntime.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlHostcheck.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlHoststatus.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlLogentry.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlNotification.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlPerfdata.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlServiceAcknowledgement.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlServiceDowntimehistory.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlServiceScheduleddowntime.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlServicecheck.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlServicestatus.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlStatechange.php | 2 +- src/Backends/MySQL/SqlObjects/MysqlTask.php | 2 +- src/Backends/PerfdataBackends/CratePerfdata.php | 2 +- src/Backends/PerfdataBackends/ElasticsearchPerfdata.php | 2 +- src/Backends/PerfdataBackends/GraphitePerfdata.php | 2 +- src/Backends/PerfdataBackends/MysqlPerfdata.php | 2 +- src/Backends/PerfdataBackends/PerfdataStorageBackends.php | 2 +- src/Backends/Redis/Redis.php | 2 +- src/Backends/StorageBackend.php | 2 +- src/BulkInsertObjectStore.php | 2 +- src/Child.php | 2 +- src/ChildSignalHandler.php | 2 +- src/Childs/HostcheckChild.php | 2 +- src/Childs/HoststatusChild.php | 2 +- src/Childs/LogentryChild.php | 2 +- src/Childs/MiscChild.php | 2 +- src/Childs/PerfdataChild.php | 2 +- src/Childs/ServicecheckChild.php | 2 +- src/Childs/ServicestatusChild.php | 2 +- src/Childs/StatechangeChild.php | 2 +- src/Config.php | 2 +- src/Config/Acknowledgement.php | 2 +- src/Config/Downtime.php | 2 +- src/Config/Hostcheck.php | 2 +- src/Config/Hoststatus.php | 2 +- src/Config/Logentry.php | 2 +- src/Config/MonitoringRestart.php | 2 +- src/Config/Notification.php | 2 +- src/Config/Perfdata.php | 2 +- src/Config/Servicecheck.php | 2 +- src/Config/Servicestatus.php | 2 +- src/Config/Statechange.php | 2 +- src/Config/StatisticType.php | 2 +- src/Config/WorkerConfig.php | 2 +- src/Console/Cleanup.php | 2 +- src/Console/Cluster.php | 2 +- src/Console/Statistics.php | 2 +- src/Exceptions/FileNotFoundException.php | 2 +- src/Exceptions/InvalidArgumentException.php | 2 +- src/Exceptions/NoBackendFoundException.php | 2 +- src/Exceptions/NotNumericValueException.php | 2 +- src/Exceptions/StorageBackendUnavailableExceptions.php | 2 +- src/Exceptions/TimeoutException.php | 2 +- src/Exceptions/UnknownTaskTypeException.php | 2 +- src/Exceptions/UnknownTypeException.php | 2 +- src/ExternalCommandFile.php | 2 +- src/GearmanWorker.php | 2 +- src/HoststatusList.php | 2 +- src/Output/HumanStatistics.php | 2 +- src/Output/NaemonPerfdata.php | 2 +- src/ParentProcess.php | 2 +- src/ParentSignalHandler.php | 2 +- src/PerfdataParser.php | 2 +- src/ProxySettings.php | 2 +- src/QueryHandler.php | 2 +- src/ServicestatusList.php | 2 +- src/StatisticCollector.php | 2 +- src/Statistics.php | 2 +- src/StatisticsMatcher.php | 2 +- src/TaskManager.php | 2 +- src/TcpSocket.php | 2 +- src/ValueObjects/Acknowledgement.php | 2 +- src/ValueObjects/DataStructInterface.php | 2 +- src/ValueObjects/Downtime.php | 2 +- src/ValueObjects/Gauge.php | 2 +- src/ValueObjects/Hostcheck.php | 2 +- src/ValueObjects/Hoststatus.php | 2 +- src/ValueObjects/Logentry.php | 2 +- src/ValueObjects/Notification.php | 2 +- src/ValueObjects/PerfdataRaw.php | 2 +- src/ValueObjects/Pid.php | 2 +- src/ValueObjects/Servicecheck.php | 2 +- src/ValueObjects/Servicestatus.php | 2 +- src/ValueObjects/Statechange.php | 2 +- src/ValueObjects/Statistic.php | 2 +- src/ValueObjects/StatusInterface.php | 2 +- src/ValueObjects/Task.php | 2 +- 113 files changed, 113 insertions(+), 113 deletions(-) diff --git a/bin/Console.php b/bin/Console.php index 3b27e46..4f0f5a9 100755 --- a/bin/Console.php +++ b/bin/Console.php @@ -2,7 +2,7 @@ Date: Thu, 6 Sep 2018 20:52:16 +0200 Subject: [PATCH 2/4] Restart dead child processes in case of an unexpected error --- bin/StatusengineWorker.php | 71 +++++-------------------------- src/Backends/Crate/Crate.php | 69 ++++++++++++++++-------------- src/Backends/MySQL/MySQL.php | 15 +++++-- src/Child.php | 9 +++- src/Childs/HostcheckChild.php | 5 +++ src/Childs/HoststatusChild.php | 5 +++ src/Childs/LogentryChild.php | 5 +++ src/Childs/MiscChild.php | 5 +++ src/Childs/PerfdataChild.php | 5 +++ src/Childs/ServicecheckChild.php | 5 +++ src/Childs/ServicestatusChild.php | 5 +++ src/Childs/StatechangeChild.php | 5 +++ src/ParentProcess.php | 61 +++++++++++++++++++++++--- src/ValueObjects/Pid.php | 19 ++++++++- 14 files changed, 179 insertions(+), 105 deletions(-) diff --git a/bin/StatusengineWorker.php b/bin/StatusengineWorker.php index 7ad5ff9..34ca347 100755 --- a/bin/StatusengineWorker.php +++ b/bin/StatusengineWorker.php @@ -42,16 +42,11 @@ $pids = []; $ParentPid = new \Statusengine\ValueObjects\Pid(getmypid()); +$ChildFactory = new \Statusengine\ChildFactory($Config, $Syslog, $ParentPid); + if ($Config->isRedisEnabled() || $Config->storeLiveDateInArchive()) { for ($i = 0; $i < $Config->getNumberOfHoststatusWorkers(); $i++) { - $Syslog->info('Fork new host status worker'); - $HoststatusChild = new Statusengine\HoststatusChild( - $Config, - $ParentPid, - $Syslog - ); - $hoststatusChildPid = $HoststatusChild->fork(); - $pids[] = $hoststatusChildPid; + $pids[] = $ChildFactory->forkHoststatusChild(); } } @@ -70,74 +65,29 @@ if ($Config->isCrateEnabled() || $Config->isMysqlEnabled()) { for ($i = 0; $i < $Config->getNumberOfLogentryWorkers(); $i++) { - $Syslog->info('Fork new log entry worker'); - $LogentryChild = new Statusengine\LogentryChild( - $Config, - $ParentPid, - $Syslog - ); - $logentryChildPid = $LogentryChild->fork(); - $pids[] = $logentryChildPid; + $pids[] = $ChildFactory->forkLogentryChild(); } for ($i = 0; $i < $Config->getNumberOfStatechangeWorkers(); $i++) { - $Syslog->info('Fork new state change worker'); - $StatechangeConfig = new Statusengine\Config\Statechange(); - $StatechangeSignalHandler = new \Statusengine\ChildSignalHandler(); - $StatechangeStatistics = new \Statusengine\Redis\Statistics($Config, $Syslog); - $StatechangeChild = new Statusengine\StatechangeChild( - $Config, - $ParentPid, - $Syslog - ); - $statechangeChildPid = $StatechangeChild->fork(); - $pids[] = $statechangeChildPid; + $pids[] = $ChildFactory->forkStatechangeChild(); } for ($i = 0; $i < $Config->getNumberOfHostcheckWorkers(); $i++) { - $Syslog->info('Fork new host check worker'); - $HostcheckChild = new Statusengine\HostcheckChild( - $Config, - $ParentPid, - $Syslog - ); - $hostcheckChildPid = $HostcheckChild->fork(); - $pids[] = $hostcheckChildPid; + $pids[] = $ChildFactory->forkHostcheckChild(); } for ($i = 0; $i < $Config->getNumberOfServicecheckWorkers(); $i++) { - $Syslog->info('Fork new service check worker'); - $ServicecheckChild = new Statusengine\ServicecheckChild( - $Config, - $ParentPid, - $Syslog - ); - $servicecheckChildPid = $ServicecheckChild->fork(); - $pids[] = $servicecheckChildPid; + $pids[] = $ChildFactory->forkServicecheckChild(); } for ($i = 0; $i < $Config->getNumberOfMiscWorkers(); $i++) { - $Syslog->info('Fork new misc worker'); - $MiscChild = new Statusengine\MiscChild( - $Config, - $ParentPid, - $Syslog - ); - $miscChildPid = $MiscChild->fork(); - $pids[] = $miscChildPid; + $pids[] = $ChildFactory->forkMiscChild(); } } if ($Config->isProcessPerfdataEnabled() && $Config->isOnePerfdataBackendEnabled()) { for ($i = 0; $i < $Config->getNumberOfPerfdataWorkers(); $i++) { - $Syslog->info('Fork new performance data worker'); - $PerfdataChild = new Statusengine\PerfdataChild( - $Config, - $ParentPid, - $Syslog - ); - $perfdataChildPid = $PerfdataChild->fork(); - $pids[] = $perfdataChildPid; + $pids[] = $ChildFactory->forkPerfdataChild(); } } @@ -160,7 +110,8 @@ $TaskManager, $Syslog, $MonitoringRestartConfig, - $StorageBackend + $StorageBackend, + $ChildFactory ); foreach ($pids as $Pid) { $ParentProcess->addChildPid($Pid); diff --git a/src/Backends/Crate/Crate.php b/src/Backends/Crate/Crate.php index 7a59bc2..ddfa08e 100644 --- a/src/Backends/Crate/Crate.php +++ b/src/Backends/Crate/Crate.php @@ -91,11 +91,11 @@ public function __construct(Config $Config, BulkInsertObjectStore $BulkInsertObj * @param null|int $startTime */ public function saveNodeName($nodeName = null, $startTime = null) { - if($nodeName === null){ + if ($nodeName === null) { $nodeName = $this->nodeName; } - if($startTime === null){ + if ($startTime === null) { $startTime = time(); } @@ -119,7 +119,7 @@ public function saveNodeName($nodeName = null, $startTime = null) { /** * @return array */ - public function getNodes(){ + public function getNodes() { $this->connect(); $query = $this->Connection->prepare('SELECT * FROM statusengine_nodes ORDER BY node_name ASC'); @@ -131,7 +131,7 @@ public function getNodes(){ } $this->disconnect(); $nodes = []; - foreach($result as $record){ + foreach ($result as $record) { $nodes[] = NodeName::fromCrateDb($record); } return $nodes; @@ -140,7 +140,7 @@ public function getNodes(){ /** * @param string $nodeName */ - public function deleteNodeByName($nodeName){ + public function deleteNodeByName($nodeName) { $this->connect(); $query = $this->Connection->prepare('DELETE FROM statusengine_nodes WHERE node_name=?'); $query->bindValue(1, $nodeName); @@ -172,6 +172,9 @@ public function connect() { $this->Connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (\Exception $e) { $this->Syslog->error($e->getMessage()); + + //rethrow exception that the parent process will not die. + throw $e; } return $this->Connection; } @@ -179,7 +182,7 @@ public function connect() { /** * @param int $timeout in seconds */ - public function setTimeout($timeout){ + public function setTimeout($timeout) { $this->Connection->setAttribute(PDO::ATTR_TIMEOUT, $timeout); } @@ -379,13 +382,13 @@ public function deleteTaskByUuids($uuids = []) { public function deleteHostchecksOlderThan($timestamp) { $partitions = $this->getPartitionsByTableName('statusengine_hostchecks'); $daysToDelete = []; - foreach($partitions as $record){ - if(isset($record['values']['day']) && $record['values']['day'] < $timestamp){ + foreach ($partitions as $record) { + if (isset($record['values']['day']) && $record['values']['day'] < $timestamp) { $daysToDelete[] = $record['values']['day']; } } - foreach($daysToDelete as $partition){ + foreach ($daysToDelete as $partition) { $this->dropPartitionsFromTableByTableNameAndDayValue('statusengine_hostchecks', $partition); } } @@ -408,13 +411,13 @@ public function deleteHostAcknowledgementsOlderThan($timestamp) { public function deleteHostNotificationsOlderThan($timestamp) { $partitions = $this->getPartitionsByTableName('statusengine_host_notifications'); $daysToDelete = []; - foreach($partitions as $record){ - if(isset($record['values']['day']) && $record['values']['day'] < $timestamp){ + foreach ($partitions as $record) { + if (isset($record['values']['day']) && $record['values']['day'] < $timestamp) { $daysToDelete[] = $record['values']['day']; } } - foreach($daysToDelete as $partition){ + foreach ($daysToDelete as $partition) { $this->dropPartitionsFromTableByTableNameAndDayValue('statusengine_host_notifications', $partition); } } @@ -425,13 +428,13 @@ public function deleteHostNotificationsOlderThan($timestamp) { public function deleteHostStatehistoryOlderThan($timestamp) { $partitions = $this->getPartitionsByTableName('statusengine_host_statehistory'); $daysToDelete = []; - foreach($partitions as $record){ - if(isset($record['values']['day']) && $record['values']['day'] < $timestamp){ + foreach ($partitions as $record) { + if (isset($record['values']['day']) && $record['values']['day'] < $timestamp) { $daysToDelete[] = $record['values']['day']; } } - foreach($daysToDelete as $partition){ + foreach ($daysToDelete as $partition) { $this->dropPartitionsFromTableByTableNameAndDayValue('statusengine_host_statehistory', $partition); } } @@ -454,13 +457,13 @@ public function deleteHostDowntimeHistoryOlderThan($timestamp) { public function deleteServicechecksOlderThan($timestamp) { $partitions = $this->getPartitionsByTableName('statusengine_servicechecks'); $daysToDelete = []; - foreach($partitions as $record){ - if(isset($record['values']['day']) && $record['values']['day'] < $timestamp){ + foreach ($partitions as $record) { + if (isset($record['values']['day']) && $record['values']['day'] < $timestamp) { $daysToDelete[] = $record['values']['day']; } } - foreach($daysToDelete as $partition){ + foreach ($daysToDelete as $partition) { $this->dropPartitionsFromTableByTableNameAndDayValue('statusengine_servicechecks', $partition); } } @@ -483,13 +486,13 @@ public function deleteServiceAcknowledgementsOlderThan($timestamp) { public function deleteServiceNotificationsOlderThan($timestamp) { $partitions = $this->getPartitionsByTableName('statusengine_service_notifications'); $daysToDelete = []; - foreach($partitions as $record){ - if(isset($record['values']['day']) && $record['values']['day'] < $timestamp){ + foreach ($partitions as $record) { + if (isset($record['values']['day']) && $record['values']['day'] < $timestamp) { $daysToDelete[] = $record['values']['day']; } } - foreach($daysToDelete as $partition){ + foreach ($daysToDelete as $partition) { $this->dropPartitionsFromTableByTableNameAndDayValue('statusengine_service_notifications', $partition); } } @@ -531,13 +534,13 @@ public function deleteServiceDowntimeHistoryOlderThan($timestamp) { public function deleteLogentriesOlderThan($timestamp) { $partitions = $this->getPartitionsByTableName('statusengine_logentries'); $daysToDelete = []; - foreach($partitions as $record){ - if(isset($record['values']['day']) && $record['values']['day'] < $timestamp){ + foreach ($partitions as $record) { + if (isset($record['values']['day']) && $record['values']['day'] < $timestamp) { $daysToDelete[] = $record['values']['day']; } } - foreach($daysToDelete as $partition){ + foreach ($daysToDelete as $partition) { $this->dropPartitionsFromTableByTableNameAndDayValue('statusengine_logentries', $partition); } } @@ -558,18 +561,18 @@ public function deleteTasksOlderThan($timestamp) { * @param int $timestamp * @return bool */ - public function deletePerfdataOlderThan($timestamp){ + public function deletePerfdataOlderThan($timestamp) { $timestamp = $timestamp * 1000; $partitions = $this->getPartitionsByTableName('statusengine_perfdata'); $daysToDelete = []; - foreach($partitions as $record){ - if(isset($record['values']['day']) && $record['values']['day'] < $timestamp){ - $daysToDelete[] = $record['values']['day']; - } + foreach ($partitions as $record) { + if (isset($record['values']['day']) && $record['values']['day'] < $timestamp) { + $daysToDelete[] = $record['values']['day']; + } } - foreach($daysToDelete as $partition){ + foreach ($daysToDelete as $partition) { $this->dropPartitionsFromTableByTableNameAndDayValue('statusengine_perfdata', $partition); } } @@ -578,7 +581,7 @@ public function deletePerfdataOlderThan($timestamp){ * @param string $tablename * @return array */ - public function getPartitionsByTableName($tablename){ + public function getPartitionsByTableName($tablename) { $query = $this->prepare( 'SELECT * FROM information_schema.table_partitions WHERE table_name=?' ); @@ -592,7 +595,7 @@ public function getPartitionsByTableName($tablename){ * @param int $dayValue * @return bool */ - public function dropPartitionsFromTableByTableNameAndDayValue($tableName, $dayValue){ + public function dropPartitionsFromTableByTableNameAndDayValue($tableName, $dayValue) { $query = $this->prepare(sprintf('DELETE FROM %s WHERE DAY = ?', $tableName)); $query->bindValue(1, $dayValue); return $query->execute(); @@ -626,7 +629,7 @@ public function getServiceScheduleddowntimeBackend() { return new CrateServiceScheduleddowntime($this, $this->nodeName); } - public function monitoringengineWasRestarted(){ + public function monitoringengineWasRestarted() { $this->connect(); $Hoststatus = new CrateHoststatus($this, $this->BulkInsertObjectStore, $this->nodeName); $Hoststatus->truncate(); diff --git a/src/Backends/MySQL/MySQL.php b/src/Backends/MySQL/MySQL.php index 06198c8..186a309 100644 --- a/src/Backends/MySQL/MySQL.php +++ b/src/Backends/MySQL/MySQL.php @@ -108,11 +108,20 @@ public function connect() { $this->Connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (\Exception $e) { $this->Syslog->error($e->getMessage()); + + //rethrow exception that the parent process will not die. + throw $e; } //Enable UTF-8 - $query = $this->Connection->prepare('SET NAMES utf8'); - $query->execute(); + try { + $query = $this->Connection->prepare('SET NAMES utf8'); + $query->execute(); + } catch (\Exception $e) { + $this->Syslog->error($e->getMessage()); + //rethrow exception that the parent process will not die. + throw $e; + } return $this->Connection; } @@ -310,7 +319,7 @@ public function deleteTaskByUuids($uuids = []) { * @param int $timestamp * @return bool */ - public function deletePerfdataOlderThan($timestamp){ + public function deletePerfdataOlderThan($timestamp) { $query = $this->prepare( 'DELETE FROM statusengine_perfdata WHERE timestamp_unix < ?' ); diff --git a/src/Child.php b/src/Child.php index 23a7676..8265460 100644 --- a/src/Child.php +++ b/src/Child.php @@ -39,6 +39,11 @@ class Child { */ protected $parentPid; + /** + * @var string + */ + protected $childName = 'Unknown'; + /** * Child constructor. * @param WorkerConfig $Config @@ -55,7 +60,7 @@ public function fork() { $pid = pcntl_fork(); if (!$pid) { //We are the child - $this->Pid = new Pid(getmypid()); + $this->Pid = new Pid(getmypid(), $this->childName); $this->setup(); @@ -64,7 +69,7 @@ public function fork() { } //Return back to the parent process - return new Pid($pid); + return new Pid($pid, $this->childName); } /** diff --git a/src/Childs/HostcheckChild.php b/src/Childs/HostcheckChild.php index 3aa1142..f19aead 100644 --- a/src/Childs/HostcheckChild.php +++ b/src/Childs/HostcheckChild.php @@ -63,6 +63,11 @@ class HostcheckChild extends Child { */ private $QueueingEngine; + /** + * @var string + */ + protected $childName = 'HostcheckChild'; + /** * HostcheckChild constructor. * @param Config $Config diff --git a/src/Childs/HoststatusChild.php b/src/Childs/HoststatusChild.php index cc3e3ce..0c8aef7 100644 --- a/src/Childs/HoststatusChild.php +++ b/src/Childs/HoststatusChild.php @@ -88,6 +88,11 @@ class HoststatusChild extends Child { */ private $QueueingEngine; + /** + * @var string + */ + protected $childName = 'HoststatusChild'; + /** * HoststatusChild constructor. * @param Config $Config diff --git a/src/Childs/LogentryChild.php b/src/Childs/LogentryChild.php index e6a9a44..bba99eb 100644 --- a/src/Childs/LogentryChild.php +++ b/src/Childs/LogentryChild.php @@ -63,6 +63,11 @@ class LogentryChild extends Child { */ private $QueueingEngine; + /** + * @var string + */ + protected $childName = 'LogentryChild'; + /** * LogentryChild constructor. * @param Config $Config diff --git a/src/Childs/MiscChild.php b/src/Childs/MiscChild.php index 68a4753..2a46c53 100644 --- a/src/Childs/MiscChild.php +++ b/src/Childs/MiscChild.php @@ -80,6 +80,11 @@ class MiscChild extends Child { */ private $Syslog; + /** + * @var string + */ + protected $childName = 'MiscChild'; + /** * MiscChild constructor. * @param Config $Config diff --git a/src/Childs/PerfdataChild.php b/src/Childs/PerfdataChild.php index 9813408..6672aa0 100644 --- a/src/Childs/PerfdataChild.php +++ b/src/Childs/PerfdataChild.php @@ -70,6 +70,11 @@ class PerfdataChild extends Child { */ private $Syslog; + /** + * @var string + */ + protected $childName = 'PerfdataChild'; + /** * PerfdataChild constructor. * @param Config $Config diff --git a/src/Childs/ServicecheckChild.php b/src/Childs/ServicecheckChild.php index 402e074..b2db357 100644 --- a/src/Childs/ServicecheckChild.php +++ b/src/Childs/ServicecheckChild.php @@ -63,6 +63,11 @@ class ServicecheckChild extends Child { */ private $QueueingEngine; + /** + * @var string + */ + protected $childName = 'ServicecheckChild'; + /** * ServicecheckChild constructor. * @param Config $Config diff --git a/src/Childs/ServicestatusChild.php b/src/Childs/ServicestatusChild.php index 8548da8..ebf76a7 100644 --- a/src/Childs/ServicestatusChild.php +++ b/src/Childs/ServicestatusChild.php @@ -88,6 +88,11 @@ class ServicestatusChild extends Child { */ private $QueueingEngine; + /** + * @var string + */ + protected $childName = 'ServicestatusChild'; + /** * ServicestatusChild constructor. * @param Config $Config diff --git a/src/Childs/StatechangeChild.php b/src/Childs/StatechangeChild.php index 93f8769..1dd0645 100644 --- a/src/Childs/StatechangeChild.php +++ b/src/Childs/StatechangeChild.php @@ -58,6 +58,11 @@ class StatechangeChild extends Child { */ private $StorageBackend; + /** + * @var string + */ + protected $childName = 'StatechangeChild'; + /** * StatechangeChild constructor. diff --git a/src/ParentProcess.php b/src/ParentProcess.php index dae94d2..c1d00b1 100644 --- a/src/ParentProcess.php +++ b/src/ParentProcess.php @@ -83,6 +83,16 @@ class ParentProcess { */ private $QueueingEngine; + /** + * @var ChildFactory + */ + private $ChildFactory; + + /** + * @var array + */ + private $deadChilds = []; + /** * ParentProcess constructor. * @param StatisticCollector $StatisticCollector @@ -93,7 +103,8 @@ public function __construct( TaskManager $TaskManager, Syslog $Syslog, $MonitoringRestartConfig, - StorageBackend $StorageBackend + StorageBackend $StorageBackend, + ChildFactory $ChildFactory ) { $this->StatisticCollector = $StatisticCollector; $this->Config = $Config; @@ -101,6 +112,7 @@ public function __construct( $this->Syslog = $Syslog; $this->MonitoringRestartConfig = $MonitoringRestartConfig; $this->StorageBackend = $StorageBackend; + $this->ChildFactory = $ChildFactory; $this->QueueingEngine = new QueueingEngine($this->Config, $this->MonitoringRestartConfig); $this->Queue = $this->QueueingEngine->getQueue(); @@ -121,18 +133,43 @@ public function loop() { $this->checkForDeadChilds(); if ($this->checkForCommands) { - $this->TaskManager->checkAndProcessTasks(); + try { + $this->TaskManager->checkAndProcessTasks(); + }catch (\Exception $exception){ + $this->Syslog->error($exception->getMessage()); + } } //Also replaces sleep(1) $jobData = $this->Queue->getJob(); if ($jobData !== null) { //Monitoring engine was restarted - if($jobData->object_type == 102){ + if ($jobData->object_type == 102) { $this->Syslog->info('Catch monitoring restart. Trigger callbacks...'); $this->StorageBackend->monitoringengineWasRestarted(); } } + + //Check for dead childs + if (!empty($this->deadChilds)) { + /** @var Pid $deadChild */ + foreach ($this->deadChilds as $deadChild) { + $this->removePid($deadChild); + if ($this->ChildFactory->canChildBeReborn($deadChild->getChildName())) { + $this->Syslog->info('Respawn dead child'); + $newPid = $this->ChildFactory->respawn($deadChild->getChildName()); + $this->addChildPid($newPid); + } else { + $this->Syslog->error(sprintf( + 'Can not respawn child of type %s', + $deadChild->getChildName() + )); + } + } + $this->deadChilds = []; + //Update StatisticCollector with the new PIDs + $this->StatisticCollector->setPids($this->getChildPids()); + } } } @@ -150,8 +187,12 @@ public function getChildPids() { return $this->pids; } + /** + * @return Pid + * @throws Exception\NotNumericValueException + */ public function getPid() { - return new Pid(getmypid()); + return new Pid(getmypid(), 'ParentProcess'); } public function checkForDeadChilds() { @@ -160,11 +201,21 @@ public function checkForDeadChilds() { if (pcntl_waitpid($Pid->getPid(), $status, WNOHANG) == 0) { //Child still alive $pidsAlive[] = $Pid; - }else{ + } else { $this->Syslog->alert(sprintf('Child with pid %s is dead!!', $Pid->getPid())); + $this->deadChilds[] = $Pid; } } $this->pids = $pidsAlive; } + private function removePid(Pid $PidToRemove) { + /** @var Pid $Pid */ + foreach ($this->pids as $index => $Pid) { + if ($Pid->getPid() === $PidToRemove->getPid()) { + unset($this->pids[$index]); + } + } + } + } \ No newline at end of file diff --git a/src/ValueObjects/Pid.php b/src/ValueObjects/Pid.php index ba0d335..712d872 100644 --- a/src/ValueObjects/Pid.php +++ b/src/ValueObjects/Pid.php @@ -28,16 +28,23 @@ class Pid { */ private $pid; + /** + * @var string + */ + private $childName = 'Unknown'; + /** * Pid constructor. * @param int $pid + * @param string $childName * @throws NotNumericValueException */ - public function __construct($pid) { + public function __construct($pid, $childName = 'Unknown') { if (!is_numeric($pid)) { throw new NotNumericValueException(sprintf('Given values %s is not numeric!', $pid)); } $this->pid = $pid; + $this->childName = $childName; } /** @@ -46,4 +53,12 @@ public function __construct($pid) { public function getPid() { return $this->pid; } -} \ No newline at end of file + + /** + * @return string + */ + public function getChildName() { + return $this->childName; + } + +} From d58bbb9a0e37b303abb2b659c53a3892f7e9eb5b Mon Sep 17 00:00:00 2001 From: Daniel Ziegler Date: Thu, 6 Sep 2018 20:52:19 +0200 Subject: [PATCH 3/4] Restart dead child processes in case of an unexpected error --- src/ChildFactory.php | 178 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 src/ChildFactory.php diff --git a/src/ChildFactory.php b/src/ChildFactory.php new file mode 100644 index 0000000..cdc2f8c --- /dev/null +++ b/src/ChildFactory.php @@ -0,0 +1,178 @@ +. + */ + +namespace Statusengine; + + +use Statusengine\ValueObjects\Pid; + +class ChildFactory { + + /** + * @var Config + */ + private $Config; + + /** + * @var Syslog + */ + private $Syslog; + + /** + * @var Pid + */ + private $ParentPid; + + public function __construct(Config $Config, Syslog $Syslog, Pid $ParentPid) { + $this->Config = $Config; + $this->Syslog = $Syslog; + $this->ParentPid = $ParentPid; + } + + /** + * @return Pid + */ + public function forkHoststatusChild() { + $this->Syslog->info('Fork new host status worker'); + $HoststatusChild = new HoststatusChild( + $this->Config, + $this->ParentPid, + $this->Syslog + ); + $Pid = $HoststatusChild->fork(); + return $Pid; + } + + /** + * @return Pid + */ + public function forkServicestatusChild() { + $this->Syslog->info('Fork new service status worker'); + $ServicestatusChild = new ServicestatusChild( + $this->Config, + $this->ParentPid, + $this->Syslog + ); + $Pid = $ServicestatusChild->fork(); + return $Pid; + } + + /** + * @return Pid + */ + public function forkLogentryChild() { + $this->Syslog->info('Fork new log entry worker'); + $LogentryChild = new LogentryChild( + $this->Config, + $this->ParentPid, + $this->Syslog + ); + $Pid = $LogentryChild->fork(); + return $Pid; + } + + /** + * @return Pid + */ + public function forkStatechangeChild() { + $this->Syslog->info('Fork new state change worker'); + $StatechangeChild = new StatechangeChild( + $this->Config, + $this->ParentPid, + $this->Syslog + ); + $Pid = $StatechangeChild->fork(); + return $Pid; + } + + /** + * @return Pid + */ + public function forkHostcheckChild() { + $this->Syslog->info('Fork new host check worker'); + $HostcheckChild = new HostcheckChild( + $this->Config, + $this->ParentPid, + $this->Syslog + ); + $Pid = $HostcheckChild->fork(); + return $Pid; + } + + /** + * @return Pid + */ + public function forkServicecheckChild() { + $this->Syslog->info('Fork new service check worker'); + $ServicecheckChild = new ServicecheckChild( + $this->Config, + $this->ParentPid, + $this->Syslog + ); + $Pid = $ServicecheckChild->fork(); + return $Pid; + } + + /** + * @return Pid + */ + public function forkMiscChild() { + $this->Syslog->info('Fork new misc worker'); + $MiscChild = new MiscChild( + $this->Config, + $this->ParentPid, + $this->Syslog + ); + $Pid = $MiscChild->fork(); + return $Pid; + } + + /** + * @return Pid + */ + public function forkPerfdataChild() { + $this->Syslog->info('Fork new performance data worker'); + $PerfdataChild = new PerfdataChild( + $this->Config, + $this->ParentPid, + $this->Syslog + ); + $Pid = $PerfdataChild->fork(); + return $Pid; + } + + /** + * @param string $childName + * @return bool + */ + public function canChildBeReborn($childName) { + $methodName = 'fork' . $childName; + return method_exists($this, $methodName); + } + + /** + * @param $childName + * @return Pid + */ + public function respawn($childName) { + $methodName = 'fork' . $childName; + return call_user_func([$this, $methodName]); + } + +} From ae11fb1647155e46efe6a3eed994c29e10585851 Mon Sep 17 00:00:00 2001 From: Daniel Ziegler Date: Thu, 6 Sep 2018 20:52:38 +0200 Subject: [PATCH 4/4] Set Version to 3.4.0 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 15a2799..1809198 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.3.0 +3.4.0