nodemecu/raspberry/registrador

70 lines
2.2 KiB
Text
Raw Permalink Normal View History

2022-03-05 18:13:50 +00:00
#!/bin/bash
source /etc/nodemecu.conf
2022-03-11 02:00:35 +00:00
source $funciones
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
# Revisando stack
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
for registro_json in $registros/*;do
test "$registro_json" = "/opt/nodemecu/stack/\*" || break
jsonlint-php $registro_json > /dev/null || mv -v $registro_json $registros_corruptos
2022-03-05 18:13:50 +00:00
done
2022-03-11 02:00:35 +00:00
while :;do
2022-03-11 17:10:48 +00:00
# archivo a procesar
file=$(get_stack)
# si no hay servidor definido se reinicia el bucle
# espera el intervalo definido o 1 minuto.
2022-03-11 17:10:48 +00:00
#[ -z $servidor ] && echo "no se ha definido un servidor de entrega" && sleep ${intervalo:-1}m && continue
2022-03-11 02:00:35 +00:00
# si no hay nada en el directorio registros reinicia el bucle
# espera el intervalo definido o 1 minuto.
[ -z $file ] && sleep ${intervalo:-1}m && continue
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
# si no respone el ping reinica el bucle
ping -c 1 fsf.org > /dev/null 2>&1 || continue
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
# si el fichero 'lock' existe (se esta creando un nuevo registro) reinicia el bucle
[ -f $lock ] && continue
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
# se testea que el servidor responda
#test_url="$(curl -s -X POST -o /dev/null -w "%{http_code}" $servidor)"
#if [ $test_url -eq 404 ];then
# echo "$(date +%Y-%m-%d-%H:%M:%S) - $url - $test_url" >> /opt/nodemecu/errors.log && sleep 300 && continue
#fi
2022-03-05 18:13:50 +00:00
2022-03-11 17:10:48 +00:00
# firma de registros
sign_file $registros/$file
2022-03-11 02:00:35 +00:00
firma="$(cat $registros/$file.sign)"
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
local_transaction_uuid=$(jq -r '."transaction_uuid"' $registros/$file)
2022-03-11 17:10:48 +00:00
remote_transaction=$(curl -s --connect-timeout 1,5 --show-error -w "~%{http_code}" -X POST -H "X-Signature: $firma" -H "Content-Type: application/json" -d @$registros/$file $servidor 2> $curl_err)
2022-03-11 02:00:35 +00:00
remote_response="$(echo $remote_transaction | cut -d '~' -f 1)"
server_error="$(echo $remote_transaction | cut -d '~' -f 2)"
if [ ${#remote_response} -eq 36 ];then
2022-03-05 18:13:50 +00:00
2022-03-11 02:00:35 +00:00
if [ $local_transaction_uuid == $remote_response ]; then
2022-03-11 17:10:48 +00:00
mv $registros/$file $registros/$file.sign $historicos
2022-03-11 02:00:35 +00:00
echo -e "$remote_response: \e[92mOK\e[0m"
fi
2022-03-05 18:13:50 +00:00
else
2022-03-11 02:00:35 +00:00
echo -e "$local_transaction_uuid: \e[91mFAIL\e[0m"
# errores
unset srv_msg
unset curl_msg
err_time=$(date +%Y-%m-%d-%H:%M:%S)
[ ! -z "$remote_response" ] && srv_msg="- $( echo $remote_response | grep -o '<body.*>.*</body>')"
[ -s "$curl_err" ] && curl_msg="- $(</tmp/curl_err)"
echo $err_time - $local_transaction_uuid - server status: $server_error $srv_msg $curl_msg >> $log
rm $curl_err
2022-03-05 18:13:50 +00:00
fi
2022-03-11 02:00:35 +00:00
done
exit
2022-03-05 18:13:50 +00:00