...

View Full Version : Question about my uptime check method



Krentenbol
12-01-2011, 04:38 PM
I am creating a toplist, I am busy with making a system to calculate the uptime. I've found some script and changed it a bit and now I got this:


<?php
include('config.php');
$result = mysql_query("SELECT * FROM status",$db);
while ($myrow = mysql_fetch_array($result))
{
if (! $sock = @fsockopen($row['ip'], $row['port'], $num, $error, 5)) {
mysql_query("UPDATE status SET offline='0' WHERE id=".$row['id']."");
mysql_query("UPDATE checks SET checks = checks +1 WHERE id=".$row['id']."");
mysql_query("UPDATE status SET down = down +1 WHERE id=".$row['id']."");
}else{
mysql_query("UPDATE status SET online='1' WHERE id=".$row['id']."");
mysql_query("UPDATE status SET checks = checks +1 WHERE id=".$row['id']."");
fclose($sock);
}
}
?>

Would this method be wise to use to check the status? And how can I make it so this script would start each 30 minutes. Since I couldn't figure that out.

Old Pedant
12-01-2011, 08:01 PM
There's no reason for separate UPDATE calls. And you have your socket close in the wrong place.



while ($myrow = mysql_fetch_array($result))
{
if (! $sock = @fsockopen($row['ip'], $row['port'], $num, $error, 5))
{
mysql_query("UPDATE status SET offline=0,checks=checks+1,down=down+1"
. " WHERE id=" . $row['id'] );
}else{
mysql_query("UPDATE status SET online=1,checks=checks+1 WHERE id=" . $row['id'] );
}
fclose($sock);
}

I don't use PHP, but I know that PHP *can* be used from a command line, so you would just need to create a scheduled task (on Windows) or a CRON job (on Linux) to do this.

Krentenbol
12-01-2011, 08:37 PM
Thank you, my code is now:


<?php
include('inc/open.php');
$result = mysql_query("SELECT * FROM status",$db) or die (mysql_error());
while ($row = mysql_fetch_array($result))
$lastcheck = $row['lastchecktime'];
$time = time();
if ($time - $lastcheck > 5*60) {
while ($myrow = mysql_fetch_array($result))
{
if (! $sock = @fsockopen($row['ip'], $row['port'], $num, $error, 5))
{
mysql_query("UPDATE status SET offline=0,checks=checks+1,down=down+1"
. " WHERE id=" . $row['id'] );
mysql_query("UPDATE lastchecktime SET ". $time .""
. " WHERE id=" . $row['id'] );
}else{
mysql_query("UPDATE status SET online=1,checks=checks+1 WHERE id=" . $row['id'] );
mysql_query("UPDATE lastchecktime SET ". $time .""
. " WHERE id=" . $row['id'] );
}
fclose($sock);
}
}
?>

When loading the page my check should increase, but it doesn't at all. I don't see why, no errors or something.

Old Pedant
12-01-2011, 09:03 PM
DEBUG DEBUG DEBUG.



<?php
include('inc/open.php');
$result = mysql_query("SELECT * FROM status",$db) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{ // you should always use braces after a while/if/etc.
$lastcheck = $row['lastchecktime'];
}
echo "<hr/>DEBUG lastcheck is " . $lastcheck . "<hr/>\n";

$time = time();
if ($time - $lastcheck > 5*60)
{
echo "DEBUG: going to do check<hr/>\n";
while ($myrow = mysql_fetch_array($result))
{
echo "DEBUG ip is " . $row['ip'] . " and port is " . $row['port'] . "<hr/>\n";
if (! $sock = @fsockopen($row['ip'], $row['port'], $num, $error, 5))
{
$sql = "UPDATE status SET offline=0,checks=checks+1,down=down+1"
. " WHERE id=" . $row['id'];
echo "DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die(mysql_error());

$sql = "UPDATE lastchecktime SET ". $time .""
. " WHERE id=" . $row['id'];
echo "DEBUG SQL: " . $sql . "<hr/>\n";
mysql_query( $sql ) or die(mysql_error());
}else{

... and so on ...

Krentenbol
12-02-2011, 02:11 PM
This problem is fixed, thank you for the help, you've helped me a lot.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum