-
Notifications
You must be signed in to change notification settings - Fork 0
/
borg_exporter
executable file
·98 lines (76 loc) · 2.91 KB
/
borg_exporter
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
#!/usr/bin/env python3
# Borg metrics exporter for prometheus
# Reads resuls of `borg info --json ...` from stdin and writes metrics on
# stdout
import argparse
import json
import os
import platform
import sys
from prometheus_client import Gauge, generate_latest, push_to_gateway
from prometheus_client.core import REGISTRY
def gen_metrics(stats):
def get(path):
mapping = stats
for k in path.split("."):
mapping = mapping[k]
return mapping
def gauge(path, desc, prefix="borg"):
name = "_".join([prefix, *path.split(".")])
value = get(path)
Gauge(name, desc, labels.keys()).labels(**labels).set(value)
labels = {
"id": get("repository.id"),
"location": get("repository.location"),
"encryption": get("encryption.mode"),
"hostname": platform.node(),
}
# available in create's output, ignore if absent
try:
gauge("archive.duration", "Duration of archive creation")
gauge("archive.stats.nfiles", "Number of files in archive")
gauge("archive.stats.original_size", "Original size of archive")
gauge("archive.stats.compressed_size", "Compressed size of archive")
gauge("archive.stats.deduplicated_size", "Deduplicated size of archive")
except:
pass
gauge("cache.stats.total_chunks", "Repository total chunks")
gauge("cache.stats.total_unique_chunks", "Repository unique chunks")
gauge("cache.stats.total_size", "All archives original size")
gauge("cache.stats.total_csize", "All archives compressed size")
gauge("cache.stats.unique_size", "All archives deduplicated size")
gauge("cache.stats.unique_csize", "All archives deduplicated compressed size")
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="borg_exporter")
group = parser.add_mutually_exclusive_group()
group.add_argument("-o", "--output", help="output file, `-` to write to stdout")
group.add_argument(
"-p",
"--push",
const="pushgateway:9091",
metavar="TARGET:PORT",
default=None,
action="store",
nargs="?",
help="Push metrics to target instead of writing to a file. "
+ "If this option is used without specifying a target, "
+ "`pushgateway:9091` is used by default",
)
options = parser.parse_args()
stats = json.load(sys.stdin)
# borg outputs a json object, bormatic exports an array of borg output
if not isinstance(stats, list):
stats = [stats]
for s in stats:
gen_metrics(s)
if options.push is not None:
push_to_gateway(options.push, job="borg", registry=REGISTRY)
else:
metrics_bytes = generate_latest()
output = options.output
if output is None or output == "-":
out = sys.stdout.buffer
else:
out = open(options.output, "wb")
with out:
out.write(metrics_bytes)