[RFC,v2] fff-autorestart: Add autorestart package

Submitted by Adrian Schmutzler on July 4, 2017, 6:41 p.m.

Details

Message ID 1499193698-30868-1-git-send-email-freifunk@adrianschmutzler.de
State Superseded
Headers show

Commit Message

Adrian Schmutzler July 4, 2017, 6:41 p.m.
This package implements an auto-restart if the router has not
been able to connect to a gateway for more than 30 minutes. If
there had been an error or crash causing the interruption, it
may be resolved by the restart while no harm is done.

This is an RFC patch, as I'm interested in implementing this into
the firmware of the routers I administer. Thus, any comments,
suggestions and so forth are welcome!

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>

Tested-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
---
 src/packages/fff/fff-autorestart/Makefile          | 40 ++++++++++++++++++++++
 .../files/usr/lib/micron.d/fff-autorestart         |  1 +
 .../fff-autorestart/files/usr/sbin/offlinerestart  | 36 +++++++++++++++++++
 src/packages/fff/fff/Makefile                      |  1 +
 4 files changed, 78 insertions(+)
 create mode 100755 src/packages/fff/fff-autorestart/Makefile
 create mode 100644 src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestart
 create mode 100755 src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart

Patch hide | download patch | download mbox

diff --git a/src/packages/fff/fff-autorestart/Makefile b/src/packages/fff/fff-autorestart/Makefile
new file mode 100755
index 0000000..b48542a
--- /dev/null
+++ b/src/packages/fff/fff-autorestart/Makefile
@@ -0,0 +1,40 @@ 
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=fff-autorestart
+PKG_VERSION:=1
+PKG_RELEASE:=1
+
+PKG_BUILD_DIR:=$(BUILD_DIR)/fff-autorestart
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/fff-autorestart
+    SECTION:=base
+    CATEGORY:=Freifunk
+    TITLE:= Freifunk-Franken Auto-Restart
+    URL:=http://www.freifunk-franken.de
+    DEPENDS:=+micrond
+endef
+
+define Package/fff-autorestart/description
+    This restarts the router if no connection
+	to gateways is available
+endef
+
+define Build/Prepare
+	echo "all: " > $(PKG_BUILD_DIR)/Makefile
+endef
+
+define Build/Configure
+	# nothing
+endef
+
+define Build/Compile
+	# nothing
+endef
+
+define Package/fff-autorestart/install
+    $(CP) ./files/* $(1)/
+endef
+
+$(eval $(call BuildPackage,fff-autorestart))
diff --git a/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestart b/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestart
new file mode 100644
index 0000000..885b50a
--- /dev/null
+++ b/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestart
@@ -0,0 +1 @@ 
+*/5 * * * * /usr/sbin/offlinerestart
diff --git a/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart b/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart
new file mode 100755
index 0000000..cb17e66
--- /dev/null
+++ b/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart
@@ -0,0 +1,36 @@ 
+#!/bin/sh
+
+UPPER_LIMIT='50' # Above this limit the gateway will be considered online
+LOWER_LIMIT='20' # Below this limit the gateway will be considered offline
+# In-between these two values the state is not changed
+
+NOW=$(date +%s)
+
+GATEWAY_TQ=`batctl gwl | grep "^=>" | awk -F'[()]' '{print $2}'| tr -d " "` # Grep the connection quality of the gateway which is currently used
+if [ ! $GATEWAY_TQ ]; # If there is no gateway there will be errors in the following if clauses
+then
+	GATEWAY_TQ=0 # Just an easy way to get an valid value if there is no gateway
+fi
+if [ $GATEWAY_TQ -gt $UPPER_LIMIT ];
+then
+	echo "Gateway TQ is $GATEWAY_TQ - Node is online"
+	if [ -s /tmp/gatewayoff ] ; then 
+		rm -f /tmp/gatewayoff
+	fi
+fi
+if [ $GATEWAY_TQ -lt $LOWER_LIMIT ];
+then
+	echo "Gateway TQ is $GATEWAY_TQ - Node is considered offline"
+	if [ ! -s /tmp/gatewayoff ] ; then 
+		echo $NOW > /tmp/gatewayoff
+	fi
+	OFFLINESINCE=$(($(date +%s)-1800)) # Restart after 30 minutes
+	if [ "$(cat /tmp/gatewayoff)" -lt "$OFFLINESINCE" ] ; then
+		#rm -f /tmp/gatewayoff
+		reboot && exit
+	fi
+fi
+if [ $GATEWAY_TQ -ge $LOWER_LIMIT -a $GATEWAY_TQ -le $UPPER_LIMIT ]; # This is just to get a clean run if we are in-between the grace periode
+then
+	echo "Gateway TQ is $GATEWAY_TQ - Do nothing"
+fi
diff --git a/src/packages/fff/fff/Makefile b/src/packages/fff/fff/Makefile
index b039c2d..21ed827 100644
--- a/src/packages/fff/fff/Makefile
+++ b/src/packages/fff/fff/Makefile
@@ -18,6 +18,7 @@  define Package/fff-base
              +ip6tables \
              +odhcp6c \
              +micrond \
