pandorafms/pandora_console/include/class/Tree.class.php

373 lines
9.4 KiB
PHP
Raw Normal View History

2014-12-16 16:39:00 +01:00
<?php
//Pandora FMS- http://pandorafms.com
// ==================================================
// Copyright (c) 2005-2011 Artica Soluciones Tecnologicas
// Please see http://pandorafms.org for full contribution list
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; version 2
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
class Tree {
private $type = null;
private $tree = array();
private $filter = array();
private $root = null;
private $childrenMethod = "on_demand";
private $countModuleStatusMethod = "on_demand";
private $countAgentStatusMethod = "on_demand";
2014-12-16 16:39:00 +01:00
public function __construct($type, $root = null,
$childrenMethod = "on_demand",
$countModuleStatusMethod = "on_demand",
$countAgentStatusMethod = "on_demand") {
2014-12-16 16:39:00 +01:00
$this->type = $type;
$this->root = $root;
2014-12-17 18:58:34 +01:00
$this->childrenMethod = $childrenMethod;
$this->countModuleStatusMethod = $countModuleStatusMethod;
$this->countAgentStatusMethod = $countAgentStatusMethod;
2014-12-16 16:39:00 +01:00
}
2014-12-17 18:58:34 +01:00
public function setType($type) {
2014-12-16 16:39:00 +01:00
$this->type = $type;
}
2014-12-17 18:58:34 +01:00
public function setFilter($filter) {
2014-12-16 16:39:00 +01:00
$this->filter = $filter;
}
2014-12-17 18:58:34 +01:00
public function getData() {
2014-12-16 16:39:00 +01:00
switch ($this->type) {
case 'os':
2014-12-17 18:58:34 +01:00
$this->getDataOS();
2014-12-16 16:39:00 +01:00
break;
case 'group':
2014-12-17 18:58:34 +01:00
$this->getDataGroup();
2014-12-16 16:39:00 +01:00
break;
case 'module_group':
2014-12-17 18:58:34 +01:00
$this->getDataModuleGroup();
2014-12-16 16:39:00 +01:00
break;
2014-12-19 18:13:47 +01:00
case 'agent':
$this->getDataModules();
2014-12-16 16:39:00 +01:00
break;
case 'tag':
2014-12-17 18:58:34 +01:00
$this->getDataTag();
2014-12-16 16:39:00 +01:00
break;
}
}
2014-12-17 18:58:34 +01:00
public function getDataOS() {
2014-12-19 18:39:18 +01:00
$list_os = os_get_os();
html_debug_print($list_os);
2014-12-16 16:39:00 +01:00
}
2014-12-17 18:58:34 +01:00
private function getRecursiveGroup($parent, $limit = null) {
2014-12-16 16:39:00 +01:00
$filter = array();
2014-12-18 18:49:57 +01:00
2014-12-17 18:58:34 +01:00
$filter['parent'] = $parent;
2014-12-16 16:39:00 +01:00
if (!empty($this->filter['search'])) {
$filter['nombre'] = "%" . $this->filter['search'] . "%";
}
// First filter by name and father
$groups = db_get_all_rows_filter('tgrupo',
$filter,
2014-12-18 18:49:57 +01:00
array('id_grupo', 'nombre'));
2014-12-16 16:39:00 +01:00
if (empty($groups))
$groups = array();
2014-12-18 18:49:57 +01:00
2014-12-16 16:39:00 +01:00
// Filter by status
2014-12-18 17:10:30 +01:00
$filter_status = AGENT_STATUS_ALL;
2014-12-16 16:39:00 +01:00
if (!empty($this->filter['status'])) {
2014-12-18 17:10:30 +01:00
$filter_status = $this->filter['status'];
2014-12-16 16:39:00 +01:00
}
2014-12-18 18:49:57 +01:00
2014-12-18 17:10:30 +01:00
foreach ($groups as $iterator => $group) {
$data = reporting_get_group_stats($group['id_grupo']);
$groups[$iterator]['icon'] = groups_get_icon($group['id_grupo']) . '.png';
2014-12-18 17:10:30 +01:00
$groups[$iterator]['counters'] = array();
$groups[$iterator]['counters']['unknown'] = $data['agents_unknown'];
$groups[$iterator]['counters']['critical'] = $data['agent_critical'];
$groups[$iterator]['counters']['warning'] = $data['agent_warning'];
$groups[$iterator]['counters']['not_init'] = $data['agent_not_init'];
$groups[$iterator]['counters']['ok'] = $data['agent_ok'];
$groups[$iterator]['counters']['total'] = $data['total_agents'];
$groups[$iterator]['status'] = $data['status'];
2014-12-18 17:10:30 +01:00
if ($filter_status != AGENT_STATUS_ALL) {
2014-12-16 16:39:00 +01:00
$remove_group = true;
2014-12-18 17:10:30 +01:00
switch ($filter_status) {
2014-12-16 16:39:00 +01:00
case AGENT_STATUS_NORMAL:
if ($groups[$iterator]['status'] === "ok")
2014-12-16 16:39:00 +01:00
$remove_group = false;
break;
case AGENT_STATUS_WARNING:
if ($groups[$iterator]['status'] === "warning")
2014-12-16 16:39:00 +01:00
$remove_group = false;
break;
case AGENT_STATUS_CRITICAL:
if ($groups[$iterator]['status'] === "critical")
2014-12-16 16:39:00 +01:00
$remove_group = false;
break;
case AGENT_STATUS_UNKNOWN:
if ($groups[$iterator]['status'] === "unknown")
2014-12-16 16:39:00 +01:00
$remove_group = false;
break;
case AGENT_STATUS_NOT_INIT:
if ($groups[$iterator]['status'] === "not_init")
2014-12-16 16:39:00 +01:00
$remove_group = false;
break;
}
2014-12-18 17:51:02 +01:00
if ($remove_group) {
2014-12-16 16:39:00 +01:00
unset($groups[$iterator]);
continue;
}
2014-12-17 18:58:34 +01:00
}
2014-12-18 17:10:30 +01:00
if (is_null($limit)) {
$groups[$iterator]['children'] =
$this->getRecursiveGroup($group['id_grupo']);
}
else if ($limit >= 1) {
$groups[$iterator]['children'] =
$this->getRecursiveGroup(
$group['id_grupo'],
($limit - 1));
2014-12-16 16:39:00 +01:00
}
$groups[$iterator]['type'] = 'group';
$groups[$iterator]['name'] = $groups[$iterator]['nombre'];
$groups[$iterator]['id'] = $groups[$iterator]['id_grupo'];
2014-12-16 16:39:00 +01:00
}
2014-12-18 17:51:02 +01:00
if ($parent == 0) {
$agents = array();
}
else {
$agents = $this->getDataAgents('group', $parent);
}
$data = array_merge($groups, $agents);
return $data;
2014-12-17 18:58:34 +01:00
}
public function getDataGroup() {
if (!empty($this->root)) {
$parent = $this->root;
}
else {
$parent = 0;
}
2014-12-18 17:51:02 +01:00
$data = $this->getRecursiveGroup($parent, 1);
// Make the data
$this->tree = array();
foreach ($data as $item) {
$temp = array();
$temp['id'] = $item['id'];
$temp['type'] = $item['type'];
$temp['name'] = $item['name'];
$temp['icon'] = $item['icon'];
$temp['status'] = $item['status'];
switch ($this->countAgentStatusMethod) {
case 'on_demand':
$temp['searchCounters'] = 1;
break;
case 'live':
$temp['searchCounters'] = 0;
$temp['counters'] = $item['counters'];
break;
}
switch ($this->childrenMethod) {
case 'on_demand':
2014-12-18 17:51:02 +01:00
if (!empty($item['children'])) {
$temp['searchChildren'] = 1;
2014-12-17 18:58:34 +01:00
// I hate myself
// No add children
2014-12-17 18:58:34 +01:00
}
else {
$temp['searchChildren'] = 0;
2014-12-17 18:58:34 +01:00
// I hate myself
// No add children
2014-12-17 18:58:34 +01:00
}
2014-12-18 18:49:57 +01:00
break;
case 'live':
$temp['searchChildren'] = 0;
$temp['children'] = $item['children'];
break;
}
2014-12-16 16:39:00 +01:00
2014-12-18 17:51:02 +01:00
$this->tree[] = $temp;
2014-12-16 16:39:00 +01:00
}
}
2014-12-19 18:13:47 +01:00
public function getDataModules() {
$modules =
agents_get_modules($this->root, array('nombre', 'id_tipo_modulo'));
if (empty($modules))
$modules = array();
$this->tree = array();
foreach ($modules as $id => $module) {
$temp = array();
$temp['type'] = 'module';
$temp['id'] = $id;
$temp['name'] = $module['nombre'];
$temp['icon'] = modules_get_type_icon(
$module['id_tipo_modulo']);
$temp['value'] = modules_get_last_value($id);
switch (modules_get_status($id)) {
case AGENT_MODULE_STATUS_CRITICAL_BAD:
case AGENT_MODULE_STATUS_CRITICAL_ALERT:
$temp['status'] = "critical";
break;
default:
case AGENT_MODULE_STATUS_NORMAL:
case AGENT_MODULE_STATUS_NORMAL_ALERT:
$temp['status'] = "ok";
break;
case AGENT_MODULE_STATUS_WARNING:
case AGENT_MODULE_STATUS_WARNING_ALERT:
$temp['status'] = "warning";
break;
case AGENT_MODULE_STATUS_UNKNOWN:
$temp['status'] = "unknown";
break;
case AGENT_MODULE_STATUS_NO_DATA:
case AGENT_MODULE_STATUS_NOT_INIT:
$temp['status'] = "not_init";
break;
}
$temp['children'] = array();
$this->tree[] = $temp;
}
}
2014-12-18 17:51:02 +01:00
public function getDataAgents($type, $id) {
switch ($type) {
case 'group':
$filter = array(
'id_grupo' => $id,
'status' => $this->filter['status'],
'nombre' => "%" . $this->filter['search'] . "%"
);
2014-12-19 13:16:06 +01:00
$agents = agents_get_agents($filter,
array('id_agente', 'nombre', 'id_os'));
2014-12-18 17:51:02 +01:00
if (empty($agents)) {
$agents = array();
}
break;
}
foreach ($agents as $iterator => $agent) {
$agents[$iterator]['type'] = 'agent';
$agents[$iterator]['id'] = $agents[$iterator]['id_agente'];
$agents[$iterator]['name'] = $agents[$iterator]['nombre'];
2014-12-19 13:16:06 +01:00
$agents[$iterator]['icon'] =
ui_print_os_icon(
$agents[$iterator]["id_os"], false, true, true,
false, true, true);
$agents[$iterator]['counters'] = array();
$agents[$iterator]['counters']['unknown'] =
agents_monitor_unknown($agents[$iterator]['id']);
$agents[$iterator]['counters']['critical'] =
agents_monitor_critical($agents[$iterator]['id']);
$agents[$iterator]['counters']['warning'] =
agents_monitor_warning($agents[$iterator]['id']);
$agents[$iterator]['counters']['not_init'] =
agents_monitor_notinit($agents[$iterator]['id']);
$agents[$iterator]['counters']['ok'] =
agents_monitor_ok($agents[$iterator]['id']);
$agents[$iterator]['counters']['total'] =
agents_monitor_total($agents[$iterator]['id']);
$agents[$iterator]['counters']['alerts'] =
agents_get_alerts_fired($agents[$iterator]['id']);
2014-12-19 13:16:06 +01:00
switch (agents_get_status($agents[$iterator]['id'])) {
case AGENT_STATUS_NORMAL:
$agents[$iterator]['status'] = "ok";
break;
case AGENT_STATUS_WARNING:
$agents[$iterator]['status'] = "warning";
break;
case AGENT_STATUS_CRITICAL:
$agents[$iterator]['status'] = "critical";
break;
case AGENT_STATUS_UNKNOWN:
$agents[$iterator]['status'] = "unknown";
break;
case AGENT_STATUS_NOT_INIT:
$agents[$iterator]['status'] = "not_init";
break;
default:
$agents[$iterator]['status'] = "none";
break;
}
2014-12-19 18:13:47 +01:00
$agents[$iterator]['children'] = array();
if ($agents[$iterator]['counters']['total'] > 0) {
switch ($this->childrenMethod) {
case 'on_demand':
$agents[$iterator]['children'] = 1;
break;
case 'live':
$modules =
agents_get_modules($agents[$iterator]['id_agente']);
// TO DO
break;
}
}
2014-12-18 17:51:02 +01:00
}
return $agents;
}
2014-12-17 18:58:34 +01:00
public function getDataModuleGroup() {
2014-12-16 16:39:00 +01:00
}
2014-12-17 18:58:34 +01:00
public function getDataTag() {
2014-12-16 16:39:00 +01:00
}
2014-12-17 18:58:34 +01:00
public function getJSON() {
$this->getData();
2014-12-16 16:39:00 +01:00
return json_encode($this->tree);
}
2014-12-17 18:58:34 +01:00
public function getArray() {
$this->getData();
return $this->tree;
}
2014-12-16 16:39:00 +01:00
}
?>