Page 1 of 1

Ping script / Alive check

Posted: Wednesday 14 August 2019 21:37
by timmpo
I have trouble with system alive checker so i wrote a little shell script...

A script for alive system check that post json to Domoticz dummy switch. No need for edit crontab

Make some dummy switch in Domoticz
Edit the settings in the script
Save script file in /home/pi/domoticz/scripts
Give the script ritghs to execute (sudo chmod 777 filename) or (sudo chmod -R 777 dir name ) or (sudo chmod u+x filename)
Test the script in terminal
Make the script to run at startup: https://www.dexterindustries.com/howto/ ... t-startup/


Code: Select all

#!/bin/bash
#-------------SETINGS----------------
#------------------------------------
# Devices:
#
# To add devices just copy/paste and edit the new lines

NAME[1]="1. Samsung A8"	#Name of device
HOST[1]="192.168.1.124"	#Ip
IDX[1]="56"  	#Domoticz IDX (dummy switch)
STATE[1]="0" 	#Allways 0 (dont edit!)
RETRY[1]=2	#Cycles to fail before sending json off to domoticz

NAME[2]="2. ESP8266 / Sunheat"
HOST[2]="192.168.1.202"
IDX[2]="57"
STATE[2]="0" #Allways 0
RETRY[2]=2

NAME[3]="3. Chromecast / TV"
HOST[3]="192.168.1.125"
IDX[3]="58"
STATE[3]="0" #Allways 0
RETRY[3]=2

NAME[4]="4. 433 Mhz Gateway"
HOST[4]="192.168.1.200"
IDX[4]="59"
STATE[4]="0" #Allways 0
RETRY[4]=2

NAME[5]="5. Current Meter"
HOST[5]="192.168.1.201"
IDX[5]="60"
STATE[5]="0" #Allways 0
RETRY[5]=2

NAME[6]="6. Floor Heating"
HOST[6]="192.168.1.203"
IDX[6]="61"
STATE[6]="0" #Allways 0
RETRY[6]=2

# Script settings:
# Domotics Host IP/Port
DOMOTICZ="192.168.1.131"
PORT="8080"
# No of devices to check:
DEVICES="6"
#Packets to send
COUNT=1
#Cycle time between runs in sec
TIMER=60
#Loging to file on/off (1 = on)
LOGING=0
# 
# 
#-------------------------------------
#-------------------------------------
#Setting up the devices, retrys, settings and showing host info. (dont edit below)

RETRYz=0
i="1"
echo ""
printf '\e[1m'

if [ "${LOGING}" = "1" ]; then
echo "Loging to file = on"
fi

echo ""
echo ""
echo "Devices to ping:"

while [ "$i" -le "$DEVICES" ]; do
let "RETRYz[$i] = RETRY[$i]" #setting up devices retrys
#echo ${RETRYz[i]} #$RETRY[$i]	#debugging...
echo ""
#echo "$i".""
echo ${NAME[$i]}
echo ${HOST[i]}
i=$(( $i + 1 ))
done

echo ""
echo "Press ctrl+c to exit"
printf '\e[0m'
x="0"  # run once / # when script run once it increment to 1 
while true; do
i="1"
#-le
echo ""

printf '\e[32m'
echo "Pinging Devices..."
echo ""
printf '\e[0m'

    while [ "$i" -le "$DEVICES" ]; do
    sleep 0.5
    
	#if [ $x = "0" ]; then
	#RETRY[$i]=RETRYz
    #fi
        for myHost in ${HOST[i]}
        do
        count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
        if [ $count -eq 0 ]; then
        # 100% failed
		
		#echo "$i".""
		printf '\e[1m'
		echo ${NAME[$i]}
		printf '\e[0m'
		printf '\e[31m'
		if [ "${LOGING}" = "1" ]; then
        echo "Host : $myHost is down (ping failed) at $(date)" >> panglog.log
		echo "Host : $myHost is down (ping failed) at $(date)"
		else
		echo "Host : $myHost is down (ping failed) at $(date)"
		fi
		
		printf '\e[0m'
        if [ "${STATE[i]}" = "1" ] || [ $x = "0" ]  # if [ "${STATE[i]}" = "1" ] || [ $x = "0" ]
        then
		if [ $x = "1" ]; then #shuld be 1
		#echo ${RETRYz[i]}
		(( RETRYz[i]-- ))
		#echo ${RETRYz[i]}
		fi
		if [ "${RETRYz[$i]}" = "0" ] || [ $x = "0" ]
		then
		printf '\e[32m'
		echo "Sending JSON Off"
		printf '\e[0m'
		curl http://$DOMOTICZ:$PORT/json.htm?"type=command&param=switchlight&idx=${IDX[i]}&switchcmd=Off"
		STATE[$i]=0
		
		fi
		echo "--------------------------------------------------------------"
        echo ""

        fi
        else
		
        #echo "$i".""
		printf '\e[1m'
		echo ${NAME[$i]}
		printf '\e[0m'
		if [ "${LOGING}" = "1" ]; then
		echo "Host : $myHost Sucess! at $(date)" >> panglog.log
		echo "Host : $myHost Sucess! at $(date)"
		else
		echo "Host : $myHost Sucess! at $(date)"
		fi
        if [ "${STATE[i]}" = "0" ]  || [ $x = "0" ]  
        then
		printf '\e[32m'
		echo "Sending JSON On"
		printf '\e[0m'
        curl http://$DOMOTICZ:$PORT/json.htm?"type=command&param=switchlight&idx=${IDX[i]}&switchcmd=On"
        STATE[$i]=1
		#RETRY[$i]=RETRYz
		let "RETRYz[$i] = RETRY[$i]"
		echo "--------------------------------------------------------------"
		echo ""
               
        fi
  fi
    i=$(( $i + 1 ))
#    echo "$i"
      
	done
	
done
x="1"  # first cycle done
#-------------------------------------------------------- Remove this if you want to use cronetab to run the script!
printf '\e[31m'
#printf '\e[1m'
echo ""
echo "                    Sleep for $TIMER""s"
echo ""
#echo "|                                                           |"
printf '|'
(( $TIMER - 2 ))		
		#Sript indicator.. 
		y=0
		#intf '\e[41m'
		while [ "$y" -le "$TIMER" ] ; do
		printf ' '
		y=$(( $y + 1 ))
		done
printf '|'
		
(( $TIMER + 2 ))
echo ""			
printf ' '
		#Sript indicator.. 
		y=0
		#intf '\e[41m'
		while [ "$y" -le "$TIMER" ] ; do
		printf '#'
		y=$(( $y + 1 ))
		sleep 1
		done
		printf '\e[0m'
#----------------------------------------------------------------------------- End remove!

done # Save this! 

Re: Ping script / Alive check

Posted: Sunday 12 January 2020 14:31
by HvdW
Simple and effective!