cambios varios

This commit is contained in:
Sergio Pernas 2022-03-17 22:39:06 +00:00
parent 3cf6f1d4bf
commit 9a98c0c9d8
10 changed files with 91 additions and 226 deletions

View File

@ -1,39 +0,0 @@
#!/bin/bash
source /etc/nodemecu.conf
if [ $UID -ne 0 ]; then
echo "Ejecute 'sudo $0'"
exit
fi
seguro="Si, se lo que estoy haciendo."
echo "Está a punto de desinstalar nodemecu, esta acción eliminará todos los archivos de instalacion y registros que se hayan tomado sin posiblidad de recuperación, haga un backup de:
- /etc/nodemecu.conf
- $directorio_instalacion
- $log
Para continuar escriba la frase '$seguro'.
"
read -p "frase: " frase
if [ "$frase" = "$seguro" ];then
rm /etc/nodemecu.conf
rm /usr/bin/nodemecu
crontab -u root -r
rm -rf $directorio_instalacion
systemctl stop nodemecu.service
systemctl disable nodemecu.service
rm /lib/systemd/system/nodemecu.service
systemctl daemon-reload
else
echo "La frase de seguridad no concuerda.
"
fi

View File

@ -1,88 +0,0 @@
#!/bin/bash
#set -x
configuracion=nodemecu.conf.ejemplo
source $configuracion
if [ $UID -ne 0 ]; then
echo "Ejecute 'sudo $0'"
exit
fi
if [ -d $directorio_instalacion ] ; then
echo "nodemecu ya se encuentra instalado"
exit
fi
ficheros="bin env arduinos.py contador generador_json funciones monitor monitor_web registrador desinstalar"
# Dependencias
echo "1. Instalando dependencias
"
apt update && apt install -y python3-pip jsonlint jq uuid
pip3 install pyserial
echo ""
# Directorio de instalación
echo "2. Creando directorios y copiando archivos"
echo "
Creación de directorios
"
mkdir -vp $directorio_instalacion/archives/historical
mkdir -v $directorio_instalacion/archives/logs
mkdir -v $registros_corruptos
mkdir -v $envios_fallidos
mkdir -v $historicos
mkdir -v $registros
mkdir -v $key_dir
echo "
Copiado de archivos
"
cp -vr $ficheros $directorio_instalacion
cp -v $configuracion /etc/nodemecu.conf
echo "
Enlaces a binarios
"
chmod 755 $directorio_instalacion/bin/nodemecu
ln -vs $directorio_instalacion/bin/nodemecu /usr/bin/nodemecu
echo "
Fichero de logs
"
touch $log
ls $log
echo "3. Configurando systemd
"
cp -v nodemecu.service /lib/systemd/system/
systemctl daemon-reload
echo "
4. Obteniendo numero de serie
"
sn=$(cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2)
sed -i 's/numero_serie=/numero_serie/' $env
echo "Número de serie: $sn"
echo "
5. Configurar
"
echo "- Ejecute 'sudo nodemecu configurar' para comenzar.
"
echo "- Ejecute 'sudo nodemecu modo sensores' para obtener datos desde sensores.
"
echo "- Ejecute 'sudo nodemecu captura iniciar' para comenzar a tomar datos de sensores o datos de prueba.
"
echo "- Ejecute 'sudo systemctl start nodemecu.service' para iniciar el envío de datos.
"

View File

@ -1 +0,0 @@
0

View File

@ -23,8 +23,7 @@ sign_file () {
local _file="$1"
test ! -f "$_file" && return 1
test -f "$_file.sig" || ssh-keygen -Y sign -f "$private_key" -n file "$_file" 2>&1 >/dev/null
test -f "$_file.sig" || ssh-keygen -Y sign -f "$private_key" -n file "$_file" >/dev/null 2>&1
cat "${_file}.sig" | grep -v SIGNATURE | tr -d "\n"
}
@ -170,3 +169,42 @@ funcion_captura() {
fi
}
funcion_validador_json() {
validar="$@"
echo $validar | jsonlint-php > /dev/null 2>&1 || return 1
}
funcion_verificar_internet() {
if host fsf.org > /dev/null 2>&1; then
return 0
else
return 1
fi
}
funcion_espera() {
sleep ${intervalo:-1}m
}
funcion_envio_registro() {
curl -s --show-error -w "~%{http_code}" \
-X POST -H "X-Signature: $(sign_file $registros/$file)" \
-H "Content-Type: application/json" -d @$registros/$file \
$servidor 2> $curl_err
}

View File

@ -58,24 +58,12 @@ json="{\"transaction_uuid\":\"$uuid\",\"serial_number\":\"$numero_serie\",\"publ
touch $lock
file=$(date +%Y%m%d%H%M%S -d @$marca_de_tiempo)-$uuid
if echo $json | jsonlint-php 2> /dev/null;then
echo $json > $registros/$file && rm $lock && echo $((conteo+1)) > $contador
if funcion_validador_json $json ;then
echo $json > $registros/$file && rm $lock
else
echo "$(date +%Y-%m-%d-%H:%M:%S -d @$marca_de_tiempo) - No se pudo obtener información." >> $log
echo "$(date +%Y-%m-%d-%H:%M:%S -d @$marca_de_tiempo) - No se pudo generar fichero json:" >> $log
echo $json >> $log
rm $lock
fi
exit
# habia una razon para esto, ya va a saltar
for ((i=1 ; i <=3 ; i++));do
if echo $json | jsonlint-php 2> /dev/null;then
echo $json > /opt/nodemecu/stack/$file && rm /opt/nodemecu/lock && echo $((counter+1)) > $contador && break
else
echo "$(date +%Y-%m-%d-%H:%M:%S) - No se pudo obtener información." >> /opt/nodemecu/errors.log && sleep 1
fi
done

