';
$table_mbs->rowclass[] = '';
$table_mbs->data[] = $tdata;
}
if (!is_metaconsole()) {
$output = '
';
} else {
$table_mbs->class = 'tactical_view';
$table_mbs->style = [];
$output = '
';
}
return $output;
}
function reporting_get_stats_agents_monitors($data)
{
global $config;
// Link URLS
$mobile = false;
if (isset($data['mobile'])) {
if ($data['mobile']) {
$mobile = true;
}
}
if ($mobile) {
$urls = [];
$urls['total_agents'] = 'index.php?page=agents';
$urls['monitor_checks'] = 'index.php?page=modules';
} else {
$urls = [];
$urls['total_agents'] = $config['homeurl'].'index.php?sec=estado&sec2=operation/agentes/estado_agente&refr=60';
$urls['monitor_checks'] = $config['homeurl'].'index.php?sec=view&sec2=operation/agentes/status_monitor&refr=60&status=-1';
}
// Agents and modules table
$table_am = html_get_predefined_table();
$tdata = [];
$tdata[0] = html_print_image('images/agent.png', true, ['title' => __('Total agents')], false, false, false, true);
$tdata[1] = $data['total_agents'] <= 0 ? '-' : $data['total_agents'];
$tdata[1] = ''.$tdata[1].'';
/*
Hello there! :)
We added some of what seems to be "buggy" messages to the openSource version recently. This is not to force open-source users to move to the enterprise version, this is just to inform people using Pandora FMS open source that it requires skilled people to maintain and keep it running smoothly without professional support. This does not imply open-source version is limited in any way. If you check the recently added code, it contains only warnings and messages, no limitations except one: we removed the option to add custom logo in header. In the Update Manager section, it warns about the 'danger’ of applying automated updates without a proper backup, remembering in the process that the Enterprise version comes with a human-tested package. Maintaining an OpenSource version with more than 500 agents is not so easy, that's why someone using a Pandora with 8000 agents should consider asking for support. It's not a joke, we know of many setups with a huge number of agents, and we hate to hear that “its becoming unstable and slow” :(
You can of course remove the warnings, that's why we include the source and do not use any kind of trick. And that's why we added here this comment, to let you know this does not reflect any change in our opensource mentality of does the last 14 years.
*/
if ($data['total_agents'] > 500 && !enterprise_installed()) {
$tdata[2] = "";
}
$tdata[3] = html_print_image('images/module.png', true, ['title' => __('Monitor checks')], false, false, false, true);
$tdata[4] = $data['monitor_checks'] <= 0 ? '-' : $data['monitor_checks'];
$tdata[4] = ''.$tdata[4].'';
/*
Hello there! :)
We added some of what seems to be "buggy" messages to the openSource version recently. This is not to force open-source users to move to the enterprise version, this is just to inform people using Pandora FMS open source that it requires skilled people to maintain and keep it running smoothly without professional support. This does not imply open-source version is limited in any way. If you check the recently added code, it contains only warnings and messages, no limitations except one: we removed the option to add custom logo in header. In the Update Manager section, it warns about the 'danger’ of applying automated updates without a proper backup, remembering in the process that the Enterprise version comes with a human-tested package. Maintaining an OpenSource version with more than 500 agents is not so easy, that's why someone using a Pandora with 8000 agents should consider asking for support. It's not a joke, we know of many setups with a huge number of agents, and we hate to hear that “its becoming unstable and slow” :(
You can of course remove the warnings, that's why we include the source and do not use any kind of trick. And that's why we added here this comment, to let you know this does not reflect any change in our opensource mentality of does the last 14 years.
*/
if ($data['total_agents']) {
if (($data['monitor_checks'] / $data['total_agents'] > 100) && !enterprise_installed()) {
$tdata[5] = "";
}
}
$table_am->rowclass[] = '';
$table_am->data[] = $tdata;
$output = '';
return $output;
}
function reporting_get_stats_users($data)
{
global $config;
// Link URLS
$urls = [];
if (check_acl($config['id_user'], 0, 'UM')) {
$urls['defined_users'] = 'index.php?sec=gusuarios&sec2=godmode/users/user_list';
} else {
$urls['defined_users'] = 'javascript:';
}
// Users table
$table_us = html_get_predefined_table();
$tdata = [];
$tdata[0] = html_print_image('images/user_green.png', true, ['title' => __('Defined users')]);
$tdata[1] = count(get_users());
$tdata[1] = ''.$tdata[1].'';
$tdata[2] = $tdata[3] = ' ';
$table_us->rowclass[] = '';
$table_us->data[] = $tdata;
$output = '';
return $output;
}
/**
* Get the average value of an agent module in a period of time.
*
* @param int Agent module id
* @param int Period of time to check (in seconds)
* @param int Top date to check the values. Default current time.
*
* @return float The average module value in the interval.
*/
function reporting_get_agentmodule_data_average($id_agent_module, $period=0, $date=0)
{
global $config;
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
$id_module_type = modules_get_agentmodule_type($id_agent_module);
$module_type = modules_get_moduletype_name($id_module_type);
$uncompressed_module = is_module_uncompressed($module_type);
// Get module data
$interval_data = db_get_all_rows_sql(
'SELECT *
FROM tagente_datos
WHERE id_agente_modulo = '.(int) $id_agent_module.' AND utimestamp > '.(int) $datelimit.' AND utimestamp < '.(int) $date.' ORDER BY utimestamp ASC',
$search_in_history_db
);
if ($interval_data === false) {
$interval_data = [];
}
// Uncompressed module data
if ($uncompressed_module) {
$min_necessary = 1;
// Compressed module data
} else {
// Get previous data
$previous_data = modules_get_previous_data($id_agent_module, $datelimit);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift($interval_data, $previous_data);
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
$min_necessary = 2;
}
if (count($interval_data) < $min_necessary) {
return false;
}
// Set initial conditions
$total = 0;
$count = 0;
if (! $uncompressed_module) {
$previous_data = array_shift($interval_data);
// Do not count the empty start of an interval as 0
if ($previous_data['utimestamp'] != $datelimit) {
$period = ($date - $previous_data['utimestamp']);
}
}
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$previous_data['datos'] = oracle_format_float_to_php($previous_data['datos']);
break;
}
foreach ($interval_data as $data) {
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$data['datos'] = oracle_format_float_to_php($data['datos']);
break;
}
if (! $uncompressed_module) {
$total += ($previous_data['datos'] * ($data['utimestamp'] - $previous_data['utimestamp']));
$previous_data = $data;
} else {
$total += $data['datos'];
$count++;
}
}
// Compressed module data
if (! $uncompressed_module) {
if ($period == 0) {
return 0;
}
return ($total / $period);
}
// Uncompressed module data
if ($count == 0) {
return 0;
}
return ($total / $count);
}
/**
* Get the MTTR value of an agent module in a period of time. See
* http://en.wikipedia.org/wiki/Mean_time_to_recovery
*
* @param int Agent module id
* @param int Period of time to check (in seconds)
* @param int Top date to check the values. Default current time.
*
* @return float The MTTR value in the interval.
*/
function reporting_get_agentmodule_mttr($id_agent_module, $period=0, $date=0)
{
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
// Read module configuration
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
$module = db_get_row_sql(
'SELECT max_critical, min_critical, id_tipo_modulo
FROM tagente_modulo
WHERE id_agente_modulo = '.(int) $id_agent_module
);
if ($module === false) {
return false;
}
$critical_min = $module['min_critical'];
$critical_max = $module['max_critical'];
$module_type = $module['id_tipo_modulo'];
// Set critical_min and critical for proc modules
$module_type_str = modules_get_type_name($module_type);
if (strstr($module_type_str, 'proc') !== false
&& ($critical_min == 0 && $critical_max == 0)
) {
$critical_min = 1;
}
// Get module data
$interval_data = db_get_all_rows_sql(
'SELECT * FROM tagente_datos
WHERE id_agente_modulo = '.(int) $id_agent_module.' AND utimestamp > '.(int) $datelimit.' AND utimestamp < '.(int) $date.' ORDER BY utimestamp ASC',
$search_in_history_db
);
if ($interval_data === false) {
$interval_data = [];
}
// Get previous data
$previous_data = modules_get_previous_data(
$id_agent_module,
$datelimit
);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift($interval_data, $previous_data);
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
if (count($interval_data) < 2) {
return false;
}
// Set initial conditions
$critical_period = 0;
$first_data = array_shift($interval_data);
$previous_utimestamp = $first_data['utimestamp'];
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$first_data['datos'] = oracle_format_float_to_php($first_data['datos']);
break;
}
if ((($critical_max > $critical_min and ($first_data['datos'] > $critical_max or $first_data['datos'] < $critical_min)))
or ($critical_max <= $critical_min and $first_data['datos'] < $critical_min)
) {
$previous_status = 1;
$critical_count = 1;
} else {
$previous_status = 0;
$critical_count = 0;
}
foreach ($interval_data as $data) {
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$data['datos'] = oracle_format_float_to_php($data['datos']);
break;
}
// Previous status was critical
if ($previous_status == 1) {
$critical_period += ($data['utimestamp'] - $previous_utimestamp);
}
// Re-calculate previous status for the next data
if ((($critical_max > $critical_min and ($data['datos'] > $critical_max or $data['datos'] < $critical_min)))
or ($critical_max <= $critical_min and $data['datos'] < $critical_min)
) {
if ($previous_status == 0) {
$critical_count++;
}
$previous_status = 1;
} else {
$previous_status = 0;
}
$previous_utimestamp = $data['utimestamp'];
}
if ($critical_count == 0) {
return 0;
}
return ($critical_period / $critical_count);
}
/**
* Get the MTBF value of an agent module in a period of time. See
* http://en.wikipedia.org/wiki/Mean_time_between_failures
*
* @param int Agent module id
* @param int Period of time to check (in seconds)
* @param int Top date to check the values. Default current time.
*
* @return float The MTBF value in the interval.
*/
function reporting_get_agentmodule_mtbf($id_agent_module, $period=0, $date=0)
{
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
// Read module configuration
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
$module = db_get_row_sql(
'SELECT max_critical, min_critical, id_tipo_modulo
FROM tagente_modulo
WHERE id_agente_modulo = '.(int) $id_agent_module
);
if ($module === false) {
return false;
}
$critical_min = $module['min_critical'];
$critical_max = $module['max_critical'];
$module_type = $module['id_tipo_modulo'];
// Set critical_min and critical for proc modules
$module_type_str = modules_get_type_name($module_type);
if (strstr($module_type_str, 'proc') !== false
&& ($critical_min == 0 && $critical_max == 0)
) {
$critical_min = 1;
}
// Get module data
$interval_data = db_get_all_rows_sql(
'SELECT * FROM tagente_datos
WHERE id_agente_modulo = '.(int) $id_agent_module.' AND utimestamp > '.(int) $datelimit.' AND utimestamp < '.(int) $date.' ORDER BY utimestamp ASC',
$search_in_history_db
);
if ($interval_data === false) {
$interval_data = [];
}
// Get previous data
$previous_data = modules_get_previous_data($id_agent_module, $datelimit);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift($interval_data, $previous_data);
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
if (count($interval_data) < 2) {
return false;
}
// Set initial conditions
$critical_period = 0;
$first_data = array_shift($interval_data);
$previous_utimestamp = $first_data['utimestamp'];
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$first_data['datos'] = oracle_format_float_to_php($first_data['datos']);
break;
}
if ((($critical_max > $critical_min and ($first_data['datos'] > $critical_max or $first_data['datos'] < $critical_min)))
or ($critical_max <= $critical_min and $first_data['datos'] < $critical_min)
) {
$previous_status = 1;
$critical_count = 1;
} else {
$previous_status = 0;
$critical_count = 0;
}
foreach ($interval_data as $data) {
// Previous status was critical
if ($previous_status == 1) {
$critical_period += ($data['utimestamp'] - $previous_utimestamp);
}
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$data['datos'] = oracle_format_float_to_php($data['datos']);
break;
}
// Re-calculate previous status for the next data
if ((($critical_max > $critical_min and ($data['datos'] > $critical_max or $data['datos'] < $critical_min)))
or ($critical_max <= $critical_min and $data['datos'] < $critical_min)
) {
if ($previous_status == 0) {
$critical_count++;
}
$previous_status = 1;
} else {
$previous_status = 0;
}
$previous_utimestamp = $data['utimestamp'];
}
if ($critical_count == 0) {
return 0;
}
return (($period - $critical_period) / $critical_count);
}
/**
* Get the TTO value of an agent module in a period of time.
*
* @param int Agent module id
* @param int Period of time to check (in seconds)
* @param int Top date to check the values. Default current time.
*
* @return float The TTO value in the interval.
*/
function reporting_get_agentmodule_tto($id_agent_module, $period=0, $date=0)
{
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
// Read module configuration
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
$module = db_get_row_sql(
'SELECT max_critical, min_critical, id_tipo_modulo
FROM tagente_modulo
WHERE id_agente_modulo = '.(int) $id_agent_module
);
if ($module === false) {
return false;
}
$critical_min = $module['min_critical'];
$critical_max = $module['max_critical'];
$module_type = $module['id_tipo_modulo'];
// Set critical_min and critical for proc modules
$module_type_str = modules_get_type_name($module_type);
if (strstr($module_type_str, 'proc') !== false
&& ($critical_min == 0 && $critical_max == 0)
) {
$critical_min = 1;
}
// Get module data
$interval_data = db_get_all_rows_sql(
'SELECT * FROM tagente_datos
WHERE id_agente_modulo = '.(int) $id_agent_module.' AND utimestamp > '.(int) $datelimit.' AND utimestamp < '.(int) $date.' ORDER BY utimestamp ASC',
$search_in_history_db
);
if ($interval_data === false) {
$interval_data = [];
}
// Get previous data
$previous_data = modules_get_previous_data($id_agent_module, $datelimit);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift($interval_data, $previous_data);
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
if (count($interval_data) < 2) {
return false;
}
// Set initial conditions
$critical_period = 0;
$first_data = array_shift($interval_data);
$previous_utimestamp = $first_data['utimestamp'];
if ((($critical_max > $critical_min and ($first_data['datos'] > $critical_max or $first_data['datos'] < $critical_min)))
or ($critical_max <= $critical_min and $first_data['datos'] < $critical_min)
) {
$previous_status = 1;
} else {
$previous_status = 0;
}
foreach ($interval_data as $data) {
// Previous status was critical
if ($previous_status == 1) {
$critical_period += ($data['utimestamp'] - $previous_utimestamp);
}
// Re-calculate previous status for the next data
if ((($critical_max > $critical_min and ($data['datos'] > $critical_max or $data['datos'] < $critical_min)))
or ($critical_max <= $critical_min and $data['datos'] < $critical_min)
) {
$previous_status = 1;
} else {
$previous_status = 0;
}
$previous_utimestamp = $data['utimestamp'];
}
return ($period - $critical_period);
}
/**
* Get the TTR value of an agent module in a period of time.
*
* @param int Agent module id
* @param int Period of time to check (in seconds)
* @param int Top date to check the values. Default current time.
*
* @return float The TTR value in the interval.
*/
function reporting_get_agentmodule_ttr($id_agent_module, $period=0, $date=0)
{
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
// Read module configuration
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
$module = db_get_row_sql(
'SELECT max_critical, min_critical, id_tipo_modulo
FROM tagente_modulo
WHERE id_agente_modulo = '.(int) $id_agent_module
);
if ($module === false) {
return false;
}
$critical_min = $module['min_critical'];
$critical_max = $module['max_critical'];
$module_type = $module['id_tipo_modulo'];
// Set critical_min and critical for proc modules
$module_type_str = modules_get_type_name($module_type);
if (strstr($module_type_str, 'proc') !== false
&& ($critical_min == 0 && $critical_max == 0)
) {
$critical_min = 1;
}
// Get module data
$interval_data = db_get_all_rows_sql(
'SELECT * FROM tagente_datos
WHERE id_agente_modulo = '.(int) $id_agent_module.' AND utimestamp > '.(int) $datelimit.' AND utimestamp < '.(int) $date.' ORDER BY utimestamp ASC',
$search_in_history_db
);
if ($interval_data === false) {
$interval_data = [];
}
// Get previous data
$previous_data = modules_get_previous_data($id_agent_module, $datelimit);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift($interval_data, $previous_data);
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
if (count($interval_data) < 2) {
return false;
}
// Set initial conditions
$critical_period = 0;
$first_data = array_shift($interval_data);
$previous_utimestamp = $first_data['utimestamp'];
if ((($critical_max > $critical_min and ($first_data['datos'] > $critical_max or $first_data['datos'] < $critical_min)))
or ($critical_max <= $critical_min and $first_data['datos'] < $critical_min)
) {
$previous_status = 1;
} else {
$previous_status = 0;
}
foreach ($interval_data as $data) {
// Previous status was critical
if ($previous_status == 1) {
$critical_period += ($data['utimestamp'] - $previous_utimestamp);
}
// Re-calculate previous status for the next data
if ((($critical_max > $critical_min and ($data['datos'] > $critical_max or $data['datos'] < $critical_min)))
or ($critical_max <= $critical_min and $data['datos'] < $critical_min)
) {
$previous_status = 1;
} else {
$previous_status = 0;
}
$previous_utimestamp = $data['utimestamp'];
}
return $critical_period;
}
/**
* Get a detailed report of the modules of the agent
*
* @param integer $id_agent Agent id to get the report for.
*
* @return array An array
*/
function reporting_get_agent_module_info($id_agent)
{
global $config;
$return = [];
$return['last_contact'] = 0;
// Last agent contact
$return['status'] = STATUS_AGENT_NO_DATA;
$return['status_img'] = ui_print_status_image(STATUS_AGENT_NO_DATA, __('Agent without data'), true);
$return['alert_status'] = 'notfired';
$return['alert_value'] = STATUS_ALERT_NOT_FIRED;
$return['alert_img'] = ui_print_status_image(STATUS_ALERT_NOT_FIRED, __('Alert not fired'), true);
$return['agent_group'] = agents_get_agent_group($id_agent);
if (!check_acl($config['id_user'], $return['agent_group'], 'AR')) {
return $return;
}
$filter = ['disabled' => 0];
$modules = agents_get_modules($id_agent, false, $filter, true, false);
if ($modules === false) {
return $return;
}
$now = get_system_time();
// Get modules status for this agent
$agent = db_get_row('tagente', 'id_agente', $id_agent);
$return['total_count'] = $agent['total_count'];
$return['normal_count'] = $agent['normal_count'];
$return['warning_count'] = $agent['warning_count'];
$return['critical_count'] = $agent['critical_count'];
$return['unknown_count'] = $agent['unknown_count'];
$return['fired_count'] = $agent['fired_count'];
$return['notinit_count'] = $agent['notinit_count'];
if ($return['total_count'] > 0) {
if ($return['critical_count'] > 0) {
$return['status'] = STATUS_AGENT_CRITICAL;
$return['status_img'] = ui_print_status_image(STATUS_AGENT_CRITICAL, __('At least one module in CRITICAL status'), true);
} else if ($return['warning_count'] > 0) {
$return['status'] = STATUS_AGENT_WARNING;
$return['status_img'] = ui_print_status_image(STATUS_AGENT_WARNING, __('At least one module in WARNING status'), true);
} else if ($return['unknown_count'] > 0) {
$return['status'] = STATUS_AGENT_DOWN;
$return['status_img'] = ui_print_status_image(STATUS_AGENT_DOWN, __('At least one module is in UKNOWN status'), true);
} else {
$return['status'] = STATUS_AGENT_OK;
$return['status_img'] = ui_print_status_image(STATUS_AGENT_OK, __('All Monitors OK'), true);
}
}
// Alert not fired is by default
if ($return['fired_count'] > 0) {
$return['alert_status'] = 'fired';
$return['alert_img'] = ui_print_status_image(STATUS_ALERT_FIRED, __('Alert fired'), true);
$return['alert_value'] = STATUS_ALERT_FIRED;
} else if (groups_give_disabled_group($return['agent_group'])) {
$return['alert_status'] = 'disabled';
$return['alert_value'] = STATUS_ALERT_DISABLED;
$return['alert_img'] = ui_print_status_image(STATUS_ALERT_DISABLED, __('Alert disabled'), true);
}
return $return;
}
/**
* Print tiny statistics of the status of one agent, group, etc.
*
* @param mixed Array with the counts of the total modules, normal modules, critical modules, warning modules, unknown modules and fired alerts
* @param bool return or echo flag
*
* @return string html formatted tiny stats of modules/alerts of an agent
*/
function reporting_tiny_stats($counts_info, $return=false, $type='agent', $separator=':', $strict_user=false)
{
global $config;
$out = '';
// Depend the type of object, the stats will refer agents, modules...
switch ($type) {
case 'modules':
$template_title['total_count'] = __('%d Total modules');
$template_title['normal_count'] = __('%d Modules in normal status');
$template_title['critical_count'] = __('%d Modules in critical status');
$template_title['warning_count'] = __('%d Modules in warning status');
$template_title['unknown_count'] = __('%d Modules in unknown status');
$template_title['not_init_count'] = __('%d Modules in not init status');
break;
case 'agent':
$template_title['total_count'] = __('%d Total modules');
$template_title['normal_count'] = __('%d Normal modules');
$template_title['critical_count'] = __('%d Critical modules');
$template_title['warning_count'] = __('%d Warning modules');
$template_title['unknown_count'] = __('%d Unknown modules');
$template_title['fired_count'] = __('%d Fired alerts');
break;
default:
$template_title['total_count'] = __('%d Total agents');
$template_title['normal_count'] = __('%d Normal agents');
$template_title['critical_count'] = __('%d Critical agents');
$template_title['warning_count'] = __('%d Warning agents');
$template_title['unknown_count'] = __('%d Unknown agents');
$template_title['not_init_count'] = __('%d not init agents');
$template_title['fired_count'] = __('%d Fired alerts');
break;
}
// Store the counts in a data structure to print hidden divs with titles
$stats = [];
if (isset($counts_info['total_count'])) {
$not_init = isset($counts_info['notinit_count']) ? $counts_info['notinit_count'] : 0;
$total_count = ($counts_info['total_count'] - $not_init);
$stats[] = [
'name' => 'total_count',
'count' => $total_count,
'title' => sprintf($template_title['total_count'], $total_count),
];
}
if (isset($counts_info['normal_count'])) {
$normal_count = $counts_info['normal_count'];
$stats[] = [
'name' => 'normal_count',
'count' => $normal_count,
'title' => sprintf($template_title['normal_count'], $normal_count),
];
}
if (isset($counts_info['critical_count'])) {
$critical_count = $counts_info['critical_count'];
$stats[] = [
'name' => 'critical_count',
'count' => $critical_count,
'title' => sprintf($template_title['critical_count'], $critical_count),
];
}
if (isset($counts_info['warning_count'])) {
$warning_count = $counts_info['warning_count'];
$stats[] = [
'name' => 'warning_count',
'count' => $warning_count,
'title' => sprintf($template_title['warning_count'], $warning_count),
];
}
if (isset($counts_info['unknown_count'])) {
$unknown_count = $counts_info['unknown_count'];
$stats[] = [
'name' => 'unknown_count',
'count' => $unknown_count,
'title' => sprintf($template_title['unknown_count'], $unknown_count),
];
}
if (isset($counts_info['not_init_count'])) {
$not_init_count = $counts_info['not_init_count'];
$stats[] = [
'name' => 'not_init_count',
'count' => $not_init_count,
'title' => sprintf($template_title['not_init_count'], $not_init_count),
];
}
if (isset($template_title['fired_count'])) {
if (isset($counts_info['fired_count'])) {
$fired_count = $counts_info['fired_count'];
$stats[] = [
'name' => 'fired_count',
'count' => $fired_count,
'title' => sprintf($template_title['fired_count'], $fired_count),
];
}
}
$uniq_id = uniqid();
foreach ($stats as $stat) {
$params = [
'id' => 'forced_title_'.$stat['name'].'_'.$uniq_id,
'class' => 'forced_title_layer',
'content' => $stat['title'],
'hidden' => true,
];
$out .= html_print_div($params, true);
}
// If total count is less than 0, is an error. Never show negative numbers
if ($total_count < 0) {
$total_count = 0;
}
$out .= ''.''.$total_count.'';
if (isset($fired_count) && $fired_count > 0) {
$out .= ' '.$separator.' '.$fired_count.'';
}
if (isset($critical_count) && $critical_count > 0) {
$out .= ' '.$separator.' '.$critical_count.'';
}
if (isset($warning_count) && $warning_count > 0) {
$out .= ' '.$separator.' '.$warning_count.'';
}
if (isset($unknown_count) && $unknown_count > 0) {
$out .= ' '.$separator.' '.$unknown_count.'';
}
if (isset($not_init_count) && $not_init_count > 0) {
$out .= ' '.$separator.' '.$not_init_count.'';
}
if (isset($normal_count) && $normal_count > 0) {
$out .= ' '.$separator.' '.$normal_count.'';
}
$out .= '';
if ($return) {
return $out;
} else {
echo $out;
}
}
/**
* Get SLA of a module.
*
* @param int Agent module to calculate SLA
* @param int Period to check the SLA compliance.
* @param int Minimum data value the module in the right interval
* @param int Maximum data value the module in the right interval. False will
* ignore max value
* @param int Beginning date of the report in UNIX time (current date by default).
* @param array $dayWeek Array of days week to extract as array('monday' => false, 'tuesday' => true....), and by default is null.
* @param string $timeFrom Time in the day to start to extract in mysql format, by default null.
* @param string $timeTo Time in the day to end to extract in mysql format, by default null.
*
* @return float SLA percentage of the requested module. False if no data were
* found
*/
function reporting_get_agentmodule_sla(
$id_agent_module,
$period=0,
$min_value=1,
$max_value=false,
$date=0,
$daysWeek=null,
$timeFrom=null,
$timeTo=null
) {
global $config;
if (empty($id_agent_module)) {
return false;
}
// Set initial conditions
$bad_period = 0;
// Limit date to start searching data
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
if ($daysWeek === null) {
$daysWeek = [];
}
// Calculate the SLA for large time without hours
if ($timeFrom == $timeTo) {
// Get interval data
$sql = sprintf(
'SELECT *
FROM tagente_datos
WHERE id_agente_modulo = %d
AND utimestamp > %d AND utimestamp <= %d',
$id_agent_module,
$datelimit,
$date
);
// Add the working times (mon - tue - wed ...) and from time to time
$days = [];
// Translate to mysql week days
if ($daysWeek) {
foreach ($daysWeek as $key => $value) {
if (!$value) {
if ($key == 'monday') {
$days[] = 2;
}
if ($key == 'tuesday') {
$days[] = 3;
}
if ($key == 'wednesday') {
$days[] = 4;
}
if ($key == 'thursday') {
$days[] = 5;
}
if ($key == 'friday') {
$days[] = 6;
}
if ($key == 'saturday') {
$days[] = 7;
}
if ($key == 'sunday') {
$days[] = 1;
}
}
}
}
if (count($days) > 0) {
$sql .= ' AND DAYOFWEEK(FROM_UNIXTIME(utimestamp)) NOT IN ('.implode(',', $days).')';
}
$sql .= "\n";
$sql .= ' ORDER BY utimestamp ASC';
$interval_data = db_get_all_rows_sql($sql, $search_in_history_db);
if ($interval_data === false) {
$interval_data = [];
}
// Calculate planned downtime dates
$downtime_dates = reporting_get_planned_downtimes_intervals(
$id_agent_module,
$datelimit,
$date
);
// Get previous data
$previous_data = modules_get_previous_data($id_agent_module, $datelimit);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift($interval_data, $previous_data);
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
if (count($interval_data) < 2) {
return false;
}
$first_data = array_shift($interval_data);
// Do not count the empty start of an interval as 0
if ($first_data['utimestamp'] != $datelimit) {
$period = ($date - $first_data['utimestamp']);
}
$previous_utimestamp = $first_data['utimestamp'];
if (( ( $max_value > $min_value and ( $first_data['datos'] > $max_value
or $first_data['datos'] < $min_value ) ) )
or ( $max_value <= $min_value
and $first_data['datos'] < $min_value )
) {
$previous_status = 1;
foreach ($downtime_dates as $date_dt) {
if (($date_dt['date_from'] <= $previous_utimestamp)
and ($date_dt['date_to'] >= $previous_utimestamp)
) {
$previous_status = 0;
}
}
} else {
$previous_status = 0;
}
foreach ($interval_data as $data) {
// Previous status was critical
if ($previous_status == 1) {
$bad_period += ($data['utimestamp'] - $previous_utimestamp);
}
if (array_key_exists('datos', $data)) {
// Re-calculate previous status for the next data
if ((($max_value > $min_value and ($data['datos'] > $max_value or $data['datos'] < $min_value)))
or ($max_value <= $min_value and $data['datos'] < $min_value)
) {
$previous_status = 1;
foreach ($downtime_dates as $date_dt) {
if (($date_dt['date_from'] <= $data['utimestamp']) and ($date_dt['date_to'] >= $data['utimestamp'])) {
$previous_status = 0;
}
}
} else {
$previous_status = 0;
}
}
$previous_utimestamp = $data['utimestamp'];
}
// Return the percentage of SLA compliance
return (float) (100 - ($bad_period / $period) * 100);
} else if ($period <= SECONDS_1DAY) {
return reporting_get_agentmodule_sla_day(
$id_agent_module,
$period,
$min_value,
$max_value,
$date,
$daysWeek,
$timeFrom,
$timeTo
);
} else {
// Extract the data each day
$sla = 0;
$i = 0;
for ($interval = SECONDS_1DAY; $interval <= $period; $interval = ($interval + SECONDS_1DAY)) {
$sla_day = reporting_get_agentmodule_sla(
$id_agent_module,
SECONDS_1DAY,
$min_value,
$max_value,
($datelimit + $interval),
$daysWeek,
$timeFrom,
$timeTo
);
// Avoid to add the period of module not init
if ($sla_day !== false) {
$sla += $sla_day;
$i++;
}
}
$sla = ($sla / $i);
return $sla;
}
}
/**
* Get the time intervals where an agentmodule is affected by the planned downtimes.
*
* @param int Agent module to calculate planned downtimes intervals.
* @param int Start date in utimestamp.
* @param int End date in utimestamp.
* @param bool Whether ot not to get the planned downtimes that affect the service associated with the agentmodule.
*
* @return array with time intervals.
*/
function reporting_get_planned_downtimes_intervals($id_agent_module, $start_date, $end_date, $check_services=false)
{
global $config;
if (empty($id_agent_module)) {
return false;
}
include_once $config['homedir'].'/include/functions_planned_downtimes.php';
$malformed_planned_downtimes = planned_downtimes_get_malformed();
if (empty($malformed_planned_downtimes)) {
$malformed_planned_downtimes = [];
}
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
$tpdr_description = 'tpdr.description';
break;
case 'oracle':
$tpdr_description = 'to_char(tpdr.description)';
break;
}
$sql_downtime = '
SELECT DISTINCT(tpdr.id),
tpdr.name,
'.$tpdr_description.",
tpdr.date_from,
tpdr.date_to,
tpdr.executed,
tpdr.id_group,
tpdr.only_alerts,
tpdr.monday,
tpdr.tuesday,
tpdr.wednesday,
tpdr.thursday,
tpdr.friday,
tpdr.saturday,
tpdr.sunday,
tpdr.periodically_time_from,
tpdr.periodically_time_to,
tpdr.periodically_day_from,
tpdr.periodically_day_to,
tpdr.type_downtime,
tpdr.type_execution,
tpdr.type_periodicity,
tpdr.id_user
FROM (
SELECT tpd.*
FROM tplanned_downtime tpd, tplanned_downtime_agents tpda, tagente_modulo tam
WHERE tpd.id = tpda.id_downtime
AND tpda.all_modules = 1
AND tpda.id_agent = tam.id_agente
AND tam.id_agente_modulo = $id_agent_module
UNION ALL
SELECT tpd.*
FROM tplanned_downtime tpd, tplanned_downtime_modules tpdm
WHERE tpd.id = tpdm.id_downtime
AND tpdm.id_agent_module = $id_agent_module
) tpdr
ORDER BY tpdr.id";
$downtimes = db_get_all_rows_sql($sql_downtime);
if ($downtimes == false) {
$downtimes = [];
}
$downtime_dates = [];
foreach ($downtimes as $downtime) {
$downtime_id = $downtime['id'];
$downtime_type = $downtime['type_execution'];
$downtime_periodicity = $downtime['type_periodicity'];
if ($downtime_type == 'once') {
$dates = [];
$dates['date_from'] = $downtime['date_from'];
$dates['date_to'] = $downtime['date_to'];
$downtime_dates[] = $dates;
} else if ($downtime_type == 'periodically') {
// If a planned downtime have malformed dates, its intervals aren't taken account
$downtime_malformed = false;
foreach ($malformed_planned_downtimes as $malformed_planned_downtime) {
if ($downtime_id == $malformed_planned_downtime['id']) {
$downtime_malformed = true;
break;
}
}
if ($downtime_malformed == true) {
continue;
}
// If a planned downtime have malformed dates, its intervals aren't taken account
$downtime_time_from = $downtime['periodically_time_from'];
$downtime_time_to = $downtime['periodically_time_to'];
$downtime_hour_from = date('H', strtotime($downtime_time_from));
$downtime_minute_from = date('i', strtotime($downtime_time_from));
$downtime_second_from = date('s', strtotime($downtime_time_from));
$downtime_hour_to = date('H', strtotime($downtime_time_to));
$downtime_minute_to = date('i', strtotime($downtime_time_to));
$downtime_second_to = date('s', strtotime($downtime_time_to));
if ($downtime_periodicity == 'monthly') {
$downtime_day_from = $downtime['periodically_day_from'];
$downtime_day_to = $downtime['periodically_day_to'];
$date_aux = strtotime(date('Y-m-01', $start_date));
$year_aux = date('Y', $date_aux);
$month_aux = date('m', $date_aux);
$end_year = date('Y', $end_date);
$end_month = date('m', $end_date);
while ($year_aux < $end_year || ($year_aux == $end_year && $month_aux <= $end_month)) {
if ($downtime_day_from > $downtime_day_to) {
$dates = [];
$dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from");
$dates['date_to'] = strtotime(date('Y-m-t H:i:s', strtotime("$year_aux-$month_aux-28 23:59:59")));
$downtime_dates[] = $dates;
$dates = [];
if (($month_aux + 1) <= 12) {
$dates['date_from'] = strtotime("$year_aux-".($month_aux + 1).'-01 00:00:00');
$dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
} else {
$dates['date_from'] = strtotime(($year_aux + 1).'-01-01 00:00:00');
$dates['date_to'] = strtotime(($year_aux + 1)."-01-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
}
$downtime_dates[] = $dates;
} else {
if ($downtime_day_from == $downtime_day_to && strtotime($downtime_time_from) > strtotime($downtime_time_to)) {
$date_aux_from = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from");
$max_day_num = date('t', $date_aux);
$dates = [];
$dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from");
$dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_from 23:59:59");
$downtime_dates[] = $dates;
if (($downtime_day_to + 1) > $max_day_num) {
$dates = [];
if (($month_aux + 1) <= 12) {
$dates['date_from'] = strtotime("$year_aux-".($month_aux + 1).'-01 00:00:00');
$dates['date_to'] = strtotime("$year_aux-".($month_aux + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
} else {
$dates['date_from'] = strtotime(($year_aux + 1).'-01-01 00:00:00');
$dates['date_to'] = strtotime(($year_aux + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
}
$downtime_dates[] = $dates;
} else {
$dates = [];
$dates['date_from'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1).' 00:00:00');
$dates['date_to'] = strtotime("$year_aux-$month_aux-".($downtime_day_to + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
$downtime_dates[] = $dates;
}
} else {
$dates = [];
$dates['date_from'] = strtotime("$year_aux-$month_aux-$downtime_day_from $downtime_hour_from:$downtime_minute_from:$downtime_second_from");
$dates['date_to'] = strtotime("$year_aux-$month_aux-$downtime_day_to $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
$downtime_dates[] = $dates;
}
}
$month_aux++;
if ($month_aux > 12) {
$month_aux = 1;
$year_aux++;
}
}
} else if ($downtime_periodicity == 'weekly') {
$date_aux = $start_date;
$active_days = [];
$active_days[0] = ($downtime['sunday'] == 1) ? true : false;
$active_days[1] = ($downtime['monday'] == 1) ? true : false;
$active_days[2] = ($downtime['tuesday'] == 1) ? true : false;
$active_days[3] = ($downtime['wednesday'] == 1) ? true : false;
$active_days[4] = ($downtime['thursday'] == 1) ? true : false;
$active_days[5] = ($downtime['friday'] == 1) ? true : false;
$active_days[6] = ($downtime['saturday'] == 1) ? true : false;
while ($date_aux <= $end_date) {
$weekday_num = date('w', $date_aux);
if ($active_days[$weekday_num]) {
$day_num = date('d', $date_aux);
$month_num = date('m', $date_aux);
$year_num = date('Y', $date_aux);
$max_day_num = date('t', $date_aux);
if (strtotime($downtime_time_from) > strtotime($downtime_time_to)) {
$dates = [];
$dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from");
$dates['date_to'] = strtotime("$year_num-$month_num-$day_num 23:59:59");
$downtime_dates[] = $dates;
$dates = [];
if (($day_num + 1) > $max_day_num) {
if (($month_num + 1) > 12) {
$dates['date_from'] = strtotime(($year_num + 1).'-01-01 00:00:00');
$dates['date_to'] = strtotime(($year_num + 1)."-01-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
} else {
$dates['date_from'] = strtotime("$year_num-".($month_num + 1).'-01 00:00:00');
$dates['date_to'] = strtotime("$year_num-".($month_num + 1)."-01 $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
}
} else {
$dates['date_from'] = strtotime("$year_num-$month_num-".($day_num + 1).' 00:00:00');
$dates['date_to'] = strtotime("$year_num-$month_num-".($day_num + 1)." $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
}
$downtime_dates[] = $dates;
} else {
$dates = [];
$dates['date_from'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_from:$downtime_minute_from:$downtime_second_from");
$dates['date_to'] = strtotime("$year_num-$month_num-$day_num $downtime_hour_to:$downtime_minute_to:$downtime_second_to");
$downtime_dates[] = $dates;
}
}
$date_aux += SECONDS_1DAY;
}
}
}
}
if ($check_services) {
enterprise_include_once('include/functions_services.php');
if (function_exists('services_get_planned_downtimes_intervals')) {
services_get_planned_downtimes_intervals($downtime_dates, $start_date, $end_date, false, $id_agent_module);
}
}
return $downtime_dates;
}
/**
* Get the maximum value of an agent module in a period of time.
*
* @param int Agent module id to get the maximum value.
* @param int Period of time to check (in seconds)
* @param int Top date to check the values. Default current time.
*
* @return float The maximum module value in the interval.
*/
function reporting_get_agentmodule_data_max($id_agent_module, $period=0, $date=0)
{
global $config;
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
$id_module_type = modules_get_agentmodule_type($id_agent_module);
$module_type = modules_get_moduletype_name($id_module_type);
$uncompressed_module = is_module_uncompressed($module_type);
// Get module data
$interval_data = db_get_all_rows_sql(
'SELECT *
FROM tagente_datos
WHERE id_agente_modulo = '.(int) $id_agent_module.' AND utimestamp > '.(int) $datelimit.' AND utimestamp < '.(int) $date.' ORDER BY utimestamp ASC',
$search_in_history_db
);
if ($interval_data === false) {
$interval_data = [];
}
// Uncompressed module data
if ($uncompressed_module) {
// Compressed module data
} else {
// Get previous data
$previous_data = modules_get_previous_data($id_agent_module, $datelimit);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift($interval_data, $previous_data);
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
}
// Set initial conditions
if (empty($iterval_data)) {
$max = 0;
} else {
if ($uncompressed_module || $interval_data[0]['utimestamp'] == $datelimit) {
$max = $interval_data[0]['datos'];
} else {
$max = 0;
}
}
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$max = oracle_format_float_to_php($max);
break;
}
foreach ($interval_data as $data) {
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$data['datos'] = oracle_format_float_to_php($data['datos']);
break;
}
if ($data['datos'] > $max) {
$max = $data['datos'];
}
}
return $max;
}
/**
* Get the minimum value of an agent module in a period of time.
*
* @param int Agent module id to get the minimum value.
* @param int Period of time to check (in seconds)
* @param int Top date to check the values in Unix time. Default current time.
*
* @return float The minimum module value of the module
*/
function reporting_get_agentmodule_data_min($id_agent_module, $period=0, $date=0)
{
global $config;
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
$id_module_type = modules_get_agentmodule_type($id_agent_module);
$module_type = modules_get_moduletype_name($id_module_type);
$uncompressed_module = is_module_uncompressed($module_type);
// Get module data
$interval_data = db_get_all_rows_sql(
'SELECT *
FROM tagente_datos
WHERE id_agente_modulo = '.(int) $id_agent_module.' AND utimestamp > '.(int) $datelimit.' AND utimestamp < '.(int) $date.' ORDER BY utimestamp ASC',
$search_in_history_db
);
if ($interval_data === false) {
$interval_data = [];
}
// Uncompressed module data
if ($uncompressed_module) {
$min_necessary = 1;
// Compressed module data
} else {
// Get previous data
$previous_data = modules_get_previous_data($id_agent_module, $datelimit);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift($interval_data, $previous_data);
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
}
if (count($interval_data) < 1) {
return false;
}
// Set initial conditions
$min = $interval_data[0]['datos'];
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$min = oracle_format_float_to_php($min);
break;
}
foreach ($interval_data as $data) {
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$data['datos'] = oracle_format_float_to_php($data['datos']);
break;
}
if ($data['datos'] < $min) {
$min = $data['datos'];
}
}
return $min;
}
/**
* Get the sum of values of an agent module in a period of time.
*
* @param int Agent module id to get the sumatory.
* @param int Period of time to check (in seconds)
* @param int Top date to check the values. Default current time.
*
* @return float The sumatory of the module values in the interval.
*/
function reporting_get_agentmodule_data_sum(
$id_agent_module,
$period=0,
$date=0
) {
global $config;
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
$datelimit = ($date - $period);
$search_in_history_db = db_search_in_history_db($datelimit);
$id_module_type = db_get_value(
'id_tipo_modulo',
'tagente_modulo',
'id_agente_modulo',
$id_agent_module
);
$module_name = db_get_value(
'nombre',
'ttipo_modulo',
'id_tipo',
$id_module_type
);
$module_interval = modules_get_interval($id_agent_module);
$uncompressed_module = is_module_uncompressed($module_name);
// Wrong module type
if (is_module_data_string($module_name)) {
return 0;
}
// Incremental modules are treated differently
$module_inc = is_module_inc($module_name);
if ($uncompressed_module) {
// Get module data
$interval_data = db_get_all_rows_sql(
'
SELECT * FROM tagente_datos
WHERE id_agente_modulo = '.(int) $id_agent_module.'
AND utimestamp > '.(int) $datelimit.'
AND utimestamp < '.(int) $date.'
ORDER BY utimestamp ASC',
$search_in_history_db
);
} else {
$interval_data = db_uncompress_module_data((int) $id_agent_module, (int) $datelimit, (int) $date);
}
if ($interval_data === false) {
$interval_data = [];
}
$min_necessary = 1;
if (count($interval_data) < $min_necessary) {
return false;
}
// Set initial conditions
$total = 0;
$partial_total = 0;
$count_sum = 0;
foreach ($interval_data as $data) {
$partial_total = 0;
$count_sum = 0;
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
// Do none
break;
case 'oracle':
$data['datos'] = oracle_format_float_to_php($data['datos']);
break;
}
if (!$module_inc) {
foreach ($data['data'] as $val) {
if (is_numeric($val['datos'])) {
$partial_total += $val['datos'];
$count_sum++;
}
}
if ($count_sum === 0) {
continue;
}
$total += ($partial_total / $count_sum);
} else {
$last = end($data['data']);
$total += $last['datos'];
}
}
return $total;
}
/**
* Get the planned downtimes that affect the passed modules on an specific datetime range.
*
* @param int Start date in utimestamp.
* @param int End date in utimestamp.
* @param array The agent modules ids.
*
* @return array with the planned downtimes that are executed in any moment of the range selected and affect the
* agent modules selected.
*/
function reporting_get_planned_downtimes($start_date, $end_date, $id_agent_modules=false)
{
global $config;
$start_time = date('H:i:s', $start_date);
$end_time = date('H:i:s', $end_date);
$start_day = date('d', $start_date);
$end_day = date('d', $end_date);
$start_month = date('m', $start_date);
$end_month = date('m', $end_date);
if ($start_date > $end_date) {
return false;
}
if (($end_date - $start_date) >= SECONDS_1MONTH) {
// If the date range is larger than 1 month, every monthly planned downtime will be inside
$periodically_monthly_w = "type_periodicity = 'monthly'";
} else {
// Check if the range is larger than the planned downtime execution, or if its start or end
// is inside the planned downtime execution.
// The start and end time is very important.
$periodically_monthly_w = "type_periodicity = 'monthly'
AND (((periodically_day_from > '$start_day'
OR (periodically_day_from = '$start_day'
AND periodically_time_from >= '$start_time'))
AND (periodically_day_to < '$end_day'
OR (periodically_day_to = '$end_day'
AND periodically_time_to <= '$end_time')))
OR ((periodically_day_from < '$start_day'
OR (periodically_day_from = '$start_day'
AND periodically_time_from <= '$start_time'))
AND (periodically_day_to > '$start_day'
OR (periodically_day_to = '$start_day'
AND periodically_time_to >= '$start_time')))
OR ((periodically_day_from < '$end_day'
OR (periodically_day_from = '$end_day'
AND periodically_time_from <= '$end_time'))
AND (periodically_day_to > '$end_day'
OR (periodically_day_to = '$end_day'
AND periodically_time_to >= '$end_time'))))";
}
$periodically_weekly_days = [];
$date_aux = $start_date;
$i = 0;
if (($end_date - $start_date) >= SECONDS_1WEEK) {
// If the date range is larger than 7 days, every weekly planned downtime will be inside.
for ($i = 0; $i < 7; $i++) {
$weekday_actual = strtolower(date('l', $date_aux));
$periodically_weekly_days[] = "($weekday_actual = 1)";
$date_aux += SECONDS_1DAY;
}
} else if (($end_date - $start_date) <= SECONDS_1DAY && $start_day == $end_day) {
// If the date range is smaller than 1 day, the start and end days can be equal or consecutive.
// If they are equal, the execution times have to be contained in the date range times or contain
// the start or end time of the date range.
$weekday_actual = strtolower(date('l', $start_date));
$periodically_weekly_days[] = "($weekday_actual = 1
AND ((periodically_time_from > '$start_time' AND periodically_time_to < '$end_time')
OR (periodically_time_from = '$start_time'
OR (periodically_time_from < '$start_time'
AND periodically_time_to >= '$start_time'))
OR (periodically_time_from = '$end_time'
OR (periodically_time_from < '$end_time'
AND periodically_time_to >= '$end_time'))))";
} else {
while ($date_aux <= $end_date && $i < 7) {
$weekday_actual = strtolower(date('l', $date_aux));
$day_num_actual = date('d', $date_aux);
if ($date_aux == $start_date) {
$periodically_weekly_days[] = "($weekday_actual = 1 AND periodically_time_to >= '$start_time')";
} else if ($day_num_actual == $end_day) {
$periodically_weekly_days[] = "($weekday_actual = 1 AND periodically_time_from <= '$end_time')";
} else {
$periodically_weekly_days[] = "($weekday_actual = 1)";
}
$date_aux += SECONDS_1DAY;
$i++;
}
}
if (!empty($periodically_weekly_days)) {
$periodically_weekly_w = "type_periodicity = 'weekly' AND (".implode(' OR ', $periodically_weekly_days).')';
$periodically_condition = "(($periodically_monthly_w) OR ($periodically_weekly_w))";
} else {
$periodically_condition = "($periodically_monthly_w)";
}
if ($id_agent_modules !== false) {
if (empty($id_agent_modules)) {
return [];
}
$id_agent_modules_str = implode(',', $id_agent_modules);
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
$tpdr_description = 'tpdr.description';
break;
case 'oracle':
$tpdr_description = 'to_char(tpdr.description)';
break;
}
$sql_downtime = '
SELECT
DISTINCT(tpdr.id),
tpdr.name,
'.$tpdr_description.",
tpdr.date_from,
tpdr.date_to,
tpdr.executed,
tpdr.id_group,
tpdr.only_alerts,
tpdr.monday,
tpdr.tuesday,
tpdr.wednesday,
tpdr.thursday,
tpdr.friday,
tpdr.saturday,
tpdr.sunday,
tpdr.periodically_time_from,
tpdr.periodically_time_to,
tpdr.periodically_day_from,
tpdr.periodically_day_to,
tpdr.type_downtime,
tpdr.type_execution,
tpdr.type_periodicity,
tpdr.id_user
FROM (
SELECT tpd.*
FROM tplanned_downtime tpd, tplanned_downtime_agents tpda, tagente_modulo tam
WHERE (tpd.id = tpda.id_downtime
AND tpda.all_modules = 1
AND tpda.id_agent = tam.id_agente
AND tam.id_agente_modulo IN ($id_agent_modules_str))
AND ((type_execution = 'periodically'
AND $periodically_condition)
OR (type_execution = 'once'
AND ((date_from >= '$start_date' AND date_to <= '$end_date')
OR (date_from <= '$start_date' AND date_to >= '$end_date')
OR (date_from <= '$start_date' AND date_to >= '$start_date')
OR (date_from <= '$end_date' AND date_to >= '$end_date'))))
UNION ALL
SELECT tpd.*
FROM tplanned_downtime tpd, tplanned_downtime_modules tpdm
WHERE (tpd.id = tpdm.id_downtime
AND tpdm.id_agent_module IN ($id_agent_modules_str))
AND ((type_execution = 'periodically'
AND $periodically_condition)
OR (type_execution = 'once'
AND ((date_from >= '$start_date' AND date_to <= '$end_date')
OR (date_from <= '$start_date' AND date_to >= '$end_date')
OR (date_from <= '$start_date' AND date_to >= '$start_date')
OR (date_from <= '$end_date' AND date_to >= '$end_date'))))
) tpdr
ORDER BY tpdr.id";
} else {
$sql_downtime = "SELECT *
FROM tplanned_downtime tpd, tplanned_downtime_modules tpdm
WHERE (type_execution = 'periodically'
AND $periodically_condition)
OR (type_execution = 'once'
AND ((date_from >= '$start_date' AND date_to <= '$end_date')
OR (date_from <= '$start_date' AND date_to >= '$end_date')
OR (date_from <= '$start_date' AND date_to >= '$start_date')
OR (date_from <= '$end_date' AND date_to >= '$end_date')))";
}
$downtimes = db_get_all_rows_sql($sql_downtime);
if ($downtimes == false) {
$downtimes = [];
}
return $downtimes;
}
/**
* Get SLA of a module.
*
* @param int Agent module to calculate SLA
* @param int Period to check the SLA compliance.
* @param int Minimum data value the module in the right interval
* @param int Maximum data value the module in the right interval. False will
* ignore max value
* @param int Beginning date of the report in UNIX time (current date by default).
* @param array $dayWeek Array of days week to extract as array('monday' => false, 'tuesday' => true....), and by default is null.
* @param string $timeFrom Time in the day to start to extract in mysql format, by default null.
* @param string $timeTo Time in the day to end to extract in mysql format, by default null.
*
* @return float SLA percentage of the requested module. False if no data were
* found
*/
function reporting_get_agentmodule_sla_day($id_agent_module, $period=0, $min_value=1, $max_value=false, $date=0, $daysWeek=null, $timeFrom=null, $timeTo=null)
{
global $config;
if (empty($id_agent_module)) {
return false;
}
// Initialize variables
if (empty($date)) {
$date = get_system_time();
}
if ($daysWeek === null) {
$daysWeek = [];
}
// Limit date to start searching data
$datelimit = ($date - $period);
// Substract the not working time
// Initialize the working time status machine ($wt_status)
// Search the first data at worktime start
$array_sla_report = reporting_get_agentmodule_sla_day_period($period, $date, $timeFrom, $timeTo);
$period_reduced = $array_sla_report[0];
$wt_status = $array_sla_report[1];
$datelimit_increased = $array_sla_report[2];
if ($period_reduced <= 0) {
return false;
}
$wt_points = reporting_get_agentmodule_sla_working_timestamp($period, $date, $timeFrom, $timeTo);
$search_in_history_db = db_search_in_history_db($datelimit);
// Get interval data
$sql = sprintf(
'SELECT *
FROM tagente_datos
WHERE id_agente_modulo = %d
AND utimestamp > %d
AND utimestamp <= %d',
$id_agent_module,
$datelimit,
$date
);
// Add the working times (mon - tue - wed ...) and from time to time
$days = [];
// Translate to mysql week days
if ($daysWeek) {
foreach ($daysWeek as $key => $value) {
if (!$value) {
if ($key == 'monday') {
$days[] = 2;
}
if ($key == 'tuesday') {
$days[] = 3;
}
if ($key == 'wednesday') {
$days[] = 4;
}
if ($key == 'thursday') {
$days[] = 5;
}
if ($key == 'friday') {
$days[] = 6;
}
if ($key == 'saturday') {
$days[] = 7;
}
if ($key == 'sunday') {
$days[] = 1;
}
}
}
}
/*
The not working time consideration is now doing in foreach loop above
switch ($config["dbtype"]) {
case "mysql":
case "postgresql":
if (count($days) > 0) {
$sql .= ' AND DAYOFWEEK(FROM_UNIXTIME(utimestamp)) NOT IN (' . implode(',', $days) . ')';
}
if ($timeFrom < $timeTo) {
$sql .= ' AND (TIME(FROM_UNIXTIME(utimestamp)) >= "' . $timeFrom . '" AND TIME(FROM_UNIXTIME(utimestamp)) <= "'. $timeTo . '")';
}
elseif ($timeFrom > $timeTo) {
$sql .= ' AND (TIME(FROM_UNIXTIME(utimestamp)) >= "' . $timeFrom . '" OR TIME(FROM_UNIXTIME(utimestamp)) <= "'. $timeTo . '")';
}
break;
case "oracle":
break;
}
* */
$sql .= ' ORDER BY utimestamp ASC';
$interval_data = db_get_all_rows_sql($sql, $search_in_history_db);
if ($interval_data === false) {
$interval_data = [];
}
// Calculate planned downtime dates
$downtime_dates = reporting_get_planned_downtimes_intervals($id_agent_module, $datelimit, $date);
// Get previous data
$previous_data = modules_get_previous_data($id_agent_module, ($datelimit + $datelimit_increased));
if ($previous_data !== false) {
$previous_data['utimestamp'] = ($datelimit + $datelimit_increased);
array_unshift($interval_data, $previous_data);
} else if (count($interval_data) > 0) {
// Propagate undefined status to first time point
$first_interval_time = array_shift($interval_data);
$previous_point = ($datelimit + $datelimit_increased);
$point = ($datelimit + $datelimit_increased);
// Remove rebased points and substract time only on working time
while ($wt_points[0] <= $first_interval_time['utimestamp']) {
$point = array_shift($wt_points);
if ($wt_status) {
$period_reduced -= ($point - $previous_point);
}
$wt_status = !$wt_status;
$previous_point = $point;
}
if ($wt_status) {
$period_reduced -= ($first_interval_time['utimestamp'] - $point);
}
array_unshift($interval_data, $first_interval_time);
}
if (count($wt_points) < 2) {
return false;
}
// Get next data
$next_data = modules_get_next_data($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
} else if (count($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop($interval_data);
array_push($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push($interval_data, $next_data);
}
if (count($interval_data) < 2) {
return false;
}
// Set initial conditions
$bad_period = 0;
$first_data = array_shift($interval_data);
// Do not count the empty start of an interval as 0
if ($first_data['utimestamp'] != $datelimit) {
$period = ($date - $first_data['utimestamp']);
}
$previous_utimestamp = $first_data['utimestamp'];
if ((($max_value > $min_value and ($first_data['datos'] > $max_value or $first_data['datos'] < $min_value)))
or ($max_value <= $min_value and $first_data['datos'] < $min_value)
) {
$previous_status = 1;
foreach ($downtime_dates as $date_dt) {
if (($date_dt['date_from'] <= $previous_utimestamp) and ($date_dt['date_to'] >= $previous_utimestamp)) {
$previous_status = 0;
}
}
} else {
$previous_status = 0;
}
foreach ($interval_data as $data) {
// Test if working time is changed
while ($wt_points[0] <= $data['utimestamp']) {
$intermediate_point = array_shift($wt_points);
if ($wt_status && ($previous_status == 1)) {
$bad_period += ($intermediate_point - $previous_utimestamp);
}
$previous_utimestamp = $intermediate_point;
$wt_status = !$wt_status;
}
// Increses bad_period only if it is working time
if ($wt_status && ($previous_status == 1)) {
$bad_period += ($data['utimestamp'] - $previous_utimestamp);
}
if (array_key_exists('datos', $data)) {
// Re-calculate previous status for the next data
if ((($max_value > $min_value and ($data['datos'] > $max_value or $data['datos'] < $min_value)))
or ($max_value <= $min_value and $data['datos'] < $min_value)
) {
$previous_status = 1;
foreach ($downtime_dates as $date_dt) {
if (($date_dt['date_from'] <= $data['utimestamp']) and ($date_dt['date_to'] >= $data['utimestamp'])) {
$previous_status = 0;
}
}
} else {
$previous_status = 0;
}
}
$previous_utimestamp = $data['utimestamp'];
}
// Return the percentage of SLA compliance
return (float) (100 - ($bad_period / $period_reduced) * 100);
}
function reporting_get_stats_servers()
{
global $config;
$server_performance = servers_get_performance();
// Alerts table
$table_srv = html_get_predefined_table();
$table_srv->style[0] = $table_srv->style[2] = 'text-align: right; padding: 5px;';
$table_srv->style[1] = $table_srv->style[3] = 'text-align: left; padding: 5px;';
$tdata = [];
''.format_numeric($server_performance['total_local_modules']).'';
$tdata[0] = html_print_image('images/module.png', true, ['title' => __('Total running modules'), 'width' => '25px']);
$tdata[1] = ''.format_numeric($server_performance['total_modules']).'';
$tdata[2] = ''.format_numeric($server_performance['total_modules_rate'], 2).'';
$tdata[3] = html_print_image('images/module.png', true, ['title' => __('Ratio').': '.__('Modules by second'), 'width' => '16px']).'/sec ';
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
$tdata = [];
$tdata[0] = '';
$table_srv->colspan[count($table_srv->data)][0] = 4;
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
$tdata = [];
$tdata[0] = html_print_image('images/database.png', true, ['title' => __('Local modules'), 'width' => '25px']);
$tdata[1] = ''.format_numeric($server_performance['total_local_modules']).'';
$tdata[2] = ''.format_numeric($server_performance['local_modules_rate'], 2).'';
$tdata[3] = html_print_image('images/module.png', true, ['title' => __('Ratio').': '.__('Modules by second'), 'width' => '16px']).'/sec ';
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
if (isset($server_performance['total_network_modules'])) {
$tdata = [];
$tdata[0] = html_print_image('images/network.png', true, ['title' => __('Network modules'), 'width' => '25px']);
$tdata[1] = ''.format_numeric($server_performance['total_network_modules']).'';
$tdata[2] = ''.format_numeric($server_performance['network_modules_rate'], 2).'';
$tdata[3] = html_print_image('images/module.png', true, ['title' => __('Ratio').': '.__('Modules by second'), 'width' => '16px']).'/sec ';
if ($server_performance['total_remote_modules'] > 10000 && !enterprise_installed()) {
$tdata[4] = "";
} else {
$tdata[4] = ' ';
}
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
}
if (isset($server_performance['total_plugin_modules'])) {
$tdata = [];
$tdata[0] = html_print_image('images/plugin.png', true, ['title' => __('Plugin modules'), 'width' => '25px']);
$tdata[1] = ''.format_numeric($server_performance['total_plugin_modules']).'';
$tdata[2] = ''.format_numeric($server_performance['plugin_modules_rate'], 2).'';
$tdata[3] = html_print_image('images/module.png', true, ['title' => __('Ratio').': '.__('Modules by second'), 'width' => '16px']).'/sec ';
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
}
if (isset($server_performance['total_prediction_modules'])) {
$tdata = [];
$tdata[0] = html_print_image('images/chart_bar.png', true, ['title' => __('Prediction modules'), 'width' => '25px']);
$tdata[1] = ''.format_numeric($server_performance['total_prediction_modules']).'';
$tdata[2] = ''.format_numeric($server_performance['prediction_modules_rate'], 2).'';
$tdata[3] = html_print_image('images/module.png', true, ['title' => __('Ratio').': '.__('Modules by second'), 'width' => '16px']).'/sec ';
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
}
if (isset($server_performance['total_wmi_modules'])) {
$tdata = [];
$tdata[0] = html_print_image('images/wmi.png', true, ['title' => __('WMI modules'), 'width' => '25px']);
$tdata[1] = ''.format_numeric($server_performance['total_wmi_modules']).'';
$tdata[2] = ''.format_numeric($server_performance['wmi_modules_rate'], 2).'';
$tdata[3] = html_print_image('images/module.png', true, ['title' => __('Ratio').': '.__('Modules by second'), 'width' => '16px']).'/sec ';
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
}
if (isset($server_performance['total_web_modules'])) {
$tdata = [];
$tdata[0] = html_print_image('images/world.png', true, ['title' => __('Web modules'), 'width' => '25px']);
$tdata[1] = ''.format_numeric($server_performance['total_web_modules']).'';
$tdata[2] = ''.format_numeric($server_performance['web_modules_rate'], 2).'';
$tdata[3] = html_print_image('images/module.png', true, ['title' => __('Ratio').': '.__('Modules by second'), 'width' => '16px']).'/sec ';
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
}
$tdata = [];
$tdata[0] = '';
$table_srv->colspan[count($table_srv->data)][0] = 4;
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
$tdata = [];
$tdata[0] = html_print_image(
'images/lightning_go.png',
true,
[
'title' => __('Total events'),
'width' => '25px',
]
);
$tdata[1] = ''.html_print_image('images/spinner.gif', true).'';
/*
Hello there! :)
We added some of what seems to be "buggy" messages to the openSource version recently. This is not to force open-source users to move to the enterprise version, this is just to inform people using Pandora FMS open source that it requires skilled people to maintain and keep it running smoothly without professional support. This does not imply open-source version is limited in any way. If you check the recently added code, it contains only warnings and messages, no limitations except one: we removed the option to add custom logo in header. In the Update Manager section, it warns about the 'danger’ of applying automated updates without a proper backup, remembering in the process that the Enterprise version comes with a human-tested package. Maintaining an OpenSource version with more than 500 agents is not so easy, that's why someone using a Pandora with 8000 agents should consider asking for support. It's not a joke, we know of many setups with a huge number of agents, and we hate to hear that “its becoming unstable and slow” :(
You can of course remove the warnings, that's why we include the source and do not use any kind of trick. And that's why we added here this comment, to let you know this does not reflect any change in our opensource mentality of does the last 14 years.
*/
if ($system_events > 50000 && !enterprise_installed()) {
$tdata[2] = "";
} else {
$tdata[3] = ' ';
}
$table_srv->colspan[count($table_srv->data)][1] = 2;
$table_srv->rowclass[] = '';
$table_srv->data[] = $tdata;
$output = '';
$output .= '';
return $output;
}
/**
* Get all the template graphs a user can see.
*
* @param $id_user User id to check.
* @param $only_names Wheter to return only graphs names in an associative array
* or all the values.
* @param $returnAllGroup Wheter to return graphs of group All or not.
* @param $privileges Privileges to check in user group
*
* @return template graphs of a an user. Empty array if none.
*/
function reporting_template_graphs_get_user($id_user=0, $only_names=false, $returnAllGroup=true, $privileges='RR')
{
global $config;
if (!$id_user) {
$id_user = $config['id_user'];
}
$groups = users_get_groups($id_user, $privileges, $returnAllGroup);
$all_templates = db_get_all_rows_in_table('tgraph_template', 'name');
if ($all_templates === false) {
return [];
}
$templates = [];
foreach ($all_templates as $template) {
if (!in_array($template['id_group'], array_keys($groups))) {
continue;
}
if ($template['id_user'] != $id_user && $template['private']) {
continue;
}
if ($template['id_group'] > 0) {
if (!isset($groups[$template['id_group']])) {
continue;
}
}
if ($only_names) {
$templates[$template['id_graph_template']] = $template['name'];
} else {
$templates[$template['id_graph_template']] = $template;
$templatesCount = db_get_value_sql('SELECT COUNT(id_gs_template) FROM tgraph_source_template WHERE id_template = '.$template['id_graph_template']);
$templates[$template['id_graph_template']]['graphs_template_count'] = $templatesCount;
}
}
return $templates;
}
/**
* Get a human readable representation of the planned downtime date.
*
* @param array $planned_downtime Planned downtime row.
*
* @return string Representation of the date.
*/
function reporting_format_planned_downtime_dates($planned_downtime)
{
$dates = '';
if (!isset($planned_downtime) || !isset($planned_downtime['type_execution'])) {
return '';
}
switch ($planned_downtime['type_execution']) {
case 'once':
$dates = date('Y-m-d H:i', $planned_downtime['date_from']).' '.__('to').' '.date('Y-m-d H:i', $planned_downtime['date_to']);
break;
case 'periodically':
if (!isset($planned_downtime['type_periodicity'])) {
return '';
}
switch ($planned_downtime['type_periodicity']) {
case 'weekly':
$dates = __('Weekly:');
$dates .= ' ';
if ($planned_downtime['monday']) {
$dates .= __('Mon');
$dates .= ' ';
}
if ($planned_downtime['tuesday']) {
$dates .= __('Tue');
$dates .= ' ';
}
if ($planned_downtime['wednesday']) {
$dates .= __('Wed');
$dates .= ' ';
}
if ($planned_downtime['thursday']) {
$dates .= __('Thu');
$dates .= ' ';
}
if ($planned_downtime['friday']) {
$dates .= __('Fri');
$dates .= ' ';
}
if ($planned_downtime['saturday']) {
$dates .= __('Sat');
$dates .= ' ';
}
if ($planned_downtime['sunday']) {
$dates .= __('Sun');
$dates .= ' ';
}
$dates .= ' ('.$planned_downtime['periodically_time_from'];
$dates .= '-'.$planned_downtime['periodically_time_to'].')';
break;
case 'monthly':
$dates = __('Monthly:').' ';
$dates .= __('From day').' '.$planned_downtime['periodically_day_from'];
$dates .= ' '.strtolower(__('To day')).' ';
$dates .= $planned_downtime['periodically_day_to'];
$dates .= ' ('.$planned_downtime['periodically_time_from'];
$dates .= '-'.$planned_downtime['periodically_time_to'].')';
break;
}
break;
}
return $dates;
}
/**
* Get real period in SLA subtracting worktime period.
* Get if is working in the first point
* Get time between first point and
*
* @param int Period to check the SLA compliance.
* @param int Date_end date end the sla compliace interval
* @param int Working Time start
* @param int Working Time end
*
* @return array (int fixed SLA period, bool inside working time)
* found
*/
function reporting_get_agentmodule_sla_day_period($period, $date_end, $wt_start='00:00:00', $wt_end='23:59:59')
{
$date_start = ($date_end - $period);
// Converts to timestamp
$human_date_end = date('H:i:s', $date_end);
$human_date_start = date('H:i:s', $date_start);
// Store into an array the points
// "s" start SLA interval point
// "e" end SLA interval point
// "f" start worktime interval point (from)
// "t" end worktime interval point (to)
$tp = [
's' => strtotime($human_date_start),
'e' => strtotime($human_date_end),
'f' => strtotime($wt_start),
't' => strtotime($wt_end),
];
asort($tp);
$order = '';
foreach ($tp as $type => $time) {
$order .= $type;
}
$period_reduced = $period;
$start_working = true;
$datelimit_increased = 0;
// Special case. If $order = "seft" and start time == end time it should be treated like "esft"
if (($period > 0) and ($human_date_end == $human_date_start) and ($order == 'seft')) {
$order = 'esft';
}
// Discriminates the cases depends what time point is higher than other
switch ($order) {
case 'setf':
case 'etfs':
case 'tfse':
case 'fset':
// Default $period_reduced
// Default $start_working
// Default $datelimit_increased
break;
case 'stef':
case 'tefs':
case 'fste':
$period_reduced = ($period - ($tp['e'] - $tp['t']));
// Default $start_working
// Default $datelimit_increased
break;
case 'stfe':
case 'estf':
case 'tfes':
$period_reduced = ($period - ($tp['f'] - $tp['t']));
// Default $start_working
// Default $datelimit_increased
break;
case 'tsef':
case 'seft':
case 'ftse':
case 'efts':
$period_reduced = -1;
$start_working = false;
// Default $datelimit_increased
break;
case 'tsfe':
case 'etsf':
case 'sfet':
$period_reduced = ($period - ($tp['f'] - $tp['s']));
$start_working = false;
$datelimit_increased = ($tp['f'] - $tp['s']);
break;
case 'efst':
$period_reduced = ($tp['t'] - $tp['s']);
// Default $start_working
// Default $datelimit_increased
break;
case 'fest':
$period_reduced = (($tp['t'] - $tp['s']) + ($tp['e'] - $tp['f']));
// Default $start_working
// Default $datelimit_increased
break;
case 'tesf':
$period_reduced = (SECONDS_1DAY - ($tp['f'] - $tp['t']));
$start_working = false;
$datelimit_increased = ($tp['f'] - $tp['s']);
break;
case 'sfte':
case 'esft':
$period_reduced = ($tp['t'] - $tp['f']);
$start_working = false;
$datelimit_increased = ($tp['f'] - $tp['s']);
break;
case 'ftes':
$period_reduced = ($tp['t'] - $tp['f']);
$start_working = false;
$datelimit_increased = ($tp['f'] + SECONDS_1DAY - $tp['s']);
break;
case 'fets':
$period_reduced = ($tp['e'] - $tp['f']);
$start_working = false;
$datelimit_increased = ($tp['f'] + SECONDS_1DAY - $tp['s']);
break;
default:
// Default $period_reduced
// Default $start_working
// Default $datelimit_increased
break;
}
return [
$period_reduced,
$start_working,
$datelimit_increased,
];
}
/**
* Get working time SLA in timestamp form. Get all items and discard previous not necessaries
*
* @param int Period to check the SLA compliance.
* @param int Date_end date end the sla compliace interval
* @param int Working Time start
* @param int Working Time end
*
* @return array work time points
* found
*/
function reporting_get_agentmodule_sla_working_timestamp($period, $date_end, $wt_start='00:00:00', $wt_end='23:59:59')
{
$date_previous_day = ($date_end - SECONDS_1DAY);
$wt = [];
// Calculate posibles data points
$relative_date_end = strtotime(date('H:i:s', $date_end));
$relative_00_00_00 = strtotime('00:00:00');
$relative_wt_start = (strtotime($wt_start) - $relative_00_00_00);
$relative_wt_end = (strtotime($wt_end) - $relative_00_00_00);
$absolute_previous_00_00_00 = ($date_previous_day - ($relative_date_end - $relative_00_00_00));
$absolute_00_00_00 = ($date_end - ($relative_date_end - $relative_00_00_00));
array_push($wt, $absolute_previous_00_00_00);
if ($relative_wt_start < $relative_wt_end) {
array_push($wt, ($absolute_previous_00_00_00 + $relative_wt_start));
array_push($wt, ($absolute_previous_00_00_00 + $relative_wt_end));
array_push($wt, ($absolute_00_00_00 + $relative_wt_start));
array_push($wt, ($absolute_00_00_00 + $relative_wt_end));
} else {
array_push($wt, ($absolute_previous_00_00_00 + $relative_wt_end));
array_push($wt, ($absolute_previous_00_00_00 + $relative_wt_start));
array_push($wt, ($absolute_00_00_00 + $relative_wt_end));
array_push($wt, ($absolute_00_00_00 + $relative_wt_start));
}
array_push($wt, ($absolute_00_00_00 + SECONDS_1DAY));
// Discard outside period time points
$date_start = ($date_end - $period);
$first_time = array_shift($wt);
while ($first_time < $date_start) {
if (empty($wt)) {
return $wt;
}
$first_time = array_shift($wt);
}
array_unshift($wt, $first_time);
return $wt;
}
function reporting_label_macro($item, $label)
{
switch ($item['type']) {
case 'event_report_agent':
case 'alert_report_agent':
case 'agent_configuration':
case 'event_report_log':
if (preg_match('/_agent_/', $label)) {
$agent_name = agents_get_alias($item['id_agent']);
$label = str_replace('_agent_', $agent_name, $label);
}
if (preg_match('/_agentdescription_/', $label)) {
$agent_name = agents_get_description($item['id_agent']);
$label = str_replace('_agentdescription_', $agent_name, $label);
}
if (preg_match('/_agentgroup_/', $label)) {
$agent_name = groups_get_name(agents_get_agent_group($item['id_agent']), true);
$label = str_replace('_agentgroup_', $agent_name, $label);
}
if (preg_match('/_address_/', $label)) {
$agent_name = agents_get_address($item['id_agent']);
$label = str_replace('_address_', $agent_name, $label);
}
break;
case 'simple_graph':
case 'module_histogram_graph':
case 'custom_graph':
case 'simple_baseline_graph':
case 'event_report_module':
case 'alert_report_module':
case 'historical_data':
case 'sumatory':
case 'database_serialized':
case 'monitor_report':
case 'min_value':
case 'max_value':
case 'avg_value':
case 'projection_graph':
case 'prediction_date':
case 'TTRT':
case 'TTO':
case 'MTBF':
case 'MTTR':
case 'automatic_graph':
if (preg_match('/_agent_/', $label)) {
if (isset($item['agents']) && count($item['agents']) > 1) {
$agent_name = count($item['agents']).__(' agents');
} else {
$agent_name = agents_get_alias($item['id_agent']);
}
$label = str_replace('_agent_', $agent_name, $label);
}
if (preg_match('/_agentdescription_/', $label)) {
if (count($item['agents']) > 1) {
$agent_name = '';
} else {
$agent_name = agents_get_description($item['id_agent']);
}
$label = str_replace('_agentdescription_', $agent_name, $label);
}
if (preg_match('/_agentgroup_/', $label)) {
if (count($item['agents']) > 1) {
$agent_name = '';
} else {
$agent_name = groups_get_name(agents_get_agent_group($item['id_agent']), true);
}
$label = str_replace('_agentgroup_', $agent_name, $label);
}
if (preg_match('/_address_/', $label)) {
if (count($item['agents']) > 1) {
$agent_name = '';
} else {
$agent_name = agents_get_address($item['id_agent']);
}
$label = str_replace('_address_', $agent_name, $label);
}
if (preg_match('/_module_/', $label)) {
if ($item['modules'] > 1) {
$module_name = $item['modules'].__(' modules');
} else {
$module_name = modules_get_agentmodule_name($item['id_agent_module']);
}
$label = str_replace('_module_', $module_name, $label);
}
if (preg_match('/_moduledescription_/', $label)) {
if ($item['modules'] > 1) {
$module_description = '';
} else {
$module_description = modules_get_agentmodule_descripcion($item['id_agent_module']);
}
$label = str_replace('_moduledescription_', $module_description, $label);
}
break;
}
return $label;
}
/**
* @brief Calculates the SLA compliance value given an sla array
*
* @param Array With keys time_ok, time_error, time_downtime and time_unknown
* @return SLA Return the compliance value.
*/
function reporting_sla_get_compliance_from_array($sla_array)
{
$time_compliance = ($sla_array['time_ok'] + $sla_array['time_unknown'] + $sla_array['time_downtime']);
$time_total_working = ($time_compliance + $sla_array['time_error']);
return $time_compliance == 0 ? 0 : (($time_compliance / $time_total_working) * 100);
}
/**
* @brief Calculates if an SLA array is not init
*
* @param Array With keys time_ok, time_error, time_downtime and time_unknown
* @return boolean True if not init
*/
function reporting_sla_is_not_init_from_array($sla_array)
{
if ($sla_array['time_total'] == 0) {
return false;
}
return $sla_array['time_not_init'] == $sla_array['time_total'];
}
/**
* @brief Calculates if an SLA array is ignored
*
* @param Array With keys time_ok, time_error, time_downtime and time_unknown
* @return boolean True if igonred time
*/
function reporting_sla_is_ignored_from_array($sla_array)
{
if ($sla_array['time_total'] > 0) {
return false;
}
return $sla_array['time_not_init'] == 0;
}
/**
* @brief Given a period, get the SLA status of the period.
*
* @param Array An array with all times to calculate the SLA
* @param int Priority mode. Setting this parameter to REPORT_PRIORITY_MODE_OK
* and there is no critical in this period, return an OK without look for
* not init, downtimes, unknown and others...
*
* @return integer Status
*/
function reporting_sla_get_status_period($sla_times, $priority_mode=REPORT_PRIORITY_MODE_OK)
{
if ($sla_times['time_error'] > 0) {
return REPORT_STATUS_ERR;
}
if ($priority_mode == REPORT_PRIORITY_MODE_OK && $sla_times['time_ok'] > 0) {
return REPORT_STATUS_OK;
}
if ($sla_times['time_out'] > 0) {
return REPORT_STATUS_IGNORED;
}
if ($sla_times['time_downtime'] > 0) {
return REPORT_STATUS_DOWNTIME;
}
if ($sla_times['time_unknown'] > 0) {
return REPORT_STATUS_UNKNOWN;
}
if ($sla_times['time_not_init'] > 0) {
return REPORT_STATUS_NOT_INIT;
}
if ($sla_times['time_ok'] > 0) {
return REPORT_STATUS_OK;
}
return REPORT_STATUS_IGNORED;
}
/**
* @brief Translate the status to the color to graph_sla_slicebar function
*
* @param int The status in number
* @return integer The index of color array to graph_sla_slicebar function
*/
function reporting_translate_sla_status_for_graph($status)
{
$sts = [
REPORT_STATUS_ERR => 3,
REPORT_STATUS_OK => 1,
REPORT_STATUS_UNKNOWN => 4,
REPORT_STATUS_NOT_INIT => 6,
REPORT_STATUS_DOWNTIME => 5,
REPORT_STATUS_IGNORED => 7,
];
return $sts[$status];
}
/**
* Print header to report pdf and add page break
*
* @param string $title Title of report.
* @param string $description Description of report.
*
* @return html Return table of header.
*/
function reporting_header_table_for_pdf(string $title='', string $description='')
{
$result_pdf .= '';
$result_pdf .= '
';
$result_pdf .= '
';
$result_pdf .= '
';
$result_pdf .= $title;
$result_pdf .= '
';
$result_pdf .= '
';
$result_pdf .= $description;
$result_pdf .= '
';
return $result_pdf;
}
/**
* Build the required data to build network traffic top N report
*
* @param int Period (time window).
* @param array Information about the item of report.
* @param bool Pdf or not
*
* @return array With report presentation info and report data.
*/
function reporting_nt_top_n_report($period, $content, $pdf)
{
$return = [];
$return['type'] = 'nt_top_n';
$return['title'] = $content['name'];
$return['description'] = $content['description'];
// Get the data sent and received
$return['data'] = [];
$start_time = ($period['datetime'] - (int) $content['period']);
$return['data']['send'] = network_matrix_get_top(
$content['top_n_value'],
true,
$start_time,
$period['datetime']
);
$return['data']['recv'] = network_matrix_get_top(
$content['top_n_value'],
false,
$start_time,
$period['datetime']
);
return $return;
}