forked from prometheus/mysqld_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqld_exporter.go
256 lines (238 loc) · 9.52 KB
/
mysqld_exporter.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"fmt"
"net/http"
"os"
"path"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/ini.v1"
"github.com/prometheus/mysqld_exporter/collector"
)
var (
listenAddress = kingpin.Flag(
"web.listen-address",
"Address to listen on for web interface and telemetry.",
).Default(":9104").String()
metricPath = kingpin.Flag(
"web.telemetry-path",
"Path under which to expose metrics.",
).Default("/metrics").String()
configMycnf = kingpin.Flag(
"config.my-cnf",
"Path to .my.cnf file to read MySQL credentials from.",
).Default(path.Join(os.Getenv("HOME"), ".my.cnf")).String()
collectProcesslist = kingpin.Flag(
"collect.info_schema.processlist",
"Collect current thread state counts from the information_schema.processlist",
).Default("false").Bool()
collectTableSchema = kingpin.Flag(
"collect.info_schema.tables",
"Collect metrics from information_schema.tables",
).Default("true").Bool()
collectInnodbTablespaces = kingpin.Flag(
"collect.info_schema.innodb_tablespaces",
"Collect metrics from information_schema.innodb_sys_tablespaces",
).Default("false").Bool()
collectInnodbMetrics = kingpin.Flag(
"collect.info_schema.innodb_metrics",
"Collect metrics from information_schema.innodb_metrics",
).Default("false").Bool()
collectGlobalStatus = kingpin.Flag(
"collect.global_status",
"Collect from SHOW GLOBAL STATUS",
).Default("true").Bool()
collectGlobalVariables = kingpin.Flag(
"collect.global_variables",
"Collect from SHOW GLOBAL VARIABLES",
).Default("true").Bool()
collectSlaveStatus = kingpin.Flag(
"collect.slave_status",
"Collect from SHOW SLAVE STATUS",
).Default("true").Bool()
collectAutoIncrementColumns = kingpin.Flag(
"collect.auto_increment.columns",
"Collect auto_increment columns and max values from information_schema",
).Default("false").Bool()
collectBinlogSize = kingpin.Flag(
"collect.binlog_size",
"Collect the current size of all registered binlog files",
).Default("false").Bool()
collectPerfTableIOWaits = kingpin.Flag(
"collect.perf_schema.tableiowaits",
"Collect metrics from performance_schema.table_io_waits_summary_by_table",
).Default("false").Bool()
collectPerfIndexIOWaits = kingpin.Flag(
"collect.perf_schema.indexiowaits",
"Collect metrics from performance_schema.table_io_waits_summary_by_index_usage",
).Default("false").Bool()
collectPerfTableLockWaits = kingpin.Flag(
"collect.perf_schema.tablelocks",
"Collect metrics from performance_schema.table_lock_waits_summary_by_table",
).Default("false").Bool()
collectPerfEventsStatements = kingpin.Flag(
"collect.perf_schema.eventsstatements",
"Collect metrics from performance_schema.events_statements_summary_by_digest",
).Default("false").Bool()
collectPerfEventsWaits = kingpin.Flag(
"collect.perf_schema.eventswaits",
"Collect metrics from performance_schema.events_waits_summary_global_by_event_name",
).Default("false").Bool()
collectPerfFileEvents = kingpin.Flag(
"collect.perf_schema.file_events",
"Collect metrics from performance_schema.file_summary_by_event_name",
).Default("false").Bool()
collectPerfFileInstances = kingpin.Flag(
"collect.perf_schema.file_instances",
"Collect metrics from performance_schema.file_summary_by_instance",
).Default("false").Bool()
collectUserStat = kingpin.Flag(
"collect.info_schema.userstats",
"If running with userstat=1, set to true to collect user statistics",
).Default("false").Bool()
collectClientStat = kingpin.Flag(
"collect.info_schema.clientstats",
"If running with userstat=1, set to true to collect client statistics",
).Default("false").Bool()
collectTableStat = kingpin.Flag(
"collect.info_schema.tablestats",
"If running with userstat=1, set to true to collect table statistics",
).Default("false").Bool()
collectQueryResponseTime = kingpin.Flag(
"collect.info_schema.query_response_time",
"Collect query response time distribution if query_response_time_stats is ON.",
).Default("false").Bool()
collectEngineTokudbStatus = kingpin.Flag(
"collect.engine_tokudb_status",
"Collect from SHOW ENGINE TOKUDB STATUS",
).Default("false").Bool()
collectEngineInnodbStatus = kingpin.Flag(
"collect.engine_innodb_status",
"Collect from SHOW ENGINE INNODB STATUS",
).Default("false").Bool()
collectHeartbeat = kingpin.Flag(
"collect.heartbeat",
"Collect from heartbeat",
).Default("false").Bool()
collectHeartbeatDatabase = kingpin.Flag(
"collect.heartbeat.database",
"Database from where to collect heartbeat data",
).Default("heartbeat").String()
collectHeartbeatTable = kingpin.Flag(
"collect.heartbeat.table",
"Table from where to collect heartbeat data",
).Default("heartbeat").String()
dsn string
)
func parseMycnf(config interface{}) (string, error) {
var dsn string
cfg, err := ini.Load(config)
if err != nil {
return dsn, fmt.Errorf("failed reading ini file: %s", err)
}
user := cfg.Section("client").Key("user").String()
password := cfg.Section("client").Key("password").String()
if (user == "") || (password == "") {
return dsn, fmt.Errorf("no user or password specified under [client] in %s", config)
}
host := cfg.Section("client").Key("host").MustString("localhost")
port := cfg.Section("client").Key("port").MustUint(3306)
socket := cfg.Section("client").Key("socket").String()
if socket != "" {
dsn = fmt.Sprintf("%s:%s@unix(%s)/", user, password, socket)
} else {
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/", user, password, host, port)
}
log.Debugln(dsn)
return dsn, nil
}
func init() {
prometheus.MustRegister(version.NewCollector("mysqld_exporter"))
}
func filter(filters map[string]bool, name string, flag bool) bool {
if len(filters) > 0 {
return flag && filters[name]
}
return flag
}
func handler(w http.ResponseWriter, r *http.Request) {
var filters map[string]bool
params := r.URL.Query()["collect[]"]
log.Debugln("collect query:", params)
if len(params) > 0 {
filters = make(map[string]bool)
for _, param := range params {
filters[param] = true
}
}
collect := collector.Collect{
Processlist: filter(filters, "info_schema.processlist", *collectProcesslist),
TableSchema: filter(filters, "info_schema.tables", *collectTableSchema),
InnodbTablespaces: filter(filters, "info_schema.innodb_tablespaces", *collectInnodbTablespaces),
InnodbMetrics: filter(filters, "info_schema.innodb_metrics", *collectInnodbMetrics),
GlobalStatus: filter(filters, "global_status", *collectGlobalStatus),
GlobalVariables: filter(filters, "global_variables", *collectGlobalVariables),
SlaveStatus: filter(filters, "slave_status", *collectSlaveStatus),
AutoIncrementColumns: filter(filters, "auto_increment.columns", *collectAutoIncrementColumns),
BinlogSize: filter(filters, "binlog_size", *collectBinlogSize),
PerfTableIOWaits: filter(filters, "perf_schema.tableiowaits", *collectPerfTableIOWaits),
PerfIndexIOWaits: filter(filters, "perf_schema.indexiowaits", *collectPerfIndexIOWaits),
PerfTableLockWaits: filter(filters, "perf_schema.tablelocks", *collectPerfTableLockWaits),
PerfEventsStatements: filter(filters, "perf_schema.eventsstatements", *collectPerfEventsStatements),
PerfEventsWaits: filter(filters, "perf_schema.eventswaits", *collectPerfEventsWaits),
PerfFileEvents: filter(filters, "perf_schema.file_events", *collectPerfFileEvents),
PerfFileInstances: filter(filters, "perf_schema.file_instances", *collectPerfFileInstances),
UserStat: filter(filters, "info_schema.userstats", *collectUserStat),
ClientStat: filter(filters, "info_schema.clientstats", *collectClientStat),
TableStat: filter(filters, "info_schema.tablestats", *collectTableStat),
QueryResponseTime: filter(filters, "info_schema.query_response_time", *collectQueryResponseTime),
EngineTokudbStatus: filter(filters, "engine_tokudb_status", *collectEngineTokudbStatus),
EngineInnodbStatus: filter(filters, "engine_innodb_status", *collectEngineInnodbStatus),
Heartbeat: filter(filters, "heartbeat", *collectHeartbeat),
HeartbeatDatabase: *collectHeartbeatDatabase,
HeartbeatTable: *collectHeartbeatTable,
}
registry := prometheus.NewRegistry()
registry.MustRegister(collector.New(dsn, collect))
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
// Delegate http serving to Prometheus client library, which will call collector.Collect.
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
func main() {
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("mysqld_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
// landingPage contains the HTML served at '/'.
// TODO: Make this nicer and more informative.
var landingPage = []byte(`<html>
<head><title>MySQLd exporter</title></head>
<body>
<h1>MySQLd exporter</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`)
log.Infoln("Starting mysqld_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
dsn = os.Getenv("DATA_SOURCE_NAME")
if len(dsn) == 0 {
var err error
if dsn, err = parseMycnf(*configMycnf); err != nil {
log.Fatal(err)
}
}
http.HandleFunc(*metricPath, prometheus.InstrumentHandlerFunc("metrics", handler))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(landingPage)
})
log.Infoln("Listening on", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}