PDA

View Full Version : A script to ping servers from a DB


-fedexer-
03-31-2008, 09:01 PM
Ok well i've been working on a script which will become a cron job , which will select all the completed servers from a database, and then ping each of those, and if online change the online column to 1 and if offline change it to 0.
Also i am trying to insert the time and date into a datetime field in the database but i think i am calling it wrongly or something as it never changes.
I thought i had the script completed, but it seems there are a few flaws, such as the script does not seem to check the servers at all....
Also im not sure it ever reaches the end as it does seem to take a very.. very.. very long time to do anything.

Any help would be appreciated , even if it is modding a previously created script of which you can point me in the direction of.

My script http://forums.x10hosting.com/images/smilies/new/happysad.gif:
<?php
require_once("../connection/connect.php");
header("Expires: Thu, 17 May 2001 10:17:17 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache");
$query = "SELECT * FROM serverlist WHERE serverCompleted = '1'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$query = "SELECT * FROM serverlistglobal WHERE ID = '1'";
$result = mysql_query($query);
$globalVars = mysql_fetch_array($result);
$lastGlobCheck = $globalVars['serverChecks'];
$newGlobCheck = $lastGlobCheck + 1;
$query = "UPDATE serverlistglobal SET serverChecks = '$newGlobCheck' WHERE ID = '1'";
$result = mysql_query($query);

do{
//check for socket open
$checkSock = @fsockopen($row['serverIP'], $row['serverPort'], $errno, $errstr, 30);
if(!$checkSock){
$uptime = ($row['serverChecks'] / $globalVars['serverChecks']);
$query = "UPDATE serverlist SET serverOnline = '0', serverUptime = '$uptime' WHERE serverID = '".$row['serverID']."'";
$result = mysql_query($query);
}else{
$lastChecks = $row['serverChecks'];
$newChecks = $lastChecks + 1;
$query = "UPDATE serverlist SET serverOnline = '1', serverChecks = '$newChecks' WHERE serverID = '".$row['serverID']."'";
$result = mysql_query($query);
$uptime = ($row['serverChecks'] / $globalVars['serverChecks']);
$query = "UPDATE serverlist SET serverOnline = '1', serverChecks = '$newChecks' WHERE serverID = '".$row['serverID']."'";
$result = mysql_query($query);
}
@fclose($checkSock);
}while($row = @mysql_fetch_array($result));
$query = "SELECT * FROM serverlistglobal WHERE ID = '1'";
$result = mysql_query($query);
$globalVars = mysql_fetch_array($result);
$time = date("Y-m-d H:i:s");
$query = "UPDATE serverlistglobal SET lastCheckTime = '$time' WHERE ID = '1'";
$result = mysql_query($query);

exit;
?>

Thanks
-fedexer-

student101
04-11-2008, 03:25 PM
Forgive me for asking but this is just my :-? curiosity;

How do ping a database from a database?

Also you keep stating:
$query = "UPDATE serverlistglobal SET lastCheckTime = '$time' WHERE ID = '1'";
What if the ID isn't "1" ?

-fedexer-
04-25-2008, 06:15 PM
Well i was never pinging a database 0.o, anyway i fixed this a while ago so the thread can be closed or whatever 8-)