lcmlog 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. #!/bin/bash
  2. DIR="/var/local/log/lcmlog-data"
  3. EDITOR="${EDITOR:-nano}"
  4. ######################
  5. # AUXILIARY FUNCTION #
  6. ######################
  7. usage()
  8. {
  9. echo "Usage: $0 [-g|-p|-u]"
  10. echo "Use -h option to show the help message."
  11. }
  12. ######
  13. method_help()
  14. {
  15. echo "lcmlog: client side of a tool for reading and writing the lcm staff logs."
  16. echo
  17. echo "Usage: $0 [option]"
  18. echo
  19. echo "Options:"
  20. echo " -g Method GET: print all the logs that meet the required criteria"
  21. echo " -p Method POST: add a log to the database"
  22. echo " -u Method UPDATE: regenerate the internal server database"
  23. echo " -h Show this help"
  24. }
  25. ######
  26. method_get()
  27. {
  28. pipe=$1
  29. # KIND
  30. # The user must say if he's looking for 150 or Admin logs
  31. echo "Search for admin logs [a] or 150 logs [1]?"
  32. read CHAR
  33. while [ -z $CHAR ]; do
  34. echo "Retry, admin logs [a] or 150 logs [1]?"
  35. read CHAR
  36. done
  37. while [ $CHAR != "a" ] && [ $CHAR != "1" ]; do
  38. echo "Retry, admin logs [a] or 150 logs [1]?"
  39. read CHAR
  40. while [ -z $CHAR ]; do
  41. echo "Retry, admin logs [a] or 150 logs [1]?"
  42. read CHAR
  43. done
  44. done
  45. # Select the correct kind
  46. if [ $CHAR == "a" ]; then
  47. KIND="Admin"
  48. elif [ $CHAR == "1" ]; then
  49. KIND="150"
  50. fi
  51. # USER
  52. echo
  53. echo "Search for which user? (optional)"
  54. read USER
  55. # DATE
  56. echo
  57. echo "Search for which date? (yyyy-mm-dd) (optional)"
  58. read DATE
  59. while ! [ -z $DATE ]; do
  60. if [ ${DATE:4:1} != '-' ] || [ ${DATE:7:1} != '-' ] || (( ${#DATE} != 10 )); then
  61. echo "Format: yyyy-mm-dd (i.e. 9 is 09). Reinsert."
  62. read DATE
  63. else
  64. break
  65. fi
  66. done
  67. # TAGS
  68. echo
  69. echo "Search for which tags? (insert comma separated tags) (optional)"
  70. read TAGS
  71. # Update pipe
  72. printf "%s\n" "GET" >> $pipe
  73. printf "%s\n" "${KIND}" >> $pipe
  74. printf "%s\n" "${DATE}" >> $pipe
  75. printf "%s\n" "${TAGS}" >> $pipe
  76. printf "%s\n" "${USER}" >> $pipe
  77. }
  78. ######
  79. method_post()
  80. {
  81. pipe=$1
  82. # KIND
  83. AUX="n"
  84. while [ $AUX == n ]; do
  85. echo "Send an admin log [a] or 150 log [1]?"
  86. read CHAR
  87. while [ -z $CHAR ]; do
  88. echo "Retry, admin logs [a] or 150 logs [1]?"
  89. read CHAR
  90. done
  91. while [ $CHAR != "a" ] && [ $CHAR != "1" ]; do
  92. echo "Retry, admin logs [a] or 150 logs [1]?"
  93. read CHAR
  94. while [ -z $CHAR ]; do
  95. echo "Retry, admin logs [a] or 150 logs [1]?"
  96. read CHAR
  97. done
  98. done
  99. # Select the correct kind
  100. if [ $CHAR == "a" ]; then
  101. KIND="Admin"
  102. elif [ $CHAR == "1" ]; then
  103. KIND="150"
  104. fi
  105. echo "Confirm $KIND log? [y|n] "
  106. read AUX
  107. while [ -z $AUX ]; do
  108. echo "Yes (y) or no (n)? "
  109. read AUX
  110. done
  111. while [ $AUX != "y" ] && [ $AUX != "n" ]; do
  112. echo "Yes (y) or no (n)? "
  113. read AUX
  114. while [ -z $AUX ]; do
  115. echo "Yes (y) or no (n)? "
  116. read AUX
  117. done
  118. done
  119. done
  120. # DATE
  121. echo
  122. echo "Insert the date."
  123. # Year
  124. echo "Year? (yyyy)"
  125. read YEAR
  126. YEAR=$((10#$YEAR))
  127. re='^[0-9]+$'
  128. while ! [[ $YEAR =~ $re ]]; do
  129. echo "Wrong, not a number: reinsert."
  130. read YEAR
  131. YEAR=$((10#$YEAR))
  132. done
  133. while (( ${#YEAR} != 4 )); do
  134. echo "Wrong year, reinsert."
  135. read YEAR
  136. YEAR=$((10#$YEAR))
  137. while ! [[ $YEAR =~ $re ]]; do
  138. echo "Wrong, not a number: reinsert."
  139. read YEAR
  140. YEAR=$((10#$YEAR))
  141. done
  142. done
  143. # Month
  144. echo "Month? (mm)"
  145. read MONTH
  146. MONTH=$((10#$MONTH))
  147. re='^[0-9]+$'
  148. while ! [[ $MONTH =~ $re ]]; do
  149. echo "Wrong, not a number: reinsert."
  150. read MONTH
  151. MONTH=$((10#$MONTH))
  152. done
  153. while (( $MONTH < 1 )) || (( $MONTH > 12 )); do
  154. echo "Wrong month, reinsert."
  155. read MONTH
  156. MONTH=$((10#$MONTH))
  157. while ! [[ $MONTH =~ $re ]]; do
  158. echo "Wrong, not a number: reinsert."
  159. read MONTH
  160. MONTH=$((10#$MONTH))
  161. done
  162. done
  163. # Day
  164. echo "Day? (dd)"
  165. if (( $MONTH == 2 )); then
  166. echo "Be careful about the day! There is no check about leap years."
  167. fi
  168. read DAY
  169. DAY=$((10#$DAY))
  170. re='^[0-9]+$'
  171. while ! [[ $DAY =~ $re ]]; do
  172. echo "Wrong, not a number: reinsert."
  173. read DAY
  174. DAY=$((10#$DAY))
  175. done
  176. # Month with 31 day
  177. if (( $MONTH == 1 )) || (( $MONTH == 3 )) || (( $MONTH == 5 )) || (( $MONTH == 7 )) || (( $MONTH == 8 )) || (( $MONTH == 10 )) || (( $MONTH == 12 )); then
  178. while (( $DAY < 1 )) || (( $DAY > 31 )); do
  179. echo "Wrong day, reinsert."
  180. read DAY
  181. DAY=$((10#$DAY))
  182. while ! [[ $DAY =~ $re ]]; do
  183. echo "Wrong, not a number: reinsert."
  184. read DAY
  185. DAY=$((10#$DAY))
  186. done
  187. done
  188. # Month with 30 day
  189. elif (( $MONTH == 4 )) || (( $MONTH == 6 )) || (( $MONTH == 9 )) || (( $MONTH == 11 )); then
  190. while (( $DAY < 1 )) || (( $DAY > 30 )); do
  191. echo "Wrong day, reinsert."
  192. read DAY
  193. DAY=$((10#$DAY))
  194. while ! [[ $DAY =~ $re ]]; do
  195. echo "Wrong, not a number: reinsert."
  196. read DAY
  197. DAY=$((10#$DAY))
  198. done
  199. done
  200. # Month with 29 day
  201. elif (( $MONTH == 2 )); then
  202. while (( $DAY < 1 )) || (( $DAY > 29 )); do
  203. echo "Wrong day, reinsert."
  204. read DAY
  205. DAY=$((10#$DAY))
  206. while ! [[ $DAY =~ $re ]]; do
  207. echo "Wrong, not a number: reinsert."
  208. read DAY
  209. DAY=$((10#$DAY))
  210. done
  211. done
  212. fi
  213. ## Turn month and day in correctly strings
  214. if (( ${#MONTH} == 1 )); then
  215. MONTH=0$MONTH
  216. fi
  217. if (( ${#DAY} == 1 )); then
  218. DAY="0$DAY"
  219. #DAY=$((10#$DAY))
  220. fi
  221. # Morning or afternoon shift
  222. if [ $KIND == "150" ]; then
  223. echo "Morning [m] or afternoon [p] shift?"
  224. read CHAR
  225. while [ -z $CHAR ]; do
  226. echo "Retry, morning [m] or afternoon [p] shift?"
  227. read CHAR
  228. done
  229. while [ $CHAR != "m" ] && [ $CHAR != "p" ]; do
  230. echo "Retry, morning [m] or afternoon [p] shift?"
  231. read CHAR
  232. while [ -z $CHAR ]; do
  233. echo "Retry, morning [m] or afternoon [p] shift?"
  234. read CHAR
  235. done
  236. done
  237. # Select the correct kind
  238. if [ $CHAR == "m" ]; then
  239. MP="mat"
  240. elif [ $CHAR == "p" ]; then
  241. MP="pom"
  242. fi
  243. DATE=$YEAR"-"$MONTH"-"$DAY"_"$MP
  244. else
  245. DATE=$YEAR"-"$MONTH"-"$DAY
  246. fi
  247. # TAGS
  248. echo
  249. echo "Tags? (insert comma separated tags)"
  250. read TAGS
  251. while [ -z $TAGS ]; do
  252. echo "Insert tags."
  253. read TAGS
  254. done
  255. SEND_MAIL=""
  256. echo
  257. read -p "Send the log via email? [default: y] " AUX
  258. while [ "$AUX" != "y" ] && [ "$AUX" != "n" ] && ! [ -z "$AUX" ]; do
  259. read -p "Yes (y) or no (n)? [default: y] " AUX
  260. done
  261. [ -z "$AUX" ] && AUX="y"
  262. if [ "$AUX" = "y" ]; then
  263. SEND_MAIL="MAIL"
  264. fi
  265. # Update of the pipe
  266. printf "%s\n" "POST" >> $pipe
  267. printf "%s\n" "${KIND}" >> $pipe
  268. printf "%s\n" "${DATE}" >> $pipe
  269. printf "%s\n" "${TAGS}" >> $pipe
  270. [ -z "$SEND_MAIL" ] || printf "%s\n" "${SEND_MAIL}" >> $pipe
  271. # Write log
  272. FILE_NAME=$(mktemp)
  273. echo
  274. read -p "Upload the log via file? [default: n] " AUX
  275. while [ "$AUX" != "y" ] && [ "$AUX" != "n" ] && ! [ -z "$AUX" ]; do
  276. read -p "Yes (y) or no (n)? [default: n] " AUX
  277. done
  278. [ -z "$AUX" ] && AUX="n"
  279. if [ "$AUX" = "n" ];
  280. then
  281. echo
  282. echo "Please write your log:"
  283. echo
  284. $EDITOR $FILE_NAME
  285. cat $FILE_NAME >> $pipe
  286. rm $FILE_NAME
  287. else
  288. echo
  289. # echo "Please insert your log file path:"
  290. read -e -p "Please insert your log file path: " FILE
  291. while ! [ -z "$FILE" ] && ! [ -f "$FILE" ]; do
  292. # echo "Please insert a valid path:"
  293. read -e -p "Please insert a valid path: " FILE
  294. done
  295. echo
  296. cat $FILE >> $pipe
  297. fi
  298. }
  299. ###############
  300. # MAIN SCRIPT #
  301. ###############
  302. ## ENTRY POINT - INITIALIZATION
  303. # Check number of arguments
  304. if (( $# != 1 )); then
  305. usage
  306. exit 1
  307. fi
  308. # Check that the option is only one
  309. # --> -g OK
  310. # -gp NO
  311. if (( ${#1} != 2 )); then
  312. usage
  313. exit 1
  314. fi
  315. # Select method from the option
  316. while getopts ":gpuh" o; do
  317. case $o in
  318. g) METHOD="GET";;
  319. p) METHOD="POST";;
  320. u) METHOD="UPDATE";;
  321. h) method_help && exit 0;;
  322. #*) usage;;
  323. \?) usage && exit 1;;
  324. esac
  325. done
  326. # You must reset of the correct $OPTIND
  327. shift $(( $OPTIND -1 ))
  328. # Security control
  329. if [ -z "${METHOD}" ]; then usage; fi
  330. ## CHECK CONFIG FILE
  331. if ! [ -f ~/.config/lcmlog/config ]; then
  332. echo "Config file not found. The config file must be generated in order to use this program."
  333. echo "Do you want to generate it? (folder: ~/.config/lcmlog) [y|n]"
  334. read AUX
  335. while [ -z $AUX ]; do
  336. echo "Yes (y) or no (n)? "
  337. read AUX
  338. done
  339. while [ $AUX != "y" ] && [ $AUX != "n" ]; do
  340. echo "Yes (y) or no (n)? "
  341. read AUX
  342. while [ -z $AUX ]; do
  343. echo "Yes (y) or no (n)? "
  344. read AUX
  345. done
  346. done
  347. if [ $AUX == "y" ]; then
  348. mkdir -p ~/.config/lcmlog
  349. echo \
  350. "# lcmlog config file
  351. # Write this file as a ssh_config file.
  352. # For more info, run the command 'man ssh_config'.
  353. # Host is 'server': do not change the name.
  354. Host server
  355. HostName
  356. # User and Port parameters should be used only out of the lcm cluster.
  357. #User
  358. #Port" \
  359. > ~/.config/lcmlog/config
  360. echo "Done. Now you should edit the file and add HostName (and, in case, User and Port)."
  361. echo "Opening file with $EDITOR in 4 seconds..."
  362. sleep 4
  363. $EDITOR ~/.config/lcmlog/config
  364. echo
  365. else
  366. echo "Exit."
  367. exit 0
  368. fi
  369. fi
  370. ## APPLY METHOD
  371. # Temporary pipe to send input to the server
  372. pipe=$(mktemp)
  373. # Use the correct method
  374. # Method UPDATE
  375. if [ "$METHOD" == "UPDATE" ]; then
  376. echo "The UPDATE method is used to regenerate the internal server database."
  377. echo "It should only be called if a log file is added by hand,"
  378. echo "or if some of the log files get renamed or corrupted."
  379. echo "It assumes that all the log files are correctly formatted."
  380. echo
  381. echo "Are you sure? [y|n] "
  382. read ANSWER
  383. while [ -z $ANSWER ]; do
  384. printf "Yes (y) or no (n)? "
  385. read ANSWER
  386. done
  387. while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do
  388. printf "Yes (y) or no (n)? "
  389. read ANSWER
  390. while [ -z $ANSWER ]; do
  391. printf "Yes (y) or no (n)? "
  392. read ANSWER
  393. done
  394. done
  395. if [ $ANSWER == "n" ]; then exit 0; fi
  396. printf "UPDATE\n" >> $pipe
  397. # Method GET
  398. elif [ "$METHOD" == "GET" ]; then
  399. method_get $pipe
  400. # Method POST
  401. elif [ "$METHOD" == "POST" ]; then
  402. method_post $pipe
  403. fi
  404. # Remove accents
  405. pipe2=$(mktemp)
  406. sed -e 's/à/a`/g' -e 's/è/e`/g' -e "s/é/e'/g" -e 's/ì/i`/g' -e 's/ò/o`/g' -e 's/ù/u`/g' -e 's/È/E`/g' < $pipe > $pipe2
  407. # Call the server
  408. echo
  409. ssh -F ~/.config/lcmlog/config -s server lcmlog < $pipe2
  410. RET=$?
  411. ### ENDING
  412. # Remove temporary pipe
  413. rm $pipe $pipe2
  414. # Check success or error
  415. if (( $RET == 0 )); then
  416. echo "Operation $METHOD completed successfully."
  417. exit 0
  418. else
  419. printf "Server error: $RET\n" >&2
  420. exit $RET
  421. fi