|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "runtime" |
| 7 | + |
| 8 | + "gopkg.in/alecthomas/kingpin.v2" |
| 9 | + |
| 10 | + _ "github.com/lib/pq" |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 13 | + "github.com/prometheus/common/log" |
| 14 | + |
| 15 | + "github.com/wrouesnel/postgres_exporter/collector" |
| 16 | +) |
| 17 | + |
| 18 | +// Version is set during build to the git describe version |
| 19 | +// (semantic version)-(commitish) form. |
| 20 | +var Version = "0.0.1" |
| 21 | + |
| 22 | +var ( |
| 23 | + listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9187").OverrideDefaultFromEnvar("PG_EXPORTER_WEB_LISTEN_ADDRESS").String() |
| 24 | + metricPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").OverrideDefaultFromEnvar("PG_EXPORTER_WEB_TELEMETRY_PATH").String() |
| 25 | + queriesPath = kingpin.Flag("extend.query-path", "Path to custom queries to run.").Default("").OverrideDefaultFromEnvar("PG_EXPORTER_EXTEND_QUERY_PATH").String() |
| 26 | + onlyDumpMaps = kingpin.Flag("dumpmaps", "Do not run, simply dump the maps.").Bool() |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + kingpin.Version(fmt.Sprintf("postgres_exporter %s (built with %s)\n", Version, runtime.Version())) |
| 31 | + log.AddFlags(kingpin.CommandLine) |
| 32 | + kingpin.Parse() |
| 33 | + |
| 34 | + // landingPage contains the HTML served at '/'. |
| 35 | + // TODO: Make this nicer and more informative. |
| 36 | + var landingPage = []byte(`<html> |
| 37 | + <head><title>Postgres exporter</title></head> |
| 38 | + <body> |
| 39 | + <h1>Postgres exporter</h1> |
| 40 | + <p><a href='` + *metricPath + `'>Metrics</a></p> |
| 41 | + </body> |
| 42 | + </html> |
| 43 | + `) |
| 44 | + |
| 45 | + if *onlyDumpMaps { |
| 46 | + collector.DumpMaps() |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + dsn := collector.GetDataSource() |
| 51 | + if len(dsn) == 0 { |
| 52 | + log.Fatal("couldn't find environment variables describing the datasource to use") |
| 53 | + } |
| 54 | + |
| 55 | + exporter := collector.NewExporter(dsn, *queriesPath) |
| 56 | + defer exporter.Close() |
| 57 | + |
| 58 | + prometheus.MustRegister(exporter) |
| 59 | + |
| 60 | + http.Handle(*metricPath, promhttp.Handler()) |
| 61 | + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 62 | + w.Header().Set("Content-Type", "Content-Type:text/plain; charset=UTF-8") // nolint: errcheck |
| 63 | + w.Write(landingPage) // nolint: errcheck |
| 64 | + }) |
| 65 | + |
| 66 | + log.Infof("Starting Server: %s", *listenAddress) |
| 67 | + log.Fatal(http.ListenAndServe(*listenAddress, nil)) |
| 68 | +} |
0 commit comments