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';
23require_once dirname(__FILE__).'/include/triggers.inc.php';
24
25$page['title'] = _('100 busiest triggers');
26$page['file'] = 'toptriggers.php';
27$page['scripts'] = ['multiselect.js', 'class.calendar.js', 'gtlc.js'];
28
29require_once dirname(__FILE__).'/include/page_header.php';
30
31//	VAR					TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
32$fields = [
33	'groupids' =>	[T_ZBX_INT,			O_OPT,	P_SYS,	DB_ID,	null],
34	'hostids' =>	[T_ZBX_INT,			O_OPT,	P_SYS,	DB_ID,	null],
35	'severities' =>	[T_ZBX_INT,			O_OPT,	P_SYS,	null,	null],
36	'from' =>		[T_ZBX_RANGE_TIME,	O_OPT,	P_SYS,	null,	null],
37	'to' =>			[T_ZBX_RANGE_TIME,	O_OPT,	P_SYS,	null,	null],
38	'filter_rst' =>	[T_ZBX_STR,			O_OPT,	P_SYS,	null,	null],
39	'filter_set' =>	[T_ZBX_STR,			O_OPT,	P_SYS,	null,	null]
40];
41check_fields($fields);
42validateTimeSelectorPeriod(getRequest('from'), getRequest('to'));
43
44$data['config'] = select_config();
45
46/*
47 * Filter
48 */
49if (hasRequest('filter_set')) {
50	// prepare severity array
51	$severities = hasRequest('severities') ? array_keys(getRequest('severities')) : [];
52
53	CProfile::updateArray('web.toptriggers.filter.severities', $severities, PROFILE_TYPE_STR);
54	CProfile::updateArray('web.toptriggers.filter.groupids', getRequest('groupids', []), PROFILE_TYPE_STR);
55	CProfile::updateArray('web.toptriggers.filter.hostids', getRequest('hostids', []), PROFILE_TYPE_STR);
56}
57elseif (hasRequest('filter_rst')) {
58	DBstart();
59	CProfile::deleteIdx('web.toptriggers.filter.severities');
60	CProfile::deleteIdx('web.toptriggers.filter.groupids');
61	CProfile::deleteIdx('web.toptriggers.filter.hostids');
62	DBend();
63}
64
65$timeselector_options = [
66	'profileIdx' => 'web.toptriggers.filter',
67	'profileIdx2' => 0,
68	'from' => getRequest('from'),
69	'to' => getRequest('to')
70];
71updateTimeSelectorPeriod($timeselector_options);
72
73$data['filter'] = [
74	'severities' => CProfile::getArray('web.toptriggers.filter.severities', []),
75	'timeline' => getTimeSelectorPeriod($timeselector_options),
76	'active_tab' => CProfile::get('web.toptriggers.filter.active', 1)
77];
78
79// multiselect host groups
80$data['multiSelectHostGroupData'] = [];
81$groupids = CProfile::getArray('web.toptriggers.filter.groupids', []);
82
83if ($groupids) {
84	$groupids = getSubGroups($groupids, $data['multiSelectHostGroupData']);
85}
86
87// multiselect hosts
88$data['multiSelectHostData'] = [];
89$hostids = CProfile::getArray('web.toptriggers.filter.hostids', []);
90
91if ($hostids) {
92	$filterHosts = API::Host()->get([
93		'output' => ['hostid', 'name'],
94		'hostids' => $hostids
95	]);
96
97	foreach ($filterHosts as $filterHost) {
98		$data['multiSelectHostData'][] = [
99			'id' => $filterHost['hostid'],
100			'name' => $filterHost['name']
101		];
102	}
103}
104
105// data generation
106$triggersEventCount = [];
107
108// get 100 triggerids with max event count
109$sql = 'SELECT e.objectid,count(distinct e.eventid) AS cnt_event'.
110		' FROM triggers t,events e'.
111		' WHERE t.triggerid=e.objectid'.
112			' AND e.source='.EVENT_SOURCE_TRIGGERS.
113			' AND e.object='.EVENT_OBJECT_TRIGGER.
114			' AND e.clock>='.zbx_dbstr($data['filter']['timeline']['from_ts']).
115			' AND e.clock<='.zbx_dbstr($data['filter']['timeline']['to_ts']);
116
117if ($data['filter']['severities']) {
118	$sql .= ' AND '.dbConditionInt('t.priority', $data['filter']['severities']);
119}
120
121if ($hostids) {
122	$inHosts = ' AND '.dbConditionInt('i.hostid', $hostids);
123}
124
125if ($groupids) {
126	$inGroups = ' AND '.dbConditionInt('hgg.groupid', $groupids);
127}
128
129if (CWebUser::getType() == USER_TYPE_SUPER_ADMIN && ($groupids || $hostids)) {
130	$sql .= ' AND EXISTS ('.
131				'SELECT NULL'.
132				' FROM functions f,items i,hosts_groups hgg'.
133				' WHERE t.triggerid=f.triggerid'.
134					' AND f.itemid=i.itemid'.
135					' AND i.hostid=hgg.hostid'.
136					($hostids ? $inHosts : '').
137					($groupids ? $inGroups : '').
138			')';
139}
140elseif (CWebUser::getType() != USER_TYPE_SUPER_ADMIN) {
141	// add permission filter
142	$userId = CWebUser::$data['userid'];
143	$userGroups = getUserGroupsByUserId($userId);
144	$sql .= ' AND EXISTS ('.
145				'SELECT NULL'.
146				' FROM functions f,items i,hosts_groups hgg'.
147				' JOIN rights r'.
148					' ON r.id=hgg.groupid'.
149						' AND '.dbConditionInt('r.groupid', $userGroups).
150				' WHERE t.triggerid=f.triggerid'.
151					' AND f.itemid=i.itemid'.
152					' AND i.hostid=hgg.hostid'.
153					($hostids ? $inHosts : '').
154					($groupids ? $inGroups : '').
155				' GROUP BY f.triggerid'.
156				' HAVING MIN(r.permission)>'.PERM_DENY.
157			')';
158}
159$sql .= ' AND '.dbConditionInt('t.flags', [ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED]).
160		' GROUP BY e.objectid'.
161		' ORDER BY cnt_event DESC';
162$result = DBselect($sql, 100);
163while ($row = DBfetch($result)) {
164	$triggersEventCount[$row['objectid']] = $row['cnt_event'];
165}
166
167$data['triggers'] = API::Trigger()->get([
168	'output' => ['triggerid', 'description', 'expression', 'priority', 'lastchange'],
169	'selectHosts' => ['hostid', 'status', 'name'],
170	'triggerids' => array_keys($triggersEventCount),
171	'expandDescription' => true,
172	'preservekeys' => true
173]);
174
175$trigger_hostids = [];
176
177foreach ($data['triggers'] as $triggerId => $trigger) {
178	$hostId = $trigger['hosts'][0]['hostid'];
179	$trigger_hostids[$hostId] = $hostId;
180
181	$data['triggers'][$triggerId]['cnt_event'] = $triggersEventCount[$triggerId];
182}
183
184CArrayHelper::sort($data['triggers'], [
185	['field' => 'cnt_event', 'order' => ZBX_SORT_DOWN],
186	'host', 'description', 'priority'
187]);
188
189$data['hosts'] = API::Host()->get([
190	'output' => ['hostid', 'status'],
191	'hostids' => $trigger_hostids,
192	'preservekeys' => true
193]);
194
195// render view
196$historyView = new CView('reports.toptriggers', $data);
197$historyView->render();
198$historyView->show();
199
200require_once dirname(__FILE__).'/include/page_footer.php';
201