The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

a way to auto email users with errors?

Discussion in 'E-mail Discussions' started by rastoma, Jun 13, 2004.

  1. rastoma

    rastoma Well-Known Member

    Joined:
    Aug 27, 2001
    Messages:
    158
    Likes Received:
    0
    Trophy Points:
    16
    Is is possible to have a script scan through the error_log periodically for users that have missing files and contact that user or better yet to copy a jpg or txt file in the place of the missing file saying that a file is missing?
     
  2. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    If you mean a way for 404 errors to be known by the website owner, my 404 pages automatically email me when they are called. I just call a cgi script as an image, and the script emails me and tells me what the referring page (url that was called) is.

    Just add this to your 404 page:
    <img src="http://www.domain.com/cgi-bin/error.cgi" height="1" width="1" border="0">

    Here's the error.cgi perl script:
    Code:
    #!/usr/bin/perl
    
    $mailprog = '/usr/sbin/sendmail';
    
    open(MAIL,"|$mailprog -t");
    
    print MAIL "To: error\@PremierWebsiteSolutions.com\n";
    print MAIL "From: Mike <webmaster\@PremierWebsiteSolutions.com>\n";
    print MAIL "Subject: 404 Error Occured\n\n";
    print MAIL "The page that was attempted to be called is $ENV{HTTP_REFERER}\n\n";
    
    close (MAIL);
    
    exit;
    
    Change the path in the image tag and the email addresses in the script and you will be emailed any time a 404 occurs.


    I fixed the code. I edited my version a bit to make it simpler, but made a small error in the changes.
     
    #2 PWSowner, Jun 13, 2004
    Last edited: Jun 13, 2004
  3. sawbuck

    sawbuck Well-Known Member

    Joined:
    Jan 18, 2004
    Messages:
    1,367
    Likes Received:
    5
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    Thanks Mike!
     
  4. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    No problem. I think everyone's 404 page should use it. ;)

    Now if every website in the world used it, and each user sent me a penny, I'd be happily retired. :D ;)
     
  5. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
  6. sawbuck

    sawbuck Well-Known Member

    Joined:
    Jan 18, 2004
    Messages:
    1,367
    Likes Received:
    5
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    You're probably too young to retire.:)
     
    #6 sawbuck, Jun 13, 2004
    Last edited: Jun 13, 2004
  7. Sheldon

    Sheldon Well-Known Member

    Joined:
    Jun 7, 2004
    Messages:
    378
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Canada
    hmm I see this as being abused!

    although nice job man! ... any way of it only sending the error once a day or something?
     
  8. Website Rob

    Website Rob Well-Known Member

    Joined:
    Mar 23, 2002
    Messages:
    1,506
    Likes Received:
    0
    Trophy Points:
    36
    Location:
    Alberta, Canada
    cPanel Access Level:
    Root Administrator
    Nice touch, Mike, and thanks for sharing. :D
     
  9. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    My code had a minor error which has been repaired.
     
  10. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    The error.cgi script shown above is actually a cut down version of what I use which is a counter script that I just modified to use on my 404 pages to email me when errors occur. The cut down version does work but generates a 500 error in the error logs, probably because it's being called to create an image but doesn't. The full version that I actually use generates a counter image which isn't seen because the image tag gives height and width of 1. Whenever it is called, it also sends the email notice.

    If you don't want error logs, you will need to use this full version. You still just put the image tag in your 404 page, but use this script instead og the one above.

    Code:
    #!/usr/bin/perl
    
    # This is a graphical counter that is called from the <img> tag.
    # Syntax is:
    # <img src="path/to/script/error.cgi?404.txt">
    # So, for example, if your CounterDataFile was count.txt in the /file dir :
    # <img src="cgi-bin/error.cgi?404.txt">
    #
    # The script will create a 404.txt file which will keep a count of the number of times it was called.
    
    $mailprog = '/usr/sbin/sendmail';
    
       $minLen = 4;           # minimum number of digits in bigmap
       $isHigh = 1;           # if 1, digits are 16 pixels high, to
                              # allow room for border
       $isInverse = 1;        # 1 = digits are white on black
                              # 0 = black on white
       $lockWait = 5;         # number of seconds to wait for lock
       $counterFile = $ENV{QUERY_STRING};   # where the logging file is.
                                            # This allows many counters
                                            # to run off of this script.
    
       &initialize;
    
       &incrementCount;
    
       &generateBitmap;
       &writeBitmap;
       &email;
    
       exit(0);
    
    
    sub writeBitmap {
       print ("Content-type: image/x-xbitmap\n\n");
       if ($isHigh) {
          printf ("#define count_width %d\n#define count_height 16\n", 
                  $len*8);
       }
       else {
          printf ("#define count_width %d\n#define count_height 10\n", 
                  $len*8);
       }
       printf STDOUT "static char count_bits[] = {\n";
       for($i = 0; $i < ($#bytes + 1); $i++) {
          print("0x$bytes[$i]");
          if ($i != $#bytes) {
             print(",");
             if (($i+1) % 7 == 0) {
                print("\n");
             }
          }
       }
       print("};\n");
    }
    
    # generateBitmap() - $count contains number to display
    #                    $minLen contains minimum number of digits to display
    #                    $isHigh is one for 16 bit high numbers (else 10)
    #                    $isInverse is one for reverse (white on black);
    sub generateBitmap {
       $count = $totalReads;
       @bytes = ();
       $len = length($count) > $minLen ? length($count) : $minLen;
       $formattedCount = sprintf("%0${len}d",$count);
       if ($isHigh) {
          for ($i = 0; $i < $len*3; $i++ ) {
             if ($isInverse) {
                push(@bytes,"ff");       # add three blank rows to each digit
             }
             else {
                push(@bytes,"00");
             }
          }
       }
       for ($y=0; $y < 10; $y++) {
           for ($x=0; $x < $len; $x++) {
               $digit = substr($formattedCount,$x,1);
               if ($isInverse) {             # $inv = 1 for inverted text
                   $byte = substr(@invdigits[$digit],$y*3,2);
               } 
               else {
                   $byte = substr(@digits[$digit],$y*3,2);
               }
               push(@bytes,$byte);
           }
       }
       if ($isHigh) {
          for ($i = 0; $i < $len*3; $i++ ) {
             if ($isInverse) {
                push(@bytes,"ff");       # add three blank rows to each digit
             }
             else {
                push(@bytes,"00");
             }
          }
       }
    }
    
    sub initialize {
      # $hostname = $ENV{REMOTE_HOST};
      # $host     = $ENV{REMOTE_ADDR};
      # $port     = $ENV{SERVER_PORT};
       # bitmap for each digit
       #  Each digit is 8 pixels wide, 10 high
       #  @invdigits are white on black, @digits black on white
       @invdigits = ("c3 99 99 99 99 99 99 99 99 c3",  # 0
                     "cf c7 cf cf cf cf cf cf cf c7",  # 1
                     "c3 99 9f 9f cf e7 f3 f9 f9 81",  # 2
                     "c3 99 9f 9f c7 9f 9f 9f 99 c3",  # 3
                     "cf cf c7 c7 cb cb cd 81 cf 87",  # 4
                     "81 f9 f9 f9 c1 9f 9f 9f 99 c3",  # 5
                     "c7 f3 f9 f9 c1 99 99 99 99 c3",  # 6
                     "81 99 9f 9f cf cf e7 e7 f3 f3",  # 7
                     "c3 99 99 99 c3 99 99 99 99 c3",  # 8
                     "c3 99 99 99 99 83 9f 9f cf e3"); # 9
       
          @digits = ("3c 66 66 66 66 66 66 66 66 3c",  # 0
                     "30 38 30 30 30 30 30 30 30 30",  # 1
                     "3c 66 60 60 30 18 0c 06 06 7e",  # 2
                     "3c 66 60 60 38 60 60 60 66 3c",  # 3
                     "30 30 38 38 34 34 32 7e 30 78",  # 4
                     "7e 06 06 06 3e 60 60 60 66 3c",  # 5
                     "38 0c 06 06 3e 66 66 66 66 3c",  # 6
                     "7e 66 60 60 30 30 18 18 0c 0c",  # 7
                     "3c 66 66 66 3c 66 66 66 66 3c",  # 8
                     "3c 66 66 66 66 7c 60 60 30 1c"); # 9
    }
    sub incrementCount {
       &incrementTotalReads;
    }
    
    
    sub incrementTotalReads {
       if (-e $counterFile) {
         open(COUNT,"$counterFile") || die("Can't open $counterFile: $!\n");
       }
       $totalReads = <COUNT>;
       #chop $totalReads;
       close(COUNT);
    
         $totalReads++;
         open(COUNT,">$counterFile") || die "$0: can\'t open $counterFile: $!\n";
         print (COUNT "$totalReads\n");
         close(COUNT);
    
    
    }
    
    
    sub email {
    
    open(MAIL,"|$mailprog -t");
    
    print MAIL "To: error\@PremierWebsiteSolutions.com\n";
    print MAIL "From: Mike <webmaster\@PremierWebsiteSolutions.com>\n";
    print MAIL "Subject: 404 Error Occured\n\n";
    print MAIL "The page that was attempted to be called is $ENV{HTTP_REFERER}\n\n";
    
    close (MAIL);
    
    }
    
    Don't forget to change the email addresses.
     
  11. Website Rob

    Website Rob Well-Known Member

    Joined:
    Mar 23, 2002
    Messages:
    1,506
    Likes Received:
    0
    Trophy Points:
    36
    Location:
    Alberta, Canada
    cPanel Access Level:
    Root Administrator
    Without getting into who did what, the one line image generation was all that is/was required, to remove the 'end of headers' error msg.

    #!/usr/bin/perl

    print ("Content-type: image/x-xbitmap\n\n");

    $mailprog = "/usr/sbin/sendmail";

    open(MAIL,"|$mailprog -t");

    print MAIL "To: error\@domain.com\n";
    print MAIL "From: 404 error <page_error\@domain.com>\n";
    print MAIL "Subject: A 404 Error has Occured\n\n";
    print MAIL "The page that was attempted to be called is $ENV{HTTP_REFERER}\n\n";

    close (MAIL);

    exit;


    So, to cover all bases, thank you both for sharing. ;)
     
  12. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    True, that would do the trick. It would mean the script wouldn't keep a count of occurences in a seperate text file, but not everyone wants to bother with that.
     
  13. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    I have no idea where I got the original counter script from. It was about 3 years ago I found it.
     
  14. Website Rob

    Website Rob Well-Known Member

    Joined:
    Mar 23, 2002
    Messages:
    1,506
    Likes Received:
    0
    Trophy Points:
    36
    Location:
    Alberta, Canada
    cPanel Access Level:
    Root Administrator
    You know, I just realized both posts -- short version & long version -- were made by Mike. I wasn't looking at the poster name and had thought two different people posted.

    Please excuse my confusing posts on who posted what and we shall give all credit to Mike. &nbsp;here, here for another job well done.


    Also, would you state for the record, Mike, if this code is GPL or not?


    Edit: gets confusing when we are both posting at the same time. :D
     
  15. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    Who's posting at the same time? It's me then you then me ..... ;)

    Anyway, the original counter script isn't mine. I just added the subroutine to send the email.

    As far as I'm concerned, it's GPL, but I don't know about the original code.
     
  16. sawbuck

    sawbuck Well-Known Member

    Joined:
    Jan 18, 2004
    Messages:
    1,367
    Likes Received:
    5
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    Squirrel-
    Do I understand correctly that by adding the line Website Rob suggested to the first script that the error should be eliminated?
    If so didn't work that way for me.
    Any other thoughts?
    Thanks
     
  17. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    It should, but I haven't tested that. The full script shouldn't generate an error.
     
  18. sawbuck

    sawbuck Well-Known Member

    Joined:
    Jan 18, 2004
    Messages:
    1,367
    Likes Received:
    5
    Trophy Points:
    38
    cPanel Access Level:
    Root Administrator
    Must be something wrong on my end as either way I get errors. Now also receiving failed to open log file - fopen permission denied.
    Some of us just don't know better than to quit while we are ahead.:)
     
  19. PWSowner

    PWSowner Well-Known Member

    Joined:
    Nov 10, 2001
    Messages:
    2,948
    Likes Received:
    3
    Trophy Points:
    38
    Location:
    ON, Canada
    I sent you a PM
     
Loading...

Share This Page