From 705127a95e2665d53329617fee7aa92a89723cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Mo=C3=9Fhammer?= Date: Mon, 15 Jul 2013 13:21:48 +0200 Subject: [PATCH] Make sql-queries more standard compliant The connection now uses ONLY_FULL_GROUP_BY when connecting and also forces ANSI SQL, this is reflected in the query codebase. Also the comment count fields are added in this commit. refs #4179 --- library/Icinga/Data/Db/Connection.php | 3 +- .../controllers/ListController.php | 17 ++--- .../controllers/ShowController.php | 2 +- .../views/scripts/list/hosts.phtml | 67 ++++++++++--------- .../Monitoring/Backend/AbstractBackend.php | 2 + .../Backend/Ido/Query/StatusQuery.php | 48 +++++++++---- 6 files changed, 81 insertions(+), 58 deletions(-) diff --git a/library/Icinga/Data/Db/Connection.php b/library/Icinga/Data/Db/Connection.php index 1c8f9a226..c01721fc6 100644 --- a/library/Icinga/Data/Db/Connection.php +++ b/library/Icinga/Data/Db/Connection.php @@ -57,7 +57,8 @@ class Connection implements DatasourceInterface $adapter = 'Pdo_Mysql'; $drv_options[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET SESSION SQL_MODE='STRICT_ALL_TABLES,NO_ZERO_IN_DATE," - . "NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION';"; + ."NO_ZERO_IN_DATE,ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY," + . "NO_ENGINE_SUBSTITUTION';"; // Not using ONLY_FULL_GROUP_BY as of performance impact // TODO: NO_ZERO_IN_DATE as been added with 5.1.11. Is it // ignored by other versions? diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 500660967..75f4b9ae3 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -37,8 +37,10 @@ class Monitoring_ListController extends ModuleActionController 'host_handled', 'host_last_state_change', 'host_notifications_enabled', - 'host_problems' - // 'host_comment_count' + 'host_problems', + 'host_action_url', + 'host_notes_url', + 'host_last_comment' ) ); @@ -54,20 +56,11 @@ class Monitoring_ListController extends ModuleActionController } } - $this->view->hosts->getQuery()->group('host_id'); + //$this->view->hosts->getQuery()->group('host_id'); if ($this->_getParam('dump') === 'sql') { echo '
' . htmlspecialchars(wordwrap($this->view->hosts->getQuery()->dump())) . '
'; exit; } - // TODO: Get rid of "preserve" - $preserve = array(); - if ($this->_getParam('sort')) { - $preserve['sort'] = $this->view->sort = $this->_getParam('sort'); - } - if ($this->_getParam('backend')) { - $preserve['backend'] = $this->_getParam('backend'); - } - $this->view->preserve = $preserve; if ($this->_getParam('view') === 'compact') { $this->_helper->viewRenderer('hosts_compact'); diff --git a/modules/monitoring/application/controllers/ShowController.php b/modules/monitoring/application/controllers/ShowController.php index f50cc8acf..b24f5f876 100644 --- a/modules/monitoring/application/controllers/ShowController.php +++ b/modules/monitoring/application/controllers/ShowController.php @@ -7,7 +7,7 @@ use Icinga\Web\ModuleActionController; use Icinga\Web\Hook; use Icinga\Monitoring\Object\Host; use Icinga\Monitoring\Object\Service; - +use Icinga\Application\Benchmark; /** * Class Monitoring_ShowController * diff --git a/modules/monitoring/application/views/scripts/list/hosts.phtml b/modules/monitoring/application/views/scripts/list/hosts.phtml index 8695a2fe7..acac94638 100644 --- a/modules/monitoring/application/views/scripts/list/hosts.phtml +++ b/modules/monitoring/application/views/scripts/list/hosts.phtml @@ -6,11 +6,9 @@ $viewHelper = $this->getHelper('MonitoringState'); $trimArea = $this->getHelper('Trim'); ?>
Sort by formSelect( 'sort', @@ -51,59 +49,59 @@ $trimArea = $this->getHelper('Trim');
start(); ?> host_handled): ?> - + host_acknowledged && !$host->host_in_downtime): ?> - + host_is_flapping): ?> - + host_notifications_enabled): ?> - + host_in_downtime): ?> - + end(); ?>
-
- qlink( - "".ucfirst($viewHelper->monitoringState($host, 'host'))."". - '
since '. - $this->timeSince($host->host_last_state_change), - 'monitoring/show/history', array( - 'host' => $host->host_name - ), - array('quote' => false) - );?> - host_state_type === 0): ?> - - - - -
+
+ qlink( + "".ucfirst($viewHelper->monitoringState($host, 'host'))."". + '
since '. + $this->timeSince($host->host_last_state_change), + 'monitoring/show/history', array( + 'host' => $host->host_name + ), + array('quote' => false) + );?> + host_state_type == 0): ?> + + + + +
- host_comment_count > 0): ?> - -     - + host_last_comment !== null): ?> + + + qlink( "".$host->host_name."
". - "".$host->host_address."", + "".$host->host_address."", 'monitoring/show/host', array( 'host' => $host->host_name ), array( @@ -111,6 +109,15 @@ $trimArea = $this->getHelper('Trim'); 'quote' => false ) ); ?> + + + host_action_url != ""): ?> + Action + + + host_notes_url != ""): ?> + Notes + escape(substr(strip_tags($host->host_output), 0, 10000)); ?> diff --git a/modules/monitoring/library/Monitoring/Backend/AbstractBackend.php b/modules/monitoring/library/Monitoring/Backend/AbstractBackend.php index 1661425c8..4d95593a1 100644 --- a/modules/monitoring/library/Monitoring/Backend/AbstractBackend.php +++ b/modules/monitoring/library/Monitoring/Backend/AbstractBackend.php @@ -156,9 +156,11 @@ class AbstractBackend implements DatasourceInterface ); } + $select = $this->select() ->from('status', $fields) ->where('host_name', $host); + return $select->fetchRow(); } diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/StatusQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/StatusQuery.php index 1f6ede2bd..332484d45 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/StatusQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/StatusQuery.php @@ -8,13 +8,15 @@ class StatusQuery extends AbstractQuery protected $columnMap = array( 'hosts' => array( - 'host' => 'ho.name1 COLLATE latin1_general_ci', - 'host_name' => 'ho.name1 COLLATE latin1_general_ci', - 'host_display_name' => 'h.display_name', - 'host_alias' => 'h.alias', - 'host_address' => 'h.address', - 'host_ipv4' => 'INET_ATON(h.address)', - 'host_icon_image' => 'h.icon_image', + 'host' => 'ho.name1 COLLATE latin1_general_ci', + 'host_name' => 'ho.name1 COLLATE latin1_general_ci', + 'host_display_name' => 'h.display_name', + 'host_alias' => 'h.alias', + 'host_address' => 'h.address', + 'host_ipv4' => 'INET_ATON(h.address)', + 'host_icon_image' => 'h.icon_image', + 'host_action_url' => 'h.action_url', + 'host_notes_url' => 'h.notes_url' ), 'hoststatus' => array( 'host_state' => 'CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 99 ELSE hs.current_state END', @@ -30,7 +32,7 @@ class StatusQuery extends AbstractQuery 'host_check_command' => 'hs.check_command', 'host_current_check_attempt' => 'hs.current_check_attempt', 'host_max_check_attempts' => 'hs.max_check_attempts', - 'host_attempt' => 'CONCAT(hs.current_check_attempt, "/", hs.max_check_attempts)', + 'host_attempt' => 'CONCAT(hs.current_check_attempt, \'/\', hs.max_check_attempts)', 'host_last_check' => 'hs.last_check', 'host_next_check' => 'hs.next_check', 'host_check_type' => 'hs.check_type', @@ -68,7 +70,7 @@ class StatusQuery extends AbstractQuery 'host_status_update_time' => 'hs.status_update_time', 'host_problems' => 'CASE WHEN hs.current_state = 0 THEN 0 ELSE 1 END', 'host_severity' => 'CASE WHEN hs.current_state = 0 - THEN + THEN CASE WHEN hs.has_been_checked = 0 OR hs.has_been_checked IS NULL THEN 16 ELSE 0 @@ -139,11 +141,17 @@ class StatusQuery extends AbstractQuery 'service_last_time_critical' => 'ss.last_time_critical', 'service_last_time_unknown' => 'ss.last_time_unknown', ), + 'lasthostcomment' => array( + 'host_last_comment' => 'hlc.comment_id' + ), + 'lastservicecomment' => array( + 'service_last_comment' => 'slc.comment_id' + ), 'status' => array( 'problems' => 'CASE WHEN ss.current_state = 0 THEN 0 ELSE 1 END', 'handled' => 'CASE WHEN ss.problem_has_been_acknowledged = 1 OR ss.scheduled_downtime_depth > 0 THEN 1 ELSE 0 END', 'severity' => 'CASE WHEN ss.current_state = 0 - THEN + THEN CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 16 ELSE 0 @@ -160,7 +168,7 @@ class StatusQuery extends AbstractQuery ELSE CASE WHEN ss.has_been_checked = 0 OR ss.has_been_checked IS NULL THEN 16 WHEN ss.current_state = 1 THEN 32 - WHEN ss.current_state = 2 THEN 128 + WHEN ss.current_state = 2 THEN 128 WHEN ss.current_state = 3 THEN 64 ELSE 256 END @@ -231,11 +239,11 @@ class StatusQuery extends AbstractQuery array() )->join( array('so' => $this->prefix . 'objects'), - "so.$this->object_id = s.service_object_id AND so.is_active = 1", + 'so.'.$this->object_id.' = s.service_object_id AND so.is_active = 1', array() )->joinLeft( array('ss' => $this->prefix . 'servicestatus'), - "so.$this->object_id = ss.service_object_id", + 'so.'.$this->object_id.' = ss.service_object_id', array() ); } @@ -258,7 +266,7 @@ class StatusQuery extends AbstractQuery array() )->join( array('hg' => $this->prefix . 'hostgroups'), - "hgm.hostgroup_id = hg.$this->hostgroup_id", + 'hgm.hostgroup_id = hg'.$this->hostgroup_id, array() ); @@ -306,4 +314,16 @@ class StatusQuery extends AbstractQuery return $this; } + + protected function joinLasthostcomment() + { + $this->baseQuery->joinleft( + array ('hlc' => new \Zend_Db_Expr( + '(SELECT MAX(c.comment_id) as comment_id, c.object_id '. + 'FROM icinga_comments c GROUP BY c.object_id)') + ), + 'hlc.object_id = hs.host_object_id', + array() + ); + } }