Changes between Initial Version and Version 1 of ErrorCodeCheck


Ignore:
Timestamp:
07/10/13 10:00:21 (3 years ago)
Author:
chris
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ErrorCodeCheck

    v1 v1  
     1= Webserver log file error code checking = 
     2 
     3Following is a script which is installed on wiki:PuffinServer as {{{/usr/local/bin/weblog-erors}}} and which is run via {{{/etc/logrotate.d/nginx}}} and it sends a email with the results just before the log is rotated each day: 
     4 
     5{{{ 
     6/var/log/nginx/*.log { 
     7        daily 
     8        missingok 
     9        rotate 52 
     10        compress 
     11        delaycompress 
     12        notifempty 
     13        create 0640 www-data adm 
     14        sharedscripts 
     15        prerotate 
     16                /usr/local/bin/nginx-logs 
     17                /usr/local/bin/weblog-erors chris@webarchitects.co.uk 
     18                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ 
     19                        run-parts /etc/logrotate.d/httpd-prerotate; \ 
     20                fi \ 
     21        endscript 
     22        postrotate 
     23                [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid` 
     24        endscript 
     25} 
     26}}} 
     27 
     28The script can also be run on the commend line and piped into a pager, for example: 
     29 
     30{{{ 
     31sudo /usr/local/bin/weblog-erors | less 
     32}}} 
     33 
     34== weblog-erors == 
     35 
     36{{{ 
     37#!/bin/bash 
     38 
     39# The log file we are checking, best run the script via logrotate, 
     40# for example edit /etc/logrotate.d/nginx to: 
     41# 
     42#   prerotate 
     43#          /usr/local/bin/weblog-erors chris@webarchitects.co.uk 
     44#          if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ 
     45#                  run-parts /etc/logrotate.d/httpd-prerotate; \ 
     46#          fi \ 
     47#   endscript 
     48LOGFILE="/var/log/nginx/access.log" 
     49 
     50# Optional email address to send the reults to 
     51EMAIL=$1 
     52 
     53# check that the script is being run by root 
     54if [[ "$(id -u)" != "0" ]] ; then 
     55  echo "You must run $0 as root or via sudo"  
     56  exit 2 
     57fi 
     58 
     59# grep for all the ilines with error codes   
     60ERRORS=$(grep '1.[0|1]" [4|5]0[2|3|4] ' $LOGFILE) 
     61# grep for to totals for these errors codes  
     62ERRORS_403=$(grep -c '1.[0|1]" 403 ' $LOGFILE) 
     63ERRORS_404=$(grep -c '1.[0|1]" 404 ' $LOGFILE) 
     64ERRORS_502=$(grep -c '1.[0|1]" 502 ' $LOGFILE) 
     65ERRORS_503=$(grep -c '1.[0|1]" 503 ' $LOGFILE) 
     66ERRORS_504=$(grep -c '1.[0|1]" 504 ' $LOGFILE) 
     67 
     68# check to see if any errors were found 
     69if [[ $ERRORS ]]; then 
     70  # check for a email address  
     71  if [[ $EMAIL ]]; then 
     72    # we were supplied with a email address so send the results by email using mutt 
     73    # name of the server 
     74    HOSTNAME=$(hostname) 
     75    # email subject line 
     76    SUBJECT="$ERRORS_403 403, $ERRORS_404 404, $ERRORS_502 502, $ERRORS_503 503 and $ERRORS_504 504 errors from $HOSTNAME" 
     77    echo "$ERRORS" | mutt -s "$SUBJECT" $EMAIL 
     78  # we don't have a email address so display the results of the grep 
     79  else [[ ! $EMAIL ]] 
     80    echo "Supply a email address on the command line to send the following results by email"  
     81    echo "" 
     82    echo "Total 403 errors: $ERRORS_403" 
     83    echo "Total 404 errors: $ERRORS_404" 
     84    echo "Total 502 errors: $ERRORS_502" 
     85    echo "Total 503 errors: $ERRORS_503" 
     86    echo "Total 504 errors: $ERRORS_504" 
     87    echo "" 
     88    echo "Lines with errors from $LOGFILE" 
     89    echo "" 
     90    echo "$ERRORS"  
     91    echo "" 
     92    echo "This script is best piped into a pager, eg:" 
     93    echo "$0 | less" 
     94  fi 
     95else 
     96   # we don't want any output if a email address is specified as the script is run via cron 
     97   if [[ ! $EMAIL ]] ; then 
     98     echo "No recent 403, 404, 502, 503 or 504 errors were found"  
     99   fi 
     100fi 
     101}}}