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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
<?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 graphs generated by selected graph prototype or preview of graph prototype.
*/
class CScreenLldGraph extends CScreenLldGraphBase {
/**
* @var array
*/
protected $createdGraphIds = [];
/**
* @var array
*/
protected $graphPrototype = null;
/**
* Returns screen items for surrogate screen.
*
* @return array
*/
protected function getSurrogateScreenItems() {
$createdGraphIds = $this->getCreatedGraphIds();
return $this->getGraphsForSurrogateScreen($createdGraphIds);
}
/**
* Retrieves graphs created for graph prototype given as resource for this screen item
* and returns array of the graph IDs.
*
* @return array
*/
protected function getCreatedGraphIds() {
if (!$this->createdGraphIds) {
$graphPrototype = $this->getGraphPrototype();
if ($graphPrototype) {
// Get all created (discovered) graphs for host of graph prototype.
$allCreatedGraphs = API::Graph()->get([
'output' => ['graphid', 'name'],
'hostids' => [$graphPrototype['discoveryRule']['hostid']],
'selectGraphDiscovery' => ['graphid', 'parent_graphid'],
'expandName' => true,
'filter' => ['flags' => ZBX_FLAG_DISCOVERY_CREATED],
]);
// Collect those graph IDs where parent graph is graph prototype selected for
// this screen item as resource.
foreach ($allCreatedGraphs as $graph) {
if ($graph['graphDiscovery']['parent_graphid'] == $graphPrototype['graphid']) {
$this->createdGraphIds[$graph['graphid']] = $graph['name'];
}
}
natsort($this->createdGraphIds);
$this->createdGraphIds = array_keys($this->createdGraphIds);
}
}
return $this->createdGraphIds;
}
/**
* Makes graph screen items from given graph IDs.
*
* @param array $graphIds
*
* @return array
*/
protected function getGraphsForSurrogateScreen(array $graphIds) {
$screenItemTemplate = $this->getScreenItemTemplate(SCREEN_RESOURCE_GRAPH);
$screenItems = [];
foreach ($graphIds as $graphId) {
$screenItem = $screenItemTemplate;
$screenItem['resourceid'] = $graphId;
$screenItem['screenitemid'] = $graphId;
$screenItems[] = $screenItem;
}
return $screenItems;
}
/**
* Resolves and retrieves effective graph prototype used in this screen item.
*
* @return array|bool
*/
protected function getGraphPrototype() {
if ($this->graphPrototype === null) {
$resourceid = array_key_exists('real_resourceid', $this->screenitem)
? $this->screenitem['real_resourceid']
: $this->screenitem['resourceid'];
$options = [
'output' => ['graphid', 'name', 'graphtype', 'show_legend', 'show_3d', 'templated'],
'selectDiscoveryRule' => ['hostid']
];
/*
* If screen item is dynamic or is templated screen, real graph prototype is looked up by "name"
* used as resource ID for this screen item and by current host.
*/
if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && $this->hostid) {
$currentGraphPrototype = API::GraphPrototype()->get([
'output' => ['name'],
'graphids' => [$resourceid]
]);
$currentGraphPrototype = reset($currentGraphPrototype);
$options['hostids'] = [$this->hostid];
$options['filter'] = ['name' => $currentGraphPrototype['name']];
}
// otherwise just use resource ID given to this screen item.
else {
$options['graphids'] = [$resourceid];
}
$selectedGraphPrototype = API::GraphPrototype()->get($options);
$this->graphPrototype = reset($selectedGraphPrototype);
}
return $this->graphPrototype;
}
/**
* Returns output for preview of graph prototype.
*
* @return CTag
*/
protected function getPreviewOutput() {
$graphPrototype = $this->getGraphPrototype();
switch ($graphPrototype['graphtype']) {
case GRAPH_TYPE_NORMAL:
case GRAPH_TYPE_STACKED:
$url = 'chart3.php';
break;
case GRAPH_TYPE_EXPLODED:
case GRAPH_TYPE_3D_EXPLODED:
case GRAPH_TYPE_3D:
case GRAPH_TYPE_PIE:
$url = 'chart7.php';
break;
default:
show_error_message(_('Graph prototype not found.'));
exit;
}
$graphPrototypeItems = API::GraphItem()->get([
'output' => [
'gitemid', 'itemid', 'sortorder', 'flags', 'type', 'calc_fnc', 'drawtype', 'yaxisside', 'color'
],
'graphids' => [$graphPrototype['graphid']]
]);
$queryParams = [
'items' => $graphPrototypeItems,
'graphtype' => $graphPrototype['graphtype'],
'period' => 3600,
'legend' => $graphPrototype['show_legend'],
'graph3d' => $graphPrototype['show_3d'],
'width' => $this->screenitem['width'],
'height' => $this->screenitem['height'],
'name' => $graphPrototype['name']
];
$url .= '?'.http_build_query($queryParams);
return new CSpan(new CImg($url));
}
}
|