76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.mziesel.nl/mans/zadmin/internal/agent"
|
|
"git.mziesel.nl/mans/zadmin/internal/config"
|
|
"git.mziesel.nl/mans/zadmin/internal/logger"
|
|
"github.com/nats-io/nats.go"
|
|
)
|
|
|
|
func main() {
|
|
log := logger.NewConsoleLogger()
|
|
cfg, err := config.LoadAgentConfig()
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
natsConn, err := nats.Connect(cfg.NATSURL, nats.UserInfo(cfg.NATSUser, cfg.NATSPass))
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Info("Connected to nats")
|
|
|
|
agent := agent.NewAgent(cfg, log, natsConn)
|
|
|
|
var wg sync.WaitGroup
|
|
shouldExit := false
|
|
wg.Add(1)
|
|
|
|
agent.Logger.Info("Starting agent...")
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
for !shouldExit {
|
|
agent.Logger.Info("Collecting host data")
|
|
// host, err := agent.GetHost()
|
|
// if err != nil {
|
|
// agent.Logger.Error(err)
|
|
// return
|
|
// }
|
|
stats, err := agent.GetHostStatistics()
|
|
if err != nil {
|
|
agent.Logger.Error(err)
|
|
}
|
|
agent.Logger.Info("Publishing host data")
|
|
agent.PublishHostStats(&stats)
|
|
time.Sleep(30 * time.Second)
|
|
}
|
|
}()
|
|
|
|
// Wait for interrupt signal to gracefully shutdown the server
|
|
sigChan := make(chan os.Signal, 1)
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
<-sigChan
|
|
|
|
// Create a context with a timeout for the shutdown
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
// Shutdown the server
|
|
// note: ctx is not used at the moment
|
|
if err := agent.Stop(ctx); err != nil {
|
|
agent.Logger.Fatal("Agent forced to shutdown:", err)
|
|
}
|
|
|
|
agent.Logger.Info("Agent exited gracefully")
|
|
}
|