コード例 #1
0
ファイル: triggers.inc.php プロジェクト: TonywalkerCN/Zabbix
/**
 * Creates and returns the trigger overview table for the given hosts.
 *
 * @param array  	$hosts							an array of hosts with host IDs as keys
 * @param string 	$hosts[hostid][name]
 * @param string 	$hosts[hostid][hostid]
 * @param array		$triggers
 * @param string	$triggers[][triggerid]
 * @param string	$triggers[][description]
 * @param string	$triggers[][expression]
 * @param int		$triggers[][value]
 * @param int		$triggers[][lastchange]
 * @param int		$triggers[][flags]
 * @param array		$triggers[][url]
 * @param int		$triggers[][priority]
 * @param array		$triggers[][hosts]
 * @param string	$triggers[][hosts][][hostid]
 * @param string	$triggers[][hosts][][name]
 * @param string 	$pageFile						the page where the element is displayed
 * @param int    	$viewMode						table display style: either hosts on top, or host on the left side
 * @param string 	$screenId						the ID of the screen, that contains the trigger overview table
 *
 * @return CTableInfo
 */
function getTriggersOverview(array $hosts, array $triggers, $pageFile, $viewMode = null, $screenId = null)
{
    $data = array();
    $hostNames = array();
    foreach ($triggers as $trigger) {
        $trigger['description'] = CMacrosResolverHelper::resolveTriggerReference($trigger['expression'], $trigger['description']);
        foreach ($trigger['hosts'] as $host) {
            // triggers may belong to hosts that are filtered out and shouldn't be displayed, skip them
            if (!isset($hosts[$host['hostid']])) {
                continue;
            }
            $hostNames[$host['hostid']] = $host['name'];
            // a little tricky check for attempt to overwrite active trigger (value=1) with
            // inactive or active trigger with lower priority.
            if (!isset($data[$trigger['description']][$host['name']]) || ($data[$trigger['description']][$host['name']]['value'] == TRIGGER_VALUE_FALSE && $trigger['value'] == TRIGGER_VALUE_TRUE || ($data[$trigger['description']][$host['name']]['value'] == TRIGGER_VALUE_FALSE || $trigger['value'] == TRIGGER_VALUE_TRUE) && $trigger['priority'] > $data[$trigger['description']][$host['name']]['priority'])) {
                $data[$trigger['description']][$host['name']] = array('hostid' => $host['hostid'], 'triggerid' => $trigger['triggerid'], 'value' => $trigger['value'], 'lastchange' => $trigger['lastchange'], 'priority' => $trigger['priority'], 'flags' => $trigger['flags'], 'url' => $trigger['url'], 'hosts' => array($host));
            }
        }
    }
    $triggerTable = new CTableInfo(_('No triggers found.'));
    if (empty($hostNames)) {
        return $triggerTable;
    }
    $triggerTable->makeVerticalRotation();
    order_result($hostNames);
    if ($viewMode == STYLE_TOP) {
        // header
        $header = array(new CCol(_('Triggers'), 'center'));
        foreach ($hostNames as $hostName) {
            $header[] = new CCol($hostName, 'vertical_rotation');
        }
        $triggerTable->setHeader($header, 'vertical_header');
        // data
        foreach ($data as $description => $triggerHosts) {
            $columns = array(nbsp($description));
            foreach ($hostNames as $hostName) {
                $columns[] = getTriggerOverviewCells(isset($triggerHosts[$hostName]) ? $triggerHosts[$hostName] : null, $pageFile, $screenId);
            }
            $triggerTable->addRow($columns);
        }
    } else {
        // header
        $header = array(new CCol(_('Host'), 'center'));
        foreach ($data as $description => $triggerHosts) {
            $header[] = new CCol($description, 'vertical_rotation');
        }
        $triggerTable->setHeader($header, 'vertical_header');
        // data
        $scripts = API::Script()->getScriptsByHosts(zbx_objectValues($hosts, 'hostid'));
        foreach ($hostNames as $hostId => $hostName) {
            $name = new CSpan($hostName, 'link_menu');
            $name->setMenuPopup(CMenuPopupHelper::getHost($hosts[$hostId], $scripts[$hostId]));
            $columns = array($name);
            foreach ($data as $triggerHosts) {
                $columns[] = getTriggerOverviewCells(isset($triggerHosts[$hostName]) ? $triggerHosts[$hostName] : null, $pageFile, $screenId);
            }
            $triggerTable->addRow($columns);
        }
    }
    return $triggerTable;
}
コード例 #2
0
/**
 * Creates and returns the trigger overview table for the given hosts.
 *
 * @param array  $hostIds
 * @param string $application	name of application to filter
 * @param string $pageFile		the page where the element is displayed
 * @param int    $viewMode		table display style: either hosts on top, or host on the left side
 * @param string $screenId		the ID of the screen, that contains the trigger overview table
 *
 * @return CTableInfo
 */
