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 $item_prototype = $this->getItemPrototype(); 117 118 $src = (new CUrl('chart3.php')) 119 ->setArgument('items', [$item_prototype]) 120 ->setArgument('period', 3600) 121 ->setArgument('legend', 1) 122 ->setArgument('width', $this->screenitem['width']) 123 ->setArgument('height', $this->screenitem['height']) 124 ->setArgument('name', $item_prototype['hosts'][0]['name'].NAME_DELIMITER.$item_prototype['name']); 125 126 return new CSpan(new CImg($src->getUrl())); 127 } 128 129 /** 130 * Resolves and retrieves effective item prototype used in this screen item. 131 * 132 * @return array|bool 133 */ 134 protected function getItemPrototype() { 135 if ($this->itemPrototype === null) { 136 $resourceid = array_key_exists('real_resourceid', $this->screenitem) 137 ? $this->screenitem['real_resourceid'] 138 : $this->screenitem['resourceid']; 139 140 $options = [ 141 'output' => ['itemid', 'name'], 142 'selectHosts' => ['name'], 143 'selectDiscoveryRule' => ['hostid'] 144 ]; 145 146 /* 147 * If screen item is dynamic or is templated screen, real item prototype is looked up by "key" 148 * used as resource ID for this screen item and by current host. 149 */ 150 if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && $this->hostid) { 151 $currentItemPrototype = API::ItemPrototype()->get([ 152 'output' => ['key_'], 153 'itemids' => [$resourceid] 154 ]); 155 $currentItemPrototype = reset($currentItemPrototype); 156 157 $options['hostids'] = [$this->hostid]; 158 $options['filter'] = ['key_' => $currentItemPrototype['key_']]; 159 } 160 // otherwise just use resource ID given to to this screen item. 161 else { 162 $options['itemids'] = [$resourceid]; 163 } 164 165 $selectedItemPrototype = API::ItemPrototype()->get($options); 166 $this->itemPrototype = reset($selectedItemPrototype); 167 } 168 169 return $this->itemPrototype; 170 } 171} 172