lcmlog 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. read -p "Send the log via email? [default: y] " AUX
  257. while [ "$AUX" != "y" ] && [ "$AUX" != "n" ] && ! [ -z "$AUX" ]; do
  258. read -p "Yes (y) or no (n)? [default: y] " AUX
  259. done
  260. [ -z "$AUX" ] && AUX="y"
  261. if [ "$AUX" = "y" ]; then
  262. SEND_MAIL="MAIL"
  263. fi
  264. # Update of the pipe
  265. printf "%s\n" "POST" >> $pipe
  266. printf "%s\n" "${KIND}" >> $pipe
  267. printf "%s\n" "${DATE}" >> $pipe
  268. printf "%s\n" "${TAGS}" >> $pipe
  269. [ -z "$SEND_MAIL" ] || printf "%s\n" "${SEND_MAIL}" >> $pipe
  270. # Write log
  271. echo
  272. echo "Please write your log:"
  273. echo
  274. # cat - >> $pipe
  275. FILE_NAME=$(mktemp)
  276. $EDITOR $FILE_NAME
  277. cat $FILE_NAME >> $pipe
  278. rm $FILE_NAME
  279. }
  280. ###############
  281. # MAIN SCRIPT #
  282. ###############
  283. ## ENTRY POINT - INITIALIZATION
  284. # Check number of arguments
  285. if (( $# != 1 )); then
  286. usage
  287. exit 1
  288. fi
  289. # Check that the option is only one
  290. # --> -g OK
  291. # -gp NO
  292. if (( ${#1} != 2 )); then
  293. usage
  294. exit 1
  295. fi
  296. # Select method from the option
  297. while getopts ":gpuh" o; do
  298. case $o in
  299. g) METHOD="GET";;
  300. p) METHOD="POST";;
  301. u) METHOD="UPDATE";;
  302. h) method_help && exit 0;;
  303. #*) usage;;
  304. \?) usage && exit 1;;
  305. esac
  306. done
  307. # You must reset of the correct $OPTIND
  308. shift $(( $OPTIND -1 ))
  309. # Security control
  310. if [ -z "${METHOD}" ]; then usage; fi
  311. ## CHECK CONFIG FILE
  312. if ! [ -f ~/.config/lcmlog/config ]; then
  313. echo "Config file not found. The config file must be generated in order to use this program."
  314. echo "Do you want to generate it? (folder: ~/.config/lcmlog) [y|n]"
  315. read AUX
  316. while [ -z $AUX ]; do
  317. echo "Yes (y) or no (n)? "
  318. read AUX
  319. done
  320. while [ $AUX != "y" ] && [ $AUX != "n" ]; do
  321. echo "Yes (y) or no (n)? "
  322. read AUX
  323. while [ -z $AUX ]; do
  324. echo "Yes (y) or no (n)? "
  325. read AUX
  326. done
  327. done
  328. if [ $AUX == "y" ]; then
  329. mkdir -p ~/.config/lcmlog
  330. echo \
  331. "# lcmlog config file
  332. # Write this file as a ssh_config file.
  333. # For more info, run the command 'man ssh_config'.
  334. # Host is 'server': do not change the name.
  335. Host server
  336. HostName
  337. # User and Port parameters should be used only out of the lcm cluster.
  338. #User
  339. #Port" \
  340. > ~/.config/lcmlog/config
  341. echo "Done. Now you should edit the file and add HostName (and, in case, User and Port)."
  342. echo "Opening file with $EDITOR in 4 seconds..."
  343. sleep 4
  344. $EDITOR ~/.config/lcmlog/config
  345. echo
  346. else
  347. echo "Exit."
  348. exit 0
  349. fi
  350. fi
  351. ## APPLY METHOD
  352. # Temporary pipe to send input to the server
  353. pipe=$(mktemp)
  354. # Use the correct method
  355. # Method UPDATE
  356. if [ "$METHOD" == "UPDATE" ]; then
  357. echo "The UPDATE method is used to regenerate the internal server database."
  358. echo "It should only be called if a log file is added by hand,"
  359. echo "or if some of the log files get renamed or corrupted."
  360. echo "It assumes that all the log files are correctly formatted."
  361. echo
  362. echo "Are you sure? [y|n] "
  363. read ANSWER
  364. while [ -z $ANSWER ]; do
  365. printf "Yes (y) or no (n)? "
  366. read ANSWER
  367. done
  368. while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do
  369. printf "Yes (y) or no (n)? "
  370. read ANSWER
  371. while [ -z $ANSWER ]; do
  372. printf "Yes (y) or no (n)? "
  373. read ANSWER
  374. done
  375. done
  376. if [ $ANSWER == "n" ]; then exit 0; fi
  377. printf "UPDATE\n" >> $pipe
  378. # Method GET
  379. elif [ "$METHOD" == "GET" ]; then
  380. method_get $pipe
  381. # Method POST
  382. elif [ "$METHOD" == "POST" ]; then
  383. method_post $pipe
  384. fi
  385. # Remove accents
  386. pipe2=$(mktemp)
  387. 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
  388. # Call the server
  389. echo
  390. ssh -F ~/.config/lcmlog/config -s server lcmlog < $pipe2
  391. ### ENDING
  392. # Remove temporary pipe
  393. rm $pipe $pipe2
  394. # Check success or error
  395. RET=$?
  396. if (( $RET == 0 )); then
  397. echo "Operation $METHOD completed successfully."
  398. exit 0
  399. else
  400. printf "Server error: $RET\n" >&2
  401. exit $RET
  402. fi