+			 +fff-autorestart \
              +fff-nodewatcher \
              +fff-web \
              +fff-uradvd \

Comments

Tim Niemeyer July 9, 2017, 9:17 a.m.
Hi

Am Dienstag, den 04.07.2017, 20:41 +0200 schrieb Adrian Schmutzler:
> This package implements an auto-restart if the router has not
> been able to connect to a gateway for more than 30 minutes. If
> there had been an error or crash causing the interruption, it
> may be resolved by the restart while no harm is done.
Hm.. Ich glaube, theoretisch sollte da nichts böses passieren. Aber
irgendwie habe ich trotzdem kein gutes Gefühl dabei.

> This is an RFC patch, as I'm interested in implementing this into
> the firmware of the routers I administer. Thus, any comments,
> suggestions and so forth are welcome!
Es wird mit dem Script auf jeden Fall schwieriger Fehler zu finden. Ich
würde sogar vermuten, dass es den ein oder anderen auch mal in die
Weißglut treiben könnten, wenn plötzlich mitten im Entwickeln das Gerät
plötzlich n Reboot hinlegt und man dann nicht sicher weiß was da los
war.

Grundsätzlich sollten solche Script mMn das letzte Mittel der Wahl sein.
Wir hatten mal sowas ähnliches, den "ath9kwatchdog".

Neben der Bitte den Shellchecker über das Script laufen zu lassen, unten
noch n paar Detail-Kommentare.

> Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
> 
> Tested-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
> ---
>  src/packages/fff/fff-autorestart/Makefile          | 40 ++++++++++++++++++++++
>  .../files/usr/lib/micron.d/fff-autorestart         |  1 +
>  .../fff-autorestart/files/usr/sbin/offlinerestart  | 36 +++++++++++++++++++
>  src/packages/fff/fff/Makefile                      |  1 +
>  4 files changed, 78 insertions(+)
>  create mode 100755 src/packages/fff/fff-autorestart/Makefile
>  create mode 100644 src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestart
>  create mode 100755 src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart
> 
> diff --git a/src/packages/fff/fff-autorestart/Makefile b/src/packages/fff/fff-autorestart/Makefile
> new file mode 100755
> index 0000000..b48542a
> --- /dev/null
> +++ b/src/packages/fff/fff-autorestart/Makefile
> @@ -0,0 +1,40 @@
> +include $(TOPDIR)/rules.mk
> +
> +PKG_NAME:=fff-autorestart
> +PKG_VERSION:=1
> +PKG_RELEASE:=1
> +
> +PKG_BUILD_DIR:=$(BUILD_DIR)/fff-autorestart
> +
> +include $(INCLUDE_DIR)/package.mk
> +
> +define Package/fff-autorestart
> +    SECTION:=base
> +    CATEGORY:=Freifunk
> +    TITLE:= Freifunk-Franken Auto-Restart
> +    URL:=http://www.freifunk-franken.de
> +    DEPENDS:=+micrond
> +endef
> +
> +define Package/fff-autorestart/description
> +    This restarts the router if no connection
> +	to gateways is available
> +endef
> +
> +define Build/Prepare
> +	echo "all: " > $(PKG_BUILD_DIR)/Makefile
> +endef
> +
> +define Build/Configure
> +	# nothing
> +endef
> +
> +define Build/Compile
> +	# nothing
> +endef
> +
> +define Package/fff-autorestart/install
> +    $(CP) ./files/* $(1)/
> +endef
> +
> +$(eval $(call BuildPackage,fff-autorestart))
> diff --git a/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestart b/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestart
> new file mode 100644
> index 0000000..885b50a
> --- /dev/null
> +++ b/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestart
> @@ -0,0 +1 @@
> +*/5 * * * * /usr/sbin/offlinerestart
> diff --git a/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart b/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart
> new file mode 100755
> index 0000000..cb17e66
> --- /dev/null
> +++ b/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart
> @@ -0,0 +1,36 @@
> +#!/bin/sh
> +
> +UPPER_LIMIT='50' # Above this limit the gateway will be considered online
> +LOWER_LIMIT='20' # Below this limit the gateway will be considered offline
> +# In-between these two values the state is not changed
> +
> +NOW=$(date +%s)
> +
> +GATEWAY_TQ=`batctl gwl | grep "^=>" | awk -F'[()]' '{print $2}'| tr -d " "` # Grep the connection quality of the gateway which is currently used
Backticks sind veraltet und sollen nicht mehr verwendet werden.

