ajaxMethods = ['getStatusHeatMap']; ui_require_css_file('heatmap'); $this->title = __('Groups'); $this->total = $this->calculateTotalGroups(); } /** * Return the total groups. * * @return integer */ public function calculateTotalGroups():int { $total = db_get_value_sql('SELECT count(*) FROM tgrupo'); return $total; } /** * Return the status groups in heat map. * * @return string */ public function getStatusHeatMap():string { global $config; if (isset($config['id_group']) === false) { $config['id_group'] = false; } $groups = users_get_groups($config['id_group'], 'AR', false); if (is_array($groups) === true && count($groups) >= 10) { return $this->getStatusHeatMapGroup(); } $agents = agents_get_agents(); if (is_array($agents) === true && count($agents) >= 10) { $this->title = __('My monitored agents'); return $this->getStatusHeatMapAgents().''.$this->title.''; } $this->title = __('My monitored modules'); return $this->getStatusHeatMapModules().''.$this->title.''; } /** * Return the status modules in heatmap. * * @return string */ public function getStatusHeatMapModules():string { global $config; $width = (int) get_parameter('width', 350); $height = (int) get_parameter('height', 275); $id_groups = array_keys(users_get_groups($config['id_user'], 'AR', false)); if (in_array(0, $id_groups) === false) { foreach ($id_groups as $key => $id_group) { if ((bool) check_acl_restricted_all($config['id_user'], $id_group, 'AR') === false) { unset($id_groups[$key]); } } } $id_groups = implode(',', $id_groups); $modules = modules_get_modules_in_group($id_groups); $heatmap = ''; if (is_array($modules) === true) { $total_groups = count($modules); if ($total_groups === 0) { return graph_nodata_image(['width' => '400']); } // Best square. $high = (float) max($width, $height); $low = 0.0; while (abs($high - $low) > 0.000001) { $mid = (($high + $low) / 2.0); $midval = (floor($width / $mid) * floor($height / $mid)); if ($midval >= $total_groups) { $low = $mid; } else { $high = $mid; } } $square_length = min(($width / floor($width / $low)), ($height / floor($height / $low))); // Print starmap. $heatmap .= sprintf( '', $width, $height ); $heatmap .= ''; $row = 0; $column = 0; $x = 0; $y = 0; $cont = 1; foreach ($modules as $key => $value) { $module_id = $value['id_agente_modulo']; $module_status = db_get_row( 'tagente_estado', 'id_agente_modulo', $module_id, ); $module_value = modules_get_last_value($module_id); $status = ''; $title = ''; modules_get_status($module_id, $module_status['estado'], $module_value, $status, $title); switch ($status) { case STATUS_MODULE_NO_DATA: // Not init status. $status = 'notinit'; break; case STATUS_MODULE_CRITICAL: // Critical status. $status = 'critical'; break; case STATUS_MODULE_WARNING: // Warning status. $status = 'warning'; break; case STATUS_MODULE_OK: // Normal status. $status = 'normal'; break; case 3: case -1: default: // Unknown status. $status = 'unknown'; break; } $redirect = ''; if (check_acl($config['id_user'], 0, 'AW')) { $redirect = 'onclick="redirectHeatmap(\'module\', '.$module_id.', '.$value['id_agente'].')"'; } $heatmap .= sprintf( '', 'rect_'.$cont, $x, $value['nombre'], $y, $row, $column, $square_length, $square_length, $status, random_int(1, 10) ); $y += $square_length; $row++; if ((int) ($y + $square_length) > (int) $height) { $y = 0; $x += $square_length; $row = 0; $column++; } if ((int) ($x + $square_length) > (int) $width) { $x = 0; $y += $square_length; $column = 0; $row++; } $cont++; } } $heatmap .= ''; $heatmap .= ''; $heatmap .= ''; return html_print_div( [ 'content' => $heatmap, 'style' => 'margin: 0 auto; width: fit-content; min-height: 285px;', ], true ); } /** * Return the status agents in heat map. * * @return string */ public function getStatusHeatMapAgents():string { global $config; $width = get_parameter('width', 350); $height = get_parameter('height', 275); $id_groups = array_keys(users_get_groups($config['id_user'], 'AR', false)); if (in_array(0, $id_groups) === false) { foreach ($id_groups as $key => $id_group) { if ((bool) check_acl_restricted_all($config['id_user'], $id_group, 'AR') === false) { unset($id_groups[$key]); } } } $id_groups = implode(',', $id_groups); $sql = 'SELECT * FROM tagente a LEFT JOIN tagent_secondary_group g ON g.id_agent = a.id_agente WHERE (g.id_group IN ('.$id_groups.') OR a.id_grupo IN ('.$id_groups.')) AND a.disabled = 0'; $all_agents = db_get_all_rows_sql($sql); if (empty($all_agents)) { return null; } $total_agents = count($all_agents); // Best square. $high = (float) max($width, $height); $low = 0.0; while (abs($high - $low) > 0.000001) { $mid = (($high + $low) / 2.0); $midval = (floor($width / $mid) * floor($height / $mid)); if ($midval >= $total_agents) { $low = $mid; } else { $high = $mid; } } $square_length = min(($width / floor($width / $low)), ($height / floor($height / $low))); // Print starmap. $heatmap = sprintf( '', $width, $height ); $heatmap .= ''; $row = 0; $column = 0; $x = 0; $y = 0; $cont = 1; foreach ($all_agents as $key => $value) { // Colour by status. $status = agents_get_status_from_counts($value); switch ($status) { case 5: // Not init status. $status = 'notinit'; break; case 1: // Critical status. $status = 'critical'; break; case 2: // Warning status. $status = 'warning'; break; case 0: // Normal status. $status = 'normal'; break; case 3: case -1: default: // Unknown status. $status = 'unknown'; break; } $heatmap .= sprintf( '', 'rect_'.$cont, $x, $value['alias'], $y, $row, $column, $square_length, $square_length, $status, random_int(1, 10) ); $y += $square_length; $row++; if ((int) ($y + $square_length) > (int) $height) { $y = 0; $x += $square_length; $row = 0; $column++; } if ((int) ($x + $square_length) > (int) $width) { $x = 0; $y += $square_length; $column = 0; $row++; } $cont++; } $heatmap .= ''; $heatmap .= ''; $heatmap .= ''; return html_print_div( [ 'content' => $heatmap, 'style' => 'margin: 0 auto; width: fit-content; min-height: 285px;', ], true ); } /** * Return the status groups in heat map. * * @return string */ public function getStatusHeatMapGroup():string { global $config; $width = get_parameter('width', 350); $height = get_parameter('height', 275); // ACL Check. $agent_a = check_acl($config['id_user'], 0, 'AR'); $agent_w = check_acl($config['id_user'], 0, 'AW'); $groups_list = groupview_get_groups_list( $config['id_user'], ($agent_a == true) ? 'AR' : (($agent_w == true) ? 'AW' : 'AR'), true ); $total_groups = $groups_list['counter']; $groups = $groups_list['groups']; // Best square. $high = (float) max($width, $height); $low = 0.0; while (abs($high - $low) > 0.000001) { $mid = (($high + $low) / 2.0); $midval = (floor($width / $mid) * floor($height / $mid)); if ($midval >= $total_groups) { $low = $mid; } else { $high = $mid; } } $square_length = min(($width / floor($width / $low)), ($height / floor($height / $low))); // Print starmap. $heatmap = sprintf( '', $width, $height ); $heatmap .= ''; $row = 0; $column = 0; $x = 0; $y = 0; $cont = 1; foreach ($groups as $key => $value) { if ($value['_name_'] === __('All')) { continue; } if ($value['_monitors_critical_'] > 0) { $status = 'critical'; } else if ($value['_monitors_warning_'] > 0) { $status = 'warning'; } else if (($value['_monitors_unknown_'] > 0) || ($value['_agents_unknown_'] > 0)) { $status = 'unknown'; } else if ($value['_monitors_ok_'] > 0) { $status = 'normal'; } else { $status = 'unknown'; } $heatmap .= sprintf( '', 'rect_'.$cont, $x, $value['_name_'], $y, $row, $column, $square_length, $square_length, $status, random_int(1, 10) ); $y += $square_length; $row++; if ((int) ($y + $square_length) > (int) $height) { $y = 0; $x += $square_length; $row = 0; $column++; } if ((int) ($x + $square_length) > (int) $width) { $x = 0; $y += $square_length; $column = 0; $row++; } $cont++; } $heatmap .= ''; $heatmap .= ''; $heatmap .= ''; return html_print_div( [ 'content' => $heatmap, 'style' => 'margin: 0 auto; width: fit-content; min-height: 285px;', ], true ); } }