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
22require_once dirname(__FILE__).'/include/config.inc.php';
23require_once dirname(__FILE__).'/include/services.inc.php';
24
25$page['file'] = 'chart5.php';
26$page['type'] = PAGE_TYPE_IMAGE;
27
28require_once dirname(__FILE__).'/include/page_header.php';
29
30// VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
31$fields = [
32	'serviceid' => [T_ZBX_INT, O_MAND, P_SYS, DB_ID, null]
33];
34if (!check_fields($fields)) {
35	exit();
36}
37
38/*
39 * Permissions
40 */
41$service = API::Service()->get([
42	'output' => ['serviceid', 'name'],
43	'serviceids' => $_REQUEST['serviceid']
44]);
45$service = reset($service);
46if (!$service) {
47	access_deny();
48}
49
50/*
51 * Display
52 */
53$start_time = microtime(true);
54
55$sizeX = 900;
56$sizeY = 300;
57
58$shiftX = 12;
59$shiftYup = 25;
60$shiftYdown = 25 + 15 * 3;
61
62if (function_exists('imagecolorexactalpha') && function_exists('imagecreatetruecolor') && @imagecreatetruecolor(1, 1)) {
63	$im = imagecreatetruecolor($sizeX + $shiftX + 61, $sizeY + $shiftYup + $shiftYdown + 10);
64}
65else {
66	$im = imagecreate($sizeX + $shiftX + 61, $sizeY + $shiftYup + $shiftYdown + 10);
67}
68
69$graphtheme = [
70	'theme' => 'blue-theme',
71	'textcolor' => '1F2C33',
72	'highlightcolor' => 'E33734',
73	'backgroundcolor' => 'FFFFFF',
74	'graphcolor' => 'FFFFFF',
75	'gridcolor' => 'CCD5D9',
76	'maingridcolor' => 'ACBBC2',
77	'gridbordercolor' => 'ACBBC2',
78	'nonworktimecolor' => 'EBEBEB',
79	'leftpercentilecolor' => '429E47',
80	'righttpercentilecolor' => 'E33734'
81];
82
83$themes = DB::find('graph_theme', [
84	'theme' => getUserTheme(CWebUser::$data)
85]);
86if ($themes) {
87	$graphtheme = $themes[0];
88}
89
90$black = get_color($im, '000000');
91$green = get_color($im, '34AF67');
92$red = get_color($im, 'D64E4E');
93$grey = get_color($im, '969696', 50);
94$backgroundcolor = get_color($im, $graphtheme['backgroundcolor']);
95$gridcolor = get_color($im, $graphtheme['gridcolor']);
96$textcolor = get_color($im, $graphtheme['textcolor']);
97$highlightcolor = get_color($im, $graphtheme['highlightcolor']);
98
99$x = imagesx($im);
100$y = imagesy($im);
101
102imagefilledrectangle($im, 0, 0, $x, $y, $backgroundcolor);
103
104$d = zbx_date2str(_x('Y', DATE_FORMAT_CONTEXT));
105$str = _s('%1$s (year %2$s)', $service['name'], $d);
106$x = imagesx($im) / 2 - imagefontwidth(4) * mb_strlen($str) / 2;
107imageText($im, 10, 0, $x, 14, $textcolor, $str);
108
109$now = time(null);
110$to_time = $now;
111
112$count_now = [];
113$problem = [];
114
115$start = mktime(0, 0, 0, 1, 1, date('Y'));
116
117$wday = date('w', $start);
118if ($wday == 0) {
119	$wday = 7;
120}
121$start = $start - ($wday - 1) * 24 * 3600;
122
123$weeks = (int) date('W') + ($wday ? 1 : 0);
124
125$intervals = [];
126for ($i = 0; $i < 52; $i++) {
127	if (($period_start = $start + 7 * 24 * 3600 * $i) > time()) {
128		break;
129	}
130
131	if (($period_end = $start + 7 * 24 * 3600 * ($i + 1)) > time()) {
132		$period_end = time();
133	}
134
135	$intervals[] = [
136		'from' => $period_start,
137		'to' => $period_end
138	];
139}
140
141$sla = API::Service()->getSla([
142	'serviceids' => $service['serviceid'],
143	'intervals' => $intervals
144]);
145$sla = reset($sla);
146
147foreach ($sla['sla'] as $i => $intervalSla) {
148	$problem[$i] = 100 - $intervalSla['problemTime'];
149	$ok[$i] = $intervalSla['sla'];
150	$count_now[$i] = 1;
151}
152
153for ($i = 0; $i <= $sizeY; $i += $sizeY / 10) {
154	dashedLine($im, $shiftX, $i + $shiftYup, $sizeX + $shiftX, $i + $shiftYup, $gridcolor);
155}
156
157for ($i = 0, $period_start = $start; $i <= $sizeX; $i += $sizeX / 52) {
158	dashedLine($im, $i + $shiftX, $shiftYup, $i + $shiftX, $sizeY + $shiftYup, $gridcolor);
159	imageText($im, 6, 90, $i + $shiftX + 4, $sizeY + $shiftYup + 35, $textcolor, zbx_date2str(_('d.M'), $period_start));
160	$period_start += 7 * 24 * 3600;
161}
162
163$maxY = max(max($problem), 100);
164$minY = 0;
165
166$maxX = $sizeX;
167$minX = 0;
168
169for ($i = 1; $i <= $weeks; $i++) {
170	if (!isset($ok[$i-1])) {
171		continue;
172	}
173	$x2 = ($sizeX / 52) * ($i - 1 - $minX) * $sizeX / ($maxX - $minX);
174	$y2 = $sizeY * ($ok[$i - 1] - $minY) / ($maxY - $minY);
175
176	$maxSizeY = $sizeY;
177	if ($i == $weeks) {
178		$maxSizeY = $sizeY * (date('w') / 7);
179		$y2 = $maxSizeY * ($ok[$i - 1] - $minY) / ($maxY - $minY);
180	}
181
182	if ($y2 != 0) {
183		imagefilledrectangle(
184			$im,
185			$x2 + $shiftX, $shiftYup + $sizeY - $y2,
186			$x2 + $shiftX + 8, $shiftYup + $sizeY,
187			$green
188		);
189	}
190	if ($y2 != $maxSizeY) {
191		imagefilledrectangle(
192			$im,
193			$x2 + $shiftX, $shiftYup + $sizeY - $maxSizeY,
194			$x2 + $shiftX + 8, $shiftYup + $sizeY - $y2,
195			$red
196		);
197	}
198}
199
200for ($i = 0; $i <= $sizeY; $i += $sizeY / 10) {
201	imageText($im, 7, 0, $sizeX + 5 + $shiftX, $sizeY - $i - 4 + $shiftYup + 8, $highlightcolor,
202		($i * ($maxY - $minY) / $sizeY + $minY).'%'
203	);
204}
205
206$x = $shiftX;
207$y = $sizeY + $shiftYup + 31;
208
209imagefilledrectangle($im, $x, $y + 15 * 1, $x + 10, $y + 10 + 15 * 1, $green);
210imagerectangle($im, $x, $y + 15 * 1, $x + 10, $y + 10 + 15 * 1, $black);
211imageText($im, 8, 0, $x + 14, $y + 10 + 15 * 1, $textcolor, _('OK').' (%)');
212
213imagefilledrectangle($im, $x, $y + 15 * 2, $x + 10, $y + 10 + 15 * 2, $red);
214imagerectangle($im, $x, $y + 15 * 2, $x + 10, $y + 10 + 15 * 2, $black);
215imageText($im, 8, 0, $x + 14, $y + 10 + 15 * 2, $textcolor, _('PROBLEM').' (%)');
216
217imagestringup($im, 1, imagesx($im) - 10, imagesy($im) - 50, ZABBIX_HOMEPAGE, $grey);
218
219$str = sprintf('%0.2f', microtime(true) - $start_time);
220$str = _s('Generated in %s sec', $str);
221$strSize = imageTextSize(6, 0, $str);
222imageText($im, 6, 0, imagesx($im) - $strSize['width'] - 5, imagesy($im) - 5, $grey, $str);
223imageOut($im);
224imagedestroy($im);
225
226require_once dirname(__FILE__).'/include/page_footer.php';
227