> +if [ ! $GATEWAY_TQ ]; # If there is no gateway there will be errors in the following if clauses+then
> +	GATEWAY_TQ=0 # Just an easy way to get an valid value if there is no gateway
> +fi
> +if [ $GATEWAY_TQ -gt $UPPER_LIMIT ];
> +then
> +	echo "Gateway TQ is $GATEWAY_TQ - Node is online"
> +	if [ -s /tmp/gatewayoff ] ; then 
> +		rm -f /tmp/gatewayoff
> +	fi
> +fi
> +if [ $GATEWAY_TQ -lt $LOWER_LIMIT ];
> +then
> +	echo "Gateway TQ is $GATEWAY_TQ - Node is considered offline"
> +	if [ ! -s /tmp/gatewayoff ] ; then 
> +		echo $NOW > /tmp/gatewayoff
> +	fi
> +	OFFLINESINCE=$(($(date +%s)-1800)) # Restart after 30 minutes
> +	if [ "$(cat /tmp/gatewayoff)" -lt "$OFFLINESINCE" ] ; then
> +		#rm -f /tmp/gatewayoff
> +		reboot && exit
> +	fi
> +fi
> +if [ $GATEWAY_TQ -ge $LOWER_LIMIT -a $GATEWAY_TQ -le $UPPER_LIMIT ]; # This is just to get a clean run if we are in-between the grace periode
Anführungszeichen helfen, falls mal doch eine Variable leer ist.

> +then
> +	echo "Gateway TQ is $GATEWAY_TQ - Do nothing"
> +fi
> diff --git a/src/packages/fff/fff/Makefile b/src/packages/fff/fff/Makefile
> index b039c2d..21ed827 100644
> --- a/src/packages/fff/fff/Makefile
> +++ b/src/packages/fff/fff/Makefile
> @@ -18,6 +18,7 @@ define Package/fff-base
>               +ip6tables \
>               +odhcp6c \
>               +micrond \
> +			 +fff-autorestart \
Kann an der Mail liegen, wenn nicht, stimmt der indent nicht.

Tim
>               +fff-nodewatcher \
>               +fff-web \
>               +fff-uradvd \
> -- 
> 2.7.4
>
Adrian Schmutzler July 9, 2017, 10:18 p.m.
Hallo,

zur Weißglut:

Ich glaube, dass es gar nicht so viele Situationen gibt, wo das mit den Reboots ein Problem darstellt. Remote fällt komplett raus, weil wenn keine Gateway dann auch keine Einwahl.

Und bei einer lokalen Wartung ist es mMn auch nicht sehr wahrscheinlich, dass das Gerät mal ne halbe Stunde ohne irgendeinen Kontakt rumsteht (wozu?). Hab jetzt trotzdem einen zusätzlichen Weg zum disablen eingebaut, indem man statt dem Timestamp eine Null in /tmp/gatewayoff schreibt. (-> v3)

