1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21require_once dirname(__FILE__).'/include/config.inc.php';
22require_once dirname(__FILE__).'/include/graphs.inc.php';
23
24$page['file'] = 'chart3.php';
25$page['type'] = PAGE_TYPE_IMAGE;
26
27require_once dirname(__FILE__).'/include/page_header.php';
28
29// VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
30$fields = [
31	'period' =>			[T_ZBX_INT, O_OPT, P_NZERO,	BETWEEN(ZBX_MIN_PERIOD, ZBX_MAX_PERIOD), null],
32	'stime' =>			[T_ZBX_INT, O_OPT, P_NZERO,	null,				null],
33	'profileIdx' =>		[T_ZBX_STR, O_OPT, null,		null,				null],
34	'profileIdx2' =>	[T_ZBX_STR, O_OPT, null,		null,				null],
35	'httptestid' =>		[T_ZBX_INT, O_OPT, P_NZERO,	null,				null],
36	'http_item_type' =>	[T_ZBX_INT, O_OPT, null,		null,				null],
37	'name' =>			[T_ZBX_STR, O_OPT, null,		null,				null],
38	'width' =>			[T_ZBX_INT, O_OPT, null,		BETWEEN(20, 65535),	null],
39	'height' =>			[T_ZBX_INT, O_OPT, null,		BETWEEN(0, 65535),	null],
40	'ymin_type' =>		[T_ZBX_INT, O_OPT, null,		IN('0,1,2'),		null],
41	'ymax_type' =>		[T_ZBX_INT, O_OPT, null,		IN('0,1,2'),		null],
42	'ymin_itemid' =>	[T_ZBX_INT, O_OPT, null,		DB_ID,				null],
43	'ymax_itemid' =>	[T_ZBX_INT, O_OPT, null,		DB_ID,				null],
44	'legend' =>			[T_ZBX_INT, O_OPT, null,		IN('0,1'),			null],
45	'showworkperiod' =>	[T_ZBX_INT, O_OPT, null,		IN('0,1'),			null],
46	'showtriggers' =>	[T_ZBX_INT, O_OPT, null,		IN('0,1'),			null],
47	'graphtype' =>		[T_ZBX_INT, O_OPT, null,		IN('0,1'),			null],
48	'yaxismin' =>		[T_ZBX_DBL, O_OPT, null,		null,				null],
49	'yaxismax' =>		[T_ZBX_DBL, O_OPT, null,		null,				null],
50	'percent_left' =>	[T_ZBX_DBL, O_OPT, null,		BETWEEN(0, 100),	null],
51	'percent_right' =>	[T_ZBX_DBL, O_OPT, null,		BETWEEN(0, 100),	null],
52	'items' =>			[T_ZBX_STR, O_OPT, null,		null,				null]
53];
54if (!check_fields($fields)) {
55	exit();
56}
57
58if ($httptestid = getRequest('httptestid', false)) {
59	if (!API::HttpTest()->isReadable([$_REQUEST['httptestid']])) {
60		access_deny();
61	}
62
63	$color = [
64		'current' => 0,
65		0 => ['next' => '1'],
66		1 => ['color' => 'Red', 'next' => '2'],
67		2 => ['color' => 'Dark Green', 'next' => '3'],
68		3 => ['color' => 'Blue', 'next' => '4'],
69		4 => ['color' => 'Dark Yellow', 'next' => '5'],
70		5 => ['color' => 'Cyan', 'next' => '6'],
71		6 => ['color' => 'Gray', 'next' => '7'],
72		7 => ['color' => 'Dark Red', 'next' => '8'],
73		8 => ['color' => 'Green', 'next' => '9'],
74		9 => ['color' => 'Dark Blue', 'next' => '10'],
75		10 => ['color' => 'Yellow', 'next' => '11'],
76		11 => ['color' => 'Black', 'next' => '1']
77	];
78
79	$items = [];
80
81	$dbItems = DBselect(
82		'SELECT i.itemid'.
83		' FROM httpstepitem hi,items i,httpstep hs'.
84		' WHERE i.itemid=hi.itemid'.
85			' AND hs.httptestid='.zbx_dbstr($httptestid).
86			' AND hs.httpstepid=hi.httpstepid'.
87			' AND hi.type='.zbx_dbstr(getRequest('http_item_type', HTTPSTEP_ITEM_TYPE_TIME)).
88		' ORDER BY hs.no DESC'
89	);
90	while ($item = DBfetch($dbItems)) {
91		$itemColor = $color[$color['current'] = $color[$color['current']]['next']]['color'];
92
93		$items[] = ['itemid' => $item['itemid'], 'color' => $itemColor];
94	}
95
96	$name = getRequest('name', '');
97}
98elseif ($items = getRequest('items', [])) {
99	CArrayHelper::sort($items, ['sortorder']);
100
101	$dbItems = API::Item()->get([
102		'itemids' => zbx_objectValues($items, 'itemid'),
103		'output' => ['itemid'],
104		'filter' => [
105			'flags' => [ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_PROTOTYPE, ZBX_FLAG_DISCOVERY_CREATED]
106		],
107		'webitems' => true,
108		'preservekeys' => true
109	]);
110
111	foreach ($items as $item) {
112		if (!isset($dbItems[$item['itemid']])) {
113			access_deny();
114		}
115	}
116	$name = getRequest('name', '');
117}
118else {
119	show_error_message(_('No items defined.'));
120	exit;
121}
122
123/*
124 * Display
125 */
126$profileIdx = getRequest('profileIdx', 'web.httptest');
127$profileIdx2 = getRequest('httptestid', getRequest('profileIdx2'));
128
129$timeline = CScreenBase::calculateTime([
130	'profileIdx' => $profileIdx,
131	'profileIdx2' => $profileIdx2,
132	'period' => getRequest('period'),
133	'stime' => getRequest('stime')
134]);
135
136CProfile::update($profileIdx.'.httptestid', $profileIdx2, PROFILE_TYPE_ID);
137
138$graph = new CLineGraphDraw(getRequest('graphtype', GRAPH_TYPE_NORMAL));
139$graph->setHeader($name);
140$graph->setPeriod($timeline['period']);
141$graph->setSTime($timeline['stime']);
142$graph->setWidth(getRequest('width', 900));
143$graph->setHeight(getRequest('height', 200));
144$graph->showLegend(getRequest('legend', 1));
145$graph->showWorkPeriod(getRequest('showworkperiod', 1));
146$graph->showTriggers(getRequest('showtriggers', 1));
147$graph->setYMinAxisType(getRequest('ymin_type', GRAPH_YAXIS_TYPE_CALCULATED));
148$graph->setYMaxAxisType(getRequest('ymax_type', GRAPH_YAXIS_TYPE_CALCULATED));
149$graph->setYAxisMin(getRequest('yaxismin', 0.00));
150$graph->setYAxisMax(getRequest('yaxismax', 100.00));
151$graph->setYMinItemId(getRequest('ymin_itemid', 0));
152$graph->setYMaxItemId(getRequest('ymax_itemid', 0));
153$graph->setLeftPercentage(getRequest('percent_left', 0));
154$graph->setRightPercentage(getRequest('percent_right', 0));
155
156foreach ($items as $item) {
157	$graph->addItem(
158		$item['itemid'],
159		isset($item['yaxisside']) ? $item['yaxisside'] : null,
160		isset($item['calc_fnc']) ? $item['calc_fnc'] : null,
161		isset($item['color']) ? $item['color'] : null,
162		isset($item['drawtype']) ? $item['drawtype'] : null
163	);
164}
165
166$graph->draw();
167
168require_once dirname(__FILE__).'/include/page_footer.php';
169