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
22$drules = API::DRule()->get([
23	'output' => ['druleid', 'name'],
24	'selectDHosts' => ['status'],
25	'filter' => ['status' => DHOST_STATUS_ACTIVE]
26]);
27CArrayHelper::sort($drules, ['name']);
28
29foreach ($drules as &$drule) {
30	$drule['up'] = 0;
31	$drule['down'] = 0;
32
33	foreach ($drule['dhosts'] as $dhost){
34		if (DRULE_STATUS_DISABLED == $dhost['status']) {
35			$drule['down']++;
36		}
37		else {
38			$drule['up']++;
39		}
40	}
41}
42unset($drule);
43
44$table = (new CTableInfo())
45	->setHeader([
46		_('Discovery rule'),
47		_x('Up', 'discovery results in dashboard'),
48		_x('Down', 'discovery results in dashboard')
49	]);
50
51foreach ($drules as $drule) {
52	$table->addRow([
53		new CLink($drule['name'], 'zabbix.php?action=discovery.view&druleid='.$drule['druleid']),
54		(new CSpan($drule['up']))->addClass(ZBX_STYLE_GREEN),
55		(new CSpan($drule['down']))->addClass(($drule['down'] != 0) ? ZBX_STYLE_RED : ZBX_STYLE_GREEN)
56	]);
57}
58
59$output = [
60	'header' => _('Discovery status'),
61	'body' => (new CDiv([getMessages(), $table]))->toString(),
62	'footer' => (new CListItem(_s('Updated: %s', zbx_date2str(TIME_FORMAT_SECONDS))))->toString()
63];
64
65if ($data['user']['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
66	CProfiler::getInstance()->stop();
67	$output['debug'] = CProfiler::getInstance()->make()->toString();
68}
69
70echo (new CJson())->encode($output);
71