1<?php 2/** 3 * Created by PhpStorm. 4 * User: carst 5 * Date: 17.02.2018 6 * Time: 20:09 7 */ 8 9namespace Icinga\Module\Grafana\Controllers; 10 11use Icinga\Module\Grafana\Web\Controller\MonitoringAwareController; 12use Icinga\Module\Monitoring\Object\Service; 13use Icinga\Module\Monitoring\Object\Host; 14use Icinga\Web\Widget\Tab; 15use Icinga\Web\Hook; 16use Icinga\Module\Grafana\Helpers\Timeranges; 17use Icinga\Application\Config; 18 19 20class ShowController extends MonitoringAwareController 21{ 22 /** @var bool */ 23 protected $showFullscreen; 24 protected $host; 25 protected $custvardisable = "grafana_graph_disable"; 26 27 public function init() 28 { 29 $this->assertPermission('grafana/showall'); 30 $this->view->showFullscreen 31 = $this->showFullscreen 32 = (bool)$this->_helper->layout()->showFullscreen; 33 $this->host = $this->getParam('host'); 34 $this->config = Config::module('grafana')->getSection('grafana'); 35 /** 36 * Name of the custom varibale to disable graph 37 */ 38 $this->custvardisable = ($this->config->get('custvardisable', $this->custvardisable)); 39 } 40 41 public function indexAction() 42 { 43 $this->disableAutoRefresh(); 44 $this->view->host = $this->host; 45 46 if (!$this->showFullscreen) { 47 $tabs = $this->getTabs(); 48 $tabs->add('graphs', array( 49 'label' => $this->translate('Grafana Graphs'), 50 'url' => $this->getRequest()->getUrl() 51 ))->activate('graphs'); 52 53 $fullscreen = new Tab(array( 54 'title' => $this->translate('Print'), 55 'icon' => 'print', 56 'url' => ((string)htmlspecialchars_decode($this->getRequest()->getUrl())) . '&showFullscreen' 57 )); 58 $fullscreen->setTargetBlank(); 59 $tabs->addAsDropdown('fullscreen', $fullscreen); 60 61 $this->view->title = sprintf( 62 $this->translate('Performance graphs for %s'), 63 $this->host 64 ); 65 } 66 67 // Preserve timerange if selected 68 $parameters = ['host' => $this->host]; 69 if ($this->hasParam('timerange')) { 70 $parameters['timerange'] = $this->getParam('timerange'); 71 } 72 73 /* The timerange menu */ 74 $menu = new Timeranges($parameters, 'grafana/show'); 75 $this->view->menu = $menu->getTimerangeMenu(); 76 77 /* first host object for host graph */ 78 $this->object = $this->getHostObject($this->host); 79 $customvars= $this->object->fetchCustomvars()->customvars; 80 if ($this->object->process_perfdata || !(isset($customvars[$this->custvardisable]) && json_decode(strtolower($customvars[$this->custvardisable])) !== false)) { 81 $objects[] = $this->object; 82 } 83 /* Get all services for this host */ 84 $query = $this->backend->select()->from('servicestatus', [ 85 'service_description', 86 ]); 87 $this->applyRestriction('monitoring/filter/objects', $query); 88 89 foreach ($query->where('host_name', $this->host) as $service) { 90 $this->object = $this->getServiceObject($service->service_description, $this->host); 91 $customvars= $this->object->fetchCustomvars()->customvars; 92 if ($this->object->process_perfdata && !(isset($customvars[$this->custvardisable]) && json_decode(strtolower($customvars[$this->custvardisable])) !== false)) { 93 $objects[] = $this->object; 94 } 95 } 96 unset($this->object); 97 unset($customvars); 98 $this->view->objects = $objects; 99 $this->view->grapher = Hook::first('grapher'); 100 } 101 102 103 public function getHostObject($host) 104 { 105 106 $myHost = new Host($this->backend, $host); 107 $this->applyRestriction('monitoring/filter/objects', $myHost); 108 109 if ($myHost->fetch() === false) { 110 $this->httpNotFound($this->translate('Host not found')); 111 } 112 113 return $myHost; 114 } 115 116 public function getServiceObject($service, $host) 117 { 118 $myService = new Service( 119 $this->backend, 120 $host, 121 $service 122 ); 123 $this->applyRestriction('monitoring/filter/objects', $myService); 124 125 if ($myService->fetch() === false) { 126 $this->httpNotFound($this->translate('Service not found')); 127 } 128 129 return $myService; 130 } 131 132 133}