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	'period' =>			[T_ZBX_INT, O_OPT, P_NZERO, BETWEEN(ZBX_MIN_PERIOD, ZBX_MAX_PERIOD), null],
34	'stime' =>			[T_ZBX_STR, 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	'updateProfile' =>	[T_ZBX_STR, O_OPT, null,	null,		null],
38	'from' =>			[T_ZBX_INT, O_OPT, null,	'{} >= 0',	null],
39	'width' =>			[T_ZBX_INT, O_OPT, null,	BETWEEN(20, 65535),	null],
40	'height' =>			[T_ZBX_INT, O_OPT, null,	'{} > 0',	null],
41	'batch' =>			[T_ZBX_INT, O_OPT, null,	IN('0,1'),	null],
42];
43if (!check_fields($fields)) {
44	exit();
45}
46
47$itemIds = getRequest('itemids');
48
49/*
50 * Permissions
51 */
52$items = API::Item()->get([
53	'output' => ['itemid', 'name'],
54	'selectHosts' => ['name'],
55	'itemids' => $itemIds,
56	'webitems' => true,
57	'preservekeys' => true
58]);
59foreach ($itemIds as $itemId) {
60	if (!isset($items[$itemId])) {
61		access_deny();
62	}
63}
64
65$hostNames = [];
66foreach ($items as &$item) {
67	$item['hostname'] = $item['hosts'][0]['name'];
68	if (!in_array($item['hostname'], $hostNames)) {
69		$hostNames[] = $item['hostname'];
70	}
71}
72unset($item);
73// sort items
74CArrayHelper::sort($items, ['name', 'hostname', 'itemid']);
75
76/*
77 * Display
78 */
79$timeline = CScreenBase::calculateTime([
80	'profileIdx' => getRequest('profileIdx', 'web.screens'),
81	'profileIdx2' => getRequest('profileIdx2'),
82	'updateProfile' => getRequest('updateProfile', true),
83	'period' => getRequest('period'),
84	'stime' => getRequest('stime')
85]);
86
87$graph = new CLineGraphDraw(getRequest('type'));
88$graph->setPeriod($timeline['period']);
89$graph->setSTime($timeline['stime']);
90
91// change how the graph will be displayed if more than one item is selected
92if (getRequest('batch')) {
93	// set a default header
94	if (count($hostNames) == 1) {
95		$graph->setHeader($hostNames[0].NAME_DELIMITER._('Item values'));
96	}
97	else {
98		$graph->setHeader(_('Item values'));
99	}
100
101	// hide triggers
102	$graph->showTriggers(false);
103}
104
105if (isset($_REQUEST['from'])) {
106	$graph->setFrom($_REQUEST['from']);
107}
108if (isset($_REQUEST['width'])) {
109	$graph->setWidth($_REQUEST['width']);
110}
111if (isset($_REQUEST['height'])) {
112	$graph->setHeight($_REQUEST['height']);
113}
114
115foreach ($items as $item) {
116	$graph->addItem($item['itemid'], GRAPH_YAXIS_SIDE_DEFAULT, (getRequest('batch')) ? CALC_FNC_AVG : CALC_FNC_ALL,
117		rgb2hex(get_next_color(1))
118	);
119}
120
121$graph->draw();
122
123require_once dirname(__FILE__).'/include/page_footer.php';
124