extract_log 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #!/bin/ksh
  2. #
  3. # extract_log.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. ##########################################################################
  22. ##########################################################################
  23. #
  24. # Script specific global variables
  25. #
  26. ##########################################################################
  27. ##########################################################################
  28. COMPONENTS_FILES=""
  29. COMPONENTS=""
  30. DEBUG="False"
  31. LOG_FILE=""
  32. ERROR_FILE=""
  33. LOG_DIRECTORY=""
  34. PROG_NAME="`basename $0`"
  35. usage ()
  36. {
  37. print -u1 "USAGE: $1"
  38. print -u1 "\t[{-c | -components_file} <file>]"
  39. print -u1 "\t # Specifies a file containing a list of components to"
  40. print -u1 "\t # be extracted. Multiple -c flags can be specified."
  41. print -u1 "\t{-e | -errorfile} <file>"
  42. print -u1 "\t # Specifies the error file to send errors."
  43. print -u1 "\t[-h | -? | -help]"
  44. print -u1 "\t # Print usage and exit"
  45. print -u1 "\t[{-ld | -logdirectory} <directory>]"
  46. print -u1 "\t # Specifies an alternative directory to store the"
  47. print -u1 "\t # extracted component logs. Defaults to the directory"
  48. print -u1 "\t # containing the log file."
  49. print -u1 "\t{-l | -logfile} <file>"
  50. print -u1 "\t # Specifies the log file to be extracted from."
  51. print -u1 "\t[component ...]"
  52. print -u1 "\t # Specifies individual components to be extraced."
  53. print -u1 "\t # Each component specification is should correspond"
  54. print -u1 "\t # to an individual directory in the source tree."
  55. }
  56. #
  57. # Do command-line processing
  58. #
  59. while [ $# -gt 0 ]; do
  60. case $1 in
  61. -debug)
  62. DEBUG="True"
  63. shift 1 ;;
  64. -c | -components_file)
  65. if [ $# -lt 2 ]; then
  66. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  67. do_exit 1
  68. fi
  69. COMPONENTS_FILES="$2 $COMPONENTS_FILES"
  70. shift 2 ;;
  71. -e | -errorfile)
  72. if [ $# -lt 2 ]; then
  73. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  74. do_exit 1
  75. fi
  76. ERROR_FILE=$2
  77. shift 2 ;;
  78. -ld | -logdirectory)
  79. if [ $# -lt 2 ]; then
  80. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  81. do_exit 1
  82. fi
  83. LOG_DIRECTORY=$2
  84. shift 2 ;;
  85. -l | -logfile)
  86. if [ $# -lt 2 ]; then
  87. print -u2 "$PROG_NAME: $1 option missing value; exiting ..."
  88. do_exit 1
  89. fi
  90. LOG_FILE=$2
  91. shift 2 ;;
  92. -h | -? | -help)
  93. usage $PROG_NAME
  94. do_exit 1 ;;
  95. *)
  96. COMPONENTS="$COMPONENTS $1"
  97. shift 1;;
  98. esac
  99. done
  100. if [ ! -z "$ERROR_FILE" ]
  101. then
  102. exec 2>> $ERROR_FILE
  103. fi
  104. #
  105. # Check to make sure that the command-line parameters make sense.
  106. #
  107. if [ -z "$COMPONENTS_FILES" ] && [ -z "$COMPONENTS" ]
  108. then
  109. print -u2 "$PROG_NAME: No components or component files specified."
  110. print -u2 "$PROG_NAME: exiting ..."
  111. do_exit 1
  112. fi
  113. for f in $COMPONENTS_FILES
  114. do
  115. if [ ! -f $f ]
  116. then
  117. print -u2 "$PROG_NAME: Component file \"$f\" does not exist."
  118. print -u2 "$PROG_NAME: 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."
  125. print -u2 "$PROG_NAME: exiting ..."
  126. do_exit 1
  127. fi
  128. if [ ! -f $LOG_FILE ]
  129. then
  130. print -u2 "$PROG_NAME: Log file \"$LOG_FILE\" does not exist."
  131. print -u2 "$PROG_NAME: exiting ..."
  132. do_exit 1
  133. fi
  134. if [ -n "$LOG_DIRECTORY" ] && [ ! -d $LOG_DIRECTORY ]
  135. then
  136. print -u2 "$PROG_NAME: Log directory \"$LOG_DIRECTORY\" does not exist."
  137. print -u2 "$PROG_NAME: exiting ..."
  138. do_exit 1
  139. fi
  140. if [ -z "$LOG_DIRECTORY" ]
  141. then
  142. LOG_DIRECTORY=`dirname $LOG_FILE`
  143. #
  144. # Just being paranoid. dirname should return '.' if there is no
  145. # directory component.
  146. #
  147. if [ -z "$LOG_DIRECTORY" ]
  148. then
  149. LOG_DIRECTORY='.'
  150. fi
  151. fi
  152. #
  153. # Collect all the components from the components files.
  154. #
  155. for f in "$COMPONENTS_FILES"
  156. do
  157. for c in `cat $f`
  158. do
  159. COMPONENTS="$COMPONENTS $c"
  160. done
  161. done
  162. #
  163. # Collect all the build messages
  164. # ignoring those that have been commented out.
  165. #
  166. MESSAGES="XXXXXXX"
  167. IFS="
  168. "
  169. for m in `cat $BUILD_MSGS`
  170. do
  171. MESSAGES="$MESSAGES|$m"
  172. done
  173. IFS=" "
  174. #
  175. # Build the awk script
  176. #
  177. SCRIPT=/tmp/${PROG_NAME}.$$.awk
  178. do_register_temporary_file $SCRIPT
  179. touch $SCRIPT
  180. chmod 775 $SCRIPT
  181. print -n -u1 'BEGIN {
  182. do_print = 0
  183. }
  184. /.*/ {
  185. if (' >> $SCRIPT
  186. IFS="|"
  187. let i=0
  188. for m in $MESSAGES
  189. do
  190. if [ i -gt 0 ]; then
  191. print -n -u1 " || " >> $SCRIPT
  192. fi
  193. print -n -u1 "index(\$0, \"$m \")" >> $SCRIPT
  194. let i=$i+1
  195. done
  196. IFS=" "
  197. #
  198. # NOTE on: (index($NF, PATTERN) == 1 || index($NF, PATTERN) == 3)
  199. # This check is intended to guard against false matches on
  200. # subcomponents: i.e. config and programs/dtlogin/config.
  201. # The problem is that top level subdirectories show up differently
  202. # than lower level directories. E.g.:
  203. # /prog/cde/config => making all in ./config...
  204. # /prog/cde/programs/dtlogin => making all in programs/dtlogin...
  205. #
  206. # There are 2 ways to handle this, in the components files or here.
  207. # I've chosen here.
  208. #
  209. print -n -u1 ')
  210. {
  211. if (index($NF, PATTERN) == 1 || index($NF, PATTERN) == 3)
  212. {
  213. do_print = 1
  214. print
  215. next
  216. }
  217. else
  218. {
  219. do_print = 0
  220. next
  221. }
  222. }
  223. if (do_print) print
  224. }' >> $SCRIPT
  225. #
  226. # Extract each of the specified component logs.
  227. #
  228. TMP_LOG_FILE=${LOG_FILE}.$$
  229. do_register_temporary_file $TMP_LOG_FILE
  230. sed -n -e 's/\(.\{0,254\}\).*/\1/p' $LOG_FILE > $TMP_LOG_FILE
  231. for c in $COMPONENTS
  232. do
  233. COMPONENT_LOG=$LOG_DIRECTORY/`echo $c | tr "/" ","`.log
  234. PATTERN="$c"
  235. #
  236. # sed protects awk from lines which are too long.
  237. #
  238. if [ "$DEBUG" = "True" ]
  239. then
  240. echo "awk -f $SCRIPT PATTERN=$PATTERN $TMP_LOG_FILE >
  241. $COMPONENT_LOG"
  242. else
  243. awk -f $SCRIPT PATTERN=$PATTERN $TMP_LOG_FILE > $COMPONENT_LOG
  244. fi
  245. done
  246. #
  247. # Clean up temporary files and exit
  248. #
  249. do_exit 0