lcmlog 8.8 KB

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