diff --git a/master b/master new file mode 100755 index 0000000..1aed73b --- /dev/null +++ b/master @@ -0,0 +1,135 @@ +#!/bin/bash + + +# (1) Init. +#--------------------------------------------------------# +# (1) Get current absolute dir +ROOT=$(dirname `realpath $0`); + +# (2) Check argc +test $# -lt 2 && echo -e "error: too few arguments\n\n\e[1mUSAGE\e[0m\n\tmaster \n\n\e[1mARGUMENTS\e[0m\n\t\tThe key of the pool\n\t\tThe host where the pool is hosted\n\t\tTo port where the pool is bound\n\t\tThe folder containing ONLY the logger daemons\n" && exit 1; + +# (3) Check @PORT range # +MIN_PORT=1024; +MAX_PORT=49151; +test "$3" -gt "$MAX_PORT" -o "$3" -lt "$MIN_PORT" && echo "error: must be between $MIN_PORT and $MAX_PORT" && exit 1; + +# (4) Set argument explicit names # +POOL_KEY="$1"; +POOL_HOST="$2"; +POOL_PORT="$3"; +LOGGER_DIR="`realpath $4`"; + +# (5) Check @LOGGER_DIR # +test ! -d "$LOGGER_DIR" && echo "error: is not a valid folder." && exit 1; + +# (6) init logger variables # +declare -A OUTPUT_PID; # will contain each bind-output PIDs +declare -A LOGGER_PID; # will contain each logger PIDs + +# (7) get logger list # +LOGGERS=(`ls $LOGGER_DIR`); + +# (8) If no logger -> exit # +test "${#LOGGERS[@]}" -eq 0 && echo "error: no logger found in ." && exit 1; + +# (9) Get local IP address # +LOCAL_IP="`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'`"; + +test -z "$LOCAL_IP" && echo "error: cannot get local IP address." && exit 1; +LOCAL_IP="localhost"; + + + + +# (2) Set exit management +#--------------------------------------------------------# +# (1) On-Exit routine # +on_exit(){ + + echo "killing 'bind-output' sub processes"; + + for logger_name in "${!OUTPUT_PID[@]}"; do + + # kill each logger OUTPUT + kill -9 ${OUTPUT_PID[$logger_name]} 2>/dev/null; + + done; + + for logger_name in "${!LOGGER_PID[@]}"; do + + # kill each logger OUTPUT + kill -9 ${LOGGER_PID[$logger_name]} 2>/dev/null; + + done; + + echo "killing 'master' output socket"; + kill -9 $MASTER_PID 2>/dev/null; + + echo "killing 'master' loop"; + # kill -9 $LOOP_PID 2>/dev/null; + +} + +trap "on_exit;" INT KILL; + + + + + +# (3) Connect to pool +#--------------------------------------------------------# +# (1) Bind-output # +echo "(.) connect to $POOL_HOST:$POOL_PORT"; +$ROOT/bind-output master $POOL_HOST $POOL_PORT& +MASTER_PID=$?; + + +# (4) Launch master manager +#--------------------------------------------------------# +# (1) For each logger +PORT="$MIN_PORT"; +for logger_name in "${LOGGERS[@]}"; do + + # Find an available port + while true; do + + # if port not in use -> use it + ss -tl4 "( sport = :$PORT )" | grep "$PORT" >/dev/null 2>&1 || break; + + # else try next port (+1) + PORT="`expr $PORT + 1`"; + + done; + + # send logger port request + echo " (.) Sending logger@$logger_name request"; + $ROOT/write master "$POOL_KEY:$logger_name:$LOCAL_IP:$PORT"; + + # check pool response + RESP="`nc -w 5 -lp $PORT`"; + + # empty response -> ignore + test -z "$RESP" && continue; + + # not a number -> ignore + echo -n "$RESP" | grep -P '^\d+$' >/dev/null || continue; + + + # Create output bound + echo " (.) Connecting '$logger_name' to $POOL_HOST:$RESP"; + $ROOT/bind-output master_$logger_name $POOL_HOST $RESP& + OUTPUT_PID[$logger_name]=$!; + + echo " (.) launching logger@$logger_name"; + # launch logger bound to output + ( $LOGGER_DIR/$logger_name | cat > /tmp/outbuf_master_$logger_name )& + LOGGER_PID[$logger_name]=$!; + + + + # increment port for next logger + PORT="`expr $PORT + 1`"; + +done; + diff --git a/pool b/pool index e9bc45e..dd9fd8b 100755 --- a/pool +++ b/pool @@ -65,6 +65,7 @@ POOL_PID=$?; # (4) Launch pool manager #--------------------------------------------------------# # infinite listener +PORT="$MIN_PORT"; while sleep 1; do # Listen on port @POOL_PORT @@ -72,7 +73,7 @@ while sleep 1; do # Do nothing if empty msg or invalid format test -z "$MSG" && continue; - echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\\ invalid format' && continue; + echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\ invalid format' && continue; # Extract ID - PORT KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`"; @@ -81,13 +82,13 @@ while sleep 1; do LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`"; # if ID does not match - test "$KEY" != "$POOL_KEY" && echo ' /!\\ wrong id' && continue; + test "$KEY" != "$POOL_KEY" && echo ' /!\ wrong id' && continue; # create new listening socket echo " (.) binding input for logger@$LOGGER_NAME ($LOGGER_HOST:$LOGGER_PORT)"; # Find an available port - PORT="$MIN_PORT"; + PORT="`expr $PORT + 1`"; while true; do diff --git a/trigger b/trigger deleted file mode 100755 index f9b8cd3..0000000 --- a/trigger +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -# (0) Get current ABS directory -ROOT=$(dirname `realpath $0`); - -# (1) Check arguments -test $# -lt 1 && echo "ERR: Missing arguments (trigger_port)" && exit 1; - -# (2) Wait for triggering by peer -echo "(.) set up [trigger:$1]"; -nc -lp $1; - -# (3) Log trigger received # -echo " > RECEIVED [trigger:$1]";