function getTriggersOverview($hostIds, $application, $pageFile, $viewMode = null, $screenId = null)
{
    if (is_null($viewMode)) {
        $viewMode = CProfile::get('web.overview.view.style', STYLE_TOP);
    }
    // get application ids
    $applicationIds = null;
    if ($application !== '') {
        $dbApplications = API::Application()->get(array('hostids' => $hostIds, 'filter' => array('name' => $application), 'output' => array('applicationid')));
        $applicationIds = zbx_objectValues($dbApplications, 'applicationid');
        $hostIds = null;
    }
    // get triggers
    $dbTriggers = API::Trigger()->get(array('hostids' => $hostIds, 'applicationids' => $applicationIds, 'monitored' => true, 'skipDependent' => true, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => array('hostid', 'name'), 'sortfield' => 'description'));
    // get hosts
    $hostIds = array();
    foreach ($dbTriggers as $trigger) {
        $host = reset($trigger['hosts']);
        $hostIds[$host['hostid']] = $host['hostid'];
    }
    $hosts = API::Host()->get(array('output' => array('name', 'hostid', 'status'), 'hostids' => $hostIds, 'preservekeys' => true, 'selectScreens' => $viewMode == STYLE_LEFT ? API_OUTPUT_COUNT : null));
    $triggers = array();
    $hostNames = array();
    foreach ($dbTriggers as $trigger) {
        $host = reset($trigger['hosts']);
        $host['name'] = get_node_name_by_elid($host['hostid'], null, NAME_DELIMITER) . $host['name'];
        $trigger['description'] = CMacrosResolverHelper::resolveTriggerReference($trigger['expression'], $trigger['description']);
        $hostNames[$host['hostid']] = $host['name'];
        // a little tricky check for attempt to overwrite active trigger (value=1) with
        // inactive or active trigger with lower priority.
        if (!isset($triggers[$trigger['description']][$host['name']]) || ($triggers[$trigger['description']][$host['name']]['value'] == TRIGGER_VALUE_FALSE && $trigger['value'] == TRIGGER_VALUE_TRUE || ($triggers[$trigger['description']][$host['name']]['value'] == TRIGGER_VALUE_FALSE || $trigger['value'] == TRIGGER_VALUE_TRUE) && $trigger['priority'] > $triggers[$trigger['description']][$host['name']]['priority'])) {
            $triggers[$trigger['description']][$host['name']] = array('hostid' => $host['hostid'], 'triggerid' => $trigger['triggerid'], 'value' => $trigger['value'], 'lastchange' => $trigger['lastchange'], 'priority' => $trigger['priority'], 'flags' => $trigger['flags'], 'url' => $trigger['url'], 'hosts' => array($host));
        }
    }
    $triggerTable = new CTableInfo(_('No triggers found.'));
    if (empty($hostNames)) {
        return $triggerTable;
    }
    $triggerTable->makeVerticalRotation();
    order_result($hostNames);
    if ($viewMode == STYLE_TOP) {
        // header
        $header = array(new CCol(_('Triggers'), 'center'));
        foreach ($hostNames as $hostName) {
            $header[] = new CCol($hostName, 'vertical_rotation');
        }
        $triggerTable->setHeader($header, 'vertical_header');
        // data
        foreach ($triggers as $description => $triggerHosts) {
            $columns = array(nbsp($description));
            foreach ($hostNames as $hostName) {
                $columns[] = getTriggerOverviewCells(isset($triggerHosts[$hostName]) ? $triggerHosts[$hostName] : null, $pageFile, $screenId);
            }
            $triggerTable->addRow($columns);
        }
    } else {
        // header
        $header = array(new CCol(_('Host'), 'center'));
        foreach ($triggers as $description => $triggerHosts) {
            $header[] = new CCol($description, 'vertical_rotation');
        }
        $triggerTable->setHeader($header, 'vertical_header');
        // data
        $scripts = API::Script()->getScriptsByHosts(zbx_objectValues($hosts, 'hostid'));
        foreach ($hostNames as $hostId => $hostName) {
            $name = new CSpan($hostName, 'link_menu');
            $name->setMenuPopup(getMenuPopupHost($hosts[$hostId], $scripts[$hostId]));
            $columns = array($name);
            foreach ($triggers as $triggerHosts) {
                $columns[] = getTriggerOverviewCells(isset($triggerHosts[$hostName]) ? $triggerHosts[$hostName] : null, $pageFile, $screenId);
            }
            $triggerTable->addRow($columns);
        }
    }
    return $triggerTable;
}
コード例 #3
0
/**
 * Creates and returns the trigger overview table for the given hosts.
 *
 * @param array  	$hosts							an array of hosts with host IDs as keys
 * @param string 	$hosts[hostid][name]
 * @param string 	$hosts[hostid][hostid]
 * @param array		$triggers
 * @param string	$triggers[][triggerid]
 * @param string	$triggers[][description]
 * @param string	$triggers[][expression]
 * @param int		$triggers[][value]
 * @param int		$triggers[][lastchange]
 * @param int		$triggers[][flags]
 * @param array		$triggers[][url]
 * @param int		$triggers[][priority]
 * @param array		$triggers[][hosts]
 * @param string	$triggers[][hosts][][hostid]
 * @param string	$triggers[][hosts][][name]
 * @param string 	$pageFile						the page where the element is displayed
 * @param int    	$viewMode						table display style: either hosts on top, or host on the left side
 * @param string 	$screenId						the ID of the screen, that contains the trigger overview table
 *
 * @return CTableInfo
 */
