/** * Delete existing trigger prototypes. * * @see https://www.zabbix.com/documentation/3.0/manual/api/reference/triggerprototype/delete * * @param array $triggerPrototypeIds * @param bool $nopermissions * * @throws APIException * * @return array */ public function delete(array $triggerPrototypeIds, $nopermissions = false) { if (!$triggerPrototypeIds) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); } // TODO: remove $nopermissions hack if (!$nopermissions) { $dbTriggerPrototypes = $this->get(['triggerids' => $triggerPrototypeIds, 'output' => ['description', 'expression', 'templateid'], 'editable' => true, 'preservekeys' => true]); foreach ($triggerPrototypeIds as $triggerPrototypeId) { if (!isset($dbTriggerPrototypes[$triggerPrototypeId])) { self::exception(ZBX_API_ERROR_PARAMETERS, _('No permissions to referred object or it does not exist!')); } $dbTriggerPrototype = $dbTriggerPrototypes[$triggerPrototypeId]; if ($dbTriggerPrototype['templateid'] != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot delete templated trigger "%1$s:%2$s".', $dbTriggerPrototype['description'], CMacrosResolverHelper::resolveTriggerExpression($dbTriggerPrototype['expression']))); } } } // get child trigger prototypes $parentTriggerPrototypeIds = $triggerPrototypeIds; do { $dbTriggerPrototypes = DBselect('SELECT triggerid' . ' FROM triggers' . ' WHERE ' . dbConditionInt('templateid', $parentTriggerPrototypeIds)); $parentTriggerPrototypeIds = []; while ($dbTriggerPrototype = DBfetch($dbTriggerPrototypes)) { $parentTriggerPrototypeIds[] = $dbTriggerPrototype['triggerid']; $triggerPrototypeIds[$dbTriggerPrototype['triggerid']] = $dbTriggerPrototype['triggerid']; } } while ($parentTriggerPrototypeIds); // delete triggers created from this prototype $createdTriggerIds = DBfetchColumn(DBselect('SELECT triggerid' . ' FROM trigger_discovery' . ' WHERE ' . dbConditionInt('parent_triggerid', $triggerPrototypeIds)), 'triggerid'); if ($createdTriggerIds) { API::Trigger()->delete($createdTriggerIds, true); } // select all trigger prototypes which are deleted (include children) $dbTriggerPrototypes = $this->get(['triggerids' => $triggerPrototypeIds, 'output' => ['triggerid', 'description', 'expression'], 'nopermissions' => true, 'preservekeys' => true, 'selectHosts' => ['name']]); // TODO: REMOVE info foreach ($dbTriggerPrototypes as $dbTriggerPrototype) { info(_s('Deleted: Trigger prototype "%1$s" on "%2$s".', $dbTriggerPrototype['description'], implode(', ', zbx_objectValues($dbTriggerPrototype['hosts'], 'name')))); add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_TRIGGER_PROTOTYPE, $dbTriggerPrototype['triggerid'], $dbTriggerPrototype['description'] . ':' . $dbTriggerPrototype['expression'], null, null, null); } DB::delete('triggers', ['triggerid' => $triggerPrototypeIds]); return ['triggerids' => $triggerPrototypeIds]; }
/** * Delete DiscoveryRules. * * @param array $ruleids * * @return array */ public function delete($ruleids, $nopermissions = false) { if (empty($ruleids)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.')); } $delRuleIds = zbx_toArray($ruleids); $ruleids = zbx_toHash($ruleids); $delRules = $this->get(array('output' => API_OUTPUT_EXTEND, 'itemids' => $ruleids, 'editable' => true, 'preservekeys' => true, 'selectHosts' => array('name'))); // TODO: remove $nopermissions hack if (!$nopermissions) { foreach ($ruleids as $ruleid) { if (!isset($delRules[$ruleid])) { self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!')); } if ($delRules[$ruleid]['templateid'] != 0) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete templated items.')); } } } // get child discovery rules $parentItemids = $ruleids; $childTuleids = array(); do { $dbItems = DBselect('SELECT i.itemid FROM items i WHERE ' . dbConditionInt('i.templateid', $parentItemids)); $parentItemids = array(); while ($dbItem = DBfetch($dbItems)) { $parentItemids[$dbItem['itemid']] = $dbItem['itemid']; $childTuleids[$dbItem['itemid']] = $dbItem['itemid']; } } while (!empty($parentItemids)); $delRulesChilds = $this->get(array('output' => API_OUTPUT_EXTEND, 'itemids' => $childTuleids, 'nopermissions' => true, 'preservekeys' => true, 'selectHosts' => array('name'))); $delRules = array_merge($delRules, $delRulesChilds); $ruleids = array_merge($ruleids, $childTuleids); $iprototypeids = array(); $dbItems = DBselect('SELECT i.itemid' . ' FROM item_discovery id,items i' . ' WHERE i.itemid=id.itemid' . ' AND ' . dbConditionInt('parent_itemid', $ruleids)); while ($item = DBfetch($dbItems)) { $iprototypeids[$item['itemid']] = $item['itemid']; } if (!empty($iprototypeids)) { if (!API::Itemprototype()->delete($iprototypeids, true)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete discovery rule')); } } // delete host prototypes $hostPrototypeIds = DBfetchColumn(DBselect('SELECT hd.hostid' . ' FROM host_discovery hd' . ' WHERE ' . dbConditionInt('hd.parent_itemid', $ruleids)), 'hostid'); if ($hostPrototypeIds) { if (!API::HostPrototype()->delete($hostPrototypeIds, true)) { self::exception(ZBX_API_ERROR_PARAMETERS, _('Cannot delete host prototype.')); } } // delete LLD rules DB::delete('items', array('itemid' => $ruleids)); // TODO: remove info from API foreach ($delRules as $item) { $host = reset($item['hosts']); info(_s('Deleted: Discovery rule "%1$s" on "%2$s".', $item['name'], $host['name'])); } return array('ruleids' => $delRuleIds); }
/** * Update web item application linkage. * * @param array $itemIds * @param string $appId */ protected function updateItemsApplications(array $itemIds, $appId) { if (empty($appId)) { DB::delete('items_applications', array('itemid' => $itemIds)); } else { $linkedItemIds = DBfetchColumn(DBselect('SELECT ia.itemid FROM items_applications ia WHERE ' . dbConditionInt('ia.itemid', $itemIds)), 'itemid'); if (!empty($linkedItemIds)) { DB::update('items_applications', array('values' => array('applicationid' => $appId), 'where' => array('itemid' => $linkedItemIds))); } $notLinkedItemIds = array_diff($itemIds, $linkedItemIds); if (!empty($notLinkedItemIds)) { $insert = array(); foreach ($notLinkedItemIds as $itemId) { $insert[] = array('itemid' => $itemId, 'applicationid' => $appId); } DB::insert('items_applications', $insert); } } }
/** * Return IDs of applications that are children only (!) of the given parents. * * @param array $parentApplicationIds * * @return array */ public function fetchExclusiveChildIds(array $parentApplicationIds) { return DBfetchColumn(DBselect('SELECT at.applicationid ' . ' FROM application_template at' . ' WHERE ' . dbConditionInt('at.templateid', $parentApplicationIds) . ' AND NOT EXISTS (SELECT NULL FROM application_template at2 WHERE ' . ' at.applicationid=at2.applicationid' . ' AND ' . dbConditionInt('at2.templateid', $parentApplicationIds, true) . ')'), 'applicationid'); }