icingaweb2/modules/monitoring/application/views/helpers/MonitoringProperties.php

288 lines
7.2 KiB
PHP
Raw Normal View History

<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
/**
* Class Zend_View_Helper_MonitoringProperties
*/
class Zend_View_Helper_MonitoringProperties extends Zend_View_Helper_Abstract
{
/**
* Value for check type active
*/
const CHECK_ACTIVE = 'ACTIVE';
/**
* Value for check type passive
*/
const CHECK_PASSIVE = 'PASSIVE';
/**
* Value for check type disabled
*/
const CHECK_DISABLED = 'DISABLED';
/**
* Return value for not available
*/
const VALUE_NA = 'N/A';
/**
* Return value for "YES"
*/
const VALUE_YES = 'YES';
/**
* Return value for "NO"
*/
const VALUE_NO = 'NO';
/**
* Label / value mapping for object keys
*
* Keys can be callables in this object
*
* @var array
*/
private static $keys = array(
'buildAttempt' => 'Current attempt',
'last_check' => 'Last check time',
'buildCheckType' => 'Check type',
'buildLatency' => 'Check latency / duration',
'buildNextCheck' => 'Next scheduled active check',
'buildLastStateChange' => 'Last state change',
'buildLastNotification' => 'Last notification',
'buildFlapping' => 'Is this %s flapping?',
'buildScheduledDowntime' => 'In scheduled downtime?',
'status_update_time' => 'Last update'
);
private static $notificationReasons = array(
0 => 'NORMAL',
1 => 'ACKNOWLEDGEMENT',
2 => 'FLAPPING START',
3 => 'FLAPPING STOP',
4 => 'FLAPPING DISABLED',
5 => 'DOWNTIME START',
6 => 'DOWNTIME END',
7 => 'DOWNTIME CANCELLED',
8 => 'CUSTOM',
9 => 'STALKING'
);
/**
* Return the object type
* @param stdClass $object
* @return mixed
*/
private function getObjectType(stdClass $object)
{
2013-07-23 12:18:27 +02:00
$keys = array_keys(get_object_vars($object));
$keyParts = explode('_', array_shift($keys), 2);
return array_shift($keyParts);
}
/**
* Drop all object specific attribute prefixes
* @param stdClass $object
* @param $type
* @return object
*/
private function dropObjectType(stdClass $object, $type)
{
$vars = get_object_vars($object);
$out = array();
foreach ($vars as $name => $value) {
$name = str_replace($type. '_', '', $name);
$out[$name] = $value;
}
return (object)$out;
}
/**
* Get string for attempt
* @param stdClass $object
* @return string
*/
private function buildAttempt(stdClass $object)
{
return sprintf(
'%s/%s (%s state)',
$object->current_check_attempt,
$object->max_check_attempts,
($object->state_type === '1') ? 'HARD' : 'SOFT'
);
}
/**
* Generic fomatter for float values
* @param $value
* @return string
*/
private function floatFormatter($value)
{
return sprintf('%.4f', $value);
}
/**
* Get the string for check type
* @param stdClass $object
* @return string
*/
private function buildCheckType(stdClass $object)
{
if ($object->passive_checks_enabled === '1' && $object->active_checks_enabled === '0') {
return self::CHECK_PASSIVE;
} elseif ($object->passive_checks_enabled === '0' && $object->active_checks_enabled === '0') {
return self::CHECK_DISABLED;
}
return self::CHECK_ACTIVE;
}
/**
* Get string for latency
* @param stdClass $object
* @return string
*/
private function buildLatency(stdClass $object)
{
$val = '';
if ($this->buildCheckType($object) === self::CHECK_PASSIVE) {
$val .= self::VALUE_NA;
} else {
2013-07-23 12:18:27 +02:00
$val .= $this->floatFormatter(
Merge branch 'feature/service-detail-view-4181' of ssh://git.icinga.org/icinga2-web Conflicts: library/Icinga/Application/Logger.php library/Icinga/Application/Web.php library/Icinga/Authentication/Backend/LdapUserBackend.php library/Icinga/Authentication/Credentials.php library/Icinga/Authentication/Manager.php library/Icinga/Authentication/PhpSession.php library/Icinga/Authentication/Session.php library/Icinga/Authentication/User.php library/Icinga/Authentication/UserBackend.php library/Icinga/Backend/AbstractBackend.php library/Icinga/Backend/DataView/ObjectRemappingView.php library/Icinga/Backend/Query.php library/Icinga/Backend/Statusdat/DataView/StatusdatHostView.php library/Icinga/Backend/Statusdat/HostgroupsummaryQuery.php library/Icinga/Backend/Statusdat/HostlistQuery.php library/Icinga/Backend/Statusdat/ServicegroupsummaryQuery.php library/Icinga/Backend/Statusdat/ServicelistQuery.php library/Icinga/Exception/ConfigurationError.php library/Icinga/Exception/MissingParameterException.php library/Icinga/Exception/NotImplementedError.php library/Icinga/Exception/ProgrammingError.php library/Icinga/Exception/SystemPermissionException.php library/Icinga/Protocol/AbstractQuery.php library/Icinga/Protocol/Commandpipe/Acknowledgement.php library/Icinga/Protocol/Commandpipe/CommandPipe.php library/Icinga/Protocol/Commandpipe/Comment.php library/Icinga/Protocol/Commandpipe/Downtime.php library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php library/Icinga/Protocol/Commandpipe/IComment.php library/Icinga/Protocol/Commandpipe/PropertyModifier.php library/Icinga/Protocol/Ldap/Connection.php library/Icinga/Protocol/Ldap/Exception.php library/Icinga/Protocol/Ldap/LdapUtils.php library/Icinga/Protocol/Ldap/Node.php library/Icinga/Protocol/Ldap/Query.php library/Icinga/Protocol/Ldap/Root.php library/Icinga/Protocol/Statusdat/Exception/ParsingException.php library/Icinga/Protocol/Statusdat/IReader.php library/Icinga/Protocol/Statusdat/ObjectContainer.php library/Icinga/Protocol/Statusdat/Parser.php library/Icinga/Protocol/Statusdat/Query.php library/Icinga/Protocol/Statusdat/Query/Expression.php library/Icinga/Protocol/Statusdat/Query/Group.php library/Icinga/Protocol/Statusdat/Query/IQueryPart.php library/Icinga/Protocol/Statusdat/Reader.php library/Icinga/Protocol/Statusdat/RuntimeStateContainer.php library/Icinga/Protocol/Statusdat/View/AccessorStrategy.php library/Icinga/Web/ActionController.php library/Icinga/Web/Form.php library/Icinga/Web/Hook/Configuration/ConfigurationTab.php library/Icinga/Web/Hook/Configuration/ConfigurationTabBuilder.php library/Icinga/Web/Hook/Configuration/ConfigurationTabInterface.php library/Icinga/Web/Hook/Grapher.php library/Icinga/Web/Hook/Toptray.php library/Icinga/Web/ModuleActionController.php library/Icinga/Web/Notification.php library/Icinga/Web/Paginator/Adapter/QueryAdapter.php library/Icinga/Web/Paginator/ScrollingStyle/SlidingWithBorder.php library/Icinga/Web/Widget.php modules/monitoring/application/views/helpers/MonitoringCommands.php modules/monitoring/application/views/helpers/MonitoringFlags.php modules/monitoring/application/views/helpers/MonitoringProperties.php modules/monitoring/application/views/scripts/show/components/comments.phtml modules/monitoring/application/views/scripts/show/components/downtime.phtml modules/monitoring/application/views/scripts/show/components/flags.phtml modules/monitoring/application/views/scripts/show/components/properties.phtml modules/monitoring/application/views/scripts/show/header.phtml modules/monitoring/application/views/scripts/show/host.phtml modules/monitoring/application/views/scripts/show/service.phtml modules/monitoring/library/Monitoring/Backend/AbstractBackend.php modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeQuery.php modules/monitoring/library/Monitoring/Backend/Ido/Query/StatusQuery.php modules/monitoring/library/Monitoring/Backend/Statusdat.php modules/monitoring/library/Monitoring/Backend/Statusdat/Criteria/Order.php modules/monitoring/library/Monitoring/Backend/Statusdat/DataView/StatusdatServiceView.php modules/monitoring/library/Monitoring/Backend/Statusdat/Query/GroupsummaryQuery.php modules/monitoring/library/Monitoring/Backend/Statusdat/Query/Query.php modules/monitoring/library/Monitoring/Command/Meta.php modules/monitoring/library/Monitoring/View/DowntimeView.php modules/monitoring/test/php/application/views/helpers/MonitoringCommandsTest.php modules/monitoring/test/php/application/views/helpers/MonitoringPropertiesTest.php modules/monitoring/test/php/library/Command/MetaTest.php
2013-07-23 16:32:00 +02:00
(isset($object->check_latency)) ? $object->check_latency : 0
2013-07-23 12:18:27 +02:00
);
}
2013-07-23 12:18:27 +02:00
$val .= ' / '. $this->floatFormatter(
isset($object->check_execution_time) ? $object->check_execution_time : 0
). ' seconds';
return $val;
}
/**
* Get string for next check
* @param stdClass $object
* @return string
*/
private function buildNextCheck(stdClass $object)
{
if ($this->buildCheckType($object) === self::CHECK_PASSIVE) {
return self::VALUE_NA;
} else {
return $object->next_check;
}
}
/**
* Get date for last state change
* @param stdClass $object
* @return string
*/
private function buildLastStateChange(stdClass $object)
{
return strftime('%Y-%m-%d %H:%M:%S', $object->last_state_change);
}
/**
* Get string for "last notification"
* @param stdClass $object
* @return string
*/
private function buildLastNotification(stdClass $object)
{
$val = '';
if ($object->last_notification === '0000-00-00 00:00:00') {
$val .= self::VALUE_NA;
} else {
$val .= $object->last_notification;
}
$val .= sprintf(' (notification %d)', $object->current_notification_number);
return $val;
}
/**
* Get string for "is flapping"
* @param stdClass $object
* @return string
*/
private function buildFlapping(stdClass $object)
{
$val = '';
if ($object->is_flapping === '0') {
$val .= self::VALUE_NO;
} else {
$val .= self::VALUE_YES;
}
$val .= sprintf(' (%.2f%% state change)', $object->percent_state_change);
return $val;
}
/**
* Get string for scheduled downtime
* @param stdClass $object
* @return string
*/
private function buildScheduledDowntime(stdClass $object)
{
if ($object->in_downtime === '1') {
return self::VALUE_YES;
}
return self::VALUE_NO;
}
/**
* Get an array which represent monitoring properties
*
* @param stdClass $object
* @return array
*/
public function monitoringProperties(stdClass $object)
{
$type = $this->getObjectType($object);
$object = $this->dropObjectType($object, $type);
$out = array();
foreach (self::$keys as $property => $label) {
$label = sprintf($label, $type);
if (is_callable(array(&$this, $property))) {
$out[$label] = $this->$property($object);
} elseif (isset($object->{$property})) {
$out[$label] = $object->{$property};
}
}
return $out;
}
public function getNotificationType(stdClass $notification)
{
$reason = intval($notification->notification_reason);
if (!isset(self::$notificationReasons[$reason])) {
return 'N/A';
}
$type = self::$notificationReasons[$reason];
if ($reason === 8) {
if (intval($notification->notification_type) === 0) {
$type .= '(UP)';
} else {
$type .= '(OK)';
}
}
return $type;
}
}
// @codingStandardsIgnoreStop