From c4f8c10dc9cec7256498430e5bfa378dea1d80bd Mon Sep 17 00:00:00 2001 From: ramonn Date: Thu, 16 Jul 2009 16:15:10 +0000 Subject: [PATCH] 2009-07-16 Ramon Novoa * include/functions_networkmap.php: Added to repository. Functions used to draw network maps. * operation/agentes/networkmap.php: Moved network map generation functions to functions_networkmap.php. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1807 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 8 + .../include/functions_networkmap.php | 280 +++++++++++++++++ .../operation/agentes/networkmap.php | 284 +----------------- 3 files changed, 295 insertions(+), 277 deletions(-) create mode 100644 pandora_console/include/functions_networkmap.php diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index f2a0b72994..11586961f3 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,11 @@ +2009-07-16 Ramon Novoa + + * include/functions_networkmap.php: Added to repository. Functions used + to draw network maps. + + * operation/agentes/networkmap.php: Moved network map generation + functions to functions_networkmap.php. + 2009-07-16 Pablo de la ConcepcipĆ³n * include/functions_agents.php: fixed repeated empty test and safe_int call diff --git a/pandora_console/include/functions_networkmap.php b/pandora_console/include/functions_networkmap.php new file mode 100644 index 0000000000..d9c440e3f8 --- /dev/null +++ b/pandora_console/include/functions_networkmap.php @@ -0,0 +1,280 @@ += 1) + $filter['id_grupo'] = $group; + + // Get agent data + $agents = get_agents ($filter, + array ('id_grupo, nombre, id_os, id_parent, id_agente')); + if ($agents === false) + return false; + + // Open Graph + $graph = open_graph ($layout, $nooverlap, $pure, $zoom, $ranksep, $font_size); + + // Parse agents + $nodes = array (); + foreach ($agents as $agent) { + // Save node parent information to define edges later + if ($agent['id_parent'] != "0") { + $parents[$agent['id_agente']] = $agent['id_parent']; + } else { + $orphans[$agent['id_agente']] = 1; + } + + // Add node + $nodes[$agent['id_agente']] = $agent; + } + + if (empty ($nodes)) { + return false; + } + // Create nodes + foreach ($nodes as $node_id => $node) { + if ($center > 0 && ! is_descendant ($node_id, $center, $parents)) { + unset ($parents[$node_id]); + unset ($orphans[$node_id]); + unset ($nodes[$node_id]); + continue; + } + + $graph .= create_node ($node , $simple, $font_size)."\n\t\t"; + } + + // Define edges + foreach ($parents as $node => $parent_id) { + // Verify that the parent is in the graph + if (isset ($nodes[$parent_id])) { + $graph .= create_edge ($node, $parent_id, $layout, $nooverlap, $pure, $zoom, $ranksep, $simple, $regen, $font_size, $group); + } else { + $orphans[$node] = 1; + } + } + + // Create a central node if orphan nodes exist + if (count ($orphans)) { + $graph .= create_pandora_node ($pandora_name, $font_size, $simple); + } + + // Define edges for orphan nodes + foreach (array_keys($orphans) as $node) { + $graph .= create_edge ('0', $node, $layout, $nooverlap, $pure, $zoom, $ranksep, $simple, $regen, $font_size, $group); + } + + // Close graph + $graph .= close_graph (); + + return $graph; +} + +// Returns an edge definition +function create_edge ($head, $tail, $layout, $nooverlap, $pure, $zoom, $ranksep, $simple, $regen, $font_size, $group) { + + // edgeURL allows node navigation + $edge = $head.' -- '.$tail.'[color="#BDBDBD", headclip=false, tailclip=false, + edgeURL="index.php?sec=estado&sec2=operation/agentes/networkmap¢er='.$head. + '&layout='.$layout.'&nooverlap=' .$nooverlap.'&pure='.$pure. + '&zoom='.$zoom.'&ranksep='.$ranksep.'&simple='.$simple.'®en=1'. + '&font_size='.$font_size.'&group='.$group.'"];'; + + return $edge; +} + +// Returns a node definition +function create_node ($agent, $simple = 0, $font_size = 10) { + $sql = sprintf ('SELECT COUNT(tagente_modulo.id_agente) + FROM tagente_estado, tagente_modulo + WHERE tagente_modulo.id_agente = %d + AND tagente_modulo.id_tipo_modulo in (2, 6, 9, 18, 21, 100) + AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo + AND tagente_modulo.disabled = 0 + AND tagente_estado.estado = 1', + $agent['id_agente']); + $bad_modules = get_db_sql ($sql); + + // Set node status + if ($bad_modules) { + $status_color = '#FF1D1D'; + } else { + $status_color = '#8DFF1D'; + } + + // Check for alert + $sql = sprintf ('SELECT COUNT(talert_template_modules.id) + FROM talert_template_modules, tagente_modulo, tagente + WHERE tagente.id_agente = %d + AND tagente.disabled = 0 + AND tagente.id_agente = tagente_modulo.id_agente + AND tagente_modulo.disabled = 0 + AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module + AND talert_template_modules.times_fired > 0 ', + $agent['id_agente']); + $alert_modules = get_db_sql ($sql); + if ($alert_modules) + $status_color = '#FFE308'; + + // Short name + $name = strtolower ($agent["nombre"]); + if (strlen ($name) > 16) + $name = substr ($name, 0, 16); + + if ($simple == 0){ + // Set node icon + if (file_exists ('images/networkmap/'.$agent['id_os'].'.png')) { + $img_node = 'images/networkmap/'.$agent['id_os'].'.png'; + } else { + $img_node = 'images/networkmap/0.png'; + } + + $node = $agent['id_agente'].' [ color="'.$status_color.'", fontsize='.$font_size.', style="filled", fixedsize=true, width=0.40, height=0.40, label=< +
'.$name.'
>, + shape="ellipse", URL="index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].'", + tooltip="ajax.php?page=operation/agentes/ver_agente&get_agent_status_tooltip=1&id_agent='.$agent['id_agente'].'"];'; + } else { + $node = $agent['id_agente'] . ' [ color="' . $status_color . '", fontsize='.$font_size.', style="filled", fixedsize=true, width=0.20, height=0.20, label="", tooltip="ajax.php?page=operation/agentes/ver_agente&get_agent_status_tooltip=1&id_agent='.$agent['id_agente'].'"];'; + } + return $node; +} + +// Returns the definition of the central module +function create_pandora_node ($name, $font_size = 10, $simple = 0) { + $img = ''; + $name = ''.$name.''; + $label = ''.$img.$name.'
'; + if ($simple == 1){ + $label = ''; + } + + $node = '0 [ color="#364D1F", fontsize='.$font_size.', style="filled", fixedsize=true, width=0.8, height=0.6, label=<'.$label.'>, + shape="ellipse", URL="index.php?sec=estado&sec2=operation/agentes/estado_grupo" ];'; + + return $node; +} + +// Opens a group definition +function open_group ($id) { + $img = 'images/'.get_group_icon ($id).'.png'; + $name = get_group_name ($id); + + $group = 'subgraph cluster_' . $id . + ' { style=filled; color=darkolivegreen3; label=< + +
'.$name.'
>; tooltip="'.$name.'"; + URL="index.php?sec=estado&sec2=operation/agentes/estado_agente&group_id=' + . $id . '";'; + + return $group; +} + +// Closes a group definition +function close_group () { + return '}'; +} + +// Opens a graph definition +function open_graph ($layout, $nooverlap, $pure, $zoom, $ranksep, $font_size) { + $overlap = 'compress'; + $size_x = 8; + $size_y = 5.4; + $size = ''; + + if ($layout == 'radial') + $overlap = 'true'; + + if ($layout == 'flat' || $layout == 'radial' || $layout == 'spring1' || $layout == "spring2") + if ($nooverlap != '') + $overlap = 'scalexy'; + + if ($zoom > 0) { + $size_x *= $zoom; + $size_y *= $zoom; + } + $size = $size_x . ',' . $size_y; + + // BEWARE: graphwiz DONT use single ('), you need double (") + $head = "graph networkmap { labeljust=l; margin=0; "; + if ($nooverlap != '') { + $head .= "overlap=\"$overlap\";"; + $head .= "ranksep=\"$ranksep\";"; + $head .= "outputorder=edgesfirst;"; + } + $head .= "ratio=fill;"; + $head .= "root=0;"; + $head .= "size=\"$size\";"; + + return $head; +} + +// Closes a graph definition +function close_graph () { + return '}'; +} + +// Returns the filter used to achieve the desired layout +function get_filter ($layout) { + switch($layout) { + case 'flat': + return 'dot'; + case 'radial': + return 'twopi'; + case 'circular': + return 'circo'; + case 'spring1': + return 'neato'; + case 'spring2': + return 'fdp'; + default: + return 'twopi'; + } +} + +?> + diff --git a/pandora_console/operation/agentes/networkmap.php b/pandora_console/operation/agentes/networkmap.php index b3453ab209..1be76f7253 100644 --- a/pandora_console/operation/agentes/networkmap.php +++ b/pandora_console/operation/agentes/networkmap.php @@ -26,278 +26,20 @@ if (! give_acl ($config['id_user'], 0, "AR")) { exit; } -require_once ('include/functions_agents.php'); - -$pandora_name = 'Pandora FMS'; +require_once ('include/functions_networkmap.php'); // Load variables $layout = (string) get_parameter ('layout', 'radial'); -$nooverlap = (boolean) get_parameter ('nooverlap', 0); +$nooverlap = (int) get_parameter ('nooverlap', 0); $pure = (int) get_parameter ('pure'); $zoom = (float) get_parameter ('zoom'); $ranksep = (float) get_parameter ('ranksep', 2.5); -$simple = (boolean) get_parameter ('simple', 0); -$regen = (boolean) get_parameter ('regen',1); // Always regen by default +$simple = (int) get_parameter ('simple', 0); +$regen = (int) get_parameter ('regen',1); // Always regen by default $font_size = (int) get_parameter ('font_size', 12); $group = (int) get_parameter ('group', 0); $center = (int) get_parameter ('center', 0); -// Check if a node descends from a given node -function is_descendant ($node, $ascendant, $parents) { - if (! isset ($parents[$node])) { - return false; - } - - if ($node == $ascendant) { - return true; - } - - return is_descendant ($parents[$node], $ascendant, $parents); -} - -// Generate a dot graph definition for graphviz -function generate_dot ($group, $simple, $font_size) { - global $config, $pandora_name, $center; - - $parents = array(); - $orphans = array(); - - $filter = array (); - $filter['disabled'] = 0; - if ($group >= 1) - $filter['id_grupo'] = $group; - - // Get agent data - $agents = get_agents ($filter, - array ('id_grupo, nombre, id_os, id_parent, id_agente')); - if ($agents === false) - return false; - - // Open Graph - $graph = open_graph (); - - // Parse agents - $nodes = array (); - foreach ($agents as $agent) { - // Save node parent information to define edges later - if ($agent['id_parent'] != "0") { - $parents[$agent['id_agente']] = $agent['id_parent']; - } else { - $orphans[$agent['id_agente']] = 1; - } - - // Add node - $nodes[$agent['id_agente']] = $agent; - } - - if (empty ($nodes)) { - return false; - } - // Create nodes - foreach ($nodes as $node_id => $node) { - if ($center > 0 && ! is_descendant ($node_id, $center, $parents)) { - unset ($parents[$node_id]); - unset ($orphans[$node_id]); - unset ($nodes[$node_id]); - continue; - } - - $graph .= create_node ($node , $simple, $font_size)."\n\t\t"; - } - - // Define edges - foreach ($parents as $node => $parent_id) { - // Verify that the parent is in the graph - if (isset ($nodes[$parent_id])) { - $graph .= create_edge ($node, $parent_id); - } else { - $orphans[$node] = 1; - } - } - - // Create a central node if orphan nodes exist - if (count ($orphans)) { - $graph .= create_pandora_node ($pandora_name, $font_size); - } - - // Define edges for orphan nodes - foreach (array_keys($orphans) as $node) { - $graph .= create_edge ('0', $node); - } - - // Close graph - $graph .= close_graph (); - - return $graph; -} - -// Returns an edge definition -function create_edge ($head, $tail) { - global $layout, $nooverlap, $pure, $zoom, - $ranksep, $simple, $regen, $font_size, $group; - - // edgeURL allows node navigation - $edge = $head.' -- '.$tail.'[color="#BDBDBD", headclip=false, tailclip=false, - edgeURL="index.php?sec=estado&sec2=operation/agentes/networkmap¢er='.$head. - '&layout='.$layout.'&nooverlap=' .$nooverlap.'&pure='.$pure. - '&zoom='.$zoom.'&ranksep='.$ranksep.'&simple='.$simple.'®en=1'. - '&font_size='.$font_size.'&group='.$group.'"];'; - - return $edge; -} - -// Returns a node definition -function create_node ($agent, $simple = 0, $font_size = 10) { - $sql = sprintf ('SELECT COUNT(tagente_modulo.id_agente) - FROM tagente_estado, tagente_modulo - WHERE tagente_modulo.id_agente = %d - AND tagente_modulo.id_tipo_modulo in (2, 6, 9, 18, 21, 100) - AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.disabled = 0 - AND tagente_estado.estado = 1', - $agent['id_agente']); - $bad_modules = get_db_sql ($sql); - - // Set node status - if ($bad_modules) { - $status_color = '#FF1D1D'; - } else { - $status_color = '#8DFF1D'; - } - - // Check for alert - $sql = sprintf ('SELECT COUNT(talert_template_modules.id) - FROM talert_template_modules, tagente_modulo, tagente - WHERE tagente.id_agente = %d - AND tagente.disabled = 0 - AND tagente.id_agente = tagente_modulo.id_agente - AND tagente_modulo.disabled = 0 - AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module - AND talert_template_modules.times_fired > 0 ', - $agent['id_agente']); - $alert_modules = get_db_sql ($sql); - if ($alert_modules) - $status_color = '#FFE308'; - - // Short name - $name = strtolower ($agent["nombre"]); - if (strlen ($name) > 16) - $name = substr ($name, 0, 16); - - if ($simple == 0){ - // Set node icon - if (file_exists ('images/networkmap/'.$agent['id_os'].'.png')) { - $img_node = 'images/networkmap/'.$agent['id_os'].'.png'; - } else { - $img_node = 'images/networkmap/0.png'; - } - - $node = $agent['id_agente'].' [ color="'.$status_color.'", fontsize='.$font_size.', style="filled", fixedsize=true, width=0.40, height=0.40, label=< -
'.$name.'
>, - shape="ellipse", URL="index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].'", - tooltip="ajax.php?page=operation/agentes/ver_agente&get_agent_status_tooltip=1&id_agent='.$agent['id_agente'].'"];'; - } else { - $node = $agent['id_agente'] . ' [ color="' . $status_color . '", fontsize='.$font_size.', style="filled", fixedsize=true, width=0.20, height=0.20, label="", tooltip="ajax.php?page=operation/agentes/ver_agente&get_agent_status_tooltip=1&id_agent='.$agent['id_agente'].'"];'; - } - return $node; -} - -// Returns the definition of the central module -function create_pandora_node ($name, $font_size = 10) { - global $simple; - $img = ''; - $name = ''.$name.''; - $label = ''.$img.$name.'
'; - if ($simple == 1){ - $label = ""; - } - - $node = '0 [ color="#364D1F", fontsize='.$font_size.', style="filled", fixedsize=true, width=0.8, height=0.6, label=<'.$label.'>, - shape="ellipse", URL="index.php?sec=estado&sec2=operation/agentes/estado_grupo" ];'; - - return $node; -} - -// Opens a group definition -function open_group ($id) { - $img = 'images/'.get_group_icon ($id).'.png'; - $name = get_group_name ($id); - - $group = 'subgraph cluster_' . $id . - ' { style=filled; color=darkolivegreen3; label=< - -
'.$name.'
>; tooltip="'.$name.'"; - URL="index.php?sec=estado&sec2=operation/agentes/estado_agente&group_id=' - . $id . '";'; - - return $group; -} - -// Closes a group definition -function close_group () { - return '}'; -} - -// Opens a graph definition -function open_graph () { - global $config, $layout, $nooverlap, $pure, $zoom, $ranksep, $font_size; - $overlap = 'compress'; - $size_x = 8; - $size_y = 5.4; - $size = ''; - - if ($layout == 'radial') - $overlap = 'true'; - - if ($layout == 'flat' || $layout == 'radial' || $layout == 'spring1' || $layout == "spring2") - if ($nooverlap != '') - $overlap = 'scalexy'; - - if ($pure == 1 && $zoom > 1 ) { - $size_x *= $zoom; - $size_y *= $zoom; - } - $size = $size_x . ',' . $size_y; - - // BEWARE: graphwiz DONT use single ('), you need double (") - $head = "graph networkmap { labeljust=l; margin=0; "; - if ($nooverlap != '') { - $head .= "overlap=\"$overlap\";"; - $head .= "ranksep=\"$ranksep\";"; - $head .= "outputorder=edgesfirst;"; - } - $head .= "ratio=fill;"; - $head .= "root=0;"; - $head .= "size=\"$size\";"; - - return $head; -} - -// Closes a graph definition -function close_graph () { - return '}'; -} - -// Returns the filter used to achieve the desired layout -function set_filter () { - global $layout; - - switch($layout) { - case 'flat': - return 'dot'; - case 'radial': - return 'twopi'; - case 'circular': - return 'circo'; - case 'spring1': - return 'neato'; - case 'spring2': - return 'fdp'; - default: - return 'twopi'; - } -} - /* Main code */ echo '

'.__('Pandora agents').' » '.__('Network map').' '; @@ -378,10 +120,11 @@ echo ''; echo ''; // Set filter -$filter = set_filter (); +$filter = get_filter ($layout); // Generate dot file -$graph = generate_dot ($group, $simple, $font_size); +$graph = generate_dot (__('Pandora FMS'), $group, $simple, $font_size, $layout, $nooverlap, $zoom, $ranksep, $center, $regen, $pure); + if ($graph === false) { print_error_message (__('Map could not be generated')); echo __('No agents found'); @@ -442,17 +185,4 @@ if ($result !== false) { return; } -require_css_file ('cluetip'); -require_jquery_file ('cluetip'); ?> -