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' ;
2020-07-01 20:50:07 +02:00
use PandoraFMS\Enterprise\Service ;
2019-02-27 10:28:36 +01:00
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
2020-07-01 20:50:07 +02:00
private $metaID = 0 ;
2019-02-22 13:07:47 +01:00
2020-07-01 20:50:07 +02:00
public function __construct (
$type ,
$rootType = '' ,
$id =- 1 ,
$rootID =- 1 ,
$serverID = false ,
$childrenMethod = 'on_demand' ,
$access = 'AR' ,
$id_server_meta = 0
) {
2019-02-27 10:28:36 +01:00
global $config ;
2019-02-22 13:07:47 +01:00
2020-07-01 20:50:07 +02:00
if ( $id_server_meta > 0 ) {
$this -> metaID = $id_server_meta ;
}
parent :: __construct (
$type ,
$rootType ,
$id ,
$rootID ,
$serverID ,
$childrenMethod ,
$access ,
$id_server_meta
);
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 ()
{
2020-07-01 20:50:07 +02:00
if ( is_metaconsole () === true && $this -> metaID > 0 ) {
// Impersonate node.
\enterprise_include_once ( 'include/functions_metaconsole.php' );
\enterprise_hook (
'metaconsole_connect' ,
[
null ,
$this -> metaID ,
]
);
}
2019-02-27 10:28:36 +01:00
if ( $this -> id == - 1 ) {
$this -> getFirstLevel ();
} else if ( $this -> type == 'services' ) {
$this -> getSecondLevel ();
} else if ( $this -> type == 'agent' ) {
$this -> getThirdLevel ();
}
2020-07-01 20:50:07 +02:00
if ( is_metaconsole () === true && $this -> metaID > 0 ) {
// Restore connection.
\enterprise_hook ( 'metaconsole_restore_db' );
}
2019-02-27 10:28:36 +01:00
}
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 ()
{
2020-03-11 15:00:35 +01:00
$is_favourite = $this -> getServiceFavouriteFilter ();
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 (
2020-07-01 20:50:07 +02:00
' SELECT
ts . id ,
ts . id_agent_module ,
ts . name ,
ts . name as `alias` ,
ts . id as `rootID` ,
" services " as `rootType` ,
" services " as `type` ,
ts . 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 tse . id_service = ts . id
WHERE ts . id NOT IN (
SELECT DISTINCT id_service_child
FROM tservice_element
WHERE id_server_meta = 0
)
% s
% s
GROUP BY ts . id ' ,
$is_favourite ,
$groups_acl
2019-02-27 10:28:36 +01:00
);
$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 );
2020-07-01 20:50:07 +02:00
$data_services = array_reduce (
$data_services ,
function ( $carry , $item ) {
if ( $item [ 'id_server_meta' ] > 0
&& is_metaconsole () === true
) {
// Impersonate node.
\enterprise_include_once ( 'include/functions_metaconsole.php' );
$r = \enterprise_hook (
'metaconsole_connect' ,
[
null ,
$item [ 'id_server_meta' ],
]
);
if ( $r === NOERR ) {
$item = db_get_row_sql (
sprintf (
' SELECT
ts . id ,
ts . id_agent_module ,
ts . name ,
ts . name as `alias` ,
% d as `rootID` ,
" services " as `rootType` ,
" services " as `type` ,
ts . quiet ,
% d as id_server_meta ,
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 tse . id_service = ts . id
WHERE ts . id = % d
GROUP BY ts . id ' ,
$item [ 'id_server_meta' ],
$item [ 'rootID' ],
$item [ 'id' ]
)
);
$item [ 'obj' ] = new Service ( $item [ 'id' ]);
}
// Restore connection.
\enterprise_hook ( 'metaconsole_restore_db' );
} else {
$item [ 'obj' ] = new Service ( $item [ 'id' ]);
}
$carry [] = $item ;
return $carry ;
},
[]
);
2019-02-27 10:28:36 +01:00
$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' ];
2020-07-01 20:50:07 +02:00
if ( $this -> metaID > 0 ) {
$service_stats [ $service [ 'id' ]][ 'metaID' ] = $this -> metaID ;
} else {
$service_stats [ $service [ 'id' ]][ 'metaID' ] = $service [ 'id_server_meta' ];
}
2019-02-27 10:28:36 +01:00
$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' ],
];
2020-07-01 20:50:07 +02:00
switch ( $service [ 'obj' ] -> status ()) {
2019-02-27 10:28:36 +01:00
case SERVICE_STATUS_NORMAL :
2020-07-01 20:50:07 +02:00
$service_stats [ $service [ '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." />' ;
2019-02-27 10:28:36 +01:00
break ;
case SERVICE_STATUS_CRITICAL :
2020-07-01 20:50:07 +02:00
$service_stats [ $service [ '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." />' ;
2019-02-27 10:28:36 +01:00
break ;
case SERVICE_STATUS_WARNING :
2020-07-01 20:50:07 +02:00
$service_stats [ $service [ '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 ;
case SERVICE_STATUS_UNKNOWN :
default :
2020-07-01 20:50:07 +02:00
$service_stats [ $service [ '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." />' ;
2019-02-27 10:28:36 +01:00
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 ();
2020-07-01 20:50:07 +02:00
$sql = sprintf (
' SELECT
ts . id ,
ts . id_agent_module ,
ts . name ,
ts . name as `alias` ,
tse . id_service as `rootID` ,
" services " as `rootType` ,
" services " as `type` ,
ts . quiet ,
tse . id_server_meta ,
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
INNER JOIN tservice_element tse
ON tse . id_service_child = ts . id
WHERE
tse . id_service = % d
% s
GROUP BY ts . id ' ,
$this -> id ,
$group_acl
);
2019-02-22 13:07:47 +01:00
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
2020-03-11 15:00:35 +01:00
protected function getServiceFavouriteFilter ()
{
if ( isset ( $this -> filter [ 'is_favourite' ]) && ! empty ( $this -> filter [ 'is_favourite' ])) {
return ' AND is_favourite = 1' ;
}
return '' ;
}
2019-02-27 10:28:36 +01:00
}