/** * Update IconMap. * @param array $iconMaps * @return array */ public function update(array $iconMaps) { if (USER_TYPE_SUPER_ADMIN != self::$userData['type']) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('Only Super Admins can update icon maps.')); } $iconMaps = zbx_toArray($iconMaps); $iconMapids = zbx_objectValues($iconMaps, 'iconmapid'); $updates = []; $duplicates = []; foreach ($iconMaps as $iconMap) { if (!check_db_fields(['iconmapid' => null], $iconMap)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect parameters for icon map update method "%s".', $iconMap['name'])); } if (isset($iconMap['name'])) { if (zbx_empty($iconMap['name'])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map name cannot be empty.')); } elseif (isset($duplicates[$iconMap['name']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot create icon maps with identical name "%s".', $iconMap['name'])); } else { $duplicates[$iconMap['name']] = $iconMap['name']; } } } $this->validateMappings($iconMaps, false); $iconMapsUpd = API::IconMap()->get(['iconmapids' => $iconMapids, 'output' => API_OUTPUT_EXTEND, 'preservekeys' => true, 'selectMappings' => API_OUTPUT_EXTEND]); $oldIconMappings = []; $newIconMappings = []; foreach ($iconMaps as $iconMap) { if (!isset($iconMapsUpd[$iconMap['iconmapid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map with iconmapid "%s" does not exist.', $iconMap['iconmapid'])); } // Existence if (isset($iconMap['name'])) { $iconMapExists = $this->get(['filter' => ['name' => $iconMap['name']], 'output' => ['iconmapid'], 'editable' => true, 'nopermissions' => true, 'preservekeys' => true]); if (($iconMapExists = reset($iconMapExists)) && bccomp($iconMapExists['iconmapid'], $iconMap['iconmapid']) != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" already exists.', $iconMap['name'])); } } if (isset($iconMap['mappings'])) { $mappingsDb = $iconMapsUpd[$iconMap['iconmapid']]['mappings']; foreach ($mappingsDb as $mapping) { $oldIconMappings[] = $mapping; } foreach ($iconMap['mappings'] as $mapping) { $mapping['iconmapid'] = $iconMap['iconmapid']; $newIconMappings[] = $mapping; } } $iconMapid = $iconMap['iconmapid']; unset($iconMap['iconmapid']); if (!empty($iconMap)) { $updates[] = ['values' => $iconMap, 'where' => ['iconmapid' => $iconMapid]]; } } DB::save('icon_map', $iconMaps); DB::replace('icon_mapping', $oldIconMappings, $newIconMappings); return ['iconmapids' => $iconMapids]; }
/** * Updates maintenance time periods. * * @param array $maintenance * @param array $oldMaintenance */ protected function replaceTimePeriods(array $oldMaintenance, array $maintenance) { // replace time periods $timePeriods = DB::replace('timeperiods', $oldMaintenance['timeperiods'], $maintenance['timeperiods']); // link new time periods to maintenance $oldTimePeriods = zbx_toHash($oldMaintenance['timeperiods'], 'timeperiodid'); $newMaintenanceWindows = []; foreach ($timePeriods as $tp) { if (!isset($oldTimePeriods[$tp['timeperiodid']])) { $newMaintenanceWindows[] = ['maintenanceid' => $maintenance['maintenanceid'], 'timeperiodid' => $tp['timeperiodid']]; } } DB::insert('maintenances_windows', $newMaintenanceWindows); }
<?php //require_once('../helpers/assign.php'); require_once '../models/meekrodb.class.php'; error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); $db = new MeekroDB(); $items = json_decode(file_get_contents('https://api.guildwars2.com/v2/items/')); $total = $items[count($items)]; $start = 1; $end = 200; foreach ($items as $i) { $ib[] = $i; if (count($ib) >= 190) { $batchList[] = implode(",", $ib); unset($ib); } } $batchList[] = implode(",", $ib); foreach ($batchList as $ItemRequest) { $itemDetails = json_decode(file_get_contents('https://api.guildwars2.com/v2/items?ids=' . $ItemRequest)); foreach ($itemDetails as $item) { DB::replace('gw2_items', array('id' => $item->id, 'name' => $item->name, 'description' => $item->description, 'item_type' => $item->type, 'rarity' => $item->rarity, 'vendor_value' => $item->vendor_value, 'icon' => $item->icon)); } $price = json_decode(file_get_contents('https://api.guildwars2.com/v2/commerce/prices?ids=' . $ItemRequest)); #// get tp prices foreach ($price as $tp) { $sell_price = $tp->sells->unit_price; DB::update('gw2_items', array('tp_price' => $sell_price), "id=%i", $tp->id); } }
/** * Update existing drules. * * @param array( * druleid => int, * name => string, * proxy_hostid => int, * iprange => string, * delay => string, * status => int, * dchecks => array( * array( * dcheckid => int, * type => int, * ports => string, * key_ => string, * snmp_community => string, * snmpv3_securityname => string, * snmpv3_securitylevel => int, * snmpv3_authpassphrase => string, * snmpv3_privpassphrase => string, * uniq => int, * ), ... * ) * ) $dRules * * @return array */ public function update(array $dRules) { $this->checkInput($dRules); $this->validateRequiredFields($dRules, __FUNCTION__); $dRuleIds = zbx_objectValues($dRules, 'druleid'); $dRulesDb = API::DRule()->get(array('druleids' => $dRuleIds, 'output' => API_OUTPUT_EXTEND, 'selectDChecks' => API_OUTPUT_EXTEND, 'editable' => true, 'preservekeys' => true)); $defaultValues = DB::getDefaults('dchecks'); $dRulesUpdate = array(); $dCheckIdsDelete = array(); $dChecksCreate = array(); $dRuleNamesChanged = array(); // validate drule duplicate names foreach ($dRules as $dRule) { if (!isset($dRulesDb[$dRule['druleid']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } if ($dRulesDb[$dRule['druleid']]['name'] !== $dRule['name']) { if (isset($dRuleNamesChanged[$dRule['name']])) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Discovery rule "%1$s" already exists.', $dRule['name'])); } else { $dRuleNamesChanged[$dRule['name']] = $dRule['name']; } } } if ($dRuleNamesChanged) { $dbDRules = API::getApiService()->select($this->tableName(), array('output' => array('name'), 'filter' => array('name' => $dRuleNamesChanged), 'limit' => 1)); if ($dbDRules) { $dbDRule = reset($dbDRules); self::exception(ZBX_API_ERROR_PARAMETERS, _s('Discovery rule "%1$s" already exists.', $dbDRule['name'])); } } foreach ($dRules as $dRule) { $dRulesUpdate[] = array('values' => $dRule, 'where' => array('druleid' => $dRule['druleid'])); // update dchecks $dbChecks = $dRulesDb[$dRule['druleid']]['dchecks']; $newChecks = array(); $oldChecks = array(); foreach ($dRule['dchecks'] as $check) { $check['druleid'] = $dRule['druleid']; if (!isset($check['dcheckid'])) { $newChecks[] = array_merge($defaultValues, $check); } else { $oldChecks[] = $check; } } $delDCheckIds = array_diff(zbx_objectValues($dbChecks, 'dcheckid'), zbx_objectValues($oldChecks, 'dcheckid')); if ($delDCheckIds) { $this->deleteActionConditions($delDCheckIds); } DB::replace('dchecks', $dbChecks, array_merge($oldChecks, $newChecks)); } DB::update('drules', $dRulesUpdate); return array('druleids' => $dRuleIds); }
/** * Update host groups with new hosts (rewrite). * * @param array $data * @param array $data['groups'] * @param array $data['hosts'] * @param array $data['templates'] * * @return array */ public function massUpdate(array $data) { $groupIds = array_unique(zbx_objectValues(zbx_toArray($data['groups']), 'groupid')); $hostIds = array_unique(zbx_objectValues(isset($data['hosts']) ? zbx_toArray($data['hosts']) : null, 'hostid')); $templateIds = array_unique(zbx_objectValues(isset($data['templates']) ? zbx_toArray($data['templates']) : null, 'templateid')); $workHostIds = array(); // validate permission $allowedGroups = $this->get(array('groupids' => $groupIds, 'editable' => true, 'preservekeys' => true)); foreach ($groupIds as $groupId) { if (!isset($allowedGroups[$groupId])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.')); } } // validate allowed hosts if (!empty($hostIds)) { $allowedHosts = API::Host()->get(array('hostids' => $hostIds, 'editable' => true, 'preservekeys' => true)); foreach ($hostIds as $hostId) { if (!isset($allowedHosts[$hostId])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.')); } $workHostIds[$hostId] = $hostId; } } // validate allowed templates if (!empty($templateIds)) { $allowedTemplates = API::Template()->get(array('templateids' => $templateIds, 'editable' => true, 'preservekeys' => true)); foreach ($templateIds as $templateId) { if (!isset($allowedTemplates[$templateId])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('You do not have permission to perform this operation.')); } $workHostIds[$templateId] = $templateId; } } // get old records $oldRecords = DBfetchArray(DBselect('SELECT *' . ' FROM hosts_groups hg' . ' WHERE ' . dbConditionInt('hg.groupid', $groupIds))); // calculate new records $replaceRecords = array(); $newRecords = array(); $hostIdsToValidate = array(); foreach ($groupIds as $groupId) { $groupRecords = array(); foreach ($oldRecords as $oldRecord) { if ($oldRecord['groupid'] == $groupId) { $groupRecords[] = $oldRecord; } } // find records for replace foreach ($groupRecords as $groupRecord) { if (isset($workHostIds[$groupRecord['hostid']])) { $replaceRecords[] = $groupRecord; } } // find records for create $groupHostIds = zbx_toHash(zbx_objectValues($groupRecords, 'hostid')); $newHostIds = array_diff($workHostIds, $groupHostIds); if ($newHostIds) { foreach ($newHostIds as $newHostId) { $newRecords[] = array('groupid' => $groupId, 'hostid' => $newHostId); } } // find records for delete $deleteHostIds = array_diff($groupHostIds, $workHostIds); if ($deleteHostIds) { foreach ($deleteHostIds as $deleteHostId) { $hostIdsToValidate[$deleteHostId] = $deleteHostId; } } } // validate hosts without groups if ($hostIdsToValidate) { $unlinkable = getUnlinkableHosts($groupIds, $hostIdsToValidate); if (count($unlinkable) != count($hostIdsToValidate)) { self::exception(ZBX_API_ERROR_PARAMETERS, 'One of the objects is left without host group.'); } } // save DB::replace('hosts_groups', $oldRecords, array_merge($replaceRecords, $newRecords)); return array('groupids' => $groupIds); }
public static function readdSiteResponseProcessor($historyID, $responseData) { responseDirectErrorHandler($historyID, $responseData); if (empty($responseData['success'])) { return false; } if (!empty($responseData['success']['error'])) { //There is no variable "$responseData['error_data']" exists in plugin. especially, in add_site task. Error wont come in success array ... DB::update("?:history_additional_data", array('status' => 'error', 'errorMsg' => $responseData['success']['error'], 'error' => $responseData['success']['error_code']), "historyID='" . $historyID . "'"); } elseif (!empty($responseData['success'])) { DB::update("?:history_additional_data", array('status' => 'success'), "historyID='" . $historyID . "'"); $privateKey = $GLOBALS['storage']['oldSite']['readdSitePrivateKey']; $isOpenSSLActive = '1'; if ($responseData['success']['no_openssl']) { $privateKey = NULL; $isOpenSSLActive = '0'; } $URLParts = explode('/', $responseData['success']['site_home']); $siteData = array("privateKey" => $privateKey, "randomSignature" => $responseData['success']['no_openssl']); $siteID = DB::getField("?:history", "siteID", "historyID = '" . $historyID . "'"); DB::update("?:sites", $siteData, "siteID = '" . $siteID . "'"); DB::replace("?:user_access", array('userID' => $GLOBALS['userID'], 'siteID' => $siteID)); unset($GLOBALS['storage']['oldSite']); //---------------------------post process------------------------> $allParams = array('action' => 'getStats', 'args' => array('siteIDs' => array($siteID), 'extras' => array('directExecute' => true, 'doNotShowUser' => true))); panelRequestManager::handler($allParams); setHook('postReaddSite', $siteID); //check this once } }
public function save() { $cols = []; $vals = []; foreach (static::$db_columns as $c) { if (isset($this->{$c[0]})) { $cols[] = $c[0]; $vals[$c[0]] = $this->{$c[0]}; } } if (isset($this->{static::$db_idcolumn})) { $cols[] = static::$db_idcolumn; $vals[static::$db_idcolumn] = $this->{static::$db_idcolumn}; DB::replace(static::$db_tablename, $cols, $vals); } else { $this->{static::$db_idcolumn} = DB::insert(static::$db_tablename, $cols, $vals); } }
/** * Update host groups with new hosts and templates. * * @param array $data * @param array $data['groups'] * @param array $data['hosts'] * @param array $data['templates'] * * @return array returns array of group IDs that hosts and templates have been added to and removed from */ public function massUpdate(array $data) { $data['groups'] = zbx_toArray($data['groups']); $data['hosts'] = isset($data['hosts']) ? zbx_toArray($data['hosts']) : array(); $data['templates'] = isset($data['templates']) ? zbx_toArray($data['templates']) : array(); $this->validateMassUpdate($data); $groupIds = zbx_objectValues($data['groups'], 'groupid'); $hostIds = zbx_objectValues($data['hosts'], 'hostid'); $templateIds = zbx_objectValues($data['templates'], 'templateid'); $objectIds = zbx_toHash(array_merge($hostIds, $templateIds)); // get old records and skip discovered hosts $oldRecords = DBfetchArray(DBselect('SELECT hg.hostid,hg.groupid,hg.hostgroupid' . ' FROM hosts_groups hg,hosts h' . ' WHERE ' . dbConditionInt('hg.groupid', $groupIds) . ' AND hg.hostid=h.hostid' . ' AND h.flags=' . ZBX_FLAG_DISCOVERY_NORMAL)); // calculate new records $replaceRecords = array(); $newRecords = array(); foreach ($groupIds as $groupId) { $groupRecords = array(); foreach ($oldRecords as $oldRecord) { if ($oldRecord['groupid'] == $groupId) { $groupRecords[] = $oldRecord; } } // find records for replace foreach ($groupRecords as $groupRecord) { if (isset($objectIds[$groupRecord['hostid']])) { $replaceRecords[] = $groupRecord; } } // find records for create $groupHostIds = zbx_toHash(zbx_objectValues($groupRecords, 'hostid')); $newHostIds = array_diff($objectIds, $groupHostIds); foreach ($newHostIds as $newHostId) { $newRecords[] = array('groupid' => $groupId, 'hostid' => $newHostId); } } DB::replace('hosts_groups', $oldRecords, array_merge($replaceRecords, $newRecords)); return array('groupids' => $groupIds); }
if (!empty($_POST['user_id'])) { $data['user_id'] = intval($_POST['user_id']); } elseif (!empty($_GET['user_id'])) { $data['user_id'] = intval($_GET['user_id']); } } } else { $data['user_id'] = $user['user_id']; } if (!empty($_POST['password'])) { $data['password'] = md5($_POST['password']); } if ($data['user_id']) { DB::update('ebay_users', $data, "WHERE `user_id` = {$data['user_id']}"); } else { DB::replace('ebay_users', $data); } if ($user['group'] === 'admin') { header('location:dashboard.php'); } else { header('location:profile.php'); } } require_once 'blocks/head.php'; require_once 'blocks/menu.php'; ?> <body> <div class="container-fluid span14" style="margin-top: 0px"> <div class="row-fluid">
/** * Update applications. * * @param array $applications * * @return array */ public function update(array $applications) { $update = array(); $applicationTemplates = array(); foreach ($applications as $application) { if (isset($application['applicationTemplates'])) { foreach ($application['applicationTemplates'] as $applicationTemplate) { $applicationTemplates[] = $applicationTemplate; } unset($application['applicationTemplates']); } $update[] = array('values' => $application, 'where' => array('applicationid' => $application['applicationid'])); } DB::update('applications', $update); // replace existing application templates if ($applicationTemplates) { $dbApplicationTemplates = DBfetchArray(DBselect('SELECT * ' . ' FROM application_template at' . ' WHERE ' . dbConditionInt('at.applicationid', zbx_objectValues($applications, 'applicationid')))); DB::replace('application_template', $dbApplicationTemplates, $applicationTemplates); } // TODO: REMOVE info $dbCursor = DBselect('SELECT a.name,h.name AS hostname' . ' FROM applications a' . ' INNER JOIN hosts h ON h.hostid=a.hostid' . ' WHERE ' . dbConditionInt('a.applicationid', zbx_objectValues($applications, 'applicationid'))); while ($app = DBfetch($dbCursor)) { info(_s('Updated: Application "%1$s" on "%2$s".', $app['name'], $app['hostname'])); } return $applications; }
/** * Update host groups with new hosts (rewrite). * * @param array $data * @param array $data['groups'] * @param array $data['hosts'] * @param array $data['templates'] * * @return array */ public function massUpdate(array $data) { $groupIds = array_unique(zbx_objectValues(zbx_toArray($data['groups']), 'groupid')); $hostIds = array_unique(zbx_objectValues(isset($data['hosts']) ? zbx_toArray($data['hosts']) : null, 'hostid')); $templateIds = array_unique(zbx_objectValues(isset($data['templates']) ? zbx_toArray($data['templates']) : null, 'templateid')); $workHostIds = array(); // validate permission $allowedGroups = $this->get(array('output' => array('groupid'), 'groupids' => $groupIds, 'editable' => true, 'preservekeys' => true)); foreach ($groupIds as $groupId) { if (!isset($allowedGroups[$groupId])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } } // validate allowed hosts if (!empty($hostIds)) { if (!API::Host()->isWritable($hostIds)) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } // check if any of the hosts are discovered $this->checkValidator($hostIds, new CHostNormalValidator(array('message' => _('Cannot update groups for discovered host "%1$s".')))); $workHostIds = zbx_toHash($hostIds); } // validate allowed templates if (!empty($templateIds)) { $allowedTemplates = API::Template()->get(array('output' => array('templateid'), 'templateids' => $templateIds, 'editable' => true, 'preservekeys' => true)); foreach ($templateIds as $templateId) { if (!isset($allowedTemplates[$templateId])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } $workHostIds[$templateId] = $templateId; } } // get old records // skip discovered hosts $oldRecords = DBfetchArray(DBselect('SELECT *' . ' FROM hosts_groups hg,hosts h' . ' WHERE ' . dbConditionInt('hg.groupid', $groupIds) . ' AND hg.hostid=h.hostid' . ' AND h.flags=' . ZBX_FLAG_DISCOVERY_NORMAL)); // calculate new records $replaceRecords = array(); $newRecords = array(); $hostIdsToValidate = array(); foreach ($groupIds as $groupId) { $groupRecords = array(); foreach ($oldRecords as $oldRecord) { if ($oldRecord['groupid'] == $groupId) { $groupRecords[] = $oldRecord; } } // find records for replace foreach ($groupRecords as $groupRecord) { if (isset($workHostIds[$groupRecord['hostid']])) { $replaceRecords[] = $groupRecord; } } // find records for create $groupHostIds = zbx_toHash(zbx_objectValues($groupRecords, 'hostid')); $newHostIds = array_diff($workHostIds, $groupHostIds); if ($newHostIds) { foreach ($newHostIds as $newHostId) { $newRecords[] = array('groupid' => $groupId, 'hostid' => $newHostId); } } // find records for delete $deleteHostIds = array_diff($groupHostIds, $workHostIds); if ($deleteHostIds) { foreach ($deleteHostIds as $deleteHostId) { $hostIdsToValidate[$deleteHostId] = $deleteHostId; } } } // validate hosts without groups if ($hostIdsToValidate) { $unlinkable = getUnlinkableHostIds($groupIds, $hostIdsToValidate); if (count($unlinkable) != count($hostIdsToValidate)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('One of the objects is left without a host group.')); } } // save DB::replace('hosts_groups', $oldRecords, array_merge($replaceRecords, $newRecords)); return array('groupids' => $groupIds); }
/** * Update existing drules. * * @param array( * druleid => int, * name => string, * proxy_hostid => int, * iprange => string, * delay => string, * status => int, * dchecks => array( * array( * dcheckid => int, * type => int, * ports => string, * key_ => string, * snmp_community => string, * snmpv3_securityname => string, * snmpv3_securitylevel => int, * snmpv3_authpassphrase => string, * snmpv3_privpassphrase => string, * uniq => int, * ), ... * ) * ) $dRules * * @return array */ public function update(array $dRules) { $this->checkInput($dRules); $this->validateRequiredFields($dRules, __FUNCTION__); $dRuleIds = zbx_objectValues($dRules, 'druleid'); $dRulesDb = API::DRule()->get(array('druleids' => $dRuleIds, 'output' => API_OUTPUT_EXTEND, 'selectDChecks' => API_OUTPUT_EXTEND, 'editable' => true, 'preservekeys' => true)); $defaultValues = DB::getDefaults('dchecks'); $dRulesUpdate = $dCheckIdsDelete = $dChecksCreate = array(); foreach ($dRules as $dRule) { // validate drule duplicate names if (strcmp($dRulesDb[$dRule['druleid']]['name'], $dRule['name']) != 0) { if ($this->exists($dRule)) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Discovery rule "%s" already exists.', $dRule['name'])); } } $dRulesUpdate[] = array('values' => $dRule, 'where' => array('druleid' => $dRule['druleid'])); // update dchecks $dbChecks = $dRulesDb[$dRule['druleid']]['dchecks']; $newChecks = array(); foreach ($dRule['dchecks'] as $cnum => $check) { if (!isset($check['druleid'])) { $check['druleid'] = $dRule['druleid']; unset($check['dcheckid']); $newChecks[] = array_merge($defaultValues, $check); unset($dRule['dchecks'][$cnum]); } } $delDCheckIds = array_diff(zbx_objectValues($dbChecks, 'dcheckid'), zbx_objectValues($dRule['dchecks'], 'dcheckid')); if ($delDCheckIds) { $this->deleteActionConditions($delDCheckIds); } DB::replace('dchecks', $dbChecks, array_merge($dRule['dchecks'], $newChecks)); } DB::update('drules', $dRulesUpdate); return array('druleids' => $dRuleIds); }
protected function updateReal($items) { $items = zbx_toArray($items); $data = []; foreach ($items as $inum => $item) { $data[] = ['values' => $item, 'where' => ['itemid' => $item['itemid']]]; } $result = DB::update('items', $data); if (!$result) { self::exception(ZBX_API_ERROR_PARAMETERS, 'DBerror'); } $itemids = []; $itemidsWithApplications = []; $itemApplications = []; foreach ($items as $item) { if (!isset($item['applications'])) { array_push($itemids, $item['itemid']); continue; } $itemidsWithApplications[] = $item['itemid']; foreach ($item['applications'] as $anum => $appid) { $itemApplications[] = ['applicationid' => $appid, 'itemid' => $item['itemid']]; } } if (!empty($itemidsWithApplications)) { DB::delete('items_applications', ['itemid' => $itemidsWithApplications]); DB::insert('items_applications', $itemApplications); } // application prototypes that are no longer linked to items will be deleted from database $application_prototypes_to_remove = []; // currently linked item prototypes and application prototypes $old_records = []; // records that will be added or replaced with in item_application_prototype table $new_records = []; // Get discovery rule IDs for all items. $itemids_with_application_prototypes = []; foreach ($items as $item) { if (array_key_exists('applicationPrototypes', $item) && is_array($item['applicationPrototypes']) && !array_key_exists('ruleid', $item)) { $itemids_with_application_prototypes[$item['itemid']] = true; } } if ($itemids_with_application_prototypes) { $discovery_rules = DBfetchArray(DBselect('SELECT id.itemid,id.parent_itemid' . ' FROM item_discovery id' . ' WHERE ' . dbConditionInt('id.itemid', array_keys($itemids_with_application_prototypes)))); $discovery_rules = zbx_toHash($discovery_rules, 'itemid'); } // Process application prototypes. foreach ($items as $item) { /* * "applicationPrototypes" is an array of "name" properties. It can also be an empty array in case * application prototypes should be unlinked from item prototypes. */ if (array_key_exists('applicationPrototypes', $item) && is_array($item['applicationPrototypes'])) { // Get discovery rule ID for current item prototype, if it is not yet set. if (array_key_exists('ruleid', $item)) { $discovery_ruleid = $item['ruleid']; } else { $discovery_ruleid = $discovery_rules[$item['itemid']]['parent_itemid']; } /* * Get currently linked application prototypes to current item prototype together * existing application prototypes in this discovery rule. There cannot be unlinked * application prototypes. */ $db_item_application_prototypes = DBfetchArray(DBselect('SELECT DISTINCT ap.application_prototypeid,ap.name,ap.templateid,iap.item_application_prototypeid' . ' FROM application_prototype ap,item_application_prototype iap' . ' WHERE ap.application_prototypeid=iap.application_prototypeid' . ' AND ap.itemid=' . zbx_dbstr($discovery_ruleid) . ' AND iap.itemid=' . zbx_dbstr($item['itemid']))); // Gather all item application prototype records in $old_records for each item. foreach ($db_item_application_prototypes as $db_item_application_prototype) { $id = $db_item_application_prototype['item_application_prototypeid']; $application_prototypeid = $db_item_application_prototype['application_prototypeid']; $old_records[$id] = ['item_application_prototypeid' => $id, 'application_prototypeid' => $application_prototypeid, 'itemid' => $item['itemid']]; } $application_prototypes = zbx_toHash($item['applicationPrototypes'], 'name'); /* * Check given application prototype names if they exist in database. If they exist, return IDs. * Other application prototypes will be created later. */ $db_application_prototypes = DBfetchArray(DBselect('SELECT ap.application_prototypeid,ap.name,ap.templateid' . ' FROM application_prototype ap' . ' WHERE ap.itemid=' . zbx_dbstr($discovery_ruleid) . ' AND ' . dbConditionString('ap.name', array_keys($application_prototypes)))); $names = []; foreach ($db_application_prototypes as $db_application_prototype) { $names[] = (string) $db_application_prototype['name']; } $db_application_prototypes = zbx_toHash($db_application_prototypes, 'name'); // New application prototype names that need to be created in database. $application_prototypes_to_create = []; // An array that contains "application_prototypeid" as key and "templateid" as value. $application_prototypes_to_update = []; /* * Cycle each application prototype and check if it exists. If not, we will create it. Else for existing * application prototypes check only if template ID has changed. No other parameters can change. * Otherwise application prototypes are deleted or stay unchanged. */ foreach ($application_prototypes as $application_prototype) { if (!in_array((string) $application_prototype['name'], $names, true)) { $application_prototypes_to_create[] = ['itemid' => $discovery_ruleid, 'name' => $application_prototype['name'], 'templateid' => array_key_exists('templateid', $application_prototype) ? $application_prototype['templateid'] : null]; } elseif (array_key_exists('templateid', $application_prototype)) { $db_application_prototype = $db_application_prototypes[$application_prototype['name']]; if ($db_application_prototype['templateid'] != $application_prototype['templateid']) { $id = $db_application_prototype['application_prototypeid']; $application_prototypes_to_update[$id] = $application_prototype['templateid']; } } } // Collect already existing application prototype IDs. foreach ($db_application_prototypes as $db_application_prototype) { $application_prototypeid = $db_application_prototype['application_prototypeid']; $new_application_prototype = $application_prototypes[$db_application_prototype['name']]; $templateid = array_key_exists('templateid', $new_application_prototype) ? $new_application_prototype['templateid'] : 0; $new_records[] = ['application_prototypeid' => $application_prototypeid, 'itemid' => $item['itemid']]; } // Create new application prototypes, get new IDs. if ($application_prototypes_to_create) { $ids = DB::insert('application_prototype', $application_prototypes_to_create); foreach ($ids as $id) { $new_records[] = ['application_prototypeid' => $id, 'itemid' => $item['itemid']]; } } // Update application prototype template IDs. if ($application_prototypes_to_update) { foreach ($application_prototypes_to_update as $application_prototypeid => $templateid) { DB::update('application_prototype', ['values' => ['templateid' => $templateid], 'where' => ['application_prototypeid' => $application_prototypeid]]); } } /* * Collect application prototype IDs that will be unlinked from item prototypes, in case those * application prototypes should be permanently deleted when no longer linked to any item prototypes. */ $db_item_application_prototypes = zbx_toHash($db_item_application_prototypes, 'name'); $application_prototypes_to_unlink = array_diff_key($db_item_application_prototypes, $application_prototypes); foreach ($application_prototypes_to_unlink as $application_prototype) { $application_prototypes_to_remove[$application_prototype['application_prototypeid']] = true; } } } // Remove unchanged records. foreach ($old_records as $i => $old_record) { foreach ($new_records as $j => $new_record) { if ($old_record['application_prototypeid'] == $new_record['application_prototypeid'] && $old_record['itemid'] == $new_record['itemid']) { unset($old_records[$i]); unset($new_records[$j]); } } } // Find discovered applications that have been discovered from only one rule and delete them. DB::replace('item_application_prototype', $old_records, $new_records); // Find and delete application prototypes from database that are no longer linked to any item prototypes. if ($application_prototypes_to_remove) { $this->deleteApplicationPrototypes(array_keys($application_prototypes_to_remove)); } // TODO: REMOVE info $itemHosts = $this->get(['itemids' => $itemids, 'output' => ['name'], 'selectHosts' => ['name'], 'nopermissions' => true]); foreach ($itemHosts as $item) { $host = reset($item['hosts']); info(_s('Updated: Item prototype "%1$s" on "%2$s".', $item['name'], $host['name'])); } }
private static function updateGroupsSites($params) { if (empty($params)) { return false; } foreach ($params as $groupID => $siteIDs) { if (empty($siteIDs)) { continue; } DB::delete("?:groups_sites", "groupID = '" . $groupID . "'"); foreach ($siteIDs as $siteID) { if (is_numeric($siteID)) { DB::replace("?:groups_sites", array('groupID' => $groupID, 'siteID' => $siteID)); } } } return true; }
public static function getStatsResponseProcessor($historyID, $responseData) { responseDirectErrorHandler($historyID, $responseData); $siteID = DB::getField("?:history", "siteID", "historyID='" . $historyID . "'"); if (empty($siteID)) { return false; } if (empty($responseData['success'])) { //For left site color code DB::update("?:sites", array('connectionStatus' => '0'), "siteID = '" . $siteID . "'"); } else { DB::update("?:sites", array('connectionStatus' => '1'), "siteID = '" . $siteID . "'"); } if (empty($responseData['success'])) { return false; } self::backupStatusCheck($responseData); if (!empty($responseData['success']['error'])) { DB::update("?:history_additional_data", array('status' => 'error', 'errorMsg' => $responseData['success']['error'], 'error' => $responseData['success']['error_code']), "historyID='" . $historyID . "'"); return false; } DB::update("?:history_additional_data", array('status' => 'success'), "historyID='" . $historyID . "' AND uniqueName = 'getStats'"); $siteStatsData = array(); $siteStatsData['siteID'] = $siteID; $siteStatsData['stats'] = base64_encode(serialize($responseData['success'])); $siteStatsData['lastUpdatedTime'] = time(); DB::replace("?:site_stats", $siteStatsData); $callOpt = false; if ($responseData['success']['use_cookie'] == 1) { $callOpt = DB::getField("?:sites", "callOpt", "siteID = '" . $siteID . "'"); if (!empty($callOpt)) { $callOpt = unserialize($callOpt); } else { $callOpt = array(); } $callOpt['useCookie'] = 1; $callOpt = serialize($callOpt); } if (!empty($responseData['success']['wordpress_version']) && !empty($responseData['success']['client_version'])) { $updateSiteData = array("WPVersion" => $responseData['success']['wordpress_version'], "pluginVersion" => $responseData['success']['client_version']); if (!empty($callOpt)) { $updateSiteData['callOpt'] = $callOpt; } DB::update("?:sites", $updateSiteData, "siteID = '" . $siteID . "'"); } }