systemd-journal.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /*
  3. * netdata systemd-journal.plugin
  4. * Copyright (C) 2023 Netdata Inc.
  5. * GPL v3+
  6. */
  7. #include "systemd-internals.h"
  8. /*
  9. * TODO
  10. *
  11. * _UDEV_DEVLINK is frequently set more than once per field - support multi-value faces
  12. *
  13. */
  14. #define FACET_MAX_VALUE_LENGTH 8192
  15. #define SYSTEMD_JOURNAL_FUNCTION_DESCRIPTION "View, search and analyze systemd journal entries."
  16. #define SYSTEMD_JOURNAL_FUNCTION_NAME "systemd-journal"
  17. #define SYSTEMD_JOURNAL_DEFAULT_TIMEOUT 60
  18. #define SYSTEMD_JOURNAL_MAX_PARAMS 1000
  19. #define SYSTEMD_JOURNAL_DEFAULT_QUERY_DURATION (1 * 3600)
  20. #define SYSTEMD_JOURNAL_DEFAULT_ITEMS_PER_QUERY 200
  21. #define SYSTEMD_JOURNAL_DEFAULT_ITEMS_SAMPLING 1000000
  22. #define SYSTEMD_JOURNAL_SAMPLING_SLOTS 1000
  23. #define SYSTEMD_JOURNAL_SAMPLING_RECALIBRATE 10000
  24. #define SYSTEMD_JOURNAL_PROGRESS_EVERY_UT (250 * USEC_PER_MS)
  25. #define JOURNAL_PARAMETER_HELP "help"
  26. #define JOURNAL_PARAMETER_AFTER "after"
  27. #define JOURNAL_PARAMETER_BEFORE "before"
  28. #define JOURNAL_PARAMETER_ANCHOR "anchor"
  29. #define JOURNAL_PARAMETER_LAST "last"
  30. #define JOURNAL_PARAMETER_QUERY "query"
  31. #define JOURNAL_PARAMETER_FACETS "facets"
  32. #define JOURNAL_PARAMETER_HISTOGRAM "histogram"
  33. #define JOURNAL_PARAMETER_DIRECTION "direction"
  34. #define JOURNAL_PARAMETER_IF_MODIFIED_SINCE "if_modified_since"
  35. #define JOURNAL_PARAMETER_DATA_ONLY "data_only"
  36. #define JOURNAL_PARAMETER_SOURCE "source"
  37. #define JOURNAL_PARAMETER_INFO "info"
  38. #define JOURNAL_PARAMETER_SLICE "slice"
  39. #define JOURNAL_PARAMETER_DELTA "delta"
  40. #define JOURNAL_PARAMETER_TAIL "tail"
  41. #define JOURNAL_PARAMETER_SAMPLING "sampling"
  42. #define JOURNAL_KEY_ND_JOURNAL_FILE "ND_JOURNAL_FILE"
  43. #define JOURNAL_KEY_ND_JOURNAL_PROCESS "ND_JOURNAL_PROCESS"
  44. #define JOURNAL_DEFAULT_SLICE_MODE true
  45. #define JOURNAL_DEFAULT_DIRECTION FACETS_ANCHOR_DIRECTION_BACKWARD
  46. #define SYSTEMD_ALWAYS_VISIBLE_KEYS NULL
  47. #define SYSTEMD_KEYS_EXCLUDED_FROM_FACETS \
  48. "!MESSAGE_ID" \
  49. "|*MESSAGE*" \
  50. "|*_RAW" \
  51. "|*_USEC" \
  52. "|*_NSEC" \
  53. "|*TIMESTAMP*" \
  54. "|*_ID" \
  55. "|*_ID_*" \
  56. "|__*" \
  57. ""
  58. #define SYSTEMD_KEYS_INCLUDED_IN_FACETS \
  59. \
  60. /* --- USER JOURNAL FIELDS --- */ \
  61. \
  62. /* "|MESSAGE" */ \
  63. "|MESSAGE_ID" \
  64. "|PRIORITY" \
  65. "|CODE_FILE" \
  66. /* "|CODE_LINE" */ \
  67. "|CODE_FUNC" \
  68. "|ERRNO" \
  69. /* "|INVOCATION_ID" */ \
  70. /* "|USER_INVOCATION_ID" */ \
  71. "|SYSLOG_FACILITY" \
  72. "|SYSLOG_IDENTIFIER" \
  73. /* "|SYSLOG_PID" */ \
  74. /* "|SYSLOG_TIMESTAMP" */ \
  75. /* "|SYSLOG_RAW" */ \
  76. /* "!DOCUMENTATION" */ \
  77. /* "|TID" */ \
  78. "|UNIT" \
  79. "|USER_UNIT" \
  80. "|UNIT_RESULT" /* undocumented */ \
  81. \
  82. \
  83. /* --- TRUSTED JOURNAL FIELDS --- */ \
  84. \
  85. /* "|_PID" */ \
  86. "|_UID" \
  87. "|_GID" \
  88. "|_COMM" \
  89. "|_EXE" \
  90. /* "|_CMDLINE" */ \
  91. "|_CAP_EFFECTIVE" \
  92. /* "|_AUDIT_SESSION" */ \
  93. "|_AUDIT_LOGINUID" \
  94. "|_SYSTEMD_CGROUP" \
  95. "|_SYSTEMD_SLICE" \
  96. "|_SYSTEMD_UNIT" \
  97. "|_SYSTEMD_USER_UNIT" \
  98. "|_SYSTEMD_USER_SLICE" \
  99. "|_SYSTEMD_SESSION" \
  100. "|_SYSTEMD_OWNER_UID" \
  101. "|_SELINUX_CONTEXT" \
  102. /* "|_SOURCE_REALTIME_TIMESTAMP" */ \
  103. "|_BOOT_ID" \
  104. "|_MACHINE_ID" \
  105. /* "|_SYSTEMD_INVOCATION_ID" */ \
  106. "|_HOSTNAME" \
  107. "|_TRANSPORT" \
  108. "|_STREAM_ID" \
  109. /* "|LINE_BREAK" */ \
  110. "|_NAMESPACE" \
  111. "|_RUNTIME_SCOPE" \
  112. \
  113. \
  114. /* --- KERNEL JOURNAL FIELDS --- */ \
  115. \
  116. /* "|_KERNEL_DEVICE" */ \
  117. "|_KERNEL_SUBSYSTEM" \
  118. /* "|_UDEV_SYSNAME" */ \
  119. "|_UDEV_DEVNODE" \
  120. /* "|_UDEV_DEVLINK" */ \
  121. \
  122. \
  123. /* --- LOGGING ON BEHALF --- */ \
  124. \
  125. "|OBJECT_UID" \
  126. "|OBJECT_GID" \
  127. "|OBJECT_COMM" \
  128. "|OBJECT_EXE" \
  129. /* "|OBJECT_CMDLINE" */ \
  130. /* "|OBJECT_AUDIT_SESSION" */ \
  131. "|OBJECT_AUDIT_LOGINUID" \
  132. "|OBJECT_SYSTEMD_CGROUP" \
  133. "|OBJECT_SYSTEMD_SESSION" \
  134. "|OBJECT_SYSTEMD_OWNER_UID" \
  135. "|OBJECT_SYSTEMD_UNIT" \
  136. "|OBJECT_SYSTEMD_USER_UNIT" \
  137. \
  138. \
  139. /* --- CORE DUMPS --- */ \
  140. \
  141. "|COREDUMP_COMM" \
  142. "|COREDUMP_UNIT" \
  143. "|COREDUMP_USER_UNIT" \
  144. "|COREDUMP_SIGNAL_NAME" \
  145. "|COREDUMP_CGROUP" \
  146. \
  147. \
  148. /* --- DOCKER --- */ \
  149. \
  150. "|CONTAINER_ID" \
  151. /* "|CONTAINER_ID_FULL" */ \
  152. "|CONTAINER_NAME" \
  153. "|CONTAINER_TAG" \
  154. "|IMAGE_NAME" /* undocumented */ \
  155. /* "|CONTAINER_PARTIAL_MESSAGE" */ \
  156. \
  157. \
  158. /* --- NETDATA --- */ \
  159. \
  160. "|ND_NIDL_NODE" \
  161. "|ND_NIDL_CONTEXT" \
  162. "|ND_LOG_SOURCE" \
  163. /*"|ND_MODULE" */ \
  164. "|ND_ALERT_NAME" \
  165. "|ND_ALERT_CLASS" \
  166. "|ND_ALERT_COMPONENT" \
  167. "|ND_ALERT_TYPE" \
  168. \
  169. ""
  170. // ----------------------------------------------------------------------------
  171. typedef struct function_query_status {
  172. bool *cancelled; // a pointer to the cancelling boolean
  173. usec_t *stop_monotonic_ut;
  174. // request
  175. const char *transaction;
  176. SD_JOURNAL_FILE_SOURCE_TYPE source_type;
  177. SIMPLE_PATTERN *sources;
  178. usec_t after_ut;
  179. usec_t before_ut;
  180. struct {
  181. usec_t start_ut;
  182. usec_t stop_ut;
  183. } anchor;
  184. FACETS_ANCHOR_DIRECTION direction;
  185. size_t entries;
  186. usec_t if_modified_since;
  187. bool delta;
  188. bool tail;
  189. bool data_only;
  190. bool slice;
  191. size_t sampling;
  192. size_t filters;
  193. usec_t last_modified;
  194. const char *query;
  195. const char *histogram;
  196. struct {
  197. usec_t start_ut; // the starting time of the query - we start from this
  198. usec_t stop_ut; // the ending time of the query - we stop at this
  199. usec_t first_msg_ut;
  200. sd_id128_t first_msg_writer;
  201. uint64_t first_msg_seqnum;
  202. } query_file;
  203. struct {
  204. uint32_t enable_after_samples;
  205. uint32_t slots;
  206. uint32_t sampled;
  207. uint32_t unsampled;
  208. uint32_t estimated;
  209. } samples;
  210. struct {
  211. uint32_t enable_after_samples;
  212. uint32_t every;
  213. uint32_t skipped;
  214. uint32_t recalibrate;
  215. uint32_t sampled;
  216. uint32_t unsampled;
  217. uint32_t estimated;
  218. } samples_per_file;
  219. struct {
  220. usec_t start_ut;
  221. usec_t end_ut;
  222. usec_t step_ut;
  223. uint32_t enable_after_samples;
  224. uint32_t sampled[SYSTEMD_JOURNAL_SAMPLING_SLOTS];
  225. uint32_t unsampled[SYSTEMD_JOURNAL_SAMPLING_SLOTS];
  226. } samples_per_time_slot;
  227. // per file progress info
  228. // size_t cached_count;
  229. // progress statistics
  230. usec_t matches_setup_ut;
  231. size_t rows_useful;
  232. size_t rows_read;
  233. size_t bytes_read;
  234. size_t files_matched;
  235. size_t file_working;
  236. } FUNCTION_QUERY_STATUS;
  237. static void log_fqs(FUNCTION_QUERY_STATUS *fqs, const char *msg) {
  238. netdata_log_error("ERROR: %s, on query "
  239. "timeframe [%"PRIu64" - %"PRIu64"], "
  240. "anchor [%"PRIu64" - %"PRIu64"], "
  241. "if_modified_since %"PRIu64", "
  242. "data_only:%s, delta:%s, tail:%s, direction:%s"
  243. , msg
  244. , fqs->after_ut, fqs->before_ut
  245. , fqs->anchor.start_ut, fqs->anchor.stop_ut
  246. , fqs->if_modified_since
  247. , fqs->data_only ? "true" : "false"
  248. , fqs->delta ? "true" : "false"
  249. , fqs->tail ? "tail" : "false"
  250. , fqs->direction == FACETS_ANCHOR_DIRECTION_FORWARD ? "forward" : "backward");
  251. }
  252. static inline bool netdata_systemd_journal_seek_to(sd_journal *j, usec_t timestamp) {
  253. if(sd_journal_seek_realtime_usec(j, timestamp) < 0) {
  254. netdata_log_error("SYSTEMD-JOURNAL: Failed to seek to %" PRIu64, timestamp);
  255. if(sd_journal_seek_tail(j) < 0) {
  256. netdata_log_error("SYSTEMD-JOURNAL: Failed to seek to journal's tail");
  257. return false;
  258. }
  259. }
  260. return true;
  261. }
  262. #define JD_SOURCE_REALTIME_TIMESTAMP "_SOURCE_REALTIME_TIMESTAMP"
  263. // ----------------------------------------------------------------------------
  264. // sampling support
  265. static void sampling_query_init(FUNCTION_QUERY_STATUS *fqs, FACETS *facets) {
  266. if(!fqs->sampling)
  267. return;
  268. if(!fqs->slice) {
  269. // the user is doing a full data query
  270. // disable sampling
  271. fqs->sampling = 0;
  272. return;
  273. }
  274. if(fqs->data_only) {
  275. // the user is doing a data query
  276. // disable sampling
  277. fqs->sampling = 0;
  278. return;
  279. }
  280. if(!fqs->files_matched) {
  281. // no files have been matched
  282. // disable sampling
  283. fqs->sampling = 0;
  284. return;
  285. }
  286. fqs->samples.slots = facets_histogram_slots(facets);
  287. if(fqs->samples.slots < 2) fqs->samples.slots = 2;
  288. if(fqs->samples.slots > SYSTEMD_JOURNAL_SAMPLING_SLOTS)
  289. fqs->samples.slots = SYSTEMD_JOURNAL_SAMPLING_SLOTS;
  290. if(!fqs->after_ut || !fqs->before_ut || fqs->after_ut >= fqs->before_ut) {
  291. // we don't have enough information for sampling
  292. fqs->sampling = 0;
  293. return;
  294. }
  295. usec_t delta = fqs->before_ut - fqs->after_ut;
  296. usec_t step = delta / facets_histogram_slots(facets) - 1;
  297. if(step < 1) step = 1;
  298. fqs->samples_per_time_slot.start_ut = fqs->after_ut;
  299. fqs->samples_per_time_slot.end_ut = fqs->before_ut;
  300. fqs->samples_per_time_slot.step_ut = step;
  301. // the minimum number of rows to enable sampling
  302. fqs->samples.enable_after_samples = fqs->sampling / 2;
  303. size_t files_matched = fqs->files_matched;
  304. if(!files_matched)
  305. files_matched = 1;
  306. // the minimum number of rows per file to enable sampling
  307. fqs->samples_per_file.enable_after_samples = (fqs->sampling / 4) / files_matched;
  308. if(fqs->samples_per_file.enable_after_samples < fqs->entries)
  309. fqs->samples_per_file.enable_after_samples = fqs->entries;
  310. // the minimum number of rows per time slot to enable sampling
  311. fqs->samples_per_time_slot.enable_after_samples = (fqs->sampling / 4) / fqs->samples.slots;
  312. if(fqs->samples_per_time_slot.enable_after_samples < fqs->entries)
  313. fqs->samples_per_time_slot.enable_after_samples = fqs->entries;
  314. }
  315. static void sampling_file_init(FUNCTION_QUERY_STATUS *fqs, struct journal_file *jf __maybe_unused) {
  316. fqs->samples_per_file.sampled = 0;
  317. fqs->samples_per_file.unsampled = 0;
  318. fqs->samples_per_file.estimated = 0;
  319. fqs->samples_per_file.every = 0;
  320. fqs->samples_per_file.skipped = 0;
  321. fqs->samples_per_file.recalibrate = 0;
  322. }
  323. static size_t sampling_file_lines_scanned_so_far(FUNCTION_QUERY_STATUS *fqs) {
  324. size_t sampled = fqs->samples_per_file.sampled + fqs->samples_per_file.unsampled;
  325. if(!sampled) sampled = 1;
  326. return sampled;
  327. }
  328. static void sampling_running_file_query_overlapping_timeframe_ut(
  329. FUNCTION_QUERY_STATUS *fqs, struct journal_file *jf, FACETS_ANCHOR_DIRECTION direction,
  330. usec_t msg_ut, usec_t *after_ut, usec_t *before_ut) {
  331. // find the overlap of the query and file timeframes
  332. // taking into account the first message we encountered
  333. usec_t oldest_ut, newest_ut;
  334. if(direction == FACETS_ANCHOR_DIRECTION_FORWARD) {
  335. // the first message we know (oldest)
  336. oldest_ut = fqs->query_file.first_msg_ut ? fqs->query_file.first_msg_ut : jf->msg_first_ut;
  337. if(!oldest_ut) oldest_ut = fqs->query_file.start_ut;
  338. if(jf->msg_last_ut)
  339. newest_ut = MIN(fqs->query_file.stop_ut, jf->msg_last_ut);
  340. else if(jf->file_last_modified_ut)
  341. newest_ut = MIN(fqs->query_file.stop_ut, jf->file_last_modified_ut);
  342. else
  343. newest_ut = fqs->query_file.stop_ut;
  344. if(msg_ut < oldest_ut)
  345. oldest_ut = msg_ut - 1;
  346. }
  347. else /* BACKWARD */ {
  348. // the latest message we know (newest)
  349. newest_ut = fqs->query_file.first_msg_ut ? fqs->query_file.first_msg_ut : jf->msg_last_ut;
  350. if(!newest_ut) newest_ut = fqs->query_file.start_ut;
  351. if(jf->msg_first_ut)
  352. oldest_ut = MAX(fqs->query_file.stop_ut, jf->msg_first_ut);
  353. else
  354. oldest_ut = fqs->query_file.stop_ut;
  355. if(newest_ut < msg_ut)
  356. newest_ut = msg_ut + 1;
  357. }
  358. *after_ut = oldest_ut;
  359. *before_ut = newest_ut;
  360. }
  361. static double sampling_running_file_query_progress_by_time(FUNCTION_QUERY_STATUS *fqs, struct journal_file *jf,
  362. FACETS_ANCHOR_DIRECTION direction, usec_t msg_ut) {
  363. usec_t after_ut, before_ut, elapsed_ut;
  364. sampling_running_file_query_overlapping_timeframe_ut(fqs, jf, direction, msg_ut, &after_ut, &before_ut);
  365. if(direction == FACETS_ANCHOR_DIRECTION_FORWARD)
  366. elapsed_ut = msg_ut - after_ut;
  367. else
  368. elapsed_ut = before_ut - msg_ut;
  369. usec_t total_ut = before_ut - after_ut;
  370. double progress = (double)elapsed_ut / (double)total_ut;
  371. return progress;
  372. }
  373. static usec_t sampling_running_file_query_remaining_time(FUNCTION_QUERY_STATUS *fqs, struct journal_file *jf,
  374. FACETS_ANCHOR_DIRECTION direction, usec_t msg_ut,
  375. usec_t *total_time_ut, usec_t *remaining_start_ut,
  376. usec_t *remaining_end_ut) {
  377. usec_t after_ut, before_ut;
  378. sampling_running_file_query_overlapping_timeframe_ut(fqs, jf, direction, msg_ut, &after_ut, &before_ut);
  379. // since we have a timestamp in msg_ut
  380. // this timestamp can extend the overlap
  381. if(msg_ut <= after_ut)
  382. after_ut = msg_ut - 1;
  383. if(msg_ut >= before_ut)
  384. before_ut = msg_ut + 1;
  385. // return the remaining duration
  386. usec_t remaining_from_ut, remaining_to_ut;
  387. if(direction == FACETS_ANCHOR_DIRECTION_FORWARD) {
  388. remaining_from_ut = msg_ut;
  389. remaining_to_ut = before_ut;
  390. }
  391. else {
  392. remaining_from_ut = after_ut;
  393. remaining_to_ut = msg_ut;
  394. }
  395. usec_t remaining_ut = remaining_to_ut - remaining_from_ut;
  396. if(total_time_ut)
  397. *total_time_ut = (before_ut > after_ut) ? before_ut - after_ut : 1;
  398. if(remaining_start_ut)
  399. *remaining_start_ut = remaining_from_ut;
  400. if(remaining_end_ut)
  401. *remaining_end_ut = remaining_to_ut;
  402. return remaining_ut;
  403. }
  404. static size_t sampling_running_file_query_estimate_remaining_lines_by_time(FUNCTION_QUERY_STATUS *fqs,
  405. struct journal_file *jf,
  406. FACETS_ANCHOR_DIRECTION direction,
  407. usec_t msg_ut) {
  408. size_t scanned_lines = sampling_file_lines_scanned_so_far(fqs);
  409. // Calculate the proportion of time covered
  410. usec_t total_time_ut, remaining_start_ut, remaining_end_ut;
  411. usec_t remaining_time_ut = sampling_running_file_query_remaining_time(fqs, jf, direction, msg_ut, &total_time_ut,
  412. &remaining_start_ut, &remaining_end_ut);
  413. if (total_time_ut == 0) total_time_ut = 1;
  414. double proportion_by_time = (double) (total_time_ut - remaining_time_ut) / (double) total_time_ut;
  415. if (proportion_by_time == 0 || proportion_by_time > 1.0 || !isfinite(proportion_by_time))
  416. proportion_by_time = 1.0;
  417. // Estimate the total number of lines in the file
  418. size_t expected_matching_logs_by_time = (size_t)((double)scanned_lines / proportion_by_time);
  419. if(jf->messages_in_file && expected_matching_logs_by_time > jf->messages_in_file)
  420. expected_matching_logs_by_time = jf->messages_in_file;
  421. // Calculate the estimated number of remaining lines
  422. size_t remaining_logs_by_time = expected_matching_logs_by_time - scanned_lines;
  423. if (remaining_logs_by_time < 1) remaining_logs_by_time = 1;
  424. // nd_log(NDLS_COLLECTORS, NDLP_INFO,
  425. // "JOURNAL ESTIMATION: '%s' "
  426. // "scanned_lines=%zu [sampled=%zu, unsampled=%zu, estimated=%zu], "
  427. // "file [%"PRIu64" - %"PRIu64", duration %"PRId64", known lines in file %zu], "
  428. // "query [%"PRIu64" - %"PRIu64", duration %"PRId64"], "
  429. // "first message read from the file at %"PRIu64", current message at %"PRIu64", "
  430. // "proportion of time %.2f %%, "
  431. // "expected total lines in file %zu, "
  432. // "remaining lines %zu, "
  433. // "remaining time %"PRIu64" [%"PRIu64" - %"PRIu64", duration %"PRId64"]"
  434. // , jf->filename
  435. // , scanned_lines, fqs->samples_per_file.sampled, fqs->samples_per_file.unsampled, fqs->samples_per_file.estimated
  436. // , jf->msg_first_ut, jf->msg_last_ut, jf->msg_last_ut - jf->msg_first_ut, jf->messages_in_file
  437. // , fqs->query_file.start_ut, fqs->query_file.stop_ut, fqs->query_file.stop_ut - fqs->query_file.start_ut
  438. // , fqs->query_file.first_msg_ut, msg_ut
  439. // , proportion_by_time * 100.0
  440. // , expected_matching_logs_by_time
  441. // , remaining_logs_by_time
  442. // , remaining_time_ut, remaining_start_ut, remaining_end_ut, remaining_end_ut - remaining_start_ut
  443. // );
  444. return remaining_logs_by_time;
  445. }
  446. static size_t sampling_running_file_query_estimate_remaining_lines(sd_journal *j __maybe_unused, FUNCTION_QUERY_STATUS *fqs, struct journal_file *jf, FACETS_ANCHOR_DIRECTION direction, usec_t msg_ut) {
  447. size_t remaining_logs_by_seqnum = 0;
  448. #ifdef HAVE_SD_JOURNAL_GET_SEQNUM
  449. size_t expected_matching_logs_by_seqnum = 0;
  450. double proportion_by_seqnum = 0.0;
  451. uint64_t current_msg_seqnum;
  452. sd_id128_t current_msg_writer;
  453. if(!fqs->query_file.first_msg_seqnum || sd_journal_get_seqnum(j, &current_msg_seqnum, &current_msg_writer) < 0) {
  454. fqs->query_file.first_msg_seqnum = 0;
  455. fqs->query_file.first_msg_writer = SD_ID128_NULL;
  456. }
  457. else if(jf->messages_in_file) {
  458. size_t scanned_lines = sampling_file_lines_scanned_so_far(fqs);
  459. double proportion_of_all_lines_so_far;
  460. if(direction == FACETS_ANCHOR_DIRECTION_FORWARD)
  461. proportion_of_all_lines_so_far = (double)scanned_lines / (double)(current_msg_seqnum - jf->first_seqnum);
  462. else
  463. proportion_of_all_lines_so_far = (double)scanned_lines / (double)(jf->last_seqnum - current_msg_seqnum);
  464. if(proportion_of_all_lines_so_far > 1.0)
  465. proportion_of_all_lines_so_far = 1.0;
  466. expected_matching_logs_by_seqnum = (size_t)(proportion_of_all_lines_so_far * (double)jf->messages_in_file);
  467. proportion_by_seqnum = (double)scanned_lines / (double)expected_matching_logs_by_seqnum;
  468. if (proportion_by_seqnum == 0 || proportion_by_seqnum > 1.0 || !isfinite(proportion_by_seqnum))
  469. proportion_by_seqnum = 1.0;
  470. remaining_logs_by_seqnum = expected_matching_logs_by_seqnum - scanned_lines;
  471. if(!remaining_logs_by_seqnum) remaining_logs_by_seqnum = 1;
  472. }
  473. #endif
  474. if(remaining_logs_by_seqnum)
  475. return remaining_logs_by_seqnum;
  476. return sampling_running_file_query_estimate_remaining_lines_by_time(fqs, jf, direction, msg_ut);
  477. }
  478. static void sampling_decide_file_sampling_every(sd_journal *j, FUNCTION_QUERY_STATUS *fqs, struct journal_file *jf, FACETS_ANCHOR_DIRECTION direction, usec_t msg_ut) {
  479. size_t files_matched = fqs->files_matched;
  480. if(!files_matched) files_matched = 1;
  481. size_t remaining_lines = sampling_running_file_query_estimate_remaining_lines(j, fqs, jf, direction, msg_ut);
  482. size_t wanted_samples = (fqs->sampling / 2) / files_matched;
  483. if(!wanted_samples) wanted_samples = 1;
  484. fqs->samples_per_file.every = remaining_lines / wanted_samples;
  485. if(fqs->samples_per_file.every < 1)
  486. fqs->samples_per_file.every = 1;
  487. }
  488. typedef enum {
  489. SAMPLING_STOP_AND_ESTIMATE = -1,
  490. SAMPLING_FULL = 0,
  491. SAMPLING_SKIP_FIELDS = 1,
  492. } sampling_t;
  493. static inline sampling_t is_row_in_sample(sd_journal *j, FUNCTION_QUERY_STATUS *fqs, struct journal_file *jf, usec_t msg_ut, FACETS_ANCHOR_DIRECTION direction, bool candidate_to_keep) {
  494. if(!fqs->sampling || candidate_to_keep)
  495. return SAMPLING_FULL;
  496. if(unlikely(msg_ut < fqs->samples_per_time_slot.start_ut))
  497. msg_ut = fqs->samples_per_time_slot.start_ut;
  498. if(unlikely(msg_ut > fqs->samples_per_time_slot.end_ut))
  499. msg_ut = fqs->samples_per_time_slot.end_ut;
  500. size_t slot = (msg_ut - fqs->samples_per_time_slot.start_ut) / fqs->samples_per_time_slot.step_ut;
  501. if(slot >= fqs->samples.slots)
  502. slot = fqs->samples.slots - 1;
  503. bool should_sample = false;
  504. if(fqs->samples.sampled < fqs->samples.enable_after_samples ||
  505. fqs->samples_per_file.sampled < fqs->samples_per_file.enable_after_samples ||
  506. fqs->samples_per_time_slot.sampled[slot] < fqs->samples_per_time_slot.enable_after_samples)
  507. should_sample = true;
  508. else if(fqs->samples_per_file.recalibrate >= SYSTEMD_JOURNAL_SAMPLING_RECALIBRATE || !fqs->samples_per_file.every) {
  509. // this is the first to be unsampled for this file
  510. sampling_decide_file_sampling_every(j, fqs, jf, direction, msg_ut);
  511. fqs->samples_per_file.recalibrate = 0;
  512. should_sample = true;
  513. }
  514. else {
  515. // we sample 1 every fqs->samples_per_file.every
  516. if(fqs->samples_per_file.skipped >= fqs->samples_per_file.every) {
  517. fqs->samples_per_file.skipped = 0;
  518. should_sample = true;
  519. }
  520. else
  521. fqs->samples_per_file.skipped++;
  522. }
  523. if(should_sample) {
  524. fqs->samples.sampled++;
  525. fqs->samples_per_file.sampled++;
  526. fqs->samples_per_time_slot.sampled[slot]++;
  527. return SAMPLING_FULL;
  528. }
  529. fqs->samples_per_file.recalibrate++;
  530. fqs->samples.unsampled++;
  531. fqs->samples_per_file.unsampled++;
  532. fqs->samples_per_time_slot.unsampled[slot]++;
  533. if(fqs->samples_per_file.unsampled > fqs->samples_per_file.sampled) {
  534. double progress_by_time = sampling_running_file_query_progress_by_time(fqs, jf, direction, msg_ut);
  535. if(progress_by_time > SYSTEMD_JOURNAL_ENABLE_ESTIMATIONS_FILE_PERCENTAGE)
  536. return SAMPLING_STOP_AND_ESTIMATE;
  537. }
  538. return SAMPLING_SKIP_FIELDS;
  539. }
  540. static void sampling_update_running_query_file_estimates(FACETS *facets, sd_journal *j, FUNCTION_QUERY_STATUS *fqs, struct journal_file *jf, usec_t msg_ut, FACETS_ANCHOR_DIRECTION direction) {
  541. usec_t total_time_ut, remaining_start_ut, remaining_end_ut;
  542. sampling_running_file_query_remaining_time(fqs, jf, direction, msg_ut, &total_time_ut, &remaining_start_ut,
  543. &remaining_end_ut);
  544. size_t remaining_lines = sampling_running_file_query_estimate_remaining_lines(j, fqs, jf, direction, msg_ut);
  545. facets_update_estimations(facets, remaining_start_ut, remaining_end_ut, remaining_lines);
  546. fqs->samples.estimated += remaining_lines;
  547. fqs->samples_per_file.estimated += remaining_lines;
  548. }
  549. // ----------------------------------------------------------------------------
  550. static inline size_t netdata_systemd_journal_process_row(sd_journal *j, FACETS *facets, struct journal_file *jf, usec_t *msg_ut) {
  551. const void *data;
  552. size_t length, bytes = 0;
  553. facets_add_key_value_length(facets, JOURNAL_KEY_ND_JOURNAL_FILE, sizeof(JOURNAL_KEY_ND_JOURNAL_FILE) - 1, jf->filename, jf->filename_len);
  554. SD_JOURNAL_FOREACH_DATA(j, data, length) {
  555. const char *key, *value;
  556. size_t key_length, value_length;
  557. if(!parse_journal_field(data, length, &key, &key_length, &value, &value_length))
  558. continue;
  559. #ifdef NETDATA_INTERNAL_CHECKS
  560. usec_t origin_journal_ut = *msg_ut;
  561. #endif
  562. if(unlikely(key_length == sizeof(JD_SOURCE_REALTIME_TIMESTAMP) - 1 &&
  563. memcmp(key, JD_SOURCE_REALTIME_TIMESTAMP, sizeof(JD_SOURCE_REALTIME_TIMESTAMP) - 1) == 0)) {
  564. usec_t ut = str2ull(value, NULL);
  565. if(ut && ut < *msg_ut) {
  566. usec_t delta = *msg_ut - ut;
  567. *msg_ut = ut;
  568. if(delta > JOURNAL_VS_REALTIME_DELTA_MAX_UT)
  569. delta = JOURNAL_VS_REALTIME_DELTA_MAX_UT;
  570. // update max_journal_vs_realtime_delta_ut if the delta increased
  571. usec_t expected = jf->max_journal_vs_realtime_delta_ut;
  572. do {
  573. if(delta <= expected)
  574. break;
  575. } while(!__atomic_compare_exchange_n(&jf->max_journal_vs_realtime_delta_ut, &expected, delta, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  576. internal_error(delta > expected,
  577. "increased max_journal_vs_realtime_delta_ut from %"PRIu64" to %"PRIu64", "
  578. "journal %"PRIu64", actual %"PRIu64" (delta %"PRIu64")"
  579. , expected, delta, origin_journal_ut, *msg_ut, origin_journal_ut - (*msg_ut));
  580. }
  581. }
  582. bytes += length;
  583. facets_add_key_value_length(facets, key, key_length, value, value_length <= FACET_MAX_VALUE_LENGTH ? value_length : FACET_MAX_VALUE_LENGTH);
  584. }
  585. return bytes;
  586. }
  587. #define FUNCTION_PROGRESS_UPDATE_ROWS(rows_read, rows) __atomic_fetch_add(&(rows_read), rows, __ATOMIC_RELAXED)
  588. #define FUNCTION_PROGRESS_UPDATE_BYTES(bytes_read, bytes) __atomic_fetch_add(&(bytes_read), bytes, __ATOMIC_RELAXED)
  589. #define FUNCTION_PROGRESS_EVERY_ROWS (1ULL << 13)
  590. #define FUNCTION_DATA_ONLY_CHECK_EVERY_ROWS (1ULL << 7)
  591. static inline ND_SD_JOURNAL_STATUS check_stop(const bool *cancelled, const usec_t *stop_monotonic_ut) {
  592. if(cancelled && __atomic_load_n(cancelled, __ATOMIC_RELAXED)) {
  593. internal_error(true, "Function has been cancelled");
  594. return ND_SD_JOURNAL_CANCELLED;
  595. }
  596. if(now_monotonic_usec() > __atomic_load_n(stop_monotonic_ut, __ATOMIC_RELAXED)) {
  597. internal_error(true, "Function timed out");
  598. return ND_SD_JOURNAL_TIMED_OUT;
  599. }
  600. return ND_SD_JOURNAL_OK;
  601. }
  602. ND_SD_JOURNAL_STATUS netdata_systemd_journal_query_backward(
  603. sd_journal *j, BUFFER *wb __maybe_unused, FACETS *facets,
  604. struct journal_file *jf, FUNCTION_QUERY_STATUS *fqs) {
  605. usec_t anchor_delta = __atomic_load_n(&jf->max_journal_vs_realtime_delta_ut, __ATOMIC_RELAXED);
  606. usec_t start_ut = ((fqs->data_only && fqs->anchor.start_ut) ? fqs->anchor.start_ut : fqs->before_ut) + anchor_delta;
  607. usec_t stop_ut = (fqs->data_only && fqs->anchor.stop_ut) ? fqs->anchor.stop_ut : fqs->after_ut;
  608. bool stop_when_full = (fqs->data_only && !fqs->anchor.stop_ut);
  609. fqs->query_file.start_ut = start_ut;
  610. fqs->query_file.stop_ut = stop_ut;
  611. if(!netdata_systemd_journal_seek_to(j, start_ut))
  612. return ND_SD_JOURNAL_FAILED_TO_SEEK;
  613. size_t errors_no_timestamp = 0;
  614. usec_t latest_msg_ut = 0; // the biggest timestamp we have seen so far
  615. usec_t first_msg_ut = 0; // the first message we got from the db
  616. size_t row_counter = 0, last_row_counter = 0, rows_useful = 0;
  617. size_t bytes = 0, last_bytes = 0;
  618. usec_t last_usec_from = 0;
  619. usec_t last_usec_to = 0;
  620. ND_SD_JOURNAL_STATUS status = ND_SD_JOURNAL_OK;
  621. facets_rows_begin(facets);
  622. while (status == ND_SD_JOURNAL_OK && sd_journal_previous(j) > 0) {
  623. usec_t msg_ut = 0;
  624. if(sd_journal_get_realtime_usec(j, &msg_ut) < 0 || !msg_ut) {
  625. errors_no_timestamp++;
  626. continue;
  627. }
  628. if (unlikely(msg_ut > start_ut))
  629. continue;
  630. if (unlikely(msg_ut < stop_ut))
  631. break;
  632. if(unlikely(msg_ut > latest_msg_ut))
  633. latest_msg_ut = msg_ut;
  634. if(unlikely(!first_msg_ut)) {
  635. first_msg_ut = msg_ut;
  636. fqs->query_file.first_msg_ut = msg_ut;
  637. #ifdef HAVE_SD_JOURNAL_GET_SEQNUM
  638. if(sd_journal_get_seqnum(j, &fqs->query_file.first_msg_seqnum, &fqs->query_file.first_msg_writer) < 0) {
  639. fqs->query_file.first_msg_seqnum = 0;
  640. fqs->query_file.first_msg_writer = SD_ID128_NULL;
  641. }
  642. #endif
  643. }
  644. sampling_t sample = is_row_in_sample(j, fqs, jf, msg_ut,
  645. FACETS_ANCHOR_DIRECTION_BACKWARD,
  646. facets_row_candidate_to_keep(facets, msg_ut));
  647. if(sample == SAMPLING_FULL) {
  648. bytes += netdata_systemd_journal_process_row(j, facets, jf, &msg_ut);
  649. // make sure each line gets a unique timestamp
  650. if(unlikely(msg_ut >= last_usec_from && msg_ut <= last_usec_to))
  651. msg_ut = --last_usec_from;
  652. else
  653. last_usec_from = last_usec_to = msg_ut;
  654. if(facets_row_finished(facets, msg_ut))
  655. rows_useful++;
  656. row_counter++;
  657. if(unlikely((row_counter % FUNCTION_DATA_ONLY_CHECK_EVERY_ROWS) == 0 &&
  658. stop_when_full &&
  659. facets_rows(facets) >= fqs->entries)) {
  660. // stop the data only query
  661. usec_t oldest = facets_row_oldest_ut(facets);
  662. if(oldest && msg_ut < (oldest - anchor_delta))
  663. break;
  664. }
  665. if(unlikely(row_counter % FUNCTION_PROGRESS_EVERY_ROWS == 0)) {
  666. FUNCTION_PROGRESS_UPDATE_ROWS(fqs->rows_read, row_counter - last_row_counter);
  667. last_row_counter = row_counter;
  668. FUNCTION_PROGRESS_UPDATE_BYTES(fqs->bytes_read, bytes - last_bytes);
  669. last_bytes = bytes;
  670. status = check_stop(fqs->cancelled, fqs->stop_monotonic_ut);
  671. }
  672. }
  673. else if(sample == SAMPLING_SKIP_FIELDS)
  674. facets_row_finished_unsampled(facets, msg_ut);
  675. else {
  676. sampling_update_running_query_file_estimates(facets, j, fqs, jf, msg_ut, FACETS_ANCHOR_DIRECTION_BACKWARD);
  677. break;
  678. }
  679. }
  680. FUNCTION_PROGRESS_UPDATE_ROWS(fqs->rows_read, row_counter - last_row_counter);
  681. FUNCTION_PROGRESS_UPDATE_BYTES(fqs->bytes_read, bytes - last_bytes);
  682. fqs->rows_useful += rows_useful;
  683. if(errors_no_timestamp)
  684. netdata_log_error("SYSTEMD-JOURNAL: %zu lines did not have timestamps", errors_no_timestamp);
  685. if(latest_msg_ut > fqs->last_modified)
  686. fqs->last_modified = latest_msg_ut;
  687. return status;
  688. }
  689. ND_SD_JOURNAL_STATUS netdata_systemd_journal_query_forward(
  690. sd_journal *j, BUFFER *wb __maybe_unused, FACETS *facets,
  691. struct journal_file *jf, FUNCTION_QUERY_STATUS *fqs) {
  692. usec_t anchor_delta = __atomic_load_n(&jf->max_journal_vs_realtime_delta_ut, __ATOMIC_RELAXED);
  693. usec_t start_ut = (fqs->data_only && fqs->anchor.start_ut) ? fqs->anchor.start_ut : fqs->after_ut;
  694. usec_t stop_ut = ((fqs->data_only && fqs->anchor.stop_ut) ? fqs->anchor.stop_ut : fqs->before_ut) + anchor_delta;
  695. bool stop_when_full = (fqs->data_only && !fqs->anchor.stop_ut);
  696. fqs->query_file.start_ut = start_ut;
  697. fqs->query_file.stop_ut = stop_ut;
  698. if(!netdata_systemd_journal_seek_to(j, start_ut))
  699. return ND_SD_JOURNAL_FAILED_TO_SEEK;
  700. size_t errors_no_timestamp = 0;
  701. usec_t latest_msg_ut = 0; // the biggest timestamp we have seen so far
  702. usec_t first_msg_ut = 0; // the first message we got from the db
  703. size_t row_counter = 0, last_row_counter = 0, rows_useful = 0;
  704. size_t bytes = 0, last_bytes = 0;
  705. usec_t last_usec_from = 0;
  706. usec_t last_usec_to = 0;
  707. ND_SD_JOURNAL_STATUS status = ND_SD_JOURNAL_OK;
  708. facets_rows_begin(facets);
  709. while (status == ND_SD_JOURNAL_OK && sd_journal_next(j) > 0) {
  710. usec_t msg_ut = 0;
  711. if(sd_journal_get_realtime_usec(j, &msg_ut) < 0 || !msg_ut) {
  712. errors_no_timestamp++;
  713. continue;
  714. }
  715. if (unlikely(msg_ut < start_ut))
  716. continue;
  717. if (unlikely(msg_ut > stop_ut))
  718. break;
  719. if(likely(msg_ut > latest_msg_ut))
  720. latest_msg_ut = msg_ut;
  721. if(unlikely(!first_msg_ut)) {
  722. first_msg_ut = msg_ut;
  723. fqs->query_file.first_msg_ut = msg_ut;
  724. }
  725. sampling_t sample = is_row_in_sample(j, fqs, jf, msg_ut,
  726. FACETS_ANCHOR_DIRECTION_FORWARD,
  727. facets_row_candidate_to_keep(facets, msg_ut));
  728. if(sample == SAMPLING_FULL) {
  729. bytes += netdata_systemd_journal_process_row(j, facets, jf, &msg_ut);
  730. // make sure each line gets a unique timestamp
  731. if(unlikely(msg_ut >= last_usec_from && msg_ut <= last_usec_to))
  732. msg_ut = ++last_usec_to;
  733. else
  734. last_usec_from = last_usec_to = msg_ut;
  735. if(facets_row_finished(facets, msg_ut))
  736. rows_useful++;
  737. row_counter++;
  738. if(unlikely((row_counter % FUNCTION_DATA_ONLY_CHECK_EVERY_ROWS) == 0 &&
  739. stop_when_full &&
  740. facets_rows(facets) >= fqs->entries)) {
  741. // stop the data only query
  742. usec_t newest = facets_row_newest_ut(facets);
  743. if(newest && msg_ut > (newest + anchor_delta))
  744. break;
  745. }
  746. if(unlikely(row_counter % FUNCTION_PROGRESS_EVERY_ROWS == 0)) {
  747. FUNCTION_PROGRESS_UPDATE_ROWS(fqs->rows_read, row_counter - last_row_counter);
  748. last_row_counter = row_counter;
  749. FUNCTION_PROGRESS_UPDATE_BYTES(fqs->bytes_read, bytes - last_bytes);
  750. last_bytes = bytes;
  751. status = check_stop(fqs->cancelled, fqs->stop_monotonic_ut);
  752. }
  753. }
  754. else if(sample == SAMPLING_SKIP_FIELDS)
  755. facets_row_finished_unsampled(facets, msg_ut);
  756. else {
  757. sampling_update_running_query_file_estimates(facets, j, fqs, jf, msg_ut, FACETS_ANCHOR_DIRECTION_FORWARD);
  758. break;
  759. }
  760. }
  761. FUNCTION_PROGRESS_UPDATE_ROWS(fqs->rows_read, row_counter - last_row_counter);
  762. FUNCTION_PROGRESS_UPDATE_BYTES(fqs->bytes_read, bytes - last_bytes);
  763. fqs->rows_useful += rows_useful;
  764. if(errors_no_timestamp)
  765. netdata_log_error("SYSTEMD-JOURNAL: %zu lines did not have timestamps", errors_no_timestamp);
  766. if(latest_msg_ut > fqs->last_modified)
  767. fqs->last_modified = latest_msg_ut;
  768. return status;
  769. }
  770. bool netdata_systemd_journal_check_if_modified_since(sd_journal *j, usec_t seek_to, usec_t last_modified) {
  771. // return true, if data have been modified since the timestamp
  772. if(!last_modified || !seek_to)
  773. return false;
  774. if(!netdata_systemd_journal_seek_to(j, seek_to))
  775. return false;
  776. usec_t first_msg_ut = 0;
  777. while (sd_journal_previous(j) > 0) {
  778. usec_t msg_ut;
  779. if(sd_journal_get_realtime_usec(j, &msg_ut) < 0)
  780. continue;
  781. first_msg_ut = msg_ut;
  782. break;
  783. }
  784. return first_msg_ut != last_modified;
  785. }
  786. #ifdef HAVE_SD_JOURNAL_RESTART_FIELDS
  787. static bool netdata_systemd_filtering_by_journal(sd_journal *j, FACETS *facets, FUNCTION_QUERY_STATUS *fqs) {
  788. const char *field = NULL;
  789. const void *data = NULL;
  790. size_t data_length;
  791. size_t added_keys = 0;
  792. size_t failures = 0;
  793. size_t filters_added = 0;
  794. SD_JOURNAL_FOREACH_FIELD(j, field) { // for each key
  795. bool interesting;
  796. if(fqs->data_only)
  797. interesting = facets_key_name_is_filter(facets, field);
  798. else
  799. interesting = facets_key_name_is_facet(facets, field);
  800. if(interesting) {
  801. if(sd_journal_query_unique(j, field) >= 0) {
  802. bool added_this_key = false;
  803. size_t added_values = 0;
  804. SD_JOURNAL_FOREACH_UNIQUE(j, data, data_length) { // for each value of the key
  805. const char *key, *value;
  806. size_t key_length, value_length;
  807. if(!parse_journal_field(data, data_length, &key, &key_length, &value, &value_length))
  808. continue;
  809. facets_add_possible_value_name_to_key(facets, key, key_length, value, value_length);
  810. if(!facets_key_name_value_length_is_selected(facets, key, key_length, value, value_length))
  811. continue;
  812. if(added_keys && !added_this_key) {
  813. if(sd_journal_add_conjunction(j) < 0) // key AND key AND key
  814. failures++;
  815. added_this_key = true;
  816. added_keys++;
  817. }
  818. else if(added_values)
  819. if(sd_journal_add_disjunction(j) < 0) // value OR value OR value
  820. failures++;
  821. if(sd_journal_add_match(j, data, data_length) < 0)
  822. failures++;
  823. if(!added_keys) {
  824. added_keys++;
  825. added_this_key = true;
  826. }
  827. added_values++;
  828. filters_added++;
  829. }
  830. }
  831. }
  832. }
  833. if(failures) {
  834. log_fqs(fqs, "failed to setup journal filter, will run the full query.");
  835. sd_journal_flush_matches(j);
  836. return true;
  837. }
  838. return filters_added ? true : false;
  839. }
  840. #endif // HAVE_SD_JOURNAL_RESTART_FIELDS
  841. static ND_SD_JOURNAL_STATUS netdata_systemd_journal_query_one_file(
  842. const char *filename, BUFFER *wb, FACETS *facets,
  843. struct journal_file *jf, FUNCTION_QUERY_STATUS *fqs) {
  844. sd_journal *j = NULL;
  845. errno = 0;
  846. fstat_cache_enable_on_thread();
  847. const char *paths[2] = {
  848. [0] = filename,
  849. [1] = NULL,
  850. };
  851. if(sd_journal_open_files(&j, paths, ND_SD_JOURNAL_OPEN_FLAGS) < 0 || !j) {
  852. netdata_log_error("JOURNAL: cannot open file '%s' for query", filename);
  853. fstat_cache_disable_on_thread();
  854. return ND_SD_JOURNAL_FAILED_TO_OPEN;
  855. }
  856. ND_SD_JOURNAL_STATUS status;
  857. bool matches_filters = true;
  858. #ifdef HAVE_SD_JOURNAL_RESTART_FIELDS
  859. if(fqs->slice) {
  860. usec_t started = now_monotonic_usec();
  861. matches_filters = netdata_systemd_filtering_by_journal(j, facets, fqs) || !fqs->filters;
  862. usec_t ended = now_monotonic_usec();
  863. fqs->matches_setup_ut += (ended - started);
  864. }
  865. #endif // HAVE_SD_JOURNAL_RESTART_FIELDS
  866. if(matches_filters) {
  867. if(fqs->direction == FACETS_ANCHOR_DIRECTION_FORWARD)
  868. status = netdata_systemd_journal_query_forward(j, wb, facets, jf, fqs);
  869. else
  870. status = netdata_systemd_journal_query_backward(j, wb, facets, jf, fqs);
  871. }
  872. else
  873. status = ND_SD_JOURNAL_NO_FILE_MATCHED;
  874. sd_journal_close(j);
  875. fstat_cache_disable_on_thread();
  876. return status;
  877. }
  878. static bool jf_is_mine(struct journal_file *jf, FUNCTION_QUERY_STATUS *fqs) {
  879. if((fqs->source_type == SDJF_NONE && !fqs->sources) || (jf->source_type & fqs->source_type) ||
  880. (fqs->sources && simple_pattern_matches(fqs->sources, string2str(jf->source)))) {
  881. if(!jf->msg_last_ut)
  882. // the file is not scanned yet, or the timestamps have not been updated,
  883. // so we don't know if it can contribute or not - let's add it.
  884. return true;
  885. usec_t anchor_delta = JOURNAL_VS_REALTIME_DELTA_MAX_UT;
  886. usec_t first_ut = jf->msg_first_ut - anchor_delta;
  887. usec_t last_ut = jf->msg_last_ut + anchor_delta;
  888. if(last_ut >= fqs->after_ut && first_ut <= fqs->before_ut)
  889. return true;
  890. }
  891. return false;
  892. }
  893. static int netdata_systemd_journal_query(BUFFER *wb, FACETS *facets, FUNCTION_QUERY_STATUS *fqs) {
  894. ND_SD_JOURNAL_STATUS status = ND_SD_JOURNAL_NO_FILE_MATCHED;
  895. struct journal_file *jf;
  896. fqs->files_matched = 0;
  897. fqs->file_working = 0;
  898. fqs->rows_useful = 0;
  899. fqs->rows_read = 0;
  900. fqs->bytes_read = 0;
  901. size_t files_used = 0;
  902. size_t files_max = dictionary_entries(journal_files_registry);
  903. const DICTIONARY_ITEM *file_items[files_max];
  904. // count the files
  905. bool files_are_newer = false;
  906. dfe_start_read(journal_files_registry, jf) {
  907. if(!jf_is_mine(jf, fqs))
  908. continue;
  909. file_items[files_used++] = dictionary_acquired_item_dup(journal_files_registry, jf_dfe.item);
  910. if(jf->msg_last_ut > fqs->if_modified_since)
  911. files_are_newer = true;
  912. }
  913. dfe_done(jf);
  914. fqs->files_matched = files_used;
  915. if(fqs->if_modified_since && !files_are_newer) {
  916. buffer_flush(wb);
  917. return HTTP_RESP_NOT_MODIFIED;
  918. }
  919. // sort the files, so that they are optimal for facets
  920. if(files_used >= 2) {
  921. if (fqs->direction == FACETS_ANCHOR_DIRECTION_BACKWARD)
  922. qsort(file_items, files_used, sizeof(const DICTIONARY_ITEM *),
  923. journal_file_dict_items_backward_compar);
  924. else
  925. qsort(file_items, files_used, sizeof(const DICTIONARY_ITEM *),
  926. journal_file_dict_items_forward_compar);
  927. }
  928. bool partial = false;
  929. usec_t query_started_ut = now_monotonic_usec();
  930. usec_t started_ut = query_started_ut;
  931. usec_t ended_ut = started_ut;
  932. usec_t duration_ut = 0, max_duration_ut = 0;
  933. usec_t progress_duration_ut = 0;
  934. sampling_query_init(fqs, facets);
  935. buffer_json_member_add_array(wb, "_journal_files");
  936. for(size_t f = 0; f < files_used ;f++) {
  937. const char *filename = dictionary_acquired_item_name(file_items[f]);
  938. jf = dictionary_acquired_item_value(file_items[f]);
  939. if(!jf_is_mine(jf, fqs))
  940. continue;
  941. started_ut = ended_ut;
  942. // do not even try to do the query if we expect it to pass the timeout
  943. if(ended_ut + max_duration_ut * 3 >= *fqs->stop_monotonic_ut) {
  944. partial = true;
  945. status = ND_SD_JOURNAL_TIMED_OUT;
  946. break;
  947. }
  948. fqs->file_working++;
  949. // fqs->cached_count = 0;
  950. size_t fs_calls = fstat_thread_calls;
  951. size_t fs_cached = fstat_thread_cached_responses;
  952. size_t rows_useful = fqs->rows_useful;
  953. size_t rows_read = fqs->rows_read;
  954. size_t bytes_read = fqs->bytes_read;
  955. size_t matches_setup_ut = fqs->matches_setup_ut;
  956. sampling_file_init(fqs, jf);
  957. ND_SD_JOURNAL_STATUS tmp_status = netdata_systemd_journal_query_one_file(filename, wb, facets, jf, fqs);
  958. // nd_log(NDLS_COLLECTORS, NDLP_INFO,
  959. // "JOURNAL ESTIMATION FINAL: '%s' "
  960. // "total lines %zu [sampled=%zu, unsampled=%zu, estimated=%zu], "
  961. // "file [%"PRIu64" - %"PRIu64", duration %"PRId64", known lines in file %zu], "
  962. // "query [%"PRIu64" - %"PRIu64", duration %"PRId64"], "
  963. // , jf->filename
  964. // , fqs->samples_per_file.sampled + fqs->samples_per_file.unsampled + fqs->samples_per_file.estimated
  965. // , fqs->samples_per_file.sampled, fqs->samples_per_file.unsampled, fqs->samples_per_file.estimated
  966. // , jf->msg_first_ut, jf->msg_last_ut, jf->msg_last_ut - jf->msg_first_ut, jf->messages_in_file
  967. // , fqs->query_file.start_ut, fqs->query_file.stop_ut, fqs->query_file.stop_ut - fqs->query_file.start_ut
  968. // );
  969. rows_useful = fqs->rows_useful - rows_useful;
  970. rows_read = fqs->rows_read - rows_read;
  971. bytes_read = fqs->bytes_read - bytes_read;
  972. matches_setup_ut = fqs->matches_setup_ut - matches_setup_ut;
  973. fs_calls = fstat_thread_calls - fs_calls;
  974. fs_cached = fstat_thread_cached_responses - fs_cached;
  975. ended_ut = now_monotonic_usec();
  976. duration_ut = ended_ut - started_ut;
  977. if(duration_ut > max_duration_ut)
  978. max_duration_ut = duration_ut;
  979. progress_duration_ut += duration_ut;
  980. if(progress_duration_ut >= SYSTEMD_JOURNAL_PROGRESS_EVERY_UT) {
  981. progress_duration_ut = 0;
  982. netdata_mutex_lock(&stdout_mutex);
  983. pluginsd_function_progress_to_stdout(fqs->transaction, f + 1, files_used);
  984. netdata_mutex_unlock(&stdout_mutex);
  985. }
  986. buffer_json_add_array_item_object(wb); // journal file
  987. {
  988. // information about the file
  989. buffer_json_member_add_string(wb, "_filename", filename);
  990. buffer_json_member_add_uint64(wb, "_source_type", jf->source_type);
  991. buffer_json_member_add_string(wb, "_source", string2str(jf->source));
  992. buffer_json_member_add_uint64(wb, "_last_modified_ut", jf->file_last_modified_ut);
  993. buffer_json_member_add_uint64(wb, "_msg_first_ut", jf->msg_first_ut);
  994. buffer_json_member_add_uint64(wb, "_msg_last_ut", jf->msg_last_ut);
  995. buffer_json_member_add_uint64(wb, "_journal_vs_realtime_delta_ut", jf->max_journal_vs_realtime_delta_ut);
  996. // information about the current use of the file
  997. buffer_json_member_add_uint64(wb, "duration_ut", ended_ut - started_ut);
  998. buffer_json_member_add_uint64(wb, "rows_read", rows_read);
  999. buffer_json_member_add_uint64(wb, "rows_useful", rows_useful);
  1000. buffer_json_member_add_double(wb, "rows_per_second", (double) rows_read / (double) duration_ut * (double) USEC_PER_SEC);
  1001. buffer_json_member_add_uint64(wb, "bytes_read", bytes_read);
  1002. buffer_json_member_add_double(wb, "bytes_per_second", (double) bytes_read / (double) duration_ut * (double) USEC_PER_SEC);
  1003. buffer_json_member_add_uint64(wb, "duration_matches_ut", matches_setup_ut);
  1004. buffer_json_member_add_uint64(wb, "fstat_query_calls", fs_calls);
  1005. buffer_json_member_add_uint64(wb, "fstat_query_cached_responses", fs_cached);
  1006. if(fqs->sampling) {
  1007. buffer_json_member_add_object(wb, "_sampling");
  1008. {
  1009. buffer_json_member_add_uint64(wb, "sampled", fqs->samples_per_file.sampled);
  1010. buffer_json_member_add_uint64(wb, "unsampled", fqs->samples_per_file.unsampled);
  1011. buffer_json_member_add_uint64(wb, "estimated", fqs->samples_per_file.estimated);
  1012. }
  1013. buffer_json_object_close(wb); // _sampling
  1014. }
  1015. }
  1016. buffer_json_object_close(wb); // journal file
  1017. bool stop = false;
  1018. switch(tmp_status) {
  1019. case ND_SD_JOURNAL_OK:
  1020. case ND_SD_JOURNAL_NO_FILE_MATCHED:
  1021. status = (status == ND_SD_JOURNAL_OK) ? ND_SD_JOURNAL_OK : tmp_status;
  1022. break;
  1023. case ND_SD_JOURNAL_FAILED_TO_OPEN:
  1024. case ND_SD_JOURNAL_FAILED_TO_SEEK:
  1025. partial = true;
  1026. if(status == ND_SD_JOURNAL_NO_FILE_MATCHED)
  1027. status = tmp_status;
  1028. break;
  1029. case ND_SD_JOURNAL_CANCELLED:
  1030. case ND_SD_JOURNAL_TIMED_OUT:
  1031. partial = true;
  1032. stop = true;
  1033. status = tmp_status;
  1034. break;
  1035. case ND_SD_JOURNAL_NOT_MODIFIED:
  1036. internal_fatal(true, "this should never be returned here");
  1037. break;
  1038. }
  1039. if(stop)
  1040. break;
  1041. }
  1042. buffer_json_array_close(wb); // _journal_files
  1043. // release the files
  1044. for(size_t f = 0; f < files_used ;f++)
  1045. dictionary_acquired_item_release(journal_files_registry, file_items[f]);
  1046. switch (status) {
  1047. case ND_SD_JOURNAL_OK:
  1048. if(fqs->if_modified_since && !fqs->rows_useful) {
  1049. buffer_flush(wb);
  1050. return HTTP_RESP_NOT_MODIFIED;
  1051. }
  1052. break;
  1053. case ND_SD_JOURNAL_TIMED_OUT:
  1054. case ND_SD_JOURNAL_NO_FILE_MATCHED:
  1055. break;
  1056. case ND_SD_JOURNAL_CANCELLED:
  1057. buffer_flush(wb);
  1058. return HTTP_RESP_CLIENT_CLOSED_REQUEST;
  1059. case ND_SD_JOURNAL_NOT_MODIFIED:
  1060. buffer_flush(wb);
  1061. return HTTP_RESP_NOT_MODIFIED;
  1062. default:
  1063. case ND_SD_JOURNAL_FAILED_TO_OPEN:
  1064. case ND_SD_JOURNAL_FAILED_TO_SEEK:
  1065. buffer_flush(wb);
  1066. return HTTP_RESP_INTERNAL_SERVER_ERROR;
  1067. }
  1068. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  1069. buffer_json_member_add_boolean(wb, "partial", partial);
  1070. buffer_json_member_add_string(wb, "type", "table");
  1071. // build a message for the query
  1072. if(!fqs->data_only) {
  1073. CLEAN_BUFFER *msg = buffer_create(0, NULL);
  1074. CLEAN_BUFFER *msg_description = buffer_create(0, NULL);
  1075. ND_LOG_FIELD_PRIORITY msg_priority = NDLP_INFO;
  1076. if(!journal_files_completed_once()) {
  1077. buffer_strcat(msg, "Journals are still being scanned. ");
  1078. buffer_strcat(msg_description
  1079. , "LIBRARY SCAN: The journal files are still being scanned, you are probably viewing incomplete data. ");
  1080. msg_priority = NDLP_WARNING;
  1081. }
  1082. if(partial) {
  1083. buffer_strcat(msg, "Query timed-out, incomplete data. ");
  1084. buffer_strcat(msg_description
  1085. , "QUERY TIMEOUT: The query timed out and may not include all the data of the selected window. ");
  1086. msg_priority = NDLP_WARNING;
  1087. }
  1088. if(fqs->samples.estimated || fqs->samples.unsampled) {
  1089. double percent = (double) (fqs->samples.sampled * 100.0 /
  1090. (fqs->samples.estimated + fqs->samples.unsampled + fqs->samples.sampled));
  1091. buffer_sprintf(msg, "%.2f%% real data", percent);
  1092. buffer_sprintf(msg_description, "ACTUAL DATA: The filters counters reflect %0.2f%% of the data. ", percent);
  1093. msg_priority = MIN(msg_priority, NDLP_NOTICE);
  1094. }
  1095. if(fqs->samples.unsampled) {
  1096. double percent = (double) (fqs->samples.unsampled * 100.0 /
  1097. (fqs->samples.estimated + fqs->samples.unsampled + fqs->samples.sampled));
  1098. buffer_sprintf(msg, ", %.2f%% unsampled", percent);
  1099. buffer_sprintf(msg_description
  1100. , "UNSAMPLED DATA: %0.2f%% of the events exist and have been counted, but their values have not been evaluated, so they are not included in the filters counters. "
  1101. , percent);
  1102. msg_priority = MIN(msg_priority, NDLP_NOTICE);
  1103. }
  1104. if(fqs->samples.estimated) {
  1105. double percent = (double) (fqs->samples.estimated * 100.0 /
  1106. (fqs->samples.estimated + fqs->samples.unsampled + fqs->samples.sampled));
  1107. buffer_sprintf(msg, ", %.2f%% estimated", percent);
  1108. buffer_sprintf(msg_description
  1109. , "ESTIMATED DATA: The query selected a large amount of data, so to avoid delaying too much, the presented data are estimated by %0.2f%%. "
  1110. , percent);
  1111. msg_priority = MIN(msg_priority, NDLP_NOTICE);
  1112. }
  1113. buffer_json_member_add_object(wb, "message");
  1114. if(buffer_tostring(msg)) {
  1115. buffer_json_member_add_string(wb, "title", buffer_tostring(msg));
  1116. buffer_json_member_add_string(wb, "description", buffer_tostring(msg_description));
  1117. buffer_json_member_add_string(wb, "status", nd_log_id2priority(msg_priority));
  1118. }
  1119. // else send an empty object if there is nothing to tell
  1120. buffer_json_object_close(wb); // message
  1121. }
  1122. if(!fqs->data_only) {
  1123. buffer_json_member_add_time_t(wb, "update_every", 1);
  1124. buffer_json_member_add_string(wb, "help", SYSTEMD_JOURNAL_FUNCTION_DESCRIPTION);
  1125. }
  1126. if(!fqs->data_only || fqs->tail)
  1127. buffer_json_member_add_uint64(wb, "last_modified", fqs->last_modified);
  1128. facets_sort_and_reorder_keys(facets);
  1129. facets_report(facets, wb, used_hashes_registry);
  1130. buffer_json_member_add_time_t(wb, "expires", now_realtime_sec() + (fqs->data_only ? 3600 : 0));
  1131. buffer_json_member_add_object(wb, "_fstat_caching");
  1132. {
  1133. buffer_json_member_add_uint64(wb, "calls", fstat_thread_calls);
  1134. buffer_json_member_add_uint64(wb, "cached", fstat_thread_cached_responses);
  1135. }
  1136. buffer_json_object_close(wb); // _fstat_caching
  1137. if(fqs->sampling) {
  1138. buffer_json_member_add_object(wb, "_sampling");
  1139. {
  1140. buffer_json_member_add_uint64(wb, "sampled", fqs->samples.sampled);
  1141. buffer_json_member_add_uint64(wb, "unsampled", fqs->samples.unsampled);
  1142. buffer_json_member_add_uint64(wb, "estimated", fqs->samples.estimated);
  1143. }
  1144. buffer_json_object_close(wb); // _sampling
  1145. }
  1146. buffer_json_finalize(wb);
  1147. return HTTP_RESP_OK;
  1148. }
  1149. static void netdata_systemd_journal_function_help(const char *transaction) {
  1150. BUFFER *wb = buffer_create(0, NULL);
  1151. buffer_sprintf(wb,
  1152. "%s / %s\n"
  1153. "\n"
  1154. "%s\n"
  1155. "\n"
  1156. "The following parameters are supported:\n"
  1157. "\n"
  1158. " "JOURNAL_PARAMETER_HELP"\n"
  1159. " Shows this help message.\n"
  1160. "\n"
  1161. " "JOURNAL_PARAMETER_INFO"\n"
  1162. " Request initial configuration information about the plugin.\n"
  1163. " The key entity returned is the required_params array, which includes\n"
  1164. " all the available systemd journal sources.\n"
  1165. " When `"JOURNAL_PARAMETER_INFO"` is requested, all other parameters are ignored.\n"
  1166. "\n"
  1167. " "JOURNAL_PARAMETER_DATA_ONLY":true or "JOURNAL_PARAMETER_DATA_ONLY":false\n"
  1168. " Quickly respond with data requested, without generating a\n"
  1169. " `histogram`, `facets` counters and `items`.\n"
  1170. "\n"
  1171. " "JOURNAL_PARAMETER_DELTA":true or "JOURNAL_PARAMETER_DELTA":false\n"
  1172. " When doing data only queries, include deltas for histogram, facets and items.\n"
  1173. "\n"
  1174. " "JOURNAL_PARAMETER_TAIL":true or "JOURNAL_PARAMETER_TAIL":false\n"
  1175. " When doing data only queries, respond with the newest messages,\n"
  1176. " and up to the anchor, but calculate deltas (if requested) for\n"
  1177. " the duration [anchor - before].\n"
  1178. "\n"
  1179. " "JOURNAL_PARAMETER_SLICE":true or "JOURNAL_PARAMETER_SLICE":false\n"
  1180. " When it is turned on, the plugin is executing filtering via libsystemd,\n"
  1181. " utilizing all the available indexes of the journal files.\n"
  1182. " When it is off, only the time constraint is handled by libsystemd and\n"
  1183. " all filtering is done by the plugin.\n"
  1184. " The default is: %s\n"
  1185. "\n"
  1186. " "JOURNAL_PARAMETER_SOURCE":SOURCE\n"
  1187. " Query only the specified journal sources.\n"
  1188. " Do an `"JOURNAL_PARAMETER_INFO"` query to find the sources.\n"
  1189. "\n"
  1190. " "JOURNAL_PARAMETER_BEFORE":TIMESTAMP_IN_SECONDS\n"
  1191. " Absolute or relative (to now) timestamp in seconds, to start the query.\n"
  1192. " The query is always executed from the most recent to the oldest log entry.\n"
  1193. " If not given the default is: now.\n"
  1194. "\n"
  1195. " "JOURNAL_PARAMETER_AFTER":TIMESTAMP_IN_SECONDS\n"
  1196. " Absolute or relative (to `before`) timestamp in seconds, to end the query.\n"
  1197. " If not given, the default is %d.\n"
  1198. "\n"
  1199. " "JOURNAL_PARAMETER_LAST":ITEMS\n"
  1200. " The number of items to return.\n"
  1201. " The default is %d.\n"
  1202. "\n"
  1203. " "JOURNAL_PARAMETER_SAMPLING":ITEMS\n"
  1204. " The number of log entries to sample to estimate facets counters and histogram.\n"
  1205. " The default is %d.\n"
  1206. "\n"
  1207. " "JOURNAL_PARAMETER_ANCHOR":TIMESTAMP_IN_MICROSECONDS\n"
  1208. " Return items relative to this timestamp.\n"
  1209. " The exact items to be returned depend on the query `"JOURNAL_PARAMETER_DIRECTION"`.\n"
  1210. "\n"
  1211. " "JOURNAL_PARAMETER_DIRECTION":forward or "JOURNAL_PARAMETER_DIRECTION":backward\n"
  1212. " When set to `backward` (default) the items returned are the newest before the\n"
  1213. " `"JOURNAL_PARAMETER_ANCHOR"`, (or `"JOURNAL_PARAMETER_BEFORE"` if `"JOURNAL_PARAMETER_ANCHOR"` is not set)\n"
  1214. " When set to `forward` the items returned are the oldest after the\n"
  1215. " `"JOURNAL_PARAMETER_ANCHOR"`, (or `"JOURNAL_PARAMETER_AFTER"` if `"JOURNAL_PARAMETER_ANCHOR"` is not set)\n"
  1216. " The default is: %s\n"
  1217. "\n"
  1218. " "JOURNAL_PARAMETER_QUERY":SIMPLE_PATTERN\n"
  1219. " Do a full text search to find the log entries matching the pattern given.\n"
  1220. " The plugin is searching for matches on all fields of the database.\n"
  1221. "\n"
  1222. " "JOURNAL_PARAMETER_IF_MODIFIED_SINCE":TIMESTAMP_IN_MICROSECONDS\n"
  1223. " Each successful response, includes a `last_modified` field.\n"
  1224. " By providing the timestamp to the `"JOURNAL_PARAMETER_IF_MODIFIED_SINCE"` parameter,\n"
  1225. " the plugin will return 200 with a successful response, or 304 if the source has not\n"
  1226. " been modified since that timestamp.\n"
  1227. "\n"
  1228. " "JOURNAL_PARAMETER_HISTOGRAM":facet_id\n"
  1229. " Use the given `facet_id` for the histogram.\n"
  1230. " This parameter is ignored in `"JOURNAL_PARAMETER_DATA_ONLY"` mode.\n"
  1231. "\n"
  1232. " "JOURNAL_PARAMETER_FACETS":facet_id1,facet_id2,facet_id3,...\n"
  1233. " Add the given facets to the list of fields for which analysis is required.\n"
  1234. " The plugin will offer both a histogram and facet value counters for its values.\n"
  1235. " This parameter is ignored in `"JOURNAL_PARAMETER_DATA_ONLY"` mode.\n"
  1236. "\n"
  1237. " facet_id:value_id1,value_id2,value_id3,...\n"
  1238. " Apply filters to the query, based on the facet IDs returned.\n"
  1239. " Each `facet_id` can be given once, but multiple `facet_ids` can be given.\n"
  1240. "\n"
  1241. , program_name
  1242. , SYSTEMD_JOURNAL_FUNCTION_NAME
  1243. , SYSTEMD_JOURNAL_FUNCTION_DESCRIPTION
  1244. , JOURNAL_DEFAULT_SLICE_MODE ? "true" : "false" // slice
  1245. , -SYSTEMD_JOURNAL_DEFAULT_QUERY_DURATION
  1246. , SYSTEMD_JOURNAL_DEFAULT_ITEMS_PER_QUERY
  1247. , SYSTEMD_JOURNAL_DEFAULT_ITEMS_SAMPLING
  1248. , JOURNAL_DEFAULT_DIRECTION == FACETS_ANCHOR_DIRECTION_BACKWARD ? "backward" : "forward"
  1249. );
  1250. netdata_mutex_lock(&stdout_mutex);
  1251. pluginsd_function_result_to_stdout(transaction, HTTP_RESP_OK, "text/plain", now_realtime_sec() + 3600, wb);
  1252. netdata_mutex_unlock(&stdout_mutex);
  1253. buffer_free(wb);
  1254. }
  1255. void function_systemd_journal(const char *transaction, char *function, usec_t *stop_monotonic_ut, bool *cancelled,
  1256. BUFFER *payload __maybe_unused, const char *source __maybe_unused, void *data __maybe_unused) {
  1257. fstat_thread_calls = 0;
  1258. fstat_thread_cached_responses = 0;
  1259. BUFFER *wb = buffer_create(0, NULL);
  1260. buffer_flush(wb);
  1261. buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_MINIFY);
  1262. FUNCTION_QUERY_STATUS tmp_fqs = {
  1263. .cancelled = cancelled,
  1264. .stop_monotonic_ut = stop_monotonic_ut,
  1265. };
  1266. FUNCTION_QUERY_STATUS *fqs = NULL;
  1267. FACETS *facets = facets_create(50, FACETS_OPTION_ALL_KEYS_FTS,
  1268. SYSTEMD_ALWAYS_VISIBLE_KEYS,
  1269. SYSTEMD_KEYS_INCLUDED_IN_FACETS,
  1270. SYSTEMD_KEYS_EXCLUDED_FROM_FACETS);
  1271. facets_accepted_param(facets, JOURNAL_PARAMETER_INFO);
  1272. facets_accepted_param(facets, JOURNAL_PARAMETER_SOURCE);
  1273. facets_accepted_param(facets, JOURNAL_PARAMETER_AFTER);
  1274. facets_accepted_param(facets, JOURNAL_PARAMETER_BEFORE);
  1275. facets_accepted_param(facets, JOURNAL_PARAMETER_ANCHOR);
  1276. facets_accepted_param(facets, JOURNAL_PARAMETER_DIRECTION);
  1277. facets_accepted_param(facets, JOURNAL_PARAMETER_LAST);
  1278. facets_accepted_param(facets, JOURNAL_PARAMETER_QUERY);
  1279. facets_accepted_param(facets, JOURNAL_PARAMETER_FACETS);
  1280. facets_accepted_param(facets, JOURNAL_PARAMETER_HISTOGRAM);
  1281. facets_accepted_param(facets, JOURNAL_PARAMETER_IF_MODIFIED_SINCE);
  1282. facets_accepted_param(facets, JOURNAL_PARAMETER_DATA_ONLY);
  1283. facets_accepted_param(facets, JOURNAL_PARAMETER_DELTA);
  1284. facets_accepted_param(facets, JOURNAL_PARAMETER_TAIL);
  1285. facets_accepted_param(facets, JOURNAL_PARAMETER_SAMPLING);
  1286. #ifdef HAVE_SD_JOURNAL_RESTART_FIELDS
  1287. facets_accepted_param(facets, JOURNAL_PARAMETER_SLICE);
  1288. #endif // HAVE_SD_JOURNAL_RESTART_FIELDS
  1289. // register the fields in the order you want them on the dashboard
  1290. facets_register_row_severity(facets, syslog_priority_to_facet_severity, NULL);
  1291. facets_register_key_name(facets, "_HOSTNAME",
  1292. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_VISIBLE);
  1293. facets_register_dynamic_key_name(facets, JOURNAL_KEY_ND_JOURNAL_PROCESS,
  1294. FACET_KEY_OPTION_NEVER_FACET | FACET_KEY_OPTION_VISIBLE,
  1295. netdata_systemd_journal_dynamic_row_id, NULL);
  1296. facets_register_key_name(facets, "MESSAGE",
  1297. FACET_KEY_OPTION_NEVER_FACET | FACET_KEY_OPTION_MAIN_TEXT |
  1298. FACET_KEY_OPTION_VISIBLE | FACET_KEY_OPTION_FTS);
  1299. // facets_register_dynamic_key_name(facets, "MESSAGE",
  1300. // FACET_KEY_OPTION_NEVER_FACET | FACET_KEY_OPTION_MAIN_TEXT | FACET_KEY_OPTION_RICH_TEXT |
  1301. // FACET_KEY_OPTION_VISIBLE | FACET_KEY_OPTION_FTS,
  1302. // netdata_systemd_journal_rich_message, NULL);
  1303. facets_register_key_name_transformation(facets, "PRIORITY",
  1304. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW |
  1305. FACET_KEY_OPTION_EXPANDED_FILTER,
  1306. netdata_systemd_journal_transform_priority, NULL);
  1307. facets_register_key_name_transformation(facets, "SYSLOG_FACILITY",
  1308. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW |
  1309. FACET_KEY_OPTION_EXPANDED_FILTER,
  1310. netdata_systemd_journal_transform_syslog_facility, NULL);
  1311. facets_register_key_name_transformation(facets, "ERRNO",
  1312. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW,
  1313. netdata_systemd_journal_transform_errno, NULL);
  1314. facets_register_key_name(facets, JOURNAL_KEY_ND_JOURNAL_FILE,
  1315. FACET_KEY_OPTION_NEVER_FACET);
  1316. facets_register_key_name(facets, "SYSLOG_IDENTIFIER",
  1317. FACET_KEY_OPTION_FACET);
  1318. facets_register_key_name(facets, "UNIT",
  1319. FACET_KEY_OPTION_FACET);
  1320. facets_register_key_name(facets, "USER_UNIT",
  1321. FACET_KEY_OPTION_FACET);
  1322. facets_register_key_name_transformation(facets, "MESSAGE_ID",
  1323. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW |
  1324. FACET_KEY_OPTION_EXPANDED_FILTER,
  1325. netdata_systemd_journal_transform_message_id, NULL);
  1326. facets_register_key_name_transformation(facets, "_BOOT_ID",
  1327. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW,
  1328. netdata_systemd_journal_transform_boot_id, NULL);
  1329. facets_register_key_name_transformation(facets, "_SYSTEMD_OWNER_UID",
  1330. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW,
  1331. netdata_systemd_journal_transform_uid, NULL);
  1332. facets_register_key_name_transformation(facets, "_UID",
  1333. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW,
  1334. netdata_systemd_journal_transform_uid, NULL);
  1335. facets_register_key_name_transformation(facets, "OBJECT_SYSTEMD_OWNER_UID",
  1336. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW,
  1337. netdata_systemd_journal_transform_uid, NULL);
  1338. facets_register_key_name_transformation(facets, "OBJECT_UID",
  1339. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW,
  1340. netdata_systemd_journal_transform_uid, NULL);
  1341. facets_register_key_name_transformation(facets, "_GID",
  1342. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW,
  1343. netdata_systemd_journal_transform_gid, NULL);
  1344. facets_register_key_name_transformation(facets, "OBJECT_GID",
  1345. FACET_KEY_OPTION_FACET | FACET_KEY_OPTION_TRANSFORM_VIEW,
  1346. netdata_systemd_journal_transform_gid, NULL);
  1347. facets_register_key_name_transformation(facets, "_CAP_EFFECTIVE",
  1348. FACET_KEY_OPTION_TRANSFORM_VIEW,
  1349. netdata_systemd_journal_transform_cap_effective, NULL);
  1350. facets_register_key_name_transformation(facets, "_AUDIT_LOGINUID",
  1351. FACET_KEY_OPTION_TRANSFORM_VIEW,
  1352. netdata_systemd_journal_transform_uid, NULL);
  1353. facets_register_key_name_transformation(facets, "OBJECT_AUDIT_LOGINUID",
  1354. FACET_KEY_OPTION_TRANSFORM_VIEW,
  1355. netdata_systemd_journal_transform_uid, NULL);
  1356. facets_register_key_name_transformation(facets, "_SOURCE_REALTIME_TIMESTAMP",
  1357. FACET_KEY_OPTION_TRANSFORM_VIEW,
  1358. netdata_systemd_journal_transform_timestamp_usec, NULL);
  1359. // ------------------------------------------------------------------------
  1360. // parse the parameters
  1361. bool info = false, data_only = false, slice = JOURNAL_DEFAULT_SLICE_MODE, delta = false, tail = false;
  1362. time_t after_s = 0, before_s = 0;
  1363. usec_t anchor = 0;
  1364. usec_t if_modified_since = 0;
  1365. size_t last = 0;
  1366. FACETS_ANCHOR_DIRECTION direction = JOURNAL_DEFAULT_DIRECTION;
  1367. const char *query = NULL;
  1368. const char *chart = NULL;
  1369. SIMPLE_PATTERN *sources = NULL;
  1370. SD_JOURNAL_FILE_SOURCE_TYPE source_type = SDJF_ALL;
  1371. size_t filters = 0;
  1372. size_t sampling = SYSTEMD_JOURNAL_DEFAULT_ITEMS_SAMPLING;
  1373. buffer_json_member_add_object(wb, "_request");
  1374. char *words[SYSTEMD_JOURNAL_MAX_PARAMS] = { NULL };
  1375. size_t num_words = quoted_strings_splitter_pluginsd(function, words, SYSTEMD_JOURNAL_MAX_PARAMS);
  1376. for(int i = 1; i < SYSTEMD_JOURNAL_MAX_PARAMS ;i++) {
  1377. char *keyword = get_word(words, num_words, i);
  1378. if(!keyword) break;
  1379. if(strcmp(keyword, JOURNAL_PARAMETER_HELP) == 0) {
  1380. netdata_systemd_journal_function_help(transaction);
  1381. goto cleanup;
  1382. }
  1383. else if(strcmp(keyword, JOURNAL_PARAMETER_INFO) == 0) {
  1384. info = true;
  1385. }
  1386. else if(strncmp(keyword, JOURNAL_PARAMETER_DELTA ":", sizeof(JOURNAL_PARAMETER_DELTA ":") - 1) == 0) {
  1387. char *v = &keyword[sizeof(JOURNAL_PARAMETER_DELTA ":") - 1];
  1388. if(strcmp(v, "false") == 0 || strcmp(v, "no") == 0 || strcmp(v, "0") == 0)
  1389. delta = false;
  1390. else
  1391. delta = true;
  1392. }
  1393. else if(strncmp(keyword, JOURNAL_PARAMETER_TAIL ":", sizeof(JOURNAL_PARAMETER_TAIL ":") - 1) == 0) {
  1394. char *v = &keyword[sizeof(JOURNAL_PARAMETER_TAIL ":") - 1];
  1395. if(strcmp(v, "false") == 0 || strcmp(v, "no") == 0 || strcmp(v, "0") == 0)
  1396. tail = false;
  1397. else
  1398. tail = true;
  1399. }
  1400. else if(strncmp(keyword, JOURNAL_PARAMETER_SAMPLING ":", sizeof(JOURNAL_PARAMETER_SAMPLING ":") - 1) == 0) {
  1401. sampling = str2ul(&keyword[sizeof(JOURNAL_PARAMETER_SAMPLING ":") - 1]);
  1402. }
  1403. else if(strncmp(keyword, JOURNAL_PARAMETER_DATA_ONLY ":", sizeof(JOURNAL_PARAMETER_DATA_ONLY ":") - 1) == 0) {
  1404. char *v = &keyword[sizeof(JOURNAL_PARAMETER_DATA_ONLY ":") - 1];
  1405. if(strcmp(v, "false") == 0 || strcmp(v, "no") == 0 || strcmp(v, "0") == 0)
  1406. data_only = false;
  1407. else
  1408. data_only = true;
  1409. }
  1410. else if(strncmp(keyword, JOURNAL_PARAMETER_SLICE ":", sizeof(JOURNAL_PARAMETER_SLICE ":") - 1) == 0) {
  1411. char *v = &keyword[sizeof(JOURNAL_PARAMETER_SLICE ":") - 1];
  1412. if(strcmp(v, "false") == 0 || strcmp(v, "no") == 0 || strcmp(v, "0") == 0)
  1413. slice = false;
  1414. else
  1415. slice = true;
  1416. }
  1417. else if(strncmp(keyword, JOURNAL_PARAMETER_SOURCE ":", sizeof(JOURNAL_PARAMETER_SOURCE ":") - 1) == 0) {
  1418. const char *value = &keyword[sizeof(JOURNAL_PARAMETER_SOURCE ":") - 1];
  1419. buffer_json_member_add_array(wb, JOURNAL_PARAMETER_SOURCE);
  1420. BUFFER *sources_list = buffer_create(0, NULL);
  1421. source_type = SDJF_NONE;
  1422. while(value) {
  1423. char *sep = strchr(value, ',');
  1424. if(sep)
  1425. *sep++ = '\0';
  1426. buffer_json_add_array_item_string(wb, value);
  1427. if(strcmp(value, SDJF_SOURCE_ALL_NAME) == 0) {
  1428. source_type |= SDJF_ALL;
  1429. value = NULL;
  1430. }
  1431. else if(strcmp(value, SDJF_SOURCE_LOCAL_NAME) == 0) {
  1432. source_type |= SDJF_LOCAL_ALL;
  1433. value = NULL;
  1434. }
  1435. else if(strcmp(value, SDJF_SOURCE_REMOTES_NAME) == 0) {
  1436. source_type |= SDJF_REMOTE_ALL;
  1437. value = NULL;
  1438. }
  1439. else if(strcmp(value, SDJF_SOURCE_NAMESPACES_NAME) == 0) {
  1440. source_type |= SDJF_LOCAL_NAMESPACE;
  1441. value = NULL;
  1442. }
  1443. else if(strcmp(value, SDJF_SOURCE_LOCAL_SYSTEM_NAME) == 0) {
  1444. source_type |= SDJF_LOCAL_SYSTEM;
  1445. value = NULL;
  1446. }
  1447. else if(strcmp(value, SDJF_SOURCE_LOCAL_USERS_NAME) == 0) {
  1448. source_type |= SDJF_LOCAL_USER;
  1449. value = NULL;
  1450. }
  1451. else if(strcmp(value, SDJF_SOURCE_LOCAL_OTHER_NAME) == 0) {
  1452. source_type |= SDJF_LOCAL_OTHER;
  1453. value = NULL;
  1454. }
  1455. else {
  1456. // else, match the source, whatever it is
  1457. if(buffer_strlen(sources_list))
  1458. buffer_strcat(sources_list, ",");
  1459. buffer_strcat(sources_list, value);
  1460. }
  1461. value = sep;
  1462. }
  1463. if(buffer_strlen(sources_list)) {
  1464. simple_pattern_free(sources);
  1465. sources = simple_pattern_create(buffer_tostring(sources_list), ",", SIMPLE_PATTERN_EXACT, false);
  1466. }
  1467. buffer_free(sources_list);
  1468. buffer_json_array_close(wb); // source
  1469. }
  1470. else if(strncmp(keyword, JOURNAL_PARAMETER_AFTER ":", sizeof(JOURNAL_PARAMETER_AFTER ":") - 1) == 0) {
  1471. after_s = str2l(&keyword[sizeof(JOURNAL_PARAMETER_AFTER ":") - 1]);
  1472. }
  1473. else if(strncmp(keyword, JOURNAL_PARAMETER_BEFORE ":", sizeof(JOURNAL_PARAMETER_BEFORE ":") - 1) == 0) {
  1474. before_s = str2l(&keyword[sizeof(JOURNAL_PARAMETER_BEFORE ":") - 1]);
  1475. }
  1476. else if(strncmp(keyword, JOURNAL_PARAMETER_IF_MODIFIED_SINCE ":", sizeof(JOURNAL_PARAMETER_IF_MODIFIED_SINCE ":") - 1) == 0) {
  1477. if_modified_since = str2ull(&keyword[sizeof(JOURNAL_PARAMETER_IF_MODIFIED_SINCE ":") - 1], NULL);
  1478. }
  1479. else if(strncmp(keyword, JOURNAL_PARAMETER_ANCHOR ":", sizeof(JOURNAL_PARAMETER_ANCHOR ":") - 1) == 0) {
  1480. anchor = str2ull(&keyword[sizeof(JOURNAL_PARAMETER_ANCHOR ":") - 1], NULL);
  1481. }
  1482. else if(strncmp(keyword, JOURNAL_PARAMETER_DIRECTION ":", sizeof(JOURNAL_PARAMETER_DIRECTION ":") - 1) == 0) {
  1483. direction = strcasecmp(&keyword[sizeof(JOURNAL_PARAMETER_DIRECTION ":") - 1], "forward") == 0 ? FACETS_ANCHOR_DIRECTION_FORWARD : FACETS_ANCHOR_DIRECTION_BACKWARD;
  1484. }
  1485. else if(strncmp(keyword, JOURNAL_PARAMETER_LAST ":", sizeof(JOURNAL_PARAMETER_LAST ":") - 1) == 0) {
  1486. last = str2ul(&keyword[sizeof(JOURNAL_PARAMETER_LAST ":") - 1]);
  1487. }
  1488. else if(strncmp(keyword, JOURNAL_PARAMETER_QUERY ":", sizeof(JOURNAL_PARAMETER_QUERY ":") - 1) == 0) {
  1489. query= &keyword[sizeof(JOURNAL_PARAMETER_QUERY ":") - 1];
  1490. }
  1491. else if(strncmp(keyword, JOURNAL_PARAMETER_HISTOGRAM ":", sizeof(JOURNAL_PARAMETER_HISTOGRAM ":") - 1) == 0) {
  1492. chart = &keyword[sizeof(JOURNAL_PARAMETER_HISTOGRAM ":") - 1];
  1493. }
  1494. else if(strncmp(keyword, JOURNAL_PARAMETER_FACETS ":", sizeof(JOURNAL_PARAMETER_FACETS ":") - 1) == 0) {
  1495. char *value = &keyword[sizeof(JOURNAL_PARAMETER_FACETS ":") - 1];
  1496. if(*value) {
  1497. buffer_json_member_add_array(wb, JOURNAL_PARAMETER_FACETS);
  1498. while(value) {
  1499. char *sep = strchr(value, ',');
  1500. if(sep)
  1501. *sep++ = '\0';
  1502. facets_register_facet_id(facets, value, FACET_KEY_OPTION_FACET|FACET_KEY_OPTION_FTS|FACET_KEY_OPTION_REORDER);
  1503. buffer_json_add_array_item_string(wb, value);
  1504. value = sep;
  1505. }
  1506. buffer_json_array_close(wb); // JOURNAL_PARAMETER_FACETS
  1507. }
  1508. }
  1509. else {
  1510. char *value = strchr(keyword, ':');
  1511. if(value) {
  1512. *value++ = '\0';
  1513. buffer_json_member_add_array(wb, keyword);
  1514. while(value) {
  1515. char *sep = strchr(value, ',');
  1516. if(sep)
  1517. *sep++ = '\0';
  1518. facets_register_facet_id_filter(facets, keyword, value, FACET_KEY_OPTION_FACET|FACET_KEY_OPTION_FTS|FACET_KEY_OPTION_REORDER);
  1519. buffer_json_add_array_item_string(wb, value);
  1520. filters++;
  1521. value = sep;
  1522. }
  1523. buffer_json_array_close(wb); // keyword
  1524. }
  1525. }
  1526. }
  1527. // ------------------------------------------------------------------------
  1528. // put this request into the progress db
  1529. fqs = &tmp_fqs;
  1530. // ------------------------------------------------------------------------
  1531. // validate parameters
  1532. time_t now_s = now_realtime_sec();
  1533. time_t expires = now_s + 1;
  1534. if(!after_s && !before_s) {
  1535. before_s = now_s;
  1536. after_s = before_s - SYSTEMD_JOURNAL_DEFAULT_QUERY_DURATION;
  1537. }
  1538. else
  1539. rrdr_relative_window_to_absolute(&after_s, &before_s, now_s);
  1540. if(after_s > before_s) {
  1541. time_t tmp = after_s;
  1542. after_s = before_s;
  1543. before_s = tmp;
  1544. }
  1545. if(after_s == before_s)
  1546. after_s = before_s - SYSTEMD_JOURNAL_DEFAULT_QUERY_DURATION;
  1547. if(!last)
  1548. last = SYSTEMD_JOURNAL_DEFAULT_ITEMS_PER_QUERY;
  1549. // ------------------------------------------------------------------------
  1550. // set query time-frame, anchors and direction
  1551. fqs->transaction = transaction;
  1552. fqs->after_ut = after_s * USEC_PER_SEC;
  1553. fqs->before_ut = (before_s * USEC_PER_SEC) + USEC_PER_SEC - 1;
  1554. fqs->if_modified_since = if_modified_since;
  1555. fqs->data_only = data_only;
  1556. fqs->delta = (fqs->data_only) ? delta : false;
  1557. fqs->tail = (fqs->data_only && fqs->if_modified_since) ? tail : false;
  1558. fqs->sources = sources;
  1559. fqs->source_type = source_type;
  1560. fqs->entries = last;
  1561. fqs->last_modified = 0;
  1562. fqs->filters = filters;
  1563. fqs->query = (query && *query) ? query : NULL;
  1564. fqs->histogram = (chart && *chart) ? chart : NULL;
  1565. fqs->direction = direction;
  1566. fqs->anchor.start_ut = anchor;
  1567. fqs->anchor.stop_ut = 0;
  1568. fqs->sampling = sampling;
  1569. if(fqs->anchor.start_ut && fqs->tail) {
  1570. // a tail request
  1571. // we need the top X entries from BEFORE
  1572. // but, we need to calculate the facets and the
  1573. // histogram up to the anchor
  1574. fqs->direction = direction = FACETS_ANCHOR_DIRECTION_BACKWARD;
  1575. fqs->anchor.start_ut = 0;
  1576. fqs->anchor.stop_ut = anchor;
  1577. }
  1578. if(anchor && anchor < fqs->after_ut) {
  1579. log_fqs(fqs, "received anchor is too small for query timeframe, ignoring anchor");
  1580. anchor = 0;
  1581. fqs->anchor.start_ut = 0;
  1582. fqs->anchor.stop_ut = 0;
  1583. fqs->direction = direction = FACETS_ANCHOR_DIRECTION_BACKWARD;
  1584. }
  1585. else if(anchor > fqs->before_ut) {
  1586. log_fqs(fqs, "received anchor is too big for query timeframe, ignoring anchor");
  1587. anchor = 0;
  1588. fqs->anchor.start_ut = 0;
  1589. fqs->anchor.stop_ut = 0;
  1590. fqs->direction = direction = FACETS_ANCHOR_DIRECTION_BACKWARD;
  1591. }
  1592. facets_set_anchor(facets, fqs->anchor.start_ut, fqs->anchor.stop_ut, fqs->direction);
  1593. facets_set_additional_options(facets,
  1594. ((fqs->data_only) ? FACETS_OPTION_DATA_ONLY : 0) |
  1595. ((fqs->delta) ? FACETS_OPTION_SHOW_DELTAS : 0));
  1596. // ------------------------------------------------------------------------
  1597. // set the rest of the query parameters
  1598. facets_set_items(facets, fqs->entries);
  1599. facets_set_query(facets, fqs->query);
  1600. #ifdef HAVE_SD_JOURNAL_RESTART_FIELDS
  1601. fqs->slice = slice;
  1602. if(slice)
  1603. facets_enable_slice_mode(facets);
  1604. #else
  1605. fqs->slice = false;
  1606. #endif
  1607. if(fqs->histogram)
  1608. facets_set_timeframe_and_histogram_by_id(facets, fqs->histogram, fqs->after_ut, fqs->before_ut);
  1609. else
  1610. facets_set_timeframe_and_histogram_by_name(facets, "PRIORITY", fqs->after_ut, fqs->before_ut);
  1611. // ------------------------------------------------------------------------
  1612. // complete the request object
  1613. buffer_json_member_add_boolean(wb, JOURNAL_PARAMETER_INFO, false);
  1614. buffer_json_member_add_boolean(wb, JOURNAL_PARAMETER_SLICE, fqs->slice);
  1615. buffer_json_member_add_boolean(wb, JOURNAL_PARAMETER_DATA_ONLY, fqs->data_only);
  1616. buffer_json_member_add_boolean(wb, JOURNAL_PARAMETER_DELTA, fqs->delta);
  1617. buffer_json_member_add_boolean(wb, JOURNAL_PARAMETER_TAIL, fqs->tail);
  1618. buffer_json_member_add_uint64(wb, JOURNAL_PARAMETER_SAMPLING, fqs->sampling);
  1619. buffer_json_member_add_uint64(wb, "source_type", fqs->source_type);
  1620. buffer_json_member_add_uint64(wb, JOURNAL_PARAMETER_AFTER, fqs->after_ut / USEC_PER_SEC);
  1621. buffer_json_member_add_uint64(wb, JOURNAL_PARAMETER_BEFORE, fqs->before_ut / USEC_PER_SEC);
  1622. buffer_json_member_add_uint64(wb, "if_modified_since", fqs->if_modified_since);
  1623. buffer_json_member_add_uint64(wb, JOURNAL_PARAMETER_ANCHOR, anchor);
  1624. buffer_json_member_add_string(wb, JOURNAL_PARAMETER_DIRECTION, fqs->direction == FACETS_ANCHOR_DIRECTION_FORWARD ? "forward" : "backward");
  1625. buffer_json_member_add_uint64(wb, JOURNAL_PARAMETER_LAST, fqs->entries);
  1626. buffer_json_member_add_string(wb, JOURNAL_PARAMETER_QUERY, fqs->query);
  1627. buffer_json_member_add_string(wb, JOURNAL_PARAMETER_HISTOGRAM, fqs->histogram);
  1628. buffer_json_object_close(wb); // request
  1629. buffer_json_journal_versions(wb);
  1630. // ------------------------------------------------------------------------
  1631. // run the request
  1632. int response;
  1633. if(info) {
  1634. facets_accepted_parameters_to_json_array(facets, wb, false);
  1635. buffer_json_member_add_array(wb, "required_params");
  1636. {
  1637. buffer_json_add_array_item_object(wb);
  1638. {
  1639. buffer_json_member_add_string(wb, "id", "source");
  1640. buffer_json_member_add_string(wb, "name", "source");
  1641. buffer_json_member_add_string(wb, "help", "Select the SystemD Journal source to query");
  1642. buffer_json_member_add_string(wb, "type", "multiselect");
  1643. buffer_json_member_add_array(wb, "options");
  1644. {
  1645. available_journal_file_sources_to_json_array(wb);
  1646. }
  1647. buffer_json_array_close(wb); // options array
  1648. }
  1649. buffer_json_object_close(wb); // required params object
  1650. }
  1651. buffer_json_array_close(wb); // required_params array
  1652. facets_table_config(wb);
  1653. buffer_json_member_add_uint64(wb, "status", HTTP_RESP_OK);
  1654. buffer_json_member_add_string(wb, "type", "table");
  1655. buffer_json_member_add_string(wb, "help", SYSTEMD_JOURNAL_FUNCTION_DESCRIPTION);
  1656. buffer_json_finalize(wb);
  1657. response = HTTP_RESP_OK;
  1658. goto output;
  1659. }
  1660. response = netdata_systemd_journal_query(wb, facets, fqs);
  1661. // ------------------------------------------------------------------------
  1662. // handle error response
  1663. if(response != HTTP_RESP_OK) {
  1664. netdata_mutex_lock(&stdout_mutex);
  1665. pluginsd_function_json_error_to_stdout(transaction, response, "failed");
  1666. netdata_mutex_unlock(&stdout_mutex);
  1667. goto cleanup;
  1668. }
  1669. output:
  1670. netdata_mutex_lock(&stdout_mutex);
  1671. pluginsd_function_result_to_stdout(transaction, response, "application/json", expires, wb);
  1672. netdata_mutex_unlock(&stdout_mutex);
  1673. cleanup:
  1674. simple_pattern_free(sources);
  1675. facets_destroy(facets);
  1676. buffer_free(wb);
  1677. }