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 22class CControllerReportServices extends CController { 23 24 const YEAR_LEFT_SHIFT = 5; 25 26 private $service = null; 27 28 protected function init() { 29 $this->disableSIDValidation(); 30 } 31 32 protected function checkInput() { 33 $fields = [ 34 'fullscreen' => 'in 0,1', 35 'serviceid' => 'fatal|required|db services.serviceid', 36 'period' => 'in daily,weekly,monthly,yearly', 37 'year' => 'int32' 38 ]; 39 40 $ret = $this->validateInput($fields); 41 42 if (!$ret) { 43 $this->setResponse(new CControllerResponseFatal()); 44 } 45 46 return $ret; 47 } 48 49 protected function checkPermissions() { 50 if ($this->getUserType() < USER_TYPE_ZABBIX_USER) { 51 return false; 52 } 53 54 $services = API::Service()->get([ 55 'output' => ['serviceid', 'name', 'showsla', 'goodsla'], 56 'serviceids' => [$this->getInput('serviceid')] 57 ]); 58 59 if (!$services) { 60 return false; 61 } 62 63 $this->service = $services[0]; 64 65 return true; 66 } 67 68 protected function doAction() { 69 // default values 70 $data = [ 71 'fullscreen' => $this->getInput('fullscreen', 0), 72 'period' => $this->getInput('period', 'yearly'), 73 'service' => $this->service, 74 'year' => $this->getInput('year', date('Y')), 75 'YEAR_LEFT_SHIFT' => self::YEAR_LEFT_SHIFT 76 ]; 77 78 switch ($data['period']) { 79 case 'yearly': 80 $from = date('Y') - self::YEAR_LEFT_SHIFT; 81 $to = date('Y'); 82 83 function get_time($year, $y) { 84 return mktime(0, 0, 0, 1, 1, $y); 85 } 86 break; 87 88 case 'monthly': 89 $from = 1; 90 $to = 12; 91 92 function get_time($year, $m) { 93 return mktime(0, 0, 0, $m, 1, $year); 94 } 95 break; 96 97 case 'weekly': 98 $from = 0; 99 $to = 52; 100 101 function get_time($year, $w) { 102 $time = mktime(0, 0, 0, 1, 1, $year); 103 $wd = date('w', $time); 104 $wd = $wd == 0 ? 6 : $wd - 1; 105 $beg = $time - $wd * SEC_PER_DAY; 106 107 return strtotime("+$w week", $beg); 108 } 109 break; 110 111 case 'daily': 112 $from = 1; 113 $to = DAY_IN_YEAR; 114 115 function get_time($year, $d) { 116 return mktime(0, 0, 0, 1, $d, $year); 117 } 118 break; 119 } 120 121 $now = time(); 122 $intervals = []; 123 124 for ($t = $from; $t <= $to; $t++) { 125 if (($start = get_time($data['year'], $t)) > $now) { 126 break; 127 } 128 129 if (($end = get_time($data['year'], $t + 1)) > $now) { 130 $end = $now; 131 } 132 133 $intervals[] = [ 134 'from' => $start, 135 'to' => $end 136 ]; 137 } 138 139 $sla = API::Service()->getSla([ 140 'serviceids' => [$this->service['serviceid']], 141 'intervals' => $intervals 142 ]); 143 $data['sla'] = reset($sla); 144 145 $response = new CControllerResponseData($data); 146 $response->setTitle(_('IT services availability report')); 147 $this->setResponse($response); 148 } 149} 150