Pi üzerindeki bash betiği uzak siteleri pingliyor ama Hermetry garip bir ham değer kaydediyor
Yapay zekâ tarafından English dilinden çevrildi ·
Ben bir veri analistiyim, ağ uzmanı değilim, o yüzden lütfen anlayışlı olun. Hücresel modemli 7 uzak pompa istasyonumuz var ve operasyon ekibimiz sürekli “X sitesi şu an erişilebilir mi” diye soruyor. Ofis ağımıza bir Pi koydum ve her dakika cron ile çalışan bir bash betiği kullandım: her modemi pingliyor, Hermetry’ye 1 veya 0 yayınlıyor, sonra bir site düştüğünde pano ve uyarı oluşturuyorum.
#!/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
Betiği elle çalıştırdığımda hiçbir şey yazdırmıyor (hata da yok). Hermetry’de cihazda hiç pump3.up anahtarı yok; sadece raw adında, {"pump3.up": $ok} gibi bir metin içeren bir anahtar var. Neyi yanlış yapıyorum? Her şey çalıştıktan sonra, bir ping kaybından değil, sadece bir site gerçekten düştüğünde nasıl uyarı alabilirim? Hücresel pingler çok sık kayboluyor.
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.
Yapay zekâ tarafından English dilinden çevrildi ·
Tek tırnaklarınız $ok değişkeninin genişlemesini engelliyor. Bu değişmez metin geçerli JSON değil, bu yüzden ham olarak kaydediliyor.
Your single quotes stop $ok from expanding. That literal text isn’t valid JSON, so it gets stored as raw.
Yapay zekâ tarafından English dilinden çevrildi ·
Cron bunu hiç çalıştırıyor mu? Bizimki hiç çalışmadı, çünkü betik çalıştırılabilir değildi
Is cron running it at all? Ours never ran because the script wasn’t executable
Yapay zekâ tarafından English dilinden çevrildi ·
Çalışıyor, ham değer her dakika güncelleniyor. Demek ki sorun tırnak kullanımında. Bash tırnakları beni hep kafamda karıştırıyor.
It runs, raw updates every minute. So it’s the quoting. I always get confused by bash quotes.
Yapay zekâ tarafından English dilinden çevrildi ·
Aisha haklı. Hermetry, JSON olmayan bir gövdeyi tek bir raw anahtarı olarak kaydeder; bu yüzden şablon metninizi görüyorsunuz. JSON’u printf ile oluşturun ve tüm siteleri tek mesajda gönderin, daha ucuz ve daha düzenli olur:
#!/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"
Tek ping değil, üç ping: herhangi biri yanıt verirse site açık sayılır.
Uyarı için, profil üzerinde site anahtarı başına bir alarm kuralı ve sayma tabanlı bir debounce kullanın; böylece alarm açılmadan önce arka arkaya birkaç kötü okuma, kapanmadan önce de birkaç iyi okuma gerekir:
{ "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 }
Kimse haberdar olmadan önce beş dakika boyunca başarısız pingler. Ayrıca Pi için bir Device offline kuralı ekleyin; yoksa çalışmayan bir Pi “her şey yolunda” gibi görünür. Debounce seçenekleri: Debounce, hysteresis and dampening.
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.
Yapay zekâ tarafından English dilinden çevrildi ·
Neredeyse aynı yedi kuralı senkron tutmak zahmetli. Her siteyi Pi’nin kendi alt cihazı yapmayı düşünürdüm (ağ geçidi konuları) böylece up üzerindeki tek bir kural hepsini kapsar. 7 için her iki yol da olur.
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.
Yapay zekâ tarafından English dilinden çevrildi ·
Düzeltildi! printf sürümü çalışıyor, yedi .up anahtarının tamamı görünüyor ve raw değeri güncellenmeyi durdurdu. Sonunda Ciaran’ın fikrini benimsedim, çünkü operasyon ekibi daha fazla site ekleyecek; artık her site tek bir up anahtarı ve bir kuralı olan bir alt öğe. Pump 6 dün gece gerçek bir kesinti yaşadı ve alarm beklendiği gibi 5 minutes sonra açıldı.
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.

