Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question about my uptime check method

    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:

    Code:
    <?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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    There's no reason for separate UPDATE calls. And you have your socket close in the wrong place.

    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you, my code is now:

    Code:
    <?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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    DEBUG DEBUG DEBUG.

    Code:
    <?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 ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This problem is fixed, thank you for the help, you've helped me a lot.
    Last edited by Krentenbol; 12-02-2011 at 03:23 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •