lcmlog 8.3 KB

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