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	'from' =>			[T_ZBX_RANGE_TIME,	O_OPT, P_SYS,		null,				null],
32	'to' =>				[T_ZBX_RANGE_TIME,	O_OPT, P_SYS,		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(CLineGraphDraw::GRAPH_WIDTH_MIN, 65535),	null],
39	'height' =>			[T_ZBX_INT,			O_OPT, null,	BETWEEN(CLineGraphDraw::GRAPH_HEIGHT_MIN, 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	'outer' =>			[T_ZBX_INT,			O_OPT, null,		IN('0,1'),			null],
53	'items' =>			[T_ZBX_STR,			O_OPT, null,		null,				null],
54	'i' =>				[T_ZBX_STR,			O_OPT, null,		null,				null],
55	'onlyHeight' =>		[T_ZBX_INT,			O_OPT, null,		IN('0,1'),			null],
56	'widget_view' =>	[T_ZBX_INT,			O_OPT, null,		IN('0,1'),			null]
57];
58if (!check_fields($fields)) {
59	exit();
60}
61validateTimeSelectorPeriod(getRequest('from'), getRequest('to'));
62
63$graph_items = [];
64
65if ($httptestid = getRequest('httptestid', false)) {
66	$httptests = API::HttpTest()->get([
67		'output' => [],
68		'httptestids' => $httptestid,
69		'selectHosts' => ['hostid', 'name', 'host']
70	]);
71
72	if (!$httptests) {
73		access_deny();
74	}
75
76	$colors = ['Red', 'Dark Green', 'Blue', 'Dark Yellow', 'Cyan', 'Gray', 'Dark Red', 'Green', 'Dark Blue', 'Yellow',
77		'Black'
78	];
79	$color = false;
80	$items = [];
81	$hosts = zbx_toHash($httptests[0]['hosts'], 'hostid');
82
83	$dbItems = DBselect(
84		'SELECT i.itemid,i.type,i.name,i.delay,i.units,i.hostid,i.history,i.trends,i.value_type,i.key_'.
85		' FROM httpstepitem hi,items i,httpstep hs'.
86		' WHERE i.itemid=hi.itemid'.
87			' AND hs.httptestid='.zbx_dbstr($httptestid).
88			' AND hs.httpstepid=hi.httpstepid'.
89			' AND hi.type='.zbx_dbstr(getRequest('http_item_type', HTTPSTEP_ITEM_TYPE_TIME)).
90		' ORDER BY hs.no DESC'
91	);
92	while ($item = DBfetch($dbItems)) {
93		$graph_items[] = $item + [
94			'color' => ($color === false) ? reset($colors) : $color,
95			'host' => $hosts[$item['hostid']]['host'],
96			'hostname' => $hosts[$item['hostid']]['name']
97		];
98		$color = next($colors);
99	}
100
101	$name = getRequest('name', '');
102}
103elseif (hasRequest('i') || hasRequest('items')) {
104	if (hasRequest('i')) {
105		$items = array_map('expandShortGraphItem', getRequest('i', []));
106	}
107	else {
108		$items = getRequest('items', []);
109	}
110
111	CArrayHelper::sort($items, ['sortorder']);
112
113	$dbItems = API::Item()->get([
114		'itemids' => zbx_objectValues($items, 'itemid'),
115		'output' => ['itemid', 'type', 'master_itemid', 'name', 'delay', 'units', 'hostid', 'history', 'trends',
116			'value_type', 'key_'
117		],
118		'selectHosts' => ['hostid', 'name', 'host'],
119		'filter' => [
120			'flags' => [ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_PROTOTYPE, ZBX_FLAG_DISCOVERY_CREATED]
121		],
122		'webitems' => true,
123		'preservekeys' => true
124	]);
125
126	foreach ($items as $item) {
127		if (!array_key_exists($item['itemid'], $dbItems)) {
128			access_deny();
129		}
130		$host = reset($dbItems[$item['itemid']]['hosts']);
131		$graph_items[] = $dbItems[$item['itemid']] + $item + [
132			'host' => $host['host'],
133			'hostname' => $host['name']
134		];
135	}
136
137	foreach ($graph_items as &$graph_item) {
138		unset($graph_item['hosts']);
139	}
140	unset($graph_item);
141
142	$name = getRequest('name', '');
143}
144else {
145	show_error_message(_('No items defined.'));
146	exit;
147}
148
149/*
150 * Display
151 */
152$timeline = getTimeSelectorPeriod([
153	'profileIdx' => getRequest('profileIdx', 'web.httpdetails.filter'),
154	'profileIdx2' => getRequest('httptestid', getRequest('profileIdx2')),
155	'from' => getRequest('from'),
156	'to' => getRequest('to')
157]);
158
159CProfile::update($timeline['profileIdx'].'.httptestid', $timeline['profileIdx2'], PROFILE_TYPE_ID);
160
161$graph = new CLineGraphDraw(getRequest('graphtype', GRAPH_TYPE_NORMAL));
162$graph->setHeader($name);
163$graph->setPeriod($timeline['to_ts'] - $timeline['from_ts']);
164$graph->setSTime($timeline['from_ts']);
165$graph->setWidth(getRequest('width', 900));
166$graph->setHeight(getRequest('height', 200));
167$graph->showLegend(getRequest('legend', 1));
168$graph->showWorkPeriod(getRequest('showworkperiod', 1));
169$graph->showTriggers(getRequest('showtriggers', 1));
170$graph->setYMinAxisType(getRequest('ymin_type', GRAPH_YAXIS_TYPE_CALCULATED));
171$graph->setYMaxAxisType(getRequest('ymax_type', GRAPH_YAXIS_TYPE_CALCULATED));
172$graph->setYAxisMin(getRequest('yaxismin', 0.00));
173$graph->setYAxisMax(getRequest('yaxismax', 100.00));
174$graph->setYMinItemId(getRequest('ymin_itemid', 0));
175$graph->setYMaxItemId(getRequest('ymax_itemid', 0));
176$graph->setLeftPercentage(getRequest('percent_left', 0));
177$graph->setRightPercentage(getRequest('percent_right', 0));
178$graph->setOuter(getRequest('outer', 0));
179
180if (getRequest('widget_view') === '1') {
181	$graph->draw_header = false;
182	$graph->with_vertical_padding = false;
183}
184
185foreach ($graph_items as $graph_item) {
186	$graph->addItem($graph_item);
187}
188
189if (getRequest('onlyHeight', '0') === '1') {
190	$graph->drawDimensions();
191	header('X-ZBX-SBOX-HEIGHT: '.($graph->getHeight() + 1));
192}
193else {
194	$graph->draw();
195}
196
197require_once dirname(__FILE__).'/include/page_footer.php';
198