summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/httptest.inc.php
blob: 57855b650cb7a72722d94549f85480db6a4aafe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?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 = 'unknown';	break;
		}
		return $status;
	}

	function db_save_step($hostid, $applicationid, $httptestid, $testname, $name, $no, $timeout, $url, $posts, $required, $status_codes, $delay, $history, $trends){
		if( $no <= 0 ){
			error('Scenario step number can\'t be less then 1');
			return false;
		}

		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, status_codes) '.
				' values ('.$httpstepid.','.$httptestid.','.zbx_dbstr($name).','.$no.','.
				zbx_dbstr($url).','.$timeout.','.
				zbx_dbstr($posts).','.zbx_dbstr($required).','.zbx_dbstr($status_codes).')'
				)) 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).', status_codes='.zbx_dbstr($status_codes).
				' 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_FLOAT,
				'units'		=> 'bps',
				'httpstepitemtype'=> HTTPSTEP_ITEM_TYPE_IN),
			array(
				'description'	=> 'Response time for step \'$2\' of scenario \'$1\'',
				'key_'		=> 'web.test.time['.$testname.','.$name.',resp]',
				'type'		=> ITEM_VALUE_TYPE_FLOAT,
				'units'		=> 's',
				'httpstepitemtype'=> HTTPSTEP_ITEM_TYPE_TIME),
			array(
				'description'	=> 'Response code for step \'$2\' of scenario \'$1\'',
				'key_'		=> 'web.test.rspcode['.$testname.','.$name.']',
				'type'		=> ITEM_VALUE_TYPE_UINT64,
				'units'		=> '',
				'httpstepitemtype'=> HTTPSTEP_ITEM_TYPE_RSPCODE),
			);
		
		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, httpstepitem hi '.
				' where hi.httpstepid='.$httpstepid.' and hi.itemid=i.itemid '.
				' and hi.type='.$item['httpstepitemtype']));

			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));
			}

			if(!$item_data)
			{
				if (!($itemid = add_item($item['description'], $item['key_'], $hostid, $delay,
					$history, ITEM_STATUS_ACTIVE, ITEM_TYPE_HTTPTEST, '', '', $item['type'], 'localhost',
					161, $item['units'], 0, 0, '', 0, '', '', '0', $trends, '', 0, '', '', array($applicationid))))
					return false;
			}
			else
			{
				$itemid = $item_data['itemid'];

				if (!(update_item($itemid, $item['description'], $item['key_'], $hostid, $delay, $item_data['history'],
					$item_data['status'], ITEM_TYPE_HTTPTEST, '', '', $item['type'], 'localhost', 161,
					$item['units'], 0, 0, $item_data['delta'], 0, '', '', '0', $item_data['trends'], '',
					$item_data['valuemapid'], '', '', array($applicationid))))
					return false;
			}

			
			$httpstepitemid = get_dbid('httpstepitem', 'httpstepitemid');

			DBexecute('delete from httpstepitem where itemid='.$itemid);

			if (!DBexecute('insert into httpstepitem'.
				' (httpstepitemid, httpstepid, itemid, type) '.
				' values ('.$httpstepitemid.','.$httpstepid.','.$itemid.','.$item['httpstepitemtype'].')'
				)) return false;

		}

		return $httpstepid;
	}

	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));
				}

				if(!$item_data)
				{
					if (!($itemid = add_item($item['description'], $item['key_'], $hostid, $delay,
						$history, ITEM_STATUS_ACTIVE, ITEM_TYPE_HTTPTEST, '', '', $item['type'], 'localhost',
						161, $item['units'], 0, 0, '', 0, '', '', '0', $trends, '', 0, '', '', array($applicationid))))
					{
						$result = false;
						break;
					}
				}
				else
				{
					$itemid = $item_data['itemid'];

					if (!(update_item($itemid, $item['description'], $item['key_'], $hostid, $delay, $item_data['history'],
						$item_data['status'], ITEM_TYPE_HTTPTEST, '', '', $item['type'], 'localhost', 161,
						$item['units'], 0, 0, $item_data['delta'], 0, '', '', '0', $item_data['trends'], '',
						$item_data['valuemapid'], '', '', array($applicationid))))
					{
						$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	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(!delete_item($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));
	}

	function	get_httpsteps_by_httptestid($httptestid)
	{
		return DBselect('select * from httpstep where httptestid='.$httptestid);
	}

	function	get_httpstep_by_httpstepid($httpstepid)
	{
		return DBfetch(DBselect('select * from httpstep where httpstepid='.$httpstepid));
	}

	function	get_httpstep_by_no($httptestid, $no)
	{
		return DBfetch(DBselect('select * from httpstep where httptestid='.$httptestid.' and no='.$no));
	}
	
	function get_httptests_by_hostid($hostid){
		$sql = 'SELECT DISTINCT ht.* '.
				' FROM httptest ht, applications ap '.
				' WHERE ap.hostid='.$hostid.
					' AND ht.applicationid=ap.applicationid';
		return DBselect($sql);
	}
?>