2008-06-17 17:47:13 +02:00
< ? PHP
// Copyright (c) 2008 Ramon Novoa, rnovoa@gmail.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// Load global vars
require ( " include/config.php " );
2008-06-30 10:11:10 +02:00
$pandora_name = 'Pandora FMS' ;
2008-06-17 17:47:13 +02:00
// Generate a dot graph definition for graphviz
2008-07-10 13:58:40 +02:00
function generate_dot ( $simple = 0 , $font_size ) {
2008-06-30 10:11:10 +02:00
global $config ;
global $pandora_name ;
2008-06-17 17:47:13 +02:00
$group_id = - 1 ;
2008-06-18 11:59:18 +02:00
$parents = array ();
$orphans = array ();
2008-06-17 17:47:13 +02:00
// Open Graph
2008-06-17 18:29:21 +02:00
$graph = open_graph ();
2008-06-17 17:47:13 +02:00
// Get agent data
2008-07-11 22:20:43 +02:00
$agents = mysql_query ( 'SELECT id_grupo, id_parent, id_agente FROM tagente WHERE disabled = 0 ORDER BY id_grupo' );
while ( $agent = mysql_fetch_assoc ( $agents )) {
2008-06-19 02:24:05 +02:00
if ( give_acl ( $config [ " id_user " ], $agent [ " id_grupo " ], " AR " ) == 0 )
continue ;
2008-06-17 18:29:21 +02:00
// Save node parent information to define edges later
2008-06-17 17:47:13 +02:00
if ( $agent [ 'id_parent' ] != " 0 " ) {
2008-06-17 18:29:21 +02:00
$parents [ $agent [ 'id_agente' ]] = $agent [ 'id_parent' ];
2008-06-17 17:47:13 +02:00
}
else {
2008-06-17 18:29:21 +02:00
$orphans [ $agent [ 'id_agente' ]] = 1 ;
2008-06-17 17:47:13 +02:00
}
2008-06-30 16:54:56 +02:00
2008-06-17 17:47:13 +02:00
// Add node
2008-07-10 13:58:40 +02:00
$graph .= create_node ( $agent , $simple , $font_size ) . " \n \t \t " ;
2008-06-17 17:47:13 +02:00
}
// Create a central node if orphan nodes exist
2008-06-30 16:54:56 +02:00
if ( count ( $orphans )) {
2008-07-10 13:58:40 +02:00
$graph .= create_pandora_node ( $pandora_name , $font_size );
2008-06-17 17:47:13 +02:00
}
// Define edges
foreach ( $parents as $node => $parent_id ) {
2008-06-30 16:54:56 +02:00
$graph .= create_edge ( $node , $parent_id );
2008-06-17 17:47:13 +02:00
}
// Define edges for orphan nodes
2008-06-30 16:54:56 +02:00
foreach ( array_keys ( $orphans ) as $node ) {
$graph .= create_edge ( '0' , $node );
2008-06-17 17:47:13 +02:00
}
// Close graph
$graph .= close_graph ();
return $graph ;
}
// Returns an edge definition
2008-06-30 10:11:10 +02:00
function create_edge ( $head , $tail ) {
2008-06-30 16:54:56 +02:00
$edge = $head . ' -- ' . $tail . '[color="#BDBDBD", headclip=false, tailclip=false];' ;
2008-06-17 17:47:13 +02:00
return $edge ;
}
// Returns a node definition
2008-07-10 13:58:40 +02:00
function create_node ( $agent , $simple = 0 , $font_size = 10 ) {
2008-07-11 22:20:43 +02:00
$sql = sprintf ( ' SELECT COUNT ( tagente_modulo . id_agente ) FROM tagente_estado ,
2008-06-30 10:11:10 +02:00
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 );
2008-06-17 17:47:13 +02:00
// Set node status
2008-06-30 10:11:10 +02:00
if ( $bad_modules ) {
2008-06-18 11:59:18 +02:00
$status_color = '#FF1D1D' ;
2008-06-30 10:11:10 +02:00
} else {
2008-06-18 11:59:18 +02:00
$status_color = '#8DFF1D' ;
2008-06-17 17:47:13 +02:00
}
2008-07-10 13:58:40 +02:00
// Check for alert
$sql = sprintf ( 'SELECT COUNT(talerta_agente_modulo.id_aam) from talerta_agente_modulo, 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 = talerta_agente_modulo.id_agente_modulo AND talerta_agente_modulo.times_fired > 0 ' , $agent [ 'id_agente' ]);
$alert_modules = get_db_sql ( $sql );
if ( $alert_modules )
$status_color = '#FFE308' ;
2008-06-30 10:11:10 +02:00
// Short name
$name = strtolower ( $agent [ " nombre " ]);
2008-06-30 16:54:56 +02:00
if ( strlen ( $name ) > 12 )
$name = substr ( $name , 0 , 12 );
2008-06-19 02:24:05 +02:00
2008-06-30 10:11:10 +02:00
if ( $simple == 0 ){
2008-06-19 02:24:05 +02:00
// Set node icon
2008-06-30 16:54:56 +02:00
if ( file_exists ( 'images/networkmap/' . $agent [ 'id_os' ] . '.png' )) {
$img_node = 'images/networkmap/' . $agent [ 'id_os' ] . '.png' ;
2008-06-30 10:11:10 +02:00
} else {
2008-06-19 02:24:05 +02:00
$img_node = 'images/networkmap/0.png' ;
}
2008-07-10 13:58:40 +02:00
$node = $agent [ 'id_agente' ] . ' [ color="' . $status_color . '", fontsize=' . $font_size . ', style="filled", fixedsize=true, width=0.40, height=0.40, label=<<TABLE CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR><TD><IMG SRC="' . $img_node . ' " /></TD></TR>
2008-06-30 16:54:56 +02:00
< TR >< TD > '.$name.' </ TD ></ TR ></ TABLE >> ,
2008-06-30 10:11:10 +02:00
shape = " ellipse " , URL = " index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='. $agent['id_agente'] .' " ,
2008-06-30 16:54:56 +02:00
tooltip = " ajax.php?page=operation/agentes/ver_agente&get_agent_status_tooltip=1&id_agent='. $agent['id_agente'] .' " ]; ' ;
2008-06-30 10:11:10 +02:00
} else {
2008-07-10 13:58:40 +02:00
$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' ] . '"];' ;
2008-06-30 10:11:10 +02:00
}
2008-06-17 17:47:13 +02:00
return $node ;
}
// Returns the definition of the central module
2008-07-10 13:58:40 +02:00
function create_pandora_node ( $name , $font_size = 10 ) {
global $simple ;
$img = '<TR><TD><IMG SRC="images/networkmap/pandora_node.png"/></TD></TR>' ;
$name = '<TR><TD BGCOLOR="#FFFFFF">' . $name . '</TD></TR>' ;
$label = '<TABLE BORDER="0">' . $img . $name . '</TABLE>' ;
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 " ]; ' ;
2008-06-17 17:47:13 +02:00
return $node ;
}
// Opens a group definition
2008-06-30 10:11:10 +02:00
function open_group ( $id ) {
2008-06-17 17:47:13 +02:00
$img = 'images/' . dame_grupo_icono ( $id ) . '.png' ;
$name = dame_nombre_grupo ( $id );
$group = 'subgraph cluster_' . $id .
2008-06-30 10:11:10 +02:00
' { style = filled ; color = darkolivegreen3 ; label =<< TABLE BORDER = " 0 " >
2008-06-30 16:54:56 +02:00
< TR >< TD >< IMG SRC = " '. $img .' " /></ TD >< TD > '.$name.' </ TD ></ TR >
</ TABLE >> ; tooltip = " '. $name .' " ;
2008-06-30 10:11:10 +02:00
URL = " index.php?sec=estado&sec2=operation/agentes/estado_agente&group_id='
. $id . '";' ;
2008-06-17 17:47:13 +02:00
return $group ;
}
// Closes a group definition
2008-06-30 10:11:10 +02:00
function close_group () {
2008-06-17 17:47:13 +02:00
return '}' ;
}
// Opens a graph definition
2008-06-30 16:54:56 +02:00
function open_graph () {
2008-07-10 13:58:40 +02:00
global $config , $layout , $nooverlap , $pure , $zoom , $ranksep , $font_size ;
2008-06-17 17:47:13 +02:00
$overlap = 'compress' ;
2008-06-19 16:34:04 +02:00
$size_x = 8 ;
$size_y = 5.4 ;
2008-06-17 17:47:13 +02:00
$size = '' ;
2008-06-30 18:38:26 +02:00
if ( $layout == 'radial' )
$overlap = 'true' ;
2008-07-10 13:58:40 +02:00
if (( $layout == 'flat' ) OR ( $layout == 'radial' ) OR ( $layout == 'spring1' ) OR ( $layout == " spring2 " ))
2008-06-30 18:38:26 +02:00
if ( $nooverlap != '' )
$overlap = 'scalexy' ;
2008-06-17 17:47:13 +02:00
2008-06-30 18:38:26 +02:00
2008-07-10 13:58:40 +02:00
if ( $pure == 1 && $zoom > 1 ) {
2008-07-02 14:30:56 +02:00
$size_x *= $zoom ;
$size_y *= $zoom ;
2008-06-17 17:47:13 +02:00
}
$size = $size_x . ',' . $size_y ;
2008-07-10 13:58:40 +02:00
//
/*
echo " SIZE $size <br> " ;
echo " NO OVERLAP $nooverlap <br> " ;
echo " LAYOUT $layout <br> " ;
echo " FONTSIZE $font_size <br> " ;
echo " RANKSEP $ranksep <br> " ;
*/
2008-06-30 10:11:10 +02:00
// BEWARE: graphwiz DONT use single ('), you need double (")
2008-06-30 18:38:26 +02:00
$head = " graph networkmap { labeljust=l; margin=0; " ;
2008-07-10 13:58:40 +02:00
if ( $nooverlap != '' ){
$head .= " overlap= \" $overlap\ " ; " ;
2008-06-30 18:38:26 +02:00
$head .= " ranksep= \" $ranksep\ " ; " ;
2008-07-10 13:58:40 +02:00
$head .= " outputorder=edgesfirst; " ;
}
$head .= " ratio=fill; " ;
$head .= " root=0; " ;
$head .= " size= \" $size\ " ; " ;
2008-06-17 17:47:13 +02:00
return $head ;
2008-06-30 10:11:10 +02:00
}
2008-06-17 17:47:13 +02:00
// Closes a graph definition
2008-06-30 16:54:56 +02:00
function close_graph () {
2008-06-17 17:47:13 +02:00
return '}' ;
}
// Returns the filter used to achieve the desired layout
2008-06-30 16:54:56 +02:00
function set_filter () {
2008-06-17 17:47:13 +02:00
global $layout ;
switch ( $layout ) {
2008-06-30 10:11:10 +02:00
case 'flat' :
return 'dot' ;
case 'radial' :
return 'twopi' ;
case 'circular' :
return 'circo' ;
case 'spring1' :
return 'neato' ;
case 'spring2' :
return 'fdp' ;
default :
return 'twopi' ;
2008-06-17 17:47:13 +02:00
}
}
2008-06-30 10:11:10 +02:00
/* Main code */
2008-06-17 17:47:13 +02:00
// Load variables
2008-07-10 13:58:40 +02:00
$layout = ( string ) get_parameter ( 'layout' , 'radial' );
$nooverlap = ( boolean ) get_parameter ( 'nooverlap' , 0 );
2008-06-17 17:47:13 +02:00
$pure = ( int ) get_parameter ( 'pure' );
$zoom = ( float ) get_parameter ( 'zoom' );
2008-06-19 02:24:05 +02:00
$ranksep = ( float ) get_parameter ( 'ranksep' , 2.5 );
2008-07-11 22:20:43 +02:00
$simple = ( boolean ) get_parameter ( 'simple' , 0 );
$regen = ( boolean ) get_parameter ( 'regen' , 0 );
2008-07-10 13:58:40 +02:00
$font_size = ( int ) get_parameter ( 'font_size' , 12 );
2008-06-17 17:47:13 +02:00
// Login check
$id_user = $_SESSION [ " id_usuario " ];
global $REMOTE_ADDR ;
if ( comprueba_login () != 0 ) {
audit_db ( $id_user , $REMOTE_ADDR , " ACL Violation " , " Trying to access node graph builder " );
include ( " general/noaccess.php " );
exit ;
}
2008-06-30 10:11:10 +02:00
if (( give_acl ( $id_user , 0 , " AR " ) != 1 ) && ( dame_admin ( $id_user ) != 1 )) {
2008-06-17 17:47:13 +02:00
audit_db ( $id_user , $REMOTE_ADDR , " ACL Violation " , " Trying to access node graph builder " );
include ( " general/noaccess.php " );
exit ;
}
2008-06-30 16:54:56 +02:00
echo '<h2>' . lang_string ( 'ag_title' ) . ' > ' . lang_string ( " Network Map " ) . ' ' ;
2008-06-17 17:47:13 +02:00
if ( $pure == 1 ) {
2008-06-30 10:11:10 +02:00
echo '<a href="index.php?sec=estado&sec2=operation/agentes/networkmap&pure=0"><img src="images/monitor.png" title="' . lang_string ( 'Normal screen' ) . '"></a>' ;
} else {
echo '<a href="index.php?sec=estado&sec2=operation/agentes/networkmap&pure=1"><img src="images/monitor.png" title="' . lang_string ( 'Full screen' ) . '"></a>' ;
2008-06-17 17:47:13 +02:00
}
echo '</h2>' ;
// Layout selection
2008-07-10 13:58:40 +02:00
$layout_array = array (
2008-06-30 10:11:10 +02:00
'circular' => 'circular' ,
2008-07-10 13:58:40 +02:00
'radial' => 'radial' ,
2008-06-30 10:11:10 +02:00
'spring1' => 'spring 1' ,
'spring2' => 'spring 2' ,
'flat' => 'flat' );
2008-06-17 17:47:13 +02:00
2008-06-18 11:59:18 +02:00
echo '<form name="input" action="index.php?sec=estado&sec2=operation/agentes/networkmap&pure=' . $pure . '" method="post">' ;
2008-06-17 17:47:13 +02:00
echo '<table cellpadding="4" cellspacing="4" class="databox">' ;
echo '<tr>' ;
2008-06-19 02:24:05 +02:00
echo '<td valign="top">' . lang_string ( 'Layout' ) . ' ' ;
2008-06-17 17:47:13 +02:00
print_select ( $layout_array , 'layout' , $layout , '' , '' , '' );
echo '</td>' ;
2008-06-19 02:24:05 +02:00
echo '<td valign="top">' . lang_string ( 'No Overlap' ) . ' ' ;
2008-07-10 13:58:40 +02:00
print_checkbox ( 'nooverlap' , '1' , $nooverlap );
2008-06-19 02:24:05 +02:00
echo '</td>' ;
echo '<td valign="top">' . lang_string ( 'Simple' ) . ' ' ;
print_checkbox ( 'simple' , '1' , $simple );
echo '</td>' ;
2008-07-11 22:20:43 +02:00
echo '<td valign="top">' . lang_string ( 'Regenerate' ) . ' ' ;
print_checkbox ( 'regen' , '1' , $regen );
echo '</td>' ;
2008-06-17 17:47:13 +02:00
if ( $pure == " 1 " ) {
// Zoom
$zoom_array = array (
'1' => 'x1' ,
2008-06-19 02:24:05 +02:00
'1.2' => 'x2' ,
'1.6' => 'x3' ,
'2' => 'x4' ,
'2.5' => 'x5' ,
2008-06-30 10:11:10 +02:00
'5' => 'x10' ,
2008-06-17 17:47:13 +02:00
);
2008-06-19 02:24:05 +02:00
echo '<td valign="top">' . lang_string ( 'Zoom' ) . ' ' ;
2008-06-17 17:47:13 +02:00
print_select ( $zoom_array , 'zoom' , $zoom , '' , '' , '' );
echo '</td>' ;
2008-06-19 02:24:05 +02:00
2008-06-17 17:47:13 +02:00
}
2008-07-10 13:58:40 +02:00
if ( $nooverlap == 1 ){
echo " <td> " ;
echo lang_string ( 'Distance between nodes' ) . ' ' ;
print_input_text ( 'ranksep' , $ranksep , $alt = 'Separation between elements in the map (in Non-overlap mode)' , 3 , 4 , 0 );
}
echo " <td> " ;
echo lang_string ( 'Font size' ) . ' ' ;
print_input_text ( 'font_size' , $font_size , $alt = 'Font size (in pt)' , 3 , 4 , 0 );
2008-06-17 17:47:13 +02:00
//echo ' Display groups <input type="checkbox" name="group" value="group" class="chk"/>';
echo '<td>' ;
2008-06-30 18:38:26 +02:00
echo '<input name="updbutton" type="submit" class="sub upd" value="' . lang_string ( " update " ) . '">' ;
2008-06-17 17:47:13 +02:00
echo '</td>' ;
echo '</table>' ;
echo '</form>' ;
// Set filter
$filter = set_filter ();
// Generate dot file
2008-07-10 13:58:40 +02:00
$graph = generate_dot ( $simple , $font_size );
2008-06-17 17:47:13 +02:00
// Generate image and map
2008-07-11 22:20:43 +02:00
// If image was generated just a few minutes ago, then don't regenerate (it takes long) unless regen checkbox is set
$filename_map = $config [ " attachment_store " ] . " /networkmap_ " . $layout ;
$filename_img = " attachment/networkmap_ " . $layout . " _ " . $font_size ;
if ( $simple ) {
$filename_map .= " _simple " ;
$filename_img .= " _simple " ;
}
if ( $nooverlap ) {
$filename_map .= " _nooverlap " ;
$filename_img .= " _nooverlap " ;
}
$filename_map .= " .map " ;
$filename_img .= " .png " ;
2008-06-30 10:11:10 +02:00
2008-07-11 22:20:43 +02:00
if ( $regen != 1 && filemtime ( $filename_img ) > time () - 300 ) {
$result = true ;
} else {
$cmd = " echo " . escapeshellarg ( $graph ) . " | $filter -Tcmapx -o " . $filename_map . " -Tpng -o " . $filename_img ;
$result = system ( $cmd );
}
2008-06-30 10:11:10 +02:00
if ( $result !== false ) {
2008-07-11 22:20:43 +02:00
if ( ! file_exists ( $filename_map )) {
2008-06-30 10:11:10 +02:00
echo '<h2 class="err">' . lang_string ( 'Map could not be generated' ) . '</h2>' ;
echo $result ;
2008-07-11 22:20:43 +02:00
echo " <br /> Apparently something went wrong reading the output.<br /> Is " . $filter . " (usually part of GraphViz) installed and able to be executed by the webserver? " ;
echo " <br /> Is " . $config [ " attachment_store " ] . " writeable by the webserver? " ;
return ;
2008-06-30 10:11:10 +02:00
}
2008-07-11 22:20:43 +02:00
echo '<img src="' . $filename_img . '" usemap="#networkmap" />' ;
include $filename_map ;
2008-06-30 16:54:56 +02:00
} else {
echo '<h2 class="err">' . lang_string ( 'Map could not be generated' ) . '</h2>' ;
echo $result ;
2008-07-11 22:20:43 +02:00
echo " <br /> Apparently something went wrong executing the command. " ;
echo " <br /> Is " . $filter . " (usually part of GraphViz) and echo installed and able to be executed by the webserver? " ;
echo " <br /> Is your webserver restricted from executing command line tools through the system() call (PHP Safe Mode or SELinux) " ;
2008-06-30 16:54:56 +02:00
return ;
2008-06-17 17:47:13 +02:00
}
?>
2008-06-30 10:11:10 +02:00
< link rel = " stylesheet " href = " include/styles/cluetip.css " type = " text/css " />
< script type = " text/javascript " src = " include/javascript/jquery.js " ></ script >
< script type = " text/javascript " src = " include/javascript/jquery.js " ></ script >
< script type = " text/javascript " src = " include/javascript/jquery.cluetip.js " ></ script >
< script language = " javascript " type = " text/javascript " >
$ ( document ) . ready ( function () {
$ ( " area[title!='<?= $pandora_name ?>'] " ) . cluetip ({
arrows : true ,
attribute : 'title' ,
cluetipClass : 'default' ,
fx : { open : 'fadeIn' , openSpeed : 'slow' },
});
});
</ script >