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/** 23 * Shows surrogate screen filled with simple graphs generated by selected item prototype or preview of item prototype. 24 */ 25class CScreenLldSimpleGraph extends CScreenLldGraphBase { 26 27 /** 28 * @var array 29 */ 30 protected $createdItemIds = []; 31 32 /** 33 * @var array 34 */ 35 protected $itemPrototype = null; 36 37 /** 38 * Makes and returns simple graph screen items. 39 * 40 * @return array 41 */ 42 protected function getSurrogateScreenItems() { 43 $createdItemIds = $this->getCreatedItemIds(); 44 return $this->getSimpleGraphsForSurrogateScreen($createdItemIds); 45 } 46 47 /** 48 * Retrieves items created for item prototype given as resource for this screen item 49 * and returns array of the item IDs, ordered by item name. 50 * 51 * @return array 52 */ 53 protected function getCreatedItemIds() { 54 if (!$this->createdItemIds) { 55 $itemPrototype = $this->getItemPrototype(); 56 57 if ($itemPrototype) { 58 // get all created (discovered) items for current host 59 $allCreatedItems = API::Item()->get([ 60 'output' => ['itemid', 'name', 'key_', 'hostid'], 61 'hostids' => [$itemPrototype['discoveryRule']['hostid']], 62 'selectItemDiscovery' => ['itemid', 'parent_itemid'], 63 'filter' => ['flags' => ZBX_FLAG_DISCOVERY_CREATED], 64 ]); 65 66 // collect those items where parent item is item prototype selected for this screen item as resource 67 $createdItems = []; 68 foreach ($allCreatedItems as $item) { 69 if ($item['itemDiscovery']['parent_itemid'] == $itemPrototype['itemid']) { 70 $createdItems[] = $item; 71 } 72 } 73 74 $createdItems = CMacrosResolverHelper::resolveItemNames($createdItems); 75 foreach ($createdItems as $item) { 76 $this->createdItemIds[$item['itemid']] = $item['name_expanded']; 77 } 78 natsort($this->createdItemIds); 79 80 $this->createdItemIds = array_keys($this->createdItemIds); 81 } 82 } 83 84 return $this->createdItemIds; 85 } 86 87 /** 88 * Makes and return simple graph screen items from given item IDs. 89 * 90 * @param array $itemIds 91 * 92 * @return array 93 */ 94 protected function getSimpleGraphsForSurrogateScreen(array $itemIds) { 95 $screenItemTemplate = $this->getScreenItemTemplate(SCREEN_RESOURCE_SIMPLE_GRAPH); 96 97 $screenItems = []; 98 foreach ($itemIds as $itemId) { 99 $screenItem = $screenItemTemplate; 100 101 $screenItem['resourceid'] = $itemId; 102 $screenItem['screenitemid'] = $itemId; 103 104 $screenItems[] = $screenItem; 105 } 106 107 return $screenItems; 108 } 109 110 /** 111 * Returns output for simple graph preview. 112 * 113 * @return CTag 114 */ 115 protected function getPreviewOutput() { 116 $itemPrototype = $this->getItemPrototype(); 117 118 $queryParams = [ 119 'items' => [$itemPrototype], 120 'period' => 3600, 121 'legend' => 1, 122 'width' => $this->screenitem['width'], 123 'height' => $this->screenitem['height'], 124 'name' => $itemPrototype['hosts'][0]['name'].NAME_DELIMITER.$itemPrototype['name'] 125 ]; 126 127 return new CSpan(new CImg('chart3.php?'.http_build_query($queryParams))); 128 } 129 130 /** 131 * Resolves and retrieves effective item prototype used in this screen item. 132 * 133 * @return array|bool 134 */ 135 protected function getItemPrototype() { 136 if ($this->itemPrototype === null) { 137 $resourceid = array_key_exists('real_resourceid', $this->screenitem) 138 ? $this->screenitem['real_resourceid'] 139 : $this->screenitem['resourceid']; 140 141 $options = [ 142 'output' => ['itemid', 'name'], 143 'selectHosts' => ['name'], 144 'selectDiscoveryRule' => ['hostid'] 145 ]; 146 147 /* 148 * If screen item is dynamic or is templated screen, real item prototype is looked up by "key" 149 * used as resource ID for this screen item and by current host. 150 */ 151 if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && $this->hostid) { 152 $currentItemPrototype = API::ItemPrototype()->get([ 153 'output' => ['key_'], 154 'itemids' => [$resourceid] 155 ]); 156 $currentItemPrototype = reset($currentItemPrototype); 157 158 $options['hostids'] = [$this->hostid]; 159 $options['filter'] = ['key_' => $currentItemPrototype['key_']]; 160 } 161 // otherwise just use resource ID given to to this screen item. 162 else { 163 $options['itemids'] = [$resourceid]; 164 } 165 166 $selectedItemPrototype = API::ItemPrototype()->get($options); 167 $this->itemPrototype = reset($selectedItemPrototype); 168 } 169 170 return $this->itemPrototype; 171 } 172} 173