Detect if a server is up or down; Web Server, Game Server, any port

From TomSchaefer.org Wiki

Jump to: navigation, search

Monitoring Services is requires constant attention, so sys admins try to make the job easy by using a monitoring service. What if you wanted to monitor a service via a website. Well thanks to the best code language, php, there is a simple way to do this.


    <?
    $ip = "youriphere";
    $port = "yourporthere";
    if (! $sock = @fsockopen($ip, $port, $num, $error, 5))
    echo ‘<B><FONT COLOR=red>Offline</b></FONT>’;
    else{
    echo ‘<B><FONT COLOR=lime>Online</b></FONT>’;
    fclose($sock);
    }
    ?>

The above code will watch a service on a specific IP and on a specific port. Depending on the system ping the php code will echo “offline” or “online”. Simple.

What if you wanted to use images like a red button…

    <?php
    // Set the content-type
    header("Content-type: image/gif");
    $fontsize = 18;
    if(@$_GET['fontsize']) {
    $fontsize = $_GET['fontsize'];
    }
    $font = ‘yourcustomfont.ttf’;
    $text = @$_GET['text'];
    // Create the image
    $size = imagettfbbox($fontsize, 0, $font, $text);
    $width = $size[2] + $size[0] + 8;
    $height = abs($size[1]) + abs($size[7]);
    $im = imagecreate($width, $height);
    $colourBlack = imagecolorallocate($im, 255, 255, 255);
    imagecolortransparent($im, $colourBlack);
    // Create some colors
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0, 0, 0);
    // Add the text
    imagefttext($im, $fontsize, 0, 0, abs($size[5]), $black, $font, $text);
    // Using imagepng() results in clearer text compared with
    imagegif($im);
    imagedestroy($im);
    ?>

Or you can even use the first code example to echo html code for an image.

Personal tools