summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/httptest.inc.php
diff options
context:
space:
mode:
authorosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2007-01-19 15:34:39 +0000
committerosmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2007-01-19 15:34:39 +0000
commit938eeba4af4a46126fc4d2fcbe46c15c98d70090 (patch)
treedaf653ad9c11f00b291719b19c19adcb72ca20f1 /frontends/php/include/httptest.inc.php
parenta901997b009039670ecc3144465ecea54d16af4a (diff)
- developed configuration interface of http monitoring (Eugene)
git-svn-id: svn://svn.zabbix.com/trunk@3732 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'frontends/php/include/httptest.inc.php')
-rw-r--r--frontends/php/include/httptest.inc.php300
1 files changed, 300 insertions, 0 deletions
diff --git a/frontends/php/include/httptest.inc.php b/frontends/php/include/httptest.inc.php
new file mode 100644
index 00000000..aeb4dd30
--- /dev/null
+++ b/frontends/php/include/httptest.inc.php
@@ -0,0 +1,300 @@
+<?php
+/*
+** ZABBIX
+** Copyright (C) 2000-2005 SIA Zabbix
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation; either version 2 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+**/
+ require_once('include/defines.inc.php');
+ require_once('include/items.inc.php');
+?>
+<?php
+ function httptest_status2str($status)
+ {
+ switch($status)
+ {
+ case HTTPTEST_STATUS_ACTIVE: $status = S_ACTIVE; break;
+ case HTTPTEST_STATUS_DISABLED: $status = S_DISABLED; break;
+ default:
+ $status = S_UNKNOWN; break;
+ }
+ return $status;
+ }
+
+ function httptest_status2style($status)
+ {
+ switch($status)
+ {
+ case HTTPTEST_STATUS_ACTIVE: $status = 'off'; break;
+ case HTTPTEST_STATUS_DISABLED: $status = 'on'; break;
+ default:
+ $status = 'uncnown'; break;
+ }
+ return $status;
+ }
+
+ function db_save_step($hostid, $applicationid, $httptestid, $testname, $name, $no, $timeout, $url, $posts, $required)
+ {
+ $hystory = 30; // TODO !!! Allow user set this parametr
+ $trends = 90; // TODO !!! Allow user set this parametr
+
+ if (!eregi('^([0-9a-zA-Z\_\.-\$ ]+)$', $name))
+ {
+ error("Scenario step name should contain '0-9a-zA-Z_ .$'- characters only");
+ return false;
+ }
+
+ if(!($httpstep_data = DBfetch(DBselect('select httpstepid from httpstep '.
+ ' where httptestid='.$httptestid.' and name='.zbx_dbstr($name)))))
+ {
+ $httpstepid = get_dbid("httpstep","httpstepid");
+
+ if (!DBexecute('insert into httpstep'.
+ ' (httpstepid, httptestid, name, no, url, timeout, posts, required) '.
+ ' values ('.$httpstepid.','.$httptestid.','.zbx_dbstr($name).','.$no.','.
+ zbx_dbstr($url).','.$timeout.','.
+ zbx_dbstr($posts).','.zbx_dbstr($required).')'
+ )) return false;
+ }
+ else
+ {
+ $httpstepid = $httpstep_data['httpstepid'];
+
+ if (!DBexecute('update httpstep set '.
+ ' name='.zbx_dbstr($name).', no='.$no.', url='.zbx_dbstr($url).', timeout='.$timeout.','.
+ ' posts='.zbx_dbstr($posts).', required='.zbx_dbstr($required).
+ ' where httpstepid='.$httpstepid)) return false;
+ }
+
+ $monitored_items = array(
+ array(
+ 'description' => 'Download speed for step \'$2\' of scenario \'$1\'',
+ 'key_' => 'web.test.in['.$testname.','.$name.',bps]',
+ 'type' => ITEM_VALUE_TYPE_UINT64,
+ 'units' => 'bps'),
+ array(
+ 'description' => 'Response time for step \'$2\' of scenario \'$1\'',
+ 'key_' => 'web.test.time['.$testname.','.$name.',resp]',
+ 'type' => ITEM_VALUE_TYPE_UINT64,
+ 'units' => 's'),
+ array(
+ 'description' => 'Response code for step \'$2\' of scenario \'$1\'',
+ 'key_' => 'web.test.rspcode['.$testname.','.$name.']',
+ 'type' => ITEM_VALUE_TYPE_UINT64,
+ 'units' => ''),
+ );
+
+ foreach($monitored_items as $item)
+ {
+ if(!($item_data = DBfetch(DBselect('select itemid from items '.
+ ' where hostid='.$hostid.' and key_='.zbx_dbstr($item['key_'])))))
+ {
+ if (!($itemid = add_item($item['description'], $item['key_'], $hostid, 30, $hystory, ITEM_STATUS_ACTIVE,
+ ITEM_TYPE_HTTPTEST, '', '', $item['type'], 'localhost', 161, $item['units'], 0, 0, '', 0, '', '',
+ '', $trends, '', 0, '', array($applicationid))))
+ return false;
+ }
+ else
+ {
+ $itemid = $item_data['itemid'];
+
+ if (!(update_item($itemid, $item['description'], $item['key_'], $hostid, 30, $hystory, ITEM_STATUS_ACTIVE,
+ ITEM_TYPE_HTTPTEST, '', '', $item['type'], 'localhost', 161, $item['units'], 0, 0, '', 0, '', '',
+ '', $trends, '', 0, '', array($applicationid))))
+ return false;
+ }
+
+
+ $httpstepitemid = get_dbid('httpstepitem', 'httpstepitemid');
+
+ DBexecute('delete from httpstepitem where itemid='.$itemid);
+
+ if (!DBexecute('insert into httpstepitem'.
+ ' (httpstepitemid, httpstepid, itemid) '.
+ ' values ('.$httpstepitemid.','.$httpstepid.','.$itemid.')'
+ )) return false;
+
+ }
+
+ return $httpstepid;
+ }
+
+ function db_save_httptest($httptestid, $hostid, $application, $name, $delay, $status, $agent, $macros, $steps)
+ {
+ 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).
+ ' 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) '.
+ ' values ('.$httptestid.','.$applicationid.','.zbx_dbstr($name).','.
+ $delay.','.$status.','.zbx_dbstr($agent).','.zbx_dbstr($macros).')'
+ );
+
+ $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'] = '';
+
+ $result = db_save_step($hostid, $applicationid, $httptestid,
+ $name, $s['name'], $sid, $s['timeout'], $s['url'], $s['posts'], $s['required']);
+
+ 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_step($step_data['httpstepid']);
+ DBexecute('delete httpstep where httpstepid='.$step_data['httpstepid']);
+ }
+ }
+ }
+
+ // TODO !!! Create items for httptest
+
+ if(!$result && isset($test_added)) delete_httptest($httptestid);
+ else $restult = $httptestid;
+
+ DBend($result);
+
+ return $result;
+ }
+
+ function add_httptest($hostid, $application, $name, $delay, $status, $agent, $macros, $steps)
+ {
+ $result = db_save_httptest(null, $hostid, $application, $name, $delay, $status, $agent, $macros, $steps);
+
+ if($result) info("Sceanrio '".$name."' added");
+
+ return $result;
+ }
+
+ function update_httptest($httptestid, $hostid, $application, $name, $delay, $status, $agent, $macros, $steps)
+ {
+ $result = db_save_httptest($httptestid, $hostid, $application, $name, $delay, $status, $agent, $macros, $steps);
+
+ if($result) info("Sceanrio '".$name."' updated");
+
+ return $result;
+ }
+
+ function delete_httpstep($httpstepid)
+ {
+ $db_httpstepitems = DBselect('select distinct * from httpstepitem where httpstepid='.$httpstepid);
+ while($httpstepitem_data = DBfetch($db_httpstepitems))
+ {
+ if(!DBexecute('delete from httpstepitem where httpstepitemid='.$httpstepitem_data['httpstepitemid'])) return false;
+ if(!DBexecute('delete from items where itemid='.$httpstepitem_data['itemid'])) return false;
+ }
+
+ return DBexecute('delete from httpstep where httpstepid='.$httpstepid);
+ }
+
+ function delete_httptest($httptestid)
+ {
+ if (!($httptest = DBfetch(DBselect('select * from httptest where httptestid='.$httptestid)))) return false;
+
+ $db_httpstep = DBselect('select distinct s.httpstepid from httpstep s '.
+ ' where s.httptestid='.$httptestid);
+ while($httpstep_data = DBfetch($db_httpstep))
+ {
+ delete_httpstep($httpstep_data['httpstepid']);
+ }
+
+ if(!DBexecute('delete from httptest where httptestid='.$httptestid)) return false;
+
+ info("Sceanrio '".$httptest["name"]."' deleted");
+
+ return true;
+ }
+
+ function activate_httptest($httptestid)
+ {
+ return DBexecute('update httptest set status='.HTTPTEST_STATUS_ACTIVE.' where httptestid='.$httptestid);
+ }
+
+ function disable_httptest($httptestid)
+ {
+ return DBexecute('update httptest set status='.HTTPTEST_STATUS_DISABLED.' where httptestid='.$httptestid);
+ }
+
+ function delete_history_by_httptestid($httptestid)
+ {
+ $db_items = DBselect('select distinct i.itemid from items i, httpstepitem si, httpstep s '.
+ ' where s.httptestid='.$httptestid.' and si.httpstepid=s.httpstepid and i.itemid=si.itemid');
+ while($item_data = DBfetch($db_items))
+ {
+ if(!delete_history_by_itemid($item_data['itemid'], 0 /* use housekeeper */)) return false;
+ }
+ return true;
+ }
+ function get_httptest_by_httptestid($httptestid)
+ {
+ return DBfetch(DBselect('select * from httptest where httptestid='.$httptestid));
+ }
+?>