$b['order']);
}
);
foreach ($contents as $content) {
$content['name'] = io_safe_input($content['name']);
$content['description'] = io_safe_input($content['description']);
if (!empty($content['id_agent_module']) && !empty($content['id_agent'])
&& tags_has_user_acl_tags($config['id_user'])
) {
$where_tags = tags_get_acl_tags(
$config['id_user'],
$id_groups,
'AR',
'module_condition',
'AND',
'tagente_modulo',
false,
[],
true
);
$sql_tags_join = 'INNER JOIN tagente ON tagente.id_agente = t1.id_agente
INNER JOIN ttag_module ON ttag_module.id_agente_modulo = t1.id_agente_modulo
LEFT JOIN tagent_secondary_group tasg ON tagente.id_agente = tasg.id_agent';
$sql = sprintf(
'SELECT count(*) FROM tagente_modulo t1
%s WHERE t1.delete_pending = 0 AND t1.id_agente_modulo = '.$content['id_agent_module'].'
AND t1.id_agente = '.$content['id_agent'].' %s',
$sql_tags_join,
$where_tags
);
$result_tags = db_get_value_sql($sql);
if (!$result_tags) {
continue;
}
}
$server_name = $content['server_name'];
// General reports with 0 period means last value
// Avoid to overwrite it by template value.
$general_last_value = false;
if ($content['type'] === 'general' && $content['period'] == 0) {
$general_last_value = true;
}
if (!empty($period) && $general_last_value === false) {
$content['period'] = $period;
}
$content['style'] = json_decode(io_safe_output($content['style']), true);
$graphs_to_macro = db_get_all_rows_field_filter(
'tgraph_source',
'id_graph',
$content['id_gs']
);
if ($graphs_to_macro === false) {
$graphs_to_macro = [];
}
$modules_to_macro = 0;
$agents_to_macro = [];
foreach ($graphs_to_macro as $graph_item) {
$modules_to_macro++;
if (in_array('label', $content['style'])) {
if ($content['id_agent'] == 0) {
// Metaconsole connection.
if ($metaconsole_on && $server_name != '') {
$connection = metaconsole_get_connection($server_name);
if (!metaconsole_load_external_db($connection)) {
continue;
}
}
array_push(
$agents_to_macro,
modules_get_agentmodule_agent(
$graph_item['id_agent_module']
)
);
if ($metaconsole_on) {
// Restore db connection.
metaconsole_restore_db();
}
}
}
}
$agents_to_macro_aux = [];
foreach ($agents_to_macro as $ag) {
if (!in_array($ag, $agents_to_macro_aux)) {
$agents_to_macro_aux[$ag] = $ag;
}
}
$agents_to_macro = $agents_to_macro_aux;
if (!empty($report) && $from_template) {
$agents_to_macro = $content['id_agent'];
}
if (isset($content['style']['name_label'])) {
$server_name = $content['server_name'];
$metaconsole_on = is_metaconsole();
// Metaconsole connection.
if ($metaconsole_on && $server_name != '') {
$connection = metaconsole_get_connection($server_name);
if (!metaconsole_load_external_db($connection)) {
continue;
}
}
// Add macros name.
$items_label = [];
$items_label['type'] = $content['type'];
$items_label['id_agent'] = $content['id_agent'];
$items_label['id_agent_module'] = $content['id_agent_module'];
$items_label['modules'] = $modules_to_macro;
$items_label['agents'] = $agents_to_macro;
$items_label['visual_format'] = null;
$items_label['agent_description'] = agents_get_description(
$content['id_agent']
);
$items_label['agent_group'] = agents_get_agent_group(
$content['id_agent']
);
$items_label['agent_address'] = agents_get_address(
$content['id_agent']
);
$items_label['agent_alias'] = agents_get_alias(
$content['id_agent']
);
// This is for metaconsole. It is an array with modules and server (id node).
if (is_array($content['id_agent_module'])) {
$modules_server_array = $content['id_agent_module'];
$modules_array = [];
foreach ($modules_server_array as $value) {
if (is_array($value) === true) {
$modules_array[] = $value['module'];
} else {
$modules_array[] = $value;
}
}
$content['id_agent_module'] = $modules_array;
}
if (is_array($content['id_agent'])
&& count($content['id_agent']) != 1
) {
$content['style']['name_label'] = str_replace(
'_agent_',
count($content['id_agent']).__(' agents'),
$content['style']['name_label']
);
}
if (is_array($content['id_agent_module'])
&& count($content['id_agent_module']) != 1
) {
$content['style']['name_label'] = str_replace(
'_module_',
count($content['id_agent_module']).__(' modules'),
$content['style']['name_label']
);
}
if ($metaconsole_on) {
// Restore db connection.
metaconsole_restore_db();
}
$content['name'] = reporting_label_macro(
$items_label,
($content['style']['name_label'] ?? '')
);
}
switch (reporting_get_type($content)) {
case 'simple_graph':
$report['contents'][] = reporting_simple_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart
);
break;
case 'event_report_log':
$report['contents'][] = reporting_log(
$report,
$content
);
break;
case 'increment':
$report['contents'][] = reporting_increment(
$report,
$content
);
break;
case 'general':
$report['contents'][] = io_safe_output(
reporting_general(
$report,
$content
)
);
break;
case 'availability':
$report['contents'][] = io_safe_output(
reporting_availability(
$report,
$content,
$date,
$time
)
);
break;
case 'availability_graph':
$report['contents'][] = io_safe_output(
reporting_availability_graph(
$report,
$content,
$pdf
)
);
break;
case 'sql':
$report['contents'][] = reporting_sql(
$report,
$content
);
break;
case 'custom_graph':
$report['contents'][] = reporting_custom_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'custom_graph',
$pdf
);
break;
case 'automatic_graph':
$report['contents'][] = reporting_custom_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'automatic_graph',
$pdf
);
break;
case 'text':
$report['contents'][] = reporting_text(
$report,
$content
);
break;
case 'url':
$report['contents'][] = reporting_url(
$report,
$content,
$type
);
break;
case 'max_value':
$report['contents'][] = reporting_value(
$report,
$content,
'max',
$pdf
);
break;
case 'avg_value':
$report['contents'][] = reporting_value(
$report,
$content,
'avg',
$pdf
);
break;
case 'min_value':
$report['contents'][] = reporting_value(
$report,
$content,
'min',
$pdf
);
break;
case 'sumatory':
$report['contents'][] = reporting_value(
$report,
$content,
'sum',
$pdf
);
break;
case 'historical_data':
$report['contents'][] = reporting_historical_data(
$report,
$content
);
break;
case 'agent_configuration':
$report['contents'][] = io_safe_output(
reporting_agent_configuration(
$report,
$content
)
);
break;
case 'projection_graph':
$report['contents'][] = reporting_projection_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
$pdf
);
break;
case 'prediction_date':
$report['contents'][] = reporting_prediction_date(
$report,
$content
);
break;
case 'simple_baseline_graph':
$report['contents'][] = reporting_simple_baseline_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart
);
break;
case 'netflow_area':
$report['contents'][] = reporting_netflow(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'netflow_area',
$pdf
);
break;
break;
case 'netflow_data':
$report['contents'][] = reporting_netflow(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'netflow_data',
$pdf
);
break;
case 'netflow_summary':
$report['contents'][] = reporting_netflow(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'netflow_summary',
$pdf
);
break;
case 'netflow_top_N':
$report['contents'][] = reporting_netflow(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'netflow_top_N',
$pdf
);
break;
case 'monitor_report':
$report['contents'][] = reporting_monitor_report(
$report,
$content
);
break;
case 'sql_graph_vbar':
$report['contents'][] = reporting_sql_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'sql_graph_vbar'
);
break;
case 'sql_graph_hbar':
$report['contents'][] = reporting_sql_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'sql_graph_hbar'
);
break;
case 'sql_graph_pie':
$report['contents'][] = reporting_sql_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
'sql_graph_pie'
);
break;
case 'alert_report_module':
$report['contents'][] = reporting_alert_report_module(
$report,
$content
);
break;
case 'alert_report_agent':
$report['contents'][] = reporting_alert_report_agent(
$report,
$content
);
break;
case 'alert_report_group':
$report['contents'][] = reporting_alert_report_group(
$report,
$content
);
break;
case 'network_interfaces_report':
$report['contents'][] = reporting_network_interfaces_report(
$report,
$content,
$type,
$pdf
);
break;
case 'custom_render':
$report['contents'][] = reporting_custom_render(
$report,
$content,
$type,
$pdf
);
break;
case 'group_configuration':
$report['contents'][] = reporting_group_configuration(
$report,
$content
);
break;
case 'database_serialized':
$report['contents'][] = reporting_database_serialized(
$report,
$content
);
break;
case 'last_value':
$report['contents'][] = reporting_last_value(
$report,
$content,
$datetime,
$period
);
break;
case 'permissions_report':
$report['contents'][] = reporting_permissions(
$report,
$content
);
break;
case 'group_report':
$report['contents'][] = reporting_group_report(
$report,
$content
);
break;
case 'exception':
$report['contents'][] = reporting_exception(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart
);
break;
case 'agent_module':
$report['contents'][] = reporting_agent_module(
$report,
$content
);
break;
case 'agent_module_status':
$report['contents'][] = reporting_agent_module_status(
$report,
$content
);
break;
case 'alert_report_actions':
$report['contents'][] = reporting_alert_report_actions(
$report,
$content
);
break;
case 'agents_inventory':
$report['contents'][] = reporting_agents_inventory(
$report,
$content
);
break;
case 'modules_inventory':
$report['contents'][] = reporting_modules_inventory(
$report,
$content
);
break;
case 'inventory':
$report['contents'][] = reporting_inventory(
$report,
$content,
$type
);
break;
case 'inventory_changes':
$report['contents'][] = reporting_inventory_changes(
$report,
$content,
$type
);
break;
case 'IPAM_network':
$report['contents'][] = reporting_ipam(
$report,
$content
);
break;
case 'agent_detailed_event':
case 'event_report_agent':
$report_control = io_safe_output(
reporting_event_report_agent(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart
)
);
if ($report_control['total_events'] == 0 && $content['hide_no_data'] == 1) {
break;
}
$report['contents'][] = $report_control;
break;
case 'event_report_module':
$report_control = reporting_event_report_module(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
$pdf
);
if ($report_control['total_events'] == 0 && $content['hide_no_data'] == 1) {
break;
}
$report['contents'][] = $report_control;
break;
case 'event_report_group':
$report_control = reporting_event_report_group(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart
);
if ($report_control['total_events'] == 0 && $content['hide_no_data'] == 1) {
break;
}
$report['contents'][] = $report_control;
break;
case 'top_n':
$report['contents'][] = reporting_event_top_n(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart
);
break;
case 'SLA':
$report['contents'][] = reporting_SLA(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart
);
break;
case 'SLA_monthly':
$report['contents'][] = reporting_enterprise_sla_monthly_refactoriced(
$report,
$content
);
break;
case 'SLA_weekly':
$report['contents'][] = reporting_enterprise_sla_weekly(
$report,
$content
);
break;
case 'SLA_hourly':
$report['contents'][] = reporting_enterprise_sla_hourly(
$report,
$content
);
break;
case 'SLA_services':
$report['contents'][] = reporting_enterprise_sla_services_refactoriced(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
$pdf
);
break;
case 'module_histogram_graph':
$report['contents'][] = reporting_module_histogram_graph(
$report,
$content,
$pdf
);
break;
case 'ncm':
$report['contents'][] = reporting_ncm_config(
$report,
$content,
$pdf
);
break;
default:
// Default.
break;
}
$index_content++;
}
return reporting_check_structure_report($report);
}
/**
* Report SLA.
*
* @param array $report Data report.
* @param array $content Content for report.
* @param null|string $type Type report.
* @param null|integer $force_width_chart Size.
* @param null|integer $force_height_chart Size.
*
* @return array Return array for draw report SLA.
*/
function reporting_SLA(
$report,
$content,
$type='dinamic',
$force_width_chart=null,
$force_height_chart=null
) {
global $config;
$return = [];
$return['type'] = 'SLA';
if (empty($content['name'])) {
$content['name'] = __('S.L.A.');
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
// Get chart.
reporting_set_conf_charts(
$width,
$height,
$only_image,
$type,
$content,
$ttl
);
if (!empty($force_width_chart)) {
$width = $force_width_chart;
}
if (!empty($force_height_chart)) {
$height = $force_height_chart;
}
$return['id_rc'] = $content['id_rc'];
$edge_interval = 10;
if (empty($content['subitems'])) {
if (is_metaconsole()) {
metaconsole_restore_db();
}
$slas = db_get_all_rows_field_filter(
'treport_content_sla_combined',
'id_report_content',
$content['id_rc']
);
} else {
$slas = $content['subitems'];
}
if (empty($slas)) {
$return['failed'] = __('There are no SLAs defined');
} else {
include_once $config['homedir'].'/include/functions_planned_downtimes.php';
$metaconsole_on = is_metaconsole();
// Checking if needed to show graph or table.
if ($content['show_graph'] == 0 || $content['show_graph'] == 1) {
$show_table = 1;
} else {
$show_table = 0;
}
if ($content['show_graph'] == 1 || $content['show_graph'] == 2) {
$show_graphs = 1;
} else {
$show_graphs = 0;
}
$urlImage = ui_get_full_url(false, true, false, false);
$sla_failed = false;
$total_SLA = 0;
$total_result_SLA = 'ok';
$sla_showed = [];
$sla_showed_values = [];
if ((bool) $content['compare_work_time'] === true) {
$slas_compare = [];
foreach ($slas as $sla) {
$sla['compare'] = 1;
$slas_compare[] = $sla;
$sla['compare'] = 0;
$slas_compare[] = $sla;
}
$slas = $slas_compare;
}
foreach ($slas as $sla) {
$server_name = $sla['server_name'];
// Metaconsole connection.
if ($metaconsole_on && $server_name != '') {
$connection = metaconsole_get_connection($server_name);
if (!metaconsole_load_external_db($connection)) {
continue;
}
}
if (modules_is_disable_agent($sla['id_agent_module'])
|| modules_is_not_init($sla['id_agent_module'])
) {
if ($metaconsole_on) {
// Restore db connection.
metaconsole_restore_db();
}
continue;
}
// Controller min and max == 0 then dinamic min and max critical.
$dinamic_text = 0;
if ($sla['sla_min'] == 0 && $sla['sla_max'] == 0) {
$sla['sla_min'] = null;
$sla['sla_max'] = null;
$dinamic_text = __('Dynamic');
}
// Controller inverse interval.
$inverse_interval = 0;
if ((isset($sla['sla_max'])) && (isset($sla['sla_min']))) {
if ($sla['sla_max'] < $sla['sla_min']) {
$content_sla_max = $sla['sla_max'];
$sla['sla_max'] = $sla['sla_min'];
$sla['sla_min'] = $content_sla_max;
$inverse_interval = 1;
$dinamic_text = __('Inverse');
}
}
// For graph slice for module-interval, if not slice=0.
if ($show_graphs) {
$module_interval = modules_get_interval(
$sla['id_agent_module']
);
$slice = ($content['period'] / $module_interval);
} else {
$slice = 1;
}
// Call functions sla.
$sla_array = [];
if (isset($sla['compare']) === false
|| empty($sla['compare']) === true
) {
$sla_array = reporting_advanced_sla(
$sla['id_agent_module'],
($report['datetime'] - $content['period']),
$report['datetime'],
$sla['sla_min'],
$sla['sla_max'],
$inverse_interval,
[
'1' => $content['sunday'],
'2' => $content['monday'],
'3' => $content['tuesday'],
'4' => $content['wednesday'],
'5' => $content['thursday'],
'6' => $content['friday'],
'7' => $content['saturday'],
],
$content['time_from'],
$content['time_to'],
$slice
);
} else {
$sla_array = reporting_advanced_sla(
$sla['id_agent_module'],
($report['datetime'] - $content['period']),
$report['datetime'],
$sla['sla_min'],
$sla['sla_max'],
$inverse_interval,
[
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1,
'7' => 1,
],
'00:00:00',
'00:00:00',
$slice
);
}
if ($metaconsole_on) {
// Restore db connection.
metaconsole_restore_db();
}
$server_name = $sla['server_name'];
// Metaconsole connection.
if ($metaconsole_on && $server_name != '') {
$connection = metaconsole_get_connection($server_name);
if (metaconsole_connect($connection) != NOERR) {
continue;
}
}
if ($show_graphs) {
$planned_downtimes = reporting_get_planned_downtimes_intervals(
$sla['id_agent_module'],
($report['datetime'] - $content['period']),
$report['datetime']
);
if ((is_array($planned_downtimes))
&& (count($planned_downtimes) > 0)
) {
// Sort retrieved planned downtimes.
usort(
$planned_downtimes,
function ($a, $b) {
$a = intval($a['date_from']);
$b = intval($b['date_from']);
if ($a == $b) {
return 0;
}
return ($a < $b) ? (-1) : 1;
}
);
// Compress (overlapped) planned downtimes.
$npd = count($planned_downtimes);
for ($i = 0; $i < $npd; $i++) {
if (isset($planned_downtimes[($i + 1)])) {
if ($planned_downtimes[$i]['date_to'] >= $planned_downtimes[($i + 1)]['date_from']) {
// Merge.
$planned_downtimes[$i]['date_to'] = $planned_downtimes[($i + 1)]['date_to'];
array_splice($planned_downtimes, ($i + 1), 1);
$npd--;
}
}
}
} else {
$planned_downtimes = null;
}
}
$data = [];
$data['agent'] = io_safe_output(
modules_get_agentmodule_agent_alias(
$sla['id_agent_module']
)
);
$data['module'] = io_safe_output(
modules_get_agentmodule_name(
$sla['id_agent_module']
)
);
$data['max'] = $sla['sla_max'];
$data['min'] = $sla['sla_min'];
$data['sla_limit'] = $sla['sla_limit'];
$data['dinamic_text'] = $dinamic_text;
if (isset($sla['compare']) === false
|| empty($sla['compare']) === true
) {
$data['compare'] = false;
} else {
$data['compare'] = true;
}
if (isset($sla_array[0])) {
$data['time_total'] = 0;
$data['time_ok'] = 0;
$data['time_error'] = 0;
$data['time_unknown'] = 0;
$data['time_not_init'] = 0;
$data['time_downtime'] = 0;
$data['checks_total'] = 0;
$data['checks_ok'] = 0;
$data['checks_error'] = 0;
$data['checks_unknown'] = 0;
$data['checks_not_init'] = 0;
$raw_graph = [];
$i = 0;
foreach ($sla_array as $value_sla) {
$data['time_total'] += $value_sla['time_total'];
$data['time_ok'] += $value_sla['time_ok'];
$data['time_error'] += $value_sla['time_error'];
$data['time_unknown'] += $value_sla['time_unknown'];
$data['time_downtime'] += $value_sla['time_downtime'];
$data['time_not_init'] += $value_sla['time_not_init'];
$data['checks_total'] += $value_sla['checks_total'];
$data['checks_ok'] += $value_sla['checks_ok'];
$data['checks_error'] += $value_sla['checks_error'];
$data['checks_unknown'] += $value_sla['checks_unknown'];
$data['checks_not_init'] += $value_sla['checks_not_init'];
// Generate raw data for graph.
if ($value_sla['time_total'] != 0) {
if ($value_sla['time_error'] > 0) {
// ERR.
$raw_graph[$i]['data'] = 3;
} else if ($value_sla['time_unknown'] > 0) {
// UNKNOWN.
$raw_graph[$i]['data'] = 4;
} else if ($value_sla['time_not_init'] == $value_sla['time_total']
) {
// NOT INIT.
$raw_graph[$i]['data'] = 6;
} else {
$raw_graph[$i]['data'] = 1;
}
} else {
$raw_graph[$i]['data'] = 7;
}
$raw_graph[$i]['utimestamp'] = (
$value_sla['date_to'] - $value_sla['date_from']);
if (isset($planned_downtimes)) {
foreach ($planned_downtimes as $pd) {
if (($value_sla['date_from'] >= $pd['date_from'])
&& ($value_sla['date_to'] <= $pd['date_to'])
) {
$raw_graph[$i]['data'] = 5;
// In scheduled downtime.
break;
}
}
}
$i++;
}
$data['sla_value'] = reporting_sla_get_compliance_from_array(
$data
);
$data['sla_fixed'] = sla_truncate(
$data['sla_value'],
$config['graph_precision']
);
} else {
// Show only table not divider in slice for defect slice=1.
$data['time_total'] = $sla_array['time_total'];
$data['time_ok'] = $sla_array['time_ok'];
$data['time_error'] = $sla_array['time_error'];
$data['time_unknown'] = $sla_array['time_unknown'];
$data['time_downtime'] = $sla_array['time_downtime'];
$data['time_not_init'] = $sla_array['time_not_init'];
$data['checks_total'] = $sla_array['checks_total'];
$data['checks_ok'] = $sla_array['checks_ok'];
$data['checks_error'] = $sla_array['checks_error'];
$data['checks_unknown'] = $sla_array['checks_unknown'];
$data['checks_not_init'] = $sla_array['checks_not_init'];
$data['sla_value'] = $sla_array['SLA'];
$data['sla_fixed'] = $sla_array['sla_fixed'];
}
// Checks whether or not it meets the SLA.
if ($data['sla_value'] >= $sla['sla_limit']) {
$data['sla_status'] = 1;
$sla_failed = false;
} else {
$sla_failed = true;
$data['sla_status'] = 0;
}
// Do not show right modules if 'only_display_wrong' is active.
if ($content['only_display_wrong'] && $sla_failed == false) {
continue;
}
// Find order.
$data['order'] = $data['sla_value'];
if ($show_table) {
$return['data'][] = $data;
}
// Slice graphs calculation.
if ($show_graphs) {
$data_init = -1;
$acum = 0;
$sum = 0;
$array_result = [];
$i = 0;
foreach ($raw_graph as $key => $value) {
if ($data_init == -1) {
$data_init = $value['data'];
$acum = $value['utimestamp'];
$sum = $value['data'];
} else {
if ($data_init == $value['data']) {
$acum = ($acum + $value['utimestamp']);
$sum = ($sum + $value['real_data']);
} else {
$array_result[$i]['data'] = $data_init;
$array_result[$i]['utimestamp'] = $acum;
$array_result[$i]['real_data'] = $sum;
$i++;
$data_init = $value['data'];
$acum = $value['utimestamp'];
$sum = $value['real_data'];
}
}
}
$array_result[$i]['data'] = $data_init;
$array_result[$i]['utimestamp'] = $acum;
$array_result[$i]['real_data'] = $sum;
$dataslice = [];
$dataslice['agent'] = io_safe_output(
modules_get_agentmodule_agent_alias(
$sla['id_agent_module']
)
);
$dataslice['module'] = io_safe_output(
modules_get_agentmodule_name(
$sla['id_agent_module']
)
);
if (isset($sla['compare']) === false
|| empty($sla['compare']) === true
) {
$dataslice['compare'] = false;
} else {
$dataslice['compare'] = true;
}
$dataslice['sla_value'] = $data['sla_value'];
$dataslice['order'] = $data['sla_value'];
$dataslice['chart'] = graph_sla_slicebar(
$sla['id_agent_module'],
$content['period'],
$sla['sla_min'],
$sla['sla_max'],
$report['datetime'],
$content,
$content['time_from'],
$content['time_to'],
100,
((bool) $dataslice['compare'] === true) ? 50 : 80,
$urlImage,
$ttl,
$array_result,
false
);
$return['charts'][] = $dataslice;
}
if ($metaconsole_on) {
// Restore db connection.
metaconsole_restore_db();
}
}
if ($content['top_n'] == 2) {
// SLA items sorted descending.
arsort($return['data']['']);
} else if ($content['top_n'] == 1) {
// SLA items sorted ascending.
asort($sla_showed_values);
}
// Order data for ascending or descending.
if ($content['top_n'] != 0) {
switch ($content['top_n']) {
case 1:
// Order tables.
$temp = [];
foreach ($return['data'] as $row) {
$i = 0;
foreach ($temp as $t_row) {
if ($row['sla_value'] < $t_row['order']) {
break;
}
$i++;
}
array_splice($temp, $i, 0, [$row]);
}
$return['data'] = $temp;
// Order graphs.
$temp = [];
foreach ($return['charts'] as $row) {
$i = 0;
foreach ($temp as $t_row) {
if ($row['sla_value'] < $t_row['order']) {
break;
}
$i++;
}
array_splice($temp, $i, 0, [$row]);
}
$return['charts'] = $temp;
break;
case 2:
// Order tables.
$temp = [];
foreach ($return['data'] as $row) {
$i = 0;
foreach ($temp as $t_row) {
if ($row['sla_value'] > $t_row['order']) {
break;
}
$i++;
}
array_splice($temp, $i, 0, [$row]);
}
$return['data'] = $temp;
// Order graph.
$temp = [];
foreach ($return['charts'] as $row) {
$i = 0;
foreach ($temp as $t_row) {
if ($row['sla_value'] > $t_row['order']) {
break;
}
$i++;
}
array_splice($temp, $i, 0, [$row]);
}
$return['charts'] = $temp;
break;
default:
// Default.
break;
}
}
}
return reporting_check_structure_content($return);
}
function reporting_event_top_n(
$report,
$content,
$type='dinamic',
$force_width_chart=null,
$force_height_chart=null
) {
global $config;
$return['type'] = 'top_n';
if (empty($content['name'])) {
$content['name'] = __('Top N');
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$top_n = $content['top_n'];
switch ($top_n) {
case REPORT_TOP_N_MAX:
$type_top_n = __('Max');
break;
case REPORT_TOP_N_MIN:
$type_top_n = __('Min');
break;
case REPORT_TOP_N_AVG:
default:
// If nothing is selected then it will be shown the average data.
$type_top_n = __('Avg');
break;
}
$return['subtitle'] = __('Top %d', $content['top_n_value']).' - '.$type_top_n;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$order_uptodown = $content['order_uptodown'];
$top_n_value = $content['top_n_value'];
$show_graph = $content['show_graph'];
$return['top_n'] = $content['top_n_value'];
if (empty($content['subitems'])) {
// Get all the related data.
$sql = sprintf(
'SELECT id_agent_module, server_name
FROM treport_content_item
WHERE id_report_content = %d',
$content['id_rc']
);
$tops = db_get_all_rows_sql($sql);
if ($tops === false) {
$tops = [];
}
// REGEXP.
$text_agent = '';
if (isset($content['style']['text_agent']) === true
&& empty($content['style']['text_agent']) === false
) {
$text_agent = base64_decode($content['style']['text_agent']);
}
$text_agent_module = '';
if (isset($content['style']['text_agent_module']) === true
&& empty($content['style']['text_agent_module']) === false
) {
$text_agent_module = base64_decode($content['style']['text_agent_module']);
}
$modules_regex = [];
if (empty($text_agent) === false) {
if (is_metaconsole() === true) {
$nodes = metaconsole_get_connections();
foreach ($nodes as $node) {
try {
$nd = new Node($node['id']);
$nd->connect();
$modules_regex_node = modules_get_regex(
$text_agent,
$text_agent_module,
$node['server_name']
);
} catch (\Exception $e) {
$nd->disconnect();
$modules_regex_node = [];
} finally {
$nd->disconnect();
}
$modules_regex = array_merge($modules_regex, $modules_regex_node);
}
} else {
$modules_regex = modules_get_regex(
$text_agent,
$text_agent_module
);
}
}
if (empty($modules_regex) === false) {
$tops = array_merge($tops, $modules_regex);
$tops = array_reduce(
$tops,
function ($carry, $item) {
$carry[$item['id_agent_module'].'|'.$item['server_name']] = $item;
return $carry;
},
[]
);
}
} else {
$tops = $content['subitems'];
}
// Get chart.
reporting_set_conf_charts(
$width,
$height,
$only_image,
$type,
$content,
$ttl
);
if (!empty($force_width_chart)) {
$width = $force_width_chart;
}
if (!empty($force_height_chart)) {
$height = $force_height_chart;
}
// Force width 600px.
$width = 600;
if (empty($tops)) {
$return['failed'] = __('There are no Agent/Modules defined');
} else {
$data_top = [];
foreach ($tops as $key => $row) {
// Metaconsole connection.
$server_name = $row['server_name'];
if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole()) {
$connection = metaconsole_get_connection($server_name);
if (metaconsole_load_external_db($connection) != NOERR) {
continue;
}
}
$ag_name = modules_get_agentmodule_agent_alias($row['id_agent_module']);
$mod_name = modules_get_agentmodule_name($row['id_agent_module']);
$unit = db_get_value(
'unit',
'tagente_modulo',
'id_agente_modulo',
$row['id_agent_module']
);
switch ($top_n) {
case REPORT_TOP_N_MAX:
$value = reporting_get_agentmodule_data_max($row['id_agent_module'], $content['period']);
break;
case REPORT_TOP_N_MIN:
$value = reporting_get_agentmodule_data_min($row['id_agent_module'], $content['period']);
break;
case REPORT_TOP_N_AVG:
default:
// If nothing is selected then it will be shown the average data.
$value = reporting_get_agentmodule_data_average($row['id_agent_module'], $content['period']);
break;
}
// If the returned value from modules_get_agentmodule_data_max/min/avg is false it won't be stored.
if ($value !== false) {
$data_top[$key] = $value;
$id_agent_module[$key] = $row['id_agent_module'];
$agent_name[$key] = $ag_name;
$module_name[$key] = $mod_name;
$units[$key] = $unit;
}
// Restore dbconnection.
if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole()) {
metaconsole_restore_db();
}
}
if (empty($data_top)) {
$return['failed'] = __('Insuficient data');
} else {
$data_return = [];
// Order to show.
switch ($order_uptodown) {
// Descending.
case 1:
array_multisort(
$data_top,
SORT_DESC,
$agent_name,
SORT_ASC,
$module_name,
SORT_ASC,
$id_agent_module,
SORT_ASC,
$units,
SORT_ASC
);
break;
// Ascending.
case 2:
array_multisort(
$data_top,
SORT_ASC,
$agent_name,
SORT_ASC,
$module_name,
SORT_ASC,
$id_agent_module,
SORT_ASC,
$units,
SORT_ASC
);
break;
// By agent name or without selection.
case 0:
case 3:
array_multisort(
$agent_name,
SORT_ASC,
$data_top,
SORT_ASC,
$module_name,
SORT_ASC,
$id_agent_module,
SORT_ASC,
$units,
SORT_ASC
);
break;
default:
// Default.
break;
}
array_splice($data_top, $top_n_value);
array_splice($agent_name, $top_n_value);
array_splice($module_name, $top_n_value);
array_splice($id_agent_module, $top_n_value);
array_splice($units, $top_n_value);
$data_top_values = [];
$data_top_values['data_top'] = $data_top;
$data_top_values['agent_name'] = $agent_name;
$data_top_values['module_name'] = $module_name;
$data_top_values['id_agent_module'] = $id_agent_module;
$data_top_values['units'] = $units;
// Define truncate size depends the graph width.
$truncate_size = ($width / (4 * ($config['font_size'])) - 1);
if ($order_uptodown == 1 || $order_uptodown == 2) {
$i = 0;
$data_pie_graph = [];
$data_hbar = [];
foreach ($data_top as $key_dt => $dt) {
$item_name = ui_print_truncate_text(
$agent_name[$key_dt],
$truncate_size,
false,
true,
false,
'...'
);
$item_name .= ' - ';
$item_name .= ui_print_truncate_text(
$module_name[$key_dt],
$truncate_size,
false,
true,
false,
'...'
);
$item_name_key_pie = $item_name;
$exist_key = true;
while ($exist_key) {
if (isset($data_pie_graph[$item_name_key_pie])) {
$item_name_key_pie .= ' ';
} else {
$exist_key = false;
}
}
$item_name_key_hbar = $item_name;
$exist_key = true;
while ($exist_key) {
if (isset($data_hbar[$item_name_key_hbar])) {
$item_name_key_hbar = ' '.$item_name_key_hbar;
} else {
$exist_key = false;
}
}
$data_hbar[$item_name]['g'] = $dt;
$data_pie_graph[$item_name] = $dt;
if ($show_graph == 0 || $show_graph == 1) {
$data = [];
$data['agent'] = $agent_name[$key_dt];
$data['module'] = $module_name[$key_dt];
$data['value'] = $dt;
$divisor = get_data_multiplier($units[$key_dt]);
$data['formated_value'] = format_for_graph(
$dt,
2,
'.',
',',
$divisor,
$units[$key_dt]
);
$data_return[] = $data;
}
$i++;
if ($i >= $top_n_value) {
break;
}
}
} else if ($order_uptodown == 0 || $order_uptodown == 3) {
$i = 0;
$data_pie_graph = [];
$data_hbar = [];
foreach ($agent_name as $key_an => $an) {
$item_name = '';
$item_name = ui_print_truncate_text(
$agent_name[$key_an],
$truncate_size,
false,
true,
false,
'...'
).' - '.ui_print_truncate_text(
$module_name[$key_an],
$truncate_size,
false,
true,
false,
'...'
);
$item_name_key_pie = $item_name;
$exist_key = true;
while ($exist_key) {
if (isset($data_pie_graph[$item_name_key_pie])) {
$item_name_key_pie .= ' ';
} else {
$exist_key = false;
}
}
$item_name_key_hbar = $item_name;
$exist_key = true;
while ($exist_key) {
if (isset($data_hbar[$item_name_key_hbar])) {
$item_name_key_hbar = ' '.$item_name_key_hbar;
} else {
$exist_key = false;
}
}
$data_pie_graph[$item_name] = $data_top[$key_an];
$data_hbar[$item_name]['g'] = $data_top[$key_an];
$divisor = get_data_multiplier($units[$key_an]);
if ($show_graph == 0 || $show_graph == 1) {
$data = [];
$data['agent'] = $an;
$data['module'] = $module_name[$key_an];
$data['value'] = $data_top[$key_an];
$data['formated_value'] = format_for_graph(
$data_top[$key_an],
2,
'.',
',',
$divisor,
$units[$key_an]
);
$data_return[] = $data;
}
$i++;
if ($i >= $top_n_value) {
break;
}
}
}
$return['charts']['bars'] = null;
$return['charts']['pie'] = null;
if ($show_graph != REPORT_TOP_N_ONLY_TABLE) {
arsort($data_pie_graph);
$return['charts']['pie'] = pie_graph(
$data_pie_graph,
$width,
$height,
__('other'),
ui_get_full_url(false, true, false, false).'/',
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl
);
// Display bars graph.
$return['charts']['bars'] = hbar_graph(
$data_hbar,
$width,
(count($data_hbar) * 50),
[],
[],
'',
'',
false,
false,
$config['homedir'].'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
true,
$ttl,
$config['homeurl'],
'white',
'#DFDFDF'
);
}
$return['resume'] = null;
if ($content['show_resume'] && count($data_top_values) > 0) {
// Get the very first not null value.
$i = 0;
do {
$min = $data_top_values['data_top'][$i];
$i++;
} while ($min === false && $i < count($data_top_values));
$max = $min;
$avg = 0;
$i = 0;
foreach ($data_top_values['data_top'] as $key => $dtv) {
if ($dtv < $min) {
$min = $dtv;
}
if ($dtv > $max) {
$max = $dtv;
}
$avg += $dtv;
$i++;
}
$unit = $data_top_values['units'][0];
$avg = ($avg / $i);
$return['resume']['min']['value'] = $min;
$return['resume']['min']['formated_value'] = format_for_graph($min, 2, '.', ',', $divisor, $unit);
$return['resume']['avg']['value'] = $avg;
$return['resume']['avg']['formated_value'] = format_for_graph($avg, 2, '.', ',', $divisor, $unit);
$return['resume']['max']['value'] = $max;
$return['resume']['max']['formated_value'] = format_for_graph($max, 2, '.', ',', $divisor, $unit);
}
$return['data'] = $data_return;
}
}
return reporting_check_structure_content($return);
}
function reporting_event_report_group(
$report,
$content,
$type='dinamic',
$force_width_chart=null,
$force_height_chart=null
) {
global $config;
$return['type'] = 'event_report_group';
if (empty($content['name'])) {
$content['name'] = __('Event Report Group');
}
$id_meta = 0;
$node_historical_event_enbled = '';
if (is_metaconsole() === true && empty($content['server_name']) === false) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
// Check if node historical event is enable.
$sql = sprintf(
'SELECT value
FROM tconfig
WHERE token LIKE "history_event_enabled"'
);
$result = db_get_row_sql($sql);
$node_historical_event_enbled = $result['value'];
}
$history = false;
if ($config['history_event_enabled'] || $node_historical_event_enbled) {
$history = true;
}
$group_name = groups_get_name($content['id_group'], true);
$items_label = ['agent_group' => $group_name];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $group_name;
if (!empty($content['style']['event_filter_search'])) {
$return['subtitle'] .= ' ('.$content['style']['event_filter_search'].')';
}
if (!empty($content['style']['event_filter_exclude'])) {
$return['subtitle'] .= ' ('.__('Exclude ').$content['style']['event_filter_exclude'].')';
}
$return['description'] = $content['description'];
$return['show_extended_events'] = $content['show_extended_events'];
$return['date'] = reporting_get_date_text($report, $content);
$event_filter = $content['style'];
$return['show_summary_group'] = $event_filter['show_summary_group'];
$return['show_custom_data'] = (isset($event_filter['custom_data_events']) === true) ? (bool) $event_filter['custom_data_events'] : false;
// Filter.
$show_summary_group = $event_filter['show_summary_group'];
$filter_event_severity = json_decode($event_filter['filter_event_severity'], true);
$filter_event_type = json_decode($event_filter['filter_event_type'], true);
$filter_event_status = json_decode($event_filter['filter_event_status'], true);
$filter_event_filter_search = $event_filter['event_filter_search'];
$filter_event_filter_exclude = $event_filter['event_filter_exclude'];
// Graphs.
$event_graph_by_agent = $event_filter['event_graph_by_agent'];
$event_graph_by_user_validator = $event_filter['event_graph_by_user_validator'];
$event_graph_by_criticity = $event_filter['event_graph_by_criticity'];
$event_graph_validated_vs_unvalidated = $event_filter['event_graph_validated_vs_unvalidated'];
if (isset($content['recursion'])
&& $content['recursion'] == 1
&& $content['id_group'] != 0
) {
$propagate = db_get_value(
'propagate',
'tgrupo',
'id_grupo',
$content['id_group']
);
if ($propagate) {
$children = groups_get_children($content['id_group']);
$_groups = [ $content['id_group'] ];
if (!empty($children)) {
foreach ($children as $child) {
$_groups[] = (int) $child['id_grupo'];
}
}
$content['id_group'] = $_groups;
}
}
if (is_metaconsole() === true) {
metaconsole_restore_db();
}
$data = events_get_agent(
false,
$content['period'],
$report['datetime'],
$history,
$show_summary_group,
$filter_event_severity,
$filter_event_type,
$filter_event_status,
$filter_event_filter_search,
$content['id_group'],
true,
false,
false,
$id_meta,
$filter_event_filter_exclude
);
if (empty($data) === true) {
$return['failed'] = __('No events');
} else {
$return['data'] = array_reverse($data);
}
reporting_set_conf_charts(
$width,
$height,
$only_image,
$type,
$content,
$ttl
);
if (!empty($force_width_chart)) {
$width = $force_width_chart;
}
if (!empty($force_height_chart)) {
$height = $force_height_chart;
}
$return['chart']['by_agent'] = null;
$return['chart']['by_user_validator'] = null;
$return['chart']['by_criticity'] = null;
$return['chart']['validated_vs_unvalidated'] = null;
if ($event_graph_by_agent) {
$data_graph_by_agent = [];
if (empty($data) === false) {
foreach ($data as $value) {
$k = $value['alias'];
if (is_metaconsole() === true) {
$k = '('.$value['server_name'].') '.$value['alias'];
}
if (isset($data_graph_by_agent[$k]) === true) {
$data_graph_by_agent[$k]++;
} else {
$data_graph_by_agent[$k] = 1;
}
}
}
$return['chart']['by_agent'] = pie_graph(
$data_graph_by_agent,
500,
150,
__('other'),
ui_get_full_url(false, false, false, false),
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl
);
}
if ($event_graph_by_user_validator) {
$data_graph_by_user = events_get_count_events_validated_by_user($data);
$return['chart']['by_user_validator'] = pie_graph(
$data_graph_by_user,
500,
150,
__('other'),
ui_get_full_url(false, false, false, false),
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl
);
}
if ($event_graph_by_criticity) {
$data_graph_by_criticity = [];
if (empty($data) === false) {
foreach ($data as $value) {
$k = get_priority_name($value['criticity']);
if (isset($data_graph_by_criticity[$k]) === true) {
$data_graph_by_criticity[$k]++;
} else {
$data_graph_by_criticity[$k] = 1;
}
}
}
$colors = get_criticity_pie_colors($data_graph_by_criticity);
$return['chart']['by_criticity'] = pie_graph(
$data_graph_by_criticity,
500,
150,
__('other'),
ui_get_full_url(false, false, false, false),
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl,
false,
$colors
);
}
if ($event_graph_validated_vs_unvalidated) {
$data_graph_by_status = [];
if (empty($data) === false) {
$status = [
1 => __('Validated'),
0 => __('Not validated'),
];
foreach ($data as $value) {
$k = $status[$value['estado']];
if (isset($data_graph_by_status[$k]) === true) {
$data_graph_by_status[$k]++;
} else {
$data_graph_by_status[$k] = 1;
}
}
}
$return['chart']['validated_vs_unvalidated'] = pie_graph(
$data_graph_by_status,
500,
150,
__('other'),
ui_get_full_url(false, false, false, false),
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl
);
}
// Total events.
if ($return['data'] != '') {
$return['total_events'] = count($return['data']);
} else {
$return['total_events'] = 0;
}
return reporting_check_structure_content($return);
}
/**
* Events for module reports.
*
* @param array $report Report info.
* @param array $content Content info.
* @param string $type Type retun report.
* @param integer $force_width_chart Width chart.
* @param integer $force_height_chart Height chart.
* @param integer $pdf If pdf report.
*
* @return array
*/
function reporting_event_report_module(
$report,
$content,
$type='dinamic',
$force_width_chart=null,
$force_height_chart=null,
$pdf=0
) {
global $config;
if ($pdf) {
$ttl = 2;
} else {
$ttl = 1;
}
$return['type'] = 'event_report_module';
if (empty($content['name'])) {
$content['name'] = __('Event Report Module');
}
$id_server = false;
if (is_metaconsole()) {
$id_server = metaconsole_get_id_server($content['server_name']);
metaconsole_connect(null, $id_server);
}
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$module_name = modules_get_agentmodule_name(
$id_agent_module
);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'type' => $content['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_alias.' - '.io_safe_output($module_name);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($return['label'] != '') {
$return['label'] = reporting_label_macro(
$items_label,
($return['label'] ?? '')
);
}
$return['description'] = $content['description'];
$return['show_extended_events'] = $content['show_extended_events'];
$return['date'] = reporting_get_date_text($report, $content);
$event_filter = $content['style'];
$return['show_summary_group'] = $event_filter['show_summary_group'];
$return['show_custom_data'] = (isset($event_filter['custom_data_events']) === true) ? (bool) $event_filter['custom_data_events'] : false;
// Filter.
$show_summary_group = $event_filter['show_summary_group'];
$filter_event_severity = json_decode(
$event_filter['filter_event_severity'],
true
);
$filter_event_type = json_decode(
$event_filter['filter_event_type'],
true
);
$filter_event_status = json_decode(
$event_filter['filter_event_status'],
true
);
$filter_event_filter_search = $event_filter['event_filter_search'];
$filter_event_filter_exclude = $event_filter['event_filter_exclude'];
// Graphs.
$event_graph_by_user_validator = $event_filter['event_graph_by_user_validator'];
$event_graph_by_criticity = $event_filter['event_graph_by_criticity'];
$event_graph_validated_vs_unvalidated = $event_filter['event_graph_validated_vs_unvalidated'];
$server_name = $content['server_name'];
if (is_metaconsole() && $server_name != '') {
$metaconsole_dbtable = true;
} else {
$metaconsole_dbtable = false;
}
// Data events.
$data = reporting_get_module_detailed_event(
$content['id_agent_module'],
$content['period'],
$report['datetime'],
$show_summary_group,
$filter_event_severity,
$filter_event_type,
$filter_event_status,
$filter_event_filter_search,
$force_width_chart,
$event_graph_by_user_validator,
$event_graph_by_criticity,
$event_graph_validated_vs_unvalidated,
$ttl,
$id_server,
$metaconsole_dbtable,
$filter_event_filter_exclude
);
if (empty($data)) {
$return['failed'] = __('No events');
} else {
$return['data'] = array_reverse($data);
}
if (is_metaconsole() === true) {
metaconsole_restore_db();
}
// Total_events.
if ($return['data'][0]['data'] != '') {
$return['total_events'] = count($return['data'][0]['data']);
} else {
$return['total_events'] = 0;
}
return reporting_check_structure_content($return);
}
/**
* Generate agents inventory report.
*
* @param array $report Report info.
* @param array $content Content info.
*
* @return array
*/
function reporting_agents_inventory($report, $content)
{
global $config;
$return['name'] = $content['name'];
$return['type'] = 'agents_inventory';
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$external_source = io_safe_input(json_decode($content['external_source'], true));
$es_agents_inventory_display_options = $external_source['agents_inventory_display_options'];
$es_agent_custom_fields = $external_source['agent_custom_fields'];
$es_custom_fields = $external_source['agent_custom_field_filter'];
$es_os_filter = $external_source['agent_os_filter'];
$es_agent_status_filter = $external_source['agent_status_filter'];
$es_agent_version_filter = $external_source['agent_version_filter'];
$es_agent_module_search_filter = $external_source['agent_module_search_filter'];
$es_agent_group_filter = $external_source['agent_group_filter'];
$es_agent_server_filter = $external_source['agent_server_filter'];
$es_users_groups = $external_source['users_groups'];
$es_agent_remote_conf = $external_source['agent_remote_conf'];
if ($es_agents_inventory_display_options === '') {
$es_agents_inventory_display_options = [];
}
$custom_field_sql = '';
$search_sql = '';
if (empty(array_filter($es_agent_custom_fields)) === false) {
$custom_field_sql = 'INNER JOIN tagent_custom_data tacd ON tacd.id_agent = tagente.id_agente';
if ($es_agent_custom_fields[0] != 0) {
$custom_field_sql .= ' AND tacd.id_field IN ('.implode(',', $es_agent_custom_fields).')';
}
if (!empty($es_custom_fields)) {
$custom_field_sql .= ' AND tacd.description like "%'.$es_custom_fields.'%"';
} else {
$custom_field_sql .= ' AND tacd.description <> ""';
}
}
if (in_array('0', $es_os_filter) === false) {
$search_sql .= ' AND id_os IN ('.implode(',', $es_os_filter).')';
}
if (empty($es_agent_version_filter) === false) {
$search_sql .= ' AND tagente.agent_version LIKE "%'.$es_agent_version_filter.'%"';
}
if (empty($es_agent_module_search_filter) === false) {
$search_sql .= ' AND tam.nombre LIKE "%'.$es_agent_module_search_filter.'%"';
}
if (empty($es_agent_group_filter) === false) {
$search_sql .= ' AND (tagente.id_grupo = '.$es_agent_group_filter.' OR tasg.id_group = '.$es_agent_group_filter.')';
}
if (empty($es_agent_remote_conf) === false) {
$search_sql .= ' AND tagente.remote = '.$es_agent_remote_conf;
}
$user_groups_to_sql = implode(',', array_keys(users_get_groups()));
$sql = sprintf(
'SELECT DISTINCT(tagente.id_agente) AS id_agente,
tagente.id_os,
tagente.direccion,
tagente.agent_version,
tagente.alias,
tagente.id_grupo,
tagente.comentarios,
tagente.url_address,
tagente.remote
FROM tagente LEFT JOIN tagent_secondary_group tasg
ON tagente.id_agente = tasg.id_agent
LEFT JOIN tagente_modulo tam
ON tam.id_agente = tagente.id_agente
%s
WHERE (tagente.id_grupo IN (%s) OR tasg.id_group IN (%s))
%s',
$custom_field_sql,
$user_groups_to_sql,
$user_groups_to_sql,
$search_sql
);
if (is_metaconsole()) {
$servers_ids = array_column(metaconsole_get_servers(), 'id');
} else {
$servers_ids = [0];
}
$return_data = [];
foreach ($servers_ids as $server_id) {
if (is_metaconsole()) {
$server = metaconsole_get_connection_by_id($server_id);
if ((int) $es_agent_server_filter !== 0
&& (int) $es_agent_server_filter !== (int) $server_id
) {
continue;
}
metaconsole_connect($server);
}
$agents = db_get_all_rows_sql($sql);
foreach ($agents as $key => $value) {
if (array_search('secondary_groups', $es_agents_inventory_display_options) !== false) {
$sql_agent_sec_group = sprintf(
'SELECT id_group
FROM tagent_secondary_group
WHERE id_agent = %d',
$value['id_agente']
);
$agent_secondary_groups = [];
$agent_secondary_groups = db_get_all_rows_sql($sql_agent_sec_group);
$agents[$key]['secondary_groups'] = $agent_secondary_groups;
}
if (array_search('custom_fields', $es_agents_inventory_display_options) !== false) {
$sql_agent_custom_fields = sprintf(
'SELECT tacd.description, tacf.name
FROM tagent_custom_data tacd INNER JOIN tagent_custom_fields tacf
ON tacd.id_field = tacf.id_field
WHERE tacd.description != "" AND tacd.id_agent = %d',
$value['id_agente']
);
$agent_custom_fields = [];
$agent_custom_fields = db_get_all_rows_sql($sql_agent_custom_fields);
$agents[$key]['custom_fields'] = $agent_custom_fields;
}
if (array_search('estado', $es_agents_inventory_display_options) !== false) {
if (in_array(6, $es_agent_status_filter)) {
if (agents_get_status($value['id_agente']) === 0) {
unset($agents[$key]);
} else {
$agents[$key]['estado'] = agents_get_status($value['id_agente']);
}
} else {
if (in_array('-1', $es_agent_status_filter) === true || in_array(agents_get_status($value['id_agente']), $es_agent_status_filter)) {
$agents[$key]['estado'] = agents_get_status($value['id_agente']);
} else {
// Agent does not match status filter.
unset($agents[$key]);
}
}
}
}
foreach ($agents as $key => $value) {
foreach ($value as $agent_val_key => $agent_val) {
// Exclude from data to be displayed in report those fields that were not selected to be displayed by user.
if (array_search($agent_val_key, $es_agents_inventory_display_options) === false) {
unset($agents[$key][$agent_val_key]);
}
}
}
$return_data[$server_id] = $agents;
if (is_metaconsole()) {
metaconsole_restore_db();
}
}
$all_data = [];
foreach ($return_data as $server_agents) {
foreach ($server_agents as $agent) {
$all_data[] = $agent;
}
}
$return['data'] = $all_data;
return reporting_check_structure_content($return);
}
/**
* Generate modules inventory report.
*
* @param array $report Report info.
* @param array $content Content info.
*
* @return array
*/
function reporting_modules_inventory($report, $content)
{
global $config;
$return['name'] = $content['name'];
$return['type'] = 'modules_inventory';
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$external_source = io_safe_input(json_decode($content['external_source'], true));
$es_agent_group_filter = $external_source['agent_group_filter'];
$module_group = $external_source['module_group'];
$es_agent_server_filter = $external_source['agent_server_filter'];
$search_sql = '';
if (empty($es_agent_group_filter) === false) {
$search_sql .= ' AND (ta.id_grupo = '.$es_agent_group_filter.' OR tasg.id_group = '.$es_agent_group_filter.')';
}
if (empty($module_group) === false && $module_group > -1) {
$search_sql .= ' AND tam.id_module_group = '.$module_group;
}
$user_groups_to_sql = '';
$user_groupsAR = users_get_groups($config['id_user'], 'AR');
$user_groups = $user_groupsAR;
$user_groups_to_sql = implode(',', array_keys($user_groups));
$sql = sprintf(
'SELECT tam.id_agente_modulo,
tam.nombre,
tam.descripcion,
tam.id_module_group,
ttm.id_tag,
ta.id_grupo AS group_id,
tasg.id_group AS sec_group_id
FROM tagente_modulo tam
INNER JOIN tagente ta
ON tam.id_agente = ta.id_agente
LEFT JOIN tagent_secondary_group tasg
ON ta.id_agente = tasg.id_agent
LEFT JOIN ttag_module ttm
ON ttm.id_agente_modulo = tam.id_agente_modulo
WHERE (ta.id_grupo IN (%s) OR tasg.id_group IN (%s)) %s',
$user_groups_to_sql,
$user_groups_to_sql,
$search_sql
);
if (is_metaconsole()) {
$servers_ids = array_column(metaconsole_get_servers(), 'id');
} else {
$servers_ids = [0];
}
$return_data = [];
foreach ($servers_ids as $server_id) {
if (is_metaconsole()) {
$server = metaconsole_get_connection_by_id($server_id);
if ((int) $es_agent_server_filter !== 0
&& (int) $es_agent_server_filter !== (int) $server_id
) {
continue;
}
metaconsole_connect($server);
}
$agents = db_get_all_rows_sql($sql);
$return_data[$server_id] = $agents;
if (is_metaconsole()) {
metaconsole_restore_db();
}
}
$all_data = [];
foreach ($return_data as $server_agents) {
foreach ($server_agents as $agent) {
$all_data[] = $agent;
}
}
$return['data'] = $all_data;
$module_row_data = [];
foreach ($return['data'] as $row) {
if (is_array($module_row_data[$row['id_agente_modulo']]) === false) {
$module_row_data[$row['id_agente_modulo']]['nombre'] = $row['nombre'];
$module_row_data[$row['id_agente_modulo']]['descripcion'] = $row['descripcion'];
$module_row_data[$row['id_agente_modulo']]['id_module_group'] = $row['id_module_group'];
$module_row_data[$row['id_agente_modulo']]['id_tag'] = [];
$module_row_data[$row['id_agente_modulo']]['group_id'] = [];
$module_row_data[$row['id_agente_modulo']]['sec_group_id'] = [];
}
if (in_array($row['id_tag'], $module_row_data[$row['id_agente_modulo']]['id_tag']) === false
&& $row['id_tag'] !== null
) {
$module_row_data[$row['id_agente_modulo']]['id_tag'][] = $row['id_tag'];
}
if (in_array($row['group_id'], $module_row_data[$row['id_agente_modulo']]['group_id']) === false
&& $row['group_id'] !== null
) {
$module_row_data[$row['id_agente_modulo']]['group_id'][] = $row['group_id'];
}
if (in_array($row['sec_group_id'], $module_row_data[$row['id_agente_modulo']]['sec_group_id']) === false
&& $row['sec_group_id'] !== null
) {
$module_row_data[$row['id_agente_modulo']]['sec_group_id'][] = $row['sec_group_id'];
}
}
$return['data'] = $module_row_data;
return reporting_check_structure_content($return);
}
function reporting_inventory_changes($report, $content, $type)
{
global $config;
$return['type'] = 'inventory_changes';
if (empty($content['name'])) {
$content['name'] = __('Inventory Changes');
}
if ($config['metaconsole']) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$es = json_decode($content['external_source'], true);
$id_agent = $es['id_agents'];
$module_name = $es['inventory_modules'];
$id_agent_module = modules_get_agentmodule_id($module_name, $id_agent);
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = agents_get_alias($content['id_agent']);
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
switch ($type) {
case 'data':
$inventory_changes = inventory_get_changes(
$id_agent,
$module_name,
($report['datetime'] - $content['period']),
$report['datetime'],
'csv'
);
break;
default:
$inventory_changes = inventory_get_changes(
$id_agent,
$module_name,
($report['datetime'] - $content['period']),
$report['datetime'],
'array'
);
break;
}
$return['data'] = [];
if ($inventory_changes == ERR_NODATA) {
$return['failed'] = __('No changes found.');
} else {
$return['data'] = $inventory_changes;
}
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
function reporting_inventory($report, $content, $type)
{
global $config;
$es = json_decode($content['external_source'], true);
$return['type'] = 'inventory';
if (empty($content['name'])) {
$content['name'] = __('Inventory');
}
if ($config['metaconsole']) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$es = json_decode($content['external_source'], true);
$id_agent = $es['id_agents'];
$module_name = io_safe_input($es['inventory_modules']);
if (empty($module_name)) {
$module_name = [0 => 0];
}
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$date = $es['date'];
$description = $content['description'];
switch ($type) {
case 'data':
$inventory_data = inventory_get_data(
(array) $id_agent,
(array) $module_name,
$date,
'',
false,
'csv'
);
break;
default:
$inventory_data = inventory_get_data(
(array) $id_agent,
(array) $module_name,
$date,
'',
false,
'hash'
);
break;
}
if ($inventory_data == ERR_NODATA) {
$return['failed'] = __('No data found.');
} else {
$return['data'] = $inventory_data;
}
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
/**
* Build data for report alert actions.
*
* @param array $report Report info.
* @param array $content Content.
*
* @return array Result data.
*/
function reporting_alert_report_actions($report, $content)
{
$return = [];
$return['type'] = 'alert_report_actions';
if (empty($content['name']) === true) {
$content['name'] = __('Alert actions');
}
$return['title'] = io_safe_output($content['name']);
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = __('Actions');
$return['description'] = io_safe_output($content['description']);
$return['date'] = reporting_get_date_text($report, $content);
$return['data'] = [];
$es = json_decode($content['external_source'], true);
if (isset($report['id_template']) === true
&& empty($report['id_template']) === false
) {
if (is_metaconsole() === true) {
$server_id = metaconsole_get_id_server($content['server_name']);
$modules = [$server_id.'|'.$content['id_agent_module']];
$agents = [$server_id.'|'.$content['id_agent']];
} else {
$modules = [$content['id_agent_module']];
$agents = [$content['id_agent']];
}
} else {
$modules = json_decode(
io_safe_output(base64_decode($es['module'])),
true
);
$agents = json_decode(
io_safe_output(base64_decode($es['id_agents'])),
true
);
}
$period = $content['period'];
$id_group = $content['id_group'];
$templates = $es['templates'];
$actions = $es['actions'];
$show_summary = $es['show_summary'];
$group_by = $es['group_by'];
$lapse = $content['lapse'];
$only_data = $es['only_data'];
$filters = [
'group' => $id_group,
'agents' => $agents,
'modules' => $modules,
'templates' => $templates,
'actions' => $actions,
'period' => $period,
'show_summary' => (bool) $show_summary,
'only_data' => (bool) $only_data,
];
$groupsBy = [
'group_by' => $group_by,
'lapse' => $lapse,
];
$return['filters'] = $filters;
$return['groupsBy'] = $groupsBy;
$return['data'] = alerts_get_alert_fired($filters, $groupsBy);
return reporting_check_structure_content($return);
}
/**
* Data report agent/module.
*
* @param array $report Report info.
* @param array $content Content info.
*
* @return array Structure Content.
*/
function reporting_agent_module($report, $content)
{
global $config;
$external_source = json_decode(
$content['external_source'],
true
);
$agents = json_decode(
io_safe_output(
base64_decode($external_source['id_agents'])
),
true
);
$modules = json_decode(
io_safe_output(
base64_decode($external_source['module'])
),
true
);
if (isset($external_source['show_type']) === true) {
$show_type = $external_source['show_type'];
} else {
$show_type = 0;
}
$return['type'] = 'agent_module';
if (empty($content['name'])) {
$content['name'] = __('Agent/Modules');
}
$return['title'] = io_safe_output($content['name']);
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$group_name = groups_get_name($content['id_group'], true);
if ($content['id_module_group'] == 0) {
$module_group_name = __('All');
} else {
$module_group_name = db_get_value(
'name',
'tmodule_group',
'id_mg',
$content['id_module_group']
);
}
$return['subtitle'] = $group_name.' - '.$module_group_name;
$return['description'] = io_safe_output($content['description']);
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
$return['data'] = [];
$modules_by_name = [];
$cont = 0;
foreach ($modules as $modul_id) {
$modules_by_name[$cont]['name'] = io_safe_output(
modules_get_agentmodule_name($modul_id)
);
$modules_by_name[$cont]['id'] = $modul_id;
if ($show_type === '1') {
$modules_by_name[$cont]['unit'] = modules_get_unit($modul_id);
}
$cont++;
}
if ($modules_by_name == false || $agents == false) {
$return['failed'] = __('There are no agents with modules');
} else {
foreach ($agents as $agent) {
$row = [];
$row['agent_status'][$agent] = agents_get_status($agent);
$row['agent_name'] = io_safe_output(agents_get_alias($agent));
$agent_modules = agents_get_modules($agent);
$row['modules'] = [];
foreach ($modules_by_name as $module) {
if (array_key_exists($module['id'], $agent_modules)) {
if ($show_type === '1') {
$module_last_value = modules_get_last_value($module['id']);
if (!is_numeric($module_last_value)) {
$module_last_value = htmlspecialchars($module_last_value);
}
$module['datos'] = $module_last_value;
$row['modules'][$module['name']] = modules_get_agentmodule_data_for_humans($module);
$row['show_type'] = $show_type;
} else {
$row['modules'][$module['name']] = modules_get_agentmodule_status($module['id']);
}
} else {
if (!array_key_exists($module['name'], $row['modules'])) {
$row['modules'][$module['name']] = null;
}
}
}
$return['data'][] = $row;
}
}
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
/**
* Agents module status
*
* @param array $report Info Report.
* @param array $content Info content.
*
* @return array
*/
function reporting_agent_module_status($report, $content)
{
global $config;
$return['type'] = 'agent_module_status';
if (empty($content['name'])) {
$content['name'] = __('Agent/Modules Status');
}
$return['title'] = io_safe_output($content['name']);
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$group_name = groups_get_name($content['id_group'], true);
if ($content['id_module_group'] == 0) {
$module_group_name = __('All');
} else {
$module_group_name = db_get_value(
'name',
'tmodule_group',
'id_mg',
$content['id_module_group']
);
}
$return['subtitle'] = $group_name.' - '.$module_group_name;
$return['description'] = io_safe_output($content['description']);
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
$return['data'] = [];
$external_source = json_decode(
$content['external_source'],
true
);
$agents = json_decode(
io_safe_output(
base64_decode($external_source['id_agents'])
),
true
);
$modules = json_decode(
io_safe_output(
base64_decode($external_source['module'])
),
true
);
if (is_metaconsole() === true) {
$agents_per_node = [];
$modules_per_node = [];
if (empty($agents) === false) {
foreach ($agents as $value) {
$agent_array = explode('|', $value);
$agents_per_node[$agent_array[0]][] = $agent_array[1];
}
}
if (empty($modules) === false) {
foreach ($modules as $value) {
$module_array = explode('|', $value);
$modules_per_node[$module_array[0]][] = $module_array[1];
}
}
if (empty($agents_per_node) === false) {
foreach ($agents_per_node as $server => $agents) {
$connection = metaconsole_get_connection_by_id($server);
if (metaconsole_connect($connection) != NOERR) {
continue;
}
$res[$connection['server_name']] = get_status_data_agent_modules(
$content['id_group'],
$agents,
$modules_per_node[$server]
);
metaconsole_restore_db();
}
} else {
$metaconsole_connections = metaconsole_get_connection_names();
// For all nodes.
if (isset($metaconsole_connections) === true
&& is_array($metaconsole_connections) === true
) {
foreach ($metaconsole_connections as $metaconsole) {
// Get server connection data.
$server_data = metaconsole_get_connection($metaconsole);
// Establishes connection.
if (metaconsole_load_external_db($server_data) !== NOERR) {
continue;
}
$res[$server_data['server_name']] = get_status_data_agent_modules(
$content['id_group'],
$agents,
$modules
);
metaconsole_restore_db();
}
}
}
} else {
$res['node'] = get_status_data_agent_modules(
$content['id_group'],
$agents,
$modules
);
}
$return['data'] = $res;
return reporting_check_structure_content($return);
}
function reporting_exception(
$report,
$content,
$type='dinamic',
$force_width_chart=null,
$force_height_chart=null
) {
global $config;
$return['type'] = 'exception';
if (empty($content['name'])) {
$content['name'] = __('Exception');
}
$order_uptodown = $content['order_uptodown'];
$exception_condition_value = $content['exception_condition_value'];
$show_graph = $content['show_graph'];
$formated_exception_value = $exception_condition_value;
if (is_numeric($exception_condition_value)) {
$formated_exception_value = format_for_graph(
$exception_condition_value,
2
);
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$exception_condition = $content['exception_condition'];
switch ($exception_condition) {
case REPORT_EXCEPTION_CONDITION_EVERYTHING:
$return['subtitle'] = __('Exception - Everything');
$return['subtype'] = __('Everything');
break;
case REPORT_EXCEPTION_CONDITION_GE:
$return['subtitle'] = sprintf(
__('Exception - Modules over or equal to %s'),
$formated_exception_value
);
$return['subtype'] = __('Modules over or equal to %s');
break;
case REPORT_EXCEPTION_CONDITION_LE:
$return['subtitle'] = sprintf(
__('Exception - Modules under or equal to %s'),
$formated_exception_value
);
$return['subtype'] = __('Modules under or equal to %s');
break;
case REPORT_EXCEPTION_CONDITION_L:
$return['subtitle'] = sprintf(
__('Exception - Modules under %s'),
$formated_exception_value
);
$return['subtype'] = __('Modules under %s');
break;
case REPORT_EXCEPTION_CONDITION_G:
$return['subtitle'] = sprintf(
__('Exception - Modules over %s'),
$formated_exception_value
);
$return['subtype'] = __('Modules over %s');
break;
case REPORT_EXCEPTION_CONDITION_E:
$return['subtitle'] = sprintf(
__('Exception - Equal to %s'),
$formated_exception_value
);
$return['subtype'] = __('Equal to %s');
break;
case REPORT_EXCEPTION_CONDITION_NE:
$return['subtitle'] = sprintf(
__('Exception - Not equal to %s'),
$formated_exception_value
);
$return['subtype'] = __('Not equal to %s');
break;
case REPORT_EXCEPTION_CONDITION_OK:
$return['subtitle'] = __('Exception - Modules at normal status');
$return['subtype'] = __('Modules at normal status');
break;
case REPORT_EXCEPTION_CONDITION_NOT_OK:
$return['subtitle'] = __('Exception - Modules at critical or warning status');
$return['subtype'] = __('Modules at critical or warning status');
break;
default:
// Default.
break;
}
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['data'] = [];
$return['chart'] = [];
$return['resume'] = [];
if (empty($content['subitems'])) {
// Get all the related data.
$sql = sprintf(
'
SELECT id_agent_module, server_name, operation
FROM treport_content_item
WHERE id_report_content = %d',
$content['id_rc']
);
$exceptions = db_get_all_rows_sql($sql);
if ($exceptions === false) {
$exceptions = [];
}
// REGEXP.
$text_agent = '';
if (isset($content['style']['text_agent']) === true
&& empty($content['style']['text_agent']) === false
) {
$text_agent = base64_decode($content['style']['text_agent']);
}
$text_agent_module = '';
if (isset($content['style']['text_agent_module']) === true
&& empty($content['style']['text_agent_module']) === false
) {
$text_agent_module = base64_decode($content['style']['text_agent_module']);
}
$modules_regex = [];
if (empty($text_agent) === false) {
if (is_metaconsole() === true) {
$nodes = metaconsole_get_connections();
foreach ($nodes as $node) {
try {
$nd = new Node($node['id']);
$nd->connect();
$modules_regex_node = modules_get_regex(
$text_agent,
$text_agent_module,
$node['server_name']
);
} catch (\Exception $e) {
$nd->disconnect();
$modules_regex_node = [];
} finally {
$nd->disconnect();
}
$modules_regex = array_merge($modules_regex, $modules_regex_node);
}
} else {
$modules_regex = modules_get_regex(
$text_agent,
$text_agent_module
);
}
}
if (empty($modules_regex) === false) {
$exceptions = array_merge($exceptions, $modules_regex);
$exceptions = array_reduce(
$exceptions,
function ($carry, $item) {
if (isset($item['operation']) === false) {
$item['operation'] = 'avg';
}
$carry[$item['id_agent_module'].'|'.$item['server_name']] = $item;
return $carry;
},
[]
);
}
} else {
$exceptions = $content['subitems'];
}
if ($exceptions === false) {
$return['failed'] = __('There are no Agent/Modules defined');
} else {
// Get the very first not null value.
$i = 0;
do {
// Metaconsole connection.
$server_name = $exceptions[$i]['server_name'];
if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole()) {
$connection = metaconsole_get_connection($server_name);
if (metaconsole_load_external_db($connection) != NOERR) {
// ui_print_error_message ("Error connecting to ".$server_name);
continue;
}
}
if ($content['period'] == 0) {
$min = modules_get_last_value($exceptions[$i]['id_agent_module']);
} else {
switch ($exceptions[$i]['operation']) {
case 'avg':
$min = reporting_get_agentmodule_data_average(
$exceptions[$i]['id_agent_module'],
$content['period']
);
break;
case 'max':
$min = reporting_get_agentmodule_data_max(
$exceptions[$i]['id_agent_module'],
$content['period']
);
break;
case 'min':
$min = reporting_get_agentmodule_data_min(
$exceptions[$i]['id_agent_module'],
$content['period']
);
break;
default:
// Default.
break;
}
}
$i++;
// Restore dbconnection.
if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole()) {
metaconsole_restore_db();
}
} while ($min === false && $i < count($exceptions));
$max = $min;
$avg = 0;
$items = [];
$i = 0;
foreach ($exceptions as $exc) {
// Metaconsole connection.
$server_name = $exc['server_name'];
if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole()) {
metaconsole_restore_db();
$connection = metaconsole_get_connection($server_name);
if (metaconsole_load_external_db($connection) != NOERR) {
// ui_print_error_message ("Error connecting to ".$server_name);
continue;
}
}
$ag_name = modules_get_agentmodule_agent_name($exc['id_agent_module']);
$ag_alias = modules_get_agentmodule_agent_alias($exc['id_agent_module']);
$mod_name = modules_get_agentmodule_name($exc['id_agent_module']);
$unit = db_get_value('unit', 'tagente_modulo', 'id_agente_modulo', $exc['id_agent_module']);
if ($content['period'] == 0) {
$value = modules_get_last_value($exceptions[$i]['id_agent_module']);
} else {
switch ($exc['operation']) {
case 'avg':
$value = reporting_get_agentmodule_data_average($exc['id_agent_module'], $content['period']);
break;
case 'max':
$value = reporting_get_agentmodule_data_max($exc['id_agent_module'], $content['period']);
break;
case 'min':
$value = reporting_get_agentmodule_data_min($exc['id_agent_module'], $content['period']);
break;
}
}
if ($value !== false) {
if ($value > $max) {
$max = $value;
}
if ($value < $min) {
$min = $value;
}
$avg += $value;
// Skips
switch ($exception_condition) {
case REPORT_EXCEPTION_CONDITION_EVERYTHING:
break;
case REPORT_EXCEPTION_CONDITION_GE:
if ($value < $exception_condition_value) {
continue 2;
}
break;
case REPORT_EXCEPTION_CONDITION_LE:
if ($value > $exception_condition_value) {
continue 2;
}
break;
case REPORT_EXCEPTION_CONDITION_L:
if ($value > $exception_condition_value) {
continue 2;
}
break;
case REPORT_EXCEPTION_CONDITION_G:
if ($value < $exception_condition_value) {
continue 2;
}
break;
case REPORT_EXCEPTION_CONDITION_E:
if ($value != $exception_condition_value) {
continue 2;
}
break;
case REPORT_EXCEPTION_CONDITION_NE:
if ($value == $exception_condition_value) {
continue 2;
}
break;
case REPORT_EXCEPTION_CONDITION_OK:
if (modules_get_agentmodule_status($exc['id_agent_module']) != 0) {
continue 2;
}
break;
case REPORT_EXCEPTION_CONDITION_NOT_OK:
if (modules_get_agentmodule_status($exc['id_agent_module']) == 0) {
continue 2;
}
break;
}
$item = [];
$item['value'] = $value;
$item['module_id'] = $exc['id_agent_module'];
$item['module'] = $mod_name;
$item['agent'] = $ag_alias;
$item['unit'] = $unit;
if ($exc['operation'] == 'avg') {
$item['operation'] = 'rate';
} else {
$item['operation'] = $exc['operation'];
}
$items[] = $item;
$i++;
}
// Restore dbconnection
if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole()) {
metaconsole_restore_db();
}
}
if ($i == 0) {
switch ($exception_condition) {
case REPORT_EXCEPTION_CONDITION_EVERYTHING:
$return['failed'] = __('There are no Modules under those conditions.');
break;
case REPORT_EXCEPTION_CONDITION_GE:
$return['failed'] = __('There are no Modules over or equal to %s.', $exception_condition_value);
break;
case REPORT_EXCEPTION_CONDITION_LE:
$return['failed'] = __('There are no Modules less or equal to %s.', $exception_condition_value);
break;
case REPORT_EXCEPTION_CONDITION_L:
$return['failed'] = __('There are no Modules less %s.', $exception_condition_value);
break;
case REPORT_EXCEPTION_CONDITION_G:
$return['failed'] = __('There are no Modules over %s.', $exception_condition_value);
break;
case REPORT_EXCEPTION_CONDITION_E:
$return['failed'] = __('There are no Modules equal to %s', $exception_condition_value);
break;
case REPORT_EXCEPTION_CONDITION_NE:
$return['failed'] = __('There are no Modules not equal to %s', $exception_condition_value);
break;
case REPORT_EXCEPTION_CONDITION_OK:
$return['failed'] = __('There are no Modules normal status');
break;
case REPORT_EXCEPTION_CONDITION_NOT_OK:
$return['failed'] = __('There are no Modules at critial or warning status');
break;
}
} else {
$avg = ($avg / $i);
// Sort the items
$sort_number = function ($a, $b, $sort=SORT_ASC) {
if ($a == $b) {
return 0;
} else if ($a > $b) {
return ($sort === SORT_ASC) ? 1 : -1;
} else {
return ($sort === SORT_ASC) ? -1 : 1;
}
};
$sort_string = function ($a, $b, $sort=SORT_ASC) {
if ($sort === SORT_ASC) {
return strcasecmp($a, $b);
} else {
return strcasecmp($b, $a);
}
};
usort(
$items,
function ($a, $b) use ($order_uptodown, $sort_number, $sort_string) {
switch ($order_uptodown) {
case 1:
case 2:
if ($a['value'] == $b['value']) {
if ($a['agent'] == $b['agent']) {
if ($a['module'] == $b['module']) {
return $sort_number($a['module_id'], $b['module_id']);
}
return $sort_string($a['module'], $b['module']);
}
return $sort_string($a['agent'], $b['agent']);
}
return $sort_number($a['value'], $b['value'], ($order_uptodown == 1) ? SORT_DESC : SORT_ASC);
// Order by agent name or without selection
case 0:
case 3:
if ($a['agent'] == $b['agent']) {
if ($a['value'] == $b['value']) {
if ($a['module'] == $b['module']) {
return $sort_number($a['module_id'], $b['module_id']);
}
return $sort_string($a['module'], $b['module']);
}
return $sort_number($a['value'], $b['value']);
}
return $sort_string($a['agent'], $b['agent']);
}
}
);
$data_pie_graph = [];
$data_hbar = [];
foreach ($items as $key => $item) {
if ($show_graph == 1 || $show_graph == 2) {
// TODO: Find a better way to show the graphs
$data_hbar[$item['agent'].' - '.$item['operation']]['g'] = $item['value'];
$data_pie_graph[$item['agent'].' - '.$item['operation']] = $item['value'];
}
if ($show_graph == 0 || $show_graph == 1) {
$data = [];
$data['agent'] = $item['agent'];
$data['module'] = $item['module'];
$data['operation'] = __($item['operation']);
$data['value'] = $item['value'];
$data['formated_value'] = format_for_graph($item['value'], 2).' '.$item['unit'];
$return['data'][] = $data;
}
}
if ($show_graph == 1 || $show_graph == 2) {
reporting_set_conf_charts(
$width,
$height,
$only_image,
$type,
$content,
$ttl
);
if (!empty($force_width_chart)) {
$width = $force_width_chart;
}
if (!empty($force_height_chart)) {
$height = $force_height_chart;
}
$return['chart']['pie'] = pie_graph(
$data_pie_graph,
600,
150,
__('other'),
ui_get_full_url(false, false, false, false),
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl
);
$params = [
'chart_data' => $data_hbar,
'width' => 600,
'height' => (25 * count($data_hbar)),
'color' => [],
'legend' => [],
'long_index' => [],
'no_data_image' => ui_get_full_url('images/image_problem_area_small.png', false, false, false),
'xaxisname' => '',
'yaxisname' => '',
'water_mark' => ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
'font' => '',
'font_size' => '',
'unit' => '',
'ttl' => $ttl,
'homeurl' => ui_get_full_url(false, false, false, false),
'backgroundColor' => 'white',
];
$return['chart']['hbar'] = call_user_func_array(
'hbar_graph',
array_values(($params ?? []))
);
}
if ($content['show_resume'] && $i > 0) {
$return['resume']['min']['value'] = $min;
$return['resume']['min']['formated_value'] = format_for_graph($min, 2);
$return['resume']['max']['value'] = $max;
$return['resume']['max']['formated_value'] = format_for_graph($max, 2);
$return['resume']['avg']['value'] = $avg;
$return['resume']['avg']['formated_value'] = format_for_graph($avg, 2);
}
}
}
return reporting_check_structure_content($return);
}
function reporting_group_report($report, $content)
{
global $config;
$return['type'] = 'group_report';
if (empty($content['name'])) {
$content['name'] = __('Group Report');
}
if (is_metaconsole() === true) {
if (isset($content['server_name']) === true
&& empty($content['server_name']) === false
) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
$data = reporting_groups_nodes($content);
if (is_metaconsole() === true) {
metaconsole_restore_db();
}
} else {
$servers = metaconsole_get_connection_names();
if (isset($servers) === true && is_array($servers) === true) {
$group_stats = [
'monitor_checks' => 0,
'monitor_not_init' => 0,
'monitor_unknown' => 0,
'monitor_ok' => 0,
'monitor_bad' => 0,
'monitor_warning' => 0,
'monitor_critical' => 0,
'monitor_not_normal' => 0,
'monitor_alerts' => 0,
'monitor_alerts_fired' => 0,
'monitor_alerts_fire_count' => 0,
'total_agents' => 0,
'total_alerts' => 0,
'total_checks' => 0,
'alerts' => 0,
'agents_unknown' => 0,
'monitor_health' => 0,
'alert_level' => 0,
'module_sanity' => 0,
'server_sanity' => 0,
'total_not_init' => 0,
'monitor_non_init' => 0,
'agent_ok' => 0,
'agent_warning' => 0,
'agent_critical' => 0,
'agent_unknown' => 0,
'agent_not_init' => 0,
'global_health' => 0,
'alert_fired' => 0,
];
$count_events = 0;
foreach ($servers as $k_server => $v_server) {
$id_meta = metaconsole_get_id_server($v_server);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
$data_node = reporting_groups_nodes($content);
$count_events += $data_node['count_events'];
foreach ($data_node['group_stats'] as $key => $value) {
$group_stats[$key] += $value;
}
if (is_metaconsole() === true) {
metaconsole_restore_db();
}
}
$data = [
'count_events' => $count_events,
'group_stats' => $group_stats,
];
}
}
} else {
$data = reporting_groups_nodes($content);
}
$items_label = [
'agent_group' => groups_get_name($content['id_group'], true),
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['server_name'] = $content['server_name'];
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = groups_get_name($content['id_group'], true);
$return['description'] = io_safe_output($content['description']);
$return['date'] = reporting_get_date_text($report, $content);
$return['data'] = [];
$return['data']['count_events'] = $data['count_events'];
$return['data']['group_stats'] = $data['group_stats'];
return reporting_check_structure_content($return);
}
/**
* Return stats groups for node.
*
* @param array $content Info report.
*
* @return array Result.
*/
function reporting_groups_nodes($content)
{
global $config;
$id_group = groups_safe_acl(
$config['id_user'],
$content['id_group'],
'ER'
);
if (empty($id_group)) {
$events = [];
} else {
// ID group.
if (empty($id_group) === false) {
$filters['id_group_filter'] = $id_group;
}
// Status.
if (empty($filter_event_status) === false) {
$filters['status'] = EVENT_NO_VALIDATED;
}
// Grouped.
$filters['group_rep'] = EVENT_GROUP_REP_EVENTS;
$events = Event::search(
[
'te.*',
'ta.alias',
],
$filters,
0,
1000,
'desc',
'te.utimestamp'
);
}
if (empty($events)) {
$events = [];
}
$return['count_events'] = count($events);
$return['group_stats'] = reporting_get_group_stats(
$content['id_group'],
'AR',
(bool) $content['recursion']
);
return $return;
}
/**
* Create data report event agent.
*
* @param array $report Data report.
* @param array $content Content report.
* @param string $type Type report.
* @param integer $force_width_chart Force width.
* @param integer $force_height_chart Force height.
*
* @return array Data.
*/
function reporting_event_report_agent(
$report,
$content,
$type='dinamic',
$force_width_chart=null,
$force_height_chart=null
) {
global $config;
$return['type'] = 'event_report_agent';
if (empty($content['name'])) {
$content['name'] = __('Event Report Agent');
}
$history = false;
if ($config['history_event_enabled']) {
$history = true;
}
$id_server = false;
if (is_metaconsole()) {
$id_server = metaconsole_get_id_server($content['server_name']);
metaconsole_connect(null, $id_server);
}
$id_agent = $content['id_agent'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$label = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($label != '') {
$label = reporting_label_macro(
$items_label,
($label ?? '')
);
}
$return['label'] = $label;
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = io_safe_output($agent_alias);
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['show_summary_group'] = $content['style']['show_summary_group'];
$return['show_extended_events'] = $content['show_extended_events'];
$style = $content['style'];
// Filter.
$show_summary_group = $style['show_summary_group'];
$filter_event_severity = json_decode($style['filter_event_severity'], true);
$filter_event_type = json_decode($style['filter_event_type'], true);
$filter_event_status = json_decode($style['filter_event_status'], true);
$filter_event_filter_search = $style['event_filter_search'];
$filter_event_filter_exclude = $style['event_filter_exclude'];
$show_custom_data = (isset($style['custom_data_events']) === true) ? (bool) $style['custom_data_events'] : false;
$return['show_custom_data'] = $show_custom_data;
// Graph.
$event_graph_by_user_validator = $style['event_graph_by_user_validator'];
$event_graph_by_criticity = $style['event_graph_by_criticity'];
$event_graph_validated_vs_unvalidated = $style['event_graph_validated_vs_unvalidated'];
if (is_metaconsole() === true) {
metaconsole_restore_db();
}
$return['data'] = reporting_get_agents_detailed_event(
$content['id_agent'],
$content['period'],
$report['datetime'],
$history,
$show_summary_group,
$filter_event_severity,
$filter_event_type,
$filter_event_status,
$filter_event_filter_search,
$filter_event_filter_exclude,
$id_server,
$show_custom_data
);
reporting_set_conf_charts(
$width,
$height,
$only_image,
$type,
$content,
$ttl
);
if (!empty($force_width_chart)) {
$width = $force_width_chart;
}
if (!empty($force_height_chart)) {
$height = $force_height_chart;
}
$return['chart']['by_user_validator'] = null;
$return['chart']['by_criticity'] = null;
$return['chart']['validated_vs_unvalidated'] = null;
if ($event_graph_by_user_validator) {
$data_graph_by_user = events_get_count_events_validated_by_user($return['data']);
$return['chart']['by_user_validator'] = pie_graph(
$data_graph_by_user,
500,
150,
__('other'),
ui_get_full_url(false, false, false, false),
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl
);
}
if ($event_graph_by_criticity) {
$data_graph_by_criticity = [];
if (empty($return['data']) === false) {
foreach ($return['data'] as $value) {
$k = get_priority_name($value['criticity']);
if (isset($data_graph_by_criticity[$k]) === true) {
$data_graph_by_criticity[$k]++;
} else {
$data_graph_by_criticity[$k] = 1;
}
}
}
$colors = get_criticity_pie_colors($data_graph_by_criticity);
$return['chart']['by_criticity'] = pie_graph(
$data_graph_by_criticity,
500,
150,
__('other'),
ui_get_full_url(false, false, false, false),
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl,
false,
$colors
);
}
if ($event_graph_validated_vs_unvalidated) {
$data_graph_by_status = [];
if (empty($return['data']) === false) {
$status = [
1 => __('Validated'),
0 => __('Not validated'),
];
foreach ($return['data'] as $value) {
$k = $status[$value['estado']];
if (isset($data_graph_by_status[$k]) === true) {
$data_graph_by_status[$k]++;
} else {
$data_graph_by_status[$k] = 1;
}
}
}
$return['chart']['validated_vs_unvalidated'] = pie_graph(
$data_graph_by_status,
500,
150,
__('other'),
ui_get_full_url(false, false, false, false),
ui_get_full_url(false, false, false, false).'/images/logo_vertical_water.png',
$config['fontpath'],
$config['font_size'],
$ttl
);
}
// Total events.
if ($return['data'] != '') {
$return['total_events'] = count($return['data']);
} else {
$return['total_events'] = 0;
}
return reporting_check_structure_content($return);
}
/**
* Show historical data.
*
* @param array $report Data report.
* @param array $content Content report.
*
* @return array
*/
function reporting_historical_data($report, $content)
{
global $config;
$return['type'] = 'historical_data';
$period = $content['period'];
$date_limit = ($report['datetime'] - $period);
if (empty($content['name'])) {
$content['name'] = __('Historical data');
}
if (is_metaconsole()) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
$connection = metaconsole_connect($server);
}
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = io_safe_output(agents_get_alias($id_agent));
$module_name = io_safe_output(
modules_get_agentmodule_name(
$id_agent_module
)
);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_alias.' - '.$module_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($return['label'] != '') {
$return['label'] = reporting_label_macro(
$items_label,
($return['label'] ?? '')
);
}
$return['keys'] = [
__('Date'),
__('Data'),
];
$module_type = db_get_value_filter(
'id_tipo_modulo',
'tagente_modulo',
['id_agente_modulo' => $content['id_agent_module']]
);
$result = [];
switch ($module_type) {
case 3:
case 17:
case 23:
case 33:
$result = db_get_all_rows_sql(
'SELECT *
FROM tagente_datos_string
WHERE id_agente_modulo ='.$content['id_agent_module'].'
AND utimestamp >'.$date_limit.'
AND utimestamp <='.$report['datetime'],
true
);
break;
default:
$result = db_get_all_rows_sql(
'SELECT *
FROM tagente_datos
WHERE id_agente_modulo ='.$content['id_agent_module'].'
AND utimestamp >'.$date_limit.'
AND utimestamp <='.$report['datetime'],
true
);
break;
}
$data = [];
foreach ($result as $row) {
$data[] = [
__('Date') => date($config['date_format'], $row['utimestamp']),
__('Data') => $row['datos'],
];
}
$return['data'] = $data;
if (is_metaconsole() && $connection > 0) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
/**
* Show data serialized.
*
* @param array $report Data report.
* @param array $content Content report.
*
* @return array
*/
function reporting_database_serialized($report, $content)
{
global $config;
$return['type'] = 'database_serialized';
if (empty($content['name'])) {
$content['name'] = __('Database Serialized');
}
if (is_metaconsole()) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$module_name = modules_get_agentmodule_name(
$id_agent_module
);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_alias.' - '.$module_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$keys = [];
if (isset($content['header_definition']) && ($content['header_definition'] != '')) {
$keys = explode('|', $content['header_definition']);
}
$return['keys'] = $keys;
$return['agent_name_db'] = agents_get_name($id_agent);
$return['agent_name'] = $agent_alias;
$return['module_name'] = $module_name;
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($return['label'] != '') {
$return['label'] = reporting_label_macro(
$items_label,
($return['label'] ?? '')
);
}
$datelimit = ($report['datetime'] - $content['period']);
$search_in_history_db = db_search_in_history_db($datelimit);
// This query gets information from the default and the historic database.
$result = db_get_all_rows_sql(
'SELECT *
FROM tagente_datos
WHERE id_agente_modulo = '.$content['id_agent_module'].'
AND utimestamp > '.$datelimit.'
AND utimestamp <= '.$report['datetime'],
$search_in_history_db
);
// Adds string data if there is no numeric data.
if ($result === false) {
// This query gets information from the default and the historic database.
$result = db_get_all_rows_sql(
'SELECT *
FROM tagente_datos_string
WHERE id_agente_modulo = '.$content['id_agent_module'].'
AND utimestamp > '.$datelimit.'
AND utimestamp <= '.$report['datetime'],
$search_in_history_db
);
}
if ($result === false) {
$result = [];
}
$data = [];
foreach ($result as $row) {
$date = date($config['date_format'], $row['utimestamp']);
$serialized_data = $row['datos'];
// Cut line by line.
if (empty($content['line_separator'])
|| empty($serialized_data)
) {
$rowsUnserialize = [$row['datos']];
} else {
$rowsUnserialize = explode(
$content['line_separator'],
$serialized_data
);
}
foreach ($rowsUnserialize as $rowUnser) {
$row = [];
$row['date'] = $date;
$row['data'] = [];
if (empty($content['column_separator'])) {
if (empty($keys)) {
$row['data'][][] = $rowUnser;
} else {
$row['data'][][$keys[0]] = $rowUnser;
}
} else {
$columnsUnserialize = explode(
$content['column_separator'],
$rowUnser
);
$i = 0;
$temp_row = [];
foreach ($columnsUnserialize as $cell) {
if (isset($keys[$i])) {
$temp_row[$keys[$i]] = $cell;
} else {
$temp_row[] = $cell;
}
$i++;
}
$row['data'][] = $temp_row;
}
$data[] = $row;
}
}
if ($config['metaconsole']) {
metaconsole_restore_db();
}
$return['data'] = $data;
return reporting_check_structure_content($return);
}
/**
* Show last value and state of module.
*
* @param array $report Data report.
* @param array $content Content report.
* @param integer $datetime Date limit of report.
* @param integer $init_date Init date of report.
*
* @return array
*/
function reporting_last_value($report, $content, $datetime, $period)
{
global $config;
try {
$id_meta = null;
if (is_metaconsole()) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
}
$module = new Module($content['id_agent_module'], false, $server['id']);
} catch (\Exception $e) {
$result = [];
return reporting_check_structure_content($result);
}
$return['type'] = 'last_value';
if (empty($content['name'])) {
$content['name'] = __('Last Value');
}
$id_agent = $module->agent()->id_agente();
$id_agent_module = $content['id_agent_module'];
$agent_alias = $module->agent()->alias();
$module_name = $module->name();
$agent_description = $module->agent()->comentarios();
$agent_group = $module->agent()->group();
$agent_address = $module->agent()->field['direccion'];
$module_description = $module->descripcion();
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_alias.' - '.$module_name;
$return['description'] = $content['description'];
$return['agent_name_db'] = $module->agent()->id_agente();
$return['agent_name'] = $agent_alias;
$return['module_name'] = $module_name;
$return['date'] = reporting_get_date_text($report, $content);
$result = $module->getStatus()->toArray();
if ($result === false
|| $result['estado'] == AGENT_MODULE_STATUS_NO_DATA
|| $result['estado'] == AGENT_MODULE_STATUS_NOT_INIT
) {
$result['utimestamp'] = '';
$result['datos'] = __('No data to display within the selected interval');
}
if ($datetime < $result['utimestamp']) {
$id_tipo_modulo = $module->id_tipo_modulo();
$table_data = modules_get_table_data(null, $id_tipo_modulo);
if ($period !== null) {
$sql = sprintf(
'SELECT datos, utimestamp FROM %s WHERE id_agente_modulo = %d AND utimestamp BETWEEN %d AND %d ORDER BY utimestamp DESC',
$table_data,
$id_agent_module,
($datetime - $period),
$datetime
);
} else {
$sql = sprintf(
'SELECT datos, utimestamp FROM %s WHERE id_agente_modulo = %d AND utimestamp <= %d ORDER BY utimestamp DESC',
$table_data,
$id_agent_module,
$datetime
);
}
$search_in_history_db = db_search_in_history_db($datetime);
try {
$module->connectNode();
} catch (\Exception $e) {
// Do not link items if failed to find them.
$module->restoreConnection();
}
$datos = db_get_row_sql($sql, $search_in_history_db);
// Restore if needed.
$module->restoreConnection();
if ($datos !== false) {
$result['datos'] = $datos['datos'];
$result['utimestamp'] = $datos['utimestamp'];
} else {
$result['utimestamp'] = '-';
$result['estado'] = AGENT_MODULE_STATUS_NO_DATA;
$result['datos'] = __('No data to display within the selected interval');
}
}
$result['agent_name'] = $agent_alias;
$result['module_name'] = $module_name;
$return['data'] = $result;
return reporting_check_structure_content($return);
}
function reporting_permissions($report, $content)
{
global $config;
$return['type'] = 'permissions_report';
if (empty($content['name'])) {
$content['name'] = __('Permissions report');
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$external_source = io_safe_input(json_decode($content['external_source'], true));
$id_users = $external_source['id_users'];
$id_groups = $external_source['users_groups'];
// Select subtype: group by user or by group.
$select_by_group = $external_source['select_by_group'];
$return['subtype'] = (int) $select_by_group;
if ($select_by_group === REPORT_PERMISSIONS_NOT_GROUP_BY_GROUP) {
foreach ($id_users as $id_user) {
$user_name = db_get_value_filter('fullname', 'tusuario', ['id_user' => $id_user]);
$sql = sprintf(
"SELECT name, id_grupo FROM tusuario_perfil
INNER JOIN tperfil ON tperfil.id_perfil = tusuario_perfil.id_perfil
WHERE tusuario_perfil.id_usuario like '%s'",
$id_user
);
$profiles = db_get_all_rows_sql($sql);
$user_profiles = [];
if (empty($profiles)) {
$user_profiles[] = __('The user doesn\'t have any assigned profile/group');
} else {
foreach ($profiles as $user_profile) {
$user_profiles[] = $user_profile['name'].' / '.groups_get_name($user_profile['id_grupo'], true);
}
}
$data[] = [
'user_id' => io_safe_output($id_user),
'user_name' => io_safe_output($user_name),
'user_profiles' => io_safe_output($user_profiles),
];
}
} else {
$group_users = [];
foreach ($id_groups as $id_group) {
if ($id_group === '') {
$group_name = __('Unnasigned group');
$sql = 'SELECT tusuario.id_user, tusuario.fullname FROM tusuario
LEFT OUTER JOIN tusuario_perfil
ON tusuario.id_user = tusuario_perfil.id_usuario
WHERE tusuario_perfil.id_usuario IS NULL';
$group_users = db_get_all_rows_sql($sql);
$group_name = __('Unassigned group');
} else {
$group_name = groups_get_name($id_group, true);
$group_users = users_get_user_users(
$id_group,
'AR',
false,
[
'id_user',
'fullname',
],
[$id_group]
);
}
$row['users'] = [];
foreach ($group_users as $user) {
$id_user = $user['id_user'];
// Get user fullanme.
$row['users'][$id_user]['fullname'] = $user['fullname'];
if ($id_group === '') {
$row['users'][$id_user]['profiles'][] = __('The user doesn\'t have any assigned profile/group');
} else {
// Incluide id group = 0 for 'All' profile.
$sql = sprintf(
"SELECT id_perfil FROM tusuario_perfil
WHERE id_usuario LIKE '%s'
AND (
id_grupo LIKE '%s'
OR id_grupo LIKE '0'
)",
$id_user,
$id_group
);
$user_profiles_id = db_get_all_rows_sql($sql);
foreach ($user_profiles_id as $profile_id) {
$row['users'][$id_user]['profiles'][] = profile_get_name($profile_id['id_perfil']);
}
}
}
$data[] = [
'group_name' => $group_name,
'users' => $row['users'],
];
}
}
$return['data'] = [];
$return['data'] = $data;
return reporting_check_structure_content($return);
}
function reporting_group_configuration($report, $content)
{
global $config;
$return['type'] = 'group_configuration';
if (empty($content['name'])) {
$content['name'] = __('Group configuration');
}
if ($config['metaconsole']) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$group_name = groups_get_name($content['id_group'], true);
$items_label = ['agent_group' => $group_name];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $group_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['id_group'] = $content['id_group'];
if ($content['id_group'] == 0) {
switch ($config['dbtype']) {
case 'mysql':
case 'postgresql':
$sql = 'SELECT * FROM tagente;';
break;
case 'oracle':
$sql = 'SELECT * FROM tagente';
break;
}
} else {
$sql = '
SELECT *
FROM tagente
WHERE id_grupo='.$content['id_group'];
}
$agents_list = db_get_all_rows_sql($sql);
if ($agents_list === false) {
$agents_list = [];
}
$return['data'] = [];
foreach ($agents_list as $agent) {
$content_agent = $content;
$content_agent['id_agent'] = $agent['id_agente'];
// Restore the connection to metaconsole
// because into the function reporting_agent_configuration
// connect to metaconsole.
if ($config['metaconsole']) {
metaconsole_restore_db();
}
$agent_report = reporting_agent_configuration(
$report,
$content_agent
);
$return['data'][] = $agent_report['data'];
}
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
function reporting_network_interfaces_report($report, $content, $type='dinamic', $pdf=0)
{
global $config;
$return['type'] = 'network_interfaces_report';
if (empty($content['name'])) {
$content['name'] = __('Network interfaces report');
}
if (isset($content['style']['fullscale'])) {
$fullscale = (bool) $content['style']['fullscale'];
}
$group_name = groups_get_name($content['id_group']);
$items_label = ['_agentgroup_' => $group_name];
// Apply macros
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $group_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
include_once $config['homedir'].'/include/functions_custom_graphs.php';
$filter = [
'id_grupo' => $content['id_group'],
'disabled' => 0,
];
$return['failed'] = null;
$return['data'] = [];
if (is_metaconsole() === true) {
metaconsole_restore_db();
$server_names = metaconsole_get_connection_names();
if (isset($server_names) === true
&& is_array($server_names) === true
) {
foreach ($server_names as $key => $value) {
$id_meta = metaconsole_get_id_server($value);
$connection = metaconsole_get_connection_by_id($id_meta);
if (metaconsole_connect($connection) != NOERR) {
continue;
} else {
$network_interfaces_by_agents = agents_get_network_interfaces(
false,
$filter
);
$return = agents_get_network_interfaces_array(
$network_interfaces_by_agents,
$return,
$type,
$content,
$report,
$fullscale,
$pdf,
$id_meta
);
metaconsole_restore_db();
}
}
}
} else {
$network_interfaces_by_agents = agents_get_network_interfaces(false, $filter);
$return = agents_get_network_interfaces_array(
$network_interfaces_by_agents,
$return,
$type,
$content,
$report,
$fullscale,
$pdf,
$id_meta
);
}
return reporting_check_structure_content($return);
}
function reporting_custom_render($report, $content, $type='dinamic', $pdf=0)
{
global $config;
$return['type'] = 'custom_render';
if (empty($content['name'])) {
$content['name'] = __('Custom render report');
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = '';
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['failed'] = null;
$macros = [];
$patterns = [];
$substitutions = [];
if (isset($content['macros_definition']) === true
&& empty($content['macros_definition']) === false
) {
$macros = json_decode(
io_safe_output($content['macros_definition']),
true
);
if (empty($macros) === false && is_array($macros) === true) {
foreach ($macros as $key_macro => $data_macro) {
switch ($data_macro['type']) {
case 0:
// Type: String.
$patterns[] = addslashes(
'/_'.$data_macro['name'].'_/'
);
$substitutions[] = $data_macro['value'];
break;
case 1:
// Type Sql value.
$patterns[] = addslashes(
'/_'.$data_macro['name'].'_/'
);
$regex = '/(UPDATE|INSERT INTO|DELETE FROM|TRUNCATE|DROP|ALTER|CREATE|GRANT|REVOKE)\s+(.*?)\s+/i';
if (preg_match($regex, $data_macro['value']) > 0) {
$value_query = __('This query is insecure, it could apply unwanted modiffications on the schema');
} else {
$error_reporting = error_reporting();
error_reporting(0);
$value_query = db_get_value_sql(
trim($data_macro['value'], ';')
);
if ($value_query === false) {
$value_query = __('Error: %s', $config['dbconnection']->error);
}
error_reporting($error_reporting);
}
$substitutions[] = $value_query;
break;
case 2:
// Type: SQL graph.
$patterns[] = addslashes(
'/_'.$data_macro['name'].'_/'
);
$regex = '/(UPDATE|INSERT INTO|DELETE FROM|TRUNCATE|DROP|ALTER|CREATE|GRANT|REVOKE)\s+(.*?)\s+/i';
if (preg_match($regex, $data_macro['value']) > 0) {
$value_query = __('This query is insecure, it could apply unwanted modiffications on the schema');
} else {
$error_reporting = error_reporting();
error_reporting(0);
$data_query = db_get_all_rows_sql(
trim($data_macro['value'], ';')
);
error_reporting($error_reporting);
if ($data_query === false) {
$value_query = __('Error: %s', $config['dbconnection']->error);
} else {
$width = 210;
if (isset($data_macro['width']) === true
&& empty($data_macro['width']) === false
) {
$width = $data_macro['width'];
}
$height = 210;
if (isset($data_macro['height']) === true
&& empty($data_macro['height']) === false
) {
$height = $data_macro['height'];
}
// TODO: Allow to paint horizontal and vertical bar graphs for the moment only pie graphs.
$type = 'sql_graph_pie';
$SQL_GRAPH_MAX_LABEL_SIZE = 5;
$count = 0;
$flagOther = false;
foreach ($data_query as $data_item) {
$count++;
$value = 0;
if (empty($data_item['value']) === false) {
$value = $data_item['value'];
}
if ($count <= 5) {
$label = __('Data');
if (empty($data_item['label']) === false) {
$label = io_safe_output($data_item['label']);
if (strlen($label) > $SQL_GRAPH_MAX_LABEL_SIZE) {
$first_label = $label;
$label = substr(
$first_label,
0,
floor($SQL_GRAPH_MAX_LABEL_SIZE / 2)
);
$label .= '...
';
$label .= substr(
$first_label,
floor(-$SQL_GRAPH_MAX_LABEL_SIZE / 2)
);
}
}
switch ($type) {
case 'sql_graph_vbar':
default:
// Vertical bar.
$data[] = [
'tick' => $label.'_'.$count,
'data' => $value,
];
break;
case 'sql_graph_hbar':
// Horizontal bar.
$data[$label.'_'.$count]['g'] = $value;
break;
case 'sql_graph_pie':
// Pie.
$data[$label.'_'.$count] = $value;
break;
}
} else {
switch ($type) {
case 'sql_graph_vbar':
default:
// Vertical bar.
if ($flagOther === false) {
$data[] = [
'tick' => __('Other'),
'data' => $value,
];
$flagOther = true;
}
$data[(count($data) - 1)]['data'] += $value;
break;
case 'sql_graph_hbar':
// Horizontal bar.
if (isset($data[__('Other')]['g']) === false) {
$data[__('Other')]['g'] = 0;
}
$data[__('Other')]['g'] += $value;
break;
case 'sql_graph_pie':
// Pie.
if (isset($data[__('Other')]) === false) {
$data[__('Other')] = 0;
}
$data[__('Other')] += $value;
break;
}
}
}
$value_query = pie_graph(
$data,
$width,
$height,
__('other'),
ui_get_full_url(false, false, false, false),
'',
$config['fontpath'],
$config['font_size'],
($pdf === true) ? 2 : 1,
'hidden',
'',
true
);
}
}
$substitutions[] = $value_query;
break;
case 3:
// Type: Simple graph.
$patterns[] = addslashes(
'/_'.$data_macro['name'].'_/'
);
$height = $config['graph_image_height'];
if (isset($data_macro['height']) === true
&& empty($data_macro['height']) === false
) {
$height = $data_macro['height'];
}
$period = SECONDS_1DAY;
if (isset($data_macro['period']) === true
&& empty($data_macro['period']) === false
) {
$period = $data_macro['period'];
}
if (is_metaconsole() === true) {
$server = db_get_row(
'tmetaconsole_setup',
'id',
$data_macro['server_id']
);
if (metaconsole_connect($server) != NOERR) {
continue 2;
}
}
$params = [
'agent_module_id' => $data_macro['id_agent_module'],
'period' => $period,
'title' => '',
'label' => '',
'pure' => false,
'only_image' => true,
'homeurl' => ui_get_full_url(
false,
false,
false,
false
),
'ttl' => ($pdf === true) ? 2 : 1,
'show_unknown' => true,
'height' => $height,
'backgroundColor' => 'transparent',
'return_img_base_64' => true,
'server_id' => (is_metaconsole() === true) ? $data_macro['server_id'] : 0,
];
$substitutions[] = '';
if (is_metaconsole() === true) {
metaconsole_restore_db();
}
break;
default:
// Not possible.
break;
}
}
}
}
$return['data'] = preg_replace(
$patterns,
$substitutions,
$content['render_definition']
);
return reporting_check_structure_content($return);
}
function agents_get_network_interfaces_array(
$network_interfaces_by_agents,
$return,
$type,
$content,
$report,
$fullscale,
$pdf,
$id_meta
) {
global $config;
if (empty($network_interfaces_by_agents) === true
&& is_metaconsole() === false
) {
$return['failed'] = __(
'The group has no agents or none of the agents has any network interface'
);
$return['data'] = [];
} else {
if (isset($network_interfaces_by_agents) === true
&& is_array($network_interfaces_by_agents) === true
) {
foreach ($network_interfaces_by_agents as $agent_id => $agent) {
$row_data = [];
$row_data['agent'] = $agent['name'];
$row_data['interfaces'] = [];
foreach ($agent['interfaces'] as $interface_name => $interface) {
$row_interface = [];
$row_interface['name'] = $interface_name;
$row_interface['ip'] = $interface['ip'];
$row_interface['mac'] = $interface['mac'];
$row_interface['status'] = $interface['status_image'];
$row_interface['chart'] = null;
$params = [
'period' => $content['period'],
'unit_name' => array_fill(0, count($interface['traffic']), __('bytes/s')),
'date' => $report['datetime'],
'only_image' => $pdf,
'homeurl' => $config['homeurl'],
'fullscale' => $fullscale,
'server_id' => $id_meta,
'height' => $config['graph_image_height'],
'landscape' => $content['landscape'],
'return_img_base_64' => true,
'backgroundColor' => 'transparent',
'graph_render' => $content['graph_render'],
];
$params_combined = [
'labels' => array_keys($interface['traffic']),
'modules_series' => array_values($interface['traffic']),
'stacked' => CUSTOM_GRAPH_LINE,
];
switch ($type) {
case 'dinamic':
case 'static':
if (!empty($interface['traffic'])) {
if ($pdf === false) {
$row_interface['chart'] = graphic_combined_module(
array_values($interface['traffic']),
$params,
$params_combined
);
} else {
$row_interface['chart'] = '';
}
}
break;
case 'data':
if (!empty($interface['traffic'])) {
$params['return_data'] = true;
$row_interface['chart'] = graphic_combined_module(
array_values($interface['traffic']),
$params,
$params_combined
);
}
break;
}
$row_data['interfaces'][] = $row_interface;
}
$return['data'][] = $row_data;
}
}
}
return $return;
}
/**
* reporting alert get fired
*/
function reporting_alert_get_fired($id_agent_module, $id_alert_template_module, $period, $datetime, $return_empty=true)
{
$fired = [];
$firedTimes = get_module_alert_fired(
$id_agent_module,
$id_alert_template_module,
$period,
$datetime
);
if (!is_numeric($datetime)) {
$datetime = time_w_fixed_tz($datetime);
}
if (empty($datetime)) {
$datetime = get_system_time();
}
$datelimit = ($datetime - $period);
$empty = '----------------------------';
if (empty($firedTimes)) {
$firedTimes = [];
$firedTimes[0]['timestamp'] = $empty;
}
foreach ($firedTimes as $fireTime) {
if ($fireTime['utimestamp'] > $datelimit && $fireTime['utimestamp'] <= $datetime) {
$fired[] = $fireTime['timestamp'];
} else {
if ($return_empty === true) {
$fired[] = $empty;
} else {
continue;
}
}
}
return $fired;
}
/**
* Reporting alert report group
*/
function reporting_alert_report_group($report, $content)
{
global $config;
$return['type'] = 'alert_report_group';
if (empty($content['name'])) {
$content['name'] = __('Alert Report Group');
}
if ($config['metaconsole']) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$group_name = groups_get_name($content['id_group'], true);
$items_label = ['agent_group' => $group_name];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $group_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$agent_modules = alerts_get_agent_modules(
$content['id_group'],
(((string) $content['id_group'] === '0') ? true : $content['recursion'])
);
if (empty($alerts)) {
$alerts = [];
}
$data = [];
foreach ($agent_modules as $agent_module) {
$data_row = [];
$data_row['agent'] = io_safe_output(
agents_get_alias(
agents_get_agent_id_by_module_id($agent_module['id_agent_module'])
)
);
$data_row['module'] = db_get_value_filter(
'nombre',
'tagente_modulo',
['id_agente_modulo' => $agent_module['id_agent_module']]
);
// Alerts over $id_agent_module
$alerts = alerts_get_effective_alert_actions($agent_module['id_agent_module']);
if ($alerts === false) {
continue;
}
$ntemplates = 0;
foreach ($alerts as $template => $actions) {
$datetime = (int) $report['datetime'];
if (!is_numeric($datetime)) {
$datetime = time_w_fixed_tz($datetime);
}
if (empty($datetime)) {
$datetime = get_system_time();
}
$period = (int) $content['period'];
$datelimit = ($datetime - $period);
$data_action = [];
$data_action['actions'] = [];
$naction = 0;
if (isset($actions['custom'])) {
foreach ($actions['custom'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0) {
$data_action[$naction]['fired'] = __('Not triggered');
} else if ($fired > 0) {
if ($fired > $datelimit && $fired < $datetime) {
$data_action[$naction]['fired'] = $fired;
} else {
continue 2;
}
}
$naction++;
}
} else if (isset($actions['default'])) {
foreach ($actions['default'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0) {
$data_action[$naction]['fired'] = __('Not triggered');
} else if ($fired > 0) {
if ($fired > $datelimit && $fired < $datetime) {
$data_action[$naction]['fired'] = $fired;
} else {
continue 2;
}
}
$naction++;
}
} else if (isset($actions['unavailable'])) {
foreach ($actions['unavailable'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0) {
$data_action[$naction]['fired'] = __('Not triggered');
} else if ($fired > 0) {
if ($fired > $datelimit && $fired < $datetime) {
$data_action[$naction]['fired'] = $fired;
} else {
continue 2;
}
}
$naction++;
}
}
$module_actions = [];
$module_actions['template'] = $template;
$module_actions['template_fired'] = reporting_alert_get_fired(
$agent_module['id_agent_module'],
$actions['id'],
(int) $content['period'],
(int) $report['datetime'],
false
);
$module_actions['actions'] = $data_action;
$data_row['alerts'][$ntemplates] = $module_actions;
$ntemplates++;
}
if ($ntemplates > 0) {
$data[] = $data_row;
}
}
$return['data'] = $data;
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
/**
* Report alert agent.
*
* @param array $report Info report.
* @param array $content Content report.
*
* @return array
*/
function reporting_alert_report_agent($report, $content)
{
global $config;
$return['type'] = 'alert_report_agent';
if (empty($content['name'])) {
$content['name'] = __('Alert Report Agent');
}
if ($config['metaconsole']) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$id_agent = $content['id_agent'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_alias;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($return['label'] != '') {
$return['label'] = reporting_label_macro(
$items_label,
($return['label'] ?? '')
);
}
$module_list = agents_get_modules($content['id_agent']);
$data = [];
foreach ($module_list as $id => $module_name) {
$data_row = [];
$data_row['agent'] = $agent_alias;
$data_row['module'] = $module_name;
// Alerts over $id_agent_module.
$alerts = alerts_get_effective_alert_actions($id);
if ($alerts === false) {
continue;
}
$ntemplates = 0;
foreach ($alerts as $template => $actions) {
$datetime = (int) $report['datetime'];
if (!is_numeric($datetime)) {
$datetime = time_w_fixed_tz($datetime);
}
if (empty($datetime)) {
$datetime = get_system_time();
}
$period = (int) $content['period'];
$datelimit = ($datetime - $period);
$data_action = [];
$data_action['actions'] = [];
$naction = 0;
if (isset($actions['custom'])) {
foreach ($actions['custom'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)) {
$data_action[$naction]['fired'] = '----------------------------';
} else {
$data_action[$naction]['fired'] = $fired;
}
$naction++;
}
} else if (isset($actions['default'])) {
foreach ($actions['default'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)) {
$data_action[$naction]['fired'] = '----------------------------';
} else {
$data_action[$naction]['fired'] = $fired;
}
$naction++;
}
} else if (isset($actions['unavailable'])) {
foreach ($actions['unavailable'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)) {
$data_action[$naction]['fired'] = '----------------------------';
} else {
$data_action[$naction]['fired'] = $fired;
}
$naction++;
}
}
$module_actions = [];
$module_actions['template'] = $template;
$module_actions['template_fired'] = reporting_alert_get_fired(
$id,
$actions['id'],
(int) $content['period'],
(int) $report['datetime']
);
$module_actions['actions'] = $data_action;
$data_row['alerts'][$ntemplates] = $module_actions;
$ntemplates++;
}
if ($ntemplates > 0) {
$data[] = $data_row;
}
}
$return['data'] = $data;
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
/**
* Alert report module.
*
* @param array $report Info report.
* @param array $content Content report.
*
* @return array
*/
function reporting_alert_report_module($report, $content)
{
global $config;
$return['type'] = 'alert_report_module';
if (empty($content['name'])) {
$content['name'] = __('Alert Report Module');
}
if ($config['metaconsole']) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$module_name = modules_get_agentmodule_name(
$id_agent_module
);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_alias.' - '.$module_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($return['label'] != '') {
$return['label'] = reporting_label_macro(
$items_label,
($return['label'] ?? '')
);
}
$data_row = [];
$data_row['agent'] = io_safe_output($agent_alias);
$data_row['module'] = db_get_value_filter(
'nombre',
'tagente_modulo',
['id_agente_modulo' => $content['id_agent_module']]
);
// Alerts over $id_agent_module.
$alerts = alerts_get_effective_alert_actions($content['id_agent_module']);
$ntemplates = 0;
foreach ($alerts as $template => $actions) {
$datetime = (int) $report['datetime'];
if (!is_numeric($datetime)) {
$datetime = time_w_fixed_tz($datetime);
}
if (empty($datetime)) {
$datetime = get_system_time();
}
$period = (int) $content['period'];
$datelimit = ($datetime - $period);
$data_action = [];
$data_action['actions'] = [];
$naction = 0;
if (isset($actions['custom'])) {
foreach ($actions['custom'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)) {
$data_action[$naction]['fired'] = '----------------------------';
} else {
$data_action[$naction]['fired'] = $fired;
}
$naction++;
}
} else if (isset($actions['default'])) {
foreach ($actions['default'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)) {
$data_action[$naction]['fired'] = '----------------------------';
} else {
$data_action[$naction]['fired'] = $fired;
}
$naction++;
}
} else if (isset($actions['unavailable'])) {
foreach ($actions['unavailable'] as $action) {
$data_action[$naction]['name'] = $action['name'];
$fired = $action['fired'];
if ($fired == 0 || ($fired <= $datelimit || $fired > $datetime)) {
$data_action[$naction]['fired'] = '----------------------------';
} else {
$data_action[$naction]['fired'] = $fired;
}
$naction++;
}
}
$module_actions = [];
$module_actions['template'] = $template;
$module_actions['template_fired'] = reporting_alert_get_fired(
$content['id_agent_module'],
$actions['id'],
(int) $content['period'],
(int) $report['datetime']
);
$module_actions['actions'] = $data_action;
$data_row['alerts'][$ntemplates] = $module_actions;
$ntemplates++;
}
if ($ntemplates > 0) {
$data[] = $data_row;
}
$return['data'] = $data;
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
/**
* Sql graph.
*
* @param array $report Info report.
* @param array $content Content.
* @param string $type Type.
* @param integer $force_width_chart Width.
* @param integer $force_height_chart Height.
* @param string $type_sql_graph Type.
*
* @return array Return array.
*/
function reporting_sql_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
$type_sql_graph
) {
global $config;
switch ($type_sql_graph) {
case 'sql_graph_hbar':
default:
$return['type'] = 'sql_graph_hbar';
break;
case 'sql_graph_vbar':
$return['type'] = 'sql_graph_vbar';
break;
case 'sql_graph_pie':
$return['type'] = 'sql_graph_pie';
break;
}
if (empty($content['name']) === true) {
switch ($type_sql_graph) {
case 'sql_graph_vbar':
default:
$content['name'] = __('SQL Graph Vertical Bars');
break;
case 'sql_graph_hbar':
$content['name'] = __('SQL Graph Horizontal Bars');
break;
case 'sql_graph_pie':
$content['name'] = __('SQL Graph Pie');
break;
}
}
// Get chart.
reporting_set_conf_charts(
$width,
$height,
$only_image,
$type,
$content,
$ttl
);
if (empty($force_width_chart) === false) {
$width = $force_width_chart;
}
if (empty($force_height_chart) === false) {
$height = $force_height_chart;
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text();
$module_source = db_get_all_rows_sql(
'SELECT id_agent_module
FROM tgraph_source
WHERE id_graph = '.$content['id_gs']
);
if (isset($module_source) === true && is_array($module_source) === true) {
$modules = [];
foreach ($module_source as $key => $value) {
$modules[$key] = $value['id_agent_module'];
}
}
switch ($type) {
case 'dinamic':
case 'static':
default:
$return['chart'] = graph_custom_sql_graph(
$content,
$width,
$height,
$content['type'],
$only_image,
ui_get_full_url(false, false, false, false),
$ttl,
$content['top_n_value']
);
break;
case 'data':
$data = [];
$data = db_get_all_rows_sql($content['external_source']);
$return['chart'] = $data;
break;
}
return reporting_check_structure_content($return);
}
/**
* Monitor report module.
*
* @param array $report Info report.
* @param array $content Content report.
*
* @return array
*/
function reporting_monitor_report($report, $content)
{
global $config;
$return['type'] = 'monitor_report';
if (empty($content['name'])) {
$content['name'] = __('Monitor Report');
}
if (is_metaconsole()) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$module_name = modules_get_agentmodule_name(
$id_agent_module
);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_alias.' - '.$module_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($return['label'] != '') {
$return['label'] = reporting_label_macro(
$items_label,
($return['label'] ?? '')
);
}
$return['agent_name_db'] = agents_get_name($id_agent);
$return['agent_name'] = $agent_alias;
$return['module_name'] = $module_name;
// All values (except id module and report time) by default.
$report = reporting_advanced_sla(
$content['id_agent_module'],
($report['datetime'] - $content['period']),
$report['datetime']
);
if ($report['time_total'] === $report['time_unknown']
|| empty($content['id_agent_module'])
) {
$return['data']['unknown'] = 1;
} else {
$return['data']['ok']['value'] = $report['SLA'];
$return['data']['ok']['formated_value'] = $report['SLA_fixed'];
$return['data']['fail']['value'] = (100 - $return['data']['ok']['value']);
$return['data']['fail']['formated_value'] = (100 - $return['data']['ok']['formated_value']);
}
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
/**
* Generates the data structure to build a netflow report.
*
* @param array $report Global report info.
* @param array $content Report item info.
* @param string $type Report type (static, dynamic, data).
* @param integer $force_width_chart Fixed width chart.
* @param integer $force_height_chart Fixed height chart.
* @param string $type_netflow One of netflow_area, netflow_data,
* netflow_summary.
* @param boolean $pdf True if a pdf report is generating.
*
* @return array Report item structure.
*/
function reporting_netflow(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart,
$type_netflow=null,
$pdf=false
) {
global $config;
switch ($type_netflow) {
case 'netflow_area':
$return['type'] = 'netflow_area';
break;
case 'netflow_data':
$return['type'] = 'netflow_data';
break;
case 'netflow_summary':
$return['type'] = 'netflow_summary';
break;
case 'netflow_top_N':
$return['type'] = 'netflow_top_N';
break;
default:
$return['type'] = 'unknown';
break;
}
if (empty($content['name'])) {
switch ($type_netflow) {
case 'netflow_area':
$content['name'] = __('Netflow Area');
break;
case 'netflow_summary':
$content['name'] = __('Netflow Summary');
break;
case 'netflow_data':
$content['name'] = __('Netflow Data');
break;
case 'netflow_top_N':
$content['name'] = __('Netflow top-N connections');
break;
default:
$content['name'] = __('Unknown report');
break;
}
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
// Get chart.
reporting_set_conf_charts(
$width,
$height,
$only_image,
$type,
$content,
$ttl
);
if (!empty($force_width_chart)) {
$width = $force_width_chart;
}
if (!empty($force_height_chart)) {
$height = $force_height_chart;
}
// Get item filters.
$filter = db_get_row_sql(
"SELECT *
FROM tnetflow_filter
WHERE id_sg = '".(int) $content['text']."'",
false,
true
);
if ($type_netflow === 'netflow_top_N') {
// Aggregate by destination port.
$filter['aggregate'] = 'dstport';
}
switch ($type) {
case 'dinamic':
case 'static':
$return['chart'] = netflow_draw_item(
($report['datetime'] - $content['period']),
$report['datetime'],
$content['top_n'],
$type_netflow,
$filter,
$content['top_n_value'],
$content['server_name'],
(($pdf === true) ? 'PDF' : 'HTML')
);
break;
case 'data':
$data = netflow_get_item_data(
($report['datetime'] - $content['period']),
$report['datetime'],
$content['top_n'],
$type_netflow,
$filter,
$content['top_n_value'],
$content['server_name']
);
$return['data'] = (array_key_exists('data', $data) === true) ? $data['data'] : $data;
break;
default:
// Nothing to do.
break;
}
$return['subtitle'] = netflow_generate_subtitle_report(
$filter['aggregate'],
$content['top_n'],
$type_netflow
);
return reporting_check_structure_content($return);
}
function reporting_prediction_date($report, $content)
{
global $config;
$return['type'] = 'prediction_date';
if (empty($content['name'])) {
$content['name'] = __('Prediction Date');
}
$module_name = io_safe_output(
modules_get_agentmodule_name($content['id_agent_module'])
);
$agent_name = io_safe_output(
modules_get_agentmodule_agent_alias($content['id_agent_module'])
);
$agent_name_db = io_safe_output(modules_get_agentmodule_agent_name($content['id_agent_module']));
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$module_name = modules_get_agentmodule_name(
$id_agent_module
);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$description = (isset($content['description'])) ? $content['description'] : '';
if ($description != '') {
$description = reporting_label_macro(
$items_label,
($description ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_name.' - '.$module_name;
$return['description'] = $description;
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
$return['agent_name_db'] = $agent_name_db;
$return['agent_name'] = $agent_name;
$return['module_name'] = $module_name;
$intervals_text = explode(';', $content['text']);
$max_interval = $intervals_text[0];
$min_interval = $intervals_text[1];
$value = forecast_prediction_date($content['id_agent_module'], $content['period'], $max_interval, $min_interval, $content['server_name']);
if ($value === false) {
$return['data']['value'] = __('Unknown');
} else {
$return['data']['value'] = date('d M Y H:i:s', $value);
}
return reporting_check_structure_content($return);
}
function reporting_projection_graph(
$report,
$content,
$type='dinamic',
$force_width_chart=null,
$force_height_chart=null,
$pdf=false
) {
global $config;
if ($config['metaconsole']) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$return['type'] = 'projection_graph';
if (empty($content['name'])) {
$content['name'] = __('Projection Graph');
}
$module_name = io_safe_output(modules_get_agentmodule_name($content['id_agent_module']));
$agent_name = io_safe_output(modules_get_agentmodule_agent_alias($content['id_agent_module']));
$agent_name_db = io_safe_output(modules_get_agentmodule_agent_name($content['id_agent_module']));
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$module_name = modules_get_agentmodule_name(
$id_agent_module
);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['subtitle'] = $agent_name.' - '.$module_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
$return['agent_name_db'] = $agent_name_db;
$return['agent_name'] = $agent_name;
$return['module_name'] = $module_name;
set_time_limit(500);
switch ($type) {
case 'dinamic':
case 'static':
$params = [
'period' => $content['period'],
'width' => $width,
'date' => $report['datetime'],
'unit' => '',
'only_image' => $pdf,
'homeurl' => ui_get_full_url(false, false, false, false).'/',
'ttl' => $ttl,
'server_id' => $id_meta,
'height' => $config['graph_image_height'],
'landscape' => $content['landscape'],
'return_img_base_64' => true,
];
$params_combined = [
'projection' => $content['top_n_value'],
];
if ($pdf === true) {
$return['chart'] = '';
} else {
$return['chart'] = graphic_combined_module(
[$content['id_agent_module']],
$params,
$params_combined
);
}
break;
case 'data':
$return['data'] = forecast_projection_graph(
$content['id_agent_module'],
$content['period'],
$content['top_n_value'],
false,
false,
true
);
break;
}
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
function reporting_agent_configuration($report, $content)
{
global $config;
$return['type'] = 'agent_configuration';
if (empty($content['name'])) {
$content['name'] = __('Agent configuration');
}
$sql = '
SELECT *
FROM tagente
WHERE id_agente='.$content['id_agent'];
$agent_data = db_get_row_sql($sql);
$id_agent = $content['id_agent'];
$agent_alias = $agent_data['alias'];
$agent_group = groups_get_name($agent_data['id_grupo']);
$agent_description = $agent_data['comentarios'];
$agent_address = $agent_data['direccion'];
$items_label = [
'id_agent' => $id_agent,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
];
// Apply macros.
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($config['metaconsole']) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$agent_configuration = [];
$agent_configuration['name'] = $agent_alias;
$agent_configuration['group'] = $agent_group;
$agent_configuration['group_icon'] = ui_print_group_icon($agent_data['id_grupo'], true, '', '', false);
$agent_configuration['os'] = os_get_name($agent_data['id_os']);
$agent_configuration['os_icon'] = ui_print_os_icon($agent_data['id_os'], true, true);
$agent_configuration['address'] = $agent_address;
$agent_configuration['description'] = $agent_description;
$agent_configuration['enabled'] = (int) !$agent_data['disabled'];
$agent_configuration['group'] = $report['group'];
$modules = agents_get_modules($content['id_agent']);
$agent_configuration['modules'] = [];
// Agent's modules
if (!empty($modules)) {
foreach ($modules as $id_agent_module => $module) {
$sql = "
SELECT *
FROM tagente_modulo
WHERE id_agente_modulo = $id_agent_module";
$module_db = db_get_row_sql($sql);
$data_module = [];
$data_module['name'] = $module_db['nombre'];
if ($module_db['disabled']) {
$data_module['name'] .= ' ('.__('Disabled').')';
}
$data_module['type_icon'] = ui_print_moduletype_icon($module_db['id_tipo_modulo'], true);
$data_module['type'] = modules_get_type_name($module_db['id_tipo_modulo']);
$data_module['max_warning'] = $module_db['max_warning'];
$data_module['min_warning'] = $module_db['min_warning'];
$data_module['max_critical'] = $module_db['max_critical'];
$data_module['min_critical'] = $module_db['min_critical'];
$data_module['threshold'] = $module_db['module_ff_interval'];
$data_module['description'] = $module_db['descripcion'];
if (($module_db['module_interval'] == 0)
|| ($module_db['module_interval'] == '')
) {
$data_module['interval'] = db_get_value(
'intervalo',
'tagente',
'id_agente',
$content['id_agent']
);
} else {
$data_module['interval'] = $module_db['module_interval'];
}
$data_module['unit'] = $module_db['unit'];
$module_status = db_get_row(
'tagente_estado',
'id_agente_modulo',
$id_agent_module
);
modules_get_status(
$id_agent_module,
$module_status['estado'],
$module_status['datos'],
$status,
$title
);
$data_module['status_icon'] = ui_print_status_image($status, $title, true);
$data_module['status'] = $title;
$sql_tag = "
SELECT name
FROM ttag
WHERE id_tag IN (
SELECT id_tag
FROM ttag_module
WHERE id_agente_modulo = $id_agent_module)";
$tags = db_get_all_rows_sql($sql_tag);
if ($tags === false) {
$data_module['tags'] = [];
} else {
foreach ($tags as $tag) {
$data_module['tags'][] = $tag['name'];
}
}
$agent_configuration['modules'][] = $data_module;
}
}
$return['data'] = $agent_configuration;
if ($config['metaconsole']) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
/**
* Report Min, Max and Avg.
*
* @param array $report Info report.
* @param array $content Content report.
* @param string $type Type report.
* @param boolean $pdf Is pdf.
*
* @return array Data draw report.
*/
function reporting_value($report, $content, $type, $pdf=false)
{
global $config;
$return = [];
switch ($type) {
case 'max':
$return['type'] = 'max_value';
break;
case 'min':
$return['type'] = 'min_value';
break;
case 'sum':
$return['type'] = 'sumatory';
break;
case 'avg':
default:
$return['type'] = 'avg_value';
break;
}
if (empty($content['name']) === true) {
switch ($type) {
case 'max':
$content['name'] = __('Max. Value');
break;
case 'min':
$content['name'] = __('Min. Value');
break;
case 'sum':
$content['name'] = __('Summatory');
break;
case 'avg':
default:
$content['name'] = __('AVG. Value');
break;
}
}
if (is_metaconsole() === true) {
$id_meta = metaconsole_get_id_server($content['server_name']);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
$module_name = io_safe_output(
modules_get_agentmodule_name($content['id_agent_module'])
);
$agent_name = io_safe_output(
modules_get_agentmodule_agent_alias($content['id_agent_module'])
);
$agent_name_db = io_safe_output(
modules_get_agentmodule_agent_name($content['id_agent_module'])
);
$unit = db_get_value(
'unit',
'tagente_modulo',
'id_agente_modulo',
$content['id_agent_module']
);
$id_agent = agents_get_module_id(
$content['id_agent_module']
);
$id_agent_module = $content['id_agent_module'];
$agent_description = agents_get_description($id_agent);
$agent_group = agents_get_agent_group($id_agent);
$agent_address = agents_get_address($id_agent);
$agent_alias = agents_get_alias($id_agent);
$module_name = modules_get_agentmodule_name(
$id_agent_module
);
$module_description = modules_get_agentmodule_descripcion(
$id_agent_module
);
$items_label = [
'type' => $return['type'],
'id_agent' => $id_agent,
'id_agent_module' => $id_agent_module,
'agent_description' => $agent_description,
'agent_group' => $agent_group,
'agent_address' => $agent_address,
'agent_alias' => $agent_alias,
'module_name' => $module_name,
'module_description' => $module_description,
];
// Apply macros
$title = (isset($content['name'])) ? $content['name'] : '';
if ($title != '') {
$title = reporting_label_macro(
$items_label,
($title ?? '')
);
}
$label = (isset($content['style']['label'])) ? $content['style']['label'] : '';
if ($label != '') {
$label = reporting_label_macro(
$items_label,
($label ?? '')
);
}
$return['title'] = $title;
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['subtitle'] = $agent_name.' - '.$module_name;
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content);
$return['label'] = $label;
$return['agents'] = [$content['id_agent']];
$return['id_agent'] = $content['id_agent'];
$return['id_agent_module'] = $content['id_agent_module'];
$return['agent_name_db'] = $agent_name_db;
$return['agent_name'] = $agent_name;
$return['module_name'] = $module_name;
$only_image = false;
if ($pdf) {
$only_image = true;
}
$params = [
'agent_module_id' => $content['id_agent_module'],
'period' => $content['period'],
'width' => '90%',
'pure' => false,
'date' => $report['datetime'],
'only_image' => $only_image,
'homeurl' => ui_get_full_url(false, false, false, false),
'ttl' => 1,
'type_graph' => $config['type_module_charts'],
'time_interval' => $content['lapse'],
'server_id' => $id_meta,
'height' => $config['graph_image_height'],
'fullscale' => true,
'landscape' => $content['landscape'],
'return_img_base_64' => true,
];
switch ($type) {
case 'max':
case 'min':
case 'avg':
default:
$divisor = get_data_multiplier($unit);
if ($content['lapse_calc'] == 0) {
switch ($type) {
case 'max':
$value = reporting_get_agentmodule_data_max(
$content['id_agent_module'],
$content['period'],
$report['datetime']
);
break;
case 'min':
$value = reporting_get_agentmodule_data_min(
$content['id_agent_module'],
$content['period'],
$report['datetime']
);
break;
case 'avg':
default:
$value = reporting_get_agentmodule_data_average(
$content['id_agent_module'],
$content['period'],
$report['datetime']
);
break;
}
if (!$config['simple_module_value']) {
$formated_value = $value;
} else {
$formated_value = format_for_graph(
$value,
$config['graph_precision'],
'.',
',',
$divisor,
$unit
);
}
} else {
$return['visual_format'] = $content['visual_format'];
switch ($type) {
case 'max':
$params['force_interval'] = 'max_only';
$value = format_for_graph(
reporting_get_agentmodule_data_max(
$content['id_agent_module'],
$content['period'],
$report['datetime']
),
$config['graph_precision'],
'.',
',',
$divisor,
$unit
);
break;
case 'min':
$params['force_interval'] = 'min_only';
$value = format_for_graph(
reporting_get_agentmodule_data_min(
$content['id_agent_module'],
$content['period'],
$report['datetime']
),
$config['graph_precision'],
'.',
',',
$divisor,
$unit
);
break;
case 'avg':
default:
$params['force_interval'] = 'avg_only';
$value = format_for_graph(
reporting_get_agentmodule_data_average(
$content['id_agent_module'],
$content['period'],
$report['datetime']
),
$config['graph_precision'],
'.',
',',
$divisor,
$unit
);
break;
}
$return['data'][] = [
__('Agent') => $agent_name,
__('Module') => $module_name,
__('Maximun') => $value,
];
if ($content['visual_format'] != 1) {
if ($only_image === false) {
$graph = grafico_modulo_sparse($params);
} else {
$graph = '';
}
$return['data'][] = ['value' => $graph];
}
if ($content['visual_format'] != 2) {
$time_begin = db_get_row_sql('select utimestamp from tagente_datos where id_agente_modulo ='.$content['id_agent_module'], true);
for ($i = ($report['datetime'] - $content['period']); $i < $report['datetime']; $i += $content['lapse']) {
$row = [];
$row[__('Lapse')] = date('Y-m-d H:i:s', ($i + 1)).' to '.date('Y-m-d H:i:s', (($i + $content['lapse']) ));
if ($i > $time_begin['utimestamp']) {
switch ($type) {
case 'max':
$row[__('Maximun')] = format_for_graph(
reporting_get_agentmodule_data_max(
$content['id_agent_module'],
$content['lapse'],
($i + $content['lapse'])
),
$config['graph_precision'],
'.',
',',
$divisor,
$unit
);
break;
case 'min':
$row[__('Maximun')] = format_for_graph(
reporting_get_agentmodule_data_min(
$content['id_agent_module'],
$content['lapse'],
($i + $content['lapse'])
),
$config['graph_precision'],
'.',
',',
$divisor,
$unit
);
break;
case 'avg':
default:
$row[__('Maximun')] = format_for_graph(
reporting_get_agentmodule_data_average(
$content['id_agent_module'],
$content['lapse'],
($i + $content['lapse'])
),
$config['graph_precision'],
'.',
',',
$divisor,
$unit
);
break;
}
} else {
$row[__('Maximun')] = 'N/A';
}
$return['data'][] = $row;
}
}
if (is_metaconsole() === true) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
break;
case 'sum':
$value = reporting_get_agentmodule_data_sum(
$content['id_agent_module'],
$content['period'],
$report['datetime'],
$content['uncompressed_module']
);
if (!$config['simple_module_value']) {
$formated_value = $value;
} else {
$divisor = get_data_multiplier($unit);
$formated_value = format_for_graph(
$value,
$config['graph_precision'],
'.',
',',
$divisor,
$unit
);
}
break;
}
$return['data'] = [
'value' => $value,
'formated_value' => $formated_value,
];
if (is_metaconsole() === true) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
function reporting_url($report, $content, $type='dinamic')
{
global $config;
$return = [];
$return['type'] = 'url';
if (empty($content['name'])) {
$content['name'] = __('Url');
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text();
$return['url'] = $content['external_source'];
switch ($type) {
case 'dinamic':
$return['data'] = null;
break;
case 'data':
case 'static':
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $content['external_source']);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curlObj);
curl_close($curlObj);
$return['data'] = $output;
break;
}
return reporting_check_structure_content($return);
}
function reporting_text($report, $content)
{
global $config;
$return = [];
$return['type'] = 'text';
if (empty($content['name'])) {
$content['name'] = __('Text');
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text();
$return['data'] = html_entity_decode($content['text']);
return reporting_check_structure_content($return);
}
function reporting_sql($report, $content)
{
global $config;
$return = [];
$return['type'] = 'sql';
if (empty($content['name'])) {
$content['name'] = __('SQL');
}
$return['title'] = $content['name'];
$return['landscape'] = $content['landscape'];
$return['pagebreak'] = $content['pagebreak'];
$return['description'] = $content['description'];
$return['date'] = reporting_get_date_text();
if ($config['metaconsole'] && !empty($content['server_name'])) {
$id_meta = metaconsole_get_id_server(
$content['server_name']
);
$server = metaconsole_get_connection_by_id($id_meta);
metaconsole_connect($server);
}
if ($content['treport_custom_sql_id'] != 0) {
$sql = io_safe_output(
db_get_value_filter(
'`sql`',
'treport_custom_sql',
['id' => $content['treport_custom_sql_id']]
)
);
} else {
$sql = $content['external_source'];
}
// Check if exist sql macro.
$sql = reporting_sql_macro($report, $sql);
// Do a security check on SQL coming from the user.
$sql = check_sql($sql);
$return['sql'] = $sql;
$return['correct'] = 1;
$return['error'] = '';
$return['data'] = [];
if ($sql != '') {
$header = [];
if ($content['header_definition'] != '') {
$header = explode('|', $content['header_definition']);
$return['header'] = $header;
}
if ($content['id_rc'] != null) {
$historical_db = db_get_value_sql(
'SELECT historical_db from treport_content where id_rc ='.$content['id_rc']
);
if (is_metaconsole() === true) {
$historical_db = $content['historical_db'];
}
} else {
$historical_db = $content['historical_db'];
}
$result = db_get_all_rows_sql($sql, $historical_db);
if ($result !== false) {
foreach ($result as $row) {
$data_row = [];
$i = 0;
foreach ($row as $dbkey => $field) {
if (isset($header[$i])) {
$key = $header[$i];
} else {
$key = $dbkey;
}
$data_row[$key] = $field;
$i++;
}
$return['data'][] = $data_row;
}
}
} else {
$return['correct'] = 0;
$return['error'] = __('Illegal query: Due security restrictions, there are some tokens or words you cannot use: *, delete, drop, alter, modify, password, pass, insert or update.');
}
if ($config['metaconsole'] && !empty($content['server_name'])) {
metaconsole_restore_db();
}
return reporting_check_structure_content($return);
}
//
// Truncates a value
//
// Returns the truncated value
//
function sla_truncate($num, $accurancy=2)
{
if (!isset($accurancy)) {
$accurancy = 2;
}
$mult = pow(10, $accurancy);
return (floor($num * $mult) / $mult);
}
/**
* SLA check value.
*
* @param integer $value Value.
* @param integer $min Treshold min SLA.
* @param integer $max Treshold max SLA.
* @param boolean $inverse_interval Treshold inverse SLA.
*
* @return boolean Returns the interval in downtime (false if no matches).
*/
function sla_check_value($value, $min, $max, $inverse_interval=0)
{
if (!isset($inverse_interval)) {
$inverse_interval = 0;
}
if ((!isset($max)) && (!isset($min))) {
// Disabled thresholds.
return true;
}
if ($max == $min) {
// Equal.
if ($value == $max) {
return ($inverse_interval == 0) ? true : false;
}
return ($inverse_interval == 0) ? false : true;
}
if (!isset($max)) {
// Greater or equal than min.
if ($value >= $min) {
return ($inverse_interval == 0) ? true : false;
}
return ($inverse_interval == 0) ? false : true;
}
if (!isset($min)) {
// Smaller or equal than max.
if ($value <= $max) {
return ($inverse_interval == 0) ? true : false;
}
return ($inverse_interval == 0) ? false : true;
}
if (($value >= $min) && ($value <= $max)) {
return ($inverse_interval == 0) ? true : false;
}
return ($inverse_interval == 0) ? false : true;
}
/**
* SLA downtime worktime.
*
* Check (if needed) if the range specified by wt_start and wt_end is downtime.
*
* Only used for inclusive downtimes calculation (from sla_fixed_worktime).
*
* @param integer $wt_start Start of the range.
* @param integer $wt_end End of the range.
* @param boolean $inclusive_downtimes Boolean.
* @param array $planned_downtimes Array with the planned downtimes (ordered and merged).
*
* @return integer Returns the interval in downtime (false if no matches).
*/
function sla_downtime_worktime(
$wt_start,
$wt_end,
$inclusive_downtimes=1,
$planned_downtimes=null
) {
if ((!isset($planned_downtimes)) || (!is_array($planned_downtimes))) {
return false;
}
if ((!isset($wt_start)) || (!isset($wt_end)) || ($wt_start > $wt_end)) {
return false;
}
if ($inclusive_downtimes != 1) {
return false;
}
$rt = false;
foreach ($planned_downtimes as $pd) {
if (($wt_start >= $pd['date_from'])
&& ($wt_start <= $pd['date_to'])
&& ($wt_end >= $pd['date_from'])
&& ($wt_end <= $pd['date_to'])
) {
// ..[..start..end..]..
$rt = ($wt_end - $wt_start);
break;
} else if (($wt_start < $pd['date_from'])
&& ($wt_end > $pd['date_from'])
&& ($wt_end < $pd['date_to'])
) {
// ..start..[..end..]..
$rt = ($wt_end - $pd['date_from']);
break;
} else if (($wt_start >= $pd['date_from'])
&& ($wt_start < $pd['date_to'])
&& ($wt_end > $pd['date_to'])
) {
// ..[..start..]..end..
$rt = ($pd['date_to'] - $wt_start);
break;
} else if (($wt_start >= $pd['date_to'])
&& ($wt_end >= $pd['date_to'])
) {
// ..[..]..start..end..
} else {
// ..start..end..[..]..
}
}
return $rt;
}
/**
* SLA fixed worktime
*
* Check (if needed) if the range specified by wt_start and wt_end is a valid
* range or not.
*
* As worktime is order (older ... newer) the idx works as flag to identify
* last range checked, in order to improve the algorythm performance.
*
* @param integer $wt_start Start of the range.
* @param integer $wt_end End of the range.
* @param array $worktime Hash containing the valid intervals.
* @param array $planned_downtimes Array with the planned downtimes (ordered and merged).
* @param integer $inclusive_downtimes In downtime as OK (1) or ignored (0).
* @param integer $idx Last ranges checked.
*
* @return array
*/
function sla_fixed_worktime(
$wt_start,
$wt_end,
$worktime=null,
$planned_downtimes=null,
$inclusive_downtimes=1,
$idx=0
) {
$return = [];
// Accept all ranges by default.
$return['wt_valid'] = 1;
$return['interval'] = ($wt_end - $wt_start);
if ((!isset($wt_start)) || (!isset($wt_end))
|| ($wt_start > $wt_end) || ($wt_start > time())
) {
$return['wt_valid'] = 0;
$return['interval'] = 0;
}
// No exclusions defined, entire worktime is valid.
if ((!isset($worktime) || (!is_array($worktime)))) {
$time_in_downtime = sla_downtime_worktime(
$wt_start,
$wt_end,
$inclusive_downtimes,
$planned_downtimes
);
if ($time_in_downtime != false) {
$return['wt_in_downtime'] = 1;
$return['downtime_interval'] = $time_in_downtime;
$return['interval'] -= $time_in_downtime;
}
return $return;
}
// Check exceptions.
$total = count($worktime);
$return['idx'] = $idx;
if (!(($idx <= $total) && ($idx >= 0))) {
$idx = 0;
}
$start_fixed = 0;
for ($i = $idx; $i < $total; $i++) {
$wt = $worktime[$i];
if ($start_fixed == 1) {
// Intervals greater than 1 DAY.
if ($wt_end < $wt['date_from']) {
// Case G: ..end..[..]..
$time_in_downtime = sla_downtime_worktime(
$wt_start,
$wt_end,
$inclusive_downtimes,
$planned_downtimes
);
if ($time_in_downtime != false) {
$return['wt_in_downtime'] = 1;
$return['downtime_interval'] = $time_in_downtime;
$return['interval'] -= $time_in_downtime;
}
// Ignore older worktimes
$return['idx'] = $i;
return $return;
}
if (($wt_end >= $wt['date_from'])
&& ($wt_end <= $wt['date_to'])
) {
// Case H: ..[..end..]..
// add last slice.
$return['interval'] += ($wt_end - $wt['date_from']);
$time_in_downtime = sla_downtime_worktime(
$wt['date_from'],
$wt_end,
$inclusive_downtimes,
$planned_downtimes
);
if ($time_in_downtime != false) {
$return['wt_in_downtime'] = 1;
$return['downtime_interval'] = $time_in_downtime;
$return['interval'] -= $time_in_downtime;
}
return $return;
}
if (($wt_end > $wt['date_from'])
&& ($wt_end > $wt['date_to'])
) {
// Case H: ..[..]..end..
// Add current slice and continue checking.
$return['interval'] += ($wt['date_to'] - $wt['date_from']);
$time_in_downtime = sla_downtime_worktime(
$wt['date_from'],
$wt['date_to'],
$inclusive_downtimes,
$planned_downtimes
);
if ($time_in_downtime != false) {
$return['wt_in_downtime'] = 1;
$return['downtime_interval'] = $time_in_downtime;
$return['interval'] -= $time_in_downtime;
}
}
} else {
if (($wt_start < $wt['date_from'])
&& ($wt_end < $wt['date_from'])
) {
// Case A: ..start..end..[...]......
$return['wt_valid'] = 0;
$return['idx'] = $i;
return $return;
}
if (($wt_start <= $wt['date_from'])
&& ($wt_end >= $wt['date_from'])
&& ($wt_end < $wt['date_to'])
) {
// Case B: ...start..[..end..]......
$return['wt_valid'] = 1;
$return['interval'] = ($wt_end - $wt['date_from']);
$time_in_downtime = sla_downtime_worktime(
$wt['date_from'],
$wt_end,
$inclusive_downtimes,
$planned_downtimes
);
if ($time_in_downtime != false) {
$return['wt_in_downtime'] = 1;
$return['downtime_interval'] = $time_in_downtime;
$return['interval'] -= $time_in_downtime;
}
return $return;
}
if (($wt_start >= $wt['date_from'])
&& ($wt_start <= $wt['date_to'])
&& ($wt_end >= $wt['date_from'])
&& ($wt_end <= $wt['date_to'])
) {
// Case C: ...[..start..end..]......
$return['wt_valid'] = 1;
$time_in_downtime = sla_downtime_worktime(
$wt_start,
$wt_end,
$inclusive_downtimes,
$planned_downtimes
);
if ($time_in_downtime != false) {
$return['wt_in_downtime'] = 1;
$return['downtime_interval'] = $time_in_downtime;
$return['interval'] -= $time_in_downtime;
}
return $return;
}
if (($wt_start >= $wt['date_from'])
&& ($wt_start < $wt['date_to'])
&& ($wt_end > $wt['date_to'])
) {
// Case D: ...[..start..]...end.....
$return['interval'] = ($wt['date_to'] - $wt_start);
$time_in_downtime = sla_downtime_worktime(
$wt_start,
$wt['date_to'],
$inclusive_downtimes,
$planned_downtimes
);
if ($time_in_downtime != false) {
$return['wt_in_downtime'] = 1;
$return['downtime_interval'] = $time_in_downtime;
$return['interval'] -= $time_in_downtime;
}
$return['wt_valid'] = 1;
$start_fixed = 1;
// We must check if 'end' is greater than the next valid
// worktime range start time unless is the last one.
if (($i + 1) == $total) {
// If there's no more worktime ranges
// to check return the accumulated.
return $return;
}
}
if (($wt_start < $wt['date_from'])
&& ($wt_end > $wt['date_to'])
) {
// Case E: ...start...[...]...end...
$return['wt_valid'] = 1;
$return['interval'] = ($wt['date_to'] - $wt['date_from']);
$time_in_downtime = sla_downtime_worktime(
$wt['date_from'],
$wt['date_to'],
$inclusive_downtimes,
$planned_downtimes
);
if ($time_in_downtime != false) {
$return['wt_in_downtime'] = 1;
$return['downtime_interval'] = $time_in_downtime;
$return['interval'] -= $time_in_downtime;
}
if (($wt_end - $wt_start) < SECONDS_1DAY) {
// Interval is less than 1 day.
return $return;
} else {
// Interval greater than 1 day, split valid worktimes.
$start_fixed = 1;
}
}
if (($wt_start > $wt['date_to'])
&& ($wt_end > $wt['date_to'])
) {
// Case F: ...[....]..start...end...
// Invalid, check next worktime hole.
$return['wt_valid'] = 0;
// And remove current one.
$return['idx'] = ($i + 1);
}
}
}
$return['wt_valid'] = 0;
return $return;
}
/**
* Advanced SLA result with summary
*
* @param integer $id_agent_module Id_agent_module.
* @param integer $time_from Time start.
* @param integer $time_to Time end.
* @param integer $min_value Minimum value for OK status.
* @param integer $max_value Maximum value for OK status.
* @param integer $inverse_interval Inverse interval (range) for OK status.
* @param array $daysWeek Days of active work times (M-T-W-T-V-S-S).
* @param integer $timeFrom Start of work time, in each day.
* @param integer $timeTo End of work time, in each day.
* @param integer $slices Number of reports (time division).
* @param integer $inclusive_downtimes In downtime as OK (1) or ignored (0).
*
* @return array Returns a hash with the calculated data.
*/
function reporting_advanced_sla(
$id_agent_module,
$time_from=null,
$time_to=null,
$min_value=null,
$max_value=null,
$inverse_interval=0,
$daysWeek=null,
$timeFrom=null,
$timeTo=null,
$slices=1,
$inclusive_downtimes=1,
$sla_check_warning=false
) {
global $config;
// In content:
// Example: [time_from, time_to] => Worktime
// week's days => flags to manage workdays.
if (!isset($id_agent_module)) {
return false;
}
if ($slices < 1) {
$slices = 1;
}
if ((!isset($min_value)) && (!isset($max_value))) {
// Infer availability range based on the critical thresholds.
$agentmodule_info = modules_get_agentmodule($id_agent_module);
// // Check if module type is string.
$is_string_module = modules_is_string($agentmodule_info['id_agente_modulo']);
// Take in mind: the "inverse" critical threshold.
$inverse_interval = ($agentmodule_info['critical_inverse'] == 0) ? 1 : 0;
$inverse_interval_warning = (int) ($agentmodule_info['critical_warning'] ?? null);
if (!$is_string_module) {
$min_value = $agentmodule_info['min_critical'];
$max_value = $agentmodule_info['max_critical'];
$min_value_warning = $agentmodule_info['min_warning'];
$max_value_warning = $agentmodule_info['max_warning'];
} else {
$max_value = io_safe_output($agentmodule_info['str_critical']);
$max_value_warning = io_safe_output($agentmodule_info['str_warning']);
}
if (!$is_string_module) {
if (isset($min_value) === false || (int) $min_value === 0) {
$min_value = null;
}
if (isset($max_value) === false || (int) $max_value === 0) {
$max_value = null;
}
if (isset($max_value) === false && isset($min_value) === false) {
$max_value = null;
$min_value = null;
}
if (isset($min_value_warning) === false || (int) $min_value_warning === 0) {
$min_value_warning = null;
}
if (isset($max_value_warning) === false || (int) $max_value_warning === 0) {
if ((int) $max_value_warning === 0
&& $max_value_warning < $min_value_warning
&& isset($min_value) === true
&& $min_value > $max_value_warning
) {
$max_value_warning = $min_value;
} else {
$max_value_warning = null;
}
}
}
if ((!isset($min_value)) && (!isset($max_value))) {
if (($agentmodule_info['id_tipo_modulo'] == '2')
// Generic_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '6')
// Remote_icmp_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '9')
// Remote_tcp_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '18')
// Remote_snmp_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '21')
// Async_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '31')
) {
// Web_proc
// boolean values are OK if they're different from 0.
$max_value = 0;
$min_value = 0;
$inverse_interval = 1;
} else if ($agentmodule_info['id_tipo_modulo'] == '100') {
$max_value = 0.9;
$min_value = 0;
}
}
if ((!isset($min_value_warning)) && (!isset($max_value_warning))) {
if (($agentmodule_info['id_tipo_modulo'] == '2')
// Generic_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '6')
// Remote_icmp_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '9')
// Remote_tcp_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '18')
// Remote_snmp_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '21')
// Async_proc.
|| ($agentmodule_info['id_tipo_modulo'] == '31')
) {
// Web_proc
// boolean values are OK if they're different from 0.
$max_value_warning = 0;
$min_value_warning = 0;
$inverse_interval_warning = 0;
} else if ($agentmodule_info['id_tipo_modulo'] == '100') {
$max_value_warning = 0.9;
$min_value_warning = 0;
}
}
}
// By default show last day.
$datetime_to = time();
$datetime_from = ($datetime_to - SECONDS_1DAY);
// Or apply specified range.
if ((isset($time_to) && isset($time_from)) && ($time_to > $time_from)) {
$datetime_to = $time_to;
$datetime_from = $time_from;
}
if (!isset($time_to)) {
$datetime_to = $time_to;
}
if (!isset($time_from)) {
$datetime_from = $time_from;
}
$uncompressed_data = db_uncompress_module_data(
$id_agent_module,
$datetime_from,
$datetime_to
);
if (is_array($uncompressed_data)) {
$n_pools = count($uncompressed_data);
if ($n_pools == 0) {
return false;
}
}
$planned_downtimes = reporting_get_planned_downtimes_intervals(
$id_agent_module,
$datetime_from,
$datetime_to
);
if ((is_array($planned_downtimes)) && (count($planned_downtimes) > 0)) {
// Sort retrieved planned downtimes.
usort(
$planned_downtimes,
function ($a, $b) {
$a = intval($a['date_from']);
$b = intval($b['date_from']);
if ($a == $b) {
return 0;
}
return ($a < $b) ? (-1) : 1;
}
);
// Compress (overlapped) planned downtimes.
$npd = count($planned_downtimes);
for ($i = 0; $i < $npd; $i++) {
if (isset($planned_downtimes[($i + 1)])) {
if ($planned_downtimes[$i]['date_to'] >= $planned_downtimes[($i + 1)]['date_from']) {
// Merge.
$planned_downtimes[$i]['date_to'] = $planned_downtimes[($i + 1)]['date_to'];
array_splice($planned_downtimes, ($i + 1), 1);
$npd--;
}
}
}
} else {
$planned_downtimes = null;
}
// Structure retrieved: schema:
//
// uncompressed_data =>
// pool_id (int)
// utimestamp (start of current slice)
// data
// array
// utimestamp
// datos.
// Build exceptions.
$worktime = null;
if (((isset($daysWeek))
&& (isset($timeFrom))
&& (isset($timeTo)))
|| (is_array($planned_downtimes))
) {
$n = 0;
if (!isset($daysWeek)) {
// Init.
$daysWeek = [
'1' => 1,
// Sunday.
'2' => 1,
// Monday.
'3' => 1,
// Tuesday.
'4' => 1,
// Wednesday.
'5' => 1,
// Thursday.
'6' => 1,
// Friday.
'7' => 1,
// Saturday.
];
}
foreach ($daysWeek as $day) {
if ($day == 1) {
$n++;
}
}
if (($n == count($daysWeek)) && ($timeFrom == $timeTo)) {
// Ignore custom ranges.
$worktime = null;
} else {
// Get only first day.
$date_start = strtotime(date('Y/m/d', $datetime_from));
$date_end = strtotime(date('Y/m/d', $datetime_to));
$t_day = $date_start;
$i = 0;
$worktime = [];
if ($timeFrom == $timeTo) {
$timeFrom = '00:00:00';
$timeTo = '00:00:00';
}
if (!isset($timeFrom)) {
$timeFrom = '00:00:00';
}
if (!isset($timeTo)) {
$timeTo = '00:00:00';
}
// TimeFrom (seconds).
sscanf($timeFrom, '%d:%d:%d', $hours, $minutes, $seconds);
$secondsFrom = ($hours * 3600 + $minutes * 60 + $seconds);
// TimeTo (seconds).
sscanf($timeTo, '%d:%d:%d', $hours, $minutes, $seconds);
$secondsTo = ($hours * 3600 + $minutes * 60 + $seconds);
// Apply planned downtime exceptions (fix matrix).
while ($t_day <= $date_end) {
if ($daysWeek[(date('w', $t_day) + 1)] == 1) {
$wt_start = strtotime(
date('Y/m/d H:i:s', ($t_day + $secondsFrom))
);
$wt_end = strtotime(
date('Y/m/d H:i:s', ($t_day + $secondsTo))
);
if ($timeFrom == $timeTo) {
$wt_end += SECONDS_1DAY;
}
// Check if in planned downtime if exclusive downtimes.
if (($inclusive_downtimes == 0)
&& (is_array($planned_downtimes))
) {
$start_fixed = 0;
$n_planned_downtimes = count($planned_downtimes);
$i_planned_downtimes = 0;
$last_pd = end($planned_downtimes);
if ($wt_start > $last_pd['date_to']) {
// There's no more planned downtimes,
// accept remaining range.
$worktime[$i] = [];
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $wt_end;
$i++;
} else {
for ($i_planned_downtimes = 0; $i_planned_downtimes < $n_planned_downtimes; $i_planned_downtimes++) {
$pd = $planned_downtimes[$i_planned_downtimes];
if ($start_fixed == 1) {
// Interval greater than found planned downtime.
if ($wt_end < $pd['date_from']) {
$worktime[$i] = [];
// Wt_start already fixed.
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $wt_end;
$i++;
break;
}
if (( $wt_end >= $pd['date_from'] )
&& ( $wt_end <= $pd['date_to'] )
) {
$worktime[$i] = [];
// Wt_start already fixed.
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $pd['date_from'];
$i++;
break;
}
if ($wt_end > $pd['date_to']) {
$worktime[$i] = [];
// Wt_start already fixed.
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $pd['date_from'];
$i++;
$start_fixed = 0;
// Search following planned downtimes,
// we're still on work time!.
$wt_start = $pd['date_from'];
}
}
if (( $wt_start < $pd['date_from'])
&& ( $wt_end < $pd['date_from'])
) {
// Out of planned downtime: Add worktime.
$worktime[$i] = [];
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $wt_end;
$i++;
break;
}
if (( $wt_start < $pd['date_from'])
&& ( $wt_end <= $pd['date_to'])
) {
// Not all worktime in downtime.
$worktime[$i] = [];
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $pd['date_from'];
$i++;
break;
}
if (( $wt_start >= $pd['date_from'])
&& ( $wt_end <= $pd['date_to'])
) {
// All worktime in downtime, ignore.
break;
}
if (( $wt_start >= $pd['date_from'])
&& ( $wt_start <= $pd['date_to'])
&& ( $wt_end > $pd['date_to'])
) {
// Begin of the worktime in downtime, adjust.
// Search for end of worktime.
$wt_start = $pd['date_to'];
$start_fixed = 1;
}
if (( $wt_start < $pd['date_from'])
&& ( $wt_end > $pd['date_to'])
) {
// Begin of the worktime in downtime, adjust.
// Search for end of worktime.
$worktime[$i] = [];
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $pd['date_from'];
$i++;
$wt_start = $pd['date_to'];
$start_fixed = 1;
}
if (($start_fixed == 1)
&& (($i_planned_downtimes + 1) == $n_planned_downtimes)
) {
// There's no more planned downtimes,
// accept remaining range.
$worktime[$i] = [];
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $wt_end;
$i++;
break;
}
}
}
} else {
// No planned downtimes scheduled.
$worktime[$i] = [];
$worktime[$i]['date_from'] = $wt_start;
$worktime[$i]['date_to'] = $wt_end;
$i++;
}
}
$t_day = strtotime(' + 1 days', $t_day);
}
}
}
// DEBUG
// print "
Umcompressed data debug:\n"; // foreach ($uncompressed_data as $k => $caja) { // print "caja: $k\t" . $caja["utimestamp"] . "\n"; // foreach ($caja["data"] as $dato) { // print "\t" . $dato["utimestamp"] . "\t" . $dato["datos"] . "\t" . date("Y/m/d H:i:s",$dato["utimestamp"]) . "\t" . $dato["obs"] . "\n"; // } // } // print ""; // Initialization. $global_return = []; $wt_check['idx'] = 0; $last_pool_id = 0; $last_item_id = 0; // Support to slices. $global_datetime_from = $datetime_from; $global_datetime_to = $datetime_to; $range = (($datetime_to - $datetime_from) / $slices); // Analysis begins. for ($count = 0; $count < $slices; $count++) { // Use strtotime based on local timezone to avoid datetime conversions. $datetime_from = strtotime( ' + '.($count * $range).' seconds', $global_datetime_from ); $datetime_to = strtotime( ' + '.(($count + 1) * $range).' seconds', $global_datetime_from ); if ((!isset($datetime_from)) || ($datetime_from === false)) { $datetime_from = ($global_datetime_from + ($count * $range)); } if ((!isset($datetime_to)) || ($datetime_to === false)) { $datetime_to = ($global_datetime_from + (($count + 1) * $range)); } $return = []; // Timing. $time_total = 0; $time_in_ok = 0; $time_in_warning = 0; $time_in_error = 0; $time_in_unknown = 0; $time_in_not_init = 0; $time_in_down = 0; $time_out = 0; // Checks. $bad_checks = 0; $ok_checks = 0; $warning_checks = 0; $not_init_checks = 0; $unknown_checks = 0; $total_checks = 0; if (is_array($uncompressed_data)) { $n_pools = count($uncompressed_data); for ($pool_index = $last_pool_id; $pool_index < $n_pools; $pool_index++) { $pool = $uncompressed_data[$pool_index]; // Check limits. if (isset($uncompressed_data[($pool_index + 1)])) { $next_pool = $uncompressed_data[($pool_index + 1)]; } else { $next_pool = null; } if (isset($next_pool)) { $pool['next_utimestamp'] = $next_pool['utimestamp']; } else { $pool['next_utimestamp'] = $global_datetime_to; } // Update last pool checked: avoid repetition. $last_pool_id = $pool_index; if ($datetime_from > $pool['utimestamp']) { // Skip pool. continue; } // Test if need to acquire current pool. if ((($datetime_from <= $pool['utimestamp']) && ($datetime_to >= $pool['next_utimestamp'])) || ($datetime_to > $pool['utimestamp']) ) { // Acquire pool to this slice. $nitems_in_pool = count($pool['data']); for ($i = 0; $i < $nitems_in_pool; $i++) { $current_data = $pool['data'][$i]; if (($i + 1) >= $nitems_in_pool) { // If pool exceded, check next pool timestamp. $next_data = $next_pool; } else { // Pool not exceded, check next item. $next_data = $pool['data'][($i + 1)]; } if (isset($next_data['utimestamp'])) { // Check next mark time in current pool. $next_timestamp = $next_data['utimestamp']; } else { // Check last time -> datetime_to. if (!isset($next_pool)) { $next_timestamp = $global_datetime_to; } else { $next_timestamp = $datetime_to; } } // Effective time limits for current data. $wt_start = $current_data['utimestamp']; $wt_end = $next_timestamp; // Remove time spent not in planning // (and in planned downtime if needed). $wt_check = sla_fixed_worktime( $wt_start, $wt_end, $worktime, $planned_downtimes, $inclusive_downtimes, ($wt_check['idx'] ?? null) ); $time_interval = $wt_check['interval']; if (($wt_check['wt_valid'] == 1)) { $time_total += $time_interval; if ($time_interval > 0) { if (isset($current_data['type']) === false || ((int) $current_data['type'] === 0 && $i !== 0) ) { $total_checks++; } if ((isset($current_data['datos'])) && ($current_data['datos'] !== false) ) { // Check values if module is sring type. if ($is_string_module) { // OK SLA check. if (empty($max_value)) { $match = preg_match('/^'.$max_value.'$/', $current_data['datos']); } else { $match = preg_match('/'.$max_value.'/', $current_data['datos']); } // Take notice of $inverse_interval value. if ($inverse_interval === 1) { // Is not inverse. $sla_check_value = ($match === 1) ? false : true; } else { // Is inverse. $sla_check_value = ($match === 1) ? true : false; } // Warning SLA check. if (empty($max_value_warning)) { $match2 = preg_match('/^'.$max_value_warning.'$/', $current_data['datos']); } else { $match2 = preg_match('/'.$max_value_warning.'/', $current_data['datos']); } if ($inverse_interval_warning == 0) { $sla_check_value_warning = $match2; } else { $sla_check_value_warning = !$match2; } } else { // OK SLA check. $sla_check_value = sla_check_value( $current_data['datos'], $min_value, $max_value, $inverse_interval ); $sla_check_value_warning = false; if ($sla_check_value === true) { // Warning SLA check. $sla_check_value_warning = sla_check_value( $current_data['datos'], $min_value_warning, $max_value_warning, $inverse_interval_warning, 1 ); } } // Not unknown nor not init values. if ($sla_check_value_warning === true && $sla_check_warning === true) { if (isset($current_data['type']) === false || ((int) $current_data['type'] === 0 && $i !== 0) ) { $warning_checks++; } $time_in_warning += $time_interval; } else if ($sla_check_value === true) { if (isset($current_data['type']) === false || ((int) $current_data['type'] === 0 && $i !== 0) ) { $ok_checks++; } $time_in_ok += $time_interval; } else { if (isset($current_data['type']) === false || ((int) $current_data['type'] === 0 && $i !== 0) ) { $bad_checks++; } $time_in_error += $time_interval; } } else { if ($current_data['datos'] === null) { $time_in_unknown += $time_interval; if (isset($current_data['type']) === false || ((int) $current_data['type'] === 0 && $i !== 0) ) { $unknown_checks++; } } else if ($current_data['datos'] === false) { $time_in_not_init += $time_interval; if (isset($current_data['type']) === false || ((int) $current_data['type'] === 0 && $i !== 0) ) { $not_init_checks++; } } } } if ($inclusive_downtimes == 1) { if (isset($wt_check['wt_in_downtime']) === true) { // Add downtime interval as // OK in inclusion mode. $total_checks++; $ok_checks++; $time_total += $wt_check['downtime_interval']; $time_in_down += $wt_check['downtime_interval']; } } } else { $time_out += $time_interval; if ($wt_check['wt_in_downtime']) { $time_out += $wt_check['downtime_interval']; } // Ignore worktime, is in an invalid period: // scheduled downtimes in exclusion mode // not 24x7 sla's. } } } else { break; } } } else { // If monitor in not-init status => no data to show. $time_in_not_init = ($datetime_to - $datetime_from); $time_total += $time_in_not_init; $not_init_checks++; } // Timing. $return['time_total'] = $time_total; $return['time_ok'] = $time_in_ok; $return['time_warning'] = $time_in_warning; $return['time_error'] = $time_in_error; $return['time_unknown'] = $time_in_unknown; $return['time_not_init'] = $time_in_not_init; $return['time_downtime'] = $time_in_down; $return['time_out'] = $time_out; // Checks. $return['checks_total'] = $total_checks; $return['checks_ok'] = $ok_checks; $return['checks_warning'] = $warning_checks; $return['checks_error'] = $bad_checks; $return['checks_unknown'] = $unknown_checks; $return['checks_not_init'] = $not_init_checks; // SLA. $return['SLA'] = reporting_sla_get_compliance_from_array($return, $sla_check_warning); $return['sla_fixed'] = sla_truncate( $return['SLA'], $config['graph_precision'] ); // Time ranges. $return['date_from'] = $datetime_from; $return['date_to'] = $datetime_to; if ($slices > 1) { array_push($global_return, $return); } } if ($slices > 1) { return $global_return; } return $return; } /** * Report Availability. * * @param array $report Data report. * @param array $content Content for report. * @param null|string $date Date. * @param null|integer $time Time. * * @return array Return array for draw report Availability. */ function reporting_availability($report, $content, $date=false, $time=false) { global $config; $return = []; $return['type'] = 'availability'; $return['subtype'] = $content['group_by_agent']; if (empty($content['name'])) { $content['name'] = __('Availability'); } if ($date) { $datetime_to = strtotime($date.' '.$time); } $return['title'] = $content['name']; $return['landscape'] = $content['landscape']; $return['pagebreak'] = $content['pagebreak']; $return['description'] = $content['description']; $return['date'] = reporting_get_date_text( $report, $content ); $return['id_rc'] = $content['id_rc']; if ($content['show_graph']) { $return['kind_availability'] = 'address'; } else { $return['kind_availability'] = 'module'; } $avg = 0; $min = null; $min_text = ''; $max = null; $max_text = ''; $count = 0; if ($content['failover_mode']) { $availability_graph_data = reporting_availability_graph( $report, $content, false, true ); $data = $availability_graph_data['data']; foreach ($data as $key => $item_data) { $percent_ok = $item_data['sla_value']; $data[$key]['SLA'] = $percent_ok; if ($item_data['failover'] != 'result') { $data[$key]['availability_item'] = $item_data['module']; $text = $item_data['agent'].' ('.$item_data['module'].')'; $avg = ((($avg * $count) + $percent_ok) / ($count + 1)); if (isset($min) === false) { $min = $percent_ok; $min_text = $text; } else { if ($min > $percent_ok) { $min = $percent_ok; $min_text = $text; } } if (isset($max) === false) { $max = $percent_ok; $max_text = $text; } else { if ($max < $percent_ok) { $max = $percent_ok; $max_text = $text; } } $count++; } else { $data[$key]['availability_item'] = '--'; $data[$key]['agent'] = '--'; } } } else { if (empty($content['subitems'])) { if (is_metaconsole()) { metaconsole_restore_db(); } $sql = sprintf( 'SELECT id_agent_module, id_agent_module_failover, server_name, operation FROM treport_content_item WHERE id_report_content = %d', $content['id_rc'] ); $items = db_process_sql($sql); } else { $items = $content['subitems']; } $data = []; $style = io_safe_output($content['style']); if ($style['hide_notinit_agents']) { $aux_id_agents = $agents; $i = 0; foreach ($items as $item) { $utimestamp = db_get_value( 'utimestamp', 'tagente_datos', 'id_agente_modulo', $item['id_agent_module'], true ); if (($utimestamp === false) || (intval($utimestamp) > intval($datetime_to)) ) { unset($items[$i]); } $i++; } } if (empty($items) === false && (bool) $content['compare_work_time'] === true ) { $items_compare = []; foreach ($items as $item) { $item['compare'] = 1; $items_compare[] = $item; $item['compare'] = 0; $items_compare[] = $item; } $items = $items_compare; } if (empty($items) === false) { foreach ($items as $item) { // Metaconsole connection. $server_name = $item['server_name']; if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole() ) { $connection = metaconsole_get_connection($server_name); if (metaconsole_load_external_db($connection) != NOERR) { continue; } } if (modules_is_disable_agent($item['id_agent_module']) || modules_is_not_init($item['id_agent_module']) ) { // Restore dbconnection. if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole() ) { metaconsole_restore_db(); } continue; } $row = []; $text = ''; $check_sla_warning = ((int) $content['time_in_warning_status']) === 1; if (isset($item['compare']) === true && empty($item['compare']) === true ) { // Intervals is dinamic. $row['data'] = reporting_advanced_sla( $item['id_agent_module'], ($report['datetime'] - $content['period']), $report['datetime'], null, null, null, [ '1' => $content['sunday'], '2' => $content['monday'], '3' => $content['tuesday'], '4' => $content['wednesday'], '5' => $content['thursday'], '6' => $content['friday'], '7' => $content['saturday'], ], $content['time_from'], $content['time_to'], 1, 1, $check_sla_warning ); } else { // Intervals is dinamic. $row['data'] = reporting_advanced_sla( $item['id_agent_module'], ($report['datetime'] - $content['period']), $report['datetime'], null, null, 0, null, null, null, 1, 1, $check_sla_warning ); } // HACK it is saved in show_graph field. // Show interfaces instead the modules. if ($content['show_graph']) { $row['data']['availability_item'] = agents_get_address( modules_get_agentmodule_agent($item['id_agent_module']) ); if (empty($text)) { $row['data']['availability_item'] = __('No Address'); } } else { $row['data']['availability_item'] = modules_get_agentmodule_name( $item['id_agent_module'] ); } $text = $row['data']['availability_item']; $row['data']['agent'] = modules_get_agentmodule_agent_alias( $item['id_agent_module'] ); if (isset($item['compare']) === true && empty($item['compare']) === true ) { $row['data']['compare'] = false; } else { $row['data']['compare'] = true; } $text = $row['data']['agent'].' ('.$text.')'; // Restore dbconnection. if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole() ) { metaconsole_restore_db(); } // Find order. $row['data']['order'] = $row['data']['SLA']; $percent_ok = $row['data']['SLA']; $avg = ((($avg * $count) + $percent_ok) / ($count + 1)); if (isset($min) === false) { $min = $percent_ok; $min_text = $text; } else { if ($min > $percent_ok) { $min = $percent_ok; $min_text = $text; } } if (isset($max) === false) { $max = $percent_ok; $max_text = $text; } else { if ($max < $percent_ok) { $max = $percent_ok; $max_text = $text; } } $data[] = $row['data']; $count++; } switch ($content['order_uptodown']) { case REPORT_ITEM_ORDER_BY_AGENT_NAME: $temp = []; foreach ($data as $row) { $i = 0; foreach ($temp as $t_row) { if (strcmp($row['data']['agent'], $t_row['agent']) < 0) { break; } $i++; } array_splice($temp, $i, 0, [$row]); } $data = $temp; break; case REPORT_ITEM_ORDER_BY_ASCENDING: $temp = []; foreach ($data as $row) { $i = 0; foreach ($temp as $t_row) { if ($row['data']['SLA'] < $t_row['order']) { break; } $i++; } array_splice($temp, $i, 0, [$row]); } $data = $temp; break; case REPORT_ITEM_ORDER_BY_DESCENDING: $temp = []; foreach ($data as $row) { $i = 0; foreach ($temp as $t_row) { if ($row['data']['SLA'] > $t_row['order']) { break; } $i++; } array_splice($temp, $i, 0, [$row]); } $data = $temp; break; default: // Not possible. break; } } } $return['data'] = $data; $return['resume'] = []; $return['resume']['resume'] = $content['show_resume']; $return['resume']['min_text'] = $min_text; $return['resume']['min'] = $min; $return['resume']['avg'] = $avg; $return['resume']['max_text'] = $max_text; $return['resume']['max'] = $max; $return['fields'] = []; $return['fields']['total_time'] = $content['total_time']; $return['fields']['time_failed'] = $content['time_failed']; $return['fields']['time_in_ok_status'] = $content['time_in_ok_status']; $return['fields']['time_in_warning_status'] = $content['time_in_warning_status']; $return['fields']['time_in_unknown_status'] = $content['time_in_unknown_status']; $return['fields']['time_of_not_initialized_module'] = $content['time_of_not_initialized_module']; $return['fields']['time_of_downtime'] = $content['time_of_downtime']; $return['fields']['total_checks'] = $content['total_checks']; $return['fields']['checks_failed'] = $content['checks_failed']; $return['fields']['checks_in_ok_status'] = $content['checks_in_ok_status']; $return['fields']['checks_in_warning_status'] = $content['checks_in_warning_status']; $return['fields']['unknown_checks'] = $content['unknown_checks']; $return['fields']['agent_max_value'] = $content['agent_max_value']; $return['fields']['agent_min_value'] = $content['agent_min_value']; return reporting_check_structure_content($return); } /** * Reporting_availability_graph. * * @param array $report Info report. * @param array $content Content data. * @param null|boolean $pdf Output type PDF. * @param null|boolean $failover Failover mode. * * @return array Generates a structure the report. */ function reporting_availability_graph( $report, $content, $pdf=false, $failover=false ) { global $config; $return = []; $return['type'] = 'availability_graph'; $ttl = 1; if ($pdf) { $ttl = 2; } if (empty($content['name'])) { $content['name'] = __('Availability'); } $return['title'] = $content['name']; $return['landscape'] = $content['landscape']; $return['pagebreak'] = $content['pagebreak']; $return['description'] = $content['description']; $return['failover_type'] = $content['failover_type']; $return['summary'] = $content['summary']; $return['date'] = reporting_get_date_text($report, $content); // Get chart. reporting_set_conf_charts( $width, $height, $only_image, $type, $content, $ttl ); $return['id_rc'] = $content['id_rc']; $edge_interval = 10; if (empty($content['subitems'])) { if (is_metaconsole()) { metaconsole_restore_db(); } $slas = io_safe_output( db_get_all_rows_field_filter( ($failover) ? 'treport_content_item' : 'treport_content_sla_combined', 'id_report_content', $content['id_rc'] ) ); } else { $slas = $content['subitems']; } if (empty($slas)) { $return['failed'] = __('There are no SLAs defined'); } else { include_once $config['homedir'].'/include/functions_planned_downtimes.php'; $metaconsole_on = is_metaconsole(); $urlImage = ui_get_full_url(false, true, false, false); $sla_failed = false; $total_SLA = 0; $total_result_SLA = 'ok'; $sla_showed = []; $sla_showed_values = []; $priority_mode = $content['style']['priority_mode']; if (empty($slas) === false && empty($content['failover_mode']) === true && (bool) $content['compare_work_time'] === true ) { $sla_compare = []; foreach ($slas as $sla) { $sla['compare'] = 1; $sla_compare[] = $sla; $sla['compare'] = 0; $sla_compare[] = $sla; } $slas = $sla_compare; } foreach ($slas as $sla) { $server_name = $sla['server_name']; // Metaconsole connection. if ($metaconsole_on && $server_name != '') { $connection = metaconsole_get_connection($server_name); if (metaconsole_connect($connection) != NOERR) { continue; } } if ($content['failover_mode']) { $sla_failover = []; $sla_failover['primary'] = $sla; if (isset($sla['id_agent_module_failover']) === true && $sla['id_agent_module_failover'] != 0 ) { $sla_failover['failover'] = $sla; $sla_failover['failover']['id_agent_module'] = $sla['id_agent_module_failover']; } else { $sql_relations = sprintf( 'SELECT module_b FROM tmodule_relationship WHERE module_a = %d AND type = "failover"', $sla['id_agent_module'] ); $relations = db_get_all_rows_sql($sql_relations); if (isset($relations) === true && is_array($relations) === true ) { foreach ($relations as $key => $value) { $sla_failover['failover_'.$key] = $sla; $sla_failover['failover_'.$key]['id_agent_module'] = $value['module_b']; } } } // For graph slice for module-interval, if not slice=0. $module_interval = modules_get_interval($sla['id_agent_module']); $slice = ($content['period'] / $module_interval); $data_combined = []; if ((bool) $content['compare_work_time'] === true) { $sla_compare = []; foreach ($sla_failover as $k_sla => $sla) { $sla['compare'] = 1; $sla_compare[$k_sla.'_compare'] = $sla; $sla['compare'] = 0; $sla_compare[$k_sla] = $sla; } $sla_failover = $sla_compare; } foreach ($sla_failover as $k_sla => $v_sla) { $sla_array = data_compare_24x7( $v_sla, $content, $report['datetime'], $slice ); $return = prepare_data_for_paint( $v_sla, $sla_array, $content, $report['datetime'], $return, $k_sla, $pdf ); if (isset($v_sla['compare']) === true && empty($v_sla['compare']) === false ) { $data_combined_compare[] = $sla_array; } else { $data_combined[] = $sla_array; } } if (isset($data_combined_compare) === true && is_array($data_combined_compare) === true && count($data_combined_compare) > 0 ) { $return = prepare_data_for_paint( $sla, data_combined_failover($data_combined_compare), $content, $report['datetime'], $return, 'result_compare', $pdf ); } if (isset($data_combined) === true && is_array($data_combined) === true && count($data_combined) > 0 ) { $return = prepare_data_for_paint( $sla, data_combined_failover($data_combined), $content, $report['datetime'], $return, 'result', $pdf ); } } else { $sla_array = data_compare_24x7( $sla, $content, $report['datetime'], 0 ); $return = prepare_data_for_paint( $sla, $sla_array, $content, $report['datetime'], $return, '', $pdf ); } if ($metaconsole_on) { // Restore db connection. metaconsole_restore_db(); } } // SLA items sorted descending. if ($content['top_n'] == 2) { arsort($return['data']['']); } else if ($content['top_n'] == 1) { // SLA items sorted ascending. asort($sla_showed_values); } // Order data for ascending or descending. if ($content['top_n'] != 0) { switch ($content['top_n']) { case 1: // Order tables. $temp = []; foreach ($return['data'] as $row) { $i = 0; foreach ($temp as $t_row) { if ($row['sla_value'] < $t_row['order']) { break; } $i++; } array_splice($temp, $i, 0, [$row]); } $return['data'] = $temp; // Order graphs. $temp = []; foreach ($return['charts'] as $row) { $i = 0; foreach ($temp as $t_row) { if ($row['sla_value'] < $t_row['order']) { break; } $i++; } array_splice($temp, $i, 0, [$row]); } $return['charts'] = $temp; break; case 2: // Order tables. $temp = []; foreach ($return['data'] as $row) { $i = 0; foreach ($temp as $t_row) { if ($row['sla_value'] > $t_row['order']) { break; } $i++; } array_splice($temp, $i, 0, [$row]); } $return['data'] = $temp; // Order graph. $temp = []; foreach ($return['charts'] as $row) { $i = 0; foreach ($temp as $t_row) { if ($row['sla_value'] > $t_row['order']) { break; } $i++; } array_splice($temp, $i, 0, [$row]); } $return['charts'] = $temp; break; default: // If not posible. break; } } } return reporting_check_structure_content($return); } /** * Combined data modules failover. * * @param array $data Data. * * @return array */ function data_combined_failover(array $data) { $count_failover = count($data); $result = $data[0]; for ($i = 1; $count_failover > $i; $i++) { $result = array_map( function ($primary, $failover) { $return_map = []; if ($primary['date_from'] === $failover['date_from'] && $primary['date_to'] === $failover['date_to'] ) { if ($primary['time_ok'] < $failover['time_ok']) { $primary['time_total'] = $failover['time_total']; $primary['time_ok'] = $failover['time_ok']; $primary['time_error'] = $failover['time_error']; $primary['time_unknown'] = $failover['time_unknown']; $primary['time_not_init'] = $failover['time_not_init']; $primary['time_downtime'] = $failover['time_downtime']; $primary['time_out'] = $failover['time_out']; $primary['checks_total'] = $failover['checks_total']; $primary['checks_ok'] = $failover['checks_ok']; $primary['checks_error'] = $failover['checks_error']; $primary['checks_unknown'] = $failover['checks_unknown']; $primary['checks_not_init'] = $failover['checks_not_init']; $primary['SLA'] = $failover['SLA']; $primary['sla_fixed'] = $failover['sla_fixed']; } $return_map = $primary; } return $return_map; }, $result, $data[($i)] ); } return $result; } /** * Prepare data compare option. * * @param array $value Value data. * @param array $content Content report. * @param string $datetime Time. * @param integer|null $slice Number slices. * * @return array */ function data_compare_24x7( array $value, array $content, string $datetime, ?int $slice=0 ) { if (isset($value['compare']) === true && empty($value['compare']) === false ) { $content['sunday'] = 1; $content['monday'] = 1; $content['tuesday'] = 1; $content['wednesday'] = 1; $content['thursday'] = 1; $content['friday'] = 1; $content['saturday'] = 1; $content['time_from'] = '00:00'; $content['time_to'] = '23:59'; } $sla_array = data_db_uncompress_module( $value, $content, $datetime, $slice ); return $sla_array; } /** * Return data db uncompress for module. * * @param array $sla Data neccesary for db_uncompress. * @param array $content Conetent report. * @param array $datetime Date. * @param integer $slice Defined slice. * * @return array */ function data_db_uncompress_module($sla, $content, $datetime, $slice=0) { // Controller min and max == 0 then dinamic min and max critical. $dinamic_text = 0; if ($sla['sla_min'] == 0 && $sla['sla_max'] == 0) { $sla['sla_min'] = null; $sla['sla_max'] = null; $dinamic_text = __('Dynamic'); } // Controller inverse interval. $inverse_interval = 0; if ((isset($sla['sla_max'])) && (isset($sla['sla_min']))) { if ($sla['sla_max'] < $sla['sla_min']) { $content_sla_max = $sla['sla_max']; $sla['sla_max'] = $sla['sla_min']; $sla['sla_min'] = $content_sla_max; $inverse_interval = 1; $dinamic_text = __('Inverse'); } } if ($slice === 0) { // For graph slice for module-interval, if not slice=0. $module_interval = modules_get_interval($sla['id_agent_module']); $slice = ($content['period'] / $module_interval); } // Call functions sla. $sla_array = []; $sla_array = reporting_advanced_sla( $sla['id_agent_module'], ($datetime - $content['period']), $datetime, $sla['sla_min'], $sla['sla_max'], $inverse_interval, [ '1' => $content['sunday'], '2' => $content['monday'], '3' => $content['tuesday'], '4' => $content['wednesday'], '5' => $content['thursday'], '6' => $content['friday'], '7' => $content['saturday'], ], $content['time_from'], $content['time_to'], $slice ); return $sla_array; } /** * Return array planned downtimes. * * @param integer $id_agent_module Id module. * @param integer $datetime Date utimestamp. * @param integer $period Period utimestamp. * * @return array */ function reporting_get_planned_downtimes_sla($id_agent_module, $datetime, $period) { $planned_downtimes = reporting_get_planned_downtimes_intervals( $id_agent_module, ($datetime - $period), $datetime ); if ((is_array($planned_downtimes)) && (count($planned_downtimes) > 0) ) { // Sort retrieved planned downtimes. usort( $planned_downtimes, function ($a, $b) { $a = intval($a['date_from']); $b = intval($b['date_from']); if ($a == $b) { return 0; } return ($a < $b) ? (-1) : 1; } ); // Compress (overlapped) planned downtimes. $npd = count($planned_downtimes); for ($i = 0; $i < $npd; $i++) { if (isset($planned_downtimes[($i + 1)])) { if ($planned_downtimes[$i]['date_to'] >= $planned_downtimes[($i + 1)]['date_from']) { // Merge. $planned_downtimes[$i]['date_to'] = $planned_downtimes[($i + 1)]['date_to']; array_splice($planned_downtimes, ($i + 1), 1); $npd--; } } } } else { $planned_downtimes = []; } return $planned_downtimes; } /** * Prepare data for Paint in report. * * @param array $sla Data Module to sla. * @param array $sla_array Data uncompressed. * @param array $content Content report data. * @param integer $datetime Date. * @param array $return Array return. * @param string $failover Type primary, failover, Result. * @param boolean $pdf Chart pdf mode. * * @return array Return modify. */ function prepare_data_for_paint( $sla, $sla_array, $content, $datetime, $return, $failover='', $pdf=false ) { $data = []; $alias_agent = modules_get_agentmodule_agent_alias( $sla['id_agent_module'] ); if ($content['show_graph']) { $name_module = agents_get_address( modules_get_agentmodule_agent($sla['id_agent_module']) ); if (empty($name_module)) { $name_module = __('No Address'); } } else { $name_module = modules_get_agentmodule_name( $sla['id_agent_module'] ); } $data['agent'] = $alias_agent; $data['module'] = $name_module; $data['max'] = $sla['sla_max']; $data['min'] = $sla['sla_min']; $data['sla_limit'] = $sla['sla_limit']; $data['dinamic_text'] = $dinamic_text; $data['failover'] = $failover; if (isset($sla_array[0])) { $data['time_total'] = 0; $data['time_ok'] = 0; $data['time_error'] = 0; $data['time_unknown'] = 0; $data['time_not_init'] = 0; $data['time_downtime'] = 0; $data['checks_total'] = 0; $data['checks_ok'] = 0; $data['checks_error'] = 0; $data['checks_unknown'] = 0; $data['checks_not_init'] = 0; $raw_graph = []; $i = 0; foreach ($sla_array as $value_sla) { $data['time_total'] += $value_sla['time_total']; $data['time_ok'] += $value_sla['time_ok']; $data['time_error'] += $value_sla['time_error']; $data['time_unknown'] += $value_sla['time_unknown']; $data['time_downtime'] += $value_sla['time_downtime']; $data['time_not_init'] += $value_sla['time_not_init']; $data['checks_total'] += $value_sla['checks_total']; $data['checks_ok'] += $value_sla['checks_ok']; $data['checks_error'] += $value_sla['checks_error']; $data['checks_unknown'] += $value_sla['checks_unknown']; $data['checks_not_init'] += $value_sla['checks_not_init']; // Generate raw data for graph. $period = reporting_sla_get_status_period( $value_sla, $priority_mode ); $raw_graph[$i]['data'] = reporting_translate_sla_status_for_graph( $period ); $raw_graph[$i]['utimestamp'] = ($value_sla['date_to'] - $value_sla['date_from']); $i++; } $data['sla_value'] = reporting_sla_get_compliance_from_array( $data ); $data['sla_fixed'] = sla_truncate( $data['sla_value'], $config['graph_precision'] ); } else { // Show only table not divider in slice for defect slice=1. $data['time_total'] = $sla_array['time_total']; $data['time_ok'] = $sla_array['time_ok']; $data['time_error'] = $sla_array['time_error']; $data['time_unknown'] = $sla_array['time_unknown']; $data['time_downtime'] = $sla_array['time_downtime']; $data['time_not_init'] = $sla_array['time_not_init']; $data['checks_total'] = $sla_array['checks_total']; $data['checks_ok'] = $sla_array['checks_ok']; $data['checks_error'] = $sla_array['checks_error']; $data['checks_unknown'] = $sla_array['checks_unknown']; $data['checks_not_init'] = $sla_array['checks_not_init']; $data['sla_value'] = $sla_array['SLA']; } $data['compare'] = 0; if (isset($sla['compare']) === true) { if ((bool) $sla['compare'] === true) { $data['compare'] = 1; } else { $data['compare'] = 2; } } // Checks whether or not it meets the SLA. if ($data['sla_value'] >= $sla['sla_limit']) { $data['sla_status'] = 1; $sla_failed = false; } else { $sla_failed = true; $data['sla_status'] = 0; } // Do not show right modules if 'only_display_wrong' is active. if ($content['only_display_wrong'] && $sla_failed == false) { return $return; } // Find order. $data['order'] = $data['sla_value']; $return['data'][] = $data; $data_init = -1; $acum = 0; $sum = 0; $array_result = []; $i = 0; foreach ($raw_graph as $key => $value) { if ($data_init == -1) { $data_init = $value['data']; $acum = $value['utimestamp']; $sum = $value['data']; } else { if ($data_init == $value['data']) { $acum = ($acum + $value['utimestamp']); $sum = ($sum + $value['real_data']); } else { $array_result[$i]['data'] = $data_init; $array_result[$i]['utimestamp'] = $acum; $array_result[$i]['real_data'] = $sum; $i++; $data_init = $value['data']; $acum = $value['utimestamp']; $sum = $value['real_data']; } } } $array_result[$i]['data'] = $data_init; $array_result[$i]['utimestamp'] = $acum; $array_result[$i]['real_data'] = $sum; // Slice graphs calculation. $dataslice = []; $dataslice['agent'] = $alias_agent; $dataslice['module'] = $name_module; $dataslice['order'] = $data['sla_value']; $dataslice['checks_total'] = $data['checks_total']; $dataslice['checks_ok'] = $data['checks_ok']; $dataslice['time_total'] = $data['time_total']; $dataslice['time_not_init'] = $data['time_not_init']; $dataslice['sla_status'] = $data['sla_status']; $dataslice['sla_value'] = $data['sla_value']; $height = 80; if (($failover !== '' && $failover !== 'result') || (bool) $sla['compare'] === true ) { $height = 50; if ($failover !== '' && (bool) $sla['compare'] === true || $failover === 'result_compare' ) { $height = 40; } } $dataslice['chart'] = graph_sla_slicebar( $sla['id_agent_module'], $content['period'], $sla['sla_min'], $sla['sla_max'], $datetime, $content, $content['time_from'], $content['time_to'], 100, $height, $urlImage, ($pdf) ? 2 : 0, $array_result, false ); $return['charts'][] = $dataslice; return $return; } /** * reporting_increment * * Generates a structure the report. */ function reporting_increment($report, $content) { global $config; $return = []; $return['type'] = 'increment'; if (empty($content['name'])) { $content['name'] = __('Increment'); } $id_agent = $content['id_agent']; $id_agent_module = $content['id_agent_module']; $agent_description = agents_get_description($id_agent); $agent_group = agents_get_agent_group($id_agent); $agent_address = agents_get_address($id_agent); $agent_alias = agents_get_alias($id_agent); $module_name = modules_get_agentmodule_name( $id_agent_module ); $module_description = modules_get_agentmodule_descripcion( $id_agent_module ); $items_label = [ 'type' => $return['type'], 'id_agent' => $id_agent, 'id_agent_module' => $id_agent_module, 'agent_description' => $agent_description, 'agent_group' => $agent_group, 'agent_address' => $agent_address, 'agent_alias' => $agent_alias, 'module_name' => $module_name, 'module_description' => $module_description, ]; // Apply macros $title = (isset($content['name'])) ? $content['name'] : ''; if ($title != '') { $title = reporting_label_macro( $items_label, ($title ?? '') ); } $return['title'] = $title; $return['landscape'] = $content['landscape']; $return['pagebreak'] = $content['pagebreak']; $return['description'] = $content['description']; $return['id_agent_module'] = $content['id_agent_module']; $return['id_agent'] = $content['id_agent']; $id_agent_module = $content['id_agent_module']; $period = (int) $content['period']; $return['from'] = (time() - $period); $return['to'] = time(); $return['data'] = []; $search_in_history_db = db_search_in_history_db($return['from']); if (is_metaconsole()) { $sql1 = 'SELECT datos FROM tagente_datos WHERE id_agente_modulo = '.$id_agent_module.' AND utimestamp <= '.(time() - $period).' ORDER BY utimestamp DESC'; $sql2 = 'SELECT datos FROM tagente_datos WHERE id_agente_modulo = '.$id_agent_module.' ORDER BY utimestamp DESC'; metaconsole_restore_db(); $servers = db_get_all_rows_sql( 'SELECT * FROM tmetaconsole_setup WHERE disabled = 0' ); if ($servers === false) { $servers = []; } $result = []; $count_modules = 0; foreach ($servers as $server) { // If connection was good then retrieve all data server if (metaconsole_connect($server) == NOERR) { $connection = true; } else { $connection = false; } $old_data = db_get_value_sql($sql1, false, $search_in_history_db); $last_data = db_get_value_sql($sql2, false, $search_in_history_db); } } else { $old_data = db_get_value_sql( 'SELECT datos FROM tagente_datos WHERE id_agente_modulo = '.$id_agent_module.' AND utimestamp <= '.(time() - $period).' ORDER BY utimestamp DESC', false, $search_in_history_db ); $last_data = db_get_value_sql( 'SELECT datos FROM tagente_datos WHERE id_agente_modulo = '.$id_agent_module.' ORDER BY utimestamp DESC', false, $search_in_history_db ); } if (!is_metaconsole()) { } if ($old_data === false || $last_data === false) { $return['data']['message'] = __('The monitor have no data in this range of dates or monitor type is not numeric'); $return['data']['error'] = true; } else if (is_numeric($old_data) && is_numeric($last_data)) { $return['data']['old'] = round(floatval($old_data), $config['graph_precision']); $return['data']['now'] = round(floatval($last_data), $config['graph_precision']); $increment = ($old_data - $last_data); if ($increment < 0) { $return['data']['inc'] = 'positive'; $return['data']['inc_data'] = ($last_data - $old_data); } else if ($increment == 0) { $return['data']['inc'] = 'neutral'; $return['data']['inc_data'] = 0; } else { $return['data']['inc'] = 'negative'; $return['data']['inc_data'] = ($old_data - $last_data); } } else { $return['data']['message'] = __('The monitor type is not numeric'); $return['data']['error'] = true; } return reporting_check_structure_content($return); } /** * reporting_general * * Generates a structure the report. */ function reporting_general($report, $content) { global $config; $return = []; $return['type'] = 'general'; $return['subtype'] = $content['group_by_agent']; $return['resume'] = $content['show_resume']; if (empty($content['name'])) { $content['name'] = __('General'); } $return['title'] = $content['name']; $return['landscape'] = $content['landscape']; $return['pagebreak'] = $content['pagebreak']; $return['description'] = $content['description']; $return['date'] = reporting_get_date_text( $report, $content ); $return['data'] = []; $return['avg_value'] = 0; $return['min'] = []; $return['min']['value'] = null; $return['min']['formated_value'] = null; $return['min']['agent'] = null; $return['min']['module'] = null; $return['max'] = []; $return['max']['value'] = null; $return['max']['formated_value'] = null; $return['max']['agent'] = null; $return['max']['module'] = null; $return['show_in_same_row'] = $content['style']['show_in_same_row']; if (empty($content['subitems'])) { $generals = db_get_all_rows_filter( 'treport_content_item', ['id_report_content' => $content['id_rc']] ); if ($generals === false) { $generals = []; } // REGEXP. $text_agent = ''; if (isset($content['style']['text_agent']) === true && empty($content['style']['text_agent']) === false ) { $text_agent = base64_decode($content['style']['text_agent']); } $text_agent_module = ''; if (isset($content['style']['text_agent_module']) === true && empty($content['style']['text_agent_module']) === false ) { $text_agent_module = base64_decode($content['style']['text_agent_module']); } $modules_regex = []; if (empty($text_agent) === false) { if (is_metaconsole() === true) { $nodes = metaconsole_get_connections(); foreach ($nodes as $node) { try { $nd = new Node($node['id']); $nd->connect(); $modules_regex_node = modules_get_regex( $text_agent, $text_agent_module, $node['server_name'] ); } catch (\Exception $e) { $nd->disconnect(); $modules_regex_node = []; } finally { $nd->disconnect(); } $modules_regex = array_merge($modules_regex, $modules_regex_node); } } else { $modules_regex = modules_get_regex( $text_agent, $text_agent_module ); } } if (empty($modules_regex) === false) { $generals = array_merge($generals, $modules_regex); $generals = array_reduce( $generals, function ($carry, $item) { if (isset($item['operation']) === false) { $item['operation'] = 'avg'; } $carry[$item['id_agent_module'].'|'.$item['server_name']] = $item; return $carry; }, [] ); } } else { $generals = $content['subitems']; } if (empty($generals)) { $generals = []; } $i = 0; $is_string = []; foreach ($generals as $key_row => $row) { // Metaconsole connection. $server_name = $row['server_name']; if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole()) { $connection = metaconsole_get_connection($server_name); if (metaconsole_load_external_db($connection) != NOERR) { continue; } } if (modules_is_disable_agent($row['id_agent_module']) || modules_is_not_init($row['id_agent_module']) ) { if (is_metaconsole()) { // Restore db connection. metaconsole_restore_db(); } continue; } $mod_name = io_safe_output(modules_get_agentmodule_name($row['id_agent_module'])); $ag_name = modules_get_agentmodule_agent_alias($row['id_agent_module']); $name_agent = modules_get_agentmodule_agent_name($row['id_agent_module']); $type_mod = modules_get_last_value($row['id_agent_module']); $is_string[$key_row] = modules_is_string($row['id_agent_module']); $unit = db_get_value( 'unit', 'tagente_modulo', 'id_agente_modulo', $row['id_agent_module'] ); $id_module_type = db_get_value('id_tipo_modulo', 'tagente_modulo', 'nombre', $mod_name); if ($content['period'] == 0) { $data_res[$key_row] = modules_get_last_value($row['id_agent_module']); } else { $data_sum = reporting_get_agentmodule_data_sum( $row['id_agent_module'], $content['period'], $report['datetime'] ); $data_max = reporting_get_agentmodule_data_max( $row['id_agent_module'], $content['period'] ); $data_min = reporting_get_agentmodule_data_min( $row['id_agent_module'], $content['period'] ); $data_avg = reporting_get_agentmodule_data_average( $row['id_agent_module'], $content['period'] ); if ($content['style']['show_in_same_row'] && $content['group_by_agent'] == REPORT_GENERAL_NOT_GROUP_BY_AGENT) { $data_res[$key_row] = [ $data_avg, $data_max, $data_min, $data_sum, ]; } else { if (is_numeric($type_mod) && !$is_string[$key_row]) { switch ($row['operation']) { case 'sum': $data_res[$key_row] = $data_sum; break; case 'max': $data_res[$key_row] = $data_max; break; case 'min': $data_res[$key_row] = $data_min; break; case 'avg': default: $data_res[$key_row] = $data_avg; break; } } else { $data_res[$key_row] = $type_mod; } } } $divisor = get_data_multiplier($unit); switch ($content['group_by_agent']) { case REPORT_GENERAL_NOT_GROUP_BY_AGENT: $id_agent_module[$key_row] = $row['id_agent_module']; $agent_name[$key_row] = $ag_name; $module_name[$key_row] = $mod_name; $units[$key_row] = $unit; $id_module_types[$key_row] = $id_module_type; $operations[$key_row] = $row['operation']; break; case REPORT_GENERAL_GROUP_BY_AGENT: $id_module_types[$key_row] = $id_module_type; if ($id_module_types[$key_row] == 2 || $id_module_types[$key_row] == 6 || $id_module_types[$key_row] == 9 || $id_module_types[$key_row] == 18) { $data_res[$key_row] = round($data_res[$key_row], 0, PHP_ROUND_HALF_DOWN); } if ($id_module_types[$key_row] == 2 || $id_module_types[$key_row] == 6 || $id_module_types[$key_row] == 9 || $id_module_types[$key_row] == 18) { if ($data_res[$key_row] == 1) { $data_res[$key_row] = 'Up'; } else if ($data_res[$key_row] == 0) { $data_res[$key_row] = 'Down'; } } if ($data_res[$key_row] === false) { $return['data'][$name_agent][$mod_name] = null; } else { if (!is_numeric($data_res[$key_row])) { $return['data'][$name_agent][$mod_name] = $data_res[$key_row]; } else { $return['data'][$name_agent][$mod_name] = format_for_graph($data_res[$key_row], 2, '.', ',', $divisor, ' '.$unit); } } break; } if ($content['style']['show_in_same_row']) { foreach ($data_res[$key_row] as $val) { // Calculate the avg, min and max if (is_numeric($val)) { $change_min = false; if ($return['min']['value'] === null) { $change_min = true; } else { if ($return['min']['value'] > $val) { $change_min = true; } } if ($change_min) { $return['min']['value'] = $val; $return['min']['formated_value'] = format_for_graph($val, 2, '.', ',', $divisor, ' '.$unit); $return['min']['agent'] = $ag_name; $return['min']['module'] = $mod_name; } $change_max = false; if ($return['max']['value'] === null) { $change_max = true; } else { if ($return['max']['value'] < $val) { $change_max = true; } } if ($change_max) { $return['max']['value'] = $val; $return['max']['formated_value'] = format_for_graph($val, 2, '.', ',', $divisor, ' '.$unit); $return['max']['agent'] = $ag_name; $return['max']['module'] = $mod_name; } if ($i == 0) { $return['avg_value'] = $val; } else { $return['avg_value'] = ((($return['avg_value'] * $i) / ($i + 1)) + ($val / ($i + 1))); } } } } else { // Calculate the avg, min and max if (is_numeric($data_res[$key_row]) && !$is_string[$key_row]) { $change_min = false; if ($return['min']['value'] === null) { $change_min = true; } else { if ($return['min']['value'] > $data_res[$key_row]) { $change_min = true; } } if ($change_min) { $return['min']['value'] = $data_res[$key_row]; $return['min']['formated_value'] = format_for_graph($data_res[$key_row], 2, '.', ',', $divisor, ' '.$unit); $return['min']['agent'] = $ag_name; $return['min']['module'] = $mod_name; } $change_max = false; if ($return['max']['value'] === null) { $change_max = true; } else { if ($return['max']['value'] < $data_res[$key_row]) { $change_max = true; } } if ($change_max) { $return['max']['value'] = $data_res[$key_row]; $return['max']['formated_value'] = format_for_graph($data_res[$key_row], 2, '.', ',', $divisor, ' '.$unit); $return['max']['agent'] = $ag_name; $return['max']['module'] = $mod_name; } if ($i == 0) { $return['avg_value'] = $data_res[$key_row]; } else { $return['avg_value'] = ((($return['avg_value'] * $i) / ($i + 1)) + ($data_res[$key_row] / ($i + 1))); } } } $i++; // Restore dbconnection. if (($config['metaconsole'] == 1) && $server_name != '' && is_metaconsole()) { metaconsole_restore_db(); } } switch ($content['group_by_agent']) { case REPORT_GENERAL_NOT_GROUP_BY_AGENT: switch ($content['order_uptodown']) { case REPORT_ITEM_ORDER_BY_AGENT_NAME: array_multisort( $agent_name, SORT_ASC, $data_res, SORT_ASC, $module_name, SORT_ASC, $id_agent_module, SORT_ASC, $operations, SORT_ASC ); break; case REPORT_ITEM_ORDER_BY_ASCENDING: array_multisort( $data_res, SORT_ASC, $agent_name, SORT_ASC, $module_name, SORT_ASC, $id_agent_module, SORT_ASC, $operations, SORT_ASC ); break; case REPORT_ITEM_ORDER_BY_DESCENDING: array_multisort( $data_res, SORT_DESC, $agent_name, SORT_ASC, $module_name, SORT_ASC, $id_agent_module, SORT_ASC, $operations, SORT_ASC ); break; case REPORT_ITEM_ORDER_BY_UNSORT: break; } foreach ($data_res as $d_key => $d) { $data = []; $data['agent'] = $agent_name[$d_key]; $data['module'] = $module_name[$d_key]; $data['id_agent_module'] = $id_agent_module[$d_key]; $data['id_agent'] = agents_get_agent_id_by_module_id($id_agent_module[$d_key]); $data['id_module_type'] = $id_module_types[$d_key]; $data['operator'] = ''; if ($content['period'] != 0) { if ($content['style']['show_in_same_row']) { $data['operator'] = 'all'; } else { switch ($operations[$d_key]) { case 'sum': $data['operator'] = __('Summatory'); break; case 'min': $data['operator'] = __('Minimum'); break; case 'max': $data['operator'] = __('Maximum'); break; case 'avg': default: $data['operator'] = __('Rate'); break; } } } if ($content['style']['show_in_same_row']) { foreach ($d as $val) { if ($val === false) { $data['value'][] = null; } else { $divisor = get_data_multiplier($units[$d_key]); if (!is_numeric($val) || $is_string[$d_key]) { $data['value'][] = $val; // to see the chains on the table $data['formated_value'][] = $val; } else { $data['value'][] = $val; $data['formated_value'][] = format_for_graph($val, 2, '.', ',', $divisor, ' '.$units[$d_key]); } } } } else { if ($d === false) { $data['value'] = null; } else { $divisor = get_data_multiplier($units[$d_key]); if (!is_numeric($d) || $is_string[$d_key]) { $data['value'] = $d; // to see the chains on the table $data['formated_value'] = $d; } else { $data['value'] = $d; $data['formated_value'] = format_for_graph($d, 2, '.', ',', $divisor, ' '.$units[$d_key]); } } } $return['data'][] = $data; } break; } return reporting_check_structure_content($return); } function reporting_custom_graph( $report, $content, $type='dinamic', $force_width_chart=null, $force_height_chart=null, $type_report='custom_graph', $pdf=false ) { global $config; $modules = []; include_once $config['homedir'].'/include/functions_graph.php'; $return = []; $return['type'] = 'custom_graph'; if (empty($content['name'])) { if ($type_report == 'custom_graph') { $content['name'] = __('Custom graph'); } else if ($type_report == 'automatic_graph') { $content['name'] = __('Automatic combined graph'); } else { $content['name'] = ''; } } $id_meta = 0; if ($type_report == 'custom_graph') { $id_graph = $content['id_gs']; if (is_metaconsole()) { $id_meta = metaconsole_get_id_server($content['server_name']); $server = metaconsole_get_connection_by_id($id_meta); if (metaconsole_connect($server) != NOERR) { return false; } } $graphs = db_get_all_rows_field_filter( 'tgraph', 'id_graph', $content['id_gs'] ); $module_source = db_get_all_rows_sql( 'SELECT id_agent_module FROM tgraph_source WHERE id_graph = '.$content['id_gs'] ); if (isset($module_source) && is_array($module_source)) { $modules = []; foreach ($module_source as $key => $value) { $modules[$key]['module'] = $value['id_agent_module']; $modules[$key]['server'] = $id_meta; } } if (is_metaconsole()) { metaconsole_restore_db(); } } else if ($type_report == 'automatic_graph') { $graphs = db_get_all_rows_field_filter( 'tgraph', 'id_graph', $content['id_gs'] ); $graphs[0]['stacked'] = ''; $graphs[0]['summatory_series'] = ''; $graphs[0]['average_series'] = ''; $graphs[0]['modules_series'] = ''; $graphs[0]['fullscale'] = $content['style']['fullscale']; $modules = $content['id_agent_module']; if (is_metaconsole()) { $module_source = db_get_all_rows_sql( 'SELECT id_agent_module, id_server FROM tgraph_source WHERE id_graph = '.$content['id_gs'] ); if (isset($module_source) && is_array($module_source)) { $modules = []; foreach ($module_source as $key => $value) { $modules[$key]['module'] = $value['id_agent_module']; $modules[$key]['id_server'] = $value['id_server']; } } } $id_graph = 0; } else { $content['name'] = __('Simple graph'); } $id_agent = agents_get_module_id( $content['id_agent_module'] ); $agent_description = agents_get_description($id_agent); $agent_group = agents_get_agent_group($id_agent); $agent_address = agents_get_address($id_agent); $agent_alias = agents_get_alias($id_agent); $module_name = modules_get_agentmodule_name( $id_agent_module ); $module_description = modules_get_agentmodule_descripcion( $id_agent_module ); $items_label = [ 'type' => $return['type'], 'id_agent' => $id_agent, 'id_agent_module' => $id_agent_module, 'agent_description' => $agent_description, 'agent_group' => $agent_group, 'agent_address' => $agent_address, 'agent_alias' => $agent_alias, 'module_name' => $module_name, 'module_description' => $module_description, ]; // Apply macros $title = (isset($content['name'])) ? $content['name'] : ''; if ($title != '') { $title = reporting_label_macro( $items_label, ($title ?? '') ); } $description = (isset($content['description'])) ? $content['description'] : ''; if ($description != '') { $description = reporting_label_macro( $items_label, ($description ?? '') ); } $return['title'] = $title; $return['landscape'] = $content['landscape']; $return['pagebreak'] = $content['pagebreak']; $return['subtitle'] = $graphs[0]['name']; $return['agent_name'] = $agent_alias; $return['module_name'] = $module_name; $return['description'] = $description; $return['date'] = reporting_get_date_text( $report, $content ); $return['chart'] = ''; $width = null; switch ($type) { case 'dinamic': case 'static': $params = [ 'period' => $content['period'], 'width' => $width, 'date' => $report['datetime'], 'only_image' => $pdf, 'homeurl' => ui_get_full_url(false, false, false, false), 'ttl' => $ttl, 'percentil' => $graphs[0]['percentil'], 'fullscale' => $graphs[0]['fullscale'], 'server_id' => $id_meta, 'height' => $config['graph_image_height'], 'landscape' => $content['landscape'], 'return_img_base_64' => true, 'backgroundColor' => 'transparent', ]; $params_combined = [ 'stacked' => $graphs[0]['stacked'], 'summatory' => $graphs[0]['summatory_series'], 'average' => $graphs[0]['average_series'], 'modules_series' => $graphs[0]['modules_series'], 'id_graph' => $id_graph, 'type_report' => $type_report, 'labels' => $content['style']['label'], ]; $return['chart'] = ''; if ($pdf === true) { $return['chart'] .= ''; } else { if ($graphs[0]['stacked'] == CUSTOM_GRAPH_VBARS) { $return['chart'] .= '
'; $result_pdf .= $title; $result_pdf .= ' | '; $result_pdf .= ' | |
---|---|---|
'; $result_pdf .= $description; $result_pdf .= ' |
'.implode("
\n", explode("\n", $bodyEmail)).'
'; } // Extract list of emails. $destinationEmails = explode(',', io_safe_output($email)); foreach ($destinationEmails as $destination) { $destination = trim($destination); // Skip the empty 'to'. if (empty($destination) === false) { send_email_attachment($destination, $body, $subject, $attachments); } else { db_pandora_audit( AUDIT_LOG_SYSTEM, 'Cron jobs mail, empty destination email.' ); } } }