lcmlog 9.0 KB

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