main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/signal"
  8. "path/filepath"
  9. "runtime"
  10. "runtime/trace"
  11. "strconv"
  12. "syscall"
  13. "time"
  14. "net/http"
  15. _ "net/http/pprof"
  16. "github.com/grafana/grafana/pkg/log"
  17. "github.com/grafana/grafana/pkg/models"
  18. "github.com/grafana/grafana/pkg/services/sqlstore"
  19. "github.com/grafana/grafana/pkg/setting"
  20. _ "github.com/grafana/grafana/pkg/services/alerting/conditions"
  21. _ "github.com/grafana/grafana/pkg/services/alerting/notifiers"
  22. _ "github.com/grafana/grafana/pkg/tsdb/graphite"
  23. _ "github.com/grafana/grafana/pkg/tsdb/influxdb"
  24. _ "github.com/grafana/grafana/pkg/tsdb/mqe"
  25. _ "github.com/grafana/grafana/pkg/tsdb/mysql"
  26. _ "github.com/grafana/grafana/pkg/tsdb/opentsdb"
  27. _ "github.com/grafana/grafana/pkg/tsdb/prometheus"
  28. _ "github.com/grafana/grafana/pkg/tsdb/testdata"
  29. )
  30. var version = "4.1.0"
  31. var commit = "NA"
  32. var buildstamp string
  33. var build_date string
  34. var configFile = flag.String("config", "", "path to config file")
  35. var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
  36. var pidFile = flag.String("pidfile", "", "path to pid file")
  37. var exitChan = make(chan int)
  38. func init() {
  39. }
  40. func main() {
  41. v := flag.Bool("v", false, "prints current version and exits")
  42. profile := flag.Bool("profile", false, "Turn on pprof profiling")
  43. profilePort := flag.Int("profile-port", 6060, "Define custom port for profiling")
  44. flag.Parse()
  45. if *v {
  46. fmt.Printf("Version %s (commit: %s)\n", version, commit)
  47. os.Exit(0)
  48. }
  49. if *profile {
  50. runtime.SetBlockProfileRate(1)
  51. go func() {
  52. http.ListenAndServe(fmt.Sprintf("localhost:%d", *profilePort), nil)
  53. }()
  54. f, err := os.Create("trace.out")
  55. if err != nil {
  56. panic(err)
  57. }
  58. defer f.Close()
  59. err = trace.Start(f)
  60. if err != nil {
  61. panic(err)
  62. }
  63. defer trace.Stop()
  64. }
  65. buildstampInt64, _ := strconv.ParseInt(buildstamp, 10, 64)
  66. if buildstampInt64 == 0 {
  67. buildstampInt64 = time.Now().Unix()
  68. }
  69. setting.BuildVersion = version
  70. setting.BuildCommit = commit
  71. setting.BuildStamp = buildstampInt64
  72. server := NewGrafanaServer()
  73. server.Start()
  74. }
  75. func initRuntime() {
  76. err := setting.NewConfigContext(&setting.CommandLineArgs{
  77. Config: *configFile,
  78. HomePath: *homePath,
  79. Args: flag.Args(),
  80. })
  81. if err != nil {
  82. log.Fatal(3, err.Error())
  83. }
  84. logger := log.New("main")
  85. logger.Info("Starting Grafana", "version", version, "commit", commit, "compiled", time.Unix(setting.BuildStamp, 0))
  86. setting.LogConfigurationInfo()
  87. }
  88. func initSql() {
  89. sqlstore.NewEngine()
  90. sqlstore.EnsureAdminUser()
  91. }
  92. func writePIDFile() {
  93. if *pidFile == "" {
  94. return
  95. }
  96. // Ensure the required directory structure exists.
  97. err := os.MkdirAll(filepath.Dir(*pidFile), 0700)
  98. if err != nil {
  99. log.Fatal(3, "Failed to verify pid directory", err)
  100. }
  101. // Retrieve the PID and write it.
  102. pid := strconv.Itoa(os.Getpid())
  103. if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
  104. log.Fatal(3, "Failed to write pidfile", err)
  105. }
  106. }
  107. func listenToSystemSignals(server models.GrafanaServer) {
  108. signalChan := make(chan os.Signal, 1)
  109. ignoreChan := make(chan os.Signal, 1)
  110. code := 0
  111. signal.Notify(ignoreChan, syscall.SIGHUP)
  112. signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM)
  113. select {
  114. case sig := <-signalChan:
  115. // Stops trace if profiling has been enabled
  116. trace.Stop()
  117. server.Shutdown(0, fmt.Sprintf("system signal: %s", sig))
  118. case code = <-exitChan:
  119. server.Shutdown(code, "startup error")
  120. }
  121. }