/**
 * Creates and returns the trigger overview table for the given hosts.
 *
 * Possible $view_style values:
 * - STYLE_TOP
 * - STYLE_LEFT
 *
 * @param string|array $hostids
 * @param int $view_style	    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 get_triggers_overview($hostids, $view_style = null, $screenId = null)
{
    if (is_null($view_style)) {
        $view_style = CProfile::get('web.overview.view.style', STYLE_TOP);
    }
    // get triggers
    $dbTriggers = API::Trigger()->get(array('hostids' => $hostids, 'monitored' => true, 'skipDependent' => true, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => array('hostid', 'name'), 'sortfield' => 'description'));
    // get hosts
    $hostids = array();
    foreach ($dbTriggers as $trigger) {
        $hostids[] = $trigger['hosts'][0]['hostid'];
    }
    $options = array('output' => array('name', 'hostid'), 'hostids' => $hostids, 'preservekeys' => true);
    if ($view_style == STYLE_LEFT) {
        $options['selectScreens'] = API_OUTPUT_COUNT;
        $options['selectInventory'] = array('hostid');
    }
    $hosts = API::Host()->get($options);
    $triggers = array();
    $hostNames = array();
    foreach ($dbTriggers as $trigger) {
        $trigger['host'] = $trigger['hosts'][0]['name'];
        $trigger['hostid'] = $trigger['hosts'][0]['hostid'];
        $trigger['host'] = get_node_name_by_elid($trigger['hostid'], null, ': ') . $trigger['host'];
        $trigger['description'] = CTriggerHelper::expandReferenceMacros($trigger);
        $hostNames[$trigger['hostid']] = $trigger['host'];
        // 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']][$trigger['host']]) || ($triggers[$trigger['description']][$trigger['host']]['value'] == TRIGGER_VALUE_FALSE && $trigger['value'] == TRIGGER_VALUE_TRUE || ($triggers[$trigger['description']][$trigger['host']]['value'] == TRIGGER_VALUE_FALSE || $trigger['value'] == TRIGGER_VALUE_TRUE) && $trigger['priority'] > $triggers[$trigger['description']][$trigger['host']]['priority'])) {
            $triggers[$trigger['description']][$trigger['host']] = array('hostid' => $trigger['hostid'], 'triggerid' => $trigger['triggerid'], 'value' => $trigger['value'], 'lastchange' => $trigger['lastchange'], 'priority' => $trigger['priority']);
        }
    }
    $triggerTable = new CTableInfo(_('No triggers defined.'));
    if (empty($hostNames)) {
        return $triggerTable;
    }
    $triggerTable->makeVerticalRotation();
    order_result($hostNames);
    if ($view_style == 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) {
            $tableColumns = array(nbsp($description));
            foreach ($hostNames as $hostid => $hostName) {
                array_push($tableColumns, get_trigger_overview_cells($triggerHosts, $hostName, $screenId));
            }
            $triggerTable->addRow($tableColumns);
        }
    } else {
        $hostScripts = API::Script()->getScriptsByHosts(zbx_objectValues($hosts, 'hostid'));
        foreach ($hostScripts as $hostid => $scripts) {
            $hosts[$hostid]['scripts'] = $scripts;
        }
        // header
        $header = array(new CCol(_('Host'), 'center'));
        foreach ($triggers as $description => $triggerHosts) {
            $header[] = new CCol($description, 'vertical_rotation');
        }
        $triggerTable->setHeader($header, 'vertical_header');
        // data
        foreach ($hostNames as $hostid => $hostName) {
            $host = $hosts[$hostid];
            // host js link
            $hostSpan = new CSpan(nbsp($hostName), 'link_menu menu-host');
            $hostSpan->setAttribute('data-menu', hostMenuData($host, $hostScripts[$host['hostid']]));
            $tableColumns = array($hostSpan);
            foreach ($triggers as $triggerHosts) {
                array_push($tableColumns, get_trigger_overview_cells($triggerHosts, $hostName, $screenId));
            }
            $triggerTable->addRow($tableColumns);
        }
    }
    return $triggerTable;
}
Example #2
0
function get_triggers_overview($hostids, $view_style = null)
{
    $available_triggers = get_accessible_triggers(PERM_READ_ONLY, $hostids);
    if (is_null($view_style)) {
        $view_style = get_profile('web.overview.view.style', STYLE_TOP);
    }
    $table = new CTableInfo(S_NO_TRIGGERS_DEFINED);
    $result = DBselect('SELECT DISTINCT t.triggerid,t.description,t.expression,t.value,t.priority,t.lastchange,h.hostid,h.host' . ' FROM hosts h,items i,triggers t, functions f ' . ' WHERE h.status=' . HOST_STATUS_MONITORED . ' AND h.hostid=i.hostid ' . ' AND i.itemid=f.itemid ' . ' AND f.triggerid=t.triggerid' . ' AND ' . DBcondition('t.triggerid', $available_triggers) . ' AND t.status=' . TRIGGER_STATUS_ENABLED . ' AND i.status=' . ITEM_STATUS_ACTIVE . ' ORDER BY t.description');
    unset($triggers);
    unset($hosts);
    $triggers = array();
    while ($row = DBfetch($result)) {
        if (trigger_dependent($row['triggerid'])) {
            continue;
        }
        $row['host'] = get_node_name_by_elid($row['hostid']) . $row['host'];
        $row['description'] = expand_trigger_description_constants($row['description'], $row);
        $hosts[strtolower($row['host'])] = $row['host'];
        // A little tricky check for attempt to overwrite active trigger (value=1) with
        // inactive or active trigger with lower priority.
        if (!isset($triggers[$row['description']][$row['host']]) || ($triggers[$row['description']][$row['host']]['value'] == TRIGGER_VALUE_FALSE && $row['value'] == TRIGGER_VALUE_TRUE || ($triggers[$row['description']][$row['host']]['value'] == TRIGGER_VALUE_FALSE || $row['value'] == TRIGGER_VALUE_TRUE) && $row['priority'] > $triggers[$row['description']][$row['host']]['priority'])) {
            $triggers[$row['description']][$row['host']] = array('hostid' => $row['hostid'], 'triggerid' => $row['triggerid'], 'value' => $row['value'], 'lastchange' => $row['lastchange'], 'priority' => $row['priority']);
        }
    }
    if (!isset($hosts)) {
        return $table;
    }
    ksort($hosts);
    if ($view_style == STYLE_TOP) {
        $header = array(new CCol(S_TRIGGERS, 'center'));
        foreach ($hosts as $hostname) {
            $header = array_merge($header, array(new CImg('vtext.php?text=' . $hostname)));
        }
        $table->setHeader($header, 'vertical_header');
        foreach ($triggers as $descr => $trhosts) {
            $table_row = array(nbsp($descr));
            foreach ($hosts as $hostname) {
                $table_row = get_trigger_overview_cells($table_row, $trhosts, $hostname);
            }
            $table->AddRow($table_row);
        }
    } else {
        $header = array(new CCol(S_HOSTS, 'center'));
        foreach ($triggers as $descr => $trhosts) {
            $descr = array(new CImg('vtext.php?text=' . $descr));
            array_push($header, $descr);
        }
        $table->SetHeader($header, 'vertical_header');
        foreach ($hosts as $hostname) {
            $table_row = array(nbsp($hostname));
            foreach ($triggers as $descr => $trhosts) {
                $table_row = get_trigger_overview_cells($table_row, $trhosts, $hostname);
            }
            $table->AddRow($table_row);
        }
    }
    return $table;
}
function get_triggers_overview($hostids, $view_style = null)
{
    global $USER_DETAILS;
    if (is_null($view_style)) {
        $view_style = CProfile::get('web.overview.view.style', STYLE_TOP);
    }
    $table = new CTableInfo(S_NO_TRIGGERS_DEFINED);
    $options = array('hostids' => $hostids, 'monitored' => 1, 'expandData' => 1, 'skipDependent' => 1, 'output' => API_OUTPUT_EXTEND, 'sortfield' => 'description');
    $db_triggers = CTrigger::get($options);
    unset($triggers);
    unset($hosts);
    $triggers = array();
    foreach ($db_triggers as $tnum => $row) {
        $row['host'] = get_node_name_by_elid($row['hostid'], null, ': ') . $row['host'];
        $row['description'] = expand_trigger_description_constants($row['description'], $row);
        $hosts[zbx_strtolower($row['host'])] = $row['host'];
        // A little tricky check for attempt to overwrite active trigger (value=1) with
        // inactive or active trigger with lower priority.
        if (!isset($triggers[$row['description']][$row['host']]) || ($triggers[$row['description']][$row['host']]['value'] == TRIGGER_VALUE_FALSE && $row['value'] == TRIGGER_VALUE_TRUE || ($triggers[$row['description']][$row['host']]['value'] == TRIGGER_VALUE_FALSE || $row['value'] == TRIGGER_VALUE_TRUE) && $row['priority'] > $triggers[$row['description']][$row['host']]['priority'])) {
            $triggers[$row['description']][$row['host']] = array('hostid' => $row['hostid'], 'triggerid' => $row['triggerid'], 'value' => $row['value'], 'lastchange' => $row['lastchange'], 'priority' => $row['priority']);
        }
    }
    if (!isset($hosts)) {
        return $table;
    }
    ksort($hosts);
    $css = getUserTheme($USER_DETAILS);
    $vTextColor = $css == 'css_od.css' ? '&color=white' : '';
    if ($view_style == STYLE_TOP) {
        $header = array(new CCol(S_TRIGGERS, 'center'));
        foreach ($hosts as $hostname) {
            $header = array_merge($header, array(new CCol(array(new CImg('vtext.php?text=' . $hostname . $vTextColor)), 'hosts')));
        }
        $table->setHeader($header, 'vertical_header');
        foreach ($triggers as $descr => $trhosts) {
            $table_row = array(nbsp($descr));
            foreach ($hosts as $hostname) {
                $table_row = get_trigger_overview_cells($table_row, $trhosts, $hostname);
            }
            $table->addRow($table_row);
        }
    } else {
        $header = array(new CCol(S_HOSTS, 'center'));
        foreach ($triggers as $descr => $trhosts) {
            $descr = array(new CImg('vtext.php?text=' . $descr . $vTextColor));
            array_push($header, $descr);
        }
        $table->setHeader($header, 'vertical_header');
        foreach ($hosts as $hostname) {
            $table_row = array(nbsp($hostname));
            foreach ($triggers as $descr => $trhosts) {
                $table_row = get_trigger_overview_cells($table_row, $trhosts, $hostname);
            }
            $table->addRow($table_row);
        }
    }
    return $table;
}