File: CScreenLldSimpleGraph.php

package info (click to toggle)
zabbix 1%3A4.0.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 83,104 kB
  • sloc: php: 162,280; ansic: 154,778; sql: 96,479; sh: 5,281; makefile: 1,340; java: 1,068; cpp: 227; perl: 41; xml: 29
file content (172 lines) | stat: -rw-r--r-- 5,006 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


/**
 * Shows surrogate screen filled with simple graphs generated by selected item prototype or preview of item prototype.
 */
class CScreenLldSimpleGraph extends CScreenLldGraphBase {

	/**
	 * @var array
	 */
	protected $createdItemIds = [];

	/**
	 * @var array
	 */
	protected $itemPrototype = null;

	/**
	 * Makes and returns simple graph screen items.
	 *
	 * @return array
	 */
	protected function getSurrogateScreenItems() {
		$createdItemIds = $this->getCreatedItemIds();
		return $this->getSimpleGraphsForSurrogateScreen($createdItemIds);
	}

	/**
	 * Retrieves items created for item prototype given as resource for this screen item
	 * and returns array of the item IDs, ordered by item name.
	 *
	 * @return array
	 */
	protected function getCreatedItemIds() {
		if (!$this->createdItemIds) {
			$itemPrototype = $this->getItemPrototype();

			if ($itemPrototype) {
				// get all created (discovered) items for current host
				$allCreatedItems = API::Item()->get([
					'output' => ['itemid', 'name', 'key_', 'hostid'],
					'hostids' => [$itemPrototype['discoveryRule']['hostid']],
					'selectItemDiscovery' => ['itemid', 'parent_itemid'],
					'filter' => ['flags' => ZBX_FLAG_DISCOVERY_CREATED],
				]);

				// collect those items where parent item is item prototype selected for this screen item as resource
				$createdItems = [];
				foreach ($allCreatedItems as $item) {
					if ($item['itemDiscovery']['parent_itemid'] == $itemPrototype['itemid']) {
						$createdItems[] = $item;
					}
				}

				$createdItems = CMacrosResolverHelper::resolveItemNames($createdItems);
				foreach ($createdItems as $item) {
					$this->createdItemIds[$item['itemid']] = $item['name_expanded'];
				}
				natsort($this->createdItemIds);

				$this->createdItemIds = array_keys($this->createdItemIds);
			}
		}

		return $this->createdItemIds;
	}

	/**
	 * Makes and return simple graph screen items from given item IDs.
	 *
	 * @param array $itemIds
	 *
	 * @return array
	 */
	protected function getSimpleGraphsForSurrogateScreen(array $itemIds) {
		$screenItemTemplate = $this->getScreenItemTemplate(SCREEN_RESOURCE_SIMPLE_GRAPH);

		$screenItems = [];
		foreach ($itemIds as $itemId) {
			$screenItem = $screenItemTemplate;

			$screenItem['resourceid'] = $itemId;
			$screenItem['screenitemid'] = $itemId;

			$screenItems[] = $screenItem;
		}

		return $screenItems;
	}

	/**
	 * Returns output for simple graph preview.
	 *
	 * @return CTag
	 */
	protected function getPreviewOutput() {
		$itemPrototype = $this->getItemPrototype();

		$queryParams = [
			'items' => [$itemPrototype],
			'period' => 3600,
			'legend' => 1,
			'width' => $this->screenitem['width'],
			'height' => $this->screenitem['height'],
			'name' => $itemPrototype['hosts'][0]['name'].NAME_DELIMITER.$itemPrototype['name']
		];

		return new CSpan(new CImg('chart3.php?'.http_build_query($queryParams)));
	}

	/**
	 * Resolves and retrieves effective item prototype used in this screen item.
	 *
	 * @return array|bool
	 */
	protected function getItemPrototype() {
		if ($this->itemPrototype === null) {
			$resourceid = array_key_exists('real_resourceid', $this->screenitem)
				? $this->screenitem['real_resourceid']
				: $this->screenitem['resourceid'];

			$options = [
				'output' => ['itemid', 'name'],
				'selectHosts' => ['name'],
				'selectDiscoveryRule' => ['hostid']
			];

			/*
			 * If screen item is dynamic or is templated screen, real item prototype is looked up by "key"
			 * used as resource ID for this screen item and by current host.
			 */
			if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && $this->hostid) {
				$currentItemPrototype = API::ItemPrototype()->get([
					'output' => ['key_'],
					'itemids' => [$resourceid]
				]);
				$currentItemPrototype = reset($currentItemPrototype);

				$options['hostids'] = [$this->hostid];
				$options['filter'] = ['key_' => $currentItemPrototype['key_']];
			}
			// otherwise just use resource ID given to to this screen item.
			else {
				$options['itemids'] = [$resourceid];
			}

			$selectedItemPrototype = API::ItemPrototype()->get($options);
			$this->itemPrototype = reset($selectedItemPrototype);
		}

		return $this->itemPrototype;
	}
}