Example #1
0
function delete_host($hostids, $unlink_mode = false)
{
    zbx_value2array($hostids);
    if (empty($hostids)) {
        return true;
    }
    $ret = false;
    // unlink child hosts
    $db_childs = get_hosts_by_templateid($hostids);
    while ($db_child = DBfetch($db_childs)) {
        unlink_template($db_child['hostid'], $hostids, $unlink_mode);
    }
    // delete web tests
    $del_httptests = array();
    $db_httptests = get_httptests_by_hostid($hostids);
    while ($db_httptest = DBfetch($db_httptests)) {
        $del_httptests[$db_httptest['httptestid']] = $db_httptest['httptestid'];
    }
    if (!empty($del_httptests)) {
        delete_httptest($del_httptests);
    }
    // delete items -> triggers -> graphs
    $del_items = array();
    $db_items = get_items_by_hostid($hostids);
    while ($db_item = DBfetch($db_items)) {
        $del_items[$db_item['itemid']] = $db_item['itemid'];
    }
    delete_item($del_items);
    // delete screen items
    DBexecute('DELETE FROM screens_items WHERE ' . DBcondition('resourceid', $hostids)) . ' AND resourcetype=' . SCREEN_RESOURCE_HOST_TRIGGERS;
    // delete host from maps
    delete_sysmaps_elements_with_hostid($hostids);
    // delete host from maintenances
    DBexecute('DELETE FROM maintenances_hosts WHERE ' . DBcondition('hostid', $hostids));
    // delete host from group
    DBexecute('DELETE FROM hosts_groups WHERE ' . DBcondition('hostid', $hostids));
    // delete host from template linkages
    DBexecute('DELETE FROM hosts_templates WHERE ' . DBcondition('hostid', $hostids));
    // disable actions
    $actionids = array();
    // conditions
    $sql = 'SELECT DISTINCT actionid ' . ' FROM conditions ' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST . ' AND ' . DBcondition('value', $hostids, false, true);
    // FIXED[POSIBLE value type violation]!!!
    $db_actions = DBselect($sql);
    while ($db_action = DBfetch($db_actions)) {
        $actionids[$db_action['actionid']] = $db_action['actionid'];
    }
    DBexecute('UPDATE actions ' . ' SET status=' . ACTION_STATUS_DISABLED . ' WHERE ' . DBcondition('actionid', $actionids));
    // operations
    $sql = 'SELECT DISTINCT o.actionid ' . ' FROM operations o ' . ' WHERE o.operationtype IN (' . OPERATION_TYPE_GROUP_ADD . ',' . OPERATION_TYPE_GROUP_REMOVE . ') ' . ' AND ' . DBcondition('o.objectid', $hostids);
    $db_actions = DBselect($sql);
    while ($db_action = DBfetch($db_actions)) {
        $actionids[$db_action['actionid']] = $db_action['actionid'];
    }
    if (!empty($actionids)) {
        DBexecute('UPDATE actions ' . ' SET status=' . ACTION_STATUS_DISABLED . ' WHERE ' . DBcondition('actionid', $actionids));
    }
    // delete action conditions
    DBexecute('DELETE FROM conditions ' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST . ' AND ' . DBcondition('value', $hostids, false, true));
    // FIXED[POSIBLE value type violation]!!!
    // delete action operations
    DBexecute('DELETE FROM operations ' . ' WHERE operationtype IN (' . OPERATION_TYPE_TEMPLATE_ADD . ',' . OPERATION_TYPE_TEMPLATE_REMOVE . ') ' . ' AND ' . DBcondition('objectid', $hostids));
    // delete host profile
    delete_host_profile($hostids);
    delete_host_profile_ext($hostids);
    $applicationids = array();
    $query = 'SELECT a.applicationid' . ' FROM applications a' . ' WHERE	' . DBcondition('a.hostid', $hostids);
    $db_applications = DBselect($query);
    while ($app = DBfetch($db_applications)) {
        $applicationids[] = $app['applicationid'];
    }
    $result = delete_application($applicationids);
    if (!$result) {
        return false;
    }
    // delete host
    foreach ($hostids as $id) {
        /* The section should be improved */
        $host_old = get_host_by_hostid($id);
        $result = DBexecute('DELETE FROM hosts WHERE hostid=' . $id);
        if ($result) {
            if ($host_old['status'] == HOST_STATUS_TEMPLATE) {
                info(S_TEMPLATE . SPACE . $host_old['host'] . SPACE . S_HOST_HAS_BEEN_DELETED_MSG_PART2);
                add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_TEMPLATE, $id, $host_old['host'], 'hosts', NULL, NULL);
            } else {
                info(S_HOST_HAS_BEEN_DELETED_MSG_PART1 . SPACE . $host_old['host'] . SPACE . S_HOST_HAS_BEEN_DELETED_MSG_PART2);
                add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $id, $host_old['host'], 'hosts', NULL, NULL);
            }
        } else {
            break;
        }
    }
    return $result;
}
Example #2
0
 /**
  * Delete Host
  *
  * @param string|array 	$hostIds
  * @param bool			$nopermissions
  *
  * @return array|boolean
  */
 public function delete($hostIds, $nopermissions = false)
 {
     $hostIds = zbx_toArray($hostIds);
     // deprecated input support
     if ($hostIds && is_array($hostIds[0])) {
         $this->deprecated('Passing objects is deprecated, use an array of IDs instead.');
         foreach ($hostIds as $host) {
             if (!check_db_fields(array('hostid' => null), $host)) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('No host ID given.'));
             }
         }
         $hostIds = zbx_objectValues($hostIds, 'hostid');
     }
     $this->validateDelete($hostIds, $nopermissions);
     // delete the discovery rules first
     $delRules = API::DiscoveryRule()->get(array('hostids' => $hostIds, 'nopermissions' => true, 'preservekeys' => true));
     if ($delRules) {
         API::DiscoveryRule()->delete(array_keys($delRules), true);
     }
     // delete the items
     $delItems = API::Item()->get(array('templateids' => $hostIds, 'output' => array('itemid'), 'nopermissions' => true, 'preservekeys' => true));
     if ($delItems) {
         API::Item()->delete(array_keys($delItems), true);
     }
     // delete web tests
     $delHttptests = array();
     $dbHttptests = get_httptests_by_hostid($hostIds);
     while ($dbHttptest = DBfetch($dbHttptests)) {
         $delHttptests[$dbHttptest['httptestid']] = $dbHttptest['httptestid'];
     }
     if (!empty($delHttptests)) {
         API::HttpTest()->delete($delHttptests, true);
     }
     // delete screen items
     DB::delete('screens_items', array('resourceid' => $hostIds, 'resourcetype' => SCREEN_RESOURCE_HOST_TRIGGERS));
     // delete host from maps
     if (!empty($hostIds)) {
         DB::delete('sysmaps_elements', array('elementtype' => SYSMAP_ELEMENT_TYPE_HOST, 'elementid' => $hostIds));
     }
     // disable actions
     // actions from conditions
     $actionids = array();
     $sql = 'SELECT DISTINCT actionid' . ' FROM conditions' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST . ' AND ' . dbConditionString('value', $hostIds);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $sql = 'SELECT DISTINCT o.actionid' . ' FROM operations o, opcommand_hst oh' . ' WHERE o.operationid=oh.operationid' . ' AND ' . dbConditionInt('oh.hostid', $hostIds);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         $update = array();
         $update[] = array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionids));
         DB::update('actions', $update);
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_HOST, 'value' => $hostIds));
     // delete action operation commands
     $operationids = array();
     $sql = 'SELECT DISTINCT oh.operationid' . ' FROM opcommand_hst oh' . ' WHERE ' . dbConditionInt('oh.hostid', $hostIds);
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('opcommand_hst', array('hostid' => $hostIds));
     // delete empty operations
     $delOperationids = array();
     $sql = 'SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS(SELECT oh.opcommand_hstid FROM opcommand_hst oh WHERE oh.operationid=o.operationid)';
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', array('operationid' => $delOperationids));
     $hosts = API::Host()->get(array('output' => array('hostid', 'name'), 'hostids' => $hostIds, 'nopermissions' => true));
     // delete host inventory
     DB::delete('host_inventory', array('hostid' => $hostIds));
     // delete host applications
     DB::delete('applications', array('hostid' => $hostIds));
     // delete host
     DB::delete('hosts', array('hostid' => $hostIds));
     // TODO: remove info from API
     foreach ($hosts as $host) {
         info(_s('Deleted: Host "%1$s".', $host['name']));
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $host['hostid'], $host['name'], 'hosts', NULL, NULL);
     }
     // remove Monitoring > Latest data toggle profile values related to given hosts
     CProfile::delete('web.latest.toggle_other', $hostIds);
     return array('hostids' => $hostIds);
 }
