home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / www / httpmon < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  5.3 KB  |  220 lines

  1. #!/bin/ksh
  2. # @(#) httpmon.ksh 1.1.1 97/07/18
  3. # 95/04/08 john h. dubois iii (john@armory.com)
  4. # 95/07/17 Do stty sane in case tty is not already set up.
  5. # 95/07/30 Added all options, use of default file.
  6. # 96/02/06 Understand new logfile format.
  7. # 96/03/06 Use gawk instead of awk.  Some bad requests result in enough fields
  8. #          in a record to make awk die.
  9. # 96/03/11 exec monitor procs.
  10. # 96/03/16 Do not print control characters.
  11. # 97/07/18 1.1.1 Changed default file & output to standard input & output;
  12. #          removed default logfile owner.
  13.  
  14. function mon {
  15.     # Must open tty separately for reading & writing
  16.     if [ -n "$MONTTY" ]; then
  17.     exec < "$MONTTY" > "$MONTTY"
  18.     stty sane
  19.     $Debug && print "$name: Testing output to monitor tty $MONTTY"
  20.     fi
  21.     if $Debug && [ $# -eq 1 ]; then
  22.     l -- "$1" >&2
  23.     print -u2 "Monitoring $1"
  24.     fi
  25.     exec tail -f "$@" | exec gawk -v Pat="$SO_PATTERN" -v Debug=$Debug '
  26.     BEGIN {
  27.     # Adding " to FS is cheaper than parsing correctly, and this
  28.     # does not have to be perfect.
  29.     FS = "[ \t\"]+"
  30.     Debug = Debug == "true"
  31.     if (Pat != "") {
  32.         cmd = "tput rev"
  33.         cmd | getline rev
  34.         close(cmd)
  35.         cmd = "tput rmso"
  36.         cmd | getline unrev
  37.         close(cmd)
  38.     }
  39.     }
  40.     NR == 1 {
  41.     if ($2 ~ /^\[/) {
  42.         actF = 7
  43.         objF = 8
  44.     }
  45.     else {
  46.         actF = 6
  47.         objF = 7
  48.     }
  49.     }
  50.     {
  51.     if (Debug)
  52.         print "Read: " $0 > "/dev/stderr"
  53.     if (Pat != "" && $objF ~ Pat) {
  54.         printf "%s",rev
  55.         DoUnrev = 1
  56.     }
  57.     # Print source host, action, and object
  58.     # Do not print a newline until after unrev
  59.     printf "%-30s %s %s",NoCtl($1),NoCtl($actF),NoCtl($objF)
  60.     if (DoUnrev) {
  61.         printf "%s\n",unrev
  62.         DoUnrev = 0
  63.     }
  64.     else
  65.         print ""
  66.     }
  67.     function NoCtl(S) {
  68.     gsub(/[^ -~]/,"?",S)
  69.     return S
  70.     }
  71.     '
  72. }
  73.  
  74. name=${0##*/}
  75. Usage=\
  76. "Usage: $name [-hx] [-t<tty>] [-o<logfile-owner>] [-s<standout-pat>] [logfile]"
  77. MONTTY=
  78. LOGFILE=
  79. LOGOWNER=
  80. SO_PATTERN=
  81. defaultFile=/etc/default/httpmon
  82. Debug=false
  83. PATH=$PATH:/usr/local/bin    # for gawk
  84. oTERM=$TERM
  85. export TERM=
  86.  
  87. # Build help message here so it will get default var values.
  88. help="$name: monitor HTTP requests.
  89. $name monitors an HTTP request logfile.  As records are added to the logfile
  90. by the HTTP daemon, the remote hostname and HTTP request are printed.
  91. $Usage
  92. If [logfile] is not given, the standard input is read.
  93. Options:
  94. -h: Print this help.
  95. -x: Turn on debugging.
  96. -t<tty>: Send output to <tty> instead of the standard output.
  97. -y<termtype>: Set the monitor tty terminal type to be <termtype> instead of
  98.     getting it from the TERM environment variable or, if -t is used, the
  99.     /etc/ttytype terminal type database.  A terminal type is only needed if a
  100.     standout pattern is used.
  101. -o<logfile-owner>: If the logfile does not exist, create it with ownership
  102.     of <logfile-owner>.  If the logfile-owner is not set, $name will abort
  103.     if the logfile does not exist.
  104. -s<standout-pattern>: URLs that match the given pattern will be displayed in
  105.     standout, if the display is capable of it.  By default, nothing is
  106.     displayed in standout.
  107. The logfile, monitor tty, logfile owner, standout pattern, and monitor tty
  108. terminal type can be set by assigning values to LOGFILE, MONTTY, and LOGOWNER,
  109. SO_PATTERN, and TERM in the file $defaultFile, in the form e.g.:
  110.  
  111. SO_PATTERN='^/~(john|spcecdt|pax|zenomt)/'
  112. MONTTY=/dev/tty10
  113. LOGFILE=/usr/local/lib/httpd/logs/access_log
  114. LOGOWNER=spinner
  115.  
  116. Arguments given on the command line will override values assigned in the
  117. default file."
  118.  
  119. [ -f "$defaultFile" ] && . "$defaultFile"
  120.  
  121. while getopts :hxt:o:s:y: opt; do
  122.     case $opt in
  123.     h)
  124.     echo "$help"
  125.     exit 0
  126.     ;;
  127.     x)
  128.     Debug=true
  129.     ;;
  130.     o)
  131.     LOGOWNER=$OPTARG
  132.     ;;
  133.     t)
  134.     MONTTY=$OPTARG
  135.     ;;
  136.     y)
  137.     TERM=$OPTARG
  138.     ;;
  139.     s)
  140.     SO_PATTERN=$OPTARG
  141.     ;;
  142.     +?)
  143.     print -u2 "$name: options should not be preceded by a '+'."
  144.     exit 1
  145.     ;;
  146.     :)
  147.         print -r -u2 -- \
  148.         "$name: Option '$OPTARG' requires a value.  Use -h for help."
  149.         exit 1
  150.         ;;
  151.     ?)
  152.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  153.     exit 1
  154.     ;;
  155.     esac
  156. done
  157.  
  158. # remove args that were options
  159. let OPTIND=OPTIND-1
  160. shift $OPTIND
  161.  
  162. case $# in
  163. 0) 
  164.     ;;
  165. 1) LOGFILE=$1
  166.     ;;
  167. *)
  168.     print -u2 "$Usage\nUse -h for help."
  169.     exit 1
  170.     ;;
  171. esac
  172.  
  173. if [ -n "$MONTTY" ]; then
  174.     MONTTY=${MONTTY##/dev/}
  175.     if [ -n "$SO_PATTERN" -a -z "$TERM" ]; then
  176.     set -- $(egrep "[     ]+$MONTTY[     ]*\$" /etc/ttytype)
  177.     TERM=$1
  178.     if [ -z "$TERM" ]; then
  179.         print -u2 \
  180. "$name: Warning: No termtype for tty $MONTTY found in /etc/ttytype;
  181. no standout will be used."
  182.         unset SO_PATTERN
  183.     fi
  184.     fi
  185.     [[ "$MONTTY" != */* ]] && MONTTY=/dev/$MONTTY
  186. elif [ -n "$SO_PATTERN" -a -z "$TERM" ]; then
  187.     if [ -n "$oTERM" ]; then
  188.     TERM=$oTERM
  189.     else
  190.     print -u2 \
  191.     "$name: Warning: TERM not set and -t not given; no standout will be used."
  192.     unset SO_PATTERN
  193.     fi
  194. fi
  195. export TERM
  196.  
  197. $Debug && print -u2 \
  198. "LOGFILE=$LOGFILE
  199. LOGOWNER=$LOGOWNER
  200. MONTTY=$MONTTY
  201. SO_PATTERN=$SO_PATTERN
  202. TERM=$TERM"
  203.  
  204. if [ -n "$LOGFILE" -a ! -a "$LOGFILE" ]; then    # Recreate it if neccessary
  205.     if [ -n "$LOGOWNER" ]; then
  206.     $Debug && print -u2 "logfile did not exist; creating it..."
  207.     touch "$LOGFILE" &&
  208.     chown "$LOGOWNER" "$LOGFILE" &&
  209.     chmod a+r "$LOGFILE" || {
  210.         print -ru2 -- \
  211. "$name: Could not create/chown/chmod logfile $LOGFILE; aborting."
  212.         exit 1
  213.     }
  214.     else
  215.     print -ru2 -- "$name: logfile $LOGFILE does not exist; aborting."
  216.     exit 1
  217.     fi
  218. fi
  219. [ -n "$LOGFILE" ] && mon "$LOGFILE" || mon
  220.