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 db_save_httptest($httptestid, $hostid, $application, $name, $authentication, $http_user, $http_password, $delay, $status, $agent, $macros, $steps) { $history = 30; // TODO !!! Allow user to set this parameter $trends = 90; // TODO !!! Allow user to set this parameter if (!preg_match('/^([' . ZBX_PREG_PRINT . '])+$/u', $name)) { error(S_ONLY_CHARACTERS_ARE_ALLOWED); return false; } DBstart(); try { $sql = 'SELECT t.httptestid' . ' FROM httptest t, applications a' . ' WHERE t.applicationid=a.applicationid' . ' AND a.hostid=' . $hostid . ' AND t.name=' . zbx_dbstr($name); $t = DBfetch(DBselect($sql)); if (isset($httptestid) && $t && $t['httptestid'] != $httptestid || $t && !isset($httptestid)) { throw new Exception(S_SCENARIO_WITH_NAME . ' [ ' . $name . ' ] ' . S_ALREADY_EXISTS_SMALL); } $sql = 'SELECT applicationid FROM applications WHERE name=' . zbx_dbstr($application) . ' AND hostid=' . $hostid; if ($applicationid = DBfetch(DBselect($sql))) { $applicationid = $applicationid['applicationid']; } else { $result = CApplication::create(array('name' => $application, 'hostid' => $hostid)); if (!$result) { throw new Exception(S_CANNOT_ADD_NEW_APPLICATION . ' [ ' . $application . ' ]'); } else { $applicationid = reset($result['applicationids']); } } if (isset($httptestid)) { $sql = 'UPDATE httptest SET ' . ' applicationid=' . $applicationid . ', ' . ' name=' . zbx_dbstr($name) . ', ' . ' authentication=' . $authentication . ', ' . ' http_user='******', ' . ' http_password='******', ' . ' delay=' . $delay . ', ' . ' status=' . $status . ', ' . ' agent=' . zbx_dbstr($agent) . ', ' . ' macros=' . zbx_dbstr($macros) . ', ' . ' error=' . zbx_dbstr('') . ', ' . ' curstate=' . HTTPTEST_STATE_UNKNOWN . ' WHERE httptestid=' . $httptestid; if (!DBexecute($sql)) { throw new Exception('DBerror'); } } else { $httptestid = get_dbid('httptest', 'httptestid'); $values = array('httptestid' => $httptestid, 'applicationid' => $applicationid, 'name' => zbx_dbstr($name), 'authentication' => $authentication, 'http_user' => zbx_dbstr($http_user), 'http_password' => zbx_dbstr($http_password), 'delay' => $delay, 'status' => $status, 'agent' => zbx_dbstr($agent), 'macros' => zbx_dbstr($macros), 'curstate' => HTTPTEST_STATE_UNKNOWN); $sql = 'INSERT INTO httptest (' . implode(', ', array_keys($values)) . ') VALUES (' . implode(', ', $values) . ')'; if (!DBexecute($sql)) { throw new Exception('DBerror'); } } $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) { throw new Exception('Cannot create web step'); } $httpstepids[$result] = $result; } /* clean unneeded steps */ $sql = 'SELECT httpstepid FROM httpstep WHERE httptestid=' . $httptestid; $db_steps = DBselect($sql); while ($step_data = DBfetch($db_steps)) { if (!isset($httpstepids[$step_data['httpstepid']])) { delete_httpstep($step_data['httpstepid']); } } $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' => '', 'authtype' => 0, 'username' => '', 'password' => '', 'publickey' => '', 'privatekey' => '', '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))) { throw new Exception('Cannot add item'); } } 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)) { throw new Exception('Cannot update item'); } } $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'] . ')')) { throw new Exception('DBerror'); } } return DBend(true); } catch (Exception $e) { error($e->getMessage()); return DBend(false); } }