mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 01:39:34 +08:00
refactor: alerts / emails
This commit is contained in:
@@ -25,8 +25,5 @@ func main() {
|
||||
Run: func(_ *cobra.Command, _ []string) { update.UpdateBeszel() },
|
||||
})
|
||||
|
||||
hubApp := hub.NewHub(app)
|
||||
|
||||
hubApp.Run()
|
||||
|
||||
hub.NewHub(app).Run()
|
||||
}
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package alerts
|
||||
|
||||
import (
|
||||
"beszel/internal/entities/email"
|
||||
"beszel/internal/entities/system"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
@@ -72,12 +71,12 @@ func (am *AlertManager) handleSlidingValueAlert(newRecord *models.Record, alertR
|
||||
if !triggered && curValue > threshold {
|
||||
alertRecord.Set("triggered", true)
|
||||
systemName := newRecord.GetString("name")
|
||||
subject = fmt.Sprintf("%s usage threshold exceeded on %s", name, systemName)
|
||||
subject = fmt.Sprintf("%s usage above threshold on %s", name, systemName)
|
||||
body = fmt.Sprintf("%s usage on %s is %.1f%%.\n\n%s\n\n- Beszel", name, systemName, curValue, am.app.Settings().Meta.AppUrl+"/system/"+systemName)
|
||||
} else if triggered && curValue <= threshold {
|
||||
alertRecord.Set("triggered", false)
|
||||
systemName := newRecord.GetString("name")
|
||||
subject = fmt.Sprintf("%s usage returned below threshold on %s", name, systemName)
|
||||
subject = fmt.Sprintf("%s usage below threshold on %s", name, systemName)
|
||||
body = fmt.Sprintf("%s usage on %s is below threshold at %.1f%%.\n\n%s\n\n- Beszel", name, systemName, curValue, am.app.Settings().Meta.AppUrl+"/system/"+systemName)
|
||||
} else {
|
||||
// fmt.Println(name, "not triggered")
|
||||
@@ -93,11 +92,11 @@ func (am *AlertManager) handleSlidingValueAlert(newRecord *models.Record, alertR
|
||||
return
|
||||
}
|
||||
if user := alertRecord.ExpandedOne("user"); user != nil {
|
||||
am.sendAlert(email.NewEmailData(
|
||||
user.GetString("email"),
|
||||
subject,
|
||||
body,
|
||||
))
|
||||
am.sendAlert(&mailer.Message{
|
||||
To: []mail.Address{{Address: user.GetString("email")}},
|
||||
Subject: subject,
|
||||
Text: body,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,24 +129,20 @@ func (am *AlertManager) handleStatusAlerts(newStatus string, oldRecord *models.R
|
||||
}
|
||||
// send alert
|
||||
systemName := oldRecord.GetString("name")
|
||||
am.sendAlert(email.NewEmailData(
|
||||
user.GetString("email"),
|
||||
fmt.Sprintf("Connection to %s is %s %v", systemName, alertStatus, emoji),
|
||||
fmt.Sprintf("Connection to %s is %s\n\n- Beszel", systemName, alertStatus),
|
||||
))
|
||||
am.sendAlert(&mailer.Message{
|
||||
To: []mail.Address{{Address: user.GetString("email")}},
|
||||
Subject: fmt.Sprintf("Connection to %s is %s %v", systemName, alertStatus, emoji),
|
||||
Text: fmt.Sprintf("Connection to %s is %s\n\n- Beszel", systemName, alertStatus),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (am *AlertManager) sendAlert(data *email.EmailData) {
|
||||
// fmt.Println("sending alert", "to", data.to, "subj", data.subj, "body", data.body)
|
||||
message := &mailer.Message{
|
||||
From: mail.Address{
|
||||
Address: am.app.Settings().Meta.SenderAddress,
|
||||
Name: am.app.Settings().Meta.SenderName,
|
||||
},
|
||||
To: []mail.Address{{Address: data.To()}},
|
||||
Subject: data.Subject(),
|
||||
Text: data.Body(),
|
||||
func (am *AlertManager) sendAlert(message *mailer.Message) {
|
||||
// fmt.Println("sending alert", "to", message.To, "subj", message.Subject, "body", message.Text)
|
||||
message.From = mail.Address{
|
||||
Address: am.app.Settings().Meta.SenderAddress,
|
||||
Name: am.app.Settings().Meta.SenderName,
|
||||
}
|
||||
if err := am.mailClient.Send(message); err != nil {
|
||||
am.app.Logger().Error("Failed to send alert: ", "err", err.Error())
|
||||
|
@@ -2,6 +2,7 @@ package container
|
||||
|
||||
import "time"
|
||||
|
||||
// Docker container resources info from /containers/id/stats
|
||||
type Container struct {
|
||||
Id string
|
||||
IdShort string
|
||||
@@ -24,6 +25,7 @@ type Container struct {
|
||||
// Mounts []MountPoint
|
||||
}
|
||||
|
||||
// Stats to return to the hub
|
||||
type ContainerStats struct {
|
||||
Name string `json:"n"`
|
||||
Cpu float64 `json:"c"`
|
||||
@@ -32,6 +34,7 @@ type ContainerStats struct {
|
||||
NetworkRecv float64 `json:"nr"`
|
||||
}
|
||||
|
||||
// Keeps track of container stats from previous run
|
||||
type PrevContainerStats struct {
|
||||
Cpu [2]uint64
|
||||
Net struct {
|
||||
|
@@ -1,27 +0,0 @@
|
||||
package email
|
||||
|
||||
type EmailData struct {
|
||||
to string
|
||||
subj string
|
||||
body string
|
||||
}
|
||||
|
||||
func NewEmailData(to, subj, body string) *EmailData {
|
||||
return &EmailData{
|
||||
to: to,
|
||||
subj: subj,
|
||||
body: body,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EmailData) To() string {
|
||||
return e.to
|
||||
}
|
||||
|
||||
func (e *EmailData) Subject() string {
|
||||
return e.subj
|
||||
}
|
||||
|
||||
func (e *EmailData) Body() string {
|
||||
return e.body
|
||||
}
|
@@ -46,9 +46,8 @@ func NewHub(app *pocketbase.PocketBase) *Hub {
|
||||
}
|
||||
|
||||
func (h *Hub) Run() {
|
||||
|
||||
rm := records.NewRecordManager(h.app)
|
||||
am := alerts.NewAlertManager(h.app)
|
||||
var rm *records.RecordManager
|
||||
var am *alerts.AlertManager
|
||||
|
||||
// loosely check if it was executed using "go run"
|
||||
isGoRun := strings.HasPrefix(os.Args[0], os.TempDir())
|
||||
@@ -60,6 +59,13 @@ func (h *Hub) Run() {
|
||||
Dir: "../../migrations",
|
||||
})
|
||||
|
||||
// set up record manager and alert manager
|
||||
h.app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
rm = records.NewRecordManager(h.app)
|
||||
am = alerts.NewAlertManager(h.app)
|
||||
return nil
|
||||
})
|
||||
|
||||
// set auth settings
|
||||
h.app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
||||
usersCollection, err := h.app.Dao().FindCollectionByNameOrId("users")
|
||||
|
@@ -46,6 +46,7 @@ export default function MemChart({
|
||||
<YAxis
|
||||
// use "ticks" instead of domain / tickcount if need more control
|
||||
domain={[0, totalMem]}
|
||||
tickCount={9}
|
||||
className="tracking-tighter"
|
||||
width={yAxisWidth}
|
||||
tickLine={false}
|
||||
|
Reference in New Issue
Block a user