lcmlog 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. 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. FILE_NAME=$(mktemp)
  239. $EDITOR $FILE_NAME
  240. cat $FILE_NAME >> $pipe
  241. }
  242. ###############
  243. # MAIN SCRIPT #
  244. ###############
  245. ## ENTRY POINT - INITIALIZATION
  246. # Check number of arguments
  247. if (( $# != 1 )); then
  248. usage
  249. exit 1
  250. fi
  251. # Check that the option is only one
  252. # --> -g OK
  253. # -gp NO
  254. if (( ${#1} != 2 )); then
  255. usage
  256. exit 1
  257. fi
  258. # Select method from the option
  259. while getopts ":gpuh" o; do
  260. case $o in
  261. g) METHOD="GET";;
  262. p) METHOD="POST";;
  263. u) METHOD="UPDATE";;
  264. h) method_help && exit 0;;
  265. #*) usage;;
  266. \?) usage && exit 1;;
  267. esac
  268. done
  269. # You must reset of the correct $OPTIND
  270. shift $(( $OPTIND -1 ))
  271. # Security control
  272. if [ -z "${METHOD}" ]; then usage; fi
  273. ## CHECK CONFIG FILE
  274. if ! [ -f ~/.config/lcmlog/config ]; then
  275. echo "Config file not found. The config file must be generated in order to use this program."
  276. echo "Do you want to generate it? (folder: ~/.config/lcmlog) [y|n]"
  277. read AUX
  278. while [ -z $AUX ]; do
  279. echo "Yes (y) or no (n)? "
  280. read AUX
  281. done
  282. while [ $AUX != "y" ] && [ $AUX != "n" ]; do
  283. echo "Yes (y) or no (n)? "
  284. read AUX
  285. while [ -z $AUX ]; do
  286. echo "Yes (y) or no (n)? "
  287. read AUX
  288. done
  289. done
  290. if [ $AUX == "y" ]; then
  291. mkdir -p ~/.config/lcmlog
  292. echo \
  293. "# lcmlog config file
  294. # Write this file as a ssh_config file.
  295. # For more info, run the command 'man ssh_config'.
  296. # Host is 'server': do not change the name.
  297. Host server
  298. HostName
  299. # User and Port parameters should be used only out of the lcm cluster.
  300. #User
  301. #Port" \
  302. > ~/.config/lcmlog/config
  303. echo "Done. Now you should edit the file and add HostName (and eventually User and Port)."
  304. echo "Opening file with $EDITOR in 4 seconds..."
  305. sleep 4
  306. $EDITOR ~/.config/lcmlog/config
  307. echo
  308. else
  309. echo "Exit."
  310. exit 0
  311. fi
  312. fi
  313. ## APPLY METHOD
  314. # Temporary pipe to send input to the server
  315. pipe=$(mktemp)
  316. # Use the correct method
  317. # Method UPDATE
  318. if [ "$METHOD" == "UPDATE" ]; then
  319. echo "The UPDATE method is used to regenerate the internal server database."
  320. echo "It should only be called if a log file is added by hand,"
  321. echo "or if some of the log files get renamed or corrupted."
  322. echo "It assumes that all the log files are correctly formatted."
  323. echo
  324. echo "Are you shure? [y|n] "
  325. read ANSWER
  326. while [ -z $ANSWER ]; do
  327. printf "Yes (y) or no (n)? "
  328. read ANSWER
  329. done
  330. while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do
  331. printf "Yes (y) or no (n)? "
  332. read ANSWER
  333. while [ -z $ANSWER ]; do
  334. printf "Yes (y) or no (n)? "
  335. read ANSWER
  336. done
  337. done
  338. if [ $ANSWER == "n" ]; then exit 0; fi
  339. printf "UPDATE\n" >> $pipe
  340. # Method GET
  341. elif [ "$METHOD" == "GET" ]; then
  342. method_get $pipe
  343. # Method POST
  344. elif [ "$METHOD" == "POST" ]; then
  345. method_post $pipe
  346. fi
  347. # Call the server
  348. echo
  349. ssh -F ~/.config/lcmlog/config -s server lcmlog < $pipe
  350. ### ENDING
  351. # Remove temporary pipe
  352. rm $pipe
  353. # Check success or error
  354. RET=$?
  355. if (( $RET == 0 )); then
  356. echo "Operation $METHOD completed successfully."
  357. exit 0
  358. else
  359. printf "Server error: $RET\n" >&2
  360. exit $RET
  361. fi