/**
  * Resolve all kinds of macros in map labels.
  *
  * @param array  $selement
  * @param string $selement['label']						label to expand
  * @param int    $selement['elementtype']				element type
  * @param int    $selement['elementid']					element id
  * @param string $selement['elementExpressionTrigger']	if type is trigger, then trigger expression
  *
  * @return string
  */
 public function resolveMapLabelMacrosAll(array $selement)
 {
     $label = $selement['label'];
     // For host and trigger items expand macros if they exists.
     if (($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST || $selement['elementtype'] == SYSMAP_ELEMENT_TYPE_TRIGGER) && (strpos($label, 'HOST.NAME') !== false || strpos($label, 'HOSTNAME') !== false || strpos($label, 'HOST.HOST') !== false || strpos($label, 'HOST.DESCRIPTION') !== false || strpos($label, 'HOST.DNS') !== false || strpos($label, 'HOST.IP') !== false || strpos($label, 'IPADDRESS') !== false || strpos($label, 'HOST.CONN') !== false)) {
         // Priorities of interface types doesn't match interface type ids in DB.
         $priorities = [INTERFACE_TYPE_AGENT => 4, INTERFACE_TYPE_SNMP => 3, INTERFACE_TYPE_JMX => 2, INTERFACE_TYPE_IPMI => 1];
         // Get host data if element is host.
         if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST) {
             $res = DBselect('SELECT hi.ip,hi.dns,hi.useip,h.host,h.name,h.description,hi.type AS interfacetype' . ' FROM interface hi,hosts h' . ' WHERE hi.hostid=h.hostid' . ' AND hi.main=1 AND hi.hostid=' . zbx_dbstr($selement['elementid']));
             // Process interface priorities.
             $tmpPriority = 0;
             while ($dbHost = DBfetch($res)) {
                 if ($priorities[$dbHost['interfacetype']] > $tmpPriority) {
                     $resHost = $dbHost;
                     $tmpPriority = $priorities[$dbHost['interfacetype']];
                 }
             }
             $hostsByNr[''] = $resHost;
         } else {
             $res = DBselect('SELECT hi.ip,hi.dns,hi.useip,h.host,h.name,h.description,f.functionid,hi.type AS interfacetype' . ' FROM interface hi,items i,functions f,hosts h' . ' WHERE h.hostid=hi.hostid' . ' AND hi.hostid=i.hostid' . ' AND i.itemid=f.itemid' . ' AND hi.main=1 AND f.triggerid=' . zbx_dbstr($selement['elementid']) . ' ORDER BY f.functionid');
             // Process interface priorities, build $hostsByFunctionId array.
             $tmpFunctionId = -1;
             while ($dbHost = DBfetch($res)) {
                 if ($dbHost['functionid'] != $tmpFunctionId) {
                     $tmpPriority = 0;
                     $tmpFunctionId = $dbHost['functionid'];
                 }
                 if ($priorities[$dbHost['interfacetype']] > $tmpPriority) {
                     $hostsByFunctionId[$dbHost['functionid']] = $dbHost;
                     $tmpPriority = $priorities[$dbHost['interfacetype']];
                 }
             }
             // Get all function ids from expression and link host data against position in expression.
             preg_match_all('/\\{([0-9]+)\\}/', $selement['elementExpressionTrigger'], $matches);
             $hostsByNr = [];
             foreach ($matches[1] as $i => $functionid) {
                 if (isset($hostsByFunctionId[$functionid])) {
                     $hostsByNr[$i + 1] = $hostsByFunctionId[$functionid];
                 }
             }
             // For macro without numeric index.
             if (isset($hostsByNr[1])) {
                 $hostsByNr[''] = $hostsByNr[1];
             }
         }
         // Resolve functional macros like: {{HOST.HOST}:log[{HOST.HOST}.log].last(0)}.
         $label = $this->resolveMapLabelMacros($label, $hostsByNr);
         // Resolves basic macros.
         // $hostsByNr possible keys: '' and 1-9.
         foreach ($hostsByNr as $i => $host) {
             $replace = ['{HOST.NAME' . $i . '}' => $host['name'], '{HOSTNAME' . $i . '}' => $host['host'], '{HOST.HOST' . $i . '}' => $host['host'], '{HOST.DESCRIPTION' . $i . '}' => $host['description'], '{HOST.DNS' . $i . '}' => $host['dns'], '{HOST.IP' . $i . '}' => $host['ip'], '{IPADDRESS' . $i . '}' => $host['ip'], '{HOST.CONN' . $i . '}' => $host['useip'] ? $host['ip'] : $host['dns']];
             $label = str_replace(array_keys($replace), $replace, $label);
         }
     } else {
         // Resolve functional macros like: {sampleHostName:log[{HOST.HOST}.log].last(0)}, if no host provided.
         $label = $this->resolveMapLabelMacros($label);
     }
     // Resolve map specific processing consuming macros.
     switch ($selement['elementtype']) {
         case SYSMAP_ELEMENT_TYPE_HOST:
         case SYSMAP_ELEMENT_TYPE_MAP:
         case SYSMAP_ELEMENT_TYPE_TRIGGER:
         case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
             if (strpos($label, '{TRIGGERS.UNACK}') !== false) {
                 $label = str_replace('{TRIGGERS.UNACK}', get_triggers_unacknowledged($selement), $label);
             }
             if (strpos($label, '{TRIGGERS.PROBLEM.UNACK}') !== false) {
                 $label = str_replace('{TRIGGERS.PROBLEM.UNACK}', get_triggers_unacknowledged($selement, true), $label);
             }
             if (strpos($label, '{TRIGGER.EVENTS.UNACK}') !== false) {
                 $label = str_replace('{TRIGGER.EVENTS.UNACK}', get_events_unacknowledged($selement), $label);
             }
             if (strpos($label, '{TRIGGER.EVENTS.PROBLEM.UNACK}') !== false) {
                 $label = str_replace('{TRIGGER.EVENTS.PROBLEM.UNACK}', get_events_unacknowledged($selement, null, TRIGGER_VALUE_TRUE), $label);
             }
             if (strpos($label, '{TRIGGER.PROBLEM.EVENTS.PROBLEM.UNACK}') !== false) {
                 $label = str_replace('{TRIGGER.PROBLEM.EVENTS.PROBLEM.UNACK}', get_events_unacknowledged($selement, TRIGGER_VALUE_TRUE, TRIGGER_VALUE_TRUE), $label);
             }
             if (strpos($label, '{TRIGGERS.ACK}') !== false) {
                 $label = str_replace('{TRIGGERS.ACK}', get_triggers_unacknowledged($selement, null, true), $label);
             }
             if (strpos($label, '{TRIGGERS.PROBLEM.ACK}') !== false) {
                 $label = str_replace('{TRIGGERS.PROBLEM.ACK}', get_triggers_unacknowledged($selement, true, true), $label);
             }
             if (strpos($label, '{TRIGGER.EVENTS.ACK}') !== false) {
                 $label = str_replace('{TRIGGER.EVENTS.ACK}', get_events_unacknowledged($selement, null, null, true), $label);
             }
             if (strpos($label, '{TRIGGER.EVENTS.PROBLEM.ACK}') !== false) {
                 $label = str_replace('{TRIGGER.EVENTS.PROBLEM.ACK}', get_events_unacknowledged($selement, null, TRIGGER_VALUE_TRUE, true), $label);
             }
             if (strpos($label, '{TRIGGER.PROBLEM.EVENTS.PROBLEM.ACK}') !== false) {
                 $label = str_replace('{TRIGGER.PROBLEM.EVENTS.PROBLEM.ACK}', get_events_unacknowledged($selement, TRIGGER_VALUE_TRUE, TRIGGER_VALUE_TRUE, true), $label);
             }
             break;
     }
     return $label;
 }
