systemd-main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "systemd-internals.h"
  3. #include "libnetdata/required_dummies.h"
  4. #define SYSTEMD_JOURNAL_WORKER_THREADS 5
  5. netdata_mutex_t stdout_mutex = NETDATA_MUTEX_INITIALIZER;
  6. static bool plugin_should_exit = false;
  7. int main(int argc __maybe_unused, char **argv __maybe_unused) {
  8. clocks_init();
  9. netdata_thread_set_tag("SDMAIN");
  10. nd_log_initialize_for_external_plugins("systemd-journal.plugin");
  11. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  12. if(verify_netdata_host_prefix(true) == -1) exit(1);
  13. // ------------------------------------------------------------------------
  14. // initialization
  15. netdata_systemd_journal_message_ids_init();
  16. journal_init_query_status();
  17. journal_init_files_and_directories();
  18. // ------------------------------------------------------------------------
  19. // debug
  20. if(argc == 2 && strcmp(argv[1], "debug") == 0) {
  21. journal_files_registry_update();
  22. bool cancelled = false;
  23. char buf[] = "systemd-journal after:-8640000 before:0 direction:backward last:200 data_only:false slice:true source:all";
  24. // char buf[] = "systemd-journal after:1695332964 before:1695937764 direction:backward last:100 slice:true source:all DHKucpqUoe1:PtVoyIuX.MU";
  25. // char buf[] = "systemd-journal after:1694511062 before:1694514662 anchor:1694514122024403";
  26. function_systemd_journal("123", buf, 600, &cancelled);
  27. // function_systemd_units("123", "systemd-units", 600, &cancelled);
  28. exit(1);
  29. }
  30. #ifdef ENABLE_SYSTEMD_DBUS
  31. if(argc == 2 && strcmp(argv[1], "debug-units") == 0) {
  32. bool cancelled = false;
  33. function_systemd_units("123", "systemd-units", 600, &cancelled);
  34. exit(1);
  35. }
  36. #endif
  37. // ------------------------------------------------------------------------
  38. // watcher thread
  39. netdata_thread_t watcher_thread;
  40. netdata_thread_create(&watcher_thread, "SDWATCH",
  41. NETDATA_THREAD_OPTION_DONT_LOG, journal_watcher_main, NULL);
  42. // ------------------------------------------------------------------------
  43. // the event loop for functions
  44. struct functions_evloop_globals *wg =
  45. functions_evloop_init(SYSTEMD_JOURNAL_WORKER_THREADS, "SDJ", &stdout_mutex, &plugin_should_exit);
  46. functions_evloop_add_function(wg, SYSTEMD_JOURNAL_FUNCTION_NAME, function_systemd_journal,
  47. SYSTEMD_JOURNAL_DEFAULT_TIMEOUT);
  48. #ifdef ENABLE_SYSTEMD_DBUS
  49. functions_evloop_add_function(wg, SYSTEMD_UNITS_FUNCTION_NAME, function_systemd_units,
  50. SYSTEMD_UNITS_DEFAULT_TIMEOUT);
  51. #endif
  52. // ------------------------------------------------------------------------
  53. // register functions to netdata
  54. netdata_mutex_lock(&stdout_mutex);
  55. fprintf(stdout, PLUGINSD_KEYWORD_FUNCTION " GLOBAL \"%s\" %d \"%s\"\n",
  56. SYSTEMD_JOURNAL_FUNCTION_NAME, SYSTEMD_JOURNAL_DEFAULT_TIMEOUT, SYSTEMD_JOURNAL_FUNCTION_DESCRIPTION);
  57. #ifdef ENABLE_SYSTEMD_DBUS
  58. fprintf(stdout, PLUGINSD_KEYWORD_FUNCTION " GLOBAL \"%s\" %d \"%s\"\n",
  59. SYSTEMD_UNITS_FUNCTION_NAME, SYSTEMD_UNITS_DEFAULT_TIMEOUT, SYSTEMD_UNITS_FUNCTION_DESCRIPTION);
  60. #endif
  61. fflush(stdout);
  62. netdata_mutex_unlock(&stdout_mutex);
  63. // ------------------------------------------------------------------------
  64. usec_t step_ut = 100 * USEC_PER_MS;
  65. usec_t send_newline_ut = 0;
  66. usec_t since_last_scan_ut = SYSTEMD_JOURNAL_ALL_FILES_SCAN_EVERY_USEC * 2; // something big to trigger scanning at start
  67. bool tty = isatty(fileno(stderr)) == 1;
  68. heartbeat_t hb;
  69. heartbeat_init(&hb);
  70. while(!plugin_should_exit) {
  71. if(since_last_scan_ut > SYSTEMD_JOURNAL_ALL_FILES_SCAN_EVERY_USEC) {
  72. journal_files_registry_update();
  73. since_last_scan_ut = 0;
  74. }
  75. usec_t dt_ut = heartbeat_next(&hb, step_ut);
  76. since_last_scan_ut += dt_ut;
  77. send_newline_ut += dt_ut;
  78. if(!tty && send_newline_ut > USEC_PER_SEC) {
  79. send_newline_and_flush();
  80. send_newline_ut = 0;
  81. }
  82. }
  83. exit(0);
  84. }