From 9a98c0c9d8212fbe47e6c41905888072106b2e49 Mon Sep 17 00:00:00 2001 From: Sergio Pernas Date: Thu, 17 Mar 2022 22:39:06 +0000 Subject: [PATCH] cambios varios --- raspberry/bak.desinstalar | 39 ----------------- raspberry/bak.instalador | 88 --------------------------------------- raspberry/contador | 1 - raspberry/funciones | 42 ++++++++++++++++++- raspberry/generador_json | 20 ++------- raspberry/instalador | 2 +- raspberry/monitor | 16 ------- raspberry/monitor_web | 11 ----- raspberry/registrador | 85 ++++++++++++++++++++----------------- raspberry/test | 13 ------ 10 files changed, 91 insertions(+), 226 deletions(-) delete mode 100755 raspberry/bak.desinstalar delete mode 100755 raspberry/bak.instalador delete mode 100644 raspberry/contador delete mode 100755 raspberry/monitor delete mode 100755 raspberry/monitor_web delete mode 100755 raspberry/test diff --git a/raspberry/bak.desinstalar b/raspberry/bak.desinstalar deleted file mode 100755 index f2741f0..0000000 --- a/raspberry/bak.desinstalar +++ /dev/null @@ -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 diff --git a/raspberry/bak.instalador b/raspberry/bak.instalador deleted file mode 100755 index 5c736ce..0000000 --- a/raspberry/bak.instalador +++ /dev/null @@ -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. -" diff --git a/raspberry/contador b/raspberry/contador deleted file mode 100644 index 573541a..0000000 --- a/raspberry/contador +++ /dev/null @@ -1 +0,0 @@ -0 diff --git a/raspberry/funciones b/raspberry/funciones index e40efcb..0d21131 100644 --- a/raspberry/funciones +++ b/raspberry/funciones @@ -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 + +} + + + diff --git a/raspberry/generador_json b/raspberry/generador_json index e3d8859..c9421a0 100755 --- a/raspberry/generador_json +++ b/raspberry/generador_json @@ -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 diff --git a/raspberry/instalador b/raspberry/instalador index 32ca55e..655f60f 100755 --- a/raspberry/instalador +++ b/raspberry/instalador @@ -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 diff --git a/raspberry/monitor b/raspberry/monitor deleted file mode 100755 index 44b9246..0000000 --- a/raspberry/monitor +++ /dev/null @@ -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 diff --git a/raspberry/monitor_web b/raspberry/monitor_web deleted file mode 100755 index 3c3ddb8..0000000 --- a/raspberry/monitor_web +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -source /etc/nodemecu.conf - 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 "Ultimos errores registrados
" - tail -1 $log - echo "

" diff --git a/raspberry/registrador b/raspberry/registrador index 1c57ec7..bdb874f 100755 --- a/raspberry/registrador +++ b/raspberry/registrador @@ -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 '.*')" - [ -s "$curl_err" ] && curl_msg="- $(> $log - [ -s "$curl_err" ] && cat $curl_err >> $log + srv_msg="$(echo $respuesta_servidor | grep -o '.*')" + + 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 diff --git a/raspberry/test b/raspberry/test deleted file mode 100755 index dd8b12b..0000000 --- a/raspberry/test +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - - - - - -if timeout 1 python arduinos.py 0x05; then - - echo respondio -else - echo "No respondio, erro $?" - -fi