grafana/pkg/cmd/grafana-server/main.go

154 lines
4.1 KiB
Go
Raw Normal View History

2014-08-08 05:35:15 -05:00
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
2014-08-08 05:35:15 -05:00
"os"
"os/signal"
2014-10-04 06:33:20 -05:00
"runtime"
"runtime/trace"
"strconv"
"syscall"
"time"
2014-08-08 05:35:15 -05:00
2018-11-15 07:42:09 -06:00
"github.com/grafana/grafana/pkg/extensions"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
_ "github.com/grafana/grafana/pkg/services/alerting/conditions"
_ "github.com/grafana/grafana/pkg/services/alerting/notifiers"
"github.com/grafana/grafana/pkg/setting"
_ "github.com/grafana/grafana/pkg/tsdb/azuremonitor"
_ "github.com/grafana/grafana/pkg/tsdb/cloudmonitoring"
2017-04-03 07:50:40 -05:00
_ "github.com/grafana/grafana/pkg/tsdb/cloudwatch"
_ "github.com/grafana/grafana/pkg/tsdb/elasticsearch"
_ "github.com/grafana/grafana/pkg/tsdb/graphite"
_ "github.com/grafana/grafana/pkg/tsdb/influxdb"
2017-03-29 15:54:07 -05:00
_ "github.com/grafana/grafana/pkg/tsdb/mysql"
_ "github.com/grafana/grafana/pkg/tsdb/opentsdb"
Postgres Data Source (#9475) * add postgresql datasource * add rest of files for postgres datasource * fix timeseries query, remove unused code * consistent naming, refactoring * s/mysql/postgres/ * s/mysql/postgres/ * couple more tests * tests for more datatypes * fix macros for postgres * add __timeSec macro * add frontend for postgres datasource * adjust documentation * fix formatting * add proper plugin description * merge editor changes from mysql * port changes from mysql datasource * set proper defaultQuery for postgres * add time_sec to timeseries query accept int for value for timeseries query * revert allowing time_sec and handle int or float values as unix timestamp for "time" column * fix tslint error * handle decimal values in timeseries query * allow setting sslmode for postgres datasource * use type switch for handling data types * fix value for timeseries query * refactor timeseries queries to make them more flexible * remove debug statement from inner loop in type conversion * use plain for loop in getTypedRowData * fix timeseries queries * adjust postgres datasource to tsdb refactoring * adjust postgres datasource to frontend changes * update lib/pq to latest version * move type conversion to getTypedRowData * handle address types cidr, inet and macaddr * adjust response parser and docs for annotations * convert unknown types to string * add documentation for postgres datasource * add another example query with metric column * set more helpful default query * update help text in query editor * handle NULL in value column of timeseries query * add __timeGroup macro * add test for __timeGroup macro * document __timeGroup and set proper default query for annotations * fix typos in docs * add postgres to list of datasources * add postgres to builtInPlugins * mysql: refactoring as prep for merging postgres Refactors out the initialization of the xorm engine and the query logic for an sql data source. * mysql: rename refactoring + test update * postgres:refactor to use SqlEngine(same as mysql) Refactored to use a common base class with the MySql data source. Other changes from the original PR: - Changed time column to be time_sec to allow other time units in the future and to be the same as MySQL - Changed integration test to test the main Query method rather than the private transformToTable method - Changed the __timeSec macro name to __timeEpoch - Renamed PostgresExecutor to PostgresQueryEndpoint Fixes #9209 (the original PR) * postgres: encrypt password on config page With some other cosmetic changes to the config page: - placeholder texts - reset button for the password after it has been encrypted. - default value for the sslmode field. * postgres: change back col name to time from time_sec * postgres mysql: remove annotation title Title has been removed from annotations * postgres: fix images for docs page * postgres mysql: fix specs
2017-10-10 08:19:14 -05:00
_ "github.com/grafana/grafana/pkg/tsdb/postgres"
_ "github.com/grafana/grafana/pkg/tsdb/prometheus"
_ "github.com/grafana/grafana/pkg/tsdb/testdatasource"
2014-08-08 05:35:15 -05:00
)
var version = "5.0.0"
var commit = "NA"
var buildBranch = "master"
var buildstamp string
2014-08-08 05:35:15 -05:00
2014-10-04 06:33:20 -05:00
func main() {
var (
configFile = flag.String("config", "", "path to config file")
homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory")
pidFile = flag.String("pidfile", "", "path to pid file")
packaging = flag.String("packaging", "unknown", "describes the way Grafana was installed")
v = flag.Bool("v", false, "prints current version and exits")
profile = flag.Bool("profile", false, "Turn on pprof profiling")
profilePort = flag.Uint("profile-port", 6060, "Define custom port for profiling")
tracing = flag.Bool("tracing", false, "Turn on tracing")
tracingFile = flag.String("tracing-file", "trace.out", "Define tracing output file")
)
flag.Parse()
if *v {
fmt.Printf("Version %s (commit: %s, branch: %s)\n", version, commit, buildBranch)
os.Exit(0)
}
profileDiagnostics := newProfilingDiagnostics(*profile, *profilePort)
if err := profileDiagnostics.overrideWithEnv(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
traceDiagnostics := newTracingDiagnostics(*tracing, *tracingFile)
if err := traceDiagnostics.overrideWithEnv(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if profileDiagnostics.enabled {
fmt.Println("diagnostics: pprof profiling enabled", "port", profileDiagnostics.port)
runtime.SetBlockProfileRate(1)
go func() {
err := http.ListenAndServe(fmt.Sprintf("localhost:%d", profileDiagnostics.port), nil)
if err != nil {
panic(err)
}
}()
}
if traceDiagnostics.enabled {
fmt.Println("diagnostics: tracing enabled", "file", traceDiagnostics.file)
f, err := os.Create(traceDiagnostics.file)
if err != nil {
panic(err)
}
defer f.Close()
err = trace.Start(f)
if err != nil {
panic(err)
}
defer trace.Stop()
}
buildstampInt64, _ := strconv.ParseInt(buildstamp, 10, 64)
if buildstampInt64 == 0 {
buildstampInt64 = time.Now().Unix()
}
setting.BuildVersion = version
setting.BuildCommit = commit
setting.BuildStamp = buildstampInt64
setting.BuildBranch = buildBranch
setting.IsEnterprise = extensions.IsEnterprise
2018-11-15 07:42:09 -06:00
setting.Packaging = validPackaging(*packaging)
metrics.SetBuildInformation(version, commit, buildBranch)
server := NewServer(*configFile, *homePath, *pidFile)
go listenToSystemSignals(server)
err := server.Run()
code := 0
if err != nil {
code = server.ExitCode(err)
}
trace.Stop()
log.Close()
os.Exit(code)
2014-08-08 05:35:15 -05:00
}
2018-11-15 07:42:09 -06:00
func validPackaging(packaging string) string {
validTypes := []string{"dev", "deb", "rpm", "docker", "brew", "hosted", "unknown"}
for _, vt := range validTypes {
if packaging == vt {
return packaging
}
}
return "unknown"
}
func listenToSystemSignals(server *Server) {
signalChan := make(chan os.Signal, 1)
sighupChan := make(chan os.Signal, 1)
signal.Notify(sighupChan, syscall.SIGHUP)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
for {
select {
case <-sighupChan:
log.Reload()
case sig := <-signalChan:
server.Shutdown(fmt.Sprintf("System signal: %s", sig))
}
}
}