Location: PHPKode > projects > Limph > limph-1.9.7/graph.php
<?php
  /*
   Limph Is Monitoring Pingable Hosts
   Copyright (C) 2006 Jonathan Ciesla
   
   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; either version 2
   of the License, or (at your option) any later version.
   
   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
  */
require_once("config.php");
require_once("functionlib.php");
authentication(0, 0);

$link = dbms_connect($dbhost, $dbuser, $dbpass, $dbname);

//Graphviz map

//define dotfile
$fodder = "digraph tree {\n";
$fodder .= "bgcolor=grey80;\n";
$fodder .= "overlap=scale;\n";

function graph_tree_walk($name, $number){
  global $fodder;
  
  //recurse
  $query2 = "SELECT name,number,status,visible FROM hosts WHERE parent = '$number' ORDER BY name;";
  $result2 = dbms_query($query2);
  while($line2 = dbms_fetch_array($result2, "NUM")){
    if($line2[3]=="0"){
      $fodder .= "{ node [style=dotted; color=magenta;] \"$line2[0]\"; };\n";
    }else{
      if($line2[2]=="0"){
	$fodder .= "{ node [style=filled; color=red;] \"$line2[0]\"; };\n";
      }else{
	$fodder .= "{ node [style=bold; color=green;] \"$line2[0]\"; };\n";
      };
    };
    $fodder .= "\"" . $name . "\"" . " -> " . "\"" . $line2[0] . "\"" . ";\n";
    graph_tree_walk($line2[0], $line2[1]);
  };
  dbms_free_result($result2);
};

$query = "SELECT name,number FROM hosts WHERE parent = '0' ORDER BY name;";
$result = dbms_query($query);
while($line = dbms_fetch_array($result, "NUM")){
  $fodder .= "{ node [style=bold; color=green;] \"$line[0]\"; };\n";
  graph_tree_walk($line[0], $line[1]);
 };
dbms_free_result($result);

$fodder .=" }\n";

$tempnam_fodder = tempnam($_ENV['tmp'], "limph");
$tempnam_image = tempnam($_ENV['tmp'], "limph");

$tmp_image = fopen($tempnam_image, "w");

file_put_contents($tempnam_fodder, $fodder);

//run through dot
$command = $graphviz . " -Tpng " . $tempnam_fodder . " -o " . $tempnam_image;
$output = system($command);
unlink($tempnam_fodder);

$contents = file_get_contents($tempnam_image);
fclose($tmp_image);
unlink($tempnam_image);

#Inspired with permission from Nat Ciesla
$ctype = "Content-Type: image/png";
header($ctype);
echo $contents;

?>
Return current item: Limph