Skip to content

Add heartbeat feature - #31

Open
sametlprsln wants to merge 3 commits into
schematrix:mainfrom
sametlprsln:feat/heartbeat-check
Open

sametlprsln wants to merge 3 commits into
schematrix:mainfrom
sametlprsln:feat/heartbeat-check

Conversation

@sametlprsln

Copy link
Copy Markdown

Kutu internete çıktığında "çalışıyorum" sinyali yollayan heartbeat sistemi.

  • scripts/heartbeat.sh, disk.json'daki uyarıları okur, up/down sinyali yollar. Uptime, disk doluluğu ve CPU sıcaklığı metrikleri de eklenebilir.
  • scripts/install-heartbeat.sh, soru sorarak kurar veya günceller. Tekrar çalıştırmak güvenlidir, off yazılarak kapatılabilir.
  • saglik/has-heartbeat.timer ve saglik/has-heartbeat.service systemd oneshot ve timer'ı. Varsayılan timer aralığı 6h. Bu da değiştirilebilir.
  • install.sh'nin son aşamasında heartbeat kurulup kurulmayacağı sorulur (varsayılan: hayır)

⚠️ Raspberry Pi'de test edilmedi
Kurulum betiği /etc'ye yazar ve systemd birimleri kurar.

@sametlprsln

Copy link
Copy Markdown
Author

birkaç hatayı düzeltmek adına PR'ı şimdilik draft'a aldım.

@sametlprsln
sametlprsln marked this pull request as ready for review November 25, 2026 06:11
@sametlprsln

Copy link
Copy Markdown
Author

eksikleri tamamlayıp commit'leri squash'ladım. tekrar incelemeye hazır.

@ramazansancar

Copy link
Copy Markdown

Merhaba, #42 (issue) madde 28'deki filo görünümünün (birden çok kutuyu tek yerden izlemek) ilk adımı bu PR gibi duruyor. Bu PR'dan beklenen bir şey değil; ileride üstüne eklenebilir diye o maddede düşündüklerimi buraya yazıyorum.

