public function setUrl($value)
 {
     if (is_null($this->nosid)) {
         if (is_null($this->sid)) {
             $this->sid = isset($_COOKIE['zbx_sessionid']) ? substr($_COOKIE['zbx_sessionid'], 16, 16) : null;
         }
         if (!is_null($this->sid)) {
             $value .= zbx_strstr($value, '&') !== false || zbx_strstr($value, '?') !== false ? '&sid=' . $this->sid : '?sid=' . $this->sid;
         }
         $url = $value;
     } else {
         $url = $value;
     }
     $this->setAttribute('href', $url);
 }
Beispiel #2
0
function expand_trigger_description_by_data($row, $flag = ZBX_FLAG_TRIGGER)
{
    if ($row) {
        $description = expand_trigger_description_constants($row['description'], $row);
        if (is_null($row['host'])) {
            $row['host'] = '{HOSTNAME}';
        }
        $description = str_replace('{HOSTNAME}', $row['host'], $description);
        if (zbx_strstr($description, '{ITEM.LASTVALUE}')) {
            $functionid = trigger_get_N_functionid($row['expression'], 1);
            if (isset($functionid)) {
                $sql = 'SELECT i.lastvalue, i.value_type, i.itemid ' . ' FROM items i, functions f ' . ' WHERE i.itemid=f.itemid ' . ' AND f.functionid=' . $functionid;
                $row2 = DBfetch(DBselect($sql));
                if ($row2['value_type'] != ITEM_VALUE_TYPE_LOG) {
                    $description = str_replace('{ITEM.LASTVALUE}', $row2['lastvalue'], $description);
                } else {
                    $sql = 'SELECT MAX(clock) as max FROM history_log WHERE itemid=' . $row2['itemid'];
                    $row3 = DBfetch(DBselect($sql));
                    if ($row3 && !is_null($row3['max'])) {
                        $sql = 'SELECT value FROM history_log WHERE itemid=' . $row2['itemid'] . ' AND clock=' . $row3['max'];
                        $row4 = DBfetch(DBselect($sql));
                        $description = str_replace('{ITEM.LASTVALUE}', $row4['value'], $description);
                    }
                }
            }
        }
        if (zbx_strstr($description, '{ITEM.VALUE}')) {
            $value = $flag == ZBX_FLAG_TRIGGER ? trigger_get_func_value($row['expression'], ZBX_FLAG_TRIGGER, 1, 1) : trigger_get_func_value($row['expression'], ZBX_FLAG_EVENT, 1, $row['clock']);
            $description = str_replace('{ITEM.VALUE}', $value, $description);
        }
        for ($i = 1; $i < 10; $i++) {
            if (zbx_strstr($description, '{ITEM.VALUE' . $i . '}')) {
                $value = $flag == ZBX_FLAG_TRIGGER ? trigger_get_func_value($row['expression'], ZBX_FLAG_TRIGGER, $i, 1) : trigger_get_func_value($row['expression'], ZBX_FLAG_EVENT, $i, $row['clock']);
                $description = str_replace('{ITEM.VALUE' . $i . '}', $value, $description);
            }
        }
    } else {
        $description = '*ERROR*';
    }
    return $description;
}
Beispiel #3
0
function &DBselect($query, $limit = 'NO')
{
    global $DB;
    //COpt::savesqlrequest($query);
    $result = false;
    if (isset($DB['DB']) && !empty($DB['DB'])) {
        //SDI('SQL: '.$query);
        $DB['SELECT_COUNT']++;
        switch ($DB['TYPE']) {
            case 'MYSQL':
                if (zbx_numeric($limit)) {
                    $query .= ' limit ' . intval($limit);
                }
                $result = mysql_query($query, $DB['DB']);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . mysql_error() . ']');
                }
                break;
            case 'POSTGRESQL':
                if (zbx_numeric($limit)) {
                    $query .= ' limit ' . intval($limit);
                }
                $result = pg_query($DB['DB'], $query);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . pg_last_error() . ']');
                }
                break;
            case 'ORACLE':
                if (zbx_numeric($limit)) {
                    $query = 'select * from (' . $query . ') where rownum<=' . intval($limit);
                }
                $result = DBexecute($query);
                if (!$result) {
                    $e = ocierror($result);
                    error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']');
                }
                break;
            case 'SQLITE3':
                if (!$DB['TRANSACTIONS']) {
                    lock_db_access();
                }
                if (!($result = sqlite3_query($DB['DB'], $query))) {
                    error('Error in query [' . $query . '] [' . sqlite3_error($DB['DB']) . ']');
                } else {
                    $data = array();
                    while ($row = sqlite3_fetch_array($result)) {
                        foreach ($row as $id => $name) {
                            if (!zbx_strstr($id, '.')) {
                                continue;
                            }
                            $ids = explode('.', $id);
                            $row[array_pop($ids)] = $row[$id];
                            unset($row[$id]);
                        }
                        $data[] = $row;
                    }
                    sqlite3_query_close($result);
                    $result =& $data;
                }
                if (!$DB['TRANSACTIONS']) {
                    unlock_db_access();
                }
                break;
        }
        if ($DB['TRANSACTIONS'] && !$result) {
            $DB['TRANSACTION_STATE'] &= $result;
            //			SDI($query);
            //			SDI($DB['TRANSACTION_STATE']);
        }
    }
    return $result;
}
Beispiel #4
0
function calc_exp($fields, $field, $expression)
{
    //SDI("$field - expression: ".$expression);
    if (zbx_strstr($expression, '{}') && !isset($_REQUEST[$field])) {
        return FALSE;
    }
    if (zbx_strstr($expression, '{}') && !is_array($_REQUEST[$field])) {
        $expression = str_replace('{}', '$_REQUEST["' . $field . '"]', $expression);
    }
    if (zbx_strstr($expression, '{}') && is_array($_REQUEST[$field])) {
        foreach ($_REQUEST[$field] as $key => $val) {
            if (!ereg('^[a-zA-Z0-9_]+$', $key)) {
                return FALSE;
            }
            $expression2 = str_replace('{}', '$_REQUEST["' . $field . '"]["' . $key . '"]', $expression);
            if (calc_exp2($fields, $field, $expression2) == FALSE) {
                return FALSE;
            }
        }
        return TRUE;
    }
    //SDI("$field - expression: ".$expression);
    return calc_exp2($fields, $field, $expression);
}
Beispiel #5
0
function get_regexp_form()
{
    $frm_title = S_REGULAR_EXPRESSION;
    if (isset($_REQUEST['regexpid']) && !isset($_REQUEST["form_refresh"])) {
        $sql = 'SELECT re.* ' . ' FROM regexps re ' . ' WHERE ' . DBin_node('re.regexpid') . ' AND re.regexpid=' . $_REQUEST['regexpid'];
        $regexp = DBfetch(DBSelect($sql));
        $frm_title .= ' [' . $regexp['name'] . ']';
        $rename = $regexp['name'];
        $test_string = $regexp['test_string'];
        $expressions = array();
        $sql = 'SELECT e.* ' . ' FROM expressions e ' . ' WHERE ' . DBin_node('e.expressionid') . ' AND e.regexpid=' . $regexp['regexpid'] . ' ORDER BY e.expression_type';
        $db_exps = DBselect($sql);
        while ($exp = DBfetch($db_exps)) {
            $expressions[] = $exp;
        }
    } else {
        $rename = get_request('rename', '');
        $test_string = get_request('test_string', '');
        $expressions = get_request('expressions', array());
    }
    $tblRE = new CTable('', 'nowrap');
    $tblRE->addStyle('border-left: 1px #AAA solid; border-right: 1px #AAA solid; background-color: #EEE; padding: 2px; padding-left: 6px; padding-right: 6px;');
    $tblRE->addRow(array(S_NAME, new CTextBox('rename', $rename, 60)));
    $tblRE->addRow(array(S_TEST_STRING, new CTextArea('test_string', $test_string, 66, 5)));
    $tabExp = new CTableInfo();
    $td1 = new CCol(S_EXPRESSION);
    $td1->addStyle('background-color: #CCC;');
    $td2 = new CCol(S_EXPECTED_RESULT);
    $td2->addStyle('background-color: #CCC;');
    $td3 = new CCol(S_RESULT);
    $td3->addStyle('background-color: #CCC;');
    $tabExp->setHeader(array($td1, $td2, $td3));
    $final_result = !empty($test_string);
    foreach ($expressions as $id => $expression) {
        $results = array();
        $paterns = array($expression['expression']);
        if (!empty($test_string)) {
            if ($expression['expression_type'] == EXPRESSION_TYPE_ANY_INCLUDED) {
                $paterns = explode($expression['exp_delimiter'], $expression['expression']);
            }
            if (uint_in_array($expression['expression_type'], array(EXPRESSION_TYPE_TRUE, EXPRESSION_TYPE_FALSE))) {
                if ($expression['case_sensitive']) {
                    $results[$id] = ereg($paterns[0], $test_string);
                } else {
                    $results[$id] = eregi($paterns[0], $test_string);
                }
                if ($expression['expression_type'] == EXPRESSION_TYPE_TRUE) {
                    $final_result &= $results[$id];
                } else {
                    $final_result &= !$results[$id];
                }
            } else {
                $results[$id] = true;
                $tmp_result = false;
                if ($expression['case_sensitive']) {
                    foreach ($paterns as $pid => $patern) {
                        $tmp_result |= zbx_stristr($test_string, $patern) !== false;
                    }
                } else {
                    foreach ($paterns as $pid => $patern) {
                        $tmp_result |= zbx_strstr($test_string, $patern) !== false;
                    }
                }
                $results[$id] &= $tmp_result;
                $final_result &= $results[$id];
            }
        }
        if (isset($results[$id]) && $results[$id]) {
            $exp_res = new CSpan(S_TRUE_BIG, 'green bold');
        } else {
            $exp_res = new CSpan(S_FALSE_BIG, 'red bold');
        }
        $expec_result = expression_type2str($expression['expression_type']);
        if (EXPRESSION_TYPE_ANY_INCLUDED == $expression['expression_type']) {
            $expec_result .= ' (' . S_DELIMITER . "='" . $expression['exp_delimiter'] . "')";
        }
        $tabExp->addRow(array($expression['expression'], $expec_result, $exp_res));
    }
    $td = new CCol(S_COMBINED_RESULT, 'bold');
    $td->setColSpan(2);
    if ($final_result) {
        $final_result = new CSpan(S_TRUE_BIG, 'green bold');
    } else {
        $final_result = new CSpan(S_FALSE_BIG, 'red bold');
    }
    $tabExp->addRow(array($td, $final_result));
    $tblRE->addRow(array(S_RESULT, $tabExp));
    $tblFoot = new CTableInfo(null);
    $td = new CCol(array(new CButton('save', S_SAVE)));
    $td->setColSpan(2);
    $td->addStyle('text-align: right;');
    $td->addItem(SPACE);
    $td->addItem(new CButton('test', S_TEST));
    if (isset($_REQUEST['regexpid'])) {
        $td->addItem(SPACE);
        $td->addItem(new CButton('clone', S_CLONE));
        $td->addItem(SPACE);
        $td->addItem(new CButtonDelete(S_DELETE_REGULAR_EXPRESSION_Q, url_param('form') . url_param('config') . url_param('regexpid')));
    }
    $td->addItem(SPACE);
    $td->addItem(new CButtonCancel(url_param("regexpid")));
    $tblFoot->SetFooter($td);
    return array($tblRE, $tblFoot);
}
 /**
  * Matches expression as string.
  *
  * @static
  *
  * @param array $expression
  * @param string $string
  *
  * @return bool
  */
 private static function _matchString(array $expression, $string)
 {
     $result = true;
     if ($expression['expression_type'] == EXPRESSION_TYPE_ANY_INCLUDED) {
         $paterns = explode($expression['exp_delimiter'], $expression['expression']);
     } else {
         $paterns = array($expression['expression']);
     }
     $expectedResult = $expression['expression_type'] != EXPRESSION_TYPE_NOT_INCLUDED;
     if ($expression['case_sensitive']) {
         foreach ($paterns as $patern) {
             $result = $result && (zbx_strstr($string, $patern) !== false) == $expectedResult;
         }
     } else {
         foreach ($paterns as $patern) {
             $result = $result && (zbx_stristr($string, $patern) !== false) == $expectedResult;
         }
     }
     return $result;
 }