Example #3
0
 /**
  * Delete Host.
  *
  * @param array	$hostIds
  * @param bool	$nopermissions
  *
  * @return array
  */
 public function delete(array $hostIds, $nopermissions = false)
 {
     $this->validateDelete($hostIds, $nopermissions);
     // delete the discovery rules first
     $delRules = API::DiscoveryRule()->get(['output' => ['itemid'], 'hostids' => $hostIds, 'nopermissions' => true, 'preservekeys' => true]);
     if ($delRules) {
         API::DiscoveryRule()->delete(array_keys($delRules), true);
     }
     // delete the items
     $delItems = API::Item()->get(['templateids' => $hostIds, 'output' => ['itemid'], 'nopermissions' => true, 'preservekeys' => true]);
     if ($delItems) {
         API::Item()->delete(array_keys($delItems), true);
     }
     // delete web tests
     $delHttptests = [];
     $dbHttptests = get_httptests_by_hostid($hostIds);
     while ($dbHttptest = DBfetch($dbHttptests)) {
         $delHttptests[$dbHttptest['httptestid']] = $dbHttptest['httptestid'];
     }
     if (!empty($delHttptests)) {
         API::HttpTest()->delete($delHttptests, true);
     }
     // delete screen items
     DB::delete('screens_items', ['resourceid' => $hostIds, 'resourcetype' => SCREEN_RESOURCE_HOST_TRIGGERS]);
     // delete host from maps
     if (!empty($hostIds)) {
         DB::delete('sysmaps_elements', ['elementtype' => SYSMAP_ELEMENT_TYPE_HOST, 'elementid' => $hostIds]);
     }
     // disable actions
     // actions from conditions
     $actionids = [];
     $sql = 'SELECT DISTINCT actionid' . ' FROM conditions' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST . ' AND ' . dbConditionString('value', $hostIds);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $sql = 'SELECT DISTINCT o.actionid' . ' FROM operations o, opcommand_hst oh' . ' WHERE o.operationid=oh.operationid' . ' AND ' . dbConditionInt('oh.hostid', $hostIds);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         $update = [];
         $update[] = ['values' => ['status' => ACTION_STATUS_DISABLED], 'where' => ['actionid' => $actionids]];
         DB::update('actions', $update);
     }
     // delete action conditions
     DB::delete('conditions', ['conditiontype' => CONDITION_TYPE_HOST, 'value' => $hostIds]);
     // delete action operation commands
     $operationids = [];
     $sql = 'SELECT DISTINCT oh.operationid' . ' FROM opcommand_hst oh' . ' WHERE ' . dbConditionInt('oh.hostid', $hostIds);
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('opcommand_hst', ['hostid' => $hostIds]);
     // delete empty operations
     $delOperationids = [];
     $sql = 'SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS(SELECT oh.opcommand_hstid FROM opcommand_hst oh WHERE oh.operationid=o.operationid)';
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', ['operationid' => $delOperationids]);
     $hosts = API::Host()->get(['output' => ['hostid', 'name'], 'hostids' => $hostIds, 'nopermissions' => true]);
     // delete host inventory
     DB::delete('host_inventory', ['hostid' => $hostIds]);
     // delete host applications
     DB::delete('applications', ['hostid' => $hostIds]);
     // delete host
     DB::delete('hosts', ['hostid' => $hostIds]);
     // TODO: remove info from API
     foreach ($hosts as $host) {
         info(_s('Deleted: Host "%1$s".', $host['name']));
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $host['hostid'], $host['name'], 'hosts', NULL, NULL);
     }
     // remove Monitoring > Latest data toggle profile values related to given hosts
     DB::delete('profiles', ['idx' => 'web.latest.toggle_other', 'idx2' => $hostIds]);
     return ['hostids' => $hostIds];
 }
 /**
  * Delete Host
  *
  * @param array $hosts
  * @param array $hosts[0, ...]['hostid'] Host ID to delete
  *
  * @return array|boolean
  */
 public function delete($hosts)
 {
     if (empty($hosts)) {
         self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
     }
     $hosts = zbx_toArray($hosts);
     $hostids = zbx_objectValues($hosts, 'hostid');
     $this->checkInput($hosts, __FUNCTION__);
     // delete the discovery rules first
     $delRules = API::DiscoveryRule()->get(array('hostids' => $hostids, 'nopermissions' => true, 'preservekeys' => true));
     if ($delRules) {
         API::DiscoveryRule()->delete(array_keys($delRules), true);
     }
     // delete the items
     $delItems = API::Item()->get(array('templateids' => $hostids, 'output' => API_OUTPUT_SHORTEN, 'nopermissions' => true, 'preservekeys' => true));
     if ($delItems) {
         API::Item()->delete(array_keys($delItems), true);
     }
     // delete web tests
     $delHttptests = array();
     $dbHttptests = get_httptests_by_hostid($hostids);
     while ($dbHttptest = DBfetch($dbHttptests)) {
         $delHttptests[$dbHttptest['httptestid']] = $dbHttptest['httptestid'];
     }
     if (!empty($delHttptests)) {
         API::WebCheck()->delete($delHttptests);
     }
     // delete screen items
     DB::delete('screens_items', array('resourceid' => $hostids, 'resourcetype' => SCREEN_RESOURCE_HOST_TRIGGERS));
     // delete host from maps
     if (!empty($hostids)) {
         DB::delete('sysmaps_elements', array('elementtype' => SYSMAP_ELEMENT_TYPE_HOST, 'elementid' => $hostids));
     }
     // disable actions
     // actions from conditions
     $actionids = array();
     $sql = 'SELECT DISTINCT actionid' . ' FROM conditions' . ' WHERE conditiontype=' . CONDITION_TYPE_HOST . ' AND ' . dbConditionString('value', $hostids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     // actions from operations
     $sql = 'SELECT DISTINCT o.actionid' . ' FROM operations o, opcommand_hst oh' . ' WHERE o.operationid=oh.operationid' . ' AND ' . dbConditionInt('oh.hostid', $hostids);
     $dbActions = DBselect($sql);
     while ($dbAction = DBfetch($dbActions)) {
         $actionids[$dbAction['actionid']] = $dbAction['actionid'];
     }
     if (!empty($actionids)) {
         $update = array();
         $update[] = array('values' => array('status' => ACTION_STATUS_DISABLED), 'where' => array('actionid' => $actionids));
         DB::update('actions', $update);
     }
     // delete action conditions
     DB::delete('conditions', array('conditiontype' => CONDITION_TYPE_HOST, 'value' => $hostids));
     // delete action operation commands
     $operationids = array();
     $sql = 'SELECT DISTINCT oh.operationid' . ' FROM opcommand_hst oh' . ' WHERE ' . dbConditionInt('oh.hostid', $hostids);
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $operationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('opcommand_hst', array('hostid' => $hostids));
     // delete empty operations
     $delOperationids = array();
     $sql = 'SELECT DISTINCT o.operationid' . ' FROM operations o' . ' WHERE ' . dbConditionInt('o.operationid', $operationids) . ' AND NOT EXISTS(SELECT oh.opcommand_hstid FROM opcommand_hst oh WHERE oh.operationid=o.operationid)';
     $dbOperations = DBselect($sql);
     while ($dbOperation = DBfetch($dbOperations)) {
         $delOperationids[$dbOperation['operationid']] = $dbOperation['operationid'];
     }
     DB::delete('operations', array('operationid' => $delOperationids));
     $hosts = API::Host()->get(array('output' => array('hostid', 'name'), 'hostids' => $hostids, 'nopermissions' => true));
     // delete host inventory
     DB::delete('host_inventory', array('hostid' => $hostids));
     // delete host applications
     DB::delete('applications', array('hostid' => $hostids));
     // delete host
     DB::delete('hosts', array('hostid' => $hostids));
     // TODO: remove info from API
     foreach ($hosts as $host) {
         info(_s('Deleted: Host "%1$s".', $host['name']));
         add_audit_ext(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_HOST, $host['hostid'], $host['name'], 'hosts', NULL, NULL);
     }
     return array('hostids' => $hostids);
 }