-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgstat_exporter.py
396 lines (360 loc) · 14.7 KB
/
gstat_exporter.py
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from prometheus_client import start_http_server, Gauge
import argparse
import logging
import datetime
from subprocess import Popen, PIPE
from typing import Dict
from importlib.metadata import PackageNotFoundError, version
# get version
try:
__version__ = version("gstat_exporter")
except PackageNotFoundError:
# package is not installed, version unknown
__version__ = "0.0.0"
class GstatExporter:
def __init__(self, interval: int = 30, grace: int = 30, sleep: int = 15) -> None:
"""Define metrics and other neccesary variables."""
# save interval, grace, and sleep
self.interval = interval
self.grace = grace
self.sleep = sleep
# save the version as a class attribute
self.__version__ = __version__
# define the metric labels
self.labels: list[str] = [
"name",
"descr",
"mediasize",
"sectorsize",
"lunid",
"ident",
"rotationrate",
"fwsectors",
"fwheads",
]
# define the metrics
self.metrics: dict[str, Gauge] = {}
self.metrics["up"] = Gauge(
"gstat_up",
"The value of this Gauge is always 1 when the gstat_exporter is up",
)
self.metrics["queue"] = Gauge(
"gstat_queue_depth",
"The queue depth for this GEOM",
self.labels,
)
self.metrics["totalops"] = Gauge(
"gstat_total_operations_per_second",
"The total number of operations/second for this GEOM",
self.labels,
)
self.metrics["readops"] = Gauge(
"gstat_read_operations_per_second",
"The number of read operations/second for this GEOM",
self.labels,
)
self.metrics["readsize"] = Gauge(
"gstat_read_size_kilobytes",
"The size in kilobytes of read operations for this GEOM",
self.labels,
)
self.metrics["readkbs"] = Gauge(
"gstat_read_kilobytes_per_second",
"The speed in kilobytes/second of read operations for this GEOM",
self.labels,
)
self.metrics["readms"] = Gauge(
"gstat_miliseconds_per_read",
"The speed in miliseconds/read operation for this GEOM",
self.labels,
)
self.metrics["writeops"] = Gauge(
"gstat_write_operations_per_second",
"The number of write operations/second for this GEOM",
self.labels,
)
self.metrics["writesize"] = Gauge(
"gstat_write_size_kilobytes",
"The size in kilobytes of write operations for this GEOM",
self.labels,
)
self.metrics["writekbs"] = Gauge(
"gstat_write_kilobytes_per_second",
"The speed in kilobytes/second of write operations for this GEOM",
self.labels,
)
self.metrics["writems"] = Gauge(
"gstat_miliseconds_per_write",
"The speed in miliseconds/write operation for this GEOM",
self.labels,
)
self.metrics["deleteops"] = Gauge(
"gstat_delete_operations_per_second",
"The number of delete operations/second for this GEOM",
self.labels,
)
self.metrics["deletesize"] = Gauge(
"gstat_delete_size_kilobytes",
"The size in kilobytes of delete operations for this GEOM",
self.labels,
)
self.metrics["deletekbs"] = Gauge(
"gstat_delete_kilobytes_per_second",
"The speed in kilobytes/second of delete operations for this GEOM",
self.labels,
)
self.metrics["deletems"] = Gauge(
"gstat_miliseconds_per_delete",
"The speed in miliseconds/delete operation for this GEOM",
self.labels,
)
self.metrics["otherops"] = Gauge(
"gstat_other_operations_per_second",
"The number of other operations (BIO_FLUSH)/second for this GEOM",
self.labels,
)
self.metrics["otherms"] = Gauge(
"gstat_miliseconds_per_other",
"The speed in miliseconds/other operation (BIO_FLUSH) for this GEOM",
self.labels,
)
self.metrics["busy"] = Gauge(
"gstat_percent_busy",
"The percent of the time this GEOM is busy",
self.labels,
)
# start with an empty deviceinfo dict and add devices as we see them
self.deviceinfo: Dict[str, Dict[str, str]] = {}
# variables used for checking for removed devices
self.lastcheck = datetime.datetime.now()
self.timestamps: Dict[str, datetime.datetime] = {}
logging.debug("Done initialising GstatExporter class")
def get_deviceinfo(self, name: str) -> Dict[str, str]:
"""
Return a dict of GEOM device info for GEOM devices in class DISK,
for use as labels for the metrics.
Sample output from the geom command:
$ geom -p ada0
Geom class: DISK
Geom name: ada0
Providers:
1. Name: ada0
Mediasize: 250059350016 (233G)
Sectorsize: 512
Mode: r2w2e4
descr: Samsung SSD 860 EVO mSATA 250GB
lunid: 5002538e700b753f
ident: S41MNG0K907238X
rotationrate: 0
fwsectors: 63
fwheads: 16
$
"""
logging.debug(f"Getting deviceinfo for GEOM {name}...")
with Popen(
["geom", "-p", name], stdout=PIPE, bufsize=1, universal_newlines=True
) as p:
result = {}
for line in p.stdout: # type: ignore
# remove excess whitespace
line = line.strip()
# we only care about the DISK class for now
if line[0:12] == "Geom class: " and line[-4:] != "DISK":
break
if line[0:11] == "Mediasize: ":
result["mediasize"] = line[11:]
if line[0:12] == "Sectorsize: ":
result["sectorsize"] = line.split(" ")[1]
if line[0:7] == "descr: ":
result["descr"] = " ".join(line.split(" ")[1:])
if line[0:7] == "lunid: ":
result["lunid"] = line.split(" ")[1]
if line[0:7] == "ident: ":
result["ident"] = line.split(" ")[1]
if line[0:14] == "rotationrate: ":
result["rotationrate"] = line.split(" ")[1]
if line[0:11] == "fwsectors: ":
result["fwsectors"] = line.split(" ")[1]
if line[0:9] == "fwheads: ":
result["fwheads"] = line.split(" ")[1]
logging.debug(f"Returning deviceinfo for {name}: {result}")
return result
def run_gstat_forever(self) -> None:
"""
Run gstat in a loop and update stats per line
"""
logging.debug(f"Running 'gstat -pdosCI {self.sleep}s' (will loop forever)...")
with Popen(
["gstat", "-pdosCI", f"{self.sleep}s"], stdout=PIPE, bufsize=1, universal_newlines=True
) as p:
# loop over lines in the output
for line in p.stdout: # type: ignore
(
timestamp,
name,
queue_depth,
total_operations_per_second,
read_operations_per_second,
read_size_kilobytes,
read_kilobytes_per_second,
miliseconds_per_read,
write_operations_per_second,
write_size_kilobytes,
write_kilobytes_per_second,
miliseconds_per_write,
delete_operations_per_second,
delete_size_kilobytes,
delete_kilobytes_per_second,
miliseconds_per_delete,
other_operations_per_second,
miliseconds_per_other,
percent_busy,
) = line.split(",")
if timestamp == "timestamp":
# skip header line
continue
# first check if this GEOM has been seen before
if name not in self.deviceinfo:
logging.info(f"Adding new GEOM to deviceinfo: {name}")
# this is the first time we see this GEOM
self.deviceinfo[name] = {}
# we always need a value for all labels
for key in self.labels:
self.deviceinfo[name][key] = ""
# get real info from the device if it is class DISK
self.deviceinfo[name].update(self.get_deviceinfo(name))
self.deviceinfo[name].update({"name": name})
# update timestamp to track when this GEOM was last seen
self.timestamps[name] = datetime.datetime.strptime(
timestamp.split(".")[0], "%Y-%m-%d %H:%M:%S"
)
# up is always.. up
self.metrics["up"].set(1)
self.metrics["queue"].labels(**self.deviceinfo[name]).set(queue_depth)
self.metrics["totalops"].labels(**self.deviceinfo[name]).set(
total_operations_per_second
)
self.metrics["readops"].labels(**self.deviceinfo[name]).set(
read_operations_per_second
)
self.metrics["readsize"].labels(**self.deviceinfo[name]).set(
read_size_kilobytes
)
self.metrics["readkbs"].labels(**self.deviceinfo[name]).set(
read_kilobytes_per_second
)
self.metrics["readms"].labels(**self.deviceinfo[name]).set(
miliseconds_per_read
)
self.metrics["writeops"].labels(**self.deviceinfo[name]).set(
write_operations_per_second
)
self.metrics["writesize"].labels(**self.deviceinfo[name]).set(
write_size_kilobytes
)
self.metrics["writekbs"].labels(**self.deviceinfo[name]).set(
write_kilobytes_per_second
)
self.metrics["writems"].labels(**self.deviceinfo[name]).set(
miliseconds_per_write
)
self.metrics["deleteops"].labels(**self.deviceinfo[name]).set(
delete_operations_per_second
)
self.metrics["deletesize"].labels(**self.deviceinfo[name]).set(
delete_size_kilobytes
)
self.metrics["deletekbs"].labels(**self.deviceinfo[name]).set(
delete_kilobytes_per_second
)
self.metrics["deletems"].labels(**self.deviceinfo[name]).set(
miliseconds_per_delete
)
self.metrics["otherops"].labels(**self.deviceinfo[name]).set(
other_operations_per_second
)
self.metrics["otherms"].labels(**self.deviceinfo[name]).set(
miliseconds_per_other
)
self.metrics["busy"].labels(**self.deviceinfo[name]).set(percent_busy)
# check for removed GEOMs
now = datetime.datetime.now()
if (now - self.lastcheck).seconds > self.interval:
logging.debug("Running periodic check for removed devices...")
# enough time has passed since the last check
# loop over devices and check timestamp for each
remove = []
for name in self.deviceinfo.keys():
delta = (now - self.timestamps[name]).seconds
if delta > self.grace:
remove.append(name)
logging.info(
f"It has been {self.grace} seconds since gstat last reported data for GEOM {name} - removing metrics"
)
# loop over the GEOMs for which gstat stopped giving data and remove them
for name in remove:
# it has been too long since we have seen this GEOM, remove it
for metric in self.metrics.keys():
if metric == "up":
# skip the up metric
continue
self.metrics[metric].remove(*self.deviceinfo[name].values())
del self.deviceinfo[name]
self.lastcheck = datetime.datetime.now()
def main() -> None:
"""Run the main function."""
parser = argparse.ArgumentParser()
parser.add_argument(
"-g",
"--grace",
type=int,
help="Stop exporting metrics for a GEOM after gstat has not reported data from it for this many seconds. Defaults to 30 seconds.",
default=30,
)
parser.add_argument(
"-i",
"--interval",
type=int,
help="How many seconds to wait between checking for removed devices. Defaults to 30 seconds.",
default=30,
)
parser.add_argument(
"-l",
"--listen-ip",
type=str,
help="Listen IP. Defaults to 0.0.0.0 (all v4 IPs). Set to :: to listen on all v6 IPs.",
default="0.0.0.0",
)
parser.add_argument(
"-p",
"--port",
type=int,
help="Portnumber. Defaults to 9248.",
default=9248,
)
parser.add_argument(
"-s",
"--sleep",
type=int,
help="How long should gstat sleep between reporting data, in seconds. Set this to the same as your Prometheus scrape interval. Defaults to 15.",
default=15,
)
parser.add_argument(
"-d",
"--debug",
action="store_const",
dest="loglevel",
const="DEBUG",
help="Debug mode.",
default="INFO",
)
args = parser.parse_args()
logging.basicConfig(level=args.loglevel, datefmt="%Y-%m-%d %H:%M:%S %z", format="%(asctime)s - %(module)s - %(levelname)s - %(message)s")
logging.info(f"Starting gstat_exporter v{__version__} - logging at level {args.loglevel}")
logging.info(f"Starting HTTP listener on address '{args.listen_ip}' port '{args.port}'")
start_http_server(addr=args.listen_ip, port=args.port)
exporter = GstatExporter(interval=args.interval, grace=args.grace, sleep=args.sleep)
while True:
exporter.run_gstat_forever()
if __name__ == "__main__":
main()