lcmlog 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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?"
  54. read USER
  55. # DATE
  56. echo
  57. echo "Search for which date? (yyyy-mm-dd)"
  58. read DATE
  59. # TAGS
  60. echo
  61. echo "Search for which tags? (insert comma separated tags)"
  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 30 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. echo "Morning [m] or afternoon [p] shift?"
  197. read CHAR
  198. while [ -z $CHAR ]; do
  199. echo "Retry, morning [m] or afternoon [p] shift?"
  200. read CHAR
  201. done
  202. while [ $CHAR != "m" ] && [ $CHAR != "p" ]; do
  203. echo "Retry, morning [m] or afternoon [p] shift?"
  204. read CHAR
  205. while [ -z $CHAR ]; do
  206. echo "Retry, morning [m] or afternoon [p] shift?"
  207. read CHAR
  208. done
  209. done
  210. # Select the correct kind
  211. if [ $CHAR == "m" ]; then
  212. MP="mat"
  213. elif [ $CHAR == "p" ]; then
  214. MP="pom"
  215. fi
  216. # Finalization
  217. DATE=$YEAR"-"$MONTH"-"$DAY"_"$MP
  218. # TAGS
  219. echo
  220. echo "Tags? (insert comma separated tags)"
  221. read TAGS
  222. while [ -z $TAGS ]; do
  223. echo "Insert tags."
  224. read TAGS
  225. done
  226. # Update of the pipe
  227. printf "%s\n" "POST" >> $pipe
  228. printf "%s\n" "${KIND}" >> $pipe
  229. printf "%s\n" "${DATE}" >> $pipe
  230. printf "%s\n" "${TAGS}" >> $pipe
  231. # Write log
  232. echo
  233. echo "Please write your log:"
  234. echo
  235. FILE_NAME=$(mktemp)
  236. $EDITOR $FILE_NAME
  237. cat $FILE_NAME >> $pipe
  238. }
  239. ###############
  240. # MAIN SCRIPT #
  241. ###############
  242. ## ENTRY POINT - INITIALIZATION
  243. # Check number of arguments
  244. if (( $# != 1 )); then
  245. usage
  246. exit 1
  247. fi
  248. # Check that the option is only one
  249. # --> -g OK
  250. # -gp NO
  251. if (( ${#1} != 2 )); then
  252. usage
  253. exit 1
  254. fi
  255. # Select method from the option
  256. while getopts ":gpuh" o; do
  257. case $o in
  258. g) METHOD="GET";;
  259. p) METHOD="POST";;
  260. u) METHOD="UPDATE";;
  261. h) method_help && exit 0;;
  262. #*) usage;;
  263. \?) usage && exit 1;;
  264. esac
  265. done
  266. # You must reset of the correct $OPTIND
  267. shift $(( $OPTIND -1 ))
  268. # Security control
  269. if [ -z "${METHOD}" ]; then usage; fi
  270. ## CHECK CONFIG FILE
  271. if ! [ -f ~/.config/lcmlog/config ]; then
  272. echo "Config file not found. The config file must be generated in order to use this program."
  273. echo "Do you want to generate it? (folder: ~/.config/lcmlog) [y|n]"
  274. read AUX
  275. while [ -z $AUX ]; do
  276. echo "Yes (y) or no (n)? "
  277. read AUX
  278. done
  279. while [ $AUX != "y" ] && [ $AUX != "n" ]; do
  280. echo "Yes (y) or no (n)? "
  281. read AUX
  282. while [ -z $AUX ]; do
  283. echo "Yes (y) or no (n)? "
  284. read AUX
  285. done
  286. done
  287. if [ $AUX == "y" ]; then
  288. mkdir -p ~/.config/lcmlog
  289. echo \
  290. "# lcmlog config file
  291. # Write this file as a ssh_config file.
  292. # For more info, run the command 'man ssh_config'.
  293. # Host is 'server': do not change the name.
  294. Host server
  295. HostName
  296. # User and Port parameters should be used only out of the lcm cluster.
  297. #User
  298. #Port" \
  299. > ~/.config/lcmlog/config
  300. echo "Done. Now you should edit the file and add HostName (and eventually User and Port)."
  301. sleep 4
  302. nano ~/.config/lcmlog/config
  303. echo
  304. else
  305. echo "Exit."
  306. exit 0
  307. fi
  308. fi
  309. ## APPLY METHOD
  310. # Temporary pipe to send input to the server
  311. pipe=$(mktemp)
  312. # Use the correct method
  313. # Method UPDATE
  314. if [ "$METHOD" == "UPDATE" ]; then
  315. echo "The UPDATE method is used to regenerate the internal server database."
  316. echo "It should only be called if a log file is added by hand,"
  317. echo "or if some of the log files get renamed or corrupted."
  318. echo "It assumes that all the log files are correctly formatted."
  319. echo
  320. echo "Are you shure? [y|n] "
  321. read ANSWER
  322. while [ -z $ANSWER ]; do
  323. printf "Yes (y) or no (n)? "
  324. read ANSWER
  325. done
  326. while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do
  327. printf "Yes (y) or no (n)? "
  328. read ANSWER
  329. while [ -z $ANSWER ]; do
  330. printf "Yes (y) or no (n)? "
  331. read ANSWER
  332. done
  333. done
  334. if [ $ANSWER == "n" ]; then exit 0; fi
  335. printf "UPDATE\n" >> $pipe
  336. # Method GET
  337. elif [ "$METHOD" == "GET" ]; then
  338. method_get $pipe
  339. # Method POST
  340. elif [ "$METHOD" == "POST" ]; then
  341. method_post $pipe
  342. fi
  343. # Call the server
  344. echo
  345. ssh -F ~/.config/lcmlog/config -s server lcmlog < $pipe
  346. ### ENDING
  347. # Remove temporary pipe
  348. rm $pipe
  349. # Check success or error
  350. RET=$?
  351. if (( $RET == 0 )); then
  352. echo "Operation $METHOD completed successfully."
  353. exit 0
  354. else
  355. printf "Server error: $RET\n" >&2
  356. exit $RET
  357. fi