Beispiel #7
0
 function EndElement($parser, $name)
 {
     if (!$this->root) {
         return false;
     }
     global $USER_DETAILS;
     $data =& $this->data[$name];
     switch ($name) {
         case XML_TAG_HOST:
             if ($data['skip'] || !isset($data['hostid']) || !$data['hostid']) {
                 break;
             }
             // case
             if (!isset($data['port'])) {
                 $data['port'] = 10050;
             }
             if (!isset($data['status'])) {
                 $data['status'] = 0;
             }
             if (!isset($data['useip'])) {
                 $data['useip'] = 0;
             }
             if (!isset($data['dns'])) {
                 $data['dns'] = '';
             }
             if (!isset($data['ip'])) {
                 $data['ip'] = '';
             }
             if (!isset($data['proxy'])) {
                 $data['proxy'] = '';
             }
             if (!zbx_empty($data['proxy'])) {
                 $sql = 'SELECT hostid ' . ' FROM hosts ' . ' WHERE host=' . zbx_dbstr($data['proxy']) . ' AND status=' . HOST_STATUS_PROXY . ' AND ' . DBin_node('hostid', get_current_nodeid(false));
                 if ($host_data = DBfetch(DBselect($sql))) {
                     $data['proxy'] = $host_data['hostid'];
                 } else {
                     $data['proxy'] = 0;
                 }
             } else {
                 $data['proxy'] = 0;
             }
             if (update_host($data['hostid'], $data['name'], $data['port'], $data['status'], $data['useip'], $data['dns'], $data['ip'], $data['proxy'], $data['templates'], 'no', '', 623, -1, 2, '', '', null, $data['groups'])) {
                 info('Host [' . $data['name'] . '] updated');
             }
             break;
             // case
             // based on mod by scricca
         // case
         // based on mod by scricca
         case XML_TAG_HOSTPROFILE:
             if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) {
                 break;
             }
             //case
             if (!isset($data['devicetype'])) {
                 $data['devicetype'] = '';
             }
             if (!isset($data['name'])) {
                 $data['name'] = '';
             }
             if (!isset($data['os'])) {
                 $data['os'] = '';
             }
             if (!isset($data['serialno'])) {
                 $data['serialno'] = '';
             }
             if (!isset($data['tag'])) {
                 $data['tag'] = '';
             }
             if (!isset($data['macaddress'])) {
                 $data['macaddress'] = '';
             }
             if (!isset($data['hardware'])) {
                 $data['hardware'] = '';
             }
             if (!isset($data['software'])) {
                 $data['software'] = '';
             }
             if (!isset($data['contact'])) {
                 $data['contact'] = '';
             }
             if (!isset($data['location'])) {
                 $data['location'] = '';
             }
             if (!isset($data['notes'])) {
                 $data['notes'] = '';
             }
             delete_host_profile($this->data[XML_TAG_HOST]['hostid']);
             if (add_host_profile($this->data[XML_TAG_HOST]['hostid'], $data['devicetype'], $data['name'], $data['os'], $data['serialno'], $data['tag'], $data['macaddress'], $data['hardware'], $data['software'], $data['contact'], $data['location'], $data['notes'])) {
                 info('Host Profile [' . $this->data[XML_TAG_HOST]['name'] . '] updated');
             }
             break;
             // case
             //---
             // Extended profiles
         // case
         //---
         // Extended profiles
         case XML_TAG_HOSTPROFILE_EXT:
             if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) {
                 break;
             }
             //case
             if (!isset($data['device_alias'])) {
                 $data['device_alias'] = '';
             }
             if (!isset($data['device_type'])) {
                 $data['device_type'] = '';
             }
             if (!isset($data['device_chassis'])) {
                 $data['device_chassis'] = '';
             }
             if (!isset($data['device_os'])) {
                 $data['device_os'] = '';
             }
             if (!isset($data['device_os_short'])) {
                 $data['device_os_short'] = '';
             }
             if (!isset($data['device_hw_arch'])) {
                 $data['device_hw_arch'] = '';
             }
             if (!isset($data['device_serial'])) {
                 $data['device_serial'] = '';
             }
             if (!isset($data['device_model'])) {
                 $data['device_model'] = '';
             }
             if (!isset($data['device_tag'])) {
                 $data['device_tag'] = '';
             }
             if (!isset($data['device_vendor'])) {
                 $data['device_vendor'] = '';
             }
             if (!isset($data['device_contract'])) {
                 $data['device_contract'] = '';
             }
             if (!isset($data['device_who'])) {
                 $data['device_who'] = '';
             }
             if (!isset($data['device_status'])) {
                 $data['device_status'] = '';
             }
             if (!isset($data['device_app_01'])) {
                 $data['device_app_01'] = '';
             }
             if (!isset($data['device_app_02'])) {
                 $data['device_app_02'] = '';
             }
             if (!isset($data['device_app_03'])) {
                 $data['device_app_03'] = '';
             }
             if (!isset($data['device_app_04'])) {
                 $data['device_app_04'] = '';
             }
             if (!isset($data['device_app_05'])) {
                 $data['device_app_05'] = '';
             }
             if (!isset($data['device_url_1'])) {
                 $data['device_url_1'] = '';
             }
             if (!isset($data['device_url_2'])) {
                 $data['device_url_2'] = '';
             }
             if (!isset($data['device_url_3'])) {
                 $data['device_url_3'] = '';
             }
             if (!isset($data['device_networks'])) {
                 $data['device_networks'] = '';
             }
             if (!isset($data['device_notes'])) {
                 $data['device_notes'] = '';
             }
             if (!isset($data['device_hardware'])) {
                 $data['device_hardware'] = '';
             }
             if (!isset($data['device_software'])) {
                 $data['device_software'] = '';
             }
             if (!isset($data['ip_subnet_mask'])) {
                 $data['ip_subnet_mask'] = '';
             }
             if (!isset($data['ip_router'])) {
                 $data['ip_router'] = '';
             }
             if (!isset($data['ip_macaddress'])) {
                 $data['ip_macaddress'] = '';
             }
             if (!isset($data['oob_ip'])) {
                 $data['oob_ip'] = '';
             }
             if (!isset($data['oob_subnet_mask'])) {
                 $data['oob_subnet_mask'] = '';
             }
             if (!isset($data['oob_router'])) {
                 $data['oob_router'] = '';
             }
             if (!isset($data['date_hw_buy'])) {
                 $data['date_hw_buy'] = '';
             }
             if (!isset($data['date_hw_install'])) {
                 $data['date_hw_install'] = '';
             }
             if (!isset($data['date_hw_expiry'])) {
                 $data['date_hw_expiry'] = '';
             }
             if (!isset($data['date_hw_decomm'])) {
                 $data['date_hw_decomm'] = '';
             }
             if (!isset($data['site_street_1'])) {
                 $data['site_street_1'] = '';
             }
             if (!isset($data['site_street_2'])) {
                 $data['site_street_2'] = '';
             }
             if (!isset($data['site_street_3'])) {
                 $data['site_street_3'] = '';
             }
             if (!isset($data['site_city'])) {
                 $data['site_city'] = '';
             }
             if (!isset($data['site_state'])) {
                 $data['site_state'] = '';
             }
             if (!isset($data['site_country'])) {
                 $data['site_country'] = '';
             }
             if (!isset($data['site_zip'])) {
                 $data['site_zip'] = '';
             }
             if (!isset($data['site_rack'])) {
                 $data['site_rack'] = '';
             }
             if (!isset($data['site_notes'])) {
                 $data['site_notes'] = '';
             }
             if (!isset($data['poc_1_name'])) {
                 $data['poc_1_name'] = '';
             }
             if (!isset($data['poc_1_email'])) {
                 $data['poc_1_email'] = '';
             }
             if (!isset($data['poc_1_phone_1'])) {
                 $data['poc_1_phone_1'] = '';
             }
             if (!isset($data['poc_1_phone_2'])) {
                 $data['poc_1_phone_2'] = '';
             }
             if (!isset($data['poc_1_cell'])) {
                 $data['poc_1_cell'] = '';
             }
             if (!isset($data['poc_1_screen'])) {
                 $data['poc_1_screen'] = '';
             }
             if (!isset($data['poc_1_notes'])) {
                 $data['poc_1_notes'] = '';
             }
             if (!isset($data['poc_2_name'])) {
                 $data['poc_2_name'] = '';
             }
             if (!isset($data['poc_2_email'])) {
                 $data['poc_2_email'] = '';
             }
             if (!isset($data['poc_2_phone_1'])) {
                 $data['poc_2_phone_1'] = '';
             }
             if (!isset($data['poc_2_phone_2'])) {
                 $data['poc_2_phone_2'] = '';
             }
             if (!isset($data['poc_2_cell'])) {
                 $data['poc_2_cell'] = '';
             }
             if (!isset($data['poc_2_screen'])) {
                 $data['poc_2_screen'] = '';
             }
             if (!isset($data['poc_2_notes'])) {
                 $data['poc_2_notes'] = '';
             }
             delete_host_profile_ext($this->data[XML_TAG_HOST]['hostid']);
             if (add_host_profile_ext($this->data[XML_TAG_HOST]['hostid'], $data)) {
                 info('Host Extended Profile [' . $this->data[XML_TAG_HOST]['name'] . '] updated');
             }
             break;
             // case
             //---
         // case
         //---
         case XML_TAG_GROUP:
             if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) {
                 break;
             }
             //case
             $sql = 'SELECT groupid, name ' . ' FROM groups' . ' WHERE ' . DBin_node('groupid', get_current_nodeid(false)) . ' AND name=' . zbx_dbstr($this->element_data);
             if (!($group = DBfetch(DBselect($sql)))) {
                 error('Missing group [' . $this->element_data . ']');
                 break;
                 // case
             }
             if (!isset($this->available_groups[$group['groupid']])) {
                 error('Group [' . $this->element_data . '] skipped - Access deny.');
                 break;
                 // case
             }
             $this->data[XML_TAG_HOST]['groups'][$group['groupid']] = $group['groupid'];
             break;
             // case
         // case
         case XML_TAG_DEPENDENCY:
             if (!isset($data['triggerid_down']) || !$data['triggerid_down']) {
                 break;
             }
             // case
             update_trigger($data['triggerid_down'], null, null, null, null, null, null, null, $data['triggerid_up'], null);
             break;
             // case
         // case
         case XML_TAG_DEPENDS:
             if (!isset($this->data[XML_TAG_DEPENDENCY]['triggerid_down']) || !$this->data[XML_TAG_DEPENDENCY]['triggerid_down']) {
                 break;
             }
             //case
             if (!($trigger_up = get_trigger_by_description($this->element_data))) {
                 break;
             }
             array_push($this->data[XML_TAG_DEPENDENCY]['triggerid_up'], $trigger_up['triggerid']);
             break;
             // case
         // case
         case XML_TAG_APPLICATION:
             if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) {
                 break;
             }
             //case
             if (!isset($this->data[XML_TAG_ITEM])) {
                 break;
             }
             //case
             $sql = 'SELECT applicationid ' . ' FROM applications' . ' WHERE ' . DBin_node('applicationid', get_current_nodeid(false)) . ' AND name=' . zbx_dbstr($this->element_data) . ' AND hostid=' . $this->data[XML_TAG_HOST]['hostid'];
             if (!($application = DBfetch(DBselect($sql)))) {
                 $applicationid = add_application($this->element_data, $this->data[XML_TAG_HOST]['hostid']);
             } else {
                 $applicationid = $application['applicationid'];
             }
             $this->data[XML_TAG_ITEM]['applications'][] = $applicationid;
             break;
             // case
         // case
         case XML_TAG_TEMPLATE:
             if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) {
                 break;
             }
             //case
             $sql = 'SELECT DISTINCT host, hostid ' . ' FROM hosts' . ' WHERE ' . DBin_node('hostid') . ' AND host=' . zbx_dbstr($this->element_data) . ' AND status IN (' . HOST_STATUS_MONITORED . ',' . HOST_STATUS_NOT_MONITORED . ',' . HOST_STATUS_TEMPLATE . ')';
             if (!($template = DBfetch(DBselect($sql)))) {
                 error('Missing template [' . $this->element_data . ']');
                 break;
                 // case
             }
             if (!isset($this->available_hosts[$template['hostid']])) {
                 error('Template [' . $this->element_data . '] skipped - Access deny.');
                 break;
                 // case
             }
             $this->data[XML_TAG_HOST]['templates'][$template["hostid"]] = $template['host'];
             break;
             // case
         // case
         case XML_TAG_ITEM:
             if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) {
                 if (isset($this->data[XML_TAG_HOST]['skip']) && $this->data[XML_TAG_HOST]['skip']) {
                     info('Item [' . $data['description'] . '] skipped - user rule for host');
                     break;
                     // case
                 }
                 error('Item [' . $data['description'] . '] skipped - missing host');
                 break;
                 // case
             }
             if (!isset($data['description'])) {
                 $data['description'] = '';
             }
             if (!isset($data['delay'])) {
                 $data['delay'] = 30;
             }
             if (!isset($data['history'])) {
                 $data['history'] = 90;
             }
             if (!isset($data['trends'])) {
                 $data['trends'] = 365;
             }
             if (!isset($data['status'])) {
                 $data['status'] = 0;
             }
             if (!isset($data['units'])) {
                 $data['units'] = '';
             }
             if (!isset($data['multiplier'])) {
                 $data['multiplier'] = 0;
             }
             if (!isset($data['delta'])) {
                 $data['delta'] = 0;
             }
             if (!isset($data['formula'])) {
                 $data['formula'] = '';
             }
             if (!isset($data['lastlogsize'])) {
                 $data['lastlogsize'] = 0;
             }
             if (!isset($data['logtimefmt'])) {
                 $data['logtimefmt'] = '';
             }
             if (!isset($data['delay_flex'])) {
                 $data['delay_flex'] = '';
             }
             if (!isset($data['trapper_hosts'])) {
                 $data['trapper_hosts'] = '';
             }
             if (!isset($data['snmp_community'])) {
                 $data['snmp_community'] = '';
             }
             if (!isset($data['snmp_oid'])) {
                 $data['snmp_oid'] = '';
             }
             if (!isset($data['snmp_port'])) {
                 $data['snmp_port'] = 161;
             }
             if (!isset($data['snmpv3_securityname'])) {
                 $data['snmpv3_securityname'] = '';
             }
             if (!isset($data['snmpv3_securitylevel'])) {
                 $data['snmpv3_securitylevel'] = 0;
             }
             if (!isset($data['snmpv3_authpassphrase'])) {
                 $data['snmpv3_authpassphrase'] = '';
             }
             if (!isset($data['snmpv3_privpassphrase'])) {
                 $data['snmpv3_privpassphrase'] = '';
             }
             if (!isset($data['valuemap'])) {
                 $data['valuemap'] = '';
             }
             if (!isset($data['params'])) {
                 $data['params'] = '';
             }
             if (!isset($data['ipmi_sensor'])) {
                 $data['ipmi_sensor'] = '';
             }
             if (!isset($data['applications'])) {
                 $data['applications'] = array();
             }
             if (!empty($data['valuemap'])) {
                 $sql = 'SELECT valuemapid ' . ' FROM valuemaps ' . ' WHERE ' . DBin_node('valuemapid', get_current_nodeid(false)) . ' AND name=' . zbx_dbstr($data['valuemap']);
                 if ($valuemap = DBfetch(DBselect($sql))) {
                     $data['valuemapid'] = $valuemap['valuemapid'];
                 } else {
                     $data['valuemapid'] = add_valuemap($data['valuemap'], array());
                 }
             }
             $sql = 'SELECT itemid,valuemapid,templateid ' . ' FROM items ' . ' WHERE key_=' . zbx_dbstr($data['key']) . ' AND hostid=' . $this->data[XML_TAG_HOST]['hostid'] . ' AND ' . DBin_node('itemid', get_current_nodeid(false));
             if ($item = DBfetch(DBselect($sql))) {
                 /* exist */
                 if ($this->item['exist'] == 1) {
                     info('Item [' . $data['description'] . '] skipped - user rule');
                     break;
                 }
                 if (!isset($data['valuemapid'])) {
                     $data['valuemapid'] = $item['valuemapid'];
                 }
                 $data['key_'] = $data['key'];
                 $data['hostid'] = $this->data[XML_TAG_HOST]['hostid'];
                 $data['applications'] = array_unique(array_merge($data['applications'], get_applications_by_itemid($item['itemid'])));
                 $data['templateid'] = $item['templateid'];
                 check_db_fields($item, $data);
                 update_item($item['itemid'], $data);
             } else {
                 /* missed */
                 if ($this->item['missed'] == 1) {
                     info('Item [' . $data['description'] . '] skipped - user rule');
                     break;
                     // case
                 }
                 if (!isset($data['valuemapid'])) {
                     $data['valuemapid'] = 0;
                 }
                 $data['hostid'] = $this->data[XML_TAG_HOST]['hostid'];
                 $data['key_'] = $data['key'];
                 add_item($data);
             }
             break;
             // case
         // case
         case XML_TAG_TRIGGER:
             if (!isset($data['expression'])) {
                 $data['expression'] = '';
             }
             if (!isset($data['description'])) {
                 $data['description'] = '';
             }
             if (!isset($data['type'])) {
                 $data['type'] = 0;
             }
             if (!isset($data['priority'])) {
                 $data['priority'] = 0;
             }
             if (!isset($data['status'])) {
                 $data['status'] = 0;
             }
             if (!isset($data['comments'])) {
                 $data['comments'] = '';
             }
             if (!isset($data['url'])) {
                 $data['url'] = '';
             }
             if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) {
                 if (isset($this->data[XML_TAG_HOST]['skip']) && $this->data[XML_TAG_HOST]['skip']) {
                     // remember skipped triggers for dependencies
                     $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'] . ':' . $data['description'];
                     info('Trigger [' . $data['description'] . '] skipped - user rule for host');
                     break;
                     // case
                 }
                 if (zbx_strstr($data['expression'], '{HOSTNAME}')) {
                     // remember skipped triggers for dependencies
                     $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'] . ':' . $data['description'];
                     error('Trigger [' . $data['description'] . '] skipped - missing host');
                     break;
                     // case
                 }
             } else {
                 $data['expression'] = str_replace('{{HOSTNAME}:', '{' . $this->data[XML_TAG_HOST]['name'] . ':', $data['expression']);
                 $result = DBselect('SELECT DISTINCT t.triggerid,t.templateid,t.expression ' . ' FROM triggers t,functions f,items i ' . ' WHERE t.triggerid=f.triggerid ' . ' AND f.itemid=i.itemid' . ' AND i.hostid=' . $this->data[XML_TAG_HOST]['hostid'] . ' AND t.description=' . zbx_dbstr($data['description']));
                 while ($trigger = DBfetch($result)) {
                     if (explode_exp($trigger['expression'], 0) == $data['expression']) {
                         break;
                         // while
                     }
                 }
                 if (!empty($trigger)) {
                     /* exist */
                     if ($this->trigger['exist'] == 1) {
                         /* skip */
                         // remember skipped triggers for dependencies
                         $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'] . ':' . $data['description'];
                         info('Trigger [' . $data['description'] . '] skipped - user rule');
                         break;
                         // case
                     }
                     update_trigger($trigger['triggerid'], $data['expression'], $data['description'], $data['type'], $data['priority'], $data['status'], $data['comments'], $data['url'], get_trigger_dependencies_by_triggerid($trigger['triggerid']), $trigger['templateid']);
                     break;
                     // case
                 } else {
                     /* missed */
                     // continue [add_trigger]
                 }
             }
             if ($this->trigger['missed'] == 1) {
                 // remember skipped triggers for dependencies
                 $this->data[XML_TAG_DEPENDENCIES]['skip'][] = $this->data[XML_TAG_HOST]['name'] . ':' . $data['description'];
                 info('Trigger [' . $data['description'] . '] skipped - user rule');
                 break;
                 // case
             }
             add_trigger($data['expression'], $data['description'], $data['type'], $data['priority'], $data['status'], $data['comments'], $data['url']);
             break;
             // case
         // case
         case XML_TAG_GRAPH:
             if (isset($data['error'])) {
                 error('Graph [' . $data['name'] . '] skipped - error occured');
                 break;
                 // case
             }
             if (!isset($data['ymin_type'])) {
                 $data['ymin_type'] = 0;
             }
             if (!isset($data['ymax_type'])) {
                 $data['ymax_type'] = 0;
             }
             if (!isset($data['ymin_item_key'])) {
                 $data['ymin_item_key'] = '';
             }
             if (!isset($data['ymax_item_key'])) {
                 $data['ymax_item_key'] = '';
             }
             if (!isset($data['ymin_itemid'])) {
                 $data['ymin_itemid'] = 0;
             }
             if (!isset($data['ymax_itemid'])) {
                 $data['ymax_itemid'] = 0;
             }
             if (!isset($data['show_work_period'])) {
                 $data['show_work_period'] = 1;
             }
             if (!isset($data['show_triggers'])) {
                 $data['show_triggers'] = 1;
             }
             if (!isset($data['graphtype'])) {
                 $data['graphtype'] = 0;
             }
             if (!isset($data['yaxismin'])) {
                 $data['yaxismin'] = 0;
             }
             if (!isset($data['yaxismax'])) {
                 $data['yaxismax'] = 0;
             }
             if (!isset($data['show_legend'])) {
                 $data['show_legend'] = 0;
             }
             if (!isset($data['show_3d'])) {
                 $data['show_3d'] = 0;
             }
             if (!isset($data['percent_left'])) {
                 $data['percent_left'] = 0;
             }
             if (!isset($data['percent_right'])) {
                 $data['percent_right'] = 0;
             }
             if (!isset($data['items'])) {
                 $data['items'] = array();
             }
             if (!empty($data['ymin_item_key'])) {
                 $data['ymin_item_key'] = explode(':', $data['ymin_item_key']);
                 if (count($data['ymin_item_key']) < 2) {
                     $this->data[XML_TAG_GRAPH]['error'] = true;
                     error('Incorrect y min item for graph [' . $data['name'] . ']');
                     break;
                     // case
                 }
                 $data['host'] = array_shift($data['ymin_item_key']);
                 $data['ymin_item_key'] = implode(':', $data['ymin_item_key']);
                 if (!($item = get_item_by_key($data['ymin_item_key'], $data['host']))) {
                     $this->data[XML_TAG_GRAPH]['error'] = true;
                     error('Missed item [' . $data['ymin_item_key'] . '] for host [' . $data['host'] . ']');
                     break;
                     // case
                 }
                 $data['ymin_itemid'] = $item['itemid'];
             }
             if (!empty($data['ymax_item_key'])) {
                 $data['ymax_item_key'] = explode(':', $data['ymax_item_key']);
                 if (count($data['ymax_item_key']) < 2) {
                     $this->data[XML_TAG_GRAPH]['error'] = true;
                     error('Incorrect y max item for graph [' . $data['name'] . ']');
                     break;
                     // case
                 }
                 $data['host'] = array_shift($data['ymax_item_key']);
                 $data['ymax_item_key'] = implode(':', $data['ymax_item_key']);
                 if (!($item = get_item_by_key($data['ymax_item_key'], $data['host']))) {
                     $this->data[XML_TAG_GRAPH]['error'] = true;
                     error('Missed item [' . $data['ymax_item_key'] . '] for host [' . $data['host'] . ']');
                     break;
                     // case
                 }
                 $data['ymax_itemid'] = $item['itemid'];
             }
             if (!isset($this->data[XML_TAG_HOST]['hostid']) || !$this->data[XML_TAG_HOST]['hostid']) {
                 if (isset($this->data[XML_TAG_HOST]['skip']) && $this->data[XML_TAG_HOST]['skip']) {
                     info('Graph [' . $data['name'] . '] skipped - user rule for host');
                     break;
                     // case
                 }
                 foreach ($data['items'] as $id) {
                     if (zbx_strstr($data['name'], '{HOSTNAME}')) {
                         error('Graph [' . $data['name'] . '] skipped - missing host');
                         break;
                         // case
                     }
                 }
             } else {
                 if ($graph = DBfetch(DBselect('SELECT DISTINCT g.graphid, g.templateid' . ' FROM graphs g, graphs_items gi, items i' . ' WHERE g.graphid=gi.graphid ' . ' AND gi.itemid=i.itemid' . ' AND g.name=' . zbx_dbstr($data['name']) . ' AND i.hostid=' . $this->data[XML_TAG_HOST]['hostid']))) {
                     /* exist */
                     if ($this->graph['exist'] == 1) {
                         /* skip */
                         info('Graph [' . $data['name'] . '] skipped - user rule');
                         break;
                         // case
                     }
                     $data['graphid'] = $graph['graphid'];
                     update_graph_with_items($data['graphid'], $data['name'], $data['width'], $data['height'], $data['ymin_type'], $data['ymax_type'], $data['yaxismin'], $data['yaxismax'], $data['ymin_itemid'], $data['ymax_itemid'], $data['show_work_period'], $data['show_triggers'], $data['graphtype'], $data['show_legend'], $data['show_3d'], $data['percent_left'], $data['percent_right'], $data['items'], $graph['templateid']);
                 } else {
                     /* missed */
                     // continue [add_group]
                 }
             }
             if (!isset($data['graphid'])) {
                 if ($this->graph['missed'] == 1) {
                     /* skip */
                     info('Graph [' . $data['name'] . '] skipped - user rule');
                     break;
                     // case
                 }
                 $data['graphid'] = add_graph_with_items($data['name'], $data['width'], $data['height'], $data['ymin_type'], $data['ymax_type'], $data['yaxismin'], $data['yaxismax'], $data['ymin_itemid'], $data['ymax_itemid'], $data['show_work_period'], $data['show_triggers'], $data['graphtype'], $data['show_legend'], $data['show_3d'], $data['percent_left'], $data['percent_right'], $data['items']);
             }
             break;
             // case
         // case
         case XML_TAG_GRAPH_ELEMENT:
             if (!isset($this->data[XML_TAG_GRAPH])) {
                 break;
             }
             // case
             $data['key'] = explode(':', $data['item']);
             if (count($data['key']) < 2) {
                 $this->data[XML_TAG_GRAPH]['error'] = true;
                 error('Incorrect element for graph [' . $data['name'] . ']');
                 break;
                 // case
             }
             $data['host'] = array_shift($data['key']);
             $data['key'] = implode(':', $data['key']);
             if (isset($this->data[XML_TAG_HOST]['name'])) {
                 $data['host'] = str_replace('{HOSTNAME}', $this->data[XML_TAG_HOST]['name'], $data['host']);
             }
             if (!isset($data['drawtype'])) {
                 $data['drawtype'] = 0;
             }
             if (!isset($data['sortorder'])) {
                 $data['sortorder'] = 0;
             }
             if (!isset($data['color'])) {
                 $data['color'] = 'Dark Green';
             }
             if (!isset($data['yaxisside'])) {
                 $data['yaxisside'] = 1;
             }
             if (!isset($data['calc_fnc'])) {
                 $data['calc_fnc'] = 2;
             }
             if (!isset($data['type'])) {
                 $data['type'] = 0;
             }
             if (!isset($data['periods_cnt'])) {
                 $data['periods_cnt'] = 5;
             }
             if (!($item = get_item_by_key($data['key'], $data['host']))) {
                 $this->data[XML_TAG_GRAPH]['error'] = true;
                 error('Missing item [' . $data['key'] . '] for host [' . $data['host'] . ']');
                 break;
                 // case
             }
             $data['itemid'] = $item['itemid'];
             array_push($this->data[XML_TAG_GRAPH]['items'], $data);
             break;
             // case
             /*case XML_TAG_SCREEN:
             		case XML_TAG_SCREEN_ELEMENT:
             			break; // case*/
         // case
         /*case XML_TAG_SCREEN:
         		case XML_TAG_SCREEN_ELEMENT:
         			break; // case*/
         default:
             if (isset($this->sub_node) && isset($this->main_node)) {
                 $main_node = array_pop($this->main_node);
                 $this->data[$main_node][$this->sub_node] = $this->element_data;
                 array_push($this->main_node, $main_node);
             }
             $this->sub_node = null;
             return;
     }
     unset($this->data[$name], $data);
     array_pop($this->main_node);
 }
