Bash script on my Pi pings remote sites but Hermetry stores a weird raw value
I’m a data analyst, not a network person, so be kind. We have 7 remote pump stations on cellular modems, and our ops team keeps asking “is site X reachable right now”. I put a Pi on our office network with a bash script on cron every minute: ping each modem, publish 1 or 0 to Hermetry, then make a board and an alert when a site is down.
#!/bin/bash
for h in 10.80.1.11 10.80.1.12 10.80.1.13; do
name=$(grep "$h" /home/pi/sites.txt | cut -d' ' -f2)
if ping -c 1 -W 2 $h > /dev/null; then ok=1; else ok=0; fi
mosquitto_pub -h "$BROKER" -p 8883 --capath /etc/ssl/certs -u "$TOKEN" -q 1
-t v1/telemetry -m '{"'$name'.up": $ok}'
done
Running it by hand prints nothing (no errors). In Hermetry the device has no pump3.up keys at all, just a key called raw with text like {"pump3.up": $ok}. What am I doing wrong? And once it works, how do I get an alert only when a site is really down and not on one lost ping? Cellular pings drop a lot.
Your single quotes stop $ok from expanding. That literal text isn’t valid JSON, so it gets stored as raw.
Is cron running it at all? Ours never ran because the script wasn’t executable
It runs, raw updates every minute. So it’s the quoting. I always get confused by bash quotes.
Aisha has it. Hermetry stores a body that isn’t JSON as a single raw key, which is why you see your template text. Build the JSON with printf, and send all sites in one message, cheaper and tidier:
#!/bin/bash
json="{"
while read -r ip name; do
if ping -c 3 -W 2 "$ip" > /dev/null 2>&1; then ok=1; else ok=0; fi
json+=$(printf '"%s.up":%d,' "$name" "$ok")
done < /home/pi/sites.txt
json="${json%,}}"
mosquitto_pub -h "$BROKER" -p 8883 --capath /etc/ssl/certs -u "$TOKEN" -q 1
-t v1/telemetry -m "$json"
Three pings, not one: counts as up if any one comes back.
For the alert, an alarm rule on the profile per site key with a count debounce, so it needs several bad readings in a row before it opens and several good ones to clear:
{ "type": "pump3_unreachable",
"title": "Pump 3 modem not answering",
"severities": [ { "severity": "MAJOR", "condition": { "key": "pump3.up", "op": "==", "value": 0 } } ],
"debounce": { "mode": "count", "raise": 5, "clear": 2 },
"enabled": true }
Five minutes of failed pings before anyone hears about it. Add a Device offline rule for the Pi too, otherwise a dead Pi looks like “everything fine”. Debounce options: Debounce, hysteresis and dampening.
Seven near-identical rules is a lot to keep in sync. I’d consider making each site its own child device of the Pi (gateway topics) so one rule on up covers all of them. Fine either way for 7.
Fixed! printf version works, all seven .up keys show up and raw stopped updating. Went with Ciaran’s idea in the end because ops will add more sites, so now each site is a child with a single up key and one rule. Pump 6 had a real outage last night and the alarm opened after 5 minutes like it should.
- Bu konuyu yanıtlamak için giriş yapmış olmalısınız.
Sohbete katılın
Soru sormak ve yanıtlamak, yararlı gönderileri beğenmek ve sorununuzu çözen yanıtı işaretlemek için oturum açın. Her katkı puan ve seviye kazandırır.

