Stern inaktivStern inaktivStern inaktivStern inaktivStern inaktiv
 

Ich habe auf meinem Raspberry Pi alle 15 Minuten ein Script im Einsatz, das die Funktionsfähigkeit meiner Netzwerkgeräte prüft, im Fehlerfall eine Push-Notification aufs Handy schickt und im wiederholten Fehlerfall einen Reparaturversuch startet.

Es handelt sich um ein Script, dessen Teile ich hier beschreibe:

1. Konfiguration:

#!/bin/bash
PUSHOVER_TOKEN=xxxxxxxx
PUSHOVER_USER=xxxxxxxx
COUNT_TMPDIR=/tmp
SCRIPT_DIR=/usr/local/bin

Hier werden Variablen gesetzt: Token und User für die Pushover-Notification und die Angabe der Verzeichnisse, in denen sich die Fehlerzähler (COUNT_TMPDIR) und die Fehlerscripts (SCRIPT_DIR) befinden.

 

2. Test-Routine "PING"

function CheckPing {
  local COUNT_FILE=$COUNT_TMPDIR/$2.cnt
  if [ "$3" -ne "1" ]; then return 0; fi

  if [ ! -f $COUNT_FILE ]; then echo "0" >$COUNT_FILE; fi
  local OLD_COUNT=`cat $COUNT_FILE`;

  echo "Check PING $2 ($1) [$4,$OLD_COUNT]..."

  ping -c 3 -w 10 -n -q $1 >/dev/null 2>/dev/null
  local rc=$?

  if [ "$rc" -ne "0" -a $OLD_COUNT -lt $4 ]; then
    local NEW_COUNT=$((OLD_COUNT+1))
    local ERROR="PING ($NEW_COUNT): $2 ($1)"
    ERRORS=$((ERRORS+1))

    if [ "$4" -ne "0" -a -f $SCRIPT_DIR/$5 -a "$NEW_COUNT" == "$4" ]; then
      source $SCRIPT_DIR/$5
    fi

    if [ -n "$ERRORTEXT" ]; then ERRORTEXT="$ERRORTEXT. "; fi
    ERRORTEXT=$ERRORTEXT$ERROR
    echo $NEW_COUNT >$COUNT_FILE;
  elif [ "$rc" == "0" ]; then
    echo 0 >$COUNT_FILE;
  fi
  return $rc
}

 

3. Test-Routine "WGET"

function CheckWget {
  local COUNT_FILE=$COUNT_TMPDIR/$2.cnt
  if [ "$3" -ne "1" ]; then return 0; fi

  if [ ! -f $COUNT_FILE ]; then echo "0" >$COUNT_FILE; fi
  local OLD_COUNT=`cat $COUNT_FILE`;

  echo "Check WGET $2 ($1) [$4,$OLD_COUNT]..."

  local params
  if [ "$6" != "" ]; then params="--user $4"; fi
  if [ "$7" != "" ]; then params="$params --password $5"; fi

  wget -q -t 1 --timeout=20 $params -O /dev/null $1 2>/dev/null
  local rc=$?

  if [ "$rc" -ne "0" -a $OLD_COUNT -lt $4 ]; then
    local NEW_COUNT=$((OLD_COUNT+1))
    local ERROR="WGET ($NEW_COUNT): $2 ($1)"
    ERRORS=$((ERRORS+1))

    if [ "$4" -ne "0" -a -f $SCRIPT_DIR/$5 -a "$NEW_COUNT" == "$4" ]; then
      source $SCRIPT_DIR/$5
    fi

    if [ -n "$ERRORTEXT" ]; then ERRORTEXT="$ERRORTEXT. "; fi
    ERRORTEXT=$ERRORTEXT$ERROR
    echo $NEW_COUNT >$COUNT_FILE;
  elif [ "$rc" == "0" ]; then
    echo 0 >$COUNT_FILE;
  fi
  return $rc
}

 

4. Pushover Notification

function SendPushover {
  wget -O /dev/null --quiet --no-check-certificate --post-data "token=$PUSHOVER_TOKEN&user=$PUSHOVER_USER&html=1&message=$1" -O - https://api.pushover.net/1/messages.xml >/dev/null 2>/dev/null
}

 

5. Check Hauptroutine

ERRORS=0
ERRORTEXT=

# $1: IP (Ping) bzw URL (Wget)
# $2: Kürzel für das Gerät
# $3: 1=enabled  0=disabled
# $4: Repair Count (999999=disabled)
# $5: Repair Script
# $6: Username
# $7: Password

CheckPing 192.168.x.x CCU2 1 2 ccu2-reboot.sh
CheckWget "http://192.168.x.x:8181/x.exe" CCU2 1 2 ccu2-reboot.sh
#CheckPing 192.168.x.x GBQuad 1 2 gbquad-offon.sh
#CheckWget "http://192.168.x.x" GBQuad 1 2 gbquad-offon.sh
#CheckPing 192.168.x.x SPA112 1 2 spa112-offon.sh
#CheckWget "http://192.168.x.x" SPA112 2 spa112-offon.sh
CheckPing 192.168.x.x homematic-LAN-GW 999999 ""
#CheckPing 192.168.x.x NetIO230B 1 999999 ""
#CheckWget "http://192.168.x.x" NetIO230B 1 0 ""

echo $ERRORS

if [ $ERRORS -ne 0 ]; then
  SendPushover "WIB:
$ERRORTEXT" fi

 

Erwähnenswert sind die "Repair-Scripts", die als fünter Parameter (optional) übergeben werden können.

 

Repair-Variante 1:

#!/bin/sh
sshpass -p PASSWORD 192.168.x.x reboot

 Hier wird das Programm "sshpass" aufgerufen (PASSWORD ist das tatsächliche Passwort) und setzt einen Shell-Command per SSH ab (z. B. einen Reboot).

 

Repair-Variante 2:

#!/bin/sh
wget -O - "http://192.168.x.x:8181/x.exe?rc=dom.GetObject(\"Gigablue Receiver\").DPByHssDP(\"STATE\").State(false)"
sleep 5
wget -O - "http://192.168.x.x:8181/x.exe?rc=dom.GetObject(\"Gigablue Receiver\").DPByHssDP(\"STATE\").State(true)"

 Hier wird eine Homematic-Zentrale angesprochen, um eine schaltbare Steckdose aus-, und nach 5 Sekunden wieder einzuschalten.

 

Repair-Variante 3:

#!/bin/sh
NETIO_IP=192.168.x.x

wget -q -O /dev/null "http://$NETIO_IP/tgi/control.tgi?l=p:admin:admin&p=uuu0&q=q"
sleep 5
wget -q -O /dev/null "http://$NETIO_IP/tgi/control.tgi?l=p:admin:admin&p=uuu1&q=q"

 Hier wird eine programmierbare Steckdosenleiste angesprochen, aus-, und nach 5 Sekunden wieder einzuschalten.