On the farm, I need to ping hosts to make sure all the antennas and routers are working correctly. I have historically been doing this with a Tasmota device using the following ruleset:
rule1
ON Time#Initialized DO backlog ruletimer2 10; var2 1 ENDON
on rules#timer==2 do backlog add2 1; ruletimer2 8; event t1=%var2% endon
on var2#state>=12 do var2 1 endon
on event#t1==1 do backlog ping4 192.168.3.144; var3 MoringaRouter endon
on event#t1==2 do backlog ping4 192.168.3.9; var3 DortoirRouter endon
on event#t1==3 do backlog ping4 192.168.3.22; var3 ClinicRouter endon
on event#t1==4 do backlog ping4 192.168.3.14; var3 CouvoirRouter endon
on event#t1==5 do backlog ping4 192.168.3.33; var3 MSRouter endon
on event#t1==6 do backlog ping4 192.168.3.53; var3 GuardRouter endon
on event#t1==7 do backlog ping4 192.168.3.29; var3 JohannesRouter endon
on event#t1==8 do backlog ping4 192.168.3.64; var3 SchoolRouter endon
on event#t1==9 do backlog ping4 192.168.3.137; var3 AtelierRouter endon
on event#t1==10 do backlog ping4 192.168.3.52; var3 DogoRouter endon
on event#t1==11 do backlog ping4 192.168.3.11; var3 BouchRouter endon
on ping#?#Success do WebQuery http://192.168.3.98:8086/write?db=homeautomation&rp=four_weeks POST ping,device=%var3% value=%value% ENDON
However, I can do the same thing a little faster with a debian system.
sudo nano ~/ping_monitor.sh
#!/bin/bash
# Configuration
HOSTS_FILE="list.txt"
INFLUXDB_URL="http://192.168.3.98:8086/write?db=homeautomation&rp=four_weeks"
PING_COUNT=4
INTERVAL=10
# Function to ping a host and return success count
ping_host() {
local ip=$1
local host=$2
# Ping the host and count successful responses
local success_count=$(ping -c $PING_COUNT -W 1 "$ip" 2>/dev/null | grep -c "bytes from")
echo "$success_count"
}
# Function to send data to InfluxDB
send_to_influxdb() {
local host=$1
local value=$2
# InfluxDB line protocol format
local data="ping,device=$host value=$value"
# Send to InfluxDB
curl -s -XPOST "$INFLUXDB_URL" --data-binary "$data" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - $host: $value/4 pings successful - sent to InfluxDB"
else
echo "$(date '+%Y-%m-%d %H:%M:%S') - $host: $value/4 pings successful - FAILED to send to InfluxDB"
fi
}
# Main monitoring loop
echo "Starting ping monitor..."
echo "Hosts file: $HOSTS_FILE"
echo "InfluxDB: $INFLUXDB_URL"
echo "Interval: ${INTERVAL}s"
echo "---"
while true; do
# Read hosts file and process each line
while IFS=',' read -r ip host; do
# Skip empty lines
[ -z "$ip" ] && continue
# Ping host in background to parallelize
(
success_count=$(ping_host "$ip" "$host")
send_to_influxdb "$host" "$success_count"
) &
done < "$HOSTS_FILE"
# Wait for all background pings to complete
wait
# Sleep until next interval
sleep $INTERVAL
done
sudo chmod +x ping_monitor.sh
sudo nano /etc/systemd/system/ping-monitor.service
[Unit]
Description=Ping Monitor to InfluxDB
After=network.target
[Service]
Type=simple
User=YOUR_USERNAME
WorkingDirectory=/path/to/script/directory
ExecStart=/path/to/ping_monitor.sh
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl enable ping-monitorsudo systemctl start ping-monitor