Location: PHPKode > projects > Limph > limph-1.9.7/index.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");
session_start();
favicon();

?>
<script language="JavaScript" type="text/javascript">
<!--
  function box_onload() {
    document.forms[0].elements[0].focus();
  }
// -->
</script>
<?php
echo "<body onload=\"box_onload();\">";

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

$date_a = array(date("Y"), date("m"), date("d"));
$date = implode("-", $date_a);
$time_a = array(date("H"), date("i"), date("s"));
$time = implode(":", $time_a);
$datetime = $date . " " . $time;

$logout = $_POST['logout'];
if($logout){
  unset($_SESSION['limphadmin']);
  unset($_SESSION['limphstate']);
  unset($_SESSION['limphid']);
 };

$login=$_POST['login'];
if($login=="yes"){
  $id = $_POST['id'];
  $pass = $_POST['pass'];
  $query = "SELECT pass,admin FROM users WHERE id = '$id';";
  $result = dbms_query($query);
  $line = dbms_fetch_array($result, "ASSOC");
  dbms_free_result($result);
  $nimba = $line['pass'];
  $admin_level = $line['admin'];
  $salt = substr($id, 0, 2);
  $pass = crypt($pass, $salt);
  if ($nimba == $pass){ 
    $_SESSION['limphid'] = $id;
    $_SESSION['limphstate'] = "in";
    $_SESSION['limphadmin'] = $admin_level;
  } else {
    $error = 1;
  };
 };


