lcmlog 9.0 KB

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