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
21
22require_once dirname(__FILE__).'/include/config.inc.php';
23
24$page['file'] = 'chart.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	'type' =>           [T_ZBX_INT,			O_OPT, null,	IN([GRAPH_TYPE_NORMAL, GRAPH_TYPE_STACKED]), null],
32	'itemids' =>		[T_ZBX_INT,			O_MAND, P_SYS,	DB_ID,		null],
33	'from' =>			[T_ZBX_RANGE_TIME,	O_OPT, P_SYS,	null,		null],
34	'to' =>				[T_ZBX_RANGE_TIME,	O_OPT, P_SYS,	null,		null],
35	'profileIdx' =>		[T_ZBX_STR,			O_OPT, null,	null,		null],
36	'profileIdx2' =>	[T_ZBX_STR,			O_OPT, null,	null,		null],
37	'width' =>			[T_ZBX_INT,			O_OPT, null,	BETWEEN(CLineGraphDraw::GRAPH_WIDTH_MIN, 65535),	null],
38	'height' =>			[T_ZBX_INT,			O_OPT, null,	BETWEEN(CLineGraphDraw::GRAPH_HEIGHT_MIN, 65535),	null],
39	'outer' =>			[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null],
40	'batch' =>			[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null],
41	'onlyHeight' =>		[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null],
42	'legend' =>			[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null],
43	'widget_view' =>	[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null]
44];
45if (!check_fields($fields)) {
46	exit();
47}
48validateTimeSelectorPeriod(getRequest('from'), getRequest('to'));
49
50$itemIds = getRequest('itemids');
51
52/*
53 * Permissions
54 */
55$items = API::Item()->get([
56	'output' => ['itemid', 'type', 'master_itemid', 'name', 'delay', 'units', 'hostid', 'history', 'trends',
57		'value_type', 'key_'
58	],
59	'selectHosts' => ['name', 'host'],
60	'itemids' => $itemIds,
61	'webitems' => true,
62	'preservekeys' => true
63]);
64foreach ($itemIds as $itemId) {
65	if (!isset($items[$itemId])) {
66		access_deny();
67	}
68}
69
70$hostNames = [];
71foreach ($items as &$item) {
72	$item['hostname'] = $item['hosts'][0]['name'];
73	$item['host'] = $item['hosts'][0]['host'];
74	if (!in_array($item['hostname'], $hostNames)) {
75		$hostNames[] = $item['hostname'];
76	}
77}
78unset($item);
79// sort items
80CArrayHelper::sort($items, ['name', 'hostname', 'itemid']);
81
82/*
83 * Display
84 */
85$timeline = getTimeSelectorPeriod([
86	'profileIdx' => getRequest('profileIdx'),
87	'profileIdx2' => getRequest('profileIdx2'),
88	'from' => getRequest('from'),
89	'to' => getRequest('to')
90]);
91
92$graph = new CLineGraphDraw(getRequest('type'));
93$graph->setPeriod($timeline['to_ts'] - $timeline['from_ts']);
94$graph->setSTime($timeline['from_ts']);
95$graph->showLegend(getRequest('legend', 1));
96
97// change how the graph will be displayed if more than one item is selected
98if (getRequest('batch')) {
99	// set a default header
100	if (count($hostNames) == 1) {
101		$graph->setHeader($hostNames[0].NAME_DELIMITER._('Item values'));
102	}
103	else {
104		$graph->setHeader(_('Item values'));
105	}
106
107	// hide triggers
108	$graph->showTriggers(false);
109}
110
111if (hasRequest('width')) {
112	$graph->setWidth(getRequest('width'));
113}
114if (hasRequest('height')) {
115	$graph->setHeight(getRequest('height'));
116}
117if (hasRequest('outer')) {
118	$graph->setOuter(getRequest('outer'));
119}
120
121if (getRequest('widget_view') === '1') {
122	$graph->draw_header = false;
123	$graph->with_vertical_padding = false;
124}
125
126foreach ($items as $item) {
127	$graph->addItem($item + [
128		'color' => rgb2hex(get_next_color(1)),
129		'yaxisside' => GRAPH_YAXIS_SIDE_DEFAULT,
130		'calc_fnc' => (getRequest('batch')) ? CALC_FNC_AVG : CALC_FNC_ALL
131	]);
132}
133
134$min_dimensions = $graph->getMinDimensions();
135if ($min_dimensions['width'] > $graph->getWidth()) {
136	$graph->setWidth($min_dimensions['width']);
137}
138if ($min_dimensions['height'] > $graph->getHeight()) {
139	$graph->setHeight($min_dimensions['height']);
140}
141
142if (getRequest('onlyHeight', '0') === '1') {
143	$graph->drawDimensions();
144	header('X-ZBX-SBOX-HEIGHT: '.($graph->getHeight() + 1));
145}
146else {
147	$graph->draw();
148}
149
150require_once dirname(__FILE__).'/include/page_footer.php';
151