if((!isset($_SESSION['limphstate']))||(!isset($_SESSION['limphid']))){
  echo "<link rel=stylesheet href=style.css>";
  echo "<a href=\"http://limph.sourceforge.net\" target=\"_blank\">";
  echo "<img src=\"limph_logo_grey.png\" border=0></a><br />";
  
  echo "<table align=center border=2 cellpadding=5>";
  echo "<form action=index.php method=post>";
  echo "<td align=right>User ID:<input type=text name=id></td>";
  echo "</tr><tr>";
  echo "<td align=right>Password:<input type=password name=pass></td>";
  echo "</tr><tr>";
  echo "<td align=center><input type=hidden name=login value=yes>";
  echo "<input type=submit value=Login ></td>"; 
  echo "</form>";
  echo "</tr></table>";
  echo "<br />";
  if($error==1){echo "Invalid ID or Password.<br />";};
 } else {
  echo "<link rel=stylesheet href=css.php>";
  $user_id = $_SESSION['limphid'];
  $admin_level =$_SESSION['limphadmin'];

  echo "<meta http-equiv=refresh content=60;url=$siteaddress/index.php />";
  echo "<title>Limph: $sitetitle</title>";
  
  $deletehost=$_POST['deletehost'];
  if($deletehost=="yes"){
    $number = $_POST['number'];
    $query = "DELETE FROM hosts WHERE number = '$number';";
    $result = dbms_query($query);
  };

  $edithost=$_POST['edithost'];
  if($edithost=="yes"){
    $number = $_POST['number'];
    $name = dbms_escape_string($_POST['name']);
    $ip = dbms_escape_string($_POST['ip']);
    if(!$ip){
      $ip=dbms_escape_string(gethostbyname($name));
    };
    if($_POST['autoupdateip']=="on"){$autoupdateip="1";}else{$autoupdateip="0";};
    $prot = $_POST['prot'];
    $port = dbms_escape_string($_POST['port']);
    $group = $_POST['group'];
    $parent = $_POST['parent'];
    if($parent){$parent = parent_check($parent, $number);}else{$parent="0";};
    $enabled = $_POST['enabled'];
    if($enabled=="on"){$enabled="1";}else{$enabled="0";};
    $timeout = dbms_escape_string($_POST['timeout']);

    $query = "UPDATE hosts SET name='$name',ip='$ip',hostgroup='$group',enabled='$enabled',timeout='$timeout',type='$prot',port='$port',parent='$parent',autoupdateip='$autoupdateip' WHERE number = '$number';";
    $result = dbms_query($query);
  };

  $addhost=$_POST['addhost'];
  if($addhost=="yes"){
    $query = "SELECT number FROM hosts ORDER BY number DESC LIMIT 1;";
    $result1 = dbms_query($query);
    $line1 = dbms_fetch_array($result1, "NUM");
    $value = $line1[0];
    $value++;
    dbms_free_result($result1);

    $name = dbms_escape_string($_POST['name']);
    $ip = dbms_escape_string($_POST['ip']);
    if(!$ip){
      $ip=dbms_escape_string(gethostbyname($name));
    };
    if($_POST['autoupdateip']=="on"){$autoupdateip="1";}else{$autoupdateip="0";};
    $prot = $_POST['prot'];
    $port = dbms_escape_string($_POST['port']);
    $group = $_POST['group'];
    $parent = $_POST['parent'];
    if($parent){$parent = parent_check($parent, $value);}else{$parent="0";};

    if($group){
      $query = "INSERT INTO hosts (number,name,ip,hostgroup,status,enabled,timeout,type,port,parent,autoupdateip) VALUES ('$value','$name','$ip','$group','1','1','1','$prot','$port','$parent','$autoupdateip');";
      $result = dbms_query($query);
    };
  };

  //controls
  echo "<table align=center cellspacing=5>";
  echo "<tr>";
  echo "<td>";
  echo "<h3>Limph: $sitetitle</h3>";
  echo "</td><td>";
  echo "<form action=tree_view.php method=post>";
  echo "<input type=submit value=\"Tree View\">";
  #echo "<input type=image src=buttons.php?text=Tree+View&back=grey&length=75 alt=Tree_View>";
  echo "</form>";
  echo "</td><td>";
  echo "<form action=history.php method=post>";
  echo "<input type=submit value=\"History\">";
  echo "</form>";
  echo "</td><td>";
  echo "<form action=host_add.php method=post>";
  echo "<input type=submit value=\"Add Host\">";
  echo "</form>";
  if($admin_level>=1){
    echo "</td><td>";
    echo "<form action=groups_man.php method=post>";
    echo "<input type=submit value=\"Edit Host Groups\">";
    echo "</form>";

    echo "</td><td>";
    echo "<form action=layout_man.php method=post>";
    echo "<input type=submit value=\"Edit Group Columns\">";
    echo "</form>";

    echo "</td><td>";
    echo "<form action=agent.php method=post>";
    echo "<input type=submit value=\"Edit Users\">";
    echo "</form>";
    
    echo "</td><td>";
    echo "<form action=univ.php method=post>";
    echo "<input type=submit value=\"Admin\">";
    echo "</form>";
    
  };
  echo "</td><td>";
  echo "<form action=pass.php method=post>";
  echo "<input type=submit value=\"User Settings\">";
  echo "</form>";
  echo "</td><td>";
  echo "<form action=index.php method=post>";
  echo "<input type=hidden name=logout value=yes>";
  echo "<input type=submit value=\"Log Out\">";
  echo "</form>";
  echo "</td>";
  echo "</tr>";
  echo "</table>";
  echo "<hr>";

  //start table
  //get host stati by group and display
  $query = "SELECT COUNT(number) FROM layout;";
  $result = dbms_query($query);
  $line = dbms_fetch_array($result, "NUM");
  $columns = $line[0];
  dbms_free_result($result);

  echo "<table border=1 align=center>";
  echo "<tr><th colspan=$columns>Host Status</th></tr>";
  echo "<tr>";

  $query = "SELECT number,width FROM layout ORDER BY number ASC;";
  $result3 = dbms_query($query);
  
  while($line3 = dbms_fetch_array($result3, "NUM")){  //column loop
    $pos = $line3[0];
    $width = $line3[1];
    echo "<td align=center bgcolor=#808080 valign=top>";
    $query = "SELECT number,name,collapse FROM groups WHERE col = '$pos' ORDER BY priority ASC;";
    $result = dbms_query($query);
    //group container
    echo "<table border=0 align=center>";
    while($line = dbms_fetch_array($result, "NUM")){  //group loop
      echo "<tr><th align=center>{$line['1']}</th></tr>";
      if($line[2]=="0"){
	echo "<tr>";
	echo "<td align=center bgcolor=#808080>";
	$query2 = "SELECT name,status,enabled,number,port,visible FROM hosts WHERE hostgroup = '{$line['0']}' ORDER BY name ASC;";
	$result2 = dbms_query($query2);
	echo "<table border=0 align=center>";
	echo "<tr>"; 
	$count = 0;
	while($line2 = dbms_fetch_array($result2, "NUM")){  //host loop
	  $count++;
	  if($line2[1]=="1"){ $flag = "bgcolor=#00FF00"; }; #Up

	  $query = "SELECT DISTINCT disk FROM state WHERE hostname = '{$line2['0']}' AND type = 'diskfree';";
	  $result5 = dbms_query($query);
	  while($disks = dbms_fetch_array($result5, "ASSOC")){
	    $query6 = "SELECT data FROM state WHERE type = 'diskfree' AND hostname = '{$line2['0']}' AND disk = '{$disks['disk']}' ORDER BY datetime DESC LIMIT 1;";
	    $result6 = dbms_query($query6);
	    $current_free = dbms_fetch_array($result6, "ASSOC");
	    dbms_free_result($result6);
	    if(disk_thresh_check($line2[0], $disks['disk'], $current_free['data'])==1){ $flag = "bgcolor=#FFA500"; }; #Disk Threshold
	  };
	  dbms_free_result($result5);
	  
	  $query6 = "SELECT data FROM state WHERE type = 'sysload' AND hostname = '{$line2['0']}' ORDER BY datetime DESC LIMIT 1;";
	  $result6 = dbms_query($query6);
	  $current_load = dbms_fetch_array($result6, "ASSOC");
	  dbms_free_result($result6);
	  if(sysload_thresh_check($line2[0], $current_load['data'])==1){ $flag = "bgcolor=#FFA500"; }; #Load Threshold
	  
	  if($line2[5]=="0"){ $flag = "bgcolor=#FF00FF"; }; #Unreachable
	  if($line2[1]=="0"){ $flag = "bgcolor=#FF0000"; }; #Down
	  if($line2[2]=="0"){ $flag = "bgcolor=#808080"; }; #Disabled
	  echo "<td align=center $flag>";
	  //display host
	  if($line2[4]!="0"){
	    echo $line2[0] . " - " . $line2[4];
	  } else {
	    echo $line2[0];
	  };
	  echo "<form action=host_edit.php method=post>";
	  echo "<input type=hidden name=host value={$line2['3']}>";
	  echo "<input type=submit value=\"\">";
	  echo "</form>";
	  echo "</td>";
	  if($count>=$width){
	    echo "</tr><tr>";
	    $count = 0;
	  };
	};  //end host loop
	echo "</tr>";
	echo "</table>";
	dbms_free_result($result2);
	echo "</td>";
	echo "</tr>";
      } else {
	//group collapsed display.
	$query4 = "SELECT COUNT(number) FROM hosts WHERE hostgroup = '{$line['0']}' AND status = '0' AND enabled = '1';";
	$result4 = dbms_query($query4);
	$down_a = dbms_fetch_array($result4, "NUM");
	$down = $down_a[0];
	dbms_free_result($result4);
	
	$query4 = "SELECT COUNT(number) FROM hosts WHERE hostgroup = '{$line['0']}' AND status = '1' AND enabled = '1';";
	$result4 = dbms_query($query4);
	$up_a = dbms_fetch_array($result4, "NUM");
	$up = $up_a[0];
	dbms_free_result($result4);
	
	$total = $up + $down;
		
	//figure color
	if($total==$up){
	  $bgcolor="#00FF00";
	} else {
	  if(($up/$total)>="0.5"){
	    $bgcolor="#FFFF00";
	  } else {
	    $bgcolor="#FF0000";
	  };
	};
      
	echo "<tr>";
	echo "<td align=center bgcolor=$bgcolor>";
	echo "<h3>Up: $up Down: $down</h3>";
	echo "</td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td align=center>";
	//link to group detail popup window
	echo "<form action=groups_detail.php method=post>";
	echo "<input type=hidden name=groupname value=\"{$line['1']}\">";
	echo "<input type=hidden name=group value=\"{$line['0']}\">";
	echo "<input type=submit value=Detail>";
	echo "</form>";
	echo "</td>";
	echo "</tr>";
	
      }; //end collapse loop 
    
    };  //end group loop
    
    dbms_free_result($result);
    echo "</table>"; 
  }; //end column loop
  dbms_free_result($result3); 
  
  
  echo "</tr>";
  echo "</table>";
 
 }; //end logged in loop

echo "<hr>";

echo "<a href=$siteaddress/status.php target=\"_blank\">Status</a>";

limph_key();

$query = "SELECT version,year FROM universal;";
$result = dbms_query($query);
$line = dbms_fetch_array($result, "ASSOC");
dbms_free_result($result);
dbms_close($link);

echo "Limph version {$line['version']} Copyright {$line['year']} Jonathan Ciesla";
echo "<br />";
$time_a = array(date("H"), date("i"), date("s"));
$time = implode(":", $time_a);
$date = $date . " " . $time;
echo "$date<br />";
if($dbtype=="my"){
  echo "Running on MySQL";
  echo "<br />";
 };
if($dbtype=="pg"){
  echo "Running on PostgreSQL ";
  echo "<br />";
 };
if($dbtype=="sl"){
  echo "Running on SQLite";
  echo "<br />";
 };
if($dbtype=="slpdo"){
  echo "Running on SQLite/PDO";
  echo "<br />";
 };
echo "<a href=\"http://limph.sourceforge.net\" target=\"_blank\">Info</a>";
?>
Return current item: Limph