lcmlog 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #!/bin/bash
  2. DIR="/var/local/log/lcmlog-data"
  3. EDITOR="${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. SEND_MAIL="NOMAIL"
  256. ask "Send the log via email? [default: y]" "y" && SEND_MAIL="MAIL"
  257. # Update of the pipe
  258. printf "%s\n" "POST" >> $pipe
  259. printf "%s\n" "${KIND}" >> $pipe
  260. printf "%s\n" "${DATE}" >> $pipe
  261. printf "%s\n" "${TAGS}" >> $pipe
  262. printf "%s\n" "${SEND_MAIL}" >> $pipe
  263. # Write log
  264. echo
  265. echo "Please write your log:"
  266. echo
  267. # cat - >> $pipe
  268. FILE_NAME=$(mktemp)
  269. $EDITOR $FILE_NAME
  270. cat $FILE_NAME >> $pipe
  271. rm $FILE_NAME
  272. }
  273. ###############
  274. # MAIN SCRIPT #
  275. ###############
  276. ## ENTRY POINT - INITIALIZATION
  277. # Check number of arguments
  278. if (( $# != 1 )); then
  279. usage
  280. exit 1
  281. fi
  282. # Check that the option is only one
  283. # --> -g OK
  284. # -gp NO
  285. if (( ${#1} != 2 )); then
  286. usage
  287. exit 1
  288. fi
  289. # Select method from the option
  290. while getopts ":gpuh" o; do
  291. case $o in
  292. g) METHOD="GET";;
  293. p) METHOD="POST";;
  294. u) METHOD="UPDATE";;
  295. h) method_help && exit 0;;
  296. #*) usage;;
  297. \?) usage && exit 1;;
  298. esac
  299. done
  300. # You must reset of the correct $OPTIND
  301. shift $(( $OPTIND -1 ))
  302. # Security control
  303. if [ -z "${METHOD}" ]; then usage; fi
  304. ## CHECK CONFIG FILE
  305. if ! [ -f ~/.config/lcmlog/config ]; then
  306. echo "Config file not found. The config file must be generated in order to use this program."
  307. echo "Do you want to generate it? (folder: ~/.config/lcmlog) [y|n]"
  308. read AUX
  309. while [ -z $AUX ]; do
  310. echo "Yes (y) or no (n)? "
  311. read AUX
  312. done
  313. while [ $AUX != "y" ] && [ $AUX != "n" ]; do
  314. echo "Yes (y) or no (n)? "
  315. read AUX
  316. while [ -z $AUX ]; do
  317. echo "Yes (y) or no (n)? "
  318. read AUX
  319. done
  320. done
  321. if [ $AUX == "y" ]; then
  322. mkdir -p ~/.config/lcmlog
  323. echo \
  324. "# lcmlog config file
  325. # Write this file as a ssh_config file.
  326. # For more info, run the command 'man ssh_config'.
  327. # Host is 'server': do not change the name.
  328. Host server
  329. HostName
  330. # User and Port parameters should be used only out of the lcm cluster.
  331. #User
  332. #Port" \
  333. > ~/.config/lcmlog/config
  334. echo "Done. Now you should edit the file and add HostName (and, in case, User and Port)."
  335. echo "Opening file with $EDITOR in 4 seconds..."
  336. sleep 4
  337. $EDITOR ~/.config/lcmlog/config
  338. echo
  339. else
  340. echo "Exit."
  341. exit 0
  342. fi
  343. fi
  344. ## APPLY METHOD
  345. # Temporary pipe to send input to the server
  346. pipe=$(mktemp)
  347. # Use the correct method
  348. # Method UPDATE
  349. if [ "$METHOD" == "UPDATE" ]; then
  350. echo "The UPDATE method is used to regenerate the internal server database."
  351. echo "It should only be called if a log file is added by hand,"
  352. echo "or if some of the log files get renamed or corrupted."
  353. echo "It assumes that all the log files are correctly formatted."
  354. echo
  355. echo "Are you sure? [y|n] "
  356. read ANSWER
  357. while [ -z $ANSWER ]; do
  358. printf "Yes (y) or no (n)? "
  359. read ANSWER
  360. done
  361. while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do
  362. printf "Yes (y) or no (n)? "
  363. read ANSWER
  364. while [ -z $ANSWER ]; do
  365. printf "Yes (y) or no (n)? "
  366. read ANSWER
  367. done
  368. done
  369. if [ $ANSWER == "n" ]; then exit 0; fi
  370. printf "UPDATE\n" >> $pipe
  371. # Method GET
  372. elif [ "$METHOD" == "GET" ]; then
  373. method_get $pipe
  374. # Method POST
  375. elif [ "$METHOD" == "POST" ]; then
  376. method_post $pipe
  377. fi
  378. # Remove accents
  379. pipe2=$(mktemp)
  380. 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
  381. # Call the server
  382. echo
  383. ssh -F ~/.config/lcmlog/config -s server lcmlog < $pipe2
  384. ### ENDING
  385. # Remove temporary pipe
  386. rm $pipe $pipe2
  387. # Check success or error
  388. RET=$?
  389. if (( $RET == 0 )); then
  390. echo "Operation $METHOD completed successfully."
  391. exit 0
  392. else
  393. printf "Server error: $RET\n" >&2
  394. exit $RET
  395. fi