From 80e322d3d511b9079a2152e6be30b2ec71a6b598 Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 21 Jul 2024 13:29:04 -0400 Subject: [PATCH] cron / timer updates --- hub/main.go | 19 +++++++------------ hub/records.go | 5 ++--- readme.md | 4 ++-- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/hub/main.go b/hub/main.go index 198c698..1ee1a9e 100644 --- a/hub/main.go +++ b/hub/main.go @@ -67,12 +67,11 @@ func main() { return err } usersAuthOptions := usersCollection.AuthOptions() + usersAuthOptions.AllowUsernameAuth = false if os.Getenv("DISABLE_PASSWORD_AUTH") == "true" { usersAuthOptions.AllowEmailAuth = false - usersAuthOptions.AllowUsernameAuth = false } else { usersAuthOptions.AllowEmailAuth = true - usersAuthOptions.AllowUsernameAuth = true } usersCollection.SetOptions(usersAuthOptions) if err := app.Dao().SaveCollection(usersCollection); err != nil { @@ -99,11 +98,13 @@ func main() { return nil }) - // set up cron jobs + // set up cron jobs / ticker for system updates app.OnBeforeServe().Add(func(e *core.ServeEvent) error { + // 15 second ticker for system updates + go startSystemUpdateTicker() + // cron job to delete old records scheduler := cron.New() - // delete records that are older than the display period - scheduler.MustAdd("delete old records", "8 */2 * * *", func() { + scheduler.MustAdd("delete old records", "8 * * * *", func() { deleteOldRecords("system_stats", "1m", time.Hour) deleteOldRecords("container_stats", "1m", time.Hour) deleteOldRecords("system_stats", "10m", 12*time.Hour) @@ -151,12 +152,6 @@ func main() { return nil }) - // start ticker for server updates - app.OnBeforeServe().Add(func(e *core.ServeEvent) error { - go serverUpdateTicker() - return nil - }) - // immediately create connection for new servers app.OnModelAfterCreate("systems").Add(func(e *core.ModelEvent) error { go updateSystem(e.Model.(*models.Record)) @@ -206,7 +201,7 @@ func main() { } } -func serverUpdateTicker() { +func startSystemUpdateTicker() { ticker := time.NewTicker(15 * time.Second) for range ticker.C { updateSystems() diff --git a/hub/records.go b/hub/records.go index c9881cd..351f810 100644 --- a/hub/records.go +++ b/hub/records.go @@ -140,11 +140,10 @@ func twoDecimals(value float64) float64 { /* Delete records of specified collection and type that are older than timeLimit */ func deleteOldRecords(collection string, recordType string, timeLimit time.Duration) { - // log.Println("Deleting old", recordType, "records...") - timeLimitStamp := time.Now().UTC().Add(timeLimit).Format("2006-01-02 15:04:05") + timeLimitStamp := time.Now().UTC().Add(-timeLimit).Format("2006-01-02 15:04:05") records, _ := app.Dao().FindRecordsByExpr(collection, dbx.NewExp("type = {:type}", dbx.Params{"type": recordType}), - dbx.NewExp("created > {:created}", dbx.Params{"created": timeLimitStamp}), + dbx.NewExp("created < {:created}", dbx.Params{"created": timeLimitStamp}), ) for _, record := range records { if err := app.Dao().DeleteRecord(record); err != nil { diff --git a/readme.md b/readme.md index 71caa28..460bb7b 100644 --- a/readme.md +++ b/readme.md @@ -30,7 +30,7 @@ Beszel has two components: the hub and the agent. The hub is a web application, built on top of [PocketBase](https://pocketbase.io/), that provides a dashboard to view and manage your connected systems. -The agent runs on each system you want to monitor. It provides a minimal SSH server through which it communicates system information to the hub. +The agent runs on each system you want to monitor. It creates a minimal SSH server through which it communicates system metrics to the hub. ## Installation @@ -133,7 +133,7 @@ The agent's SSH server is configured to accept connections only using this key. ### Agent is not connecting -Assuming the agent is running, the connection is probably being blocked by a firewall. You should add an inbound rule to allow TCP connections to the port. Check any active firewalls on the agent system, like iptables or ufw, and in your cloud provider account if applicable. +Assuming the agent is running, the connection is probably being blocked by a firewall. You need to add an inbound rule on the agent system to allow TCP connections to the port. Check any active firewalls, like iptables or ufw, and in your cloud provider account if applicable. Connectivity can be tested by running `telnet ` or `nc -zv ` from a remote machine.