Debian Codegraph @

src/core/varlink-metrics.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-json.h"
4 #include "sd-varlink.h"
5
6 #include "hashmap.h"
7 #include "manager.h"
8 #include "metrics.h"
9 #include "service.h"
10 #include "unit-def.h"
11 #include "unit.h"
12 #include "varlink-metrics.h"
13
14 static int active_timestamp_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
15 Manager *manager = ASSERT_PTR(userdata);
16 Unit *unit;
17 char *key;
18 int r;
19
20 assert(mf && mf->name);
21 assert(vl);
22
23 _cleanup_(sd_json_variant_unrefp) sd_json_variant *enter_fields = NULL;
24 r = sd_json_buildo(&enter_fields, SD_JSON_BUILD_PAIR_STRING("event", "enter"));
25 if (r < 0)
26 return r;
27
28 _cleanup_(sd_json_variant_unrefp) sd_json_variant *exit_fields = NULL;
29 r = sd_json_buildo(&exit_fields, SD_JSON_BUILD_PAIR_STRING("event", "exit"));
30 if (r < 0)
31 return r;
32
33 HASHMAP_FOREACH_KEY(unit, key, manager->units) {
34 /* ignore aliases */
35 if (key != unit->id)
36 continue;
37
38 r = metric_build_send_unsigned(
39 mf,
40 vl,
41 unit->id,
42 unit->active_enter_timestamp.realtime,
43 enter_fields);
44 if (r < 0)
45 return r;
46
47 r = metric_build_send_unsigned(
48 mf,
49 vl,
50 unit->id,
51 unit->active_exit_timestamp.realtime,
52 exit_fields);
53 if (r < 0)
54 return r;
55 }
56
57 return 0;
58 }
59
60 static int inactive_exit_timestamp_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
61 Manager *manager = ASSERT_PTR(userdata);
62 Unit *unit;
63 char *key;
64 int r;
65
66 assert(mf && mf->name);
67 assert(vl);
68
69 HASHMAP_FOREACH_KEY(unit, key, manager->units) {
70 /* ignore aliases */
71 if (key != unit->id)
72 continue;
73
74 r = metric_build_send_unsigned(
75 mf,
76 vl,
77 unit->id,
78 unit->inactive_exit_timestamp.realtime,
79 /* fields= */ NULL);
80 if (r < 0)
81 return r;
82 }
83
84 return 0;
85 }
86
87 static int state_change_timestamp_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
88 Manager *manager = ASSERT_PTR(userdata);
89 Unit *unit;
90 char *key;
91 int r;
92
93 assert(mf && mf->name);
94 assert(vl);
95
96 HASHMAP_FOREACH_KEY(unit, key, manager->units) {
97 /* ignore aliases */
98 if (key != unit->id)
99 continue;
100
101 r = metric_build_send_unsigned(
102 mf,
103 vl,
104 unit->id,
105 unit->state_change_timestamp.realtime,
106 /* fields= */ NULL);
107 if (r < 0)
108 return r;
109 }
110
111 return 0;
112 }
113
114 static int status_errno_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
115 Manager *manager = ASSERT_PTR(userdata);
116 int r;
117
118 assert(mf && mf->name);
119 assert(vl);
120
121 LIST_FOREACH(units_by_type, unit, manager->units_by_type[UNIT_SERVICE]) {
122 r = metric_build_send_unsigned(
123 mf,
124 vl,
125 unit->id,
126 (uint64_t) SERVICE(unit)->status_errno,
127 /* fields= */ NULL);
128 if (r < 0)
129 return r;
130 }
131
132 return 0;
133 }
134
135 static int unit_active_state_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
136 Manager *manager = ASSERT_PTR(userdata);
137 Unit *unit;
138 char *key;
139 int r;
140
141 assert(mf && mf->name);
142 assert(vl);
143
144 HASHMAP_FOREACH_KEY(unit, key, manager->units) {
145 /* ignore aliases */
146 if (key != unit->id)
147 continue;
148
149 r = metric_build_send_string(
150 mf,
151 vl,
152 unit->id,
153 unit_active_state_to_string(unit_active_state(unit)),
154 /* fields= */ NULL);
155 if (r < 0)
156 return r;
157 }
158
159 return 0;
160 }
161
162 static int unit_load_state_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
163 Manager *manager = ASSERT_PTR(userdata);
164 Unit *unit;
165 char *key;
166 int r;
167
168 assert(mf && mf->name);
169 assert(vl);
170
171 HASHMAP_FOREACH_KEY(unit, key, manager->units) {
172 /* ignore aliases */
173 if (key != unit->id)
174 continue;
175
176 r = metric_build_send_string(
177 mf,
178 vl,
179 unit->id,
180 unit_load_state_to_string(unit->load_state),
181 /* fields= */ NULL);
182 if (r < 0)
183 return r;
184 }
185
186 return 0;
187 }
188
189 static int nrestarts_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
190 Manager *manager = ASSERT_PTR(userdata);
191 int r;
192
193 assert(mf && mf->name);
194 assert(vl);
195
196 LIST_FOREACH(units_by_type, unit, manager->units_by_type[UNIT_SERVICE]) {
197 r = metric_build_send_unsigned(
198 mf,
199 vl,
200 unit->id,
201 SERVICE(unit)->n_restarts,
202 /* fields= */ NULL);
203 if (r < 0)
204 return r;
205 }
206
207 return 0;
208 }
209
210 static int reload_count_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
211 Manager *manager = ASSERT_PTR(userdata);
212
213 assert(mf && mf->name);
214 assert(vl);
215
216 return metric_build_send_unsigned(
217 mf,
218 vl,
219 /* object= */ NULL,
220 manager->reload_count,
221 /* fields= */ NULL);
222 }
223
224 static int units_by_type_total_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
225 Manager *manager = ASSERT_PTR(userdata);
226 int r;
227
228 assert(mf && mf->name);
229 assert(vl);
230
231 for (UnitType type = 0; type < _UNIT_TYPE_MAX; type++) {
232 _cleanup_(sd_json_variant_unrefp) sd_json_variant *fields = NULL;
233 uint64_t counter = 0;
234
235 LIST_FOREACH(units_by_type, _u, manager->units_by_type[type])
236 counter++;
237
238 r = sd_json_buildo(&fields, SD_JSON_BUILD_PAIR_STRING("type", unit_type_to_string(type)));
239 if (r < 0)
240 return r;
241
242 r = metric_build_send_unsigned(
243 mf,
244 vl,
245 /* object= */ NULL,
246 counter,
247 fields);
248 if (r < 0)
249 return r;
250 }
251
252 return 0;
253 }
254
255 static int units_by_state_total_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
256 Manager *manager = ASSERT_PTR(userdata);
257 uint64_t counters[_UNIT_ACTIVE_STATE_MAX] = {};
258 Unit *unit;
259 char *key;
260 int r;
261
262 assert(mf && mf->name);
263 assert(vl);
264
265 /* TODO need a rework probably with state counter */
266 HASHMAP_FOREACH_KEY(unit, key, manager->units) {
267 /* ignore aliases */
268 if (key != unit->id)
269 continue;
270
271 counters[unit_active_state(unit)]++;
272 }
273
274 for (UnitActiveState state = 0; state < _UNIT_ACTIVE_STATE_MAX; state++) {
275 _cleanup_(sd_json_variant_unrefp) sd_json_variant *fields = NULL;
276
277 r = sd_json_buildo(&fields, SD_JSON_BUILD_PAIR_STRING("state", unit_active_state_to_string(state)));
278 if (r < 0)
279 return r;
280
281 r = metric_build_send_unsigned(
282 mf,
283 vl,
284 /* object= */ NULL,
285 counters[state],
286 fields);
287 if (r < 0)
288 return r;
289 }
290
291 return 0;
292 }
293
294 static int jobs_queued_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
295 Manager *manager = ASSERT_PTR(userdata);
296
297 assert(mf && mf->name);
298 assert(vl);
299
300 return metric_build_send_unsigned(
301 mf,
302 vl,
303 /* object= */ NULL,
304 hashmap_size(manager->jobs),
305 /* fields= */ NULL);
306 }
307
308 static int system_state_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
309 Manager *manager = ASSERT_PTR(userdata);
310
311 assert(mf && mf->name);
312 assert(vl);
313
314 return metric_build_send_string(
315 mf,
316 vl,
317 /* object= */ NULL,
318 manager_state_to_string(manager_state(manager)),
319 /* fields= */ NULL);
320 }
321
322 static int units_by_load_state_total_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
323 Manager *manager = ASSERT_PTR(userdata);
324 uint64_t counters[_UNIT_LOAD_STATE_MAX] = {};
325 Unit *unit;
326 char *key;
327 int r;
328
329 assert(mf && mf->name);
330 assert(vl);
331
332 HASHMAP_FOREACH_KEY(unit, key, manager->units) {
333 /* ignore aliases */
334 if (key != unit->id)
335 continue;
336
337 counters[unit->load_state]++;
338 }
339
340 for (UnitLoadState state = 0; state < _UNIT_LOAD_STATE_MAX; state++) {
341 _cleanup_(sd_json_variant_unrefp) sd_json_variant *fields = NULL;
342
343 r = sd_json_buildo(&fields, SD_JSON_BUILD_PAIR_STRING("load_state", unit_load_state_to_string(state)));
344 if (r < 0)
345 return r;
346
347 r = metric_build_send_unsigned(
348 mf,
349 vl,
350 /* object= */ NULL,
351 counters[state],
352 fields);
353 if (r < 0)
354 return r;
355 }
356
357 return 0;
358 }
359
360 static int units_total_build_json(const MetricFamily *mf, sd_varlink *vl, void *userdata) {
361 Manager *manager = ASSERT_PTR(userdata);
362 uint64_t count = 0;
363 Unit *unit;
364 char *key;
365
366 assert(mf && mf->name);
367 assert(vl);
368
369 HASHMAP_FOREACH_KEY(unit, key, manager->units) {
370 /* ignore aliases */
371 if (key != unit->id)
372 continue;
373
374 count++;
375 }
376
377 return metric_build_send_unsigned(
378 mf,
379 vl,
380 /* object= */ NULL,
381 count,
382 /* fields= */ NULL);
383 }
384
385 static const MetricFamily metric_family_table[] = {
386 /* Keep metrics ordered alphabetically */
387 {
388 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "ActiveTimestamp",
389 .description = "Per unit metric: timestamp of active state transitions in microseconds; 0 indicates the transition has not occurred",
390 .type = METRIC_FAMILY_TYPE_GAUGE,
391 .generate = active_timestamp_build_json,
392 },
393 {
394 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "InactiveExitTimestamp",
395 .description = "Per unit metric: timestamp when the unit last exited the inactive state in microseconds; 0 indicates the transition has not occurred",
396 .type = METRIC_FAMILY_TYPE_GAUGE,
397 .generate = inactive_exit_timestamp_build_json,
398 },
399 {
400 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "JobsQueued",
401 .description = "Number of jobs currently queued",
402 .type = METRIC_FAMILY_TYPE_GAUGE,
403 .generate = jobs_queued_build_json,
404 },
405 {
406 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "NRestarts",
407 .description = "Per unit metric: number of restarts",
408 .type = METRIC_FAMILY_TYPE_COUNTER,
409 .generate = nrestarts_build_json,
410 },
411 {
412 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "ReloadCount",
413 .description = "Number of successful manager reloads since startup; resets across daemon-reexec",
414 .type = METRIC_FAMILY_TYPE_COUNTER,
415 .generate = reload_count_build_json,
416 },
417 {
418 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "StateChangeTimestamp",
419 .description = "Per unit metric: timestamp of the last state change in microseconds; 0 indicates no state change has occurred",
420 .type = METRIC_FAMILY_TYPE_GAUGE,
421 .generate = state_change_timestamp_build_json,
422 },
423 {
424 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "StatusErrno",
425 .description = "Per service metric: errno status of the service",
426 .type = METRIC_FAMILY_TYPE_GAUGE,
427 .generate = status_errno_build_json,
428 },
429 {
430 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "SystemState",
431 .description = "Overall system state",
432 .type = METRIC_FAMILY_TYPE_STRING,
433 .generate = system_state_build_json,
434 },
435 {
436 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "UnitActiveState",
437 .description = "Per unit metric: active state",
438 .type = METRIC_FAMILY_TYPE_STRING,
439 .generate = unit_active_state_build_json,
440 },
441 {
442 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "UnitLoadState",
443 .description = "Per unit metric: load state",
444 .type = METRIC_FAMILY_TYPE_STRING,
445 .generate = unit_load_state_build_json,
446 },
447 {
448 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "UnitsByLoadStateTotal",
449 .description = "Total number of units by load state",
450 .type = METRIC_FAMILY_TYPE_GAUGE,
451 .generate = units_by_load_state_total_build_json,
452 },
453 {
454 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "UnitsByStateTotal",
455 .description = "Total number of units of different state",
456 .type = METRIC_FAMILY_TYPE_GAUGE,
457 .generate = units_by_state_total_build_json,
458 },
459 {
460 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "UnitsByTypeTotal",
461 .description = "Total number of units of different types",
462 .type = METRIC_FAMILY_TYPE_GAUGE,
463 .generate = units_by_type_total_build_json,
464 },
465 {
466 .name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "UnitsTotal",
467 .description = "Total number of units",
468 .type = METRIC_FAMILY_TYPE_GAUGE,
469 .generate = units_total_build_json,
470 },
471 {}
472 };
473
474 int vl_method_describe_metrics(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
475 return metrics_method_describe(metric_family_table, link, parameters, flags, userdata);
476 }
477
478 int vl_method_list_metrics(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
479 return metrics_method_list(metric_family_table, link, parameters, flags, userdata);
480 }