diff --git a/application/views/helpers/Qlink.php b/application/views/helpers/Qlink.php index 9feb10c5c..5a9ae8316 100755 --- a/application/views/helpers/Qlink.php +++ b/application/views/helpers/Qlink.php @@ -1,19 +1,31 @@ $val) { - if ($key === 'baseUrl' ) { + if ($key === 'baseUrl') { // $baseUrl = filter_var($val, FILTER_SANITIZE_URL) . '/'; $baseUrl = rawurlencode($val) . '/'; continue; @@ -47,17 +59,20 @@ class Zend_View_Helper_Qlink extends Zend_View_Helper_Abstract '%s', $this->getFormattedUrl($urlFormat, $uriParams, $baseUrl), !empty($attributes) ? ' ' . implode(' ', $attributes) : '', - $quote - ? filter_var( + $quote ? filter_var( $htmlContent, FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES - ) - // Alternativ: htmlentities($htmlContent) - : $htmlContent + ) : $htmlContent // Alternative: htmlentities($htmlContent) ); } + /** + * @param $urlFormat + * @param $uriParams + * @param null $baseUrl + * @return string + */ public function getFormattedUrl($urlFormat, $uriParams, $baseUrl = null) { $params = $args = array(); @@ -70,10 +85,9 @@ class Zend_View_Helper_Qlink extends Zend_View_Helper_Abstract } $url = $urlFormat; $url = vsprintf($url, $params); - if (! empty($args)) { + if (!empty($args)) { $url .= '?' . implode('&', $args); } - return is_null($baseUrl) ? $this->view->baseUrl($url) : $baseUrl.$url; + return is_null($baseUrl) ? $this->view->baseUrl($url) : $baseUrl . $url; } } - diff --git a/library/Icinga/Application/Logger.php b/library/Icinga/Application/Logger.php index bf3c01a16..e004fabd5 100755 --- a/library/Icinga/Application/Logger.php +++ b/library/Icinga/Application/Logger.php @@ -1,68 +1,128 @@ overwrite($config); } - public function getWriters() { + /** + * @return array + */ + public function getWriters() + { return $this->writers; } + /** + * @param \Zend_Config $config + */ public function overwrite(\Zend_Config $config) { $this->clearLog(); try { - if ($config->debug && $config->debug->enable == 1) + if ($config->debug && $config->debug->enable == 1) { $this->setupDebugLog($config); + } } catch (\Icinga\Exception\ConfigurationError $e) { - $this->warn("Could not create debug log: {$e->getMessage()}"); + $this->warn("Could not create debug log: {$e->getMessage()}"); } $this->setupLog($config); $this->flushQueue(); } + /** + * @param \Zend_Config $config + */ private function setupDebugLog(\Zend_Config $config) { $type = $config->debug->get("type", self::DEFAULT_LOG_TYPE); $target = $config->debug->get("target", self::DEFAULT_LOG_TARGET); - if ($target == self::DEFAULT_LOG_TARGET) - $type == self::DEFAULT_LOG_TYPE; + if ($target == self::DEFAULT_LOG_TARGET) { + $type == self::DEFAULT_LOG_TYPE; + } $this->addWriter($type, $target, \Zend_Log::DEBUG); } + /** + * @param \Zend_Config $config + */ private function setupLog(\Zend_Config $config) { $type = $config->get("type", self::DEFAULT_LOG_TYPE); $target = $config->get("target", self::DEFAULT_DEBUG_TARGET); - if ($target == self::DEFAULT_DEBUG_TARGET) + if ($target == self::DEFAULT_DEBUG_TARGET) { $type == self::DEFAULT_LOG_TYPE; + } $level = \Zend_Log::WARN; - if ($config->get("verbose", 0) == 1) + if ($config->get("verbose", 0) == 1) { $level = \Zend_Log::INFO; + } $this->addWriter($type, $target, $level); } - private function addWriter($type, $target , $priority) + /** + * @param $type + * @param $target + * @param $priority + * @throws \Icinga\Exception\ConfigurationError + */ + private function addWriter($type, $target, $priority) { $type[0] = strtoupper($type[0]); $writerClass = "\Zend_Log_Writer_" . $type; - if (!class_exists($writerClass)) + if (!class_exists($writerClass)) { throw new \Icinga\Exception\ConfigurationError("Could not create log: Unknown type " . $type); + } $writer = new $writerClass($target); @@ -71,13 +131,20 @@ class Logger $this->writers[] = $writer; } + /** + * flushQueue + */ public function flushQueue() { - foreach(self::$queue as $msgTypePair) { - $this->logger->log($msgTypePair[0],$msgTypePair[1]); + foreach (self::$queue as $msgTypePair) { + $this->logger->log($msgTypePair[0], $msgTypePair[1]); } } + /** + * @param array $argv + * @return string + */ public static function formatMessage(array $argv) { @@ -90,14 +157,17 @@ class Logger $format = json_encode($format); } foreach ($argv as &$arg) { - if (!is_string($arg)) + if (!is_string($arg)) { $arg = json_encode($arg); + } } return @vsprintf($format, $argv); } - + /** + * clearLog + */ public function clearLog() { $this->logger = null; @@ -105,47 +175,79 @@ class Logger $this->logger = new \Zend_Log(); } + /** + * @param \Zend_Config $config + * @return Logger + */ public static function create(\Zend_Config $config) { - if (self::$instance) + if (self::$instance) { return self::$instance->overwrite($config); + } return self::$instance = new Logger($config); } + /** + * debug + */ public static function debug() { - self::log(self::formatMessage(func_get_args()),\Zend_Log::DEBUG); + self::log(self::formatMessage(func_get_args()), \Zend_Log::DEBUG); } - public static function warn() { - self::log(self::formatMessage(func_get_args()),\Zend_Log::WARN); + /** + * + */ + public static function warn() + { + self::log(self::formatMessage(func_get_args()), \Zend_Log::WARN); } - public static function info() { - self::log(self::formatMessage(func_get_args()),\Zend_Log::INFO); + /** + * + */ + public static function info() + { + self::log(self::formatMessage(func_get_args()), \Zend_Log::INFO); } - public static function error() { - self::log(self::formatMessage(func_get_args()),\Zend_Log::ERR); + /** + * + */ + public static function error() + { + self::log(self::formatMessage(func_get_args()), \Zend_Log::ERR); } - public static function fatal() { - self::log(self::formatMessage(func_get_args()),\Zend_Log::EMERG); + /** + * + */ + public static function fatal() + { + self::log(self::formatMessage(func_get_args()), \Zend_Log::EMERG); } - private static function log($msg,$level = \Zend_Log::INFO) { + /** + * @param $msg + * @param int $level + */ + private static function log($msg, $level = \Zend_Log::INFO) + { $logger = self::$instance; - if(!$logger) { - array_push(self::$queue, array($msg,$level)); + if (!$logger) { + array_push(self::$queue, array($msg, $level)); return; } - $logger->logger->log($msg,$level); + $logger->logger->log($msg, $level); } - - public static function reset() { + /** + * + */ + public static function reset() + { self::$queue = array(); self::$instance = null; } diff --git a/library/Icinga/Backend/AbstractBackend.php b/library/Icinga/Backend/AbstractBackend.php index 2c62adbec..972bc2d72 100755 --- a/library/Icinga/Backend/AbstractBackend.php +++ b/library/Icinga/Backend/AbstractBackend.php @@ -1,16 +1,26 @@ config = $config; $this->init(); } @@ -34,7 +45,9 @@ abstract class AbstractBackend * * return void */ - protected function init() {} + protected function init() + { + } /** * Dummy function for fluent code @@ -52,44 +65,58 @@ abstract class AbstractBackend * Leave fields empty to get all available properties * * @param string Virtual table name - * @param array Fields - * return \Icinga\Backend\Ido\Query + * @param array $fields + * @throws \Exception + * @return + * @internal param \Icinga\Backend\Fields $array return \Icinga\Backend\Ido\Query* return \Icinga\Backend\Ido\Query */ public function from($virtual_table, $fields = array()) { $classname = $this->tableToClassName($virtual_table); - if (! class_exists($classname)) { + if (!class_exists($classname)) { throw new \Exception(sprintf('Asking for invalid virtual table %s', $classname)); } $query = new $classname($this, $fields); return $query; } + /** + * @param $virtual_table + * @return bool + */ public function hasView($virtual_table) { return class_exists($this->tableToClassName($virtual_table)); } + /** + * @param $virtual_table + * @return string + */ protected function tableToClassName($virtual_table) { - if (strpos($virtual_table,"/") !== false) { - list($module, $classname) = explode("/",$virtual_table,2); - $class = array_pop(explode("\\",get_class($this))); - return 'Icinga\\'.ucfirst($module).'\\Backend\\'.$class.'\\'.ucfirst($classname).'Query'; + if (strpos($virtual_table, "/") !== false) { + list($module, $classname) = explode("/", $virtual_table, 2); + $class = array_pop(explode("\\", get_class($this))); + return 'Icinga\\' . ucfirst($module) . '\\Backend\\' . $class . '\\' . ucfirst($classname) . 'Query'; } else { return get_class($this) . '\\' . ucfirst($virtual_table . 'Query'); } } + /** + * @return mixed + */ public function getName() { return preg_replace('~^.+\\\(.+?)$~', '$1', get_class($this)); } + /** + * @return mixed + */ public function __toString() { return $this->getName(); } - } - diff --git a/library/Icinga/Backend/Criteria/Order.php b/library/Icinga/Backend/Criteria/Order.php index b65b8ac59..d26392d71 100755 --- a/library/Icinga/Backend/Criteria/Order.php +++ b/library/Icinga/Backend/Criteria/Order.php @@ -1,35 +1,53 @@ unknown->warning->ok, * but also might take acknowledgments and downtimes in account + * + * @var string */ const SERVICE_STATE = "service_state"; /** * Order by the state of host objects. Mostly this is critical->unknown->warning->ok, * but also might take acknowledgments and downtimes in account + * + * @var string */ - const HOST_STATE = "host_state"; + const HOST_STATE = "host_state"; - const HOST_NAME = "host_name"; - const SERVICE_NAME = "service_description"; + /** + * @var string + */ + const HOST_NAME = "host_name"; + + /** + * + * @var string + */ + const SERVICE_NAME = "service_description"; } - diff --git a/library/Icinga/Backend/DataView/AbstractAccessorStrategy.php b/library/Icinga/Backend/DataView/AbstractAccessorStrategy.php index 7fbf56106..058653705 100755 --- a/library/Icinga/Backend/DataView/AbstractAccessorStrategy.php +++ b/library/Icinga/Backend/DataView/AbstractAccessorStrategy.php @@ -1,21 +1,26 @@ service_state, - * because this field is, e.g. under status.current_state in the status.dat view, while IDO uses servicestate->current_state. + * When accessing objects, every storage api returns them with other names. You can't simply say + * $object->service_state, because this field is, e.g. under status.current_state in the status.dat + * view, while IDO uses servicestate->current_state. * - * This view is intended for normalizing these changes, so a request of service_state returns the right field for the backend. - * When implementing it, you have to fill the mappedParameters and/or the handlerParameters array. While mappedParameters - * simply translate logic field names to storage specific ones, handlerParameters determins functions that handle data - * retrieval for the specific fields. + * This view is intended for normalizing these changes, so a request of service_state returns the + * right field for the backend. When implementing it, you have to fill the mappedParameters and/or + * the handlerParameters array. While mappedParameters simply translate logic field names to + * storage specific ones, handlerParameters determins functions that handle data retrieval for + * the specific fields. * + * @package Icinga\Backend\DataView */ class ObjectRemappingView implements AbstractAccessorStrategy { @@ -46,15 +54,17 @@ class ObjectRemappingView implements AbstractAccessorStrategy public function get(&$item, $field) { - if (isset($item->$field)) + if (isset($item->$field)) { return $item->$field; + } if (isset($this->mappedParameters[$field])) { - $mapped = explode(".",$this->mappedParameters[$field]); + $mapped = explode(".", $this->mappedParameters[$field]); $res = $item; - foreach($mapped as $map) { - if(!isset($res->$map)) + foreach ($mapped as $map) { + if (!isset($res->$map)) { return ""; + } $res = $res->$map; } return $res; @@ -76,8 +86,9 @@ class ObjectRemappingView implements AbstractAccessorStrategy */ public function getNormalizedFieldName($field) { - if(isset($this->mappedParameters[$field])) + if (isset($this->mappedParameters[$field])) { return $this->mappedParameters[$field]; + } return $field; } @@ -96,5 +107,4 @@ class ObjectRemappingView implements AbstractAccessorStrategy || isset($this->handlerParameters[$field]) ); } - } diff --git a/library/Icinga/Backend/Query.php b/library/Icinga/Backend/Query.php index 016aa00ce..2062e1ff8 100755 --- a/library/Icinga/Backend/Query.php +++ b/library/Icinga/Backend/Query.php @@ -1,23 +1,85 @@ backend = $backend; if (empty($columns) || $columns === '*') { @@ -28,6 +90,10 @@ abstract class Query $this->init(); } + /** + * @param array $filters + * @return $this + */ public function applyFilters($filters = array()) { foreach ($filters as $key => $val) { @@ -36,13 +102,23 @@ abstract class Query return $this; } + /** + * @return mixed + */ abstract protected function init(); - protected function finalize() {} + /* + * + */ + protected function finalize() + { + } /** * Return a pagination adapter for the current query * + * @param null $limit + * @param null $page * @return \Zend_Paginator */ public function paginate($limit = null, $page = null) @@ -56,11 +132,10 @@ abstract class Query $limit = $request->getParam('limit', 20); } $paginator = new \Zend_Paginator( - new \Icinga\Web\Paginator\Adapter\QueryAdapter($this) + new QueryAdapter($this) ); $paginator->setItemCountPerPage($limit); $paginator->setCurrentPageNumber($page); return $paginator; } } - diff --git a/library/Icinga/Backend/Statusdat.php b/library/Icinga/Backend/Statusdat.php index e0f16fea5..b7f7da623 100755 --- a/library/Icinga/Backend/Statusdat.php +++ b/library/Icinga/Backend/Statusdat.php @@ -1,37 +1,60 @@ reader = new StatusdatProtocol\Reader($this->config); } + /** + * @return null + */ public function getReader() { return $this->reader; } + /** + * @param array $filter + * @param array $flags + * @return mixed + */ public function listServices($filter = array(), $flags = array()) { $query = $this->select()->from("servicelist"); return $query->fetchAll(); } - + /** + * @param $host + * @return MonitoringObjectList|null + */ public function fetchHost($host) { $objs = & $this->reader->getObjects(); - if (!isset($objs["host"][$host])) + if (!isset($objs["host"][$host])) { return null; + } $result = array($objs["host"][$host]); return new MonitoringObjectList( $result, @@ -39,13 +62,19 @@ class Statusdat extends AbstractBackend ); } + /** + * @param $host + * @param $service + * @return MonitoringObjectList|null + */ public function fetchService($host, $service) { $idxName = $host . ";" . $service; $objs = & $this->reader->getObjects(); - if (!isset($objs["service"][$idxName])) + if (!isset($objs["service"][$idxName])) { return null; + } $result = array($objs["service"][$idxName]); return new MonitoringObjectList( $result, @@ -53,6 +82,4 @@ class Statusdat extends AbstractBackend ); } - - } diff --git a/library/Icinga/Backend/Statusdat/DataView/StatusdatHostView.php b/library/Icinga/Backend/Statusdat/DataView/StatusdatHostView.php index aba27469b..c08161792 100644 --- a/library/Icinga/Backend/Statusdat/DataView/StatusdatHostView.php +++ b/library/Icinga/Backend/Statusdat/DataView/StatusdatHostView.php @@ -1,16 +1,34 @@ "getHost", "downtimes_with_info" => "getDowntimes", "comments_with_info" => "getComments" ); + /** + * @var array + */ protected $mappedParameters = array( "host_address" => "host_name", "host_name" => "host_name", @@ -27,22 +45,31 @@ class StatusdatHostView extends \Icinga\Backend\DataView\ObjectRemappingView "host_check_latency" => "status.check_latency", "host_check_execution_time" => "status.check_execution_time", "active_checks_enabled" => "status.active_checks_enabled", - "acknowledged" => "status.problem_has_been_acknowledged", "host_acknowledged" => "status.problem_has_been_acknowledged", // "state" => "current_state" ); - - public function getHost(&$item) { - if(!isset($this->state["host"][$item->host_name])) + /** + * @param $item + * @return null + */ + public function getHost(&$item) + { + if (!isset($this->state["host"][$item->host_name])) { return null; - if(!isset($this->state["host"][$item->host_name])) + } + if (!isset($this->state["host"][$item->host_name])) { return null; + } return $this->state["host"][$item->host_name]; } - public function __construct(IReader $reader) { - $this->state = &$reader->getState(); + /** + * @param IReader $reader + */ + public function __construct(IReader $reader) + { + $this->state = & $reader->getState(); } } diff --git a/library/Icinga/Backend/Statusdat/DataView/StatusdatServiceView.php b/library/Icinga/Backend/Statusdat/DataView/StatusdatServiceView.php index 28de846a9..fd55d21a5 100755 --- a/library/Icinga/Backend/Statusdat/DataView/StatusdatServiceView.php +++ b/library/Icinga/Backend/Statusdat/DataView/StatusdatServiceView.php @@ -1,16 +1,30 @@ "getHost", "downtimes_with_info" => "getDowntimes" ); + /** + * @var array + */ protected $mappedParameters = array( "host_address" => "parenthost.address", "host_name" => "host_name", @@ -26,35 +40,57 @@ class StatusdatServiceView extends \Icinga\Backend\DataView\ObjectRemappingView "service_next_check" => "status.next_check", "service_check_latency" => "status.check_latency", "service_check_execution_time" => "status.check_execution_time", - "service_acknowledged" => "status.problem_has_been_acknowledged", - "service_comments" => "comment" + "service_acknowledged" => "status.problem_has_been_acknowledged", + "service_comments" => "comment" ); + /** + * @param \Icinga\Backend\DataView\The $item + * @param \Icinga\Backend\DataView\The $field + * @return \Icinga\Backend\DataView\The|string + */ public function get(&$item, $field) { - if(!isset($item->parenthost) && isset($this->state["host"])) + if (!isset($item->parenthost) && isset($this->state["host"])) { $item->parenthost = $this->state["host"]; + } - return parent::get($item,$field); + return parent::get($item, $field); } + + /** + * @param \Icinga\Backend\DataView\The $item + * @param \Icinga\Backend\DataView\The $field + * @return bool + */ public function exists(&$item, $field) { - if(!isset($item->parenthost)) + if (!isset($item->parenthost)) { $item->parenthost = $this->state["host"]; + } - return parent::exists($item,$field); + return parent::exists($item, $field); } + /** + * @param $item + * @return null + */ public function getHost(&$item) { - if (!isset($this->state["host"][$item->host_name])) + if (!isset($this->state["host"][$item->host_name])) { return null; - if (!isset($this->state["host"][$item->host_name])) + } + if (!isset($this->state["host"][$item->host_name])) { return null; + } return $this->state["host"][$item->host_name]; } + /** + * @param IReader $reader + */ public function __construct(IReader $reader) { $this->state = & $reader->getState(); diff --git a/library/Icinga/Backend/Statusdat/GroupsummaryQuery.php b/library/Icinga/Backend/Statusdat/GroupsummaryQuery.php index 89765d343..335677f3d 100755 --- a/library/Icinga/Backend/Statusdat/GroupsummaryQuery.php +++ b/library/Icinga/Backend/Statusdat/GroupsummaryQuery.php @@ -1,25 +1,45 @@ 'SUM(CASE WHEN state = 0 THEN 1 ELSE 0 END)', - 'critical' => 'SUM(CASE WHEN state = 2 AND downtime = 0 AND ack = 0 THEN 1 ELSE 0 END)', - 'critical_dt' => 'SUM(CASE WHEN state = 2 AND downtime = 1 THEN 1 ELSE 0 END)', + 'ok' => 'SUM(CASE WHEN state = 0 THEN 1 ELSE 0 END)', + 'critical' => 'SUM(CASE WHEN state = 2 AND downtime = 0 AND ack = 0 THEN 1 ELSE 0 END)', + 'critical_dt' => 'SUM(CASE WHEN state = 2 AND downtime = 1 THEN 1 ELSE 0 END)', 'critical_ack' => 'SUM(CASE WHEN state = 2 AND downtime = 0 AND ack = 1 THEN 1 ELSE 0 END)', - 'unknown' => 'SUM(CASE WHEN state = 3 AND downtime = 0 AND ack = 0 THEN 1 ELSE 0 END)', - 'unknown_dt' => 'SUM(CASE WHEN state = 3 AND downtime = 1 THEN 1 ELSE 0 END)', - 'unknown_ack' => 'SUM(CASE WHEN state = 3 AND downtime = 0 AND ack = 1 THEN 1 ELSE 0 END)', - 'warning' => 'SUM(CASE WHEN state = 1 AND downtime = 0 AND ack = 0 THEN 1 ELSE 0 END)', - 'warning_dt' => 'SUM(CASE WHEN state = 1 AND downtime = 1 THEN 1 ELSE 0 END)', - 'warning_ack' => 'SUM(CASE WHEN state = 1 AND downtime = 0 AND ack = 1 THEN 1 ELSE 0 END)', + 'unknown' => 'SUM(CASE WHEN state = 3 AND downtime = 0 AND ack = 0 THEN 1 ELSE 0 END)', + 'unknown_dt' => 'SUM(CASE WHEN state = 3 AND downtime = 1 THEN 1 ELSE 0 END)', + 'unknown_ack' => 'SUM(CASE WHEN state = 3 AND downtime = 0 AND ack = 1 THEN 1 ELSE 0 END)', + 'warning' => 'SUM(CASE WHEN state = 1 AND downtime = 0 AND ack = 0 THEN 1 ELSE 0 END)', + 'warning_dt' => 'SUM(CASE WHEN state = 1 AND downtime = 1 THEN 1 ELSE 0 END)', + 'warning_ack' => 'SUM(CASE WHEN state = 1 AND downtime = 0 AND ack = 1 THEN 1 ELSE 0 END)', ); + /** + * @var array + */ protected $order_columns = array( 'state' => array( 'ASC' => array( @@ -50,54 +70,66 @@ abstract class GroupsummaryQuery extends Query ) ); + /** + * @param $obj + * @return string + */ private function getStateType(&$obj) { - - if ($obj->status->current_state == 0) + if ($obj->status->current_state == 0) { return "ok"; + } $typeBase = ""; if ($obj->status->current_state == 1) { $typeBase = 'warning'; - } else if ($obj->status->current_state == 2) { - $typeBase = 'critical'; - } else if ($obj->status->current_state == 3) { - $typeBase = 'unknown'; + } else { + if ($obj->status->current_state == 2) { + $typeBase = 'critical'; + } else { + if ($obj->status->current_state == 3) { + $typeBase = 'unknown'; + } + } } if ($obj->status->problem_has_been_acknowledged) { return $typeBase . "_ack"; - } else if (isset($obj->status->downtime)) { - return $typeBase . "_dt"; + } else { + if (isset($obj->status->downtime)) { + return $typeBase . "_dt"; + } } return $typeBase; } + /** + * @param $indices + * @return array + */ public function groupByProblemType(&$indices) { - - - $typename = $this->groupType."_name"; + $typename = $this->groupType . "_name"; $result = array(); - foreach ($indices as $type=>$subIndices) { + foreach ($indices as $type => $subIndices) { foreach ($subIndices as $objName) { - $obj = &$this->reader->getObjectByName($type,$objName); + $obj = & $this->reader->getObjectByName($type, $objName); $statetype = $this->getStateType($obj); - foreach($obj->group as $group) { - if(!isset($result[$group])) { - $result[$group] = (object) array( - $typename => $group, - 'ok' => 0, - 'critical' => 0, - 'critical_dt' => 0, + foreach ($obj->group as $group) { + if (!isset($result[$group])) { + $result[$group] = (object)array( + $typename => $group, + 'ok' => 0, + 'critical' => 0, + 'critical_dt' => 0, 'critical_ack' => 0, - 'unknown' => 0, - 'unknown_dt' => 0, - 'unknown_ack' => 0, - 'warning' => 0, - 'warning_dt' => 0, - 'warning_ack' => 0 + 'unknown' => 0, + 'unknown_dt' => 0, + 'unknown_ack' => 0, + 'warning' => 0, + 'warning_dt' => 0, + 'warning_ack' => 0 ); } $result[$group]->$statetype++; @@ -110,21 +142,30 @@ abstract class GroupsummaryQuery extends Query /** * @var \Icinga\Protocol\Statusdat\Query + * @return mixed|void */ - - - public function init() { + public function init() + { $this->reader = $this->backend->getReader(); - $this->query = $this->reader->select()->from($this->base,array())->groupByFunction("groupByProblemType",$this)->where("COUNT{group} > 0"); + $this->query = $this->reader->select()->from($this->base, array())->groupByFunction( + "groupByProblemType", + $this + )->where("COUNT{group} > 0"); } - + /** + * @param The $column + * @param null $value + * @return $this|Query + */ public function where($column, $value = null) { if ($column === 'problems') { if ($value === 'true') { - //$this->where("COUNT{downtime} == 0 AND status.problem_has_been_acknowledged == 0 AND status.current_state > 0"); + //$this->where( + // "COUNT{downtime} == 0 AND status.problem_has_been_acknowledged == 0 AND status.current_state > 0" + // ); } } elseif ($column === 'search') { if ($value) { @@ -136,4 +177,3 @@ abstract class GroupsummaryQuery extends Query return $this; } } - diff --git a/library/Icinga/Backend/Statusdat/HostgroupsummaryQuery.php b/library/Icinga/Backend/Statusdat/HostgroupsummaryQuery.php index 14f160eca..219011a9a 100755 --- a/library/Icinga/Backend/Statusdat/HostgroupsummaryQuery.php +++ b/library/Icinga/Backend/Statusdat/HostgroupsummaryQuery.php @@ -1,10 +1,22 @@ reader = $this->backend->getReader(); - $this->query = $this->reader->select()->from("hosts",array()); + $this->query = $this->reader->select()->from("hosts", array()); } - - - } diff --git a/library/Icinga/Backend/Statusdat/Query.php b/library/Icinga/Backend/Statusdat/Query.php index 32bda66d7..00b9ae89f 100644 --- a/library/Icinga/Backend/Statusdat/Query.php +++ b/library/Icinga/Backend/Statusdat/Query.php @@ -1,28 +1,41 @@ "status.current_state", - \Icinga\Backend\Criteria\Order::STATE_CHANGE => "status.last_state_change", - \Icinga\Backend\Criteria\Order::HOST_STATE => "status.current_state", - \Icinga\Backend\Criteria\Order::HOST_NAME => "host_name", - \Icinga\Backend\Criteria\Order::SERVICE_NAME => "service_description" + Order::SERVICE_STATE => "status.current_state", + Order::STATE_CHANGE => "status.last_state_change", + Order::HOST_STATE => "status.current_state", + Order::HOST_NAME => "host_name", + Order::SERVICE_NAME => "service_description" ); /** @@ -40,10 +53,11 @@ abstract class Query extends \Icinga\Backend\Query foreach ($filters as $filter => $value) { $filter[0] = strtoupper($filter[0]); $filterMethod = "apply" . $filter . "Filter"; - if (method_exists($this, $filterMethod)) + if (method_exists($this, $filterMethod)) { $this->$filterMethod($filter, $value); - else + } else { $this->where($filter, $value); + } } return $this; } @@ -117,27 +131,45 @@ abstract class Query extends \Icinga\Backend\Query $this->query->where("(status.problem_has_been_acknowledged = ? )", $val); } + /** + * @param $type + * @param $value + */ public function applyHostnameFilter($type, $value) { - if (!is_array($value)) + if (!is_array($value)) { $value = array($value); + } $this->query->where("host_name LIKE ?", $value); } + /** + * @param $type + * @param $value + */ public function applyStateFilter($type, $value) { $this->query->where("status.current_state = $value"); } + /** + * @param $type + * @param $value + */ public function applyHoststateFilter($type, $value) { $this->query->where("host.status.current_state = $value"); } + /** + * @param $type + * @param $value + */ public function applyServiceDescriptionFilter($type, $value) { - if (!is_array($value)) + if (!is_array($value)) { $value = array($value); + } $this->query->where("service_description LIKE ?", $value); } @@ -163,52 +195,70 @@ abstract class Query extends \Icinga\Backend\Query public function order($column = '', $dir = null) { - if ($column) + if ($column) { $this->query->order($this->orderColumns[$column], strtolower($dir)); + } return $this; } /** * Applies a filter on this query by calling the statusdat where() function * - * @param $column The (statusdat!) column to filter in "field operator ?" format. (@example status.current_state > ?) + * @param $column The (statusdat!) column to filter in "field operator ?" + * format. (@example status.current_state > ?) * @param mixed $value The value to filter for * @return Query Returns this query,for fluent interface */ public function where($column, $value = null) { - if (!is_array($value)) + if (!is_array($value)) { $value = array($value); + } $this->query->where($column, $value); return $this; } + /** + * @return MList|mixed|null + */ public function fetchAll() { $view = $this->view; - if (!$this->cursor) + if (!$this->cursor) { $this->cursor = new MList($this->query->getResult(), new $view($this->reader)); + } return $this->cursor; } + /** + * @return mixed + */ public function fetchRow() { return next($this->fetchAll()); } + /** + * @return mixed|void + */ public function fetchPairs() { } + /** + * @return mixed + */ public function fetchOne() { return next($this->fetchAll()); } + /** + * @return int|mixed + */ public function count() { return count($this->query->getResult()); } - -} \ No newline at end of file +} diff --git a/library/Icinga/Backend/Statusdat/ServicegroupsummaryQuery.php b/library/Icinga/Backend/Statusdat/ServicegroupsummaryQuery.php index f59ed8454..4d01b560d 100644 --- a/library/Icinga/Backend/Statusdat/ServicegroupsummaryQuery.php +++ b/library/Icinga/Backend/Statusdat/ServicegroupsummaryQuery.php @@ -1,10 +1,22 @@ reader = $this->backend->getReader(); - $this->query = $this->reader->select()->from("services",array()); + $this->query = $this->reader->select()->from("services", array()); } - - - } diff --git a/library/Icinga/Exception/ConfigurationError.php b/library/Icinga/Exception/ConfigurationError.php index b6df04795..988246f5f 100644 --- a/library/Icinga/Exception/ConfigurationError.php +++ b/library/Icinga/Exception/ConfigurationError.php @@ -1,8 +1,13 @@ expireTime = intval($time); } + /** + * @param boolean $bool + */ public function setNotify($bool) { - $this->notify = (bool) $bool; + $this->notify = (bool)$bool; } - public function __construct(Comment $comment, $notify = false, $expire = -1, $sticky=false) + /** + * @param Comment $comment + * @param bool $notify + * @param $expire + * @param bool $sticky + */ + public function __construct(Comment $comment, $notify = false, $expire = -1, $sticky = false) { $this->comment = $comment; $this->setNotify($notify); @@ -29,30 +63,35 @@ class Acknowledgement implements IComment $this->sticky = $sticky; } + /** + * @param $type + * @return string + * @throws Exception\InvalidCommandException + */ public function getFormatString($type) { - $params = ';'.($this->sticky ? '2' : '0').';'.($this->notify ? '1 ': '0').';'.($this->comment->persistent ? '1' : '0'); - $params .= ($this->expireTime > -1 ? ';'.$this->expireTime.';' : ';').$this->comment->author.';'.$this->comment->comment; + $params = ';' + . ($this->sticky ? '2' : '0') + . ';' . ($this->notify ? '1 ' : '0') + . ';' . ($this->comment->persistent ? '1' : '0'); - switch($type) { + $params .= ($this->expireTime > -1 ? ';'. $this->expireTime . ';' : ';') + . $this->comment->author . ';' . $this->comment->comment; + + switch ($type) { case CommandPipe::TYPE_HOST: $typeVar = "HOST"; - $params = ";%s".$params; + $params = ";%s" . $params; break; case CommandPipe::TYPE_SERVICE: $typeVar = "SVC"; - $params = ";%s;%s".$params; + $params = ";%s;%s" . $params; break; default: throw new InvalidCommandException("Acknowledgements can only apply on hosts and services "); } - $base = "ACKNOWLEDGE_{$typeVar}_PROBLEM".($this->expireTime > -1 ? '_EXPIRE' : '' ); - return $base.$params; - - - + $base = "ACKNOWLEDGE_{$typeVar}_PROBLEM" . ($this->expireTime > -1 ? '_EXPIRE' : ''); + return $base . $params; } - - -} \ No newline at end of file +} diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index 34501717a..32a96ed4a 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -1,11 +1,20 @@ path = $config->path; $this->name = $config->name; - if(isset($config->host)) { + if (isset($config->host)) { $this->host = $config->host; } - if(isset($config->port)) { + if (isset($config->port)) { $this->port = $config->port; } - if(isset($config->user)) { + if (isset($config->user)) { $this->user = $config->user; } } public function send($command) { - if(!$this->host) { - IcingaLogger::debug("Attempting to send external icinga command $command to local command file {$this->path}"); + if (!$this->host) { + IcingaLogger::debug( + "Attempting to send external icinga command $command to local command file {$this->path}" + ); $file = @fopen($this->path, $this->fopen_mode); - if (!$file) - throw new \RuntimeException("Could not open icinga pipe at $file : ".print_r(error_get_last(), true)); - fwrite($file,"[".time()."] ".$command.PHP_EOL); - IcingaLogger::debug('Writing ['.time().'] '.$command.PHP_EOL); + if (!$file) { + throw new \RuntimeException("Could not open icinga pipe at $file : " . print_r(error_get_last(), true)); + } + fwrite($file, "[" . time() . "] " . $command . PHP_EOL); + IcingaLogger::debug('Writing [' . time() . '] ' . $command . PHP_EOL); fclose($file); } else { // send over ssh $retCode = 0; $output = array(); - IcingaLogger::debug('Icinga instance is on different host, attempting to send command %s via ssh to %s:%s/%s', $command, $this->host, $this->port, $this->path); - $hostConnector = $this->user ? $this->user."@".$this->host : $this->host; - exec("ssh $hostConnector -p{$this->port} \"echo '[".time()."] ".escapeshellcmd($command)."' > {$this->path}\"", $output, $retCode); - IcingaLogger::debug("$:ssh $hostConnector -p{$this->port} \"echo '[".time()."] ".escapeshellcmd($command)."' > {$this->path}\""); + IcingaLogger::debug( + 'Icinga instance is on different host, attempting to send command %s via ssh to %s:%s/%s', + $command, + $this->host, + $this->port, + $this->path + ); + $hostConnector = $this->user ? $this->user . "@" . $this->host : $this->host; + exec( + "ssh $hostConnector -p{$this->port} \"echo '[" . time() . "] " + . escapeshellcmd( + $command + ) + . "' > {$this->path}\"", + $output, + $retCode + ); + IcingaLogger::debug( + "$:ssh $hostConnector -p{$this->port} \"echo '[" . time() . "] " . escapeshellcmd( + $command + ) . "' > {$this->path}\"" + ); IcingaLogger::debug("Code code %s: %s ", $retCode, $output); - if($retCode != 0) { - throw new \RuntimeException('Could not send command to remote icinga host: '.implode("\n", $output)." (returncode $retCode)"); + if ($retCode != 0) { + throw new \RuntimeException( + 'Could not send command to remote icinga host: ' + . implode( + "\n", + $output + ) + . " (returncode $retCode)" + ); } } } - public function acknowledge($objects,IComment $acknowledgementOrComment) { - if (is_a($acknowledgementOrComment,'Icinga\Protocol\Commandpipe\Comment')) + public function acknowledge($objects, IComment $acknowledgementOrComment) + { + if (is_a($acknowledgementOrComment, 'Icinga\Protocol\Commandpipe\Comment')) { $acknowledgementOrComment = new Acknowledgement($acknowledgementOrComment); + } foreach ($objects as $object) { if (isset($object->service_description)) { $format = $acknowledgementOrComment->getFormatString(self::TYPE_SERVICE); - $this->send(sprintf($format,$object->host_name,$object->service_description)); + $this->send(sprintf($format, $object->host_name, $object->service_description)); } else { $format = $acknowledgementOrComment->getFormatString(self::TYPE_HOST); - $this->send(sprintf($format,$object->host_name)); + $this->send(sprintf($format, $object->host_name)); } } } @@ -94,28 +133,32 @@ class CommandPipe } } - public function scheduleForcedCheck($objects,$time=false,$withChilds=false) { - if (!$time) + public function scheduleForcedCheck($objects, $time = false, $withChilds = false) + { + if (!$time) { $time = time(); + } $base = "SCHEDULE_FORCED_"; foreach ($objects as $object) { if (isset($object->service_description)) { - $this->send($base."SVC_CHECK;$object->host_name;$object->service_description;$time"); + $this->send($base . "SVC_CHECK;$object->host_name;$object->service_description;$time"); } else { - $this->send($base.'HOST_'.($withChilds ? 'SVC_CHECKS' : 'CHECK' ).";$object->host_name;$time"); + $this->send($base . 'HOST_' . ($withChilds ? 'SVC_CHECKS' : 'CHECK') . ";$object->host_name;$time"); } } } - public function scheduleCheck($objects,$time=false,$withChilds=false) { - if (!$time) + public function scheduleCheck($objects, $time = false, $withChilds = false) + { + if (!$time) { $time = time(); + } $base = "SCHEDULE_"; foreach ($objects as $object) { if (isset($object->service_description)) { - $this->send($base."SVC_CHECK;$object->host_name;$object->service_description;$time"); + $this->send($base . "SVC_CHECK;$object->host_name;$object->service_description;$time"); } else { - $this->send($base.'HOST_'.($withChilds ? 'SVC_CHECKS' : 'CHECK' ).";$object->host_name;$time"); + $this->send($base . 'HOST_' . ($withChilds ? 'SVC_CHECKS' : 'CHECK') . ";$object->host_name;$time"); } } } @@ -125,10 +168,10 @@ class CommandPipe foreach ($objects as $object) { if (isset($object->service_description)) { $format = $comment->getFormatString(self::TYPE_SERVICE); - $this->send(sprintf($format,$object->host_name,$object->service_description)); + $this->send(sprintf($format, $object->host_name, $object->service_description)); } else { $format = $comment->getFormatString(self::TYPE_HOST); - $this->send(sprintf($format,$object->host_name)); + $this->send(sprintf($format, $object->host_name)); } } @@ -143,16 +186,17 @@ class CommandPipe } else { $type = "HOST_COMMENT"; } - $this->send("DEL_{$type};".intval($object->comment_id)); + $this->send("DEL_{$type};" . intval($object->comment_id)); } else { if (isset($object->service_description)) { $type = "SERVICE_COMMENT"; } else { $type = "HOST_COMMENT"; } - $cmd = "DEL_ALL_{$type}S;".$object->host_name; - if ($type == "SERVICE_COMMENT") - $cmd .= ";".$object->service_description; + $cmd = "DEL_ALL_{$type}S;" . $object->host_name; + if ($type == "SERVICE_COMMENT") { + $cmd .= ";" . $object->service_description; + } $this->send($cmd); } } @@ -171,8 +215,9 @@ class CommandPipe private function getObjectType($object) { //@TODO: This must be refactored once more commands are supported - if (isset($object->service_description)) + if (isset($object->service_description)) { return self::TYPE_SERVICE; + } return self::TYPE_HOST; } @@ -180,26 +225,31 @@ class CommandPipe { foreach ($objects as $object) { $type = $this->getObjectType($object); - if($type == self::TYPE_SERVICE) - $this->send(sprintf($downtime->getFormatString($type),$object->host_name,$object->service_description)); - else - $this->send(sprintf($downtime->getFormatString($type),$object->host_name)); + if ($type == self::TYPE_SERVICE) { + $this->send( + sprintf($downtime->getFormatString($type), $object->host_name, $object->service_description) + ); + } else { + $this->send(sprintf($downtime->getFormatString($type), $object->host_name)); + } } } - public function removeDowntime($objects,$starttime = 0) + public function removeDowntime($objects, $starttime = 0) { foreach ($objects as $object) { $type = $this->getObjectType($object); if (isset($object->downtime_id)) { - $this->send("DEL_".$type."_DOWNTIME;".$object->downtime_id); + $this->send("DEL_" . $type . "_DOWNTIME;" . $object->downtime_id); continue; } - $cmd = "DEL_DOWNTIME_BY_HOST_NAME;".$object->host_name; - if($type == self::TYPE_SERVICE) - $cmd .= ";".$object->service_description; - if($starttime != 0) - $cmd .= ";".$starttime; + $cmd = "DEL_DOWNTIME_BY_HOST_NAME;" . $object->host_name; + if ($type == self::TYPE_SERVICE) { + $cmd .= ";" . $object->service_description; + } + if ($starttime != 0) { + $cmd .= ";" . $starttime; + } $this->send($cmd); } } @@ -215,7 +265,9 @@ class CommandPipe $type = $this->getObjectType($object); $formatArray = $flags->getFormatString($type); foreach ($formatArray as $format) { - $format .= ";".$object->host_name.($type == self::TYPE_SERVICE ? ";".$object->service_description : ""); + $format .= ";" + . $object->host_name + . ($type == self::TYPE_SERVICE ? ";" . $object->service_description : ""); $this->send($format); } } @@ -223,98 +275,169 @@ class CommandPipe public function enableActiveChecks($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::ACTIVE => PropertyModifier::STATE_ENABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::ACTIVE => PropertyModifier::STATE_ENABLE + ) + ) + ); } public function disableActiveChecks($objects) { - $this->modifyMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::ACTIVE => PropertyModifier::STATE_DISABLE - ))); + $this->modifyMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::ACTIVE => PropertyModifier::STATE_DISABLE + ) + ) + ); } public function enablePassiveChecks($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::PASSIVE => PropertyModifier::STATE_ENABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::PASSIVE => PropertyModifier::STATE_ENABLE + ) + ) + ); } public function disablePassiveChecks($objects) { - $this->modifyMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::PASSIVE => PropertyModifier::STATE_DISABLE - ))); + $this->modifyMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::PASSIVE => PropertyModifier::STATE_DISABLE + ) + ) + ); } public function enableFlappingDetection($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::FLAPPING => PropertyModifier::STATE_ENABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::FLAPPING => PropertyModifier::STATE_ENABLE + ) + ) + ); } public function disableFlappingDetection($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::FLAPPING => PropertyModifier::STATE_DISABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::FLAPPING => PropertyModifier::STATE_DISABLE + ) + ) + ); } public function enableNotifications($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::NOTIFICATIONS => PropertyModifier::STATE_ENABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::NOTIFICATIONS => PropertyModifier::STATE_ENABLE + ) + ) + ); } public function disableNotifications($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::NOTIFICATIONS => PropertyModifier::STATE_DISABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::NOTIFICATIONS => PropertyModifier::STATE_DISABLE + ) + ) + ); } public function enableFreshnessChecks($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::FRESHNESS => PropertyModifier::STATE_ENABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::FRESHNESS => PropertyModifier::STATE_ENABLE + ) + ) + ); } public function disableFreshnessChecks($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::FRESHNESS => PropertyModifier::STATE_DISABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::FRESHNESS => PropertyModifier::STATE_DISABLE + ) + ) + ); } + public function enableEventHandler($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::EVENTHANDLER => PropertyModifier::STATE_ENABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::EVENTHANDLER => PropertyModifier::STATE_ENABLE + ) + ) + ); } public function disableEventHandler($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::EVENTHANDLER => PropertyModifier::STATE_DISABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::EVENTHANDLER => PropertyModifier::STATE_DISABLE + ) + ) + ); } public function enablePerfdata($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::PERFDATA => PropertyModifier::STATE_ENABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::PERFDATA => PropertyModifier::STATE_ENABLE + ) + ) + ); } public function disablePerfdata($objects) { - $this->setMonitoringProperties($objects,new PropertyModifier(array( - PropertyModifier::PERFDATA => PropertyModifier::STATE_DISABLE - ))); + $this->setMonitoringProperties( + $objects, + new PropertyModifier( + array( + PropertyModifier::PERFDATA => PropertyModifier::STATE_DISABLE + ) + ) + ); } -} \ No newline at end of file +} diff --git a/library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php b/library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php index ebd813925..8d005f4ed 100644 --- a/library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php +++ b/library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php @@ -1,7 +1,13 @@