forked from jzck/prometheus-upsc-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
upsc_to_prometheus.awk
executable file
·56 lines (50 loc) · 1.34 KB
/
upsc_to_prometheus.awk
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
#!/usr/bin/awk -f
# Rationale:
# The output of upsc contains numerical metrics and strings. The
# numerical ones are made into prometheus series, while the strings
# are made into labels that are attaches to series by prefix.
# $1 == metric name
# $2 == metric value
BEGIN { FS=": "; RS="\n" }
# replace dots with underscores
{gsub(/\./,"_",$1)}
# split outlet_N into outletN to make multiple timeseries
$1 ~ /^outlet_[0-9]+/ {gsub("outlet_","outlet",$1)}
# convert status into number
$1 ~ /ups_status/ {
if ($2 ~ /OL/) {$2 = "0"};
if ($2 ~ /OB/) {$2 = "1"};
}
# numerical values are metrics
{ if ($2 ~ /^[0-9]*\.?[0-9]+$/) {
metrics[$1] = $2
}
# non numerical values are metric labels
else {
prefix = getprefix($1)
gsub(prefix"_","",$1);
label=$1;
value=$2;
if (labels[prefix] != "")
labels[prefix]=labels[prefix]", "
else
labels[prefix]="{"
labels[prefix]=labels[prefix]label"=\""value"\"";
}}
# print all ,etrics with corresponding labels
END {
for (metric in labels) { labels[metric]=labels[metric]"}" }
for (metric in metrics) {
prefix = getprefix(metric)
print "upsc_" metric labels[prefix], metrics[metric]
}
}
function getprefix(str) {
n=split(str,array,"_");
return array[1]
# if (array[1] == "ups")
# prefix=array[1]"_"array[2]
# else
# prefix=array[1]
# return prefix
}