From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- .../application/views/helpers/CheckPerformance.php | 50 ++++++ .../application/views/helpers/ContactFlags.php | 46 +++++ .../application/views/helpers/Customvar.php | 62 +++++++ .../application/views/helpers/EscapeComment.php | 38 ++++ .../application/views/helpers/HostFlags.php | 33 ++++ .../application/views/helpers/IconImage.php | 64 +++++++ .../monitoring/application/views/helpers/Link.php | 72 ++++++++ .../application/views/helpers/MonitoringFlags.php | 40 +++++ .../application/views/helpers/Perfdata.php | 116 ++++++++++++ .../application/views/helpers/PluginOutput.php | 199 +++++++++++++++++++++ .../application/views/helpers/RuntimeVariables.php | 50 ++++++ .../application/views/helpers/ServiceFlags.php | 33 ++++ 12 files changed, 803 insertions(+) create mode 100644 modules/monitoring/application/views/helpers/CheckPerformance.php create mode 100644 modules/monitoring/application/views/helpers/ContactFlags.php create mode 100644 modules/monitoring/application/views/helpers/Customvar.php create mode 100644 modules/monitoring/application/views/helpers/EscapeComment.php create mode 100644 modules/monitoring/application/views/helpers/HostFlags.php create mode 100644 modules/monitoring/application/views/helpers/IconImage.php create mode 100644 modules/monitoring/application/views/helpers/Link.php create mode 100644 modules/monitoring/application/views/helpers/MonitoringFlags.php create mode 100644 modules/monitoring/application/views/helpers/Perfdata.php create mode 100644 modules/monitoring/application/views/helpers/PluginOutput.php create mode 100644 modules/monitoring/application/views/helpers/RuntimeVariables.php create mode 100644 modules/monitoring/application/views/helpers/ServiceFlags.php (limited to 'modules/monitoring/application/views/helpers') diff --git a/modules/monitoring/application/views/helpers/CheckPerformance.php b/modules/monitoring/application/views/helpers/CheckPerformance.php new file mode 100644 index 0000000..feac4d8 --- /dev/null +++ b/modules/monitoring/application/views/helpers/CheckPerformance.php @@ -0,0 +1,50 @@ +host_passive_count = 0; + $out->host_passive_latency_avg = 0; + $out->host_passive_execution_avg = 0; + $out->service_passive_count = 0; + $out->service_passive_latency_avg = 0; + $out->service_passive_execution_avg = 0; + $out->service_active_count = 0; + $out->service_active_latency_avg = 0; + $out->service_active_execution_avg = 0; + $out->host_active_count = 0; + $out->host_active_latency_avg = 0; + $out->host_active_execution_avg = 0; + + foreach ($results as $row) { + $key = $row->object_type . '_' . $row->check_type . '_'; + $out->{$key . 'count'} = $row->object_count; + $out->{$key . 'latency_avg'} = $row->latency / $row->object_count; + $out->{$key . 'execution_avg'} = $row->execution_time / $row->object_count; + } + return $out; + } +} diff --git a/modules/monitoring/application/views/helpers/ContactFlags.php b/modules/monitoring/application/views/helpers/ContactFlags.php new file mode 100644 index 0000000..858c726 --- /dev/null +++ b/modules/monitoring/application/views/helpers/ContactFlags.php @@ -0,0 +1,46 @@ +$optionName)) { + return $contact->$optionName; + } + $out = array(); + foreach ($contact as $key => $value) { + if (preg_match('/^contact_notify_' . $type . '_.*/', $key) && $value == true) { + $option = $this->getNotificationOptionName($key); + if (strtolower($option) != 'timeperiod') { + array_push($out, $option); + } + } + } + return implode($glue, $out); + } +} diff --git a/modules/monitoring/application/views/helpers/Customvar.php b/modules/monitoring/application/views/helpers/Customvar.php new file mode 100644 index 0000000..8bfdc3a --- /dev/null +++ b/modules/monitoring/application/views/helpers/Customvar.php @@ -0,0 +1,62 @@ +view->escape( + is_string($struct) + ? $struct + : var_export($struct, true) + ), false); + } elseif (is_array($struct)) { + return $this->renderArray($struct); + } elseif (is_object($struct)) { + return $this->renderObject($struct); + } + } + + protected function renderArray($array) + { + if (empty($array)) { + return '[]'; + } + $out = "\n"; + } + + protected function renderObject($object) + { + if (0 === count((array) $object)) { + return '{}'; + } + $out = "{}"; + } +} diff --git a/modules/monitoring/application/views/helpers/EscapeComment.php b/modules/monitoring/application/views/helpers/EscapeComment.php new file mode 100644 index 0000000..be85a22 --- /dev/null +++ b/modules/monitoring/application/views/helpers/EscapeComment.php @@ -0,0 +1,38 @@ +). + * + * @param string $comment + * + * @return string + */ + public function escapeComment($comment) + { + if (self::$purifier === null) { + require_once 'HTMLPurifier/Bootstrap.php'; + require_once 'HTMLPurifier.php'; + require_once 'HTMLPurifier.autoload.php'; + + $config = HTMLPurifier_Config::createDefault(); + $config->set('Core.EscapeNonASCIICharacters', true); + $config->set('HTML.Allowed', 'a[href]'); + $config->set('Cache.DefinitionImpl', null); + self::$purifier = new HTMLPurifier($config); + } + return self::$purifier->purify($comment); + } +} diff --git a/modules/monitoring/application/views/helpers/HostFlags.php b/modules/monitoring/application/views/helpers/HostFlags.php new file mode 100644 index 0000000..81d8ebc --- /dev/null +++ b/modules/monitoring/application/views/helpers/HostFlags.php @@ -0,0 +1,33 @@ +host_handled && $host->host_state > 0) { + $icons[] = $this->view->icon('attention-alt', $this->view->translate('Unhandled')); + } + if ($host->host_acknowledged) { + $icons[] = $this->view->icon('ok', $this->view->translate('Acknowledged')); + } + if ($host->host_is_flapping) { + $icons[] = $this->view->icon('flapping', $this->view->translate('Flapping')); + } + if (! $host->host_notifications_enabled) { + $icons[] = $this->view->icon('bell-off-empty', $this->view->translate('Notifications Disabled')); + } + if ($host->host_in_downtime) { + $icons[] = $this->view->icon('plug', $this->view->translate('In Downtime')); + } + if (! $host->host_active_checks_enabled) { + if (! $host->host_passive_checks_enabled) { + $icons[] = $this->view->icon('eye-off', $this->view->translate('Active And Passive Checks Disabled')); + } else { + $icons[] = $this->view->icon('eye-off', $this->view->translate('Active Checks Disabled')); + } + } + return implode(' ', $icons); + } +} diff --git a/modules/monitoring/application/views/helpers/IconImage.php b/modules/monitoring/application/views/helpers/IconImage.php new file mode 100644 index 0000000..3c0ca43 --- /dev/null +++ b/modules/monitoring/application/views/helpers/IconImage.php @@ -0,0 +1,64 @@ +host_icon_image && ! preg_match('/[\'"]/', $object->host_icon_image)) { + return $this->view->icon( + Macro::resolveMacros($object->host_icon_image, $object), + null, + array( + 'alt' => $object->host_icon_image_alt, + 'class' => 'host-icon-image', + 'title' => $object->host_icon_image_alt + ) + ); + } + return ''; + } + + /** + * Display the image_icon of a MonitoredObject + * + * @param MonitoredObject|stdClass $object The host or service + * @return string + */ + public function service($object) + { + if ($object->service_icon_image && ! preg_match('/[\'"]/', $object->service_icon_image)) { + return $this->view->icon( + Macro::resolveMacros($object->service_icon_image, $object), + null, + array( + 'alt' => $object->service_icon_image_alt, + 'class' => 'service-icon-image', + 'title' => $object->service_icon_image_alt + ) + ); + } + return ''; + } +} diff --git a/modules/monitoring/application/views/helpers/Link.php b/modules/monitoring/application/views/helpers/Link.php new file mode 100644 index 0000000..c5443a4 --- /dev/null +++ b/modules/monitoring/application/views/helpers/Link.php @@ -0,0 +1,72 @@ +view->qlink( + $linkText, + 'monitoring/host/show', + array('host' => $host), + array('title' => sprintf($this->view->translate('Show detailed information for host %s'), $linkText)) + ); + } + + /** + * Create a service link + * + * @param string $service Service name + * @param string $serviceLinkText Text for the service link, e.g. the service's display name + * @param string $host Hostname + * @param string $hostLinkText Text for the host link, e.g. the host's display name + * @param string $class An optional class to use for this link + * + * @return string + */ + public function service($service, $serviceLinkText, $host, $hostLinkText, $class = null) + { + return sprintf( + '%s: %s', + $this->host($host, $hostLinkText), + $this->view->qlink( + $serviceLinkText, + 'monitoring/service/show', + array('host' => $host, 'service' => $service), + array( + 'title' => sprintf( + $this->view->translate('Show detailed information for service %s on host %s'), + $serviceLinkText, + $hostLinkText + ), + 'class' => $class + ) + ) + ); + } +} diff --git a/modules/monitoring/application/views/helpers/MonitoringFlags.php b/modules/monitoring/application/views/helpers/MonitoringFlags.php new file mode 100644 index 0000000..8509e5b --- /dev/null +++ b/modules/monitoring/application/views/helpers/MonitoringFlags.php @@ -0,0 +1,40 @@ + 'Passive Checks', + 'active_checks_enabled' => 'Active Checks', + 'obsessing' => 'Obsessing', + 'notifications_enabled' => 'Notifications', + 'event_handler_enabled' => 'Event Handler', + 'flap_detection_enabled' => 'Flap Detection', + ); + + /** + * Retrieve flags as array with either true or false as value + * + * @param MonitoredObject $object + * + * @return array + */ + public function monitoringFlags(/*MonitoredObject*/ $object) + { + $flags = array(); + foreach (self::$flags as $column => $description) { + $flags[$description] = (bool) $object->{$column}; + } + return $flags; + } +} diff --git a/modules/monitoring/application/views/helpers/Perfdata.php b/modules/monitoring/application/views/helpers/Perfdata.php new file mode 100644 index 0000000..e7bc72b --- /dev/null +++ b/modules/monitoring/application/views/helpers/Perfdata.php @@ -0,0 +1,116 @@ +asArray(); + uasort( + $pieChartData, + function ($a, $b) { + return $a->worseThan($b) ? -1 : ($b->worseThan($a) ? 1 : 0); + } + ); + $results = array(); + $keys = array('', 'label', 'value', 'min', 'max', 'warn', 'crit'); + $columns = array(); + $labels = array_combine( + $keys, + array( + '', + $this->view->translate('Label'), + $this->view->translate('Value'), + $this->view->translate('Min'), + $this->view->translate('Max'), + $this->view->translate('Warning'), + $this->view->translate('Critical') + ) + ); + foreach ($pieChartData as $perfdata) { + if ($perfdata->isVisualizable()) { + $columns[''] = ''; + } + foreach ($perfdata->toArray() as $column => $value) { + if (empty($value) || + $column === 'min' && floatval($value) === 0.0 || + $column === 'max' && $perfdata->isPercentage() && floatval($value) === 100) { + continue; + } + $columns[$column] = $labels[$column]; + } + } + // restore original column array sorting + $headers = array(); + foreach ($keys as $column) { + if (isset($columns[$column])) { + $headers[$column] = $labels[$column]; + } + } + $table = array('' . implode('', $headers) . ''); + foreach ($pieChartData as $perfdata) { + if ($compact && $perfdata->isVisualizable()) { + $results[] = $perfdata->asInlinePie($color)->render(); + } else { + $data = array(); + if ($perfdata->isVisualizable()) { + $data []= $perfdata->asInlinePie($color)->render(); + } elseif (isset($columns[''])) { + $data []= ''; + } + if (! $compact) { + foreach ($perfdata->toArray() as $column => $value) { + if (! isset($columns[$column])) { + continue; + } + $text = $this->view->escape(empty($value) ? '-' : $value); + $data []= sprintf( + '%s', + $text, + $text + ); + } + } + $table []= '' . implode('', $data) . ''; + } + } + $table[] = ''; + if ($limit > 0) { + $count = $compact ? count($results) : count($table); + if ($count > $limit) { + if ($compact) { + $results = array_slice($results, 0, $limit); + $title = sprintf($this->view->translate('%d more ...'), $count - $limit); + $results[] = ''; + } else { + $table = array_slice($table, 0, $limit); + } + } + } + if ($compact) { + return join('', $results); + } else { + if (empty($table)) { + return ''; + } + return sprintf( + '%s
', + implode("\n", $table) + ); + } + } +} diff --git a/modules/monitoring/application/views/helpers/PluginOutput.php b/modules/monitoring/application/views/helpers/PluginOutput.php new file mode 100644 index 0000000..bcd3d9e --- /dev/null +++ b/modules/monitoring/application/views/helpers/PluginOutput.php @@ -0,0 +1,199 @@ +$1OK$2', + '$1WARNING$2', + '$1CRITICAL$2', + '$1UNKNOWN$2', + '$1UP$2', + '$1DOWN$2', + '@@@@@@', + ); + + /** + * Patterns to be replaced in html plugin output + * + * @var array + */ + protected static $htmlPatterns = array( + '~\\\t~', + '~\\\n~', + '~hookRenderer = (new \Icinga\Module\Monitoring\Web\Helper\PluginOutputHookRenderer())->registerHooks(); + } + + /** + * Render plugin output + * + * @param string $output + * @param bool $raw + * @param string $command Check command + * + * @return string + */ + public function pluginOutput($output, $raw = false, $command = null) + { + if (empty($output)) { + return ''; + } + + if ($command !== null) { + $output = $this->hookRenderer->render($command, $output, ! $raw); + } + + if (preg_match('~<\w+(?>\s\w+=[^>]*)?>~', $output)) { + // HTML + $output = HtmlPurifier::process(preg_replace( + self::$htmlPatterns, + self::$htmlReplacements, + $output + )); + $isHtml = true; + } else { + // Plaintext + $output = preg_replace( + self::$txtPatterns, + self::$txtReplacements, + // Not using the view here to escape this. The view sets `double_encode` to true + htmlspecialchars($output, ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5, View::CHARSET, false) + ); + $isHtml = false; + } + + $output = trim($output); + // Add zero-width space after commas which are not followed by a whitespace character + // in oder to help browsers to break words in plugin output + $output = preg_replace('/,(?=[^\s])/', ',​', $output); + if (! $raw) { + if ($isHtml) { + $output = $this->processHtml($output); + $output = '
' . $output . '
'; + } else { + $output = '
' . $output . '
'; + } + } + + return $output; + } + + /** + * Replace classic Icinga CGI links with Icinga Web 2 links and color state information, if any + * + * @param string $html + * + * @return string + */ + protected function processHtml($html) + { + $pattern = '/[([](OK|WARNING|CRITICAL|UNKNOWN|UP|DOWN)[)\]]/'; + $doc = new DOMDocument(); + $doc->loadXML('
' . $html . '
', LIBXML_NOERROR | LIBXML_NOWARNING); + $dom = new RecursiveIteratorIterator(new DomNodeIterator($doc), RecursiveIteratorIterator::SELF_FIRST); + $nodesToRemove = array(); + foreach ($dom as $node) { + /** @var \DOMNode $node */ + if ($node->nodeType === XML_TEXT_NODE) { + $start = 0; + while (preg_match($pattern, $node->nodeValue, $match, PREG_OFFSET_CAPTURE, $start)) { + $offsetLeft = $match[0][1]; + $matchLength = strlen($match[0][0]); + $leftLength = $offsetLeft - $start; + // if there is text before the match + if ($leftLength) { + // create node for leading text + $text = new DOMText(substr($node->nodeValue, $start, $leftLength)); + $node->parentNode->insertBefore($text, $node); + } + // create the new element for the match + $span = $doc->createElement('span', $match[0][0]); + $span->setAttribute('class', 'state-' . strtolower($match[1][0])); + $node->parentNode->insertBefore($span, $node); + + // start for next match + $start = $offsetLeft + $matchLength; + } + if ($start) { + // is there text left? + if (strlen($node->nodeValue) > $start) { + // create node for trailing text + $text = new DOMText(substr($node->nodeValue, $start)); + $node->parentNode->insertBefore($text, $node); + } + // delete the old node later + $nodesToRemove[] = $node; + } + } elseif ($node->nodeType === XML_ELEMENT_NODE) { + /** @var \DOMElement $node */ + if ($node->tagName === 'a' + && preg_match('~^/cgi\-bin/status\.cgi\?(.+)$~', $node->getAttribute('href'), $match) + ) { + parse_str($match[1], $params); + if (isset($params['host'])) { + $node->setAttribute( + 'href', + $this->view->baseUrl('/monitoring/host/show?host=' . urlencode($params['host'])) + ); + } + } + } + } + foreach ($nodesToRemove as $node) { + /** @var \DOMNode $node */ + $node->parentNode->removeChild($node); + } + + return substr($doc->saveHTML(), 5, -7); + } +} diff --git a/modules/monitoring/application/views/helpers/RuntimeVariables.php b/modules/monitoring/application/views/helpers/RuntimeVariables.php new file mode 100644 index 0000000..e80e8aa --- /dev/null +++ b/modules/monitoring/application/views/helpers/RuntimeVariables.php @@ -0,0 +1,50 @@ +total_hosts = isset($result->total_hosts) + ? $result->total_hosts + : 0; + $out->total_scheduled_hosts = isset($result->total_scheduled_hosts) + ? $result->total_scheduled_hosts + : 0; + $out->total_services = isset($result->total_services) + ? $result->total_services + : 0; + $out->total_scheduled_services = isset($result->total_scheduled_services) + ? $result->total_scheduled_services + : 0; + $out->average_services_per_host = $out->total_hosts > 0 + ? $out->total_services / $out->total_hosts + : 0; + $out->average_scheduled_services_per_host = $out->total_scheduled_hosts > 0 + ? $out->total_scheduled_services / $out->total_scheduled_hosts + : 0; + + return $out; + } +} diff --git a/modules/monitoring/application/views/helpers/ServiceFlags.php b/modules/monitoring/application/views/helpers/ServiceFlags.php new file mode 100644 index 0000000..47a351c --- /dev/null +++ b/modules/monitoring/application/views/helpers/ServiceFlags.php @@ -0,0 +1,33 @@ +service_handled && $service->service_state > 0) { + $icons[] = $this->view->icon('attention-alt', $this->view->translate('Unhandled')); + } + if ($service->service_acknowledged) { + $icons[] = $this->view->icon('ok', $this->view->translate('Acknowledged')); + } + if ($service->service_is_flapping) { + $icons[] = $this->view->icon('flapping', $this->view->translate('Flapping')); + } + if (! $service->service_notifications_enabled) { + $icons[] = $this->view->icon('bell-off-empty', $this->view->translate('Notifications Disabled')); + } + if ($service->service_in_downtime) { + $icons[] = $this->view->icon('plug', $this->view->translate('In Downtime')); + } + if (! $service->service_active_checks_enabled) { + if (! $service->service_passive_checks_enabled) { + $icons[] = $this->view->icon('eye-off', $this->view->translate('Active And Passive Checks Disabled')); + } else { + $icons[] = $this->view->icon('eye-off', $this->view->translate('Active Checks Disabled')); + } + } + return implode(' ', $icons); + } +} -- cgit v1.2.3