protected function doAction() { $idx = $this->getInput('idx'); $value_int = $this->getInput('value_int'); DBstart(); switch ($idx) { case 'web.latest.toggle': case 'web.latest.toggle_other': if ($value_int == 1) { // default value CProfile::delete($idx, $this->getInput('idx2')); } else { foreach ($this->getInput('idx2') as $idx2) { CProfile::update($idx, $value_int, PROFILE_TYPE_INT, $idx2); } } break; default: if ($value_int == 1) { // default value CProfile::delete($idx); } else { CProfile::update($idx, $value_int, PROFILE_TYPE_INT); } } DBend(); $this->setResponse(new CControllerResponseData(['main_block' => ''])); }
protected function doAction() { $script = []; $this->getInputs($script, ['scriptid', 'name', 'type', 'execute_on', 'command', 'description', 'usrgrpid', 'groupid', 'host_access']); $script['confirmation'] = $this->getInput('confirmation', ''); if ($this->getInput('type', ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT) == ZBX_SCRIPT_TYPE_IPMI && $this->hasInput('commandipmi')) { $script['command'] = $this->getInput('commandipmi'); } if ($this->getInput('hgstype', 1) == 0) { $script['groupid'] = 0; } DBstart(); $result = API::Script()->update($script); if ($result) { $scriptId = reset($result['scriptids']); add_audit(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_SCRIPT, 'Name [' . $this->getInput('name', '') . '] id [' . $scriptId . ']'); } $result = DBend($result); if ($result) { $response = new CControllerResponseRedirect('zabbix.php?action=script.list&uncheck=1'); $response->setMessageOk(_('Script updated')); } else { $response = new CControllerResponseRedirect('zabbix.php?action=script.edit&scriptid=' . $this->getInput('scriptid')); $response->setFormData($this->getInputAll()); $response->setMessageError(_('Cannot update script')); } $this->setResponse($response); }
public static function flush() { // if not initialised, no changes were made if (is_null(self::$profiles)) { return true; } if (self::$userDetails['userid'] <= 0) { return null; } if (!empty(self::$insert) || !empty(self::$update)) { DBstart(); foreach (self::$insert as $idx => $profile) { foreach ($profile as $idx2 => $data) { self::insertDB($idx, $data['value'], $data['type'], $idx2); } } ksort(self::$update); foreach (self::$update as $idx => $profile) { ksort($profile); foreach ($profile as $idx2 => $data) { self::updateDB($idx, $data['value'], $data['type'], $idx2); } } DBend(); } }
protected function doAction() { $proxy = []; $this->getInputs($proxy, ['host', 'status', 'description', 'tls_connect', 'tls_accept', 'tls_issuer', 'tls_subject', 'tls_psk_identity', 'tls_psk']); if ($this->getInput('status', HOST_STATUS_PROXY_ACTIVE) == HOST_STATUS_PROXY_PASSIVE) { $proxy['interface'] = []; $this->getInputs($proxy['interface'], ['dns', 'ip', 'useip', 'port']); } DBstart(); if ($this->hasInput('proxy_hostids')) { // skip discovered hosts $proxy['hosts'] = API::Host()->get(['output' => ['hostid'], 'hostids' => $this->getInput('proxy_hostids'), 'filter' => ['flags' => ZBX_FLAG_DISCOVERY_NORMAL]]); } $result = API::Proxy()->create([$proxy]); if ($result) { add_audit(AUDIT_ACTION_ADD, AUDIT_RESOURCE_PROXY, '[' . $this->getInput('host', '') . '] [' . reset($result['proxyids']) . ']'); } $result = DBend($result); if ($result) { $response = new CControllerResponseRedirect('zabbix.php?action=proxy.list&uncheck=1'); $response->setMessageOk(_('Proxy added')); } else { $response = new CControllerResponseRedirect('zabbix.php?action=proxy.edit'); $response->setFormData($this->getInputAll()); $response->setMessageError(_('Cannot add proxy')); } $this->setResponse($response); }
private static function transactionBegin() { global $DB; if ($DB['TRANSACTIONS'] == 0) { DBstart(); self::$transactionStarted = true; } }
function update_node_profile($nodeids) { DBstart(); DBexecute('DELETE FROM profiles WHERE userid=' . CWebUser::$data['userid'] . ' AND idx=' . zbx_dbstr('web.nodes.selected')); foreach ($nodeids as $nodeid) { DBexecute('INSERT INTO profiles (profileid,userid,idx,value_id,type)' . ' VALUES (' . get_dbid('profiles', 'profileid') . ',' . CWebUser::$data['userid'] . ',' . zbx_dbstr('web.nodes.selected') . ',' . $nodeid . ',4)'); } DBend(); }
protected function doAction() { $mediatype = []; $this->getInputs($mediatype, ['type', 'description', 'status']); switch ($mediatype['type']) { case MEDIA_TYPE_EMAIL: $this->getInputs($mediatype, ['smtp_server', 'smtp_port', 'smtp_helo', 'smtp_email', 'smtp_security', 'smtp_verify_peer', 'smtp_verify_host', 'smtp_authentication', 'passwd']); if ($this->hasInput('smtp_username')) { $mediatype['username'] = $this->getInput('smtp_username'); } break; case MEDIA_TYPE_EXEC: $this->getInputs($mediatype, ['exec_path']); $mediatype['exec_params'] = ''; if ($this->hasInput('exec_params')) { $exec_params = zbx_objectValues($this->getInput('exec_params'), 'exec_param'); foreach ($exec_params as $exec_param) { $mediatype['exec_params'] .= $exec_param . "\n"; } } break; case MEDIA_TYPE_SMS: $this->getInputs($mediatype, ['gsm_modem']); break; case MEDIA_TYPE_JABBER: $this->getInputs($mediatype, ['passwd']); if ($this->hasInput('jabber_username')) { $mediatype['username'] = $this->getInput('jabber_username'); } break; case MEDIA_TYPE_EZ_TEXTING: $this->getInputs($mediatype, ['passwd']); if ($this->hasInput('eztext_username')) { $mediatype['username'] = $this->getInput('eztext_username'); } if ($this->hasInput('eztext_limit')) { $mediatype['exec_path'] = $this->getInput('eztext_limit'); } break; } DBstart(); $result = API::Mediatype()->create($mediatype); if ($result) { add_audit(AUDIT_ACTION_ADD, AUDIT_RESOURCE_MEDIA_TYPE, 'Media type [' . $mediatype['description'] . ']'); } $result = DBend($result); if ($result) { $response = new CControllerResponseRedirect('zabbix.php?action=mediatype.list&uncheck=1'); $response->setMessageOk(_('Media type added')); } else { $response = new CControllerResponseRedirect('zabbix.php?action=mediatype.edit'); $response->setFormData($this->getInputAll()); $response->setMessageError(_('Cannot add media type')); } $this->setResponse($response); }
function update_node_profile($nodeids) { global $USER_DETAILS; DBstart(); $sql = 'DELETE FROM profiles WHERE userid=' . $USER_DETAILS['userid'] . ' AND idx=' . zbx_dbstr('web.nodes.selected'); DBexecute($sql); foreach ($nodeids as $nodeid) { $profileid = get_dbid('profiles', 'profileid'); $sql = 'INSERT INTO profiles (profileid, userid, idx, value_id, type)' . ' VALUES (' . $profileid . ',' . $USER_DETAILS['userid'] . ', ' . zbx_dbstr('web.nodes.selected') . ',' . $nodeid . ', 4)'; DBexecute($sql); } DBend(); }
/** * Adds favorite value to DB. * * @param string $idx identifier of favorite value group * @param int $favid value id * @param string $favobj source object * * @return bool did SQL INSERT succeeded */ public static function add($idx, $favid, $favobj = null) { if (self::exists($idx, $favid, $favobj)) { return true; } // add to cache only if cache is created if (isset(self::$cache[$idx])) { self::$cache[$idx][] = array('value' => $favid, 'source' => $favobj); } DBstart(); $values = array('profileid' => get_dbid('profiles', 'profileid'), 'userid' => CWebUser::$data['userid'], 'idx' => zbx_dbstr($idx), 'value_id' => zbx_dbstr($favid), 'type' => PROFILE_TYPE_ID); if (!is_null($favobj)) { $values['source'] = zbx_dbstr($favobj); } return DBend(DBexecute('INSERT INTO profiles (' . implode(', ', array_keys($values)) . ') VALUES (' . implode(', ', $values) . ')')); }
protected function doAction() { $profile = ['graphid' => 'web.favorite.graphids', 'itemid' => 'web.favorite.graphids', 'screenid' => 'web.favorite.screenids', 'slideshowid' => 'web.favorite.screenids', 'sysmapid' => 'web.favorite.sysmapids']; $object = $this->getInput('object'); $objectid = $this->getInput('objectid'); $data = []; DBstart(); $result = CFavorite::remove($profile[$object], $objectid, $object); $result = DBend($result); if ($result) { $data['main_block'] = '$("addrm_fav").title = "' . _('Add to favourites') . '";' . "\n" . '$("addrm_fav").onclick = function() { add2favorites("' . $object . '", "' . $objectid . '"); }' . "\n" . 'switchElementClass("addrm_fav", "btn-remove-fav", "btn-add-fav");'; } else { $data['main_block'] = ''; } $this->setResponse(new CControllerResponseData($data)); }
public static function flush() { if (!empty(self::$insert) || !empty(self::$update)) { DBstart(); foreach (self::$insert as $idx => $profile) { foreach ($profile as $idx2 => $data) { $result = self::insertDB($idx, $data['value'], $data['type'], $idx2); } } ksort(self::$update); foreach (self::$update as $idx => $profile) { ksort($profile); foreach ($profile as $idx2 => $data) { self::updateDB($idx, $data['value'], $data['type'], $idx2); } } DBend(); } }
protected function doAction() { $scriptids = $this->getInput('scriptids'); DBstart(); $result = API::Script()->delete($scriptids); if ($result) { foreach ($scriptids as $scriptid) { add_audit(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_SCRIPT, _('Script') . ' [' . $scriptid . ']'); } } $result = DBend($result); $deleted = count($scriptids); $response = new CControllerResponseRedirect('zabbix.php?action=script.list&uncheck=1'); if ($result) { $response->setMessageOk(_n('Script deleted', 'Scripts deleted', $deleted)); } else { $response->setMessageError(_n('Cannot delete script', 'Cannot delete scripts', $deleted)); } $this->setResponse($response); }
protected static function BeginTransaction($caller = 'CZBXAPI') { global $DB; if (!isset(self::$transaction[$caller])) { self::$transaction[$caller] = 0; } self::$transaction[$caller]++; //SDII(self::$transaction); if (self::$transaction['counter'] > 0) { self::$transaction['counter']++; } else { if ($DB['TRANSACTIONS'] == 0) { DBstart(); self::$transaction['counter'] = 1; self::$transaction['owner'] = $caller; } else { self::$transaction['counter'] = 2; self::$transaction['owner'] = 'DB_CLASS'; } } //SDII(self::$transaction); return true; }
/** * Delete Users * * @static * @param array $userids * @return boolean */ public static function delete($userids) { $result = false; DBstart(false); foreach ($userids as $userid) { $result = delete_user($userid); if (!$resukt) { break; } } DBend($result); if ($result) { return true; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error'); return false; } }
/** * Add items to graph * * <code> * $items = array( * *string 'graphid' => null, * array 'items' => ( * 'item1' => array( * *int 'itemid' => null, * int 'color' => '000000', * int 'drawtype' => 0, * int 'sortorder' => 0, * int 'yaxisside' => 1, * int 'calc_fnc' => 2, * int 'type' => 0, * int 'periods_cnt' => 5, * ), ... ) * ); * </code> * * @static * @param array $items multidimensional array with items data * @return boolean */ public static function addItems($items) { $error = 'Unknown ZABBIX internal error'; $result_ids = array(); $result = false; $tpl_graph = false; $graphid = $items['graphid']; $items_tmp = $items['items']; $items = array(); $itemids = array(); foreach ($items_tmp as $item) { $graph_db_fields = array('itemid' => null, 'color' => '000000', 'drawtype' => 0, 'sortorder' => 0, 'yaxisside' => 1, 'calc_fnc' => 2, 'type' => 0, 'periods_cnt' => 5); if (!check_db_fields($graph_db_fields, $item)) { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Wrong fields for item [ ' . $item['itemid'] . ' ]'); return false; } $items[$item['itemid']] = $item; $itemids[$item['itemid']] = $item['itemid']; } // check if graph is templated graph, then items cannot be added $graph = CGraph::getById(array('graphid' => $graphid)); if ($graph['templateid'] != 0) { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Cannot edit templated graph : ' . $graph['name']); return false; } // check if graph belongs to template, if so, only items from same template can be added $tmp_hosts = get_hosts_by_graphid($graphid); $host = DBfetch($tmp_hosts); // if graph belongs to template, only one host is possible if ($host["status"] == HOST_STATUS_TEMPLATE) { $sql = 'SELECT DISTINCT count(i.hostid) as count FROM items i WHERE i.hostid<>' . $host['hostid'] . ' AND ' . DBcondition('i.itemid', $itemids); $host_count = DBfetch(DBselect($sql)); if ($host_count['count']) { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'You must use items only from host : ' . $host['host'] . ' for template graph : ' . $graph['name']); return false; } $tpl_graph = true; } DBstart(false); $result = self::addItems_rec($graphid, $items, $tpl_graph); $result = DBend($result); if ($result) { return $result; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => $error); //'Internal zabbix error'); return false; } }
/** * Update triggers * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param _array $triggers multidimensional array with triggers data * @param array $triggers[0,...]['expression'] * @param array $triggers[0,...]['description'] OPTIONAL * @param array $triggers[0,...]['type'] OPTIONAL * @param array $triggers[0,...]['priority'] OPTIONAL * @param array $triggers[0,...]['status'] OPTIONAL * @param array $triggers[0,...]['comments'] OPTIONAL * @param array $triggers[0,...]['url'] OPTIONAL * @return boolean */ public static function update($triggers) { $result = false; $triggerids = array(); DBstart(false); foreach ($triggers as $trigger) { $trigger_db_fields = self::getById($trigger); if (!isset($trigger_db_fields)) { $result = false; break; } if (!check_db_fields($trigger_db_fields, $trigger)) { error('Incorrect arguments pasted to function [CTrigger::update]'); $result = false; break; } $trigger['expression'] = explode_exp($trigger['expression'], false); $result = update_trigger($trigger['triggerid'], $trigger['expression'], $trigger['description'], $trigger['type'], $trigger['priority'], $trigger['status'], $trigger['comments'], $trigger['url']); if (!$result) { break; } $triggerids[$result] = $result; } $result = DBend($result); if ($result) { return $triggerids; } else { self::$error[] = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error'); return false; } }
/** * Call the given API service method and return the response. * * @param string $requestApi API name * @param string $requestMethod API method * @param mixed $params API parameters * @param string $auth Authentication token * * @return CApiClientResponse */ public function callMethod($requestApi, $requestMethod, $params, $auth) { global $DB; $api = strtolower($requestApi); $method = strtolower($requestMethod); $response = new CApiClientResponse(); // check API if (!$this->isValidApi($api)) { $response->errorCode = ZBX_API_ERROR_PARAMETERS; $response->errorMessage = _s('Incorrect API "%1$s".', $requestApi); return $response; } // check method if (!$this->isValidMethod($api, $method)) { $response->errorCode = ZBX_API_ERROR_PARAMETERS; $response->errorMessage = _s('Incorrect method "%1$s.%2$s".', $requestApi, $requestMethod); return $response; } // check params if (!is_array($params)) { $response->errorCode = ZBX_API_ERROR_PARAMETERS; $response->errorMessage = _s('Cannot call method "%1$s.%2$s" without parameters.', $requestApi, $requestMethod); return $response; } $requiresAuthentication = $this->requiresAuthentication($api, $method); // check that no authentication token is passed to methods that don't require it if (!$requiresAuthentication && $auth !== null) { $response->errorCode = ZBX_API_ERROR_PARAMETERS; $response->errorMessage = _s('The "%1$s.%2$s" method must be called without the "auth" parameter.', $requestApi, $requestMethod); return $response; } $newTransaction = false; try { // authenticate if ($requiresAuthentication) { $this->authenticate($auth); } // the nopermission parameter must not be available for external API calls. unset($params['nopermissions']); // if no transaction has been started yet - start one if ($DB['TRANSACTIONS'] == 0) { DBstart(); $newTransaction = true; } // call API method $result = call_user_func_array([$this->serviceFactory->getObject($api), $method], [$params]); // if the method was called successfully - commit the transaction if ($newTransaction) { DBend(true); } $response->data = $result; } catch (Exception $e) { if ($newTransaction) { // if we're calling user.login and authentication failed - commit the transaction to save the // failed attempt data if ($api === 'user' && $method === 'login') { DBend(true); } else { DBend(false); } } $response->errorCode = $e instanceof APIException ? $e->getCode() : ZBX_API_ERROR_INTERNAL; $response->errorMessage = $e->getMessage(); // add debug data if ($this->debug) { $response->debug = $e->getTrace(); } } return $response; }
/** * Remove screen column. * * @param array $screen * @param int $col_num */ function delScreenColumn(array $screen, $col_num) { foreach ($screen['screenitems'] as $key => &$screen_item) { if ($screen_item['x'] == $col_num) { unset($screen['screenitems'][$key]); } elseif ($screen_item['x'] > $col_num) { $screen_item['x']--; } } unset($screen_item); DBstart(); $result = API::Screen()->update(['screenid' => $screen['screenid'], 'hsize' => $screen['hsize'] - 1, 'screenitems' => $screen['screenitems']]); if ($result) { add_audit_details(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_SCREEN, $screen['screenid'], $screen['name'], _('Column deleted')); } DBend($result); }
/** * Add Groups to existing Host * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @static * @param string $hostid * @param array $groupids * @return boolean */ public static function addGroupsToHost($data) { $hostid = $data['hostid']; $groupids = $data['groupids']; $result = false; DBstart(false); foreach ($groupids as $key => $groupid) { $hostgroupid = get_dbid("hosts_groups", "hostgroupid"); $result = DBexecute("insert into hosts_groups (hostgroupid,hostid,groupid) values ({$hostgroupid}, {$hostid}, {$groupid})"); if (!$result) { return $result; } } $result = DBend($result); if ($result) { return true; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error'); return false; } }
function update_image($imageid, $name, $imagetype, $file) { if (is_null($file)) { /* only update parameters */ return DBexecute("update images set name=" . zbx_dbstr($name) . ",imagetype=" . zbx_dbstr($imagetype) . " where imageid={$imageid}"); } else { global $DB; if ($file["error"] != 0 || $file["size"] == 0) { error("Incorrect Image"); return FALSE; } if ($file["size"] < 1024 * 1024) { $image = fread(fopen($file["tmp_name"], "r"), filesize($file["tmp_name"])); if ($DB['TYPE'] == "ORACLE") { $result = DBexecute('UPDATE images SET name=' . zbx_dbstr($name) . ',imagetype=' . zbx_dbstr($imagetype) . ' WHERE imageid=' . $imageid); if (!$result) { return $result; } DBstart(); if (!($stid = DBselect('SELECT image FROM images WHERE imageid=' . $imageid . ' FOR UPDATE'))) { DBend(); return false; } $row = DBfetch($stid); $lobimage = $row['image']; DBend($lobimage->save($image)); $lobimage->free(); return $stid; } else { if ($DB['TYPE'] == "POSTGRESQL") { $image = pg_escape_bytea($image); $sql = 'UPDATE images SET name=' . zbx_dbstr($name) . ',imagetype=' . zbx_dbstr($imagetype) . ",image='" . $image . "'" . ' WHERE imageid=' . $imageid; return DBexecute($sql); } else { if ($DB['TYPE'] == "SQLITE3") { $image = bin2hex($image); } } } $sql = 'UPDATE images SET name=' . zbx_dbstr($name) . ',imagetype=' . zbx_dbstr($imagetype) . ',image=' . zbx_dbstr($image) . ' WHERE imageid=' . $imageid; return DBexecute($sql); } else { error("Image size must be less than 1Mb"); return FALSE; } } }
/** * Update Host * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param _array $hosts multidimensional array with Hosts data * @param string $hosts['host'] Host name. * @param int $hosts['port'] Port. OPTIONAL * @param int $hosts['status'] Host Status. OPTIONAL * @param int $hosts['useip'] Use IP. OPTIONAL * @param string $hosts['dns'] DNS. OPTIONAL * @param string $hosts['ip'] IP. OPTIONAL * @param int $hosts['proxy_hostid'] Proxy Host ID. OPTIONAL * @param int $hosts['useipmi'] Use IPMI. OPTIONAL * @param string $hosts['ipmi_ip'] IPMAI IP. OPTIONAL * @param int $hosts['ipmi_port'] IPMI port. OPTIONAL * @param int $hosts['ipmi_authtype'] IPMI authentication type. OPTIONAL * @param int $hosts['ipmi_privilege'] IPMI privilege. OPTIONAL * @param string $hosts['ipmi_username'] IPMI username. OPTIONAL * @param string $hosts['ipmi_password'] IPMI password. OPTIONAL * @return boolean */ public static function update($hosts) { $templates = null; $newgroup = ''; $hostids = array(); $result = false; DBstart(false); foreach ($hosts as $host) { $sql = 'SELECT DISTINCT * ' . ' FROM hosts ' . ' WHERE hostid=' . $host['hostid']; $host_db_fields = DBfetch(DBselect($sql)); if (!isset($host_db_fields)) { $result = false; break; } if (!check_db_fields($host_db_fields, $host)) { $result = false; break; } $groups = get_groupids_by_host($host['hostid']); $result = update_host($host['hostid'], $host['host'], $host['port'], $host['status'], $host['useip'], $host['dns'], $host['ip'], $host['proxy_hostid'], $templates, $host['useipmi'], $host['ipmi_ip'], $host['ipmi_port'], $host['ipmi_authtype'], $host['ipmi_privilege'], $host['ipmi_username'], $host['ipmi_password'], $newgroup, $groups); if (!$result) { break; } $hostids[$result] = $result; } $result = DBend($result); if ($result) { return $hostids; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error'); return false; } }
/** * Link Host to Templates * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @param array $data * @param string $data['hostid'] * @param array $data['templateids'] * @return boolean */ public static function linkTemplates($data) { $result = false; $error = ''; $hostid = $data['hostid']; $templateids = $data['templateids']; DBstart(false); foreach ($templateids as $templateid) { $hosttemplateid = get_dbid('hosts_templates', 'hosttemplateid'); if (!($result = DBexecute('INSERT INTO hosts_templates VALUES (' . $hosttemplateid . ',' . $hostid . ',' . $templateid . ')'))) { break; } } if ($result) { foreach ($templateids as $templateid) { $result = sync_host_with_templates($hostid, $templateid); if (!$result) { break; } } } $result = DBend($result); if ($result) { return true; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => $error); return false; } }
function update_profile($idx, $value, $type = PROFILE_TYPE_UNKNOWN, $idx2 = null, $source = null) { global $USER_DETAILS; if ($USER_DETAILS['alias'] == ZBX_GUEST_USER) { return false; } if (profile_type($type, 'unknown')) { $type = profile_type_by_value($value); } else { $value = profile_value_by_type($value, $type); } //if($idx == 'web.audit.filter.action') SDI('PROF: v='.$value.' t='.$type); if ($value === false) { return false; } $sql_cond = ''; if (zbx_numeric($idx2)) { $sql_cond = ' AND idx2=' . $idx2 . ' AND ' . DBin_node('idx2'); } if (profile_type($type, 'array')) { $sql = 'DELETE FROM profiles ' . ' WHERE userid=' . $USER_DETAILS["userid"] . ' AND idx=' . zbx_dbstr($idx) . $sql_cond; DBstart(); DBexecute($sql); foreach ($value as $id => $val) { insert_profile($idx, $val, $type, $idx2, $source); } $result = DBend(); } else { $sql = 'SELECT profileid ' . ' FROM profiles ' . ' WHERE userid=' . $USER_DETAILS['userid'] . ' AND idx=' . zbx_dbstr($idx) . $sql_cond; $row = DBfetch(DBselect($sql)); if (!$row) { $result = insert_profile($idx, $value, $type, $idx2, $source); } else { $val = array(); $value_type = profile_field_by_type($type); $val['value_id'] = 0; $val['value_int'] = 0; $val['value_str'] = ''; $val[$value_type] = $value; $idx2 = zbx_numeric($idx2) ? $idx2 : 0; $src = is_null($source) ? '' : $source; if (is_array($value)) { $val[$value_type] = isset($value['value']) ? $value['value'] : ''; $src = isset($value['source']) ? $value['source'] : $src; } if (is_null($val[$value_type])) { return false; } $sql = 'UPDATE profiles ' . ' SET value_id=' . $val['value_id'] . ',' . ' value_int=' . $val['value_int'] . ',' . ' value_str=' . zbx_dbstr($val['value_str']) . ',' . ' type=' . $type . ',' . ' source=' . zbx_dbstr($src) . ' WHERE userid=' . $USER_DETAILS["userid"] . ' AND idx=' . zbx_dbstr($idx) . $sql_cond; //if($idx == 'web.audit.filter.action') SDI($sql); $result = DBexecute($sql); } } return $result; }
function db_save_httptest($httptestid, $hostid, $application, $name, $delay, $status, $agent, $macros, $steps) { $history = 30; // TODO !!! Allow user set this parametr $trends = 90; // TODO !!! Allow user set this parametr if (!eregi('^([0-9a-zA-Z\\_\\.[.-.]\\$ ]+)$', $name)) { error("Scenario name should contain '0-9a-zA-Z_.\$ '- characters only"); return false; } DBstart(); if ($applicationid = DBfetch(DBselect('select applicationid from applications ' . ' where name=' . zbx_dbstr($application) . ' and hostid=' . $hostid))) { $applicationid = $applicationid['applicationid']; } else { $applicationid = add_application($application, $hostid); if (!$applicationid) { error('Can\'t add new application. [' . $application . ']'); return false; } } if (isset($httptestid)) { $result = DBexecute('update httptest set ' . ' applicationid=' . $applicationid . ', name=' . zbx_dbstr($name) . ', delay=' . $delay . ',' . ' status=' . $status . ', agent=' . zbx_dbstr($agent) . ', macros=' . zbx_dbstr($macros) . ',' . ' error=' . zbx_dbstr('') . ', curstate=' . HTTPTEST_STATE_UNKNOWN . ' where httptestid=' . $httptestid); } else { $httptestid = get_dbid("httptest", "httptestid"); if (DBfetch(DBselect('select t.httptestid from httptest t, applications a where t.applicationid=a.applicationid ' . ' and a.hostid=' . $hostid . ' and t.name=' . zbx_dbstr($name)))) { error('Scenario with name [' . $name . '] already exist'); return false; } $result = DBexecute('insert into httptest' . ' (httptestid, applicationid, name, delay, status, agent, macros, curstate) ' . ' values (' . $httptestid . ',' . $applicationid . ',' . zbx_dbstr($name) . ',' . $delay . ',' . $status . ',' . zbx_dbstr($agent) . ',' . zbx_dbstr($macros) . ',' . HTTPTEST_STATE_UNKNOWN . ')'); $test_added = true; } if ($result) { $httpstepids = array(); foreach ($steps as $sid => $s) { if (!isset($s['name'])) { $s['name'] = ''; } if (!isset($s['timeout'])) { $s['timeout'] = 15; } if (!isset($s['url'])) { $s['url'] = ''; } if (!isset($s['posts'])) { $s['posts'] = ''; } if (!isset($s['required'])) { $s['required'] = ''; } if (!isset($s['status_codes'])) { $s['status_codes'] = ''; } $result = db_save_step($hostid, $applicationid, $httptestid, $name, $s['name'], $sid + 1, $s['timeout'], $s['url'], $s['posts'], $s['required'], $s['status_codes'], $delay, $history, $trends); if (!$result) { break; } $httpstepids[$result] = $result; } if ($result) { /* clean unneeded steps */ $db_steps = DBselect('select httpstepid from httpstep where httptestid=' . $httptestid); while ($step_data = DBfetch($db_steps)) { if (isset($httpstepids[$step_data['httpstepid']])) { continue; } delete_httpstep($step_data['httpstepid']); } } } if ($result) { $monitored_items = array(array('description' => 'Download speed for scenario \'$1\'', 'key_' => 'web.test.in[' . $name . ',,bps]', 'type' => ITEM_VALUE_TYPE_FLOAT, 'units' => 'bps', 'httptestitemtype' => HTTPSTEP_ITEM_TYPE_IN), array('description' => 'Failed step of scenario \'$1\'', 'key_' => 'web.test.fail[' . $name . ']', 'type' => ITEM_VALUE_TYPE_UINT64, 'units' => '', 'httptestitemtype' => HTTPSTEP_ITEM_TYPE_LASTSTEP)); foreach ($monitored_items as $item) { $item_data = DBfetch(DBselect('select i.itemid,i.history,i.trends,i.status,i.delta,i.valuemapid ' . ' from items i, httptestitem hi ' . ' where hi.httptestid=' . $httptestid . ' and hi.itemid=i.itemid ' . ' and hi.type=' . $item['httptestitemtype'])); if (!$item_data) { $item_data = DBfetch(DBselect('select i.itemid,i.history,i.trends,i.status,i.delta,i.valuemapid ' . ' from items i where i.key_=' . zbx_dbstr($item['key_']) . ' and i.hostid=' . $hostid)); } $item_args = array('description' => $item['description'], 'key_' => $item['key_'], 'hostid' => $hostid, 'delay' => $delay, 'type' => ITEM_TYPE_HTTPTEST, 'snmp_community' => '', 'snmp_oid' => '', 'value_type' => $item['type'], 'data_type' => ITEM_DATA_TYPE_DECIMAL, 'trapper_hosts' => 'localhost', 'snmp_port' => 161, 'units' => $item['units'], 'multiplier' => 0, 'snmpv3_securityname' => '', 'snmpv3_securitylevel' => 0, 'snmpv3_authpassphrase' => '', 'snmpv3_privpassphrase' => '', 'formula' => 0, 'logtimefmt' => '', 'delay_flex' => '', 'params' => '', 'ipmi_sensor' => '', 'applications' => array($applicationid)); if (!$item_data) { $item_args['history'] = $history; $item_args['status'] = ITEM_STATUS_ACTIVE; $item_args['delta'] = 0; $item_args['trends'] = $trends; $item_args['valuemapid'] = 0; if (!($itemid = add_item($item_args))) { $result = false; break; } } else { $itemid = $item_data['itemid']; $item_args['history'] = $item_data['history']; $item_args['status'] = $item_data['status']; $item_args['delta'] = $item_data['delta']; $item_args['trends'] = $item_data['trends']; $item_args['valuemapid'] = $item_data['valuemapid']; if (!update_item($itemid, $item_args)) { $result = false; break; } } $httptestitemid = get_dbid('httptestitem', 'httptestitemid'); DBexecute('delete from httptestitem where itemid=' . $itemid); if (!DBexecute('insert into httptestitem' . ' (httptestitemid, httptestid, itemid, type) ' . ' values (' . $httptestitemid . ',' . $httptestid . ',' . $itemid . ',' . $item['httptestitemtype'] . ')')) { $result = false; break; } } } if (!$result && isset($test_added)) { delete_httptest($httptestid); } else { $restult = $httptestid; } DBend($result); return $result; }
protected function doAction() { $object = $this->getInput('object'); $operation = $this->getInput('operation'); $objectids = $this->getInput('objectids'); $data = []; $result = true; DBstart(); switch ($object) { // favourite graphs case 'itemid': case 'graphid': zbx_value2array($objectids); foreach ($objectids as $id) { if ($operation == 'create') { $result &= CFavorite::add('web.favorite.graphids', $id, $object); } elseif ($operation == 'delete') { $result &= CFavorite::remove('web.favorite.graphids', $id, $object); } } $graphs = getFavouriteGraphs(); $graphs = $graphs->toString(); $data['main_block'] = 'jQuery("#' . WIDGET_FAVOURITE_GRAPHS . '").html(' . CJs::encodeJson($graphs) . ');' . 'jQuery(".action-menu").remove();' . 'jQuery("#favouriteGraphs").data(' . '"menu-popup", ' . CJs::encodeJson(CMenuPopupHelper::getFavouriteGraphs()) . ');'; break; // favourite maps // favourite maps case 'sysmapid': zbx_value2array($objectids); foreach ($objectids as $id) { if ($operation == 'create') { $result &= CFavorite::add('web.favorite.sysmapids', $id, $object); } elseif ($operation == 'delete') { $result &= CFavorite::remove('web.favorite.sysmapids', $id, $object); } } $maps = getFavouriteMaps(); $maps = $maps->toString(); $data['main_block'] = 'jQuery("#' . WIDGET_FAVOURITE_MAPS . '").html(' . CJs::encodeJson($maps) . ');' . 'jQuery(".action-menu").remove();' . 'jQuery("#favouriteMaps").data(' . '"menu-popup", ' . CJs::encodeJson(CMenuPopupHelper::getFavouriteMaps()) . ');'; break; // favourite screens, slideshows // favourite screens, slideshows case 'screenid': case 'slideshowid': zbx_value2array($objectids); foreach ($objectids as $id) { if ($operation == 'create') { $result &= CFavorite::add('web.favorite.screenids', $id, $object); } elseif ($operation == 'delete') { $result &= CFavorite::remove('web.favorite.screenids', $id, $object); } } $screens = getFavouriteScreens(); $screens = $screens->toString(); $data['main_block'] = 'jQuery("#' . WIDGET_FAVOURITE_SCREENS . '").html(' . CJs::encodeJson($screens) . ');' . 'jQuery(".action-menu").remove();' . 'jQuery("#favouriteScreens").data(' . '"menu-popup", ' . CJs::encodeJson(CMenuPopupHelper::getFavouriteScreens()) . ');'; break; default: $data['main_block'] = ''; } DBend($result); $this->setResponse(new CControllerResponseData($data)); }
/** * Update item * * {@source} * @access public * @static * @since 1.8 * @version 1 * * @static * @param array $items multidimensional array with items data * @return boolean */ public static function update($items) { $result = false; $itemids = array(); DBstart(false); foreach ($items as $item) { $result = update_item($item['itemid'], $item); if (!$result) { break; } $itemids[$result] = $result; } $result = DBend($result); if ($result) { return $itemids; } else { self::$error = array('error' => ZBX_API_ERROR_INTERNAL, 'data' => 'Internal zabbix error'); return false; } }
/** * Import configuration data. * * @todo for 1.8 version import old class CXmlImport18 is used * * @throws Exception * @throws UnexpectedValueException * @return bool */ public function import() { if (empty($this->reader)) { throw new UnexpectedValueException('Reader is not set.'); } try { // hack to make api throw exceptions // this made to not check all api calls results for false return czbxrpc::$useExceptions = true; DBstart(); $this->data = $this->reader->read($this->source); $version = $this->getImportVersion(); // if import version is 1.8 we use old class that support it. // old import class process hosts, maps and screens separately. if ($version == '1.8') { CXmlImport18::import($this->source); if ($this->options['maps']['updateExisting'] || $this->options['maps']['createMissing']) { CXmlImport18::parseMap($this->options); } if ($this->options['screens']['updateExisting'] || $this->options['screens']['createMissing']) { CXmlImport18::parseScreen($this->options); } if ($this->options['hosts']['updateExisting'] || $this->options['hosts']['createMissing'] || $this->options['templates']['updateExisting'] || $this->options['templates']['createMissing']) { CXmlImport18::parseMain($this->options); } } else { $this->formatter = $this->getFormatter($version); // pass data to formatter // export has root key "zabbix_export" which is not passed $this->formatter->setData($this->data['zabbix_export']); $this->referencer = new CImportReferencer(); // parse all import for references to resolve them all together with less sql count $this->gatherReferences(); $this->processGroups(); $this->processTemplates(); $this->processHosts(); $this->processApplications(); $this->processItems(); $this->processDiscoveryRules(); $this->processTriggers(); $this->processGraphs(); $this->processImages(); $this->processMaps(); // screens should be created after all other elements $this->processTemplateScreens(); $this->processScreens(); } // prevent api from throwing exception czbxrpc::$useExceptions = false; return DBend(true); } catch (Exception $e) { czbxrpc::$useExceptions = false; DBend(false); throw new Exception($e->getMessage(), $e->getCode()); } }
function db_save_httptest($httptestid, $hostid, $application, $name, $authentication, $http_user, $http_password, $delay, $status, $agent, $macros, $steps) { $history = 30; // TODO !!! Allow user to set this parameter $trends = 90; // TODO !!! Allow user to set this parameter if (!preg_match('/^([' . ZBX_PREG_PRINT . '])+$/u', $name)) { error(S_ONLY_CHARACTERS_ARE_ALLOWED); return false; } DBstart(); try { $sql = 'SELECT t.httptestid' . ' FROM httptest t, applications a' . ' WHERE t.applicationid=a.applicationid' . ' AND a.hostid=' . $hostid . ' AND t.name=' . zbx_dbstr($name); $t = DBfetch(DBselect($sql)); if (isset($httptestid) && $t && $t['httptestid'] != $httptestid || $t && !isset($httptestid)) { throw new Exception(S_SCENARIO_WITH_NAME . ' [ ' . $name . ' ] ' . S_ALREADY_EXISTS_SMALL); } $sql = 'SELECT applicationid FROM applications WHERE name=' . zbx_dbstr($application) . ' AND hostid=' . $hostid; if ($applicationid = DBfetch(DBselect($sql))) { $applicationid = $applicationid['applicationid']; } else { $result = CApplication::create(array('name' => $application, 'hostid' => $hostid)); if (!$result) { throw new Exception(S_CANNOT_ADD_NEW_APPLICATION . ' [ ' . $application . ' ]'); } else { $applicationid = reset($result['applicationids']); } } if (isset($httptestid)) { $sql = 'UPDATE httptest SET ' . ' applicationid=' . $applicationid . ', ' . ' name=' . zbx_dbstr($name) . ', ' . ' authentication=' . $authentication . ', ' . ' http_user='******', ' . ' http_password='******', ' . ' delay=' . $delay . ', ' . ' status=' . $status . ', ' . ' agent=' . zbx_dbstr($agent) . ', ' . ' macros=' . zbx_dbstr($macros) . ', ' . ' error=' . zbx_dbstr('') . ', ' . ' curstate=' . HTTPTEST_STATE_UNKNOWN . ' WHERE httptestid=' . $httptestid; if (!DBexecute($sql)) { throw new Exception('DBerror'); } } else { $httptestid = get_dbid('httptest', 'httptestid'); $values = array('httptestid' => $httptestid, 'applicationid' => $applicationid, 'name' => zbx_dbstr($name), 'authentication' => $authentication, 'http_user' => zbx_dbstr($http_user), 'http_password' => zbx_dbstr($http_password), 'delay' => $delay, 'status' => $status, 'agent' => zbx_dbstr($agent), 'macros' => zbx_dbstr($macros), 'curstate' => HTTPTEST_STATE_UNKNOWN); $sql = 'INSERT INTO httptest (' . implode(', ', array_keys($values)) . ') VALUES (' . implode(', ', $values) . ')'; if (!DBexecute($sql)) { throw new Exception('DBerror'); } } $httpstepids = array(); foreach ($steps as $sid => $s) { if (!isset($s['name'])) { $s['name'] = ''; } if (!isset($s['timeout'])) { $s['timeout'] = 15; } if (!isset($s['url'])) { $s['url'] = ''; } if (!isset($s['posts'])) { $s['posts'] = ''; } if (!isset($s['required'])) { $s['required'] = ''; } if (!isset($s['status_codes'])) { $s['status_codes'] = ''; } $result = db_save_step($hostid, $applicationid, $httptestid, $name, $s['name'], $sid + 1, $s['timeout'], $s['url'], $s['posts'], $s['required'], $s['status_codes'], $delay, $history, $trends); if (!$result) { throw new Exception('Cannot create web step'); } $httpstepids[$result] = $result; } /* clean unneeded steps */ $sql = 'SELECT httpstepid FROM httpstep WHERE httptestid=' . $httptestid; $db_steps = DBselect($sql); while ($step_data = DBfetch($db_steps)) { if (!isset($httpstepids[$step_data['httpstepid']])) { delete_httpstep($step_data['httpstepid']); } } $monitored_items = array(array('description' => 'Download speed for scenario \'$1\'', 'key_' => 'web.test.in[' . $name . ',,bps]', 'type' => ITEM_VALUE_TYPE_FLOAT, 'units' => 'Bps', 'httptestitemtype' => HTTPSTEP_ITEM_TYPE_IN), array('description' => 'Failed step of scenario \'$1\'', 'key_' => 'web.test.fail[' . $name . ']', 'type' => ITEM_VALUE_TYPE_UINT64, 'units' => '', 'httptestitemtype' => HTTPSTEP_ITEM_TYPE_LASTSTEP)); foreach ($monitored_items as $item) { $item_data = DBfetch(DBselect('select i.itemid,i.history,i.trends,i.status,i.delta,i.valuemapid ' . ' from items i, httptestitem hi ' . ' where hi.httptestid=' . $httptestid . ' and hi.itemid=i.itemid ' . ' and hi.type=' . $item['httptestitemtype'])); if (!$item_data) { $item_data = DBfetch(DBselect('select i.itemid,i.history,i.trends,i.status,i.delta,i.valuemapid ' . ' from items i where i.key_=' . zbx_dbstr($item['key_']) . ' and i.hostid=' . $hostid)); } $item_args = array('description' => $item['description'], 'key_' => $item['key_'], 'hostid' => $hostid, 'delay' => $delay, 'type' => ITEM_TYPE_HTTPTEST, 'snmp_community' => '', 'snmp_oid' => '', 'value_type' => $item['type'], 'data_type' => ITEM_DATA_TYPE_DECIMAL, 'trapper_hosts' => 'localhost', 'snmp_port' => 161, 'units' => $item['units'], 'multiplier' => 0, 'snmpv3_securityname' => '', 'snmpv3_securitylevel' => 0, 'snmpv3_authpassphrase' => '', 'snmpv3_privpassphrase' => '', 'formula' => 0, 'logtimefmt' => '', 'delay_flex' => '', 'authtype' => 0, 'username' => '', 'password' => '', 'publickey' => '', 'privatekey' => '', 'params' => '', 'ipmi_sensor' => '', 'applications' => array($applicationid)); if (!$item_data) { $item_args['history'] = $history; $item_args['status'] = ITEM_STATUS_ACTIVE; $item_args['delta'] = 0; $item_args['trends'] = $trends; $item_args['valuemapid'] = 0; if (!($itemid = add_item($item_args))) { throw new Exception('Cannot add item'); } } else { $itemid = $item_data['itemid']; $item_args['history'] = $item_data['history']; $item_args['status'] = $item_data['status']; $item_args['delta'] = $item_data['delta']; $item_args['trends'] = $item_data['trends']; $item_args['valuemapid'] = $item_data['valuemapid']; if (!update_item($itemid, $item_args)) { throw new Exception('Cannot update item'); } } $httptestitemid = get_dbid('httptestitem', 'httptestitemid'); DBexecute('delete from httptestitem where itemid=' . $itemid); if (!DBexecute('insert into httptestitem (httptestitemid, httptestid, itemid, type) ' . ' values (' . $httptestitemid . ',' . $httptestid . ',' . $itemid . ',' . $item['httptestitemtype'] . ')')) { throw new Exception('DBerror'); } } return DBend(true); } catch (Exception $e) { error($e->getMessage()); return DBend(false); } }
echo '$("addrm_fav").onclick = function() { add2favorites("graphid", "' . $_REQUEST['favid'] . '"); }' . "\n"; } } $result = DBend($result); if ($page['type'] == PAGE_TYPE_JS && $result) { echo 'switchElementClass("addrm_fav", "iconminus", "iconplus");'; } } } if (!empty($_REQUEST['period']) || !empty($_REQUEST['stime'])) { CScreenBase::calculateTime(array('profileIdx' => 'web.screens', 'profileIdx2' => $pageFilter->graphid, 'updateProfile' => true, 'period' => getRequest('period'), 'stime' => getRequest('stime'))); $curl = new CUrl(); $curl->removeArgument('period'); $curl->removeArgument('stime'); ob_end_clean(); DBstart(); CProfile::flush(); DBend(); redirect($curl->getUrl()); } ob_end_flush(); if ($page['type'] == PAGE_TYPE_JS || $page['type'] == PAGE_TYPE_HTML_BLOCK) { require_once dirname(__FILE__) . '/include/page_footer.php'; exit; } /* * Display */ $data = array('pageFilter' => $pageFilter, 'graphid' => $pageFilter->graphid, 'fullscreen' => $_REQUEST['fullscreen']); // render view $chartsView = new CView('monitoring.charts', $data);
$sysmaps = CMap::get($options); if (empty($sysmaps)) { print 'alert("Access denied!");'; } $selements = get_request('selements', '[]'); $selements = $json->decode($selements, true); $links = get_request('links', '[]'); $links = $json->decode($links, true); @ob_start(); try { $db_selementids = array(); $res = DBselect('SELECT selementid FROM sysmaps_elements WHERE sysmapid=' . $sysmapid); while ($db_selement = DBfetch($res)) { $db_selementids[$db_selement['selementid']] = $db_selement['selementid']; } $transaction = DBstart(); foreach ($selements as $id => $selement) { if ($selement['elementid'] == 0) { $selement['elementtype'] = SYSMAP_ELEMENT_TYPE_IMAGE; } if ($selement['iconid_off'] == 0) { throw new Exception('Cannot save map. Map element "' . $selement['label'] . '" contains no icon.'); } if (isset($selement['new'])) { $selement['sysmapid'] = $sysmapid; $selementids = CMap::addElements($selement); $selementid = reset($selementids); foreach ($links as $id => $link) { if ($link['selementid1'] == $selement['selementid']) { $links[$id]['selementid1'] = $selementid; } else {