public function checkItem($item)
 {
     if (zbx_empty($item)) {
         throw new Exception('Empty item key "' . $item . '" is used in expression.');
     }
     $itemCheck = check_item_key($item);
     if (!$itemCheck['valid']) {
         throw new Exception('Incorrect item key "' . $item . '" is used in expression. ' . $itemCheck['description']);
     }
 }
예제 #2
0
function update_item($itemid, $item)
{
    /*
    		$item = array('description','key','hostid','delay','history','status','type',
    		'snmp_community','snmp_oid','value_type','trapper_hosts','snmp_port','units','multiplier','delta',
    		'snmpv3_securityname','snmpv3_securitylevel','snmpv3_authpassphrase','snmpv3_privpassphrase',
    		'formula','trends','logtimefmt','valuemapid','delay_flex','params','ipmi_sensor','applications','templateid');
    */
    $upd_app = isset($item['applications']) && !is_null($item['applications']);
    $item_in_params = $item;
    $item_data = get_item_by_itemid_limited($itemid);
    $item_data['applications'] = get_applications_by_itemid($itemid);
    if (!check_db_fields($item_data, $item)) {
        error(S_INCORRECT_ARGUMENTS_PASSED_TO_FUNCTION . SPACE . '[update_item]');
        return false;
    }
    $host = get_host_by_hostid($item['hostid']);
    if (($i = array_search(0, $item['applications'])) !== FALSE) {
        unset($item['applications'][$i]);
    }
    if (!preg_match('/^' . ZBX_PREG_ITEM_KEY_FORMAT . '$/u', $item['key_'])) {
        error(S_INCORRECT_KEY_FORMAT . SPACE . "'key_name[param1,param2,...]'");
        return false;
    }
    if ($item['type'] == ITEM_TYPE_DB_MONITOR && $item['key_'] == 'db.odbc.select[<unique short description>]' || $item['type'] == ITEM_TYPE_SSH && $item['key_'] == 'ssh.run[<unique short description>,<ip>,<port>,<encoding>]' || $item['type'] == ITEM_TYPE_TELNET && $item['key_'] == 'telnet.run[<unique short description>,<ip>,<port>,<encoding>]') {
        error(S_ITEMS_CHECK_KEY_DEFAULT_EXAMPLE_PASSED);
        return false;
    }
    $res = calculate_item_nextcheck(0, $item['type'], $item['delay'], $item['delay_flex'], time());
    if ($res['delay'] == SEC_PER_YEAR && $item['type'] != ITEM_TYPE_ZABBIX_ACTIVE && $item['type'] != ITEM_TYPE_TRAPPER) {
        error(S_ITEM_WILL_NOT_BE_REFRESHED_PLEASE_ENTER_A_CORRECT_UPDATE_INTERVAL);
        return FALSE;
    }
    if (($item['snmp_port'] < 1 || $item['snmp_port'] > 65535) && in_array($item['type'], array(ITEM_TYPE_SNMPV1, ITEM_TYPE_SNMPV2C, ITEM_TYPE_SNMPV3))) {
        error(S_INVALID_SNMP_PORT);
        return FALSE;
    }
    if ($item['value_type'] == ITEM_VALUE_TYPE_STR) {
        $item['delta'] = 0;
    }
    if (preg_match('/^(log|logrt|eventlog)\\[/', $item['key_']) && $item['value_type'] != ITEM_VALUE_TYPE_LOG) {
        error(S_TYPE_INFORMATION_BUST_LOG_FOR_LOG_KEY);
        return FALSE;
    }
    if ($item['value_type'] != ITEM_VALUE_TYPE_UINT64) {
        $item['data_type'] = 0;
    }
    $sql = 'SELECT itemid, hostid, templateid ' . ' FROM items ' . ' WHERE hostid=' . $item['hostid'] . ' AND itemid<>' . $itemid . ' AND key_=' . zbx_dbstr($item['key_']);
    $db_item = DBfetch(DBselect($sql));
    if ($db_item && ($db_item['templateid'] != 0 || $item['templateid'] == 0)) {
        error(S_AN_ITEM_WITH_THE_KEY . SPACE . '[' . $item['key_'] . ']' . SPACE . S_ALREADY_EXISTS_FOR_HOST_SMALL . SPACE . '[' . $host['host'] . '].' . SPACE . S_THE_KEY_MUST_BE_UNIQUE);
        return FALSE;
    }
    // first update child items
    $db_tmp_items = DBselect('SELECT itemid, hostid FROM items WHERE templateid=' . $itemid);
    while ($db_tmp_item = DBfetch($db_tmp_items)) {
        $child_item_params = $item_in_params;
        $child_item_params['hostid'] = $db_tmp_item['hostid'];
        $child_item_params['templateid'] = $itemid;
        if ($upd_app) {
            $child_item_params['applications'] = get_same_applications_for_host($item['applications'], $db_tmp_item['hostid']);
        } else {
            $child_item_params['applications'] = null;
        }
        if (!check_db_fields($db_tmp_item, $child_item_params)) {
            error(S_INCORRECT_ARGUMENTS_PASSED_TO_FUNCTION . SPACE . '[update_item]');
            return false;
        }
        $result = update_item($db_tmp_item['itemid'], $child_item_params);
        // recursion!!!
        if (!$result) {
            return $result;
        }
    }
    if ($db_item && $item['templateid'] != 0) {
        $result = delete_item($db_item['itemid']);
        if (!$result) {
            error(S_CANNOT_UPDATE_ITEM . SPACE . "'" . $host["host"] . ':' . $item['key_'] . "'");
            return FALSE;
        }
    }
    //validating item key
    $checkResult = check_item_key($item['key_']);
    if (!$checkResult['valid']) {
        error(S_ERROR_IN_ITEM_KEY . SPACE . $checkResult['description']);
        return false;
    }
    $item_old = get_item_by_itemid($itemid);
    DBexecute('UPDATE items SET lastlogsize=0, mtime=0 WHERE itemid=' . $itemid . ' AND key_<>' . zbx_dbstr($item['key_']));
    if ($upd_app) {
        $result = DBexecute('DELETE FROM items_applications WHERE itemid=' . $itemid);
        foreach ($item['applications'] as $appid) {
            $itemappid = get_dbid('items_applications', 'itemappid');
            DBexecute('INSERT INTO items_applications (itemappid,itemid,applicationid) VALUES (' . $itemappid . ',' . $itemid . ',' . $appid . ')');
        }
    }
    if ($item['status'] == ITEM_STATUS_ACTIVE) {
        DBexecute("UPDATE items SET error='' WHERE itemid=" . $itemid . ' and status<>' . $item['status']);
    }
    $result = DBexecute('UPDATE items ' . ' SET description=' . zbx_dbstr($item['description']) . ',' . 'key_=' . zbx_dbstr($item['key_']) . ',' . 'hostid=' . $item['hostid'] . ',' . 'delay=' . $item['delay'] . ',' . 'history=' . $item['history'] . ',' . 'type=' . $item['type'] . ',' . 'snmp_community=' . zbx_dbstr($item['snmp_community']) . ',' . 'snmp_oid=' . zbx_dbstr($item['snmp_oid']) . ',' . 'value_type=' . $item['value_type'] . ',' . 'data_type=' . $item['data_type'] . ',' . 'trapper_hosts=' . zbx_dbstr($item['trapper_hosts']) . ',' . 'snmp_port=' . $item['snmp_port'] . ',' . 'units=' . zbx_dbstr($item['units']) . ',' . 'multiplier=' . $item['multiplier'] . ',' . 'delta=' . $item['delta'] . ',' . 'snmpv3_securityname=' . zbx_dbstr($item['snmpv3_securityname']) . ',' . 'snmpv3_securitylevel=' . $item['snmpv3_securitylevel'] . ',' . 'snmpv3_authpassphrase=' . ($item['snmpv3_securitylevel'] == ITEM_SNMPV3_SECURITYLEVEL_AUTHPRIV || $item['snmpv3_securitylevel'] == ITEM_SNMPV3_SECURITYLEVEL_AUTHNOPRIV ? zbx_dbstr($item['snmpv3_authpassphrase']) : "''") . ',' . 'snmpv3_privpassphrase=' . ($item['snmpv3_securitylevel'] == ITEM_SNMPV3_SECURITYLEVEL_AUTHPRIV ? zbx_dbstr($item['snmpv3_privpassphrase']) : "''") . ',' . 'formula=' . zbx_dbstr($item['formula']) . ',' . 'trends=' . $item['trends'] . ',' . 'logtimefmt=' . zbx_dbstr($item['logtimefmt']) . ',' . 'valuemapid=' . $item['valuemapid'] . ',' . 'delay_flex=' . zbx_dbstr($item['delay_flex']) . ',' . 'params=' . zbx_dbstr($item['params']) . ',' . 'ipmi_sensor=' . zbx_dbstr($item['ipmi_sensor']) . ',' . 'templateid=' . $item['templateid'] . ',' . 'authtype=' . $item['authtype'] . ',' . 'username='******'username']) . ',' . 'password='******'password']) . ',' . 'publickey=' . zbx_dbstr($item['publickey']) . ',' . 'privatekey=' . zbx_dbstr($item['privatekey']) . ' WHERE itemid=' . $itemid);
    if ($result) {
        $item_new = get_item_by_itemid($itemid);
        add_audit_ext(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_ITEM, $itemid, $host['host'] . ':' . $item_old['description'], 'items', $item_old, $item_new);
    }
    update_item_status($itemid, $item['status']);
    if ($result) {
        info(S_ITEM . SPACE . "'" . $host['host'] . ':' . $item['key_'] . "'" . SPACE . S_UPDATED_SMALL);
    }
    return $result;
}