From efd24df10d6dca9a807ffe1981ac7a0ee98b7686 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Wed, 11 Oct 2017 17:09:33 +0200 Subject: [PATCH] TLVHostgroupNode: Set downtime_handled based on service_in_notification_period refs #2 --- .../Monitoring/HostgroupQuery.php | 42 ++++++++++++++---- .../Monitoring/HostgroupsummaryQuery.php | 23 ++++------ .../Toplevelview/Monitoring/Servicestatus.php | 38 ++++++++++++++++ .../Monitoring/ServicestatusQuery.php | 43 +++++++++++++++++++ 4 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 library/Toplevelview/Monitoring/Servicestatus.php create mode 100644 library/Toplevelview/Monitoring/ServicestatusQuery.php diff --git a/library/Toplevelview/Monitoring/HostgroupQuery.php b/library/Toplevelview/Monitoring/HostgroupQuery.php index 46270c7..670e32f 100644 --- a/library/Toplevelview/Monitoring/HostgroupQuery.php +++ b/library/Toplevelview/Monitoring/HostgroupQuery.php @@ -7,9 +7,6 @@ /** * Patched version of HostgroupQuery - * - * Changes: - * - add service_notifications_enabled to servicestatus join */ class HostgroupQuery extends IcingaHostgroupQuery { @@ -17,11 +14,11 @@ public function init() { $patchedColumnMap = array( 'servicestatus' => array( - 'service_notifications_enabled' => 'ss.notifications_enabled', - 'service_is_flapping' => 'ss.is_flapping', - 'service_state' => 'CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE CASE WHEN ss.state_type = 1 THEN ss.current_state ELSE ss.last_hard_state END END', - 'service_handled' => 'CASE WHEN (ss.problem_has_been_acknowledged + COALESCE(hs.current_state, 0)) > 0 THEN 1 ELSE 0 END', - 'service_in_downtime' => 'CASE WHEN (ss.scheduled_downtime_depth = 0) THEN 0 ELSE 1 END', + 'service_notifications_enabled' => 'ss.notifications_enabled', + 'service_is_flapping' => 'ss.is_flapping', + 'service_state' => 'CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE CASE WHEN ss.state_type = 1 THEN ss.current_state ELSE ss.last_hard_state END END', + 'service_handled' => 'CASE WHEN (ss.problem_has_been_acknowledged + COALESCE(hs.current_state, 0)) > 0 THEN 1 ELSE 0 END', + 'service_in_downtime' => 'CASE WHEN (ss.scheduled_downtime_depth = 0) THEN 0 ELSE 1 END', ), 'hoststatus' => array( 'host_notifications_enabled' => 'hs.notifications_enabled', @@ -29,7 +26,11 @@ public function init() 'host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE CASE WHEN hs.state_type = 1 THEN hs.current_state ELSE hs.last_hard_state END END', 'host_handled' => 'CASE WHEN hs.problem_has_been_acknowledged > 0 THEN 1 ELSE 0 END', 'host_in_downtime' => 'CASE WHEN (hs.scheduled_downtime_depth = 0) THEN 0 ELSE 1 END', - ) + ), + 'servicenotificationperiod' => array( + 'service_notification_period' => 'ntpo.name1', + 'service_in_notification_period' => 'CASE WHEN ntpr.timeperiod_id IS NOT NULL THEN 1 ELSE 0 END', + ), ); foreach ($patchedColumnMap as $table => $columns) { @@ -40,4 +41,27 @@ public function init() parent::init(); } + + protected function joinServicenotificationperiod() + { + $this->select->joinLeft( + array('ntp' => $this->prefix . 'timeperiods'), + 'ntp.timeperiod_object_id = s.notification_timeperiod_object_id AND ntp.config_type = 1 AND ntp.instance_id = s.instance_id', + array() + ); + $this->select->joinLeft( + array('ntpo' => $this->prefix . 'objects'), + 'ntpo.object_id = s.notification_timeperiod_object_id', + array() + ); + $this->select->joinLeft( + array('ntpr' => $this->prefix . 'timeperiod_timeranges'), + 'ntpr.timeperiod_id = ntp.timeperiod_id + AND ntpr.day = DAYOFWEEK(UTC_DATE()) + AND ntpr.start_sec <= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(UTC_DATE()) + AND ntpr.end_sec >= UNIX_TIMESTAMP() - UNIX_TIMESTAMP(UTC_DATE()) + ', + array() + ); + } } diff --git a/library/Toplevelview/Monitoring/HostgroupsummaryQuery.php b/library/Toplevelview/Monitoring/HostgroupsummaryQuery.php index a7c9c17..9538ff9 100644 --- a/library/Toplevelview/Monitoring/HostgroupsummaryQuery.php +++ b/library/Toplevelview/Monitoring/HostgroupsummaryQuery.php @@ -9,20 +9,13 @@ /** * Patched version of HostgroupsummaryQuery - * - * Changes: - * - join service_notifications_enabled from status tables - * - modify handled logic to include disabled notifications and flapping - * - use patched version of HostgroupQuery for subQueries */ class HostgroupsummaryQuery extends IcingaHostgroupsummaryQuery { public function init() { - // TODO: service_notification_period - - $serviceOutDowntime = 'service_notifications_enabled = 1 AND service_in_downtime = 0'; - $serviceInDowntime = '(service_notifications_enabled = 0 OR service_in_downtime = 1)'; + $serviceOutDowntime = 'service_notifications_enabled = 1 AND service_in_downtime = 0 AND service_in_notification_period = 1'; + $serviceInDowntime = '(service_notifications_enabled = 0 OR service_in_downtime = 1 OR service_in_notification_period = 0)'; $hostOutDowntime = 'host_notifications_enabled = 1 AND host_in_downtime = 0'; $hostInDowntime = '(host_notifications_enabled = 0 OR host_in_downtime = 1)'; $patchServicesHandled = "(service_handled = 1 OR service_is_flapping = 1) AND $serviceOutDowntime"; @@ -83,11 +76,12 @@ protected function joinBaseTables() 'host_state', 'host_is_flapping', 'host_in_downtime', - 'service_handled' => new Zend_Db_Expr('NULL'), - 'service_state' => new Zend_Db_Expr('NULL'), - 'service_notifications_enabled' => new Zend_Db_Expr('NULL'), - 'service_is_flapping' => new Zend_Db_Expr('NULL'), - 'service_in_downtime' => new Zend_Db_Expr('NULL'), + 'service_handled' => new Zend_Db_Expr('NULL'), + 'service_state' => new Zend_Db_Expr('NULL'), + 'service_notifications_enabled' => new Zend_Db_Expr('NULL'), + 'service_in_notification_period' => new Zend_Db_Expr('NULL'), + 'service_is_flapping' => new Zend_Db_Expr('NULL'), + 'service_in_downtime' => new Zend_Db_Expr('NULL'), ) ); $this->subQueries[] = $hosts; @@ -104,6 +98,7 @@ protected function joinBaseTables() 'service_handled', 'service_state', 'service_notifications_enabled', + 'service_in_notification_period', 'service_is_flapping', 'service_in_downtime', ) diff --git a/library/Toplevelview/Monitoring/Servicestatus.php b/library/Toplevelview/Monitoring/Servicestatus.php new file mode 100644 index 0000000..513fab7 --- /dev/null +++ b/library/Toplevelview/Monitoring/Servicestatus.php @@ -0,0 +1,38 @@ + */ + +namespace Icinga\Module\Toplevelview\Monitoring; + +use Icinga\Data\ConnectionInterface; +use Icinga\Module\Monitoring\Backend\MonitoringBackend; +use Icinga\Module\Monitoring\DataView\ServiceStatus as IcingaServiceStatus; + +class ServiceStatus extends IcingaServiceStatus +{ + /** @noinspection PhpMissingParentConstructorInspection */ + /** + * @param ConnectionInterface $connection + * @param array|null $columns + * @noinspection PhpMissingParentConstructorInspection + */ + public function __construct(ConnectionInterface $connection, array $columns = null) + { + /** @var MonitoringBackend $connection */ + $this->connection = $connection; + $this->query = new ServicestatusQuery($connection->getResource(), $columns); + } + + /** + * {@inheritdoc} + */ + public function getColumns() + { + return array_merge( + parent::getColumns(), + array( + //'service_in_notification_period', + 'servicenotificationperiod_name', + ) + ); + } +} diff --git a/library/Toplevelview/Monitoring/ServicestatusQuery.php b/library/Toplevelview/Monitoring/ServicestatusQuery.php new file mode 100644 index 0000000..46270c7 --- /dev/null +++ b/library/Toplevelview/Monitoring/ServicestatusQuery.php @@ -0,0 +1,43 @@ + */ + +namespace Icinga\Module\Toplevelview\Monitoring; + +use Icinga\Module\Monitoring\Backend\Ido\Query\HostgroupQuery as IcingaHostgroupQuery; + +/** + * Patched version of HostgroupQuery + * + * Changes: + * - add service_notifications_enabled to servicestatus join + */ +class HostgroupQuery extends IcingaHostgroupQuery +{ + public function init() + { + $patchedColumnMap = array( + 'servicestatus' => array( + 'service_notifications_enabled' => 'ss.notifications_enabled', + 'service_is_flapping' => 'ss.is_flapping', + 'service_state' => 'CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 99 ELSE CASE WHEN ss.state_type = 1 THEN ss.current_state ELSE ss.last_hard_state END END', + 'service_handled' => 'CASE WHEN (ss.problem_has_been_acknowledged + COALESCE(hs.current_state, 0)) > 0 THEN 1 ELSE 0 END', + 'service_in_downtime' => 'CASE WHEN (ss.scheduled_downtime_depth = 0) THEN 0 ELSE 1 END', + ), + 'hoststatus' => array( + 'host_notifications_enabled' => 'hs.notifications_enabled', + 'host_is_flapping' => 'hs.is_flapping', + 'host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE CASE WHEN hs.state_type = 1 THEN hs.current_state ELSE hs.last_hard_state END END', + 'host_handled' => 'CASE WHEN hs.problem_has_been_acknowledged > 0 THEN 1 ELSE 0 END', + 'host_in_downtime' => 'CASE WHEN (hs.scheduled_downtime_depth = 0) THEN 0 ELSE 1 END', + ) + ); + + foreach ($patchedColumnMap as $table => $columns) { + foreach ($columns as $k => $v) { + $this->columnMap[$table][$k] = $v; + } + } + + parent::init(); + } +}