Example #2
0
/**
 * Resolve macros and return expanded map label
 * @param array $selement
 * @return string
 */
function resolveMapLabelMacrosAll(array $selement)
{
    $label = $selement['label'];
    $resolveHostMacros = false;
    if (($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST || $selement['elementtype'] == SYSMAP_ELEMENT_TYPE_TRIGGER) && (zbx_strpos($label, '{HOSTNAME}') !== false || zbx_strpos($label, '{HOST.DNS}') !== false || zbx_strpos($label, '{IPADDRESS}') !== false || zbx_strpos($label, '{HOST.CONN}') !== false)) {
        $resolveHostMacros = true;
        if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST) {
            $sql = 'SELECT host, dns, ip, useip FROM hosts WHERE hostid=' . $selement['elementid'];
        } else {
            $sql = 'SELECT h.host, h.dns, h.ip, h.useip' . ' FROM hosts h, items i, functions f' . ' WHERE h.hostid=i.hostid' . ' AND i.itemid=f.itemid' . ' AND f.triggerid=' . $selement['elementid'];
        }
        $db_host = DBfetch(DBselect($sql));
    }
    $hostParam = $resolveHostMacros && $selement['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST ? $db_host['host'] : null;
    $label = resolveMapLabelMacros($label, $hostParam);
    if ($resolveHostMacros) {
        $replace = array('{HOSTNAME}' => $db_host['host'], '{HOST.DNS}' => $db_host['dns'], '{IPADDRESS}' => $db_host['ip'], '{HOST.CONN}' => $db_host['useip'] ? $db_host['ip'] : $db_host['dns']);
        $label = str_replace(array_keys($replace), $replace, $label);
    }
    switch ($selement['elementtype']) {
        case SYSMAP_ELEMENT_TYPE_HOST:
        case SYSMAP_ELEMENT_TYPE_MAP:
        case SYSMAP_ELEMENT_TYPE_TRIGGER:
        case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
            if (zbx_strpos($label, '{TRIGGERS.UNACK}') !== false) {
                $label = str_replace('{TRIGGERS.UNACK}', get_triggers_unacknowledged($selement), $label);
            }
            if (zbx_strpos($label, '{TRIGGERS.PROBLEM.UNACK}') !== false) {
                $label = str_replace('{TRIGGERS.PROBLEM.UNACK}', get_triggers_unacknowledged($selement, true), $label);
            }
            if (zbx_strpos($label, '{TRIGGER.EVENTS.UNACK}') !== false) {
                $label = str_replace('{TRIGGER.EVENTS.UNACK}', get_events_unacknowledged($selement), $label);
            }
            if (zbx_strpos($label, '{TRIGGER.EVENTS.PROBLEM.UNACK}') !== false) {
                $label = str_replace('{TRIGGER.EVENTS.PROBLEM.UNACK}', get_events_unacknowledged($selement, null, TRIGGER_VALUE_TRUE), $label);
            }
            if (zbx_strpos($label, '{TRIGGER.PROBLEM.EVENTS.PROBLEM.UNACK}') !== false) {
                $label = str_replace('{TRIGGER.PROBLEM.EVENTS.PROBLEM.UNACK}', get_events_unacknowledged($selement, TRIGGER_VALUE_TRUE, TRIGGER_VALUE_TRUE), $label);
            }
            if (zbx_strpos($label, '{TRIGGERS.ACK}') !== false) {
                $label = str_replace('{TRIGGERS.ACK}', get_triggers_unacknowledged($selement, null, true), $label);
            }
            if (zbx_strpos($label, '{TRIGGERS.PROBLEM.ACK}') !== false) {
                $label = str_replace('{TRIGGERS.PROBLEM.ACK}', get_triggers_unacknowledged($selement, true, true), $label);
            }
            if (zbx_strpos($label, '{TRIGGER.EVENTS.ACK}') !== false) {
                $label = str_replace('{TRIGGER.EVENTS.ACK}', get_events_unacknowledged($selement, null, null, true), $label);
            }
            if (zbx_strpos($label, '{TRIGGER.EVENTS.PROBLEM.ACK}') !== false) {
                $label = str_replace('{TRIGGER.EVENTS.PROBLEM.ACK}', get_events_unacknowledged($selement, null, TRIGGER_VALUE_TRUE, true), $label);
            }
            if (zbx_strpos($label, '{TRIGGER.PROBLEM.EVENTS.PROBLEM.ACK}') !== false) {
                $label = str_replace('{TRIGGER.PROBLEM.EVENTS.PROBLEM.ACK}', get_events_unacknowledged($selement, TRIGGER_VALUE_TRUE, TRIGGER_VALUE_TRUE, true), $label);
            }
            break;
    }
    return $label;
}