1<?php
2// Pandora FMS - http://pandorafms.com
3// ==================================================
4// Copyright (c) 2005-2010 Artica Soluciones Tecnologicas
5// Please see http://pandorafms.org for full contribution list
6
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation for version 2.
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14
15include_once("../include/functions_users.php");
16
17class Agent {
18	private $correct_acl = false;
19	private $id = 0;
20	private $agent = null;
21
22	function __construct() {
23		$system = System::getInstance();
24
25		$this->id = $system->getRequest('id', 0);
26		$this->agent = agents_get_agents(array(
27			'disabled' => 0,
28			'id_agente' => $this->id), array('*'));
29
30		if (!empty($this->agent)) {
31			$this->agent = $this->agent[0];
32
33
34			if ($system->checkACL('AR', $this->agent['id_grupo'])) {
35				$this->correct_acl = true;
36			}
37			else {
38				$this->correct_acl = false;
39			}
40		}
41		else {
42			$this->agent = null;
43			$this->correct_acl = true;
44		}
45	}
46
47	public function show() {
48		if (!$this->correct_acl) {
49			$this->show_fail_acl();
50		}
51		else {
52			$this->show_agent();
53		}
54	}
55
56	private function show_fail_acl() {
57		$error['type'] = 'onStart';
58		$error['title_text'] = __('You don\'t have access to this page');
59		$error['content_text'] = __('Access to this page is restricted to authorized users only, please contact system administrator if you need assistance. <br><br>Please know that all attempts to access this page are recorded in security logs of Pandora System Database');
60		if (class_exists("HomeEnterprise"))
61			$home = new HomeEnterprise();
62		else
63			$home = new Home();
64		$home->show($error);
65	}
66
67
68	public function ajax($parameter2 = false) {
69		$system = System::getInstance();
70
71		if (!$this->correct_acl) {
72			return;
73		}
74		else {
75			switch ($parameter2) {
76				case 'render_events_bar':
77					$agent_id = $system->getRequest('agent_id', '0');
78					$width = $system->getRequest('width', '400');
79					graph_graphic_agentevents(
80						$this->id, $width, 30, SECONDS_1DAY, ui_get_full_url(false));
81					exit;
82			}
83		}
84	 }
85
86	private function show_agent() {
87		$ui = Ui::getInstance();
88		$system = System::getInstance();
89
90		$ui->createPage();
91
92		if ($this->id != 0) {
93			$agent_name = (string) agents_get_name ($this->id);
94
95			$ui->createDefaultHeader(
96				sprintf('%s', $agent_name),
97				$ui->createHeaderButton(
98					array('icon' => 'back',
99						'pos' => 'left',
100						'text' => __('Back'),
101						'href' => 'index.php?page=agents')));
102		}
103		else {
104			$ui->createDefaultHeader(__("PandoraFMS: Agents"));
105		}
106		$ui->showFooter(false);
107		$ui->beginContent();
108			if (empty($this->agent)) {
109				$ui->contentAddHtml('<span style="color: red;">' . __('No agent found') . '</span>');
110			}
111			else {
112				$ui->contentBeginGrid();
113					if ($this->agent['disabled']) {
114						$agent_name = "<em>" . $agent_name . "</em>" . ui_print_help_tip(__('Disabled'), true);
115					}
116					else if ($this->agent['quiet']) {
117						$agent_name = "<em>" . $agent_name . "&nbsp;" . html_print_image("images/dot_green.disabled.png", true, array("border" => '0', "title" => __('Quiet'), "alt" => "")) . "</em>";
118					}
119					else {
120						$agent_name = $agent_name;
121					}
122
123
124					$addresses = agents_get_addresses($this->id);
125					$address = agents_get_address($this->id);
126					foreach ($addresses as $k => $add) {
127						if ($add == $address) {
128							unset($addresses[$k]);
129						}
130					}
131					$ip = html_print_image('images/world.png', true, array('title' => __('IP address'))) . '&nbsp;&nbsp;';
132					$ip .= empty($address) ? '<em>' . __('N/A') . '</em>' : $address;
133					if (!empty($addresses)) {
134						$ip .= ui_print_help_tip(__('Other IP addresses').': <br>'.implode('<br>',$addresses), true);
135					}
136					$ip .= '<br />';
137
138					$last_contact = '<b>' . __('Last contact') . '</b>:&nbsp;'
139						.ui_print_timestamp ($this->agent["ultimo_contacto"], true) . '<br />';
140
141					$description = '<b>' . __('Description') . ':</b><br>';
142					if (empty($agent["comentarios"])) {
143						$description .= '<i>' . __('N/A') . '</i>';
144					}
145					else {
146						$description .= ui_bbcode_to_html($this->agent["comentarios"]);
147					}
148
149
150					$html = '<div class="agent_details">';
151					$html .= ui_print_group_icon ($this->agent["id_grupo"], true, "groups_small", "", false) . '&nbsp;&nbsp;';
152					$html .= '<span class="agent_name">' . $agent_name . '</span><br />';
153					$html .= $ip;
154					$html .= $last_contact;
155					$html .= $description;
156					$html .= '</div>';
157
158				$ui->contentGridAddCell($html, 'agent_details');
159					ob_start();
160					$html = '<div class="agent_graphs">';
161					$html .= "<b>" . __('Modules by status') . "</b><br />";
162					$html .= graph_agent_status ($this->id, 160, 160, true);
163					$graph_js = ob_get_clean();
164					$html = $graph_js . $html;
165					unset($this->agent['fired_count']);
166					if ($this->agent['total_count'] > 0) {
167						$html .= '<span class="agents_tiny_stats agents_tiny_stats_tactical">' . reporting_tiny_stats($this->agent, true) . ' </span><br>';
168					}
169					$html .= "<b>" . __('Events (24h)') . "</b><br /><br />";
170					$html .= '<div id="events_bar"></div>';
171					$html .= '<br>';
172					$html .= '</div>';
173				$ui->contentGridAddCell($html, 'agent_graphs');
174				$ui->contentEndGrid();
175
176
177				$modules = new Modules();
178				$filters = array('id_agent' => $this->id, 'all_modules' => true, 'status' => -1);
179				$modules->setFilters($filters);
180				$modules->disabledColumns(array('agent'));
181				$ui->contentBeginCollapsible(__('Modules'));
182				$ui->contentCollapsibleAddItem($modules->listModulesHtml(0, true));
183				$ui->contentEndCollapsible();
184
185				$alerts = new Alerts();
186				$filters = array('id_agent' => $this->id, 'all_alerts' => true);
187				$alerts->setFilters($filters);
188				$alerts->disabledColumns(array('agent'));
189				$ui->contentBeginCollapsible(__('Alerts'));
190				$ui->contentCollapsibleAddItem($alerts->listAlertsHtml(true));
191				$ui->contentEndCollapsible();
192
193				$events = new Events();
194				$events->addJavascriptDialog();
195
196				$options = $events->get_event_dialog_options();
197				$ui->addDialog($options);
198
199				$options = $events->get_event_dialog_error_options($options);
200				$ui->addDialog($options);
201
202				$ui->contentAddHtml("<a id='detail_event_dialog_hook' href='#detail_event_dialog' style='display:none;'>detail_event_hook</a>");
203				$ui->contentAddHtml("<a id='detail_event_dialog_error_hook' href='#detail_event_dialog_error' style='display:none;'>detail_event_dialog_error_hook</a>");
204
205				$ui->contentBeginCollapsible(sprintf(__('Last %s Events'), $system->getPageSize()));
206				$tabledata = $events->listEventsHtml(0, true, 'last_agent_events');
207				$ui->contentCollapsibleAddItem($tabledata['table']);
208				$ui->contentCollapsibleAddItem($events->putEventsTableJS($this->id));
209				$ui->contentEndCollapsible();
210			}
211
212		$ui->contentAddLinkListener('last_agent_events');
213		$ui->contentAddLinkListener('list_events');
214		$ui->contentAddLinkListener('list_agent_Modules');
215
216		$ui->contentAddHtml("<script type=\"text/javascript\">
217			$(document).ready(function() {
218				function set_same_heigth() {
219					//Set same height to boxes
220					var max_height = 0;
221					if ($('.agent_details').height() > $('.agent_graphs').height()) {
222						max_height = $('.agent_details').height();
223						$('.agent_graphs').height(max_height);
224					}
225					else {
226						max_height = $('.agent_graphs').height();
227						$('.agent_details').height(max_height);
228					}
229				}
230
231				if ($('.ui-block-a').css('float') != 'none') {
232					set_same_heigth();
233				}
234
235				$('.ui-collapsible').bind('expand', function () {
236					refresh_link_listener_last_agent_events();
237					refresh_link_listener_list_agent_Modules();
238				});
239
240				function ajax_load_events_bar() {
241					$('#events_bar').html('<div style=\"text-align: center\"> " . __('Loading...') . "<br /><img src=\"images/ajax-loader.gif\" /></div>');
242
243					var bar_width = $('.agent_graphs').width() * 0.9;
244
245					postvars = {};
246					postvars[\"action\"] = \"ajax\";
247					postvars[\"parameter1\"] = \"agent\";
248					postvars[\"parameter2\"] = \"render_events_bar\";
249					postvars[\"agent_id\"] = \"" . $this->id . "\";
250					postvars[\"width\"] = bar_width;
251					$.post(\"index.php\",
252						postvars,
253						function (data) {
254							$('#events_bar').html(data);
255							if ($('.ui-block-a').css('float') != 'none') {
256								set_same_heigth();
257							}
258						},
259						\"html\");
260				}
261
262				ajax_load_events_bar();
263
264				// Detect orientation change to refresh dinamic content
265				$(window).on({
266					orientationchange: function(e) {
267						// Refresh events bar
268						ajax_load_events_bar();
269
270						// Keep same height on boxes
271						if ($('.ui-block-a').css('float') == 'none') {
272							$('.agent_graphs').height('auto');
273							$('.agent_details').height('auto');
274						}
275						else {
276							set_same_heigth();
277						}
278
279					}
280				});
281
282				if ($('.ui-block-a').css('float') != 'none') {
283					set_same_heigth();
284				}
285			});
286			</script>");
287
288		$ui->endContent();
289		$ui->showPage();
290	}
291}
292