aboutsummaryrefslogtreecommitdiff
path: root/utils/prometheus-node-exporter-ucode/files/metrics.uc
blob: 3dce77accb520ca2b92e44ff14ea2ec7abd2abd6 (plain)
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
{%
'use strict';

import * as fs from "fs";
import { connect } from "ubus";
import { cursor } from "uci";

function debug(...s) {
	if (global.debug)
		warn("DEBUG: ", ...s, "\n");
}

function puts(...s) {
	return uhttpd.send(...s, "\n");
}

function govalue(value) {
	if (value == Infinity)
		return "+Inf";
	else if (value == -Infinity)
		return "-Inf";
	else if (value != value)
		return "NaN";
	else if (type(value) in [ "int", "double" ])
		return value;
	else if (type(value) in [ "bool", "string" ])
		return +value;

	return null;
}

function metric(name, mtype, help, skipdecl) {
	let func;
	let decl = skipdecl == true ? false : true;

	let yield = function(labels, value) {
		let v = govalue(value);

		if (v == null) {
			debug(`skipping metric: unsupported value '${value}' (${name})`);
			return func;
		}

		let labels_str = "";
		if (length(labels)) {
			let sep = "";
			let s;
			labels_str = "{";
			for (let l in labels) {
				if (labels[l] == null)
					s = "";
				else if (type(labels[l]) == "string") {
					s = labels[l];
					s = replace(labels[l], "\\", "\\\\");
					s = replace(s, "\"", "\\\"");
					s = replace(s, "\n", "\\n");
				} else {
					s = govalue(labels[l]);

					if (!s)
						continue;
				}

				labels_str += sep + l + "=\"" + s + "\"";
				sep = ",";
			}
			labels_str += "}";
		}

		if (decl) {
			if (help)
				puts("# HELP ", name, " ", help);
			puts("# TYPE ", name, " ", mtype);
			decl = false;
		}

		puts(name, labels_str, " ", v);
		return func;
	};

	func = yield;
	return func;
}

function counter(name, help, skipdecl) {
	return metric(name, "counter", help, skipdecl);
}

function gauge(name, help, skipdecl) {
	return metric(name, "gauge", help, skipdecl);
}

function httpstatus(status) {
	puts("Status: ", status, "\nContent-Type: text/plain; version=0.0.4; charset=utf-8\n");
}

function clockdiff(t1, t2) {
	return (t2[0] - t1[0]) * 1000000000 + t2[1] - t1[1];
}

let collectors = {};

global.handle_request = function(env) {
	let scope = {
		config: null,
		fs,
		ubus: connect(),
		counter,
		gauge,
		wsplit: function(line) {
			return split(line, /\s+/);
		},
		nextline: function(f) {
			return rtrim(f.read("line"), "\n");
		},
		oneline: function(fn) {
			let f = fs.open(fn);

			if (!f)
				return null;

			return nextline(f);
		},
		poneline: function(cmd) {
			let f = fs.popen(cmd);

			if (!f)
				return null;

			return nextline(f);
		},
	};

	if (length(collectors) < 1) {
		httpstatus("404 No Collectors found");
		return;
	}

	let cols = [];
	for (let q in split(env.QUERY_STRING, "&")) {
		let s = split(q, "=", 2);
		if (length(s) == 2 && s[0] == "collect") {
			if (!(s[1] in collectors)) {
				httpstatus(`404 Collector ${s[1]} not found`);
				return;
			}

			push(cols, s[1]);
		}
	}

	if (length(cols) > 0)
		cols = uniq(cols);
	else
		cols = keys(collectors);

	httpstatus("200 OK");

	let duration = gauge("node_scrape_collector_duration_seconds");
	let success = gauge("node_scrape_collector_success");

	for (let col in cols) {
		let ok = false;
		let t1, t2;

		scope["config"] = collectors[col].config;
		t1 = clock(true);
		try {
			ok = call(collectors[col].func, null, scope) != false;
		} catch(e) {
			warn(`error running collector '${col}':\n${e.message}\n`);
		}
		t2 = clock(true);

		duration({ collector: col }, clockdiff(t1, t2) / 1000000000.0);
		success({ collector: col }, ok);
	}
};

const lib = "/usr/share/ucode/node-exporter/lib";
const opts = {
	strict_declarations:	true,
	raw_mode:		true,
};

let cols = fs.lsdir(lib, "*.uc");
for (let col in cols) {
	let func;
	let uci = cursor();

	try {
		func = loadfile(lib + "/" + col, opts);
	} catch(e) {
		warn(`error compiling collector '${col}':\n${e.message}\n`);
		continue;
	}

	let name = substr(col, 0, -3);
	let config = uci.get_all("prometheus-node-exporter-ucode", name);
	if (!config || config[".type"] != "collector")
		config = {};
	else {
		delete config[".anonymous"];
		delete config[".type"];
		delete config[".name"];
	}

	collectors[name] = {
		func,
		config,
	};
}

warn(`prometheus-node-exporter-ucode now serving requests with ${length(collectors)} collectors\n`);

if (!("uhttpd" in global)) {
	global.debug = true;

	puts = function(...s) {
		return print(...s, "\n");
	};

	handle_request({
		QUERY_STRING: join("&", map(ARGV, v => "collect=" + v)),
	});
}
%}