#!/bin/bash DIR="/var/local/log/lcmlog-data" EDITOR="${EDITOR:-nano}" ###################### # AUXILIARY FUNCTION # ###################### usage() { echo "Usage: $0 [-g|-p|-u]" echo "Use -h option to show the help message." } ###### method_help() { echo "lcmlog: client side of a tool for reading and writing the lcm staff logs." echo echo "Usage: $0 [option]" echo echo "Options:" echo " -g Method GET: print all the logs that meet the required criteria" echo " -p Method POST: add a log to the database" echo " -u Method UPDATE: regenerate the internal server database" echo " -h Show this help" } ###### method_get() { pipe=$1 # KIND # The user must say if he's looking for 150 or Admin logs echo "Search for admin logs [a] or 150 logs [1]?" read CHAR while [ -z $CHAR ]; do echo "Retry, admin logs [a] or 150 logs [1]?" read CHAR done while [ $CHAR != "a" ] && [ $CHAR != "1" ]; do echo "Retry, admin logs [a] or 150 logs [1]?" read CHAR while [ -z $CHAR ]; do echo "Retry, admin logs [a] or 150 logs [1]?" read CHAR done done # Select the correct kind if [ $CHAR == "a" ]; then KIND="Admin" elif [ $CHAR == "1" ]; then KIND="150" fi # USER echo echo "Search for which user? (optional)" read USER # DATE echo echo "Search for which date? (yyyy-mm-dd) (optional)" read DATE while ! [ -z $DATE ]; do if [ ${DATE:4:1} != '-' ] || [ ${DATE:7:1} != '-' ] || (( ${#DATE} != 10 )); then echo "Format: yyyy-mm-dd (i.e. 9 is 09). Reinsert." read DATE else break fi done # TAGS echo echo "Search for which tags? (insert comma separated tags) (optional)" read TAGS # Update pipe printf "%s\n" "GET" >> $pipe printf "%s\n" "${KIND}" >> $pipe printf "%s\n" "${DATE}" >> $pipe printf "%s\n" "${TAGS}" >> $pipe printf "%s\n" "${USER}" >> $pipe } ###### method_post() { pipe=$1 # KIND AUX="n" while [ $AUX == n ]; do echo "Send an admin log [a] or 150 log [1]?" read CHAR while [ -z $CHAR ]; do echo "Retry, admin logs [a] or 150 logs [1]?" read CHAR done while [ $CHAR != "a" ] && [ $CHAR != "1" ]; do echo "Retry, admin logs [a] or 150 logs [1]?" read CHAR while [ -z $CHAR ]; do echo "Retry, admin logs [a] or 150 logs [1]?" read CHAR done done # Select the correct kind if [ $CHAR == "a" ]; then KIND="Admin" elif [ $CHAR == "1" ]; then KIND="150" fi echo "Confirm $KIND log? [y|n] " read AUX while [ -z $AUX ]; do echo "Yes (y) or no (n)? " read AUX done while [ $AUX != "y" ] && [ $AUX != "n" ]; do echo "Yes (y) or no (n)? " read AUX while [ -z $AUX ]; do echo "Yes (y) or no (n)? " read AUX done done done # DATE echo echo "Insert the date." # Year echo "Year? (yyyy)" read YEAR YEAR=$((10#$YEAR)) re='^[0-9]+$' while ! [[ $YEAR =~ $re ]]; do echo "Wrong, not a number: reinsert." read YEAR YEAR=$((10#$YEAR)) done while (( ${#YEAR} != 4 )); do echo "Wrong year, reinsert." read YEAR YEAR=$((10#$YEAR)) while ! [[ $YEAR =~ $re ]]; do echo "Wrong, not a number: reinsert." read YEAR YEAR=$((10#$YEAR)) done done # Month echo "Month? (mm)" read MONTH MONTH=$((10#$MONTH)) re='^[0-9]+$' while ! [[ $MONTH =~ $re ]]; do echo "Wrong, not a number: reinsert." read MONTH MONTH=$((10#$MONTH)) done while (( $MONTH < 1 )) || (( $MONTH > 12 )); do echo "Wrong month, reinsert." read MONTH MONTH=$((10#$MONTH)) while ! [[ $MONTH =~ $re ]]; do echo "Wrong, not a number: reinsert." read MONTH MONTH=$((10#$MONTH)) done done # Day echo "Day? (dd)" if (( $MONTH == 2 )); then echo "Be careful about the day! There is no check about leap years." fi read DAY DAY=$((10#$DAY)) re='^[0-9]+$' while ! [[ $DAY =~ $re ]]; do echo "Wrong, not a number: reinsert." read DAY DAY=$((10#$DAY)) done # Month with 31 day if (( $MONTH == 1 )) || (( $MONTH == 3 )) || (( $MONTH == 5 )) || (( $MONTH == 7 )) || (( $MONTH == 8 )) || (( $MONTH == 10 )) || (( $MONTH == 12 )); then while (( $DAY < 1 )) || (( $DAY > 31 )); do echo "Wrong day, reinsert." read DAY DAY=$((10#$DAY)) while ! [[ $DAY =~ $re ]]; do echo "Wrong, not a number: reinsert." read DAY DAY=$((10#$DAY)) done done # Month with 30 day elif (( $MONTH == 4 )) || (( $MONTH == 6 )) || (( $MONTH == 9 )) || (( $MONTH == 11 )); then while (( $DAY < 1 )) || (( $DAY > 30 )); do echo "Wrong day, reinsert." read DAY DAY=$((10#$DAY)) while ! [[ $DAY =~ $re ]]; do echo "Wrong, not a number: reinsert." read DAY DAY=$((10#$DAY)) done done # Month with 29 day elif (( $MONTH == 2 )); then while (( $DAY < 1 )) || (( $DAY > 29 )); do echo "Wrong day, reinsert." read DAY DAY=$((10#$DAY)) while ! [[ $DAY =~ $re ]]; do echo "Wrong, not a number: reinsert." read DAY DAY=$((10#$DAY)) done done fi ## Turn month and day in correctly strings if (( ${#MONTH} == 1 )); then MONTH=0$MONTH fi if (( ${#DAY} == 1 )); then DAY="0$DAY" #DAY=$((10#$DAY)) fi # Morning or afternoon shift if [ $KIND == "150" ]; then echo "Morning [m] or afternoon [p] shift?" read CHAR while [ -z $CHAR ]; do echo "Retry, morning [m] or afternoon [p] shift?" read CHAR done while [ $CHAR != "m" ] && [ $CHAR != "p" ]; do echo "Retry, morning [m] or afternoon [p] shift?" read CHAR while [ -z $CHAR ]; do echo "Retry, morning [m] or afternoon [p] shift?" read CHAR done done # Select the correct kind if [ $CHAR == "m" ]; then MP="mat" elif [ $CHAR == "p" ]; then MP="pom" fi DATE=$YEAR"-"$MONTH"-"$DAY"_"$MP else DATE=$YEAR"-"$MONTH"-"$DAY fi # TAGS echo echo "Tags? (insert comma separated tags)" read TAGS while [ -z $TAGS ]; do echo "Insert tags." read TAGS done SEND_MAIL="" echo read -p "Send the log via email? [default: y] " AUX while [ "$AUX" != "y" ] && [ "$AUX" != "n" ] && ! [ -z "$AUX" ]; do read -p "Yes (y) or no (n)? [default: y] " AUX done [ -z "$AUX" ] && AUX="y" if [ "$AUX" = "y" ]; then SEND_MAIL="MAIL" fi # Update of the pipe printf "%s\n" "POST" >> $pipe printf "%s\n" "${KIND}" >> $pipe printf "%s\n" "${DATE}" >> $pipe printf "%s\n" "${TAGS}" >> $pipe [ -z "$SEND_MAIL" ] || printf "%s\n" "${SEND_MAIL}" >> $pipe # Write log FILE_NAME=$(mktemp) echo read -p "Upload the log via file? [default: n] " AUX while [ "$AUX" != "y" ] && [ "$AUX" != "n" ] && ! [ -z "$AUX" ]; do read -p "Yes (y) or no (n)? [default: n] " AUX done [ -z "$AUX" ] && AUX="n" if [ "$AUX" = "n" ]; then echo echo "Please write your log:" echo $EDITOR $FILE_NAME cat $FILE_NAME >> $pipe rm $FILE_NAME else echo # echo "Please insert your log file path:" read -e -p "Please insert your log file path: " FILE while ! [ -z "$FILE" ] && ! [ -f "$FILE" ]; do # echo "Please insert a valid path:" read -e -p "Please insert a valid path: " FILE done echo cat $FILE >> $pipe fi } ############### # MAIN SCRIPT # ############### ## ENTRY POINT - INITIALIZATION # Check number of arguments if (( $# != 1 )); then usage exit 1 fi # Check that the option is only one # --> -g OK # -gp NO if (( ${#1} != 2 )); then usage exit 1 fi # Select method from the option while getopts ":gpuh" o; do case $o in g) METHOD="GET";; p) METHOD="POST";; u) METHOD="UPDATE";; h) method_help && exit 0;; #*) usage;; \?) usage && exit 1;; esac done # You must reset of the correct $OPTIND shift $(( $OPTIND -1 )) # Security control if [ -z "${METHOD}" ]; then usage; fi ## CHECK CONFIG FILE if ! [ -f ~/.config/lcmlog/config ]; then echo "Config file not found. The config file must be generated in order to use this program." echo "Do you want to generate it? (folder: ~/.config/lcmlog) [y|n]" read AUX while [ -z $AUX ]; do echo "Yes (y) or no (n)? " read AUX done while [ $AUX != "y" ] && [ $AUX != "n" ]; do echo "Yes (y) or no (n)? " read AUX while [ -z $AUX ]; do echo "Yes (y) or no (n)? " read AUX done done if [ $AUX == "y" ]; then mkdir -p ~/.config/lcmlog echo \ "# lcmlog config file # Write this file as a ssh_config file. # For more info, run the command 'man ssh_config'. # Host is 'server': do not change the name. Host server HostName # User and Port parameters should be used only out of the lcm cluster. #User #Port" \ > ~/.config/lcmlog/config echo "Done. Now you should edit the file and add HostName (and, in case, User and Port)." echo "Opening file with $EDITOR in 4 seconds..." sleep 4 $EDITOR ~/.config/lcmlog/config echo else echo "Exit." exit 0 fi fi ## APPLY METHOD # Temporary pipe to send input to the server pipe=$(mktemp) # Use the correct method # Method UPDATE if [ "$METHOD" == "UPDATE" ]; then echo "The UPDATE method is used to regenerate the internal server database." echo "It should only be called if a log file is added by hand," echo "or if some of the log files get renamed or corrupted." echo "It assumes that all the log files are correctly formatted." echo echo "Are you sure? [y|n] " read ANSWER while [ -z $ANSWER ]; do printf "Yes (y) or no (n)? " read ANSWER done while [ $ANSWER != "y" ] && [ $ANSWER != "n" ]; do printf "Yes (y) or no (n)? " read ANSWER while [ -z $ANSWER ]; do printf "Yes (y) or no (n)? " read ANSWER done done if [ $ANSWER == "n" ]; then exit 0; fi printf "UPDATE\n" >> $pipe # Method GET elif [ "$METHOD" == "GET" ]; then method_get $pipe # Method POST elif [ "$METHOD" == "POST" ]; then method_post $pipe fi # Remove accents pipe2=$(mktemp) 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 # Call the server echo ssh -F ~/.config/lcmlog/config -s server lcmlog < $pipe2 RET=$? ### ENDING # Remove temporary pipe rm $pipe $pipe2 # Check success or error if (( $RET == 0 )); then echo "Operation $METHOD completed successfully." exit 0 else printf "Server error: $RET\n" >&2 exit $RET fi