View File

@ -20,7 +20,7 @@ if [ -d $directorio_instalacion ] ; then
fi
ficheros="bin envs arduinos.py contador generador_json funciones monitor monitor_web registrador desinstalar"
ficheros="bin envs arduinos.py generador_json funciones registrador desinstalar"
# Dependencias

View File

@ -1,16 +0,0 @@
#!/bin/bash
source /etc/nodemecu.conf
while :;do
clear
echo "$(date)"
echo "Stack: $(ls $stack | wc -l) ficheros"
echo "Historicos: $(ls $historical | wc -l) ficheros"
echo "Testigo: $(cat $install_dir/counter)"
echo "Errores: $(wc -l $log) registros"
echo "Ultimo error registrado:"
tail -1 $log
echo ""
echo "'ctrl-c' para salir."
sleep 100
done

View File

@ -1,11 +0,0 @@
#!/bin/bash
source /etc/nodemecu.conf
echo "<p>$(date)<br>"
echo "Stack: $(ls $stack | wc -l) ficheros<br>"
echo "Historicos: $(ls $historical | wc -l) ficheros<br>"
echo "Testigo: $(cat $install_dir/counter)<br>"
echo "Errores: $(wc -l $log) registros<br>"
echo "Ultimos errores registrados<br>"
tail -1 $log
echo "<br></p>"

View File

@ -4,64 +4,62 @@ source /etc/nodemecu.conf
source /opt/nodemecu/envs
source $funciones
# Revisando stack
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
done
while :;do
# archivo a procesar
file=$(get_stack)
# si no hay nada que procesar reinicia el bucle
test -z $file && funcion_espera && continue
# si no hay servidor definido se reinicia el bucle
# espera el intervalo definido o 1 minuto.
#[ -z $servidor ] && echo "no se ha definido un servidor de entrega" && sleep ${intervalo:-1}m && continue
test -z $servidor && echo "No se configuró nigun servidor de entrega." && funcion_espera && continue
# 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
# si no respone el ping reinica el bucle
ping -c 1 fsf.org > /dev/null 2>&1 || continue
# si no hay internet reinica el bucle
if ! funcion_verificar_internet; then
echo "Parece no haber internet."
funcion_espera
continue
fi
# si el fichero 'lock' existe (se esta creando un nuevo registro) reinicia el bucle
[ -f $lock ] && continue
test -f $lock && continue
# validación del fichero
if ! funcion_validador_json "$(cat $registros/$file)";then
mv $registros/$file $registros_corruptos
continue
fi
# 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
# firmado de regisro
firma=$(sign_file $registros/$file)
local_transaction_uuid=$(jq -r '."transaction_uuid"' $registros/$file)
# UUID del registro a enviar
uuid_registro=$(jq -r '."transaction_uuid"' $registros/$file)
remote_transaction=$(curl -s --show-error -w "~%{http_code}" -X POST -H "X-Signature: $(sign_file $registros/$file)" -H "Content-Type: application/json" -d @$registros/$file $servidor 2> $curl_err)
remote_response="$(echo $remote_transaction | cut -d '~' -f 1)"
server_error="$(echo $remote_transaction | cut -d '~' -f 2)"
# envio de registro
transaccion=$(curl -v -s --show-error -w "~%{http_code}" -X POST -H "X-Signature: $(sign_file $registros/$file)" -H "Content-Type: application/json" -d @$registros/$file $servidor 2> $curl_err)
# respuestas del servidor
respuesta_servidor="$(echo $transaccion | cut -d '~' -f 1)"
error_servidor="$(echo $transaccion | cut -d '~' -f 2)"
if [ ${#remote_response} -eq 36 ] && [ "$local_transaction_uuid" == "$remote_response" ]; then
echo -e "$remote_response: \e[92mOK\e[0m"
if [ ${#respuesta_servidor} -eq 36 ] && [ "$uuid_registro" == "$respuesta_servidor" ]; then
echo -e "$respuesta_servidor: \e[92mOK\e[0m"
mv $registros/$file $registros/$file.sig $historicos
else
echo -e "$local_transaction_uuid: \e[91mFAIL\e[0m"
# errores
unset srv_msg
unset curl_msg
echo -e "$uuid_registro: \e[91mFAIL\e[0m"
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 message: " >> $log
[ -s "$curl_err" ] && cat $curl_err >> $log
srv_msg="$(echo $respuesta_servidor | grep -o '<body.*>.*</body>')"
echo "$err_time - $uuid_registro - Error del servidor: $error_servidor" >> $log
echo "$err_time - $uuid_registro - Mensaje del servidor:" >> $log
echo "$srv_msg" >> $log
if [ -s $curl_err ];then
echo "$err_time - $uuid_registro - Salida de curl:" >> $log
cat $curl_err >> $log
fi
rm $curl_err
exit
@ -70,3 +68,12 @@ while :;do
done
exit
######
# 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

View File

@ -1,13 +0,0 @@
#!/bin/bash
if timeout 1 python arduinos.py 0x05; then
echo respondio
else
echo "No respondio, erro $?"
fi