recog.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/bin/sh
  2. SCRIPT_VERSION="19.1($CT_BUILD_VERSION)"
  3. SCRIPT_NAME=${0}
  4. ##########################################################################################
  5. ## recog.sh v 19.1
  6. ## - REmote COpy and Gathering script.
  7. ## - Connects to remote machine, copys Collection scripts, gathers data and returns.
  8. ##
  9. COLLECTIONZIP=../../Collection_Tool.zip
  10. REMOTEMACHINE=
  11. REMOTEDIR=
  12. REM_SCRIPT_OPTIONS=
  13. REMCOG_COLLECTED=output/logs/REMCOG_collected.log
  14. REMCOG_WARNINGS=output/logs/REMCOG_warnings.log
  15. REMCOG_ERRORS=output/logs/REMCOG_errors.log
  16. RUN_AS_SUDO=
  17. ECHO_recog="echo_recog_print"
  18. ##############################################################
  19. # make echo more portable
  20. #
  21. echo_recog_print() {
  22. #IFS=" " command
  23. eval 'printf "%b\n" "$*"'
  24. }
  25. ##############################################################
  26. # make echo more portable
  27. #
  28. echo_recog_log() {
  29. $ECHO_recog "$1"
  30. $ECHO_recog "$1" >> $2
  31. }
  32. ##############################################################
  33. # recog_connect() - connect based on REMOTEINFO
  34. #
  35. recog_connect() {
  36. $ECHO_recog "\nGetting remote machine name\n"
  37. REM_MACHINE_NAME=`ssh $REMOTEINFO $RUN_AS_SUDO uname \-n`
  38. $ECHO_recog "\nCreating directory on $REM_MACHINE_NAME\n"
  39. # make temp directory on remote machine
  40. ssh $REMOTEINFO "$RUN_AS_SUDO mkdir -p /tmp/CTrecog ; $RUN_AS_SUDO chmod -R 777 /tmp/CTrecog"
  41. if [ ${?} -ne 0 ] ; then
  42. echo_recog_log "CT: RECOG-05000: ERROR: Error creating directory on remote machine." $REMCOG_ERRORS
  43. return 1
  44. fi
  45. $ECHO_recog "\nCopying Collection Tool files to remote machine.\n"
  46. #copy Collection tool
  47. scp $COLLECTIONZIP $REMOTEINFO:/tmp/CTrecog/.
  48. if [ ${?} -ne 0 ] ; then
  49. echo_recog_log "CT: RECOG-05001: ERROR: Error copying Collection Tool files to remote machine." $REMCOG_ERRORS
  50. return 1
  51. fi
  52. $ECHO_recog "\nUnzipping Collection Tool files to remote machine\n"
  53. # Unzip the tool, use -o to extrat only new files and not prompt user.
  54. ssh $REMOTEINFO $RUN_AS_SUDO unzip -q -o /tmp/CTrecog/Collection_Tool.zip -d /tmp/CTrecog
  55. if [ ${?} -ne 0 ] ; then
  56. echo_recog_log "CT: RECOG-05002: ERROR: Error unzipping Collection Tool files to remote machine." $REMCOG_ERRORS
  57. return 1
  58. fi
  59. $ECHO_recog "\nSetting up permissions on remote machines\n"
  60. # run Collection Tool
  61. ssh $REMOTEINFO $RUN_AS_SUDO chmod -R 777 /tmp/CTrecog
  62. if [ ${?} -ne 0 ] ; then
  63. echo_recog_log "CT: RECOG-05003: ERROR: Error setting up permissions on remote machines." $REMCOG_ERRORS
  64. return 1
  65. fi
  66. $ECHO_recog "\nRunning Collection Tool on remote machine with options: $REM_SCRIPT_OPTIONS \n"
  67. ssh $REMOTEINFO "cd /tmp/CTrecog/Collection/bin/ ; $RUN_AS_SUDO sh Collection.sh $REM_SCRIPT_OPTIONS ; $RUN_AS_SUDO chmod -R 777 output"
  68. if [ ${?} -ne 0 ] ; then
  69. echo_recog_log "CT: RECOG-05004: ERROR: Error running Collection Tool on remote machine." $REMCOG_ERRORS
  70. return 1
  71. fi
  72. if [ "$MASK_DATA" != "" ] ; then
  73. CT_REM_TAR_FILE=/tmp/CTrecog/Collection/bin/output/Collection-${REM_MACHINE_NAME}${PRODUCTSRUN}-masked.tar
  74. LOCAL_OUTPUT_FILE=${LOCAL_OUTPUT_DIR}/Collection-${REM_MACHINE_NAME}${PRODUCTSRUN}-masked.tar
  75. CT_REM_DEBUG_TAR_FILE=/tmp/CTrecog/Collection/bin/output/debug_Collection-${REM_MACHINE_NAME}${PRODUCTSRUN}-masked.tar
  76. LOCAL_DEBUG_OUTPUT_FILE=${LOCAL_OUTPUT_DIR}/debug_Collection-${REM_MACHINE_NAME}${PRODUCTSRUN}-masked.tar
  77. else
  78. CT_REM_TAR_FILE=/tmp/CTrecog/Collection/bin/output/Collection-${REM_MACHINE_NAME}${PRODUCTSRUN}.tar
  79. LOCAL_OUTPUT_FILE=${LOCAL_OUTPUT_DIR}/Collection-${REM_MACHINE_NAME}${PRODUCTSRUN}.tar
  80. CT_REM_DEBUG_TAR_FILE=/tmp/CTrecog/Collection/bin/output/debug_Collection-${REM_MACHINE_NAME}${PRODUCTSRUN}.tar
  81. LOCAL_DEBUG_OUTPUT_FILE=${LOCAL_OUTPUT_DIR}/debug_Collection-${REM_MACHINE_NAME}${PRODUCTSRUN}.tar
  82. fi
  83. clear
  84. TARTIMESTAMP=`date '+%Y%m%d_%H%M%S'`
  85. # check if compressed archive file exists, if so move older one.
  86. if [ -f $LOCAL_OUTPUT_FILE.bz2 ] ; then
  87. mv $LOCAL_OUTPUT_FILE.bz2 $LOCAL_OUTPUT_FILE.bz2.$TARTIMESTAMP
  88. mv $LOCAL_DEBUG_OUTPUT_FILE.bz2 $LOCAL_DEBUG_OUTPUT_FILE.bz2.$TARTIMESTAMP
  89. fi
  90. if [ -f $LOCAL_DEBUG_OUTPUT_FILE.Z ] ; then
  91. mv $LOCAL_OUTPUT_FILE.Z $LOCAL_OUTPUT_FILE.Z.$TARTIMESTAMP
  92. mv $LOCAL_DEBUG_OUTPUT_FILE.Z $LOCAL_DEBUG_OUTPUT_FILE.Z.$TARTIMESTAMP
  93. fi
  94. # gather results
  95. $ECHO_recog "\nCopying the archive from the remote machine...\n"
  96. scp $REMOTEINFO:${CT_REM_TAR_FILE}.bz2 ${LOCAL_OUTPUT_DIR}/.
  97. if [ ${?} -ne 0 ] ; then
  98. scp $REMOTEINFO:${CT_REM_TAR_FILE}.Z ${LOCAL_OUTPUT_DIR}/.
  99. if [ ${?} -ne 0 ] ; then
  100. scp $REMOTEINFO:${CT_REM_TAR_FILE} ${LOCAL_OUTPUT_DIR}/.
  101. if [ ${?} -ne 0 ] ; then
  102. echo_recog_log "CT: RECOG-05005: ERROR: Unable to collect ${CT_REM_TAR_FILE} from the remote system. Please see documentation for troubleshooting information." $REMCOG_ERRORS
  103. else
  104. echo_recog_print "Please forward ${LOCAL_OUTPUT_FILE} to your assigned consultant"
  105. fi
  106. else
  107. echo_recog_print "Please forward ${LOCAL_OUTPUT_FILE}.Z to your assigned consultant"
  108. fi
  109. else
  110. echo_recog_print "Please forward ${LOCAL_OUTPUT_FILE}.bz2 to your assigned consultant"
  111. fi
  112. $ECHO_recog "\nCopying the debug logs from the remote machine...\n"
  113. scp $REMOTEINFO:${CT_REM_DEBUG_TAR_FILE}.bz2 ${LOCAL_OUTPUT_DIR}/.
  114. if [ ${?} -ne 0 ] ; then
  115. scp $REMOTEINFO:${CT_REM_DEBUG_TAR_FILE}.Z ${LOCAL_OUTPUT_DIR}/.
  116. if [ ${?} -ne 0 ] ; then
  117. scp $REMOTEINFO:${CT_REM_DEBUG_TAR_FILE} ${LOCAL_OUTPUT_DIR}/.
  118. if [ ${?} -ne 0 ] ; then
  119. echo_recog_log "CT: RECOG-05005: ERROR: Unable to collect ${LOCAL_DEBUG_OUTPUT_FILE} from the remote system. Please see documentation for troubleshooting information." $REMCOG_ERRORS
  120. else
  121. echo_recog_print "CT: CT-00040: WARNING: Please note that for debugging purposes the following archive file was created in the output folder: $LOCAL_DEBUG_OUTPUT_FILE"
  122. fi
  123. else
  124. echo_recog_print "CT: CT-00040: WARNING: Please note that for debugging purposes the following archive file was created in the output folder: $LOCAL_DEBUG_OUTPUT_FILE.Z"
  125. fi
  126. else
  127. echo_recog_print "CT: CT-00040: WARNING: Please note that for debugging purposes the following archive file was created in the output folder: $LOCAL_DEBUG_OUTPUT_FILE.bz2"
  128. fi
  129. }
  130. ##############################################################
  131. # setArgs() - setup args
  132. #
  133. setArgs () {
  134. PRODUCTSRUN=
  135. while [ "$1" != "" ]
  136. do
  137. case $1 in
  138. -p)
  139. REMOTEPRODARGS="$1 $2"
  140. REMOTEPRODLIST=$2
  141. shift 2
  142. ;;
  143. -sudo)
  144. RUN_AS_SUDO="sudo"
  145. shift
  146. ;;
  147. *\@*)
  148. REMOTEINFO=$1
  149. shift
  150. ;;
  151. esac
  152. done
  153. if [ "$REMOTEPRODLIST" = "all" ] ; then
  154. PRODUCTSRUN="_all"
  155. else
  156. RMTMPPLIST="$REMOTEPRODLIST"
  157. REMOTEPRODLIST=
  158. PRODUCTSRUN=
  159. for RMCHOSENPROD in `echo $RMTMPPLIST | tr ',' ' '`
  160. do
  161. PRODUCTSRUN="${PRODUCTSRUN}_${RMCHOSENPROD}"
  162. done
  163. fi
  164. }
  165. LOCAL_OUTPUT_DIR=${OUTPUT_DIR}
  166. # set up args, remove -r argument from remote running of the script to avoid loop.
  167. while [ "$1" != "" ]
  168. do
  169. case $1 in
  170. -r) # ignore -r for remote run
  171. shift 2
  172. ;;
  173. -o) # ignore output for remote run put it in local tmp/CTrecog
  174. shift 2
  175. ;;
  176. -L) # ignore -L for remote run we automatically add it.
  177. shift 2
  178. ;;
  179. *)
  180. if [ "$1" = "-fastsearch" ] ; then
  181. REM_SCRIPT_OPTIONS="$REM_SCRIPT_OPTIONS $1"
  182. shift 1
  183. else
  184. REM_SCRIPT_OPTIONS="$REM_SCRIPT_OPTIONS $1 $2"
  185. shift 2
  186. fi
  187. ;;
  188. esac
  189. done
  190. # in Remote Connection mode run script without license prompt
  191. REM_SCRIPT_OPTIONS="$REM_SCRIPT_OPTIONS -L Y"
  192. # If products where chosen via the menu, add PRODUCTSRUN to options
  193. if [ "$REMOTE_PROD_OPTS" != "" ] ; then
  194. RMPRODS=${REMOTE_PROD_OPTS:1}
  195. REM_SCRIPT_OPTIONS="$REM_SCRIPT_OPTIONS -p $RMPRODS"
  196. fi
  197. REMOTEFILENAME=
  198. case "$REMOTEINFO" in
  199. *\@*)
  200. # Remote usage specified on command line
  201. recog_connect
  202. ;;
  203. *)
  204. REMOTEFILENAME=$REMOTEINFO
  205. $ECHO_recog "\nReading $REMOTEINFO..."
  206. old_IFS=$IFS # save the field separator
  207. IFS=$'\n' # new field separator, the end of line
  208. for remoteIn in `cat $REMOTEFILENAME`
  209. do
  210. case "$remoteIn" in
  211. "#"* )
  212. ;;
  213. "" )
  214. ;;
  215. *\@*)
  216. IFS=' '
  217. setArgs $remoteIn
  218. REM_SCRIPT_OPTIONS="$REM_SCRIPT_OPTIONS $REMOTEPRODARGS"
  219. $ECHO_recog "\nExecuting remote Collection for $REMOTEINFO $REM_SCRIPT_OPTIONS..."
  220. recog_connect
  221. IFS=$'\n'
  222. ;;
  223. esac
  224. done
  225. IFS=$old_IFS # restore default field separator
  226. esac