Go service (nasmon/) plus nfpm packaging (packaging/), carried over from the home_nas monorepo. Adds rpm packaging alongside the existing deb build (Fedora/RHEL/Rocky dependency names via nfpm overrides, per-format systemd unit path, dpkg+rpm-compatible maintainer scripts) and a Gitea Actions workflow that builds both formats on every push. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
// nasmon — lightweight NAS health monitor: web dashboard + SMART/ZFS alerts by e-mail.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"nasmon/internal/alert"
|
|
"nasmon/internal/config"
|
|
"nasmon/internal/history"
|
|
"nasmon/internal/sched"
|
|
"nasmon/internal/shares"
|
|
"nasmon/internal/store"
|
|
"nasmon/internal/web"
|
|
)
|
|
|
|
var version = "dev"
|
|
|
|
func main() {
|
|
listen := flag.String("listen", ":8090", "HTTP listen address")
|
|
dataDir := flag.String("data", "/var/lib/nasmon", "directory for the SQLite database")
|
|
cfgPath := flag.String("config", "/etc/nasmon/config.json", "settings file (created on first save)")
|
|
showVer := flag.Bool("version", false, "print version and exit")
|
|
flag.Parse()
|
|
if *showVer {
|
|
fmt.Println("nasmon", version)
|
|
return
|
|
}
|
|
log.SetFlags(log.LstdFlags | log.Lmsgprefix)
|
|
|
|
cfg, err := config.Load(*cfgPath)
|
|
if err != nil {
|
|
log.Fatalf("config: %v", err)
|
|
}
|
|
if err := os.MkdirAll(*dataDir, 0o750); err != nil {
|
|
log.Fatalf("data dir: %v", err)
|
|
}
|
|
st, err := store.Open(filepath.Join(*dataDir, "nasmon.db"))
|
|
if err != nil {
|
|
log.Fatalf("database: %v", err)
|
|
}
|
|
defer st.Close()
|
|
|
|
sh, err := shares.Load(filepath.Join(filepath.Dir(*cfgPath), "shares.json"))
|
|
if err != nil {
|
|
log.Fatalf("shares config: %v", err)
|
|
}
|
|
|
|
ring := history.New(24 * 60 * 6) // 24 h at 10 s
|
|
am := &alert.Manager{Store: st, Config: cfg, Send: alert.SendMail}
|
|
sc := sched.New(cfg, st, ring, am, version)
|
|
sc.LoadCachedSmart()
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
go sc.Run(ctx)
|
|
|
|
srv := &http.Server{
|
|
Addr: *listen,
|
|
Handler: web.New(cfg, st, ring, sc, sh),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
}
|
|
go func() {
|
|
log.Printf("nasmon %s listening on %s", version, *listen)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Fatalf("http: %v", err)
|
|
}
|
|
}()
|
|
|
|
<-ctx.Done()
|
|
log.Print("shutting down")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
_ = srv.Shutdown(shutdownCtx)
|
|
}
|