This repository was archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
106 lines (90 loc) · 2.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/mkabilov/postgresql_exporter/pkg/config"
"github.com/mkabilov/postgresql_exporter/pkg/pgcollector"
)
const (
indexHTML = `
<html>
<head>
<title>Postgresql Exporter</title>
</head>
<body>
<h1>Postgresql Exporter</h1>
<p>
<a href='%s'>Metrics</a>
</p>
</body>
</html>
`
)
var (
version string
showVersion = flag.Bool("version", false, "output version information, then exit")
configFile = flag.String("config", "config.yaml", "path to the config file")
metricsPath = flag.String("web.telemetry-path", "/metrics", "path under which to expose metrics")
listenAddress = flag.String("web.listen-address", ":9187", "address to listen on for web interface and telemetry")
)
func init() {
flag.Parse()
}
func main() {
if *showVersion {
fmt.Printf("postgresql prometheus exporter %s", version)
os.Exit(0)
}
cfg := config.New(*configFile)
if err := cfg.Load(); err != nil {
log.Fatalf("could not load config: %v", err)
}
collector := pgcollector.New()
collector.LoadConfig(cfg)
if err := prometheus.Register(collector); err != nil {
log.Fatalf("could not register collector: %v", err)
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(fmt.Sprintf(indexHTML, *metricsPath)))
})
mux.Handle(*metricsPath, promhttp.Handler())
srv := http.Server{
Addr: *listenAddress,
Handler: mux,
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT)
log.Printf("starting postgresql exporter: %s", srv.Addr)
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("could not start http server: %v", err)
}
}()
loop:
for {
switch sig := <-sigs; sig {
case syscall.SIGINT:
fallthrough
case syscall.SIGTERM:
break loop
case syscall.SIGHUP:
//TODO: reload config
default:
log.Printf("received signal: %v", sig)
}
}
if err := srv.Shutdown(context.Background()); err != nil {
log.Printf("could not shutdown http server: %v", err)
}
close(sigs)
}