lcmlog 8.0 KB

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