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; }
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; }
DBexecute('update httptest set nextcheck=0' . ' where httptestid=' . $id); $host = get_host_by_applicationid($httptest_data['applicationid']); add_audit(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_SCENARIO, S_SCENARIO . ' [' . $httptest_data['name'] . '] [' . $id . '] ' . S_HOST . ' [' . $host['host'] . ']' . S_HISTORY_CLEANED); } } show_messages($result, S_HISTORY_CLEANED, $result); } else { if ($_REQUEST['go'] == 'delete' && isset($_REQUEST['group_httptestid'])) { $result = false; $group_httptestid = $_REQUEST['group_httptestid']; foreach ($group_httptestid as $id) { if (!($httptest_data = get_httptest_by_httptestid($id))) { continue; } /* if($httptest_data['templateid']<>0) continue; // for future use */ if (delete_httptest($id)) { $result = true; $host = get_host_by_applicationid($httptest_data['applicationid']); add_audit(AUDIT_ACTION_DELETE, AUDIT_RESOURCE_SCENARIO, S_SCENARIO . ' [' . $httptest_data['name'] . '] [' . $id . '] ' . S_HOST . ' [' . $host['host'] . ']'); } } show_messages($result, S_SCENARIO_DELETED, null); } } } } } } } } }