function calc_exp($fields, $field, $expression)
{
    if (zbx_strstr($expression, '{}')) {
        if (!isset($_REQUEST[$field])) {
            return false;
        }
        if (!is_array($_REQUEST[$field])) {
            $expression = str_replace('{}', '$_REQUEST["' . $field . '"]', $expression);
        }
        if (is_array($_REQUEST[$field])) {
            foreach ($_REQUEST[$field] as $key => $val) {
                if (!preg_match('/^([a-zA-Z0-9_]+)$/', $key)) {
                    return false;
                }
                if (!calc_exp2($fields, str_replace('{}', '$_REQUEST["' . $field . '"]["' . $key . '"]', $expression))) {
                    return false;
                }
            }
            return true;
        }
    }
    return calc_exp2($fields, $expression);
}
Beispiel #9
0
function expand_host_ipmi_ip_by_data($ipmi_ip, $host)
{
    if (zbx_strstr($ipmi_ip, '{HOSTNAME}')) {
        $ipmi_ip = str_replace('{HOSTNAME}', $host['host'], $ipmi_ip);
    } else {
        if (zbx_strstr($ipmi_ip, '{IPADDRESS}')) {
            $ipmi_ip = str_replace('{IPADDRESS}', $host['ip'], $ipmi_ip);
        } else {
            if (zbx_strstr($ipmi_ip, '{HOST.DNS}')) {
                $ipmi_ip = str_replace('{HOST.DNS}', $host['dns'], $ipmi_ip);
            } else {
                if (zbx_strstr($ipmi_ip, '{HOST.CONN}')) {
                    $ipmi_ip = str_replace('{HOST.CONN}', $host['useip'] ? $host['ip'] : $host['dns'], $ipmi_ip);
                }
            }
        }
    }
    return $ipmi_ip;
}
Beispiel #10
0
function expand_item_key_by_data($item)
{
    $key = $item['key_'];
    if (zbx_strstr($key, '{HOSTNAME}')) {
        $host = get_host_by_itemid($item['itemid']);
        $key = str_replace('{HOSTNAME}', $host['host'], $key);
    } else {
        if (zbx_strstr($key, '{IPADDRESS}')) {
            $host = get_host_by_itemid($item['itemid']);
            $key = str_replace('{IPADDRESS}', $host['ip'], $key);
        } else {
            if (zbx_strstr($key, '{HOST.DNS}')) {
                $host = get_host_by_itemid($item['itemid']);
                $key = str_replace('{HOST.DNS}', $host['dns'], $key);
            } else {
                if (zbx_strstr($key, '{HOST.CONN}')) {
                    $host = get_host_by_itemid($item['itemid']);
                    $key = str_replace('{HOST.CONN}', $host['useip'] ? $host['ip'] : $host['dns'], $key);
                }
            }
        }
    }
    return $key;
}
function get_regexp_form()
{
    if (isset($_REQUEST['regexpid']) && !isset($_REQUEST['form_refresh'])) {
        $sql = 'SELECT re.* ' . ' FROM regexps re ' . ' WHERE ' . DBin_node('re.regexpid') . ' AND re.regexpid=' . zbx_dbstr($_REQUEST['regexpid']);
        $regexp = DBfetch(DBSelect($sql));
        $rename = $regexp['name'];
        $test_string = $regexp['test_string'];
        $expressions = array();
        $sql = 'SELECT e.* ' . ' FROM expressions e ' . ' WHERE ' . DBin_node('e.expressionid') . ' AND e.regexpid=' . zbx_dbstr($regexp['regexpid']) . ' ORDER BY e.expression_type';
        $db_exps = DBselect($sql);
        while ($exp = DBfetch($db_exps)) {
            $expressions[] = $exp;
        }
    } else {
        $rename = get_request('rename', '');
        $test_string = get_request('test_string', '');
        $expressions = get_request('expressions', array());
    }
    $tblRE = new CTable('', 'formtable nowrap');
    $tblRE->addRow(array(_('Name'), new CTextBox('rename', $rename, 60, 'no', 128)));
    $tblRE->addRow(array(_('Test string'), new CTextArea('test_string', $test_string)));
    $tabExp = new CTableInfo();
    $td1 = new CCol(_('Expression'));
    $td2 = new CCol(_('Expected result'));
    $td3 = new CCol(_('Result'));
    $tabExp->setHeader(array($td1, $td2, $td3));
    $final_result = !empty($test_string);
    foreach ($expressions as $id => $expression) {
        $results = array();
        $paterns = array($expression['expression']);
        if (!empty($test_string)) {
            if ($expression['expression_type'] == EXPRESSION_TYPE_ANY_INCLUDED) {
                $paterns = explode($expression['exp_delimiter'], $expression['expression']);
            }
            if (uint_in_array($expression['expression_type'], array(EXPRESSION_TYPE_TRUE, EXPRESSION_TYPE_FALSE))) {
                if ($expression['case_sensitive']) {
                    $results[$id] = preg_match('/' . $paterns[0] . '/', $test_string);
                } else {
                    $results[$id] = preg_match('/' . $paterns[0] . '/i', $test_string);
                }
                if ($expression['expression_type'] == EXPRESSION_TYPE_TRUE) {
                    $final_result &= $results[$id];
                } else {
                    $final_result &= !$results[$id];
                }
            } else {
                $results[$id] = true;
                $tmp_result = false;
                if ($expression['case_sensitive']) {
                    foreach ($paterns as $pid => $patern) {
                        $tmp_result |= zbx_strstr($test_string, $patern) !== false;
                    }
                } else {
                    foreach ($paterns as $pid => $patern) {
                        $tmp_result |= zbx_stristr($test_string, $patern) !== false;
                    }
                }
                if (uint_in_array($expression['expression_type'], array(EXPRESSION_TYPE_INCLUDED, EXPRESSION_TYPE_ANY_INCLUDED))) {
                    $results[$id] &= $tmp_result;
                } else {
                    if ($expression['expression_type'] == EXPRESSION_TYPE_NOT_INCLUDED) {
                        $results[$id] &= !$tmp_result;
                    }
                }
                $final_result &= $results[$id];
            }
        }
        if (isset($results[$id]) && $results[$id]) {
            $exp_res = new CSpan(_('TRUE'), 'green bold');
        } else {
            $exp_res = new CSpan(_('FALSE'), 'red bold');
        }
        $expec_result = expression_type2str($expression['expression_type']);
        if (EXPRESSION_TYPE_ANY_INCLUDED == $expression['expression_type']) {
            $expec_result .= ' (' . _('Delimiter') . "='" . $expression['exp_delimiter'] . "')";
        }
        $tabExp->addRow(array($expression['expression'], $expec_result, $exp_res));
    }
    $td = new CCol(_('Combined result'), 'bold');
    $td->setColSpan(2);
    if ($final_result) {
        $final_result = new CSpan(_('TRUE'), 'green bold');
    } else {
        $final_result = new CSpan(_('FALSE'), 'red bold');
    }
    $tabExp->addRow(array($td, $final_result));
    $tblRE->addRow(array(_('Result'), $tabExp));
    $tblFoot = new CTableInfo(null);
    $td = new CCol(array(new CSubmit('save', _('Save'))));
    $td->setColSpan(2);
    $td->addStyle('text-align: right;');
    $td->addItem(SPACE);
    $td->addItem(new CSubmit('test', _('Test')));
    if (isset($_REQUEST['regexpid'])) {
        $td->addItem(SPACE);
        $td->addItem(new CSubmit('clone', _('Clone')));
        $td->addItem(SPACE);
        $td->addItem(new CButtonDelete(_('Delete regular expression?'), url_param('form') . url_param('config') . url_param('regexpid') . url_param('delete', false, 'go')));
    }
    $td->addItem(SPACE);
    $td->addItem(new CButtonCancel(url_param("regexpid")));
    $tblFoot->setFooter($td);
    return array($tblRE, $tblFoot);
}
Beispiel #12
0
function expand_map_element_label_by_data($db_element)
{
    $label = $db_element['label'];
    if (zbx_strstr($label, '{HOSTNAME}') || zbx_strstr($label, '{HOST.DNS}') || zbx_strstr($label, '{IPADDRESS}') || zbx_strstr($label, '{HOST.CONN}')) {
        if ($db_element['elementtype'] == SYSMAP_ELEMENT_TYPE_HOST) {
            $sql = 'select * from hosts where hostid=' . $db_element['elementid'];
        } else {
            if ($db_element['elementtype'] == SYSMAP_ELEMENT_TYPE_TRIGGER) {
                $sql = 'select h.* from hosts h,items i,functions f' . ' where h.hostid=i.hostid and i.itemid=f.itemid and f.triggerid=' . $db_element['elementid'];
            } else {
                return $label;
            }
        }
        $db_hosts = DBselect($sql);
        if ($db_host = DBfetch($db_hosts)) {
            if (zbx_strstr($label, '{HOSTNAME}')) {
                $label = str_replace('{HOSTNAME}', $db_host['host'], $label);
            }
            if (zbx_strstr($label, '{HOST.DNS}')) {
                $label = str_replace('{HOST.DNS}', $db_host['dns'], $label);
            }
            if (zbx_strstr($label, '{IPADDRESS}')) {
                $label = str_replace('{IPADDRESS}', $db_host['ip'], $label);
            }
            if (zbx_strstr($label, '{HOST.CONN}')) {
                $label = str_replace('{HOST.CONN}', $db_host['useip'] ? $db_host['ip'] : $db_host['dns'], $label);
            }
        }
    }
    while (FALSE !== ($pos = strpos($label, '{'))) {
        $expr = substr($label, $pos);
        if (FALSE === ($pos = strpos($expr, '}'))) {
            break;
        }
        $expr = substr($expr, 1, $pos - 1);
        if (FALSE === ($pos = strpos($expr, ':'))) {
            $label = str_replace('{' . $expr . '}', '???', $label);
            continue;
        }
        $host = substr($expr, 0, $pos);
        $key = substr($expr, $pos + 1);
        if (FALSE === ($pos = strrpos($key, '.'))) {
            $label = str_replace('{' . $expr . '}', '???', $label);
            continue;
        }
        $function = substr($key, $pos + 1);
        $key = substr($key, 0, $pos);
        if (FALSE === ($pos = strpos($function, '('))) {
            $label = str_replace('{' . $expr . '}', '???', $label);
            continue;
        }
        $parameter = substr($function, $pos + 1);
        $function = substr($function, 0, $pos);
        if (FALSE === ($pos = strrpos($parameter, ')'))) {
            $label = str_replace('{' . $expr . '}', '???', $label);
            continue;
        }
        $parameter = substr($parameter, 0, $pos);
        $sql = 'select itemid,value_type,units from items i,hosts h where i.hostid=h.hostid and h.host=' . zbx_dbstr($host) . ' and i.key_=' . zbx_dbstr($key);
        $db_items = DBselect($sql);
        if (NULL == ($db_item = DBfetch($db_items))) {
            $label = str_replace('{' . $expr . '}', '???', $label);
            continue;
        }
        switch ($db_item['value_type']) {
            case ITEM_VALUE_TYPE_FLOAT:
                $history_table = 'history';
                $order_field = 'clock';
                break;
            case ITEM_VALUE_TYPE_UINT64:
                $history_table = 'history_uint';
                $order_field = 'clock';
                break;
            case ITEM_VALUE_TYPE_TEXT:
                $history_table = 'history_text';
                $order_field = 'id';
                break;
            case ITEM_VALUE_TYPE_LOG:
                $history_table = 'history_log';
                $order_field = 'id';
                break;
            default:
                /* ITEM_VALUE_TYPE_STR */
                $history_table = 'history_str';
                $order_field = 'clock';
        }
        if (0 == strcmp($function, 'last')) {
            $sql = 'select value from ' . $history_table . ' where itemid=' . $db_item['itemid'] . ' order by ' . $order_field . ' desc';
            $result = DBselect($sql, 1);
            if (NULL == ($row = DBfetch($result))) {
                $label = str_replace('{' . $expr . '}', '(' . S_NO_DATA_SMALL . ')', $label);
            } else {
                switch ($db_item['value_type']) {
                    case ITEM_VALUE_TYPE_FLOAT:
                    case ITEM_VALUE_TYPE_UINT64:
                        $value = convert_units($row['value'], $db_item['units']);
                        break;
                    default:
                        $value = $row['value'];
                }
                $label = str_replace('{' . $expr . '}', $value, $label);
            }
        } else {
            if (0 == strcmp($function, 'min') || 0 == strcmp($function, 'max') || 0 == strcmp($function, 'avg')) {
                if ($db_item['value_type'] != ITEM_VALUE_TYPE_FLOAT && $db_item['value_type'] != ITEM_VALUE_TYPE_UINT64) {
                    $label = str_replace('{' . $expr . '}', '???', $label);
                    continue;
                }
                $now = time(NULL) - $parameter;
                $sql = 'select ' . $function . '(value) as value from ' . $history_table . ' where clock>' . $now . ' and itemid=' . $db_item['itemid'];
                $result = DBselect($sql);
                if (NULL == ($row = DBfetch($result)) || is_null($row['value'])) {
                    $label = str_replace('{' . $expr . '}', '(' . S_NO_DATA_SMALL . ')', $label);
                } else {
                    $label = str_replace('{' . $expr . '}', convert_units($row['value'], $db_item['units']), $label);
                }
            } else {
                $label = str_replace('{' . $expr . '}', '???', $label);
                continue;
            }
        }
    }
    return $label;
}
Beispiel #13
0
function &DBselect($query, $limit = 'NO', $offset = 0)
{
    global $DB;
    $time_start = microtime(true);
    $result = false;
    if (isset($DB['DB']) && !empty($DB['DB'])) {
        $DB['SELECT_COUNT']++;
        //SDI('SQL['.$DB['SELECT_COUNT'].']: '.$query);
        switch ($DB['TYPE']) {
            case 'MYSQL':
                if (zbx_ctype_digit($limit)) {
                    $query .= ' LIMIT ' . intval($limit) . ' OFFSET ' . intval($offset);
                }
                $result = mysql_query($query, $DB['DB']);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . mysql_error() . ']');
                }
                break;
            case 'POSTGRESQL':
                if (zbx_ctype_digit($limit)) {
                    $query .= ' LIMIT ' . intval($limit) . ' OFFSET ' . intval($offset);
                }
                $result = pg_query($DB['DB'], $query);
                if (!$result) {
                    error('Error in query [' . $query . '] [' . pg_last_error() . ']');
                }
                break;
            case 'ORACLE':
                if (zbx_ctype_digit($limit)) {
                    $till = $offset + $limit;
                    $query = 'SELECT * FROM (' . $query . ') WHERE rownum BETWEEN ' . intval($offset) . ' AND ' . intval($till);
                }
                $result = OCIParse($DB['DB'], $query);
                if (!$result) {
                    $e = @ocierror();
                    error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']');
                } else {
                    if (!@OCIExecute($result, $DB['TRANSACTIONS'] ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS)) {
                        $e = ocierror($result);
                        error('SQL error [' . $e['message'] . '] in [' . $e['sqltext'] . ']');
                    }
                }
                break;
            case 'IBM_DB2':
                if (zbx_ctype_digit($limit)) {
                    $till = $offset + $limit;
                    $query = 'SELECT * FROM (' . $query . ') WHERE rownum BETWEEN ' . intval($offset) . ' AND ' . intval($till);
                }
                $options = array();
                if ($DB['TRANSACTIONS']) {
                    $options['autocommit'] = DB2_AUTOCOMMIT_OFF;
                }
                if (!($result = db2_prepare($DB['DB'], $query))) {
                    $e = @db2_stmt_errormsg($result);
                    error('SQL error [' . $query . '] in [' . $e . ']');
                } else {
                    if (true !== @db2_execute($result, $options)) {
                        $e = @db2_stmt_errormsg($result);
                        error('SQL error [' . $query . '] in [' . $e . ']');
                        $result = false;
                    }
                }
                break;
            case 'SQLITE3':
                if (!$DB['TRANSACTIONS']) {
                    lock_db_access();
                }
                if (zbx_ctype_digit($limit)) {
                    $query .= ' LIMIT ' . intval($limit) . ' OFFSET ' . intval($offset);
                }
                if (!($result = sqlite3_query($DB['DB'], $query))) {
                    error('Error in query [' . $query . '] [' . sqlite3_error($DB['DB']) . ']');
                } else {
                    $data = array();
                    while ($row = sqlite3_fetch_array($result)) {
                        foreach ($row as $id => $name) {
                            if (!zbx_strstr($id, '.')) {
                                continue;
                            }
                            $ids = explode('.', $id);
                            $row[array_pop($ids)] = $row[$id];
                            unset($row[$id]);
                        }
                        $data[] = $row;
                    }
                    sqlite3_query_close($result);
                    $result =& $data;
                }
                if (!$DB['TRANSACTIONS']) {
                    unlock_db_access();
                }
                break;
        }
        if ($DB['TRANSACTIONS'] && !$result) {
            $DB['TRANSACTION_STATE'] &= $result;
        }
    }
    COpt::savesqlrequest(microtime(true) - $time_start, $query);
    return $result;
}
function expand_trigger_description_by_data($row, $flag = ZBX_FLAG_TRIGGER)
{
    if ($row) {
        $description = expand_trigger_description_constants($row['description'], $row);
        for ($i = 0; $i < 10; $i++) {
            $macro = '{HOSTNAME' . ($i ? $i : '') . '}';
            if (zbx_strstr($description, $macro)) {
                $functionid = trigger_get_N_functionid($row['expression'], $i ? $i : 1);
                if (isset($functionid)) {
                    $sql = 'SELECT DISTINCT h.host' . ' FROM functions f,items i,hosts h' . ' WHERE f.itemid=i.itemid' . ' AND i.hostid=h.hostid' . ' AND f.functionid=' . $functionid;
                    $host = DBfetch(DBselect($sql));
                    if (is_null($host['host'])) {
                        $host['host'] = $macro;
                    }
                    $description = str_replace($macro, $host['host'], $description);
                }
            }
        }
        for ($i = 0; $i < 10; $i++) {
            $macro = '{ITEM.LASTVALUE' . ($i ? $i : '') . '}';
            if (zbx_strstr($description, $macro)) {
                $functionid = trigger_get_N_functionid($row['expression'], $i ? $i : 1);
                if (isset($functionid)) {
                    $sql = 'SELECT i.lastvalue, i.value_type, i.itemid, i.valuemapid, i.units ' . ' FROM items i, functions f ' . ' WHERE i.itemid=f.itemid ' . ' AND f.functionid=' . $functionid;
                    $row2 = DBfetch(DBselect($sql));
                    $description = str_replace($macro, format_lastvalue($row2), $description);
                }
            }
        }
        for ($i = 0; $i < 10; $i++) {
            $macro = '{ITEM.VALUE' . ($i ? $i : '') . '}';
            if (zbx_strstr($description, $macro)) {
                $value = $flag == ZBX_FLAG_TRIGGER ? trigger_get_func_value($row['expression'], ZBX_FLAG_TRIGGER, $i ? $i : 1, 1) : trigger_get_func_value($row['expression'], ZBX_FLAG_EVENT, $i ? $i : 1, $row['clock']);
                $description = str_replace($macro, $value, $description);
            }
        }
        if ($res = preg_match_all('/' . ZBX_PREG_EXPRESSION_USER_MACROS . '/', $description, $arr)) {
            $macros = CUserMacro::getMacros($arr[1], array('triggerid' => $row['triggerid']));
            $search = array_keys($macros);
            $values = array_values($macros);
            $description = str_replace($search, $values, $description);
        }
    } else {
        $description = '*ERROR*';
    }
    return $description;
}