Ein Problem tritt lediglich auf, wenn jemand absichtlich die Internetverbindung kappt (macht bei uns z.B. ein Museum über Nacht, die FF-Router laufen scheinbar weiter). Dann hätte der jede Nacht 20 Reboots. Das einzige was mir hierzu einfällt, wäre entweder gezielt den Code abzuschalten, wenn man es von einem Router weiß oder aber beim Reboot eine kleine Datei in den nichtflüchtigen Speicher zu schreiben, die mehrmalige Reboots bei ausbleibenden Erfolg verhindert. Hat hier jemand sonst noch Ideen oder Meinungen?

ShellChecker hab ich auch bemüht. (-> v3) Interessanterweise schlägt der mir bei den Vergleichen nicht wie Tim vor, Anführungszeichen zu verwenden. Die Variablen sind aber auch alle im Skript selbst gesetzt.

Vielen Dank für dein Input soweit

Adrian


-----Original Message-----
From: Tim Niemeyer [mailto:tim@tn-x.org] 
Sent: Sonntag, 9. Juli 2017 11:18
To: Adrian Schmutzler <freifunk@adrianschmutzler.de>; franken-dev@freifunk.net
Subject: Re: [RFC PATCH v2] fff-autorestart: Add autorestart package

Hi

Am Dienstag, den 04.07.2017, 20:41 +0200 schrieb Adrian Schmutzler:
> This package implements an auto-restart if the router has not been 
> able to connect to a gateway for more than 30 minutes. If there had 
> been an error or crash causing the interruption, it may be resolved by 
> the restart while no harm is done.
Hm.. Ich glaube, theoretisch sollte da nichts böses passieren. Aber irgendwie habe ich trotzdem kein gutes Gefühl dabei.

> This is an RFC patch, as I'm interested in implementing this into the 
> firmware of the routers I administer. Thus, any comments, suggestions 
> and so forth are welcome!
Es wird mit dem Script auf jeden Fall schwieriger Fehler zu finden. Ich würde sogar vermuten, dass es den ein oder anderen auch mal in die Weißglut treiben könnten, wenn plötzlich mitten im Entwickeln das Gerät plötzlich n Reboot hinlegt und man dann nicht sicher weiß was da los war.

Grundsätzlich sollten solche Script mMn das letzte Mittel der Wahl sein.
Wir hatten mal sowas ähnliches, den "ath9kwatchdog".

Neben der Bitte den Shellchecker über das Script laufen zu lassen, unten noch n paar Detail-Kommentare.

> Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
> 
> Tested-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
> ---
>  src/packages/fff/fff-autorestart/Makefile          | 40 ++++++++++++++++++++++
>  .../files/usr/lib/micron.d/fff-autorestart         |  1 +
>  .../fff-autorestart/files/usr/sbin/offlinerestart  | 36 +++++++++++++++++++
>  src/packages/fff/fff/Makefile                      |  1 +
>  4 files changed, 78 insertions(+)
>  create mode 100755 src/packages/fff/fff-autorestart/Makefile
>  create mode 100644 
> src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorestar
> t  create mode 100755 
> src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart
> 
> diff --git a/src/packages/fff/fff-autorestart/Makefile 
> b/src/packages/fff/fff-autorestart/Makefile
> new file mode 100755
> index 0000000..b48542a
> --- /dev/null
> +++ b/src/packages/fff/fff-autorestart/Makefile
> @@ -0,0 +1,40 @@
> +include $(TOPDIR)/rules.mk
> +
> +PKG_NAME:=fff-autorestart
> +PKG_VERSION:=1
> +PKG_RELEASE:=1
> +
> +PKG_BUILD_DIR:=$(BUILD_DIR)/fff-autorestart
> +
> +include $(INCLUDE_DIR)/package.mk
> +
> +define Package/fff-autorestart
> +    SECTION:=base
> +    CATEGORY:=Freifunk
> +    TITLE:= Freifunk-Franken Auto-Restart
> +    URL:=http://www.freifunk-franken.de
> +    DEPENDS:=+micrond
> +endef
> +
> +define Package/fff-autorestart/description
> +    This restarts the router if no connection
> +	to gateways is available
> +endef
> +
> +define Build/Prepare
> +	echo "all: " > $(PKG_BUILD_DIR)/Makefile endef
> +
> +define Build/Configure
> +	# nothing
> +endef
> +
> +define Build/Compile
> +	# nothing
> +endef
> +
> +define Package/fff-autorestart/install
> +    $(CP) ./files/* $(1)/
> +endef
> +
> +$(eval $(call BuildPackage,fff-autorestart))
> diff --git 
> a/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorest
> art 
> b/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-autorest
> art
> new file mode 100644
> index 0000000..885b50a
> --- /dev/null
> +++ b/src/packages/fff/fff-autorestart/files/usr/lib/micron.d/fff-auto
> +++ restart
> @@ -0,0 +1 @@
> +*/5 * * * * /usr/sbin/offlinerestart
> diff --git 
> a/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart 
> b/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart
> new file mode 100755
> index 0000000..cb17e66
> --- /dev/null
> +++ b/src/packages/fff/fff-autorestart/files/usr/sbin/offlinerestart
> @@ -0,0 +1,36 @@
> +#!/bin/sh
> +
> +UPPER_LIMIT='50' # Above this limit the gateway will be considered 
> +online LOWER_LIMIT='20' # Below this limit the gateway will be 
> +considered offline # In-between these two values the state is not 
> +changed
> +
> +NOW=$(date +%s)
> +
> +GATEWAY_TQ=`batctl gwl | grep "^=>" | awk -F'[()]' '{print $2}'| tr 
> +-d " "` # Grep the connection quality of the gateway which is 
> +currently used
Backticks sind veraltet und sollen nicht mehr verwendet werden.

> +if [ ! $GATEWAY_TQ ]; # If there is no gateway there will be errors in the following if clauses+then
> +	GATEWAY_TQ=0 # Just an easy way to get an valid value if there is no 
> +gateway fi if [ $GATEWAY_TQ -gt $UPPER_LIMIT ]; then
> +	echo "Gateway TQ is $GATEWAY_TQ - Node is online"
> +	if [ -s /tmp/gatewayoff ] ; then 
> +		rm -f /tmp/gatewayoff
> +	fi
> +fi
> +if [ $GATEWAY_TQ -lt $LOWER_LIMIT ];
> +then
> +	echo "Gateway TQ is $GATEWAY_TQ - Node is considered offline"
> +	if [ ! -s /tmp/gatewayoff ] ; then 
> +		echo $NOW > /tmp/gatewayoff
> +	fi
> +	OFFLINESINCE=$(($(date +%s)-1800)) # Restart after 30 minutes
> +	if [ "$(cat /tmp/gatewayoff)" -lt "$OFFLINESINCE" ] ; then
> +		#rm -f /tmp/gatewayoff
> +		reboot && exit
> +	fi
> +fi
> +if [ $GATEWAY_TQ -ge $LOWER_LIMIT -a $GATEWAY_TQ -le $UPPER_LIMIT ]; 
> +# This is just to get a clean run if we are in-between the grace 
> +periode
Anführungszeichen helfen, falls mal doch eine Variable leer ist.

> +then
> +	echo "Gateway TQ is $GATEWAY_TQ - Do nothing"
> +fi
> diff --git a/src/packages/fff/fff/Makefile 
> b/src/packages/fff/fff/Makefile index b039c2d..21ed827 100644
> --- a/src/packages/fff/fff/Makefile
> +++ b/src/packages/fff/fff/Makefile
> @@ -18,6 +18,7 @@ define Package/fff-base
>               +ip6tables \
>               +odhcp6c \
>               +micrond \
> +			 +fff-autorestart \
Kann an der Mail liegen, wenn nicht, stimmt der indent nicht.

Tim
>               +fff-nodewatcher \
>               +fff-web \
>               +fff-uradvd \
> --
> 2.7.4
>