A simple monitoring solution written in Go (work in progress)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.1 KiB

  1. package server
  2. import (
  3. "context"
  4. "fmt"
  5. "git.binarythought.com/cdramey/alrm/config"
  6. "net"
  7. "net/http"
  8. "time"
  9. )
  10. type Server struct {
  11. workers []*worker
  12. config *config.Config
  13. shutdownc chan bool
  14. http http.Server
  15. }
  16. func (srv *Server) Start() (bool, error) {
  17. listen, err := net.Listen("tcp", srv.config.Listen)
  18. if err != nil {
  19. return false, err
  20. }
  21. for _, w := range srv.workers {
  22. go w.start()
  23. }
  24. srv.http = http.Server{Handler: srv}
  25. go srv.http.Serve(listen)
  26. t := time.NewTicker(srv.config.Interval)
  27. defer t.Stop()
  28. for {
  29. select {
  30. case r := <-t.C:
  31. if srv.config.DebugLevel > 0 {
  32. fmt.Printf("interval check at %s\n", r)
  33. }
  34. for _, w := range srv.workers {
  35. w.wake()
  36. }
  37. case b := <-srv.shutdownc:
  38. srv.http.Shutdown(context.Background())
  39. for _, w := range srv.workers {
  40. w.shutdown()
  41. }
  42. return b, nil
  43. }
  44. }
  45. }
  46. func NewServer(cfg *config.Config) *Server {
  47. srv := &Server{
  48. config: cfg,
  49. shutdownc: make(chan bool, 1),
  50. }
  51. for _, g := range cfg.Groups {
  52. srv.workers = append(
  53. srv.workers, makeworker(g, cfg.DebugLevel),
  54. )
  55. }
  56. return srv
  57. }