@cryptotaxi247 / netdata-1 / commits / 83c0463e5

systemd-journal and facets: info and sources (#15928)

Costa Tsaousis committed Sep 11, 2023 at 09:43 UTC 83c0463e50f8c8ac5d96256a1453dbc380e85118
3 files changed +56 -21
collectors/systemd-journal.plugin/systemd-journal.c
+31 -3
@@ -33,7 +33,8 @@
33 #define JOURNAL_PARAMETER_HISTOGRAM "histogram"
34 #define JOURNAL_PARAMETER_DIRECTION "direction"
35 #define JOURNAL_PARAMETER_IF_MODIFIED_SINCE "if_modified_since"
36 -
36 +#define JOURNAL_PARAMETER_SOURCE "source"
37 +#define JOURNAL_PARAMETER_INFO "info"
38
39 #define SYSTEMD_ALWAYS_VISIBLE_KEYS NULL
40 #define SYSTEMD_KEYS_EXCLUDED_FROM_FACETS NULL
@@ -353,6 +354,8 @@ static void function_systemd_journal(const char *transaction, char *function, ch
354 SYSTEMD_KEYS_INCLUDED_IN_FACETS,
355 SYSTEMD_KEYS_EXCLUDED_FROM_FACETS);
356
357 + facets_accepted_param(facets, JOURNAL_PARAMETER_INFO);
358 + facets_accepted_param(facets, JOURNAL_PARAMETER_SOURCE);
359 facets_accepted_param(facets, JOURNAL_PARAMETER_AFTER);
360 facets_accepted_param(facets, JOURNAL_PARAMETER_BEFORE);
361 facets_accepted_param(facets, JOURNAL_PARAMETER_ANCHOR);
@@ -389,6 +392,7 @@ static void function_systemd_journal(const char *transaction, char *function, ch
392 facets_register_key_name_transformation(facets, "_GID", FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_FTS,
393 systemd_journal_transform_gid, gids);
394
395 + bool info = false;
396 time_t after_s = 0, before_s = 0;
397 usec_t anchor = 0;
398 usec_t if_modified_since = 0;
@@ -396,6 +400,7 @@ static void function_systemd_journal(const char *transaction, char *function, ch
400 FACETS_ANCHOR_DIRECTION direction = FACETS_ANCHOR_DIRECTION_BACKWARD;
401 const char *query = NULL;
402 const char *chart = NULL;
403 + const char *source = NULL;
404
405 buffer_json_member_add_object(wb, "request");
406
@@ -409,6 +414,12 @@ static void function_systemd_journal(const char *transaction, char *function, ch
414 systemd_journal_function_help(transaction);
415 goto cleanup;
416 }
417 + else if(strcmp(keyword, JOURNAL_PARAMETER_INFO) == 0) {
418 + info = true;
419 + }
420 + else if(strncmp(keyword, JOURNAL_PARAMETER_SOURCE ":", sizeof(JOURNAL_PARAMETER_SOURCE ":") - 1) == 0) {
421 + source = &keyword[sizeof(JOURNAL_PARAMETER_SOURCE ":") - 1];
422 + }
423 else if(strncmp(keyword, JOURNAL_PARAMETER_AFTER ":", sizeof(JOURNAL_PARAMETER_AFTER ":") - 1) == 0) {
424 after_s = str2l(&keyword[sizeof(JOURNAL_PARAMETER_AFTER ":") - 1]);
425 }
@@ -498,6 +509,7 @@ static void function_systemd_journal(const char *transaction, char *function, ch
509 if(!last)
510 last = SYSTEMD_JOURNAL_DEFAULT_ITEMS_PER_QUERY;
511
512 + buffer_json_member_add_string(wb, "source", source ? source : "default");
513 buffer_json_member_add_time_t(wb, "after", after_s);
514 buffer_json_member_add_time_t(wb, "before", before_s);
515 buffer_json_member_add_uint64(wb, "if_modified_since", if_modified_since);
@@ -509,19 +521,35 @@ static void function_systemd_journal(const char *transaction, char *function, ch
521 buffer_json_member_add_time_t(wb, "timeout", timeout);
522 buffer_json_object_close(wb); // request
523
524 + int response;
525 +
526 + if(info) {
527 + facets_accepted_parameters_to_json_array(facets, wb, false);
528 + buffer_json_member_add_array(wb, "sources");
529 + buffer_json_add_array_item_string(wb, "default");
530 + buffer_json_array_close(wb); // sources
531 + buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
532 + buffer_json_member_add_string(wb, "type", "table");
533 + buffer_json_member_add_string(wb, "help", SYSTEMD_JOURNAL_FUNCTION_DESCRIPTION);
534 + buffer_json_finalize(wb);
535 + response = HTTP_RESP_OK;
536 + goto output;
537 + }
538 +
539 facets_set_items(facets, last);
540 facets_set_anchor(facets, anchor, direction);
541 facets_set_query(facets, query);
542 facets_set_histogram(facets, chart ? chart : "PRIORITY", after_s * USEC_PER_SEC, before_s * USEC_PER_SEC);
543
517 - int response = systemd_journal_query(wb, facets, after_s * USEC_PER_SEC, before_s * USEC_PER_SEC,
518 - if_modified_since, now_monotonic_usec() + (timeout - 1) * USEC_PER_SEC);
544 + response = systemd_journal_query(wb, facets, after_s * USEC_PER_SEC, before_s * USEC_PER_SEC,
545 + if_modified_since, now_monotonic_usec() + (timeout - 1) * USEC_PER_SEC);
546
547 if(response != HTTP_RESP_OK) {
548 pluginsd_function_json_error_to_stdout(transaction, response, "failed");
549 goto cleanup;
550 }
551
552 +output:
553 pluginsd_function_result_begin_to_stdout(transaction, response, "application/json", expires);
554 fwrite(buffer_tostring(wb), buffer_strlen(wb), 1, stdout);
555 pluginsd_function_result_end_to_stdout();
libnetdata/facets/facets.c
+24 -18
@@ -1258,36 +1258,42 @@ cleanup:
1258 // ----------------------------------------------------------------------------
1259 // output
1260
1261 -void facets_report(FACETS *facets, BUFFER *wb) {
1262 - buffer_json_member_add_boolean(wb, "show_ids", false);
1263 - buffer_json_member_add_boolean(wb, "has_history", true);
1264 -
1265 - buffer_json_member_add_object(wb, "pagination");
1266 - buffer_json_member_add_boolean(wb, "enabled", true);
1267 - buffer_json_member_add_string(wb, "key", "anchor");
1268 - buffer_json_member_add_string(wb, "column", "timestamp");
1269 - buffer_json_object_close(wb);
1270 -
1261 +void facets_accepted_parameters_to_json_array(FACETS *facets, BUFFER *wb, bool with_keys) {
1262 buffer_json_member_add_array(wb, "accepted_params");
1263 {
1264 if(facets->accepted_params) {
1265 void *t;
1266 dfe_start_read(facets->accepted_params, t) {
1276 - buffer_json_add_array_item_string(wb, t_dfe.name);
1277 - }
1267 + buffer_json_add_array_item_string(wb, t_dfe.name);
1268 + }
1269 dfe_done(t);
1270 }
1271
1281 - FACET_KEY *k;
1282 - dfe_start_read(facets->keys, k) {
1283 - if(!k->values)
1284 - continue;
1272 + if(with_keys) {
1273 + FACET_KEY *k;
1274 + dfe_start_read(facets->keys, k){
1275 + if (!k->values)
1276 + continue;
1277
1286 - buffer_json_add_array_item_string(wb, k_dfe.name);
1278 + buffer_json_add_array_item_string(wb, k_dfe.name);
1279 + }
1280 + dfe_done(k);
1281 }
1288 - dfe_done(k);
1282 }
1283 buffer_json_array_close(wb); // accepted_params
1284 +}
1285 +
1286 +void facets_report(FACETS *facets, BUFFER *wb) {
1287 + buffer_json_member_add_boolean(wb, "show_ids", false);
1288 + buffer_json_member_add_boolean(wb, "has_history", true);
1289 +
1290 + buffer_json_member_add_object(wb, "pagination");
1291 + buffer_json_member_add_boolean(wb, "enabled", true);
1292 + buffer_json_member_add_string(wb, "key", "anchor");
1293 + buffer_json_member_add_string(wb, "column", "timestamp");
1294 + buffer_json_object_close(wb);
1295 +
1296 + facets_accepted_parameters_to_json_array(facets, wb, true);
1297
1298 buffer_json_member_add_array(wb, "facets");
1299 {
libnetdata/facets/facets.h
+1
@@ -67,5 +67,6 @@ void facets_add_key_value(FACETS *facets, const char *key, const char *value);
67 void facets_add_key_value_length(FACETS *facets, const char *key, const char *value, size_t value_len);
68
69 void facets_report(FACETS *facets, BUFFER *wb);
70 +void facets_accepted_parameters_to_json_array(FACETS *facets, BUFFER *wb, bool with_keys);
71
72 #endif