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 * A class to display web scenario details as a screen element by given "httptestid".
24 */
25class CScreenHttpTestDetails extends CScreenBase {
26
27	/**
28	 * Process screen.
29	 *
30	 * @return CDiv (screen inside container)
31	 */
32	public function get() {
33		$this->dataId = 'httptest_details';
34
35		$httptest = API::HttpTest()->get([
36			'output' => ['httptestid', 'name', 'hostid'],
37			'selectSteps' => ['httpstepid', 'name', 'no'],
38			'httptestids' => $this->profileIdx2,
39			'preservekeys' => true
40		]);
41		$httptest = reset($httptest);
42
43		if (!$httptest) {
44			$messages = [[
45				'type' => 'error',
46				'message' => _('No permissions to referred object or it does not exist!')
47			]];
48
49			return $this->getOutput(makeMessageBox(ZBX_STYLE_MSG_BAD, $messages, null, false));
50		}
51
52		$httptest['lastfailedstep'] = 0;
53		$httptest['error'] = '';
54
55		// fetch http test execution data
56		$httptest_data = Manager::HttpTest()->getLastData([$httptest['httptestid']]);
57
58		if ($httptest_data) {
59			$httptest_data = reset($httptest_data);
60		}
61
62		// fetch HTTP step items
63		$items = DBfetchArray(DBselect(
64			'SELECT i.value_type,i.units,i.itemid,hi.type,hs.httpstepid'.
65			' FROM items i,httpstepitem hi,httpstep hs'.
66			' WHERE hi.itemid=i.itemid'.
67				' AND hi.httpstepid=hs.httpstepid'.
68				' AND hs.httptestid='.zbx_dbstr($httptest['httptestid'])
69		));
70		$step_items = [];
71
72		foreach ($items as $item) {
73			$step_items[$item['httpstepid']][$item['type']] = $item + ['valuemap' => []];
74		}
75
76		// fetch HTTP item history
77		$item_history = Manager::History()->getLastValues($items);
78
79		$table = (new CTableInfo())
80			->setHeader([
81				_('Step'),
82				_('Speed'),
83				_('Response time'),
84				_('Response code'),
85				_('Status')
86		]);
87
88		$total_time = [
89			'value' => 0,
90			'value_type' => null,
91			'valuemap' => [],
92			'units' => null
93		];
94
95		order_result($httptest['steps'], 'no');
96
97		foreach ($httptest['steps'] as $step_data) {
98			$items_by_type = $step_items[$step_data['httpstepid']];
99
100			$status['msg'] = _('OK');
101			$status['style'] = ZBX_STYLE_GREEN;
102			$status['afterError'] = false;
103
104			if (!array_key_exists('lastfailedstep', $httptest_data)) {
105				$status['msg'] = '';
106			}
107			elseif ($httptest_data['lastfailedstep'] != 0) {
108				if ($httptest_data['lastfailedstep'] == $step_data['no']) {
109					$status['msg'] = ($httptest_data['error'] === null)
110						? _('Unknown error')
111						: _s('Error: %1$s', $httptest_data['error']);
112					$status['style'] = ZBX_STYLE_RED;
113				}
114				elseif ($httptest_data['lastfailedstep'] < $step_data['no']) {
115					$status['msg'] = _('Unknown');
116					$status['style'] = ZBX_STYLE_GREY;
117					$status['afterError'] = true;
118				}
119			}
120
121			foreach ($items_by_type as &$item) {
122				// Calculate the total time it took to execute the scenario.
123				// Skip steps that come after a failed step.
124				if (!$status['afterError'] && $item['type'] == HTTPSTEP_ITEM_TYPE_TIME) {
125					$total_time['value_type'] = $item['value_type'];
126					$total_time['valuemap'] = $item['valuemap'];
127					$total_time['units'] = $item['units'];
128
129					if (array_key_exists($item['itemid'], $item_history)) {
130						$history = $item_history[$item['itemid']][0];
131						$total_time['value'] += $history['value'];
132					}
133				}
134			}
135			unset($item);
136
137			// step speed
138			$speed_item = $items_by_type[HTTPSTEP_ITEM_TYPE_IN];
139			if (!$status['afterError'] && array_key_exists($speed_item['itemid'], $item_history)
140					&& $item_history[$speed_item['itemid']][0]['value'] > 0) {
141				$speed = formatHistoryValue($item_history[$speed_item['itemid']][0]['value'], $speed_item);
142			}
143			else {
144				$speed = UNKNOWN_VALUE;
145			}
146
147			// step response time
148			$resptime_item = $items_by_type[HTTPSTEP_ITEM_TYPE_TIME];
149			if (!$status['afterError'] && array_key_exists($resptime_item['itemid'], $item_history)
150					&& $item_history[$resptime_item['itemid']][0]['value'] > 0) {
151				$resp_time = formatHistoryValue($item_history[$resptime_item['itemid']][0]['value'], $resptime_item);
152			}
153			else {
154				$resp_time = UNKNOWN_VALUE;
155			}
156
157			// step response code
158			$resp_item = $items_by_type[HTTPSTEP_ITEM_TYPE_RSPCODE];
159			if (!$status['afterError'] && array_key_exists($resp_item['itemid'], $item_history)
160					&& $item_history[$resp_item['itemid']][0]['value'] > 0) {
161				$resp = formatHistoryValue($item_history[$resp_item['itemid']][0]['value'], $resp_item);
162			}
163			else {
164				$resp = UNKNOWN_VALUE;
165			}
166
167			$table->addRow([
168				CMacrosResolverHelper::resolveHttpTestName($httptest['hostid'], $step_data['name']),
169				$speed,
170				$resp_time,
171				$resp,
172				(new CSpan($status['msg']))->addClass($status['style'])
173			]);
174		}
175
176
177		if (!array_key_exists('lastfailedstep', $httptest_data)) {
178			$status_info = '';
179		}
180		elseif ($httptest_data['lastfailedstep'] != 0) {
181			$status_info = (new CSpan(
182				($httptest_data['error'] === null) ? _('Unknown error') : _s('Error: %1$s', $httptest_data['error'])
183			))->addClass(ZBX_STYLE_RED);
184		}
185		else {
186			$status_info = (new CSpan(_('OK')))->addClass(ZBX_STYLE_GREEN);
187		}
188
189		$table->addRow([
190			bold(_('TOTAL')),
191			'',
192			bold(($total_time['value']) ? formatHistoryValue($total_time['value'], $total_time) : UNKNOWN_VALUE),
193			'',
194			$status_info
195		]);
196
197		return $this->getOutput($table);
198	}
199}
200