main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "os/signal"
  7. "runtime"
  8. "runtime/trace"
  9. "strconv"
  10. "syscall"
  11. "time"
  12. "net/http"
  13. _ "net/http/pprof"
  14. "github.com/grafana/grafana/pkg/log"
  15. "github.com/grafana/grafana/pkg/metrics"
  16. "github.com/grafana/grafana/pkg/setting"
  17. _ "github.com/grafana/grafana/pkg/services/alerting/conditions"
  18. _ "github.com/grafana/grafana/pkg/services/alerting/notifiers"
  19. _ "github.com/grafana/grafana/pkg/tsdb/cloudwatch"
  20. _ "github.com/grafana/grafana/pkg/tsdb/graphite"
  21. _ "github.com/grafana/grafana/pkg/tsdb/influxdb"
  22. _ "github.com/grafana/grafana/pkg/tsdb/mysql"
  23. _ "github.com/grafana/grafana/pkg/tsdb/opentsdb"
  24. _ "github.com/grafana/grafana/pkg/tsdb/postgres"
  25. _ "github.com/grafana/grafana/pkg/tsdb/prometheus"
  26. _ "github.com/grafana/grafana/pkg/tsdb/testdata"
  27. )
  28. var version = "4.6.0"
  29. var commit = "NA"
  30. var buildstamp string
  31. var build_date string
  32. var configFile = flag.String("config", "", "path to config file")
  33. var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
  34. var pidFile = flag.String("pidfile", "", "path to pid file")
  35. var exitChan = make(chan int)
  36. func main() {
  37. v := flag.Bool("v", false, "prints current version and exits")
  38. profile := flag.Bool("profile", false, "Turn on pprof profiling")
  39. profilePort := flag.Int("profile-port", 6060, "Define custom port for profiling")
  40. flag.Parse()
  41. if *v {
  42. fmt.Printf("Version %s (commit: %s)\n", version, commit)
  43. os.Exit(0)
  44. }
  45. if *profile {
  46. runtime.SetBlockProfileRate(1)
  47. go func() {
  48. http.ListenAndServe(fmt.Sprintf("localhost:%d", *profilePort), nil)
  49. }()
  50. f, err := os.Create("trace.out")
  51. if err != nil {
  52. panic(err)
  53. }
  54. defer f.Close()
  55. err = trace.Start(f)
  56. if err != nil {
  57. panic(err)
  58. }
  59. defer trace.Stop()
  60. }
  61. buildstampInt64, _ := strconv.ParseInt(buildstamp, 10, 64)
  62. if buildstampInt64 == 0 {
  63. buildstampInt64 = time.Now().Unix()
  64. }
  65. setting.BuildVersion = version
  66. setting.BuildCommit = commit
  67. setting.BuildStamp = buildstampInt64
  68. metrics.M_Grafana_Version.WithLabelValues(version).Set(1)
  69. shutdownCompleted := make(chan int)
  70. server := NewGrafanaServer()
  71. go listenToSystemSignals(server, shutdownCompleted)
  72. go func() {
  73. code := 0
  74. if err := server.Start(); err != nil {
  75. log.Error2("Startup failed", "error", err)
  76. code = 1
  77. }
  78. exitChan <- code
  79. }()
  80. code := <-shutdownCompleted
  81. log.Info2("Grafana shutdown completed.", "code", code)
  82. log.Close()
  83. os.Exit(code)
  84. }
  85. func listenToSystemSignals(server *GrafanaServerImpl, shutdownCompleted chan int) {
  86. signalChan := make(chan os.Signal, 1)
  87. ignoreChan := make(chan os.Signal, 1)
  88. code := 0
  89. signal.Notify(ignoreChan, syscall.SIGHUP)
  90. signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM)
  91. select {
  92. case sig := <-signalChan:
  93. trace.Stop() // Stops trace if profiling has been enabled
  94. server.Shutdown(0, fmt.Sprintf("system signal: %s", sig))
  95. shutdownCompleted <- 0
  96. case code = <-exitChan:
  97. trace.Stop() // Stops trace if profiling has been enabled
  98. server.Shutdown(code, "startup error")
  99. shutdownCompleted <- code
  100. }
  101. }