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/triggers.inc.php';
24
25$page['file'] = 'chart4.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	'triggerid' => [T_ZBX_INT, O_MAND, P_SYS, DB_ID, null]
33];
34if (!check_fields($fields)) {
35	exit();
36}
37
38/*
39 * Permissions
40 */
41if (!hasRequest('triggerid')) {
42	fatal_error(_('No triggers defined.'));
43}
44
45$dbTriggers = API::Trigger()->get([
46	'output' => ['description'],
47	'triggerids' => getRequest('triggerid'),
48	'expandDescription' => true
49]);
50
51if (!$dbTriggers) {
52	access_deny();
53}
54
55$dbTrigger = reset($dbTriggers);
56
57/*
58 * Display
59 */
60$debug_mode = CWebUser::getDebugMode();
61if ($debug_mode) {
62	$start_time = microtime(true);
63}
64
65$sizeX = 900;
66$sizeY = 300;
67
68$shiftX = 12;
69$shiftYup = 17;
70$shiftYdown = 55;
71
72$im = imagecreate($sizeX + $shiftX + 61, $sizeY + $shiftYup + $shiftYdown + 10);
73
74$red = imagecolorallocate($im, 255, 0, 0);
75$darkred = imagecolorallocate($im, 150, 0, 0);
76$green = imagecolorallocate($im, 0, 255, 0);
77$darkgreen = imagecolorallocate($im, 0, 150, 0);
78$bluei = imagecolorallocate($im, 0, 0, 255);
79$darkblue = imagecolorallocate($im, 0, 0, 150);
80$yellow = imagecolorallocate($im, 255, 255, 0);
81$darkyellow = imagecolorallocate($im, 150, 150, 0);
82$cyan = imagecolorallocate($im, 0, 255, 255);
83$black = imagecolorallocate($im, 0, 0, 0);
84$gray = imagecolorallocate($im, 150, 150, 150);
85$white = imagecolorallocate($im, 255, 255, 255);
86$bg = imagecolorallocate($im, 102, 119, 136);
87
88$x = imagesx($im);
89$y = imagesy($im);
90
91imagefilledrectangle($im, 0, 0, $x, $y, $white);
92imagerectangle($im, 0, 0, $x - 1, $y - 1, $black);
93
94$str = _s('%1$s (year %2$s)', $dbTrigger['description'], zbx_date2str(_x('Y', DATE_FORMAT_CONTEXT)));
95$x = imagesx($im) / 2 - imagefontwidth(4) * mb_strlen($str) / 2;
96imageText($im, 10, 0, $x, 14, $darkred, $str);
97
98$now = time();
99$count_now = [];
100$true = [];
101$false = [];
102
103$start = mktime(0, 0, 0, 1, 1, date('Y'));
104
105$wday = date('w', $start);
106if ($wday == 0) {
107	$wday = 7;
108}
109$start = $start - ($wday - 1) * SEC_PER_DAY;
110
111$weeks = (int) (date('z') / 7 + 1);
112
113for ($i = 0; $i < $weeks; $i++) {
114	$periodStart = $start + SEC_PER_WEEK * $i;
115	$periodEnd = $start + SEC_PER_WEEK * ($i + 1);
116
117	$stat = calculateAvailability(getRequest('triggerid'), $periodStart, $periodEnd - 1);
118	$true[$i] = $stat['true'];
119	$false[$i] = $stat['false'];
120	$count_now[$i] = 1;
121}
122
123for ($i = 0; $i <= $sizeY; $i += $sizeY / 10) {
124	dashedLine($im, $shiftX, $i + $shiftYup, $sizeX + $shiftX, $i + $shiftYup, $gray);
125}
126
127for ($i = 0, $periodStart = $start; $i <= $sizeX; $i += $sizeX / 52) {
128	dashedLine($im, $i + $shiftX, $shiftYup, $i + $shiftX, $sizeY + $shiftYup, $gray);
129	imageText($im, 6, 90, $i + $shiftX + 4, $sizeY + $shiftYup + 30, $black, zbx_date2str(_('d.M'), $periodStart));
130
131	$periodStart += SEC_PER_WEEK;
132}
133
134$maxY = max(max($true), 100);
135$minY = 0;
136
137$maxX = 900;
138$minX = 0;
139
140for ($i = 1; $i <= $weeks; $i++) {
141	$x1 = (900 / 52) * $sizeX * ($i - 1 - $minX) / ($maxX - $minX);
142
143	$yt = $sizeY * $true[$i - 1] / 100;
144	if ($yt > 0) {
145		imagefilledrectangle($im, $x1 + $shiftX, $shiftYup, $x1 + $shiftX + 8, $yt + $shiftYup, imagecolorallocate($im, 235, 120, 120)); // red
146	}
147
148	$yf = $sizeY * $false[$i - 1] / 100;
149	if ($yf > 0) {
150		imagefilledrectangle($im, $x1 + $shiftX, $yt + $shiftYup, $x1 + $shiftX + 8, $sizeY + $shiftYup, imagecolorallocate($im, 120, 235, 120)); // green
151	}
152
153	if ($yt + $yf > 0) {
154		imagerectangle($im, $x1 + $shiftX, $shiftYup, $x1 + $shiftX + 8, $sizeY + $shiftYup, $black);
155	}
156}
157
158for ($i = 0; $i <= $sizeY; $i += $sizeY / 10) {
159	imageText($im, 7, 0, $sizeX + 5 + $shiftX, $sizeY - $i - 4 + $shiftYup + 8, $darkred, $i * ($maxY - $minY) / $sizeY + $minY);
160}
161
162imagefilledrectangle($im, $shiftX, $sizeY + $shiftYup + 39, $shiftX + 5, $sizeY + $shiftYup + 44, imagecolorallocate($im, 120, 235, 120));
163imagerectangle($im, $shiftX, $sizeY + $shiftYup + 39, $shiftX + 5, $sizeY + $shiftYup + 44, $black);
164imageText($im, 8, 0, $shiftX + 9, $sizeY + $shiftYup + 45, $black, _('OK').' (%)');
165
166imagefilledrectangle($im, $shiftX, $sizeY + $shiftYup + 54, $shiftX + 5, $sizeY + $shiftYup + 59, imagecolorallocate($im, 235, 120, 120));
167imagerectangle($im, $shiftX, $sizeY + $shiftYup + 54, $shiftX + 5, $sizeY + $shiftYup + 59, $black);
168imageText($im, 8, 0, $shiftX + 9, $sizeY + $shiftYup + 60, $black, _('Problems').' (%)');
169
170if ($debug_mode) {
171	$str = sprintf('%0.2f', microtime(true) - $start_time);
172	$str = _s('Generated in %s sec', $str);
173	$str_size = imageTextSize(6, 0, $str);
174	imageText($im, 6, 0, imagesx($im) - $str_size['width'] - 5, imagesy($im) - 5, $gray, $str);
175}
176
177imageOut($im);
178imagedestroy($im);
179
180require_once dirname(__FILE__).'/include/page_footer.php';
181