extract_msg 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/bin/ksh
  2. #
  3. # extract_msg.ksh
  4. #
  5. ########################################################################
  6. #set -x
  7. ##########################################################################
  8. #
  9. # Script setup: THIS NEEDS TO BE FIRST
  10. #
  11. SCRIPTS_DIR="`dirname $0`"
  12. if [ "" = "$SCRIPTS_DIR" ]; then
  13. SCRIPTS_DIR=/project/dt/scripts
  14. fi
  15. if [ ! -f $SCRIPTS_DIR/script_setup.ksh ]; then
  16. print -u2 "$PRG: File '$SCRIPTS_DIR/script_setup.ksh' NOT found!"
  17. print -u2 "$PRG: Exiting ..."
  18. exit 1
  19. fi
  20. . $SCRIPTS_DIR/script_setup.ksh
  21. DEBUG="False"
  22. IGNORED_MESSAGE_FILES=""
  23. IGNORED_MESSAGES_INIT="XXXXXXX"
  24. IGNORED_MESSAGES="$IGNORED_MESSAGES_INIT"
  25. LOG_FILE=""
  26. ERROR_FILE=""
  27. MESSAGE_FILES=""
  28. MESSAGES_INIT="XXXXXXX"
  29. MESSAGES="$MESSAGES_INIT"
  30. PROG_NAME="`basename $0`"
  31. usage ()
  32. {
  33. print -u1 "USAGE: $1"
  34. print -u1 "\t{-e | -errorfile} <file>"
  35. print -u1 "\t # Specifies the error file to send errors."
  36. print -u1 "\t[-h | -? | -help]"
  37. print -u1 "\t # Print usage and exit"
  38. print -u1 "\t[{-i | -ignoredmsgfile} <file>]"
  39. print -u1 "\t # Specifies a file containing messages to be"
  40. print -u1 "\t # ignored. Multiple -i flags can be specified."
  41. print -u1 "\t{-l | -logfile} <file>"
  42. print -u1 "\t # Specifies the log file to be extracted from."
  43. print -u1 "\t[{-m | -msgfile} <file>]"
  44. print -u1 "\t # Specifies a file containing messages to be"
  45. print -u1 "\t # extracted. Multiple -m flags can be specified."
  46. print -u1 "\t[messages ...]"
  47. print -u1 "\t # Specifies individual messages to be extraced."
  48. }
  49. #
  50. # Do command-line processing
  51. #
  52. while [ $# -gt 0 ]; do
  53. case $1 in
  54. -debug)
  55. DEBUG="True"
  56. shift 1 ;;
  57. -e | -errorfile)
  58. if [ $# -lt 2 ]; then
  59. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  60. do_exit 1
  61. fi
  62. ERROR_FILE=$2
  63. shift 2 ;;
  64. -i | -msgfile)
  65. if [ $# -lt 2 ]; then
  66. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  67. do_exit 1
  68. fi
  69. IGNORED_MESSAGE_FILES="$IGNORED_MESSAGE_FILES $2"
  70. shift 2 ;;
  71. -m | -msgfile)
  72. if [ $# -lt 2 ]; then
  73. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  74. do_exit 1
  75. fi
  76. MESSAGE_FILES="$MESSAGE_FILES $2"
  77. shift 2 ;;
  78. -l | -logfile)
  79. if [ $# -lt 2 ]; then
  80. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  81. do_exit 1
  82. fi
  83. LOG_FILE=$2
  84. shift 2 ;;
  85. -h | -? | -help)
  86. usage $PROG_NAME
  87. do_exit 1 ;;
  88. *)
  89. MESSAGES="$MESSAGES|$1"
  90. shift 1;;
  91. esac
  92. done
  93. if [ ! -z "$ERROR_FILE" ]
  94. then
  95. exec 2>> $ERROR_FILE
  96. fi
  97. #
  98. # Check to make sure that the command-line parameters make sense.
  99. #
  100. if [ -z "$MESSAGE_FILES" ] && [ "$MESSAGES" = "$MESSAGES_INIT" ]
  101. then
  102. print -u2 "$PROG_NAME: No messages or message files have been specified."
  103. print -u2 "$PROG_NAME: exiting ..."
  104. do_exit 1
  105. fi
  106. for f in $IGNORED_MESSAGE_FILES
  107. do
  108. if [ ! -f $f ]
  109. then
  110. print -u2 "$PROG_NAME: Message file \"$f\" does not exist; exiting ..."
  111. do_exit 1
  112. fi
  113. done
  114. for f in $MESSAGE_FILES
  115. do
  116. if [ ! -f $f ]
  117. then
  118. print -u2 "$PROG_NAME: Message file \"$f\" does not exist; exiting ..."
  119. do_exit 1
  120. fi
  121. done
  122. if [ -z "$LOG_FILE" ]
  123. then
  124. print -u2 "$PROG_NAME: Missing argument for log file; exiting ..."
  125. do_exit 1
  126. fi
  127. if [ ! -f $LOG_FILE ]
  128. then
  129. print -u2 "$PROG_NAME: Log file \"$LOG_FILE\" does not exist; exiting ..."
  130. do_exit 1
  131. fi
  132. #
  133. # Determine where to find perl.
  134. #
  135. PERL="/usr/local/bin/perl"
  136. if [ ! -x $PERL ]
  137. then
  138. print -u2 "$PROG_NAME: Can't find perl executable $PERL; exiting ..."
  139. do_exit 1
  140. fi
  141. #
  142. # Collect all the regular expressions from the ignored message files.
  143. #
  144. for f in $IGNORED_MESSAGE_FILES
  145. do
  146. IGNORED_MESSAGES="$IGNORED_MESSAGES`sed -e '1,$s/.*/|&/' $f`"
  147. done
  148. #
  149. # Collect all the regular expressions from the message files.
  150. #
  151. for f in $MESSAGE_FILES
  152. do
  153. MESSAGES="$MESSAGES`sed -e '1,$s/.*/|&/' $f`"
  154. done
  155. if [ "$DEBUG" = "True" ]
  156. then
  157. print -u1 "======= DEBUG DEBUG IGNORED MESSAGES DEBUG DEBUG ========"
  158. print -u1 $IGNORED_MESSAGES
  159. print -u1 "======= DEBUG DEBUG ================ DEBUG DEBUG ========"
  160. print -u1 "======= DEBUG DEBUG MESSAGES DEBUG DEBUG ========"
  161. print -u1 $MESSAGES
  162. print -u1 "======= DEBUG DEBUG ================ DEBUG DEBUG ========"
  163. fi
  164. #
  165. # Build the perl script
  166. #
  167. SCRIPT=/tmp/${PROG_NAME}.$$.pl
  168. if [ "$DEBUG" = "False" ]
  169. then
  170. do_register_temporary_file $SCRIPT
  171. fi
  172. touch $SCRIPT
  173. chmod 755 $SCRIPT
  174. print -u1 "#!$PERL" >> $SCRIPT
  175. print -u1 "LINE: while (<>) {" >> $SCRIPT
  176. IFS="|"
  177. for m in $IGNORED_MESSAGES
  178. do
  179. MESSAGE=`echo $m | sed -e 's?\/?\\\/?g'`
  180. print -u1 "next LINE if /$MESSAGE/;" >> $SCRIPT
  181. done
  182. for m in $MESSAGES
  183. do
  184. MESSAGE=`echo $m | sed -e 's?\/?\\\/?g'`
  185. print -u1 "next LINE if /$MESSAGE/ && print;" >> $SCRIPT
  186. done
  187. IFS=" "
  188. print -u1 "}" >> $SCRIPT
  189. if [ "$DEBUG" = "True" ]
  190. then
  191. print -u1 "======= DEBUG DEBUG SCRIPT DEBUG DEBUG ========"
  192. cat $SCRIPT
  193. print -u1 "======= DEBUG DEBUG ================ DEBUG DEBUG ========"
  194. fi
  195. #
  196. # Use the perl script to extract the desired messages from the log file.
  197. #
  198. if [ "$DEBUG" = "True" ]
  199. then
  200. print -u1 "======= DEBUG DEBUG RUN SCRIPT DEBUG DEBUG ========"
  201. print -u1 cat $LOG_FILE | $SCRIPT
  202. print -u1 "======= DEBUG DEBUG ================ DEBUG DEBUG ========"
  203. fi
  204. cat $LOG_FILE | $SCRIPT
  205. #
  206. # Clean up temporary files and exit
  207. #
  208. do_exit 0