function getTriggersOverview(array $hosts, array $triggers, $pageFile, $viewMode = null, $screenId = null)
{
    $data = [];
    $hostNames = [];
    $trcounter = [];
    $triggers = CMacrosResolverHelper::resolveTriggerNames($triggers, true);
    foreach ($triggers as $trigger) {
        $trigger_name = $trigger['description'];
        foreach ($trigger['hosts'] as $host) {
            // triggers may belong to hosts that are filtered out and shouldn't be displayed, skip them
            if (!isset($hosts[$host['hostid']])) {
                continue;
            }
            $hostNames[$host['hostid']] = $host['name'];
            if (!array_key_exists($host['name'], $trcounter)) {
                $trcounter[$host['name']] = [];
            }
            if (!array_key_exists($trigger_name, $trcounter[$host['name']])) {
                $trcounter[$host['name']][$trigger_name] = 0;
            }
            $data[$trigger_name][$trcounter[$host['name']][$trigger_name]][$host['name']] = ['groupid' => $trigger['groupid'], 'hostid' => $host['hostid'], 'triggerid' => $trigger['triggerid'], 'value' => $trigger['value'], 'lastchange' => $trigger['lastchange'], 'priority' => $trigger['priority'], 'flags' => $trigger['flags'], 'url' => $trigger['url'], 'hosts' => $trigger['hosts'], 'items' => $trigger['items']];
            $trcounter[$host['name']][$trigger_name]++;
        }
    }
    $triggerTable = new CTableInfo();
    if (empty($hostNames)) {
        return $triggerTable;
    }
    $triggerTable->makeVerticalRotation();
    order_result($hostNames);
    if ($viewMode == STYLE_TOP) {
        // header
        $header = [_('Triggers')];
        foreach ($hostNames as $hostName) {
            $header[] = (new CColHeader($hostName))->addClass('vertical_rotation');
        }
        $triggerTable->setHeader($header);
        // data
        foreach ($data as $trigger_name => $trigger_data) {
            foreach ($trigger_data as $trigger_hosts) {
                $columns = [nbsp($trigger_name)];
                foreach ($hostNames as $hostName) {
                    $columns[] = getTriggerOverviewCells(isset($trigger_hosts[$hostName]) ? $trigger_hosts[$hostName] : null, $pageFile, $screenId);
                }
                $triggerTable->addRow($columns);
            }
        }
    } else {
        // header
        $header = [_('Host')];
        foreach ($data as $trigger_name => $trigger_data) {
            foreach ($trigger_data as $trigger_hosts) {
                $header[] = (new CColHeader($trigger_name))->addClass('vertical_rotation');
            }
        }
        $triggerTable->setHeader($header);
        // data
        $scripts = API::Script()->getScriptsByHosts(zbx_objectValues($hosts, 'hostid'));
        foreach ($hostNames as $hostId => $hostName) {
            $name = (new CSpan($hostName))->addClass(ZBX_STYLE_LINK_ACTION);
            $name->setMenuPopup(CMenuPopupHelper::getHost($hosts[$hostId], $scripts[$hostId]));
            $columns = [(new CCol($name))->addClass(ZBX_STYLE_NOWRAP)];
            foreach ($data as $trigger_data) {
                foreach ($trigger_data as $trigger_hosts) {
                    $columns[] = getTriggerOverviewCells(isset($trigger_hosts[$hostName]) ? $trigger_hosts[$hostName] : null, $pageFile, $screenId);
                }
            }
            $triggerTable->addRow($columns);
        }
    }
    return $triggerTable;
}