This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
149 lines (138 loc) · 4.96 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"database/sql"
"flag"
"fmt"
"net/http"
"time"
"github.com/ContaAzul/postgresql_exporter/config"
"github.com/ContaAzul/postgresql_exporter/gauges"
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
"github.com/apex/httplog"
"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
addr = flag.String("listen-address", ":9111", "The address to listen on for HTTP requests.")
configFile = flag.String("config", "config.yml", "The path to the config file.")
interval = flag.Duration("interval", 30*time.Second, "interval between gathering metrics")
timeout = flag.Duration("timeout", 15*time.Second, "query timeout")
maxDBConns = flag.Int("max-db-connections", 5, "max connections to open to each database")
debug = flag.Bool("debug", false, "Enable debug mode")
)
func main() {
log.SetHandler(logfmt.Default)
flag.Parse()
var server = &http.Server{
Addr: *addr,
WriteTimeout: 1 * time.Second,
ReadTimeout: 1 * time.Second,
}
if *debug {
log.SetLevel(log.DebugLevel)
server.Handler = httplog.New(http.DefaultServeMux)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(
`<html>
<head><title>ContaAzul PostgreSQL Exporter</title></head>
<body>
<h1>ContaAzul PostgreSQL Exporter</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>
`))
})
http.Handle("/metrics", promhttp.Handler())
var config, err = config.Parse(*configFile)
if err != nil {
log.WithError(err).Fatalf("error to parse config file")
}
for _, con := range config.Databases {
var log = log.WithField("db", con.Name)
log.Info("started monitoring")
var db *sql.DB
if con.Sql.ConnectionName != "" {
log.Info(fmt.Sprintf("Using sqlproxy with connection name: %s", con.Sql.ConnectionName))
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
con.Sql.ConnectionName,
con.Sql.DatabaseUser,
con.Sql.DatabasePassword,
con.Sql.DatabaseName)
db, err = sql.Open("cloudsqlpostgres", dsn)
if err != nil {
log.WithError(err).Error("failed to open url")
}
} else {
db, err = sql.Open("postgres", con.URL)
if err != nil {
log.WithError(err).Error("failed to open url")
}
}
if err := db.Ping(); err != nil {
log.WithError(err).Error("failed to ping database")
}
db.SetMaxOpenConns(*maxDBConns)
defer db.Close()
watch(db, prometheus.DefaultRegisterer, con.Name)
}
log.WithField("addr", *addr).Info("started")
if err := server.ListenAndServe(); err != nil {
log.WithError(err).Fatal("failed to start up server")
}
}
func watch(db *sql.DB, reg prometheus.Registerer, name string) {
var gauges = gauges.New(name, db, *interval, *timeout)
reg.MustRegister(gauges.Errs)
reg.MustRegister(gauges.ConnectedBackends())
reg.MustRegister(gauges.MaxBackends())
reg.MustRegister(gauges.InstanceConnectedBackends())
reg.MustRegister(gauges.BackendsByState())
reg.MustRegister(gauges.BackendsByUserAndClientAddress())
reg.MustRegister(gauges.BackendsByWaitEventType())
reg.MustRegister(gauges.RequestedCheckpoints())
reg.MustRegister(gauges.ScheduledCheckpoints())
reg.MustRegister(gauges.BuffersMaxWrittenClean())
reg.MustRegister(gauges.BuffersWritten())
reg.MustRegister(gauges.DeadTuples())
reg.MustRegister(gauges.HeapBlocksHit())
reg.MustRegister(gauges.HeapBlocksRead())
reg.MustRegister(gauges.IndexScans())
reg.MustRegister(gauges.UnusedIndexes())
reg.MustRegister(gauges.IndexBlocksReadBySchema())
reg.MustRegister(gauges.IndexBlocksHitBySchema())
reg.MustRegister(gauges.IndexBloat())
reg.MustRegister(gauges.Locks())
reg.MustRegister(gauges.NotGrantedLocks())
reg.MustRegister(gauges.DeadLocks())
reg.MustRegister(gauges.ReplicationDelayInSeconds())
reg.MustRegister(gauges.ReplicationDelayInBytes())
reg.MustRegister(gauges.ReplicationStatus())
reg.MustRegister(gauges.Size())
reg.MustRegister(gauges.SlowestQueries())
reg.MustRegister(gauges.StreamingWALs())
reg.MustRegister(gauges.TableBloat())
reg.MustRegister(gauges.TableUsage())
reg.MustRegister(gauges.TempFiles())
reg.MustRegister(gauges.TempSize())
reg.MustRegister(gauges.TransactionsSum())
reg.MustRegister(gauges.Up())
reg.MustRegister(gauges.TableScans())
reg.MustRegister(gauges.TableSizes())
reg.MustRegister(gauges.DatabaseReadingUsage())
reg.MustRegister(gauges.DatabaseWritingUsage())
reg.MustRegister(gauges.HOTUpdates())
reg.MustRegister(gauges.TableLiveRows())
reg.MustRegister(gauges.TableDeadRows())
reg.MustRegister(gauges.DatabaseLiveRows())
reg.MustRegister(gauges.DatabaseDeadRows())
reg.MustRegister(gauges.UnvacuumedTransactions())
reg.MustRegister(gauges.LastTimeVacuumRan())
reg.MustRegister(gauges.LastTimeAutoVacuumRan())
reg.MustRegister(gauges.VacuumRunningTotal())
reg.MustRegister(gauges.ReplicationSlotStatus())
reg.MustRegister(gauges.ReplicationSlotLagInBytes())
}