2019-02-22 13:07:47 +01:00
< ? php
2019-02-27 10:28:36 +01:00
// Pandora FMS- http://pandorafms.com
2019-02-22 13:07:47 +01:00
// ==================================================
// Copyright (c) 2005-2018 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.
global $config ;
2019-02-27 10:28:36 +01:00
require_once $config [ 'homedir' ] . '/include/class/Tree.class.php' ;
class TreeService extends Tree
{
protected $propagateCounters = true ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
protected $displayAllGroups = false ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
public function __construct ( $type , $rootType = '' , $id =- 1 , $rootID =- 1 , $serverID = false , $childrenMethod = 'on_demand' , $access = 'AR' )
{
global $config ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
parent :: __construct ( $type , $rootType , $id , $rootID , $serverID , $childrenMethod , $access );
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$this -> L1fieldName = 'id_group' ;
$this -> L1extraFields = [
'ts.name AS `name`' ,
'ts.id AS `sid`' ,
];
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$this -> filter [ 'statusAgent' ] = AGENT_STATUS_ALL ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$this -> avoid_condition = true ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$this -> L2inner = ' LEFT JOIN tservice_element tse
2019-02-22 13:07:47 +01:00
ON tse . id_agent = ta . id_agente ' ;
2019-02-27 10:28:36 +01:00
$this -> L2condition = 'AND tse.id_service=' . $this -> id ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
public function setPropagateCounters ( $value )
{
$this -> propagateCounters = ( bool ) $value ;
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
public function setDisplayAllGroups ( $value )
{
$this -> displayAllGroups = ( bool ) $value ;
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
protected function getData ()
{
if ( $this -> id == - 1 ) {
$this -> getFirstLevel ();
} else if ( $this -> type == 'services' ) {
$this -> getSecondLevel ();
} else if ( $this -> type == 'agent' ) {
$this -> getThirdLevel ();
}
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
protected function getFirstLevel ()
{
2019-06-17 13:38:05 +02:00
global $config ;
2019-02-27 10:28:36 +01:00
$processed_items = $this -> getProcessedServices ();
$ids = array_keys ( $processed_items );
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$filter = [ 'id' => $ids ];
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$own_info = get_user_info ( $config [ 'id_user' ]);
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
if ( $own_info [ 'is_admin' ] || check_acl ( $config [ 'id_user' ], 0 , 'PM' )) {
$display_all_services = true ;
} else {
$display_all_services = false ;
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$this -> tree = [];
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$services = services_get_services ( $filter , false , $display_all_services );
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
foreach ( $services as $row ) {
$status = services_get_status ( $row , true );
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
switch ( $status ) {
case SERVICE_STATUS_NORMAL :
$processed_items [ $row [ 'id' ]][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_ok_ball.png" data-title="NORMAL status." data-use_title_for_force_title="1" class="forced_title" alt="NORMAL status." />' ;
break ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
case SERVICE_STATUS_CRITICAL :
$processed_items [ $row [ 'id' ]][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_critical_ball.png" data-title="CRITICAL status." data-use_title_for_force_title="1" class="forced_title" alt="CRITICAL status." />' ;
break ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
case SERVICE_STATUS_WARNING :
2019-08-01 10:36:15 +02:00
$processed_items [ $row [ 'id' ]][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_warning_ball.png" data-title="WARNING status." data-use_title_for_force_title="1" class="forced_title" alt="WARNING status." />' ;
2019-02-27 10:28:36 +01:00
break ;
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
case SERVICE_STATUS_UNKNOWN :
default :
$processed_items [ $row [ 'id' ]][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_no_data_ball.png" data-title="UNKNOWN status." data-use_title_for_force_title="1" class="forced_title" alt="UNKNOWN status." />' ;
break ;
}
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$this -> tree = $processed_items ;
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
protected function getProcessedServices ()
{
$fields = $this -> getFirstLevelFields ();
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
if ( users_can_manage_group_all ( 'AR' )) {
$groups_acl = '' ;
} else {
$groups_acl = 'AND ts.id_group IN (' . implode ( ',' , $this -> userGroupsArray ) . ')' ;
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
$sql = sprintf (
" SELECT t1.*
2019-02-22 13:07:47 +01:00
FROM tservice_element tss
RIGHT JOIN
( SELECT ts . id , ts . id_agent_module , ts . name , ts . name AS `alias` , ts . id AS `rootID` ,
'services' AS rootType , 'services' AS type ,
0 AS quiet ,
SUM ( if (( tse . id_agent <> 0 ), 1 , 0 )) AS `total_agents` ,
SUM ( if (( tse . id_agente_modulo <> 0 ), 1 , 0 )) AS `total_modules` ,
SUM ( if (( tse . id_service_child <> 0 ), 1 , 0 )) AS `total_services`
FROM tservice ts
LEFT JOIN tservice_element tse
ON ts . id = tse . id_service
GROUP BY id
) as t1
ON tss . id_service_child = t1 . id
WHERE tss . id_service_child IS NULL
% s
" ,
2019-02-27 10:28:36 +01:00
$groups_acl
);
$stats = db_get_all_rows_sql ( $sql );
$services = [];
foreach ( $stats as $service ) {
$services [ $service [ 'id' ]] = $this -> getProcessedItem ( $services [ $service [ 'id' ]]);
if (( $service [ 'total_services' ] + $service [ 'total_agents' ] + $service [ 'total_modules' ]) > 0 ) {
$services [ $service [ 'id' ]][ 'searchChildren' ] = 1 ;
} else {
$services [ $service [ 'id' ]][ 'searchChildren' ] = 0 ;
}
$services [ $service [ 'id' ]][ 'counters' ] = [
'total_services' => $service [ 'total_services' ],
'total_agents' => $service [ 'total_agents' ],
'total_modules' => $service [ 'total_modules' ],
];
$services [ $service [ 'id' ]][ 'name' ] = $service [ 'name' ];
$services [ $service [ 'id' ]][ 'id' ] = $service [ 'id' ];
$services [ $service [ 'id' ]][ 'serviceDetail' ] = 'index.php?sec=network&sec2=enterprise/operation/services/services&tab=service_map&id_service=' . ( int ) $service [ 'id' ];
}
return $services ;
}
protected function getFirstLevelFields ()
{
$fields = [];
return implode ( ',' , array_merge ( $fields , $this -> L1extraFields ));
}
protected function getSecondLevel ()
{
$data = [];
$data_agents = [];
$data_modules = [];
$data_services = [];
$sql = $this -> getSecondLevelSql ();
$data_agents = db_process_sql ( $sql );
if ( empty ( $data_agents )) {
$data_agents = [];
}
$this -> processAgents ( $data_agents );
foreach ( $data_agents as $key => $agent ) {
$data_agents [ $key ][ 'showEventsBtn' ] = 1 ;
$data_agents [ $key ][ 'eventAgent' ] = $agent [ 'id' ];
}
$sql = $this -> getSecondLevelModulesSql ();
$data_modules = db_process_sql ( $sql );
if ( empty ( $data_modules )) {
$data_modules = [];
} else {
foreach ( $data_modules as $key => $module ) {
switch ( $module [ 'estado' ]) {
case '0' :
2019-08-06 14:56:57 +02:00
$module_status = 'ok' ;
$module_title = 'NORMAL' ;
2019-02-27 10:28:36 +01:00
break ;
case '1' :
2019-08-06 14:56:57 +02:00
$module_status = 'critical' ;
$module_title = 'CRITICAL' ;
2019-02-27 10:28:36 +01:00
break ;
case '2' :
2019-08-06 14:56:57 +02:00
$module_status = 'warning' ;
$module_title = 'WARNING' ;
break ;
case '3' :
$module_status = 'down' ;
$module_title = 'UNKNOWN' ;
2019-02-27 10:28:36 +01:00
break ;
case '4' :
2019-08-06 14:56:57 +02:00
$module_status = 'no_data' ;
$module_title = 'NOT INITIALIZED' ;
2019-02-27 10:28:36 +01:00
break ;
default :
2019-08-06 14:56:57 +02:00
$module_status = 'down' ;
$module_title = 'UNKNOWN' ;
2019-02-27 10:28:36 +01:00
break ;
}
2019-08-06 14:56:57 +02:00
$data_modules [ $key ][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_' . $module_status . '_ball.png" data-title="' . $module_title . ' status." data-use_title_for_force_title="1" class="forced_title" alt="' . $module_title . ' status." />' ;
2019-02-27 10:28:36 +01:00
$data_modules [ $key ][ 'showEventsBtn' ] = 1 ;
$data_modules [ $key ][ 'eventModule' ] = $module [ 'id_agente_modulo' ];
}
}
$sql = $this -> getSecondLevelServicesSql ();
$data_services = db_process_sql ( $sql );
$service_stats = [];
foreach ( $data_services as $service ) {
$service_stats [ $service [ 'id' ]][ 'id' ] = ( int ) $service [ 'id' ];
$service_stats [ $service [ 'id' ]][ 'name' ] = $service [ 'name' ];
$service_stats [ $service [ 'id' ]][ 'alias' ] = $service [ 'name' ];
if (( $service [ 'total_services' ] + $service [ 'total_agents' ] + $service [ 'total_modules' ]) > 0 ) {
$service_stats [ $service [ 'id' ]][ 'searchChildren' ] = 1 ;
} else {
$services [ $service [ 'id' ]][ 'searchChildren' ] = 0 ;
}
$service_stats [ $service [ 'id' ]][ 'rootID' ] = $service [ 'rootID' ];
$service_stats [ $service [ 'id' ]][ 'rootType' ] = $service [ 'rootType' ];
$service_stats [ $service [ 'id' ]][ 'type' ] = 'services' ;
$service_stats [ $service [ 'id' ]][ 'children' ] = [];
$service_stats [ $service [ 'id' ]][ 'serviceDetail' ] = 'index.php?sec=network&sec2=enterprise/operation/services/services&tab=service_map&id_service=' . ( int ) $service [ 'id' ];
$service_stats [ $service [ 'id' ]][ 'counters' ] = [
'total_services' => $service [ 'total_services' ],
'total_agents' => $service [ 'total_agents' ],
'total_modules' => $service [ 'total_modules' ],
];
}
$own_info = get_user_info ( $config [ 'id_user' ]);
if ( $own_info [ 'is_admin' ] || check_acl ( $config [ 'id_user' ], 0 , 'PM' )) {
$display_all_services = true ;
} else {
$display_all_services = false ;
}
$services = services_get_services ( $filter , false , $display_all_services );
foreach ( $services as $row ) {
if ( ! array_key_exists ( $row [ 'id' ], $service_stats )) {
continue ;
}
$status = services_get_status ( $row , true );
switch ( $status ) {
case SERVICE_STATUS_NORMAL :
$service_stats [ $row [ 'id' ]][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_ok_ball.png" data-title="NORMAL status." data-use_title_for_force_title="1" class="forced_title" alt="NORMAL status." />' ;
break ;
case SERVICE_STATUS_CRITICAL :
$service_stats [ $row [ 'id' ]][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_critical_ball.png" data-title="CRITICAL status." data-use_title_for_force_title="1" class="forced_title" alt="CRITICAL status." />' ;
break ;
case SERVICE_STATUS_WARNING :
$service_stats [ $row [ 'id' ]][ $key ][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_warning_ball.png" data-title="WARNING status." data-use_title_for_force_title="1" class="forced_title" alt="WARNING status." />' ;
break ;
case SERVICE_STATUS_UNKNOWN :
default :
$service_stats [ $row [ 'id' ]][ 'statusImageHTML' ] = '<img src="images/status_sets/default/agent_no_data_ball.png" data-title="UNKNOWN status." data-use_title_for_force_title="1" class="forced_title" alt="UNKNOWN status." />' ;
break ;
}
}
$data_services = array_values ( $service_stats );
$data = array_merge ( $data_services , $data_agents , $data_modules );
if ( empty ( $data )) {
$this -> tree = [];
return ;
}
$this -> tree = $data ;
}
protected function getSecondLevelServicesSql ()
{
$group_acl = $this -> getGroupAclCondition ();
$sql = " SELECT ts.id, ts.name, tse1.id_service AS `rootID`, 'services' AS rootType, 'services' AS type, 0 AS quiet, SUM(if((tse2.id_agent<>0), 1, 0)) AS `total_agents`, SUM(if((tse2.id_agente_modulo<>0), 1, 0)) AS `total_modules`, SUM(if((tse2.id_service_child<>0), 1, 0)) AS `total_services`, 0 AS fired_count, 0 AS normal_count, 0 AS warning_count, 0 AS critical_count, 0 AS unknown_count, 0 AS notinit_count, 0 AS state_critical, 0 AS state_warning, 0 AS state_unknown, 0 AS state_notinit, 0 AS state_normal, 0 AS state_total, '' AS statusImageHTML, '' AS alertImageHTML
2019-02-22 13:07:47 +01:00
FROM tservice_element tse1
LEFT JOIN tservice_element tse2 ON tse1 . id_service_child = tse2 . id_service
LEFT JOIN tservice ts ON tse1 . id_service_child = ts . id
WHERE tse1 . id_service = $this -> id AND tse1 . id_service_child <> 0
GROUP BY tse1 . id_service_child
" ;
2019-02-27 10:28:36 +01:00
return $sql ;
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
protected function getSecondLevelModulesSql ()
{
$sql = " SELECT tse.id_agente_modulo, nombre AS `name`, nombre AS `alias`, tse.id_service AS `rootID`, 'services' AS `rootType`, 'modules' AS `type`, estado
2019-02-22 13:07:47 +01:00
FROM tservice_element tse
INNER JOIN tagente_modulo tam ON tse . id_agente_modulo = tam . id_agente_modulo
INNER JOIN tagente_estado tae ON tam . id_agente_modulo = tae . id_agente_estado
WHERE tse . id_service = $this -> id AND tse . id_agente_modulo <> 0
" ;
2019-02-27 10:28:36 +01:00
return $sql ;
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
protected function getAgentStatusFilter ( $status = self :: TV_DEFAULT_AGENT_STATUS )
{
return '' ;
}
2019-02-22 13:07:47 +01:00
2019-02-27 10:28:36 +01:00
}