Exemplo n.º 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;
}
Exemplo n.º 2
0
function copy_template_items($hostid, $templateid = null, $copy_mode = false)
{
    if ($templateid == null) {
        $templateid = array_keys(get_templates_by_hostid($hostid));
    }
    if (is_array($templateid)) {
        foreach ($templateid as $id) {
            copy_template_items($hostid, $id, $copy_mode);
        }
        // attention recursion
        return;
    }
    $db_tmp_items = get_items_by_hostid($templateid);
    while ($db_tmp_item = DBfetch($db_tmp_items)) {
        $db_tmp_item['hostid'] = $hostid;
        $db_tmp_item['applications'] = get_same_applications_for_host(get_applications_by_itemid($db_tmp_item['itemid']), $hostid);
        $db_tmp_item['templateid'] = $copy_mode ? 0 : $db_tmp_item['itemid'];
        add_item($db_tmp_item);
    }
}