lcmlog 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #!/bin/bash
  2. DIR="/var/local/log/lcmlog-data"
  3. 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. # TAGS
  60. echo
  61. echo "Search for which tags? (insert comma separated tags) (optional)"
  62. read TAGS
  63. # Update pipe
  64. printf "%s\n" "GET" >> $pipe
  65. printf "%s\n" "${KIND}" >> $pipe
  66. printf "%s\n" "${DATE}" >> $pipe
  67. printf "%s\n" "${TAGS}" >> $pipe
  68. printf "%s\n" "${USER}" >> $pipe
  69. }
  70. ######
  71. method_post()
  72. {
  73. pipe=$1
  74. # KIND
  75. AUX="n"
  76. while [ $AUX == n ]; do
  77. echo "Send an admin log [a] or 150 log [1]?"
  78. read CHAR
  79. while [ -z $CHAR ]; do
  80. echo "Retry, admin logs [a] or 150 logs [1]?"
  81. read CHAR
  82. done
  83. while [ $CHAR != "a" ] && [ $CHAR != "1" ]; do
  84. echo "Retry, admin logs [a] or 150 logs [1]?"
  85. read CHAR
  86. while [ -z $CHAR ]; do
  87. echo "Retry, admin logs [a] or 150 logs [1]?"
  88. read CHAR
  89. done
  90. done
  91. # Select the correct kind
  92. if [ $CHAR == "a" ]; then
  93. KIND="Admin"
  94. elif [ $CHAR == "1" ]; then
  95. KIND="150"
  96. fi
  97. echo "Confirm $KIND log? [y|n] "
  98. read AUX
  99. while [ -z $AUX ]; do
  100. echo "Yes (y) or no (n)? "
  101. read AUX
  102. done
  103. while [ $AUX != "y" ] && [ $AUX != "n" ]; do
  104. echo "Yes (y) or no (n)? "
  105. read AUX
  106. while [ -z $AUX ]; do
  107. echo "Yes (y) or no (n)? "
  108. read AUX
  109. done
  110. done
  111. done
  112. # DATE
  113. echo
  114. echo "Insert the date."
  115. # Year
  116. echo "Year? (yyyy)"
  117. read YEAR
  118. re='^[0-9]+$'
  119. while ! [[ $YEAR =~ $re ]]; do
  120. echo "Wrong, not a number: reinsert."
  121. read YEAR
  122. done
  123. while (( ${#YEAR} != 4 )); do
  124. echo "Wrong year, reinsert."
  125. read YEAR
  126. while ! [[ $YEAR =~ $re ]]; do
  127. echo "Wrong, not a number: reinsert."
  128. read YEAR
  129. done
  130. done
  131. # Month
  132. echo "Month? (mm)"
  133. read MONTH
  134. re='^[0-9]+$'
  135. while ! [[ $MONTH =~ $re ]]; do
  136. echo "Wrong, not a number: reinsert."
  137. read MONTH
  138. done
  139. while (( $MONTH < 1 )) || (( $MONTH > 12 )); do
  140. echo "Wrong month, reinsert."
  141. read MONTH
  142. while ! [[ $MONTH =~ $re ]]; do
  143. echo "Wrong, not a number: reinsert."
  144. read MONTH
  145. done
  146. done
  147. if (( ${#MONTH} == 1 )); then
  148. MONTH=0$MONTH
  149. fi
  150. # Day
  151. echo "Day? (dd)"
  152. if (( $MONTH == 2 )); then
  153. echo "Be careful about the day! There is no check about leap years."
  154. fi
  155. read DAY
  156. re='^[0-9]+$'
  157. while ! [[ $DAY =~ $re ]]; do
  158. echo "Wrong, not a number: reinsert."
  159. read DAY
  160. done
  161. # Month with 31 day
  162. if (( $MONTH == 1 )) || (( $MONTH == 3 )) || (( $MONTH == 5 )) || (( $MONTH == 7 )) || (( $MONTH == 8 )) || (( $MONTH == 10 )) || (( $MONTH == 12 )); then
  163. while (( $DAY < 1 )) || (( $DAY > 31 )); do
  164. echo "Wrong day, reinsert."
  165. read DAY
  166. while ! [[ $DAY =~ $re ]]; do
  167. echo "Wrong, not a number: reinsert."
  168. read DAY
  169. done
  170. done
  171. # Month with 30 day
  172. elif (( $MONTH == 4 )) || (( $MONTH == 6 )) || (( $MONTH == 9 )) || (( $MONTH == 11 )); then
  173. while (( $DAY < 1 )) || (( $DAY > 30 )); do
  174. echo "Wrong day, reinsert."
  175. read DAY
  176. while ! [[ $DAY =~ $re ]]; do
  177. echo "Wrong, not a number: reinsert."
  178. read DAY
  179. done
  180. done
  181. # Month with 29 day
  182. elif (( $MONTH == 2 )); then
  183. while (( $DAY < 1 )) || (( $DAY > 29 )); do
  184. echo "Wrong day, reinsert."
  185. read DAY
  186. while ! [[ $DAY =~ $re ]]; do
  187. echo "Wrong, not a number: reinsert."
  188. read DAY
  189. done
  190. done
  191. fi
  192. if (( ${#DAY} == 1 )); then
  193. DAY=0$DAY
  194. fi
  195. # Morning or afternoon shift
  196. if [ $KIND == "150" ]; then
  197. echo "Morning [m] or afternoon [p] shift?"
  198. read CHAR
  199. while [ -z $CHAR ]; do
  200. echo "Retry, morning [m] or afternoon [p] shift?"
  201. read CHAR
  202. done
  203. while [ $CHAR != "m" ] && [ $CHAR != "p" ]; do
  204. echo "Retry, morning [m] or afternoon [p] shift?"
  205. read CHAR
  206. while [ -z $CHAR ]; do
  207. echo "Retry, morning [m] or afternoon [p] shift?"
  208. read CHAR
  209. done
  210. done
  211. # Select the correct kind
  212. if [ $CHAR == "m" ]; then
  213. MP="mat"
  214. elif [ $CHAR == "p" ]; then
  215. MP="pom"
  216. fi
  217. DATE=$YEAR"-"$MONTH"-"$DAY"_"$MP
  218. else
  219. DATE=$YEAR"-"$MONTH"-"$DAY
  220. fi
  221. # TAGS
  222. echo
  223. echo "Tags? (insert comma separated tags)"
  224. read TAGS
  225. while [ -z $TAGS ]; do
  226. echo "Insert tags."
  227. read TAGS
  228. done
  229. # Update of the pipe
  230. printf "%s\n" "POST" >> $pipe
  231. printf "%s\n" "${KIND}" >> $pipe
  232. printf "%s\n" "${DATE}" >> $pipe
  233. printf "%s\n" "${TAGS}" >> $pipe
  234. # Write log
  235. echo
  236. echo "Please write your log:"
  237. echo
  238. # cat - >> $pipe
  239. FILE_NAME=$(mktemp)
  240. $EDITOR $FILE_NAME
  241. cat $FILE_NAME >> $pipe
  242. rm $FILE_NAME
  243. }
  244. ###############
  245. # MAIN SCRIPT #
  246. ###############
  247. ## ENTRY POINT - INITIALIZATION
  248. # Check number of arguments
  249. if (( $# != 1 )); then
  250. usage
  251. exit 1
  252. fi
  253. # Check that the option is only one
  254. # --> -g OK
  255. # -gp NO
  256. if (( ${#1} != 2 )); then
  257. usage
  258. exit 1
  259. fi
  260. # Select method from the option
  261. while getopts ":gpuh" o; do
  262. case $o in
  263. g) METHOD="GET";;
  264. p) METHOD="POST";;
  265. u) METHOD="UPDATE";;
  266. h) method_help && exit 0;;
  267. #*) usage;;
  268. \?) usage && exit 1;;
  269. esac
  270. done
  271. # You must reset of the correct $OPTIND
  272. shift $(( $OPTIND -1 ))
  273. # Security control
  274. if [ -z "${METHOD}" ]; then usage; fi
  275. ## CHECK CONFIG FILE
  276. if ! [ -f ~/.config/lcmlog/config ]; then
  277. echo "Config file not found. The config file must be generated in order to use this program."
  278. echo "Do you want to generate it? (folder: ~/.config/lcmlog) [y|n]"
  279. read AUX
  280. while [ -z $AUX ]; do
  281. echo "Yes (y) or no (n)? "
  282. read AUX
  283. done
  284. while [ $AUX != "y" ] && [ $AUX != "n" ]; do
  285. echo "Yes (y) or no (n)? "
  286. read AUX
  287. while [ -z $AUX ]; do
  288. echo "Yes (y) or no (n)? "
  289. read AUX
  290. done
  291. done
  292. if [ $AUX == "y" ]; then
  293. mkdir -p ~/.config/lcmlog
  294. echo \
  295. "# lcmlog config file
  296. # Write this file as a ssh_config file.
  297. # For more info, run the command 'man ssh_config'.
  298. # Host is 'server': do not change the name.
  299. Host server
  300. HostName
  301. # User and Port parameters should be used only out of the lcm cluster.
  302. #User
  303. #Port" \
  304. > ~/.config/lcmlog/config
  305. echo "Done. Now you should edit the file and add HostName (and, in case, User and Port)."
  306. echo "Opening file with $EDITOR in 4 seconds..."
  307. sleep 4
  308. $EDITOR ~/.config/lcmlog/config
  309. echo
  310. else
  311. echo "Exit."
  312. exit 0
  313. fi
  314. fi
  315. ## APPLY METHOD
  316. # Temporary pipe to send input to the server
  317. pipe=$(mktemp)
  318. # Use the correct method
  319. # Method UPDATE
  320. if [ "$METHOD" == "UPDATE" ]; then
  321. echo "The UPDATE method is used to regenerate the internal server database."
  322. echo "It should only be called if a log file is added by hand,"
  323. echo "or if some of the log files get renamed or corrupted."
  324. echo "It assumes that all the log files are correctly formatted."
  325. echo
  326. echo "Are you shure? [y|n] "
  327. read ANSWER
  328. while [ -z $ANSWER ]; do
  329. printf "Yes (y) or no (n)? "
  330. read ANSWER
  331. done
  332. while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do
  333. printf "Yes (y) or no (n)? "
  334. read ANSWER
  335. while [ -z $ANSWER ]; do
  336. printf "Yes (y) or no (n)? "
  337. read ANSWER
  338. done
  339. done
  340. if [ $ANSWER == "n" ]; then exit 0; fi
  341. printf "UPDATE\n" >> $pipe
  342. # Method GET
  343. elif [ "$METHOD" == "GET" ]; then
  344. method_get $pipe
  345. # Method POST
  346. elif [ "$METHOD" == "POST" ]; then
  347. method_post $pipe
  348. fi
  349. # Remove accents
  350. pipe2=$(mktemp)
  351. 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
  352. # Call the server
  353. echo
  354. ssh -F ~/.config/lcmlog/config -s server lcmlog < $pipe2
  355. ### ENDING
  356. # Remove temporary pipe
  357. rm $pipe $pipe2
  358. # Check success or error
  359. RET=$?
  360. if (( $RET == 0 )); then
  361. echo "Operation $METHOD completed successfully."
  362. exit 0
  363. else
  364. printf "Server error: $RET\n" >&2
  365. exit $RET
  366. fi