SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using PHP To Send Email When a Site is Down

    Does anyone have an idea for a way to use PHP to check if a sight is down (maybe by doing a ping periodically) and then send an email if it is down? I have a panicky site owner who wants something to constantly check to see if his site is down and get a notification when it is.

  2. #2
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are lots of companies whoch do this on the net - just google one and then tell your client to set up an account there, or offer to charge him 20% for you to set it up for him.

    Don't bother reinventing the wheel with no good reason.

    Douglas
    Hello World

  3. #3
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not a PHP master but I would've thought this wouldn't be too difficult to pull off with PHP. I mean there would be 2 parts to the PHP script. First, do a ping or some other check on the server. Second, if the value returned indicates a problem, send an email.

    Another reason I would like to try to do this through PHP is the owner is not too big on paying for 3rd party services.
    Last edited by frankdux; Dec 10, 2004 at 16:01.

  4. #4
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you need to run the script off the server as if the server is down, you cant run the script. You can ping or telnet or use snmp to check the server status. I would recommend telnet as you can do services that way. Unless it denies telnet access to the specific ports.

    BTW I love bloodsports. Cool movie.

    Silly

  5. #5
    SitePoint Enthusiast kaos's Avatar
    Join Date
    Jun 2004
    Location
    CANADA
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why not tell the site owner go sign up with siteuptime.com and get free email notification when his site is down. There are many paid services that check the site more frequently.

  6. #6
    SitePoint Enthusiast raist's Avatar
    Join Date
    Dec 2004
    Location
    Czech Republic
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, you need run this "check" script on other server.
    and best is, to run it in cron (sheduled executing), for this contact your webhosting admin.

    very simple script structure:
    with fsockopen , try to open connection to your "checked" server
    if result is false (check errsting) then send mail()
    advanced creativity : http://www.raist.cz

  7. #7
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Toronto
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by frankdux
    Does anyone have an idea for a way to use PHP to check if a sight is down (maybe by doing a ping periodically) and then send an email if it is down? I have a panicky site owner who wants something to constantly check to see if his site is down and get a notification when it is.

    There is definitly lots of ways to do this..

    setup a file on your main server and have a cron job call this file once a minute, if your hosting doesn't support cron use http://www.webbasedcron.com

    in the file have something like:
    PHP Code:

        $result 
    file_get_contents("http://www....");
        if ( !
    $result ) {
            
    // send email
        

    if the site is down it will produce a warning on the screen, the script will still run,

    good luck,
    George

  8. #8
    SitePoint Member
    Join Date
    Oct 2004
    Location
    Neverneverland
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey kaos, how good is "siteuptime.com"? And do you have any more others to recommend? Thanks.

  9. #9
    SitePoint Enthusiast kaos's Avatar
    Join Date
    Jun 2004
    Location
    CANADA
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by restless.linn
    Hey kaos, how good is "siteuptime.com"? And do you have any more others to recommend? Thanks.
    Their service does what it advertises. Why not sign up an account and test.

    I know a lot of hosting companies uses http://www.alertra.com/ , but they are quite expensive I believe.

  10. #10
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1 other thing, if the site is hosted on your server [if you have a reseller account] you couldn't really have the script running from yours becuase if his site goes down, then your site goes down as well - or is my thinking wrong here?

  11. #11
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    US
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    May be you can try our software - Radar Website Monitor

    http://www.apicalsoft.com/

  12. #12
    Internet Toughguy Kevin Boss's Avatar
    Join Date
    Nov 2004
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use siteuptime.com and would recommend

  13. #13
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    US
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want use php to do this, you must always refresh the php script.
    Or you can make it auto refresh by using Meta refresh.......

  14. #14
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by papayiya
    There is definitly lots of ways to do this..
    setup a file on your main server and have a cron job call this file once a minute, if your hosting doesn't support cron use http://www.webbasedcron.com

    in the file have something like:
    PHP Code:

        $result 
    file_get_contents("http://www....");
        if ( !
    $result ) {
            
    // send email
        

    if the site is down it will produce a warning on the screen, the script will still run,

    good luck,
    George
    Papayiya, thanks for your reply. Your approach sounds good, but I have a couple of questions.

    1) How do I run a cron job?

    2) Wouldn't I still run into the problem of if the server is down, then the PHP server scripts would not be able to execute to send an email notification?

  15. #15
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sites = array(); 
    $sites[] = 'cnn.com';  
    $sites[] = 'bluesnews.com'
    $sites[] = 'shacknews.com'
    $sites[] = 'hardocp.com'

    foreach(
    $sites as $site) {
        
    $sock fsockopen"$site"80$errno$errstr);  

        if (!
    $sock) {
            
    $to"you@domain.com";
            
    $email "$site is down";
            
    $subject "Server Down";

            
    mail($to$subject$email);
        }

    John Saunders

  16. #16
    SitePoint Member
    Join Date
    Dec 2004
    Location
    UK
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using siteuptime, so far so good
    eisolution.biz - This Site is for Sale! More than 700 members!

  17. #17
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I made a cron file and I came up with the PHP script but for some reason I can't get the PHP to execute. Here is what I have (on a different server than the site I am checking):

    servercheck.php
    PHP Code:
    #!/usr/local/bin/php -q
    <?php 
    $fp
    =fsockopen("www.example.com",80,$errno,$errstr); 
        if(!
    $fp)
         { 
            echo 
    "Couldn't create a socket connection.<br>"
            
    mail ("user@example.com""testing notification""Here is your email, the
     site is not running"
    "From: user@example.com");
            exit; 
        }
        else 
        {
            echo 
    "Site is up and running and the mail has been sent!";
            
    mail ("user@example.com""testing notification""Here is your email, the
     site is running"
    "From: user@example.com"); 
        }
    cron.txt
    PHP Code:
    2,4,20,22,30,32,36,38,40,42,50,52 * * * *  /home/example/example.com/temp/servercheck.php 
    Anyone have any ideas what might be going wrong? I have tested the servercheck.php by loading it up in the browser and it does execute through that method. Also my PHP is installed as a CGI, thus the "#!/usr/local/bin/php -q".

  18. #18
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The cron line:

    /home/example/example.com/temp/servercheck.php

    Change to:

    /home/example/example.com/temp/./servercheck.php

    Or if you want, take out the first CLI line in your php script and change cron line to:

    php /home/example/example.com/temp/servercheck.php

    If neither work, then it could be permission issue

    Silly

  19. #19
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sillysoft
    If neither work, then it could be permission issue
    Silly
    Sillysoft, good troubleshooting ideas, but no luck here. I tried the first 2 approaches and it still didn't work. Then I changed the permissions by doing chmod 777 on checkserver.php and still nothing. Any other ideas?

  20. #20
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sheesh what kind of miracle does it take to get cron and php to work together? I had been trying it on Linux where PHP was run as a CGI and then I tried it on BSD where PHP is an Apache module and still no execution of the PHP script! I am running out of ideas. Any PHP or Unix gurus out there have any suggestions?

  21. #21
    SitePoint Member zdislaw's Avatar
    Join Date
    Mar 2004
    Location
    D.C.
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It might be worth replacing "/home/example/example.com/temp/servercheck.php" in your cron with "http://example.com/servercheck.php." I've had luck running scripts on remote servers like that, but not sure about on the same box.

    Or maybe something like:
    Code:
    2,4,20,22,30,32,36,38,40,42,50,52 * * * /usr/bin/php -f /home/example/example.com/temp/servercheck.php
    Dunno really. Just thoughts. I'm not even going to pretend to know for sure.
    Tim
    "We are confronted with insurmountable opportunities."
    - Pogo

  22. #22
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After A LOT of tinkering and trying things, I FINALLY got my PHP script to execute. I found the solution in a Sitepoint forum discussion from a few years ago called CRON Tab / Unix / PHP -- Help?.

    Basically I changed my Cron command file (cron.txt) from

    0,10,20,30,40,50 * * * * /home/example/example.com/temp/servercheck.php

    to

    0,10,20,30,40,50 * * * * /usr/local/bin/php -q /home/example/example.com/temp/servercheck.php >> /dev/null

  23. #23
    SitePoint Addict frankdux's Avatar
    Join Date
    Apr 2004
    Location
    Ashland, North Carolina
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm glad that I got it to work, but now I am trying to understand why it did. Does anyone know what the "-q" and ">> /dev/null" parts do?

    ( 0,10,20,30,40,50 * * * * /usr/local/bin/php -q /home/example/example.com/temp/servercheck.php >> /dev/null )

  24. #24
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I forgot what the -q is, but >> /dev/null means your telling the job to report all output/errors to /dev/null. The script will run just fine but if you have any output from the script tt will not show it as it throws it to /dev/null. The >> /dev/null problem did not resolve your issue, the -q resolved your issue.

    Silly

  25. #25
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    -q Quiet mode i.e don't output any HTML headers
    >>dev/null throwaway any output


Bookmarks

Posting Permissions

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