Initial commit: notifications, monitoring, on-call and incidents
One binary and one SQLite file. Topics with ntfy-style publish and subscribe, HTTP/TCP/DNS/TLS/heartbeat monitors with a public status page, schedules and escalation policies, and incidents with a timeline and post-mortem. The UI follows the Lightwell design language. Builds against an unreleased Reef (secretbox) through a local replace line. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
0c5a57556a
22 files changed
+4953
No files matched your search
@@ -0,0 +1,119 @@
|
||||
package sandbar
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:embed web/index.html
|
||||
var indexHTML []byte
|
||||
|
||||
//go:embed web/status.html
|
||||
var statusHTML []byte
|
||||
|
||||
type SMTPConfig struct{ Host, Port, User, Password, From string }
|
||||
|
||||
// Options configures a Server. Zero values are sensible defaults.
|
||||
type Options struct {
|
||||
DataDir string
|
||||
URL string // public URL, used for links in notifications
|
||||
MasterKey string
|
||||
MetricsToken string
|
||||
SMTP SMTPConfig
|
||||
MessageDays int // how long topic messages are kept (default 30)
|
||||
CheckDays int // how long monitor results are kept (default 30)
|
||||
|
||||
// Tests replace these.
|
||||
Now func() time.Time
|
||||
HTTPClient *http.Client
|
||||
Mailer func(to, subject, body string) error
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
opts Options
|
||||
st *Store
|
||||
hub *hub
|
||||
|
||||
// monitor runtime state
|
||||
mu sync.Mutex
|
||||
running map[string]bool // monitor ids with a check in flight
|
||||
|
||||
loginMu sync.Mutex
|
||||
loginTry map[string][]time.Time
|
||||
|
||||
stop chan struct{}
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func New(opts Options, st *Store) *Server {
|
||||
if opts.MessageDays <= 0 {
|
||||
opts.MessageDays = 30
|
||||
}
|
||||
if opts.CheckDays <= 0 {
|
||||
opts.CheckDays = 30
|
||||
}
|
||||
return &Server{opts: opts, st: st, hub: newHub(), running: map[string]bool{}, loginTry: map[string][]time.Time{}, stop: make(chan struct{})}
|
||||
}
|
||||
|
||||
func (s *Server) now() time.Time {
|
||||
if s.opts.Now != nil {
|
||||
return s.opts.Now()
|
||||
}
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
// Start runs the background workers: monitor scheduler, escalation, pruning.
|
||||
func (s *Server) Start() {
|
||||
s.every(time.Second, func() { s.runDueMonitors(s.now()) })
|
||||
s.every(5*time.Second, func() { s.escalate(s.now()) })
|
||||
s.every(time.Hour, func() { s.st.prune(s.now(), s.opts.MessageDays, s.opts.CheckDays) })
|
||||
}
|
||||
|
||||
func (s *Server) Close() {
|
||||
close(s.stop)
|
||||
s.wg.Wait()
|
||||
}
|
||||
|
||||
func (s *Server) every(d time.Duration, f func()) {
|
||||
s.wg.Add(1)
|
||||
go func() {
|
||||
defer s.wg.Done()
|
||||
t := time.NewTicker(d)
|
||||
defer t.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-s.stop:
|
||||
return
|
||||
case <-t.C:
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("worker panic: %v", r)
|
||||
}
|
||||
}()
|
||||
f()
|
||||
}()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *Server) link(path string) string {
|
||||
if s.opts.URL == "" {
|
||||
return ""
|
||||
}
|
||||
return s.opts.URL + path
|
||||
}
|
||||
|
||||
// BootstrapAdmin creates the first administrator from the environment when there are no users yet.
|
||||
func (s *Server) BootstrapAdmin(email, password string) {
|
||||
if email == "" || password == "" || s.countUsers() > 0 {
|
||||
return
|
||||
}
|
||||
if _, err := s.createUser("Admin", email, password, "admin", ""); err != nil {
|
||||
log.Printf("SANDBAR_ADMIN_EMAIL: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user