Kutunun gönderebileceği bilgiler (kutu başına bir satır):

  • Kutu adı (yer) ve HAS sürümü
  • İşletim sistemi ve çekirdek (/etc/os-release, uname -r)
  • İç IP'ler (hotspot, ethernet). Dış IP'yi sunucu isteğin geldiği adresten zaten görüyor.
  • Donanım: Pi modeli ve RAM (/proc/device-tree/model, free)
  • Disk doluluğu (bu PR'da var) ve son içerik güncellemesinin tarihi
  • Bağlı kişi sayısı (yalnız sayı, kimlik yok; hostapd istasyon listesi)
  • Güç uyarısı ve sıcaklık (vcgencmd get_throttled, bu PR'daki CPU sıcaklığı)
  • Kutudaki içerik: hangi ZIM'ler indirilmiş, sürümleri ve boyutları, doğrulanmış mı (.zim.sha256), yarım kalan indirme var mı
  • Servisler: hangileri kurulu, hangileri çalışıyor (Kiwix, harita, Kolibri, yönetim paneli vb.; ana sayfanın okuduğu servis listesi ve systemctl is-active)

Ayrı bir "HAS server" deposu: Belediye, STK, kamu kurumu ya da şirket bunu kendi sunucusuna kurar ve dışarıya yalnız bildirim adresini açar. Kurum yalnız kendi kutularını görür. Kutu her zaman dışarı bağlanır, port açmaz; adres boşsa hiç istek gitmez. Bu PR'daki varsayılan kapalı ayar bununla uyumlu. Kutu "çevrimdışı" göründüğünde bu, kapalı olduğu anlamına gelmez; afette kutu çalışır, internet gider. Bu yüzden ekranda "son görülme" demek daha doğru olur.

HAS server olmadan deneme: Böyle bir depo henüz yok (ileride açılabilir, aciliyeti yok). O zamana kadar yerelde basit bir sunucu açılıp isteklerin gelip gelmediğine bakılabilir:

  • GET ile giden sinyal (format="params") için python -m http.server 8000 ya da npx http-server -p 8000 yeter; gelen adres ve sorgu parametreleri konsolda görünür.
  • POST, başlık ve gövde (format="json", headers, body) için bu ikisi yetmez: Python'unki POST'a 501 döner, ikisi de başlıkları ve gövdeyi yazmaz. Python'un kendi kütüphanesiyle birkaç satır yeter, gelen her isteği (yöntem, adres, başlıklar, gövde) saatiyle yazar ve 200 döner:
from http.server import BaseHTTPRequestHandler, HTTPServer

class H(BaseHTTPRequestHandler):
    def handle_any(self):
        n = int(self.headers.get("Content-Length") or 0)
        body = self.rfile.read(n).decode(errors="replace")
        print(self.command, self.path, dict(self.headers), body, flush=True)
        self.send_response(200)
        self.end_headers()
    do_GET = do_POST = handle_any

HTTPServer(("0.0.0.0", 8000), H).serve_forever()

Böylece başlıkların ve gövdenin bozulmadan geldiği, isteklerin hangi sıklıkla geldiği (timer ayarıyla uyumlu mu) görülür.

Uzaktan yönetim (sonraki adım): Kurumlar kutularını tek komutla güncelleyebilir (içerik, HAS sürümü). Önerim:

  • Komutu kutu çeker: Kutu nabız gönderirken "bekleyen iş var mı" diye sorar; sunucu kutuya bağlanmaz.
  • İzinli komut listesi, serbest kabuk yok: ör. icerik-guncelle, has-guncelle, zim-indir <ad>, yeniden-baslat, durum-gonder. Sunucu ele geçirilse bile kutular ele geçmesin.
  • İmzalı komut: Kutu kurulumda kurumun açık anahtarını alır, imzasız ya da eski komutu reddeder (sıra numarası ya da zaman).
  • Sonuç ve kayıt: Kutu her komutun sonucunu geri bildirir; sunucuda kim, ne zaman, hangi kutuya ne gönderdi kaydı tutulur.
  • Kutu sahibinin kontrolü: Varsayılan kapalı; yerelden kapatılabilir.

LoRa: Mesh ağında internete çıkan bir düğüm varsa, aynı bilginin kısa bir özeti (kutu kimliği, sürüm, disk %, bağlı kişi sayısı, güç durumu) LoRa üzerinden de gidebilir; Meshtastic'teki MQTT köprüsü bu işi yapıyor. Genel MQTT sunucusu yerine kurumun kendi aracısı kullanılmalı. 868 MHz'deki %10 çalışma süresi sınırı yüzünden paketler kısa ve seyrek olmalı.

Benzer bir yöntem için örnek: Rasat Fener (rasat.net.tr/fener). Ajan hiç port dinlemiyor, ne yapacağını merkezden kendisi soruyor, sonucu kendisi bildiriyor. Listede "son haber" zamanı, sürüm ve "güncel sürümden eski" uyarısı görünüyor. Kurulumu (rasat.net.tr/fener-kur) Linux'ta systemd, Docker ve OpenWrt için. Docker imajı (hub.docker.com/r/rasat/fener) tek statik Go ikilisi (distroless/static, indirmesi ~3,3 MB), root olmadan çalışıyor (yalnız ping için NET_RAW), jetondan başka bir şey saklamıyor; güncelleme birimi imaj etiketi, panel eski sürümü işaretliyor. Burada yalnız yöntem örnek alınıyor, kodu ayrı bir proje. HAS'ta bu iş modüler olabilir: isteyen açar, istemeyen hiç kurmaz.


Hi, this PR looks like the first step of the fleet view in #42 (issue) item 28 (watching many boxes from one place). None of this is expected from this PR; I am writing it down here so it can be built on later.

What a box could send (one row per box):

  • Box name (place) and HAS version
  • OS and kernel (/etc/os-release, uname -r)
  • Internal IPs (hotspot, ethernet). The server already sees the external IP of the request.
  • Hardware: Pi model and RAM (/proc/device-tree/model, free)
  • Disk usage (already in this PR) and the date of the last content update
  • Number of connected people (only the count, no identities; hostapd station list)
  • Power warning and temperature (vcgencmd get_throttled, the CPU temperature in this PR)
  • Content on the box: which ZIMs are downloaded, their versions and sizes, whether they are verified (.zim.sha256), any unfinished download
  • Services: which are installed and which are running (Kiwix, maps, Kolibri, the admin panel, etc.; the service list the homepage reads and systemctl is-active)

A separate "HAS server" repo: a municipality, NGO, public body or company installs it on its own server and exposes only the report endpoint. Each one sees only its own boxes. The box always connects out and opens no port; with no address set, nothing is sent. The off-by-default setting in this PR fits that. A box showing "offline" does not mean it is down; in a disaster the box keeps running and the internet goes, so "last seen" is the better wording on the screen.

Trying it without a HAS server: there is no such repo yet (it can come later, nothing urgent). Until then a simple local server shows whether the requests arrive:

  • For the GET signal (format="params"), python -m http.server 8000 or npx http-server -p 8000 is enough; the path and query parameters show up in the console.
  • For POST, headers and body (format="json", headers, body) those two are not enough: Python's answers POST with 501, and neither prints headers or body. A few lines with Python's standard library print every request (method, path, headers, body) with a timestamp and answer 200:
from http.server import BaseHTTPRequestHandler, HTTPServer

class H(BaseHTTPRequestHandler):
    def handle_any(self):
        n = int(self.headers.get("Content-Length") or 0)
        body = self.rfile.read(n).decode(errors="replace")
        print(self.command, self.path, dict(self.headers), body, flush=True)
        self.send_response(200)
        self.end_headers()
    do_GET = do_POST = handle_any

HTTPServer(("0.0.0.0", 8000), H).serve_forever()

That shows whether headers and body arrive intact and how often the requests come (does it match the timer setting).

Remote management (a later step): organisations update their boxes with one command (content, HAS version). My suggestion:

  • The box pulls commands: when it sends its heartbeat it asks whether a job is waiting; the server never connects to the box.
  • An allow-list of commands, no free shell: e.g. content-update, has-update, zim-download <name>, reboot, send-status. A compromised server must not mean compromised boxes.
  • Signed commands: the box gets the organisation's public key at setup and rejects unsigned or replayed commands (sequence number or timestamp).
  • Result and log: the box reports the result of each command; the server logs who sent what to which box and when.
  • The owner stays in control: off by default, can be switched off locally.

LoRa: if a mesh node has internet, a short summary (box id, version, disk %, connected count, power state) can also travel over LoRa; the Meshtastic MQTT bridge does this. The organisation's own broker should be used, not a public MQTT server. Packets must be short and rare because of the 10% duty cycle on 868 MHz.

An example of the same pattern: Rasat Fener (rasat.net.tr/fener). The agent listens on no port, asks the centre what to do and reports back; the list shows "last seen", the version and an "older than current" warning. It installs (rasat.net.tr/fener-kur) on Linux with systemd, with Docker and on OpenWrt. The Docker image (hub.docker.com/r/rasat/fener) is a single static Go binary (distroless/static, ~3.3 MB download), runs without root (only NET_RAW for ping), keeps nothing but its token; the image tag is the unit of update and the panel flags an out-of-date probe. Only the approach is the example; its code is a separate project. In HAS this can be a module: whoever wants it turns it on, everyone else never installs it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants