1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Port monitor script

Discussion in 'PHP' started by Demonio, Sep 23, 2009.

  1. #1
    Hi,
    I have found the following Port monitor script: http://www.techdose.com/tutorials/Basic-Port-Uptime-Script/124/page1.html,

    Someone can help me to modify it to get more than 1 host?

    Thank you
     
    Demonio, Sep 23, 2009 IP
  2. phprightnow

    phprightnow Peon

    Messages:
    296
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Assuming the script on that page actually works, this is how you would do it (all I did was create a foreach statement around it with an array of all the hosts).

    // Hosts to check
    $hosts = array("www.yourdomain.com","www.site2.com","www.site3.com","etc.com");
    
    foreach($hosts as $host) {
    // Define the ports we'll be checking
    $ports = array(21=>"FTP",25=>"Sendmail",80=>"Apache Web Server",3306=>"MySQL Server");
    
    // Initially assume there isn't a problem with the services
    $problem = 0;
    
    // Check to see if a socket can be opened to each of the ports in $ports
    foreach($ports as $port => $service){
        $fp = fsockopen($host,$port,$errno,$errstr,10); 
        if(!$fp) 
        { 
            $portmsg.="Port ".$port." - ".$service."\n";
            if($problem!=1){
                $problem=1;
            }
        }else{
            fclose($fp);
        }
        flush();
    }
    
    // Notify the intended recipients if there is a problem
    if($problem == 1){
        // send full email notifications of service outage
        $recipients = "youremail@domain.com";  // *** CHANGE THIS TO YOUR EMAIL ***
        $msg = date("M d, Y h:i:s",time())."\n\n";
        $msg.= "The following service(s) were unreachable and may require immediate attention:\n\n";
        $msg.= $portmsg; 
        $subject = 'Service Unreachable!';
        $headers .= "From: Server Status <root@localhost>\r\n";
        $headers .= "X-Sender: <root@localhost>\r\n";
        $headers .= "Content-Type: text; charset=iso-8859-1\r\n";
        mail($recipients, $subject, $msg, $headers) or die("Problem sending mail.");
    }
    }
    ?>
    PHP:
     
    phprightnow, Sep 23, 2009 IP
  3. Demonio

    Demonio Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi phprightnow,

    Thank you for your answer. Now it check more than 1 host. But when i visit it, i receive the following errors:

    Warning: fsockopen() [function.fsockopen]: unable to connect to www.validdomain.org:21 (Connection refused) in /home/user/public_html/admin/global-monitor.php on line 14
    
    Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/user/public_html/admin/global-monitor.php on line 14
    
    Warning: fsockopen() [function.fsockopen]: unable to connect to asdasdasdasdasdasdasd.com:21 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/user/public_html/admin/global-monitor.php on line 14
    
    Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/user/public_html/admin/global-monitor.php on line 14
    
    Warning: fsockopen() [function.fsockopen]: unable to connect to asdasdasdasdasdasdasd.com:25 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/user/public_html/admin/global-monitor.php on line 14
    
    Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/user/public_html/admin/global-monitor.php on line 14
    
    Warning: fsockopen() [function.fsockopen]: unable to connect to asdasdasdasdasdasdasd.com:80 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/user/public_html/admin/global-monitor.php on line 14
    
    Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/user/public_html/admin/global-monitor.php on line 14
    
    Warning: fsockopen() [function.fsockopen]: unable to connect to asdasdasdasdasdasdasd.com:3306 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/user/public_html/admin/global-monitor.php on line 14
    Code (markup):
    The first error is with a valid domain, and i don't know why give me this error. But it's not important.

    The second error i think is because the domain asdasdasdasdasdasdasd.com is not registered.

    Once its visited, it send me an e-mail with services down. But without the domain who has the services down:

    Is there some method to add the domain to the e-mail?

    Thank you
     
    Demonio, Sep 23, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You probably need to remove the "www." from the hostnames.. Ofcourse, some services like mysql may not be accessible worldwide (only locally).

    Also, to add the host, just do: $subject = 'Service Unreachable! ' . $host;

    This is all pretty basic and you should probably invest in learning PHP, please buy a book.
     
    premiumscripts, Sep 23, 2009 IP
  5. Demonio

    Demonio Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi premiumscripts,

    Thank you for the tip. I just run a hosting business and my designer is out for 4 weeks.

    I have added the host to the Subject. And now i have removed the www from hosts and the error is the same.

    Also, i don't know why fail the FTP service in the valid domain, i have accessed the server and all is running correctly.
     
    Demonio, Sep 23, 2009 IP
  6. phprightnow

    phprightnow Peon

    Messages:
    296
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why not use SiteUptime.com if you're just using this for personal reasons?
     
    phprightnow, Sep 23, 2009 IP
  7. Demonio

    Demonio Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Hi,
    Because i want to monitor an entire list of servers, and one of my purposes is (after this) create a visual monitor with the same original code.

    Thank you
     
    Demonio, Sep 24, 2009 IP
  8. phprightnow

    phprightnow Peon

    Messages:
    296
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Note, I would personally also change

    $fp = fsockopen($host,$port,$errno,$errstr,10);

    to

    $fp = @fsockopen($host,$port,$errno,$errstr,10);

    This will hide that error output on a failed server.
     
    phprightnow, Sep 24, 2009 IP
  9. Demonio

    Demonio Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Changed! Thank you phprightnow, a bit of karma for all your help :)
     
    Demonio, Sep 24, 2009 IP
  10. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #10
    A huge fail. NEVER suppress the error. Create a default exception handler and also a custom error handler. Basically, implement the custom error handler to then throw a RuntimeException which will be caught by the default exception handler.

    Remember, there is a reason why errors and exceptions are reported. They are for program logic flow and graceful recovery.

    If you would have simply looked at the error message that he posted any competent software engineer could tell immediately what the problem is:
    
    Warning: fsockopen() [function.fsockopen]: unable to connect to asdasdasdasdasdasdasd.com:25
    
    Code (markup):
    hhhmmm...I wonder why the script couldn't connect to asdasdasdasdasdasdasd.com on port 25?

    My recommendation is use real domains for testing and not fictitional ones and also not to suppress the error.
     
    Chemo, Sep 24, 2009 IP