1# read output of unbound-control stats
2# and output prometheus metrics style output.
3# use these options:
4#	server:		extended-statistics: yes
5#			statistics-cumulative: no
6#			statistics-interval: 0
7#	remote-control: control-enable: yes
8# Can use it like unbound-control stats | awk -f "metrics.awk"
9
10BEGIN {
11	FS="=";
12}
13# everything like total.num.queries=value is put in val["total.num.queries"]
14/^.*\..*=/ {
15	val[$1]=$2;
16}
17# print the output metrics
18END {
19	print "# HELP unbound_hits_queries Unbound DNS traffic and cache hits"
20	print "# TYPE unbound_hits_queries gauge"
21	print "unbound_hits_queries{type=\"total.num.queries\"} " val["total.num.queries"];
22	for (x=0; x<99; x++) {
23		if(val["thread" $x ".num.queries"] != "") {
24			print "unbound_hits_queries{type=\"thread" $x ".num.queries\"} " val["thread" $x ".num.queries"];
25		}
26	}
27	print "unbound_hits_queries{type=\"total.num.cachehits\"} " val["total.num.cachehits"];
28	print "unbound_hits_queries{type=\"total.num.prefetch\"} " val["total.num.prefetch"];
29	print "unbound_hits_queries{type=\"num.query.tcp\"} " val["num.query.tcp"];
30	print "unbound_hits_queries{type=\"num.query.tcpout\"} " val["num.query.tcpout"];
31	print "unbound_hits_queries{type=\"num.query.tls\"} " val["num.query.tls"];
32	print "unbound_hits_queries{type=\"num.query.tls.resume\"} " val["num.query.tls.resume"];
33	print "unbound_hits_queries{type=\"num.query.ipv6\"} " val["num.query.ipv6"];
34	print "unbound_hits_queries{type=\"unwanted.queries\"} " val["unwanted.queries"];
35	print ""
36
37	print "# HELP unbound_queue_queries Unbound requestlist size"
38	print "# TYPE unbound_queue_queries gauge"
39	print "unbound_queue_queries{type=\"total.requestlist.avg\"} " val["total.requestlist.avg"];
40	print "unbound_queue_queries{type=\"total.requestlist.max\"} " val["total.requestlist.max"];
41	print "unbound_queue_queries{type=\"total.requestlist.overwritten\"} " val["total.requestlist.overwritten"];
42	print "unbound_queue_queries{type=\"total.requestlist.exceeded\"} " val["total.requestlist.exceeded"];
43	print ""
44
45	print "# HELP unbound_memory_bytes Unbound memory usage"
46	print "# TYPE unbound_memory_bytes gauge"
47	print "unbound_memory_bytes{type=\"mem.cache.rrset\"} " val["mem.cache.rrset"];
48	print "unbound_memory_bytes{type=\"mem.cache.message\"} " val["mem.cache.message"];
49	print "unbound_memory_bytes{type=\"mem.mod.iterator\"} " val["mem.mod.iterator"];
50	if(val["mem.mod.validator"] != "") {
51		print "unbound_memory_bytes{type=\"mem.mod.validator\"} " val["mem.mod.validator"];
52	}
53	if(val["mem.mod.respip"] != "") {
54		print "unbound_memory_bytes{type=\"mem.mod.respip\"} " val["mem.mod.respip"];
55	}
56	if(val["mem.mod.subnet"] != "") {
57		print "unbound_memory_bytes{type=\"mem.mod.subnet\"} " val["mem.mod.subnet"];
58	}
59	if(val["mem.mod.ipsecmod"] != "") {
60		print "unbound_memory_bytes{type=\"mem.mod.ipsecmod\"} " val["mem.mod.ipsecmod"];
61	}
62	if(val["mem.mod.dynlibmod"] != "") {
63		print "unbound_memory_bytes{type=\"mem.mod.dynlibmod\"} " val["mem.mod.dynlibmod"];
64	}
65	print "unbound_memory_bytes{type=\"msg.cache.count\"} " val["msg.cache.count"];
66	print "unbound_memory_bytes{type=\"rrset.cache.count\"} " val["rrset.cache.count"];
67	print "unbound_memory_bytes{type=\"infra.cache.count\"} " val["infra.cache.count"];
68	print "unbound_memory_bytes{type=\"key.cache.count\"} " val["key.cache.count"];
69	print ""
70
71	print "# HELP unbound_by_type_queries Unbound DNS queries by type"
72	print "# TYPE unbound_by_type_queries gauge"
73	for(x in val) {
74		if(x ~ /^num.query.type./) {
75			if(val[x] != "") {
76				split(x, a, ".");
77				print "unbound_by_type_queries{type=\"" a[4] "\"} " val[x];
78			}
79		}
80	}
81	print ""
82
83	print "# HELP unbound_by_class_queries Unbound DNS queries by class"
84	print "# TYPE unbound_by_class_queries gauge"
85	for(x in val) {
86		if(x ~ /^num.query.class./) {
87			if(val[x] != "") {
88				split(x, a, ".");
89				print "unbound_by_class_queries{class=\"" a[4] "\"} " val[x];
90			}
91		}
92	}
93	print ""
94
95	print "# HELP unbound_by_opcode_queries Unbound DNS queries by opcode"
96	print "# TYPE unbound_by_opcode_queries gauge"
97	for(x in val) {
98		if(x ~ /^num.query.opcode./) {
99			if(val[x] != "") {
100				split(x, a, ".");
101				print "unbound_by_opcode_queries{opcode=\"" a[4] "\"} " val[x];
102			}
103		}
104	}
105	print ""
106
107	print "# HELP unbound_by_rcode_queries Unbound DNS answers by rcode"
108	print "# TYPE unbound_by_rcode_queries gauge"
109	for(x in val) {
110		if(x ~ /^num.answer.rcode./) {
111			if(val[x] != "") {
112				split(x, a, ".");
113				print "unbound_by_rcode_queries{rcode=\"" a[4] "\"} " val[x];
114			}
115		}
116	}
117	print ""
118
119	print "# HELP unbound_by_flags_queries Unbound DNS queries by flags"
120	print "# TYPE unbound_by_flags_queries gauge"
121	for(x in val) {
122		if(x ~ /^num.query.flags./) {
123			if(val[x] != "") {
124				split(x, a, ".");
125				print "unbound_by_flags_queries{flag=\"" a[4] "\"} " val[x];
126			}
127		}
128	}
129	if(val["num.query.edns.present"] != "") {
130		print "unbound_by_flags_queries{flag=\"num.query.edns.present\"} " val["num.query.edns.present"];
131	}
132	if(val["num.query.edns.DO"] != "") {
133		print "unbound_by_flags_queries{flag=\"num.query.edns.DO\"} " val["num.query.edns.DO"];
134	}
135	print ""
136
137	print "# HELP unbound_histogram_seconds Unbound DNS histogram of reply time"
138	print "# TYPE unbound_histogram_seconds gauge"
139	print "unbound_histogram_seconds{bucket=\"000000.000000.to.000000.000001\"} " val["histogram.000000.000000.to.000000.000001"];
140	print "unbound_histogram_seconds{bucket=\"000000.000001.to.000000.000002\"} " val["histogram.000000.000001.to.000000.000002"];
141	print "unbound_histogram_seconds{bucket=\"000000.000002.to.000000.000004\"} " val["histogram.000000.000002.to.000000.000004"];
142	print "unbound_histogram_seconds{bucket=\"000000.000004.to.000000.000008\"} " val["histogram.000000.000004.to.000000.000008"];
143	print "unbound_histogram_seconds{bucket=\"000000.000008.to.000000.000016\"} " val["histogram.000000.000008.to.000000.000016"];
144	print "unbound_histogram_seconds{bucket=\"000000.000016.to.000000.000032\"} " val["histogram.000000.000016.to.000000.000032"];
145	print "unbound_histogram_seconds{bucket=\"000000.000032.to.000000.000064\"} " val["histogram.000000.000032.to.000000.000064"];
146	print "unbound_histogram_seconds{bucket=\"000000.000064.to.000000.000128\"} " val["histogram.000000.000064.to.000000.000128"];
147	print "unbound_histogram_seconds{bucket=\"000000.000128.to.000000.000256\"} " val["histogram.000000.000128.to.000000.000256"];
148	print "unbound_histogram_seconds{bucket=\"000000.000256.to.000000.000512\"} " val["histogram.000000.000256.to.000000.000512"];
149	print "unbound_histogram_seconds{bucket=\"000000.000512.to.000000.001024\"} " val["histogram.000000.000512.to.000000.001024"];
150	print "unbound_histogram_seconds{bucket=\"000000.001024.to.000000.002048\"} " val["histogram.000000.001024.to.000000.002048"];
151	print "unbound_histogram_seconds{bucket=\"000000.002048.to.000000.004096\"} " val["histogram.000000.002048.to.000000.004096"];
152	print "unbound_histogram_seconds{bucket=\"000000.004096.to.000000.008192\"} " val["histogram.000000.004096.to.000000.008192"];
153	print "unbound_histogram_seconds{bucket=\"000000.008192.to.000000.016384\"} " val["histogram.000000.008192.to.000000.016384"];
154	print "unbound_histogram_seconds{bucket=\"000000.016384.to.000000.032768\"} " val["histogram.000000.016384.to.000000.032768"];
155	print "unbound_histogram_seconds{bucket=\"000000.032768.to.000000.065536\"} " val["histogram.000000.032768.to.000000.065536"];
156	print "unbound_histogram_seconds{bucket=\"000000.065536.to.000000.131072\"} " val["histogram.000000.065536.to.000000.131072"];
157	print "unbound_histogram_seconds{bucket=\"000000.131072.to.000000.262144\"} " val["histogram.000000.131072.to.000000.262144"];
158	print "unbound_histogram_seconds{bucket=\"000000.262144.to.000000.524288\"} " val["histogram.000000.262144.to.000000.524288"];
159	print "unbound_histogram_seconds{bucket=\"000000.524288.to.000001.000000\"} " val["histogram.000000.524288.to.000001.000000"];
160	print "unbound_histogram_seconds{bucket=\"000001.000000.to.000002.000000\"} " val["histogram.000001.000000.to.000002.000000"];
161	print "unbound_histogram_seconds{bucket=\"000002.000000.to.000004.000000\"} " val["histogram.000002.000000.to.000004.000000"];
162	print "unbound_histogram_seconds{bucket=\"000004.000000.to.000008.000000\"} " val["histogram.000004.000000.to.000008.000000"];
163	print "unbound_histogram_seconds{bucket=\"000008.000000.to.000016.000000\"} " val["histogram.000008.000000.to.000016.000000"];
164	print "unbound_histogram_seconds{bucket=\"000016.000000.to.000032.000000\"} " val["histogram.000016.000000.to.000032.000000"];
165	print "unbound_histogram_seconds{bucket=\"000032.000000.to.000064.000000\"} " val["histogram.000032.000000.to.000064.000000"];
166	print "unbound_histogram_seconds{bucket=\"000064.000000.to.000128.000000\"} " val["histogram.000064.000000.to.000128.000000"];
167	print "unbound_histogram_seconds{bucket=\"000128.000000.to.000256.000000\"} " val["histogram.000128.000000.to.000256.000000"];
168	print "unbound_histogram_seconds{bucket=\"000256.000000.to.000512.000000\"} " val["histogram.000256.000000.to.000512.000000"];
169	print "unbound_histogram_seconds{bucket=\"000512.000000.to.001024.000000\"} " val["histogram.000512.000000.to.001024.000000"];
170	print "unbound_histogram_seconds{bucket=\"001024.000000.to.002048.000000\"} " val["histogram.001024.000000.to.002048.000000"];
171	print "unbound_histogram_seconds{bucket=\"002048.000000.to.004096.000000\"} " val["histogram.002048.000000.to.004096.000000"];
172	print "unbound_histogram_seconds{bucket=\"004096.000000.to.008192.000000\"} " val["histogram.004096.000000.to.008192.000000"];
173	print "unbound_histogram_seconds{bucket=\"008192.000000.to.016384.000000\"} " val["histogram.008192.000000.to.016384.000000"];
174	print "unbound_histogram_seconds{bucket=\"016384.000000.to.032768.000000\"} " val["histogram.016384.000000.to.032768.000000"];
175	print "unbound_histogram_seconds{bucket=\"032768.000000.to.065536.000000\"} " val["histogram.032768.000000.to.065536.000000"];
176	print "unbound_histogram_seconds{bucket=\"065536.000000.to.131072.000000\"} " val["histogram.065536.000000.to.131072.000000"];
177	print "unbound_histogram_seconds{bucket=\"131072.000000.to.262144.000000\"} " val["histogram.131072.000000.to.262144.000000"];
178	print "unbound_histogram_seconds{bucket=\"262144.000000.to.524288.000000\"} " val["histogram.262144.000000.to.524288.000000"];
179	print ""
180}
181