query.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /** @file query.h
  3. * @brief Header of query.c
  4. */
  5. #ifndef QUERY_H_
  6. #define QUERY_H_
  7. #include <inttypes.h>
  8. #include <stdlib.h>
  9. #include "libnetdata/libnetdata.h"
  10. #include "defaults.h"
  11. #define LOGS_QRY_VERSION "1"
  12. #define LOGS_MANAG_FUNC_PARAM_AFTER "after"
  13. #define LOGS_MANAG_FUNC_PARAM_BEFORE "before"
  14. #define LOGS_QRY_KW_QUOTA "quota"
  15. #define LOGS_QRY_KW_CHARTNAME "chartname"
  16. #define LOGS_QRY_KW_FILENAME "filename"
  17. #define LOGS_QRY_KW_KEYWORD "keyword"
  18. #define LOGS_QRY_KW_IGNORE_CASE "ignore_case"
  19. #define LOGS_QRY_KW_SANITIZE_KW "sanitize_keyword"
  20. typedef struct {
  21. const enum {LOGS_QRY_RES_ERR_CODE_OK = 0,
  22. LOGS_QRY_RES_ERR_CODE_INV_TS_ERR,
  23. LOGS_QRY_RES_ERR_CODE_NOT_FOUND_ERR,
  24. LOGS_QRY_RES_ERR_CODE_NOT_INIT_ERR,
  25. LOGS_QRY_RES_ERR_CODE_SERVER_ERR,
  26. LOGS_QRY_RES_ERR_CODE_UNMODIFIED,
  27. LOGS_QRY_RES_ERR_CODE_CANCELLED,
  28. LOGS_QRY_RES_ERR_CODE_TIMEOUT } err_code;
  29. char const *const err_str;
  30. const int http_code;
  31. } logs_qry_res_err_t;
  32. static const logs_qry_res_err_t logs_qry_res_err[] = {
  33. { LOGS_QRY_RES_ERR_CODE_OK, "success", HTTP_RESP_OK },
  34. { LOGS_QRY_RES_ERR_CODE_INV_TS_ERR, "invalid timestamp range", HTTP_RESP_BAD_REQUEST },
  35. { LOGS_QRY_RES_ERR_CODE_NOT_FOUND_ERR, "no results found", HTTP_RESP_OK },
  36. { LOGS_QRY_RES_ERR_CODE_NOT_INIT_ERR, "logs management engine not running", HTTP_RESP_SERVICE_UNAVAILABLE },
  37. { LOGS_QRY_RES_ERR_CODE_SERVER_ERR, "server error", HTTP_RESP_INTERNAL_SERVER_ERROR },
  38. { LOGS_QRY_RES_ERR_CODE_UNMODIFIED, "not modified", HTTP_RESP_NOT_MODIFIED },
  39. { LOGS_QRY_RES_ERR_CODE_CANCELLED, "cancelled", HTTP_RESP_CLIENT_CLOSED_REQUEST },
  40. { LOGS_QRY_RES_ERR_CODE_TIMEOUT, "query timed out", HTTP_RESP_OK }
  41. };
  42. const logs_qry_res_err_t *fetch_log_sources(BUFFER *wb);
  43. /**
  44. * @brief Parameters of the query.
  45. * @param req_from_ts Requested start timestamp of query in epoch
  46. * milliseconds.
  47. *
  48. * @param req_to_ts Requested end timestamp of query in epoch milliseconds.
  49. * If it doesn't match the requested start timestamp, there may be more results
  50. * to be retrieved (for descending timestamp order queries).
  51. *
  52. * @param act_from_ts Actual start timestamp of query in epoch milliseconds.
  53. *
  54. * @param act_to_ts Actual end timestamp of query in epoch milliseconds.
  55. * If it doesn't match the requested end timestamp, there may be more results to
  56. * be retrieved (for ascending timestamp order queries).
  57. *
  58. * @param order_by_asc Equal to 1 if req_from_ts <= req_to_ts, otherwise 0.
  59. *
  60. * @param quota Request quota for results. When exceeded, query will
  61. * return, even if there are more pending results.
  62. *
  63. * @param stop_monotonic_ut Monotonic time in usec after which the query
  64. * will be timed out.
  65. *
  66. * @param chartname Chart name of log source to be queried, as it appears
  67. * on the netdata dashboard. If this is defined and not an empty string, the
  68. * filename parameter is ignored.
  69. *
  70. * @param filename Full path of log source to be queried. Will only be used
  71. * if the chartname is not used.
  72. *
  73. * @param keyword The keyword to be searched. IMPORTANT! Regular expressions
  74. * are supported (if sanitize_keyword is not set) but have not been tested
  75. * extensively, so use with caution!
  76. *
  77. * @param ignore_case If set to any integer other than 0, the query will be
  78. * case-insensitive. If not set or if set to 0, the query will be case-sensitive
  79. *
  80. * @param sanitize_keyword If set to any integer other than 0, the keyword
  81. * will be sanitized before used by the regex engine (which means the keyword
  82. * cannot be a regular expression, as it will be taken as a literal input).
  83. *
  84. * @param results_buff Buffer of BUFFER type to store the results of the
  85. * query in.
  86. *
  87. * @param results_buff->size Defines the maximum quota of results to be
  88. * expected. If exceeded, the query will return the results obtained so far.
  89. *
  90. * @param results_buff->len The exact size of the results matched.
  91. *
  92. * @param results_buff->buffer String containing the results of the query.
  93. *
  94. * @param num_lines Number of log records that match the keyword.
  95. *
  96. * @warning results_buff->size argument must be <= MAX_LOG_MSG_SIZE.
  97. */
  98. typedef struct logs_query_params {
  99. msec_t req_from_ts;
  100. msec_t req_to_ts;
  101. msec_t act_from_ts;
  102. msec_t act_to_ts;
  103. int order_by_asc;
  104. unsigned long quota;
  105. bool *cancelled;
  106. usec_t stop_monotonic_ut;
  107. char *chartname[LOGS_MANAG_MAX_COMPOUND_QUERY_SOURCES];
  108. char *filename[LOGS_MANAG_MAX_COMPOUND_QUERY_SOURCES];
  109. char *keyword;
  110. int ignore_case;
  111. int sanitize_keyword;
  112. BUFFER *results_buff;
  113. unsigned long num_lines;
  114. } logs_query_params_t;
  115. typedef struct logs_query_res_hdr {
  116. msec_t timestamp;
  117. size_t text_size;
  118. int matches;
  119. char log_source[20];
  120. char log_type[20];
  121. char basename[20];
  122. char filename[50];
  123. char chartname[20];
  124. } logs_query_res_hdr_t;
  125. /**
  126. * @brief Check if query should be terminated.
  127. * @param p_query_params See documentation of logs_query_params_t struct.
  128. * @return true if query should be terminated of false otherwise.
  129. */
  130. bool terminate_logs_manag_query(logs_query_params_t *p_query_params);
  131. /**
  132. * @brief Primary query API.
  133. * @param p_query_params See documentation of logs_query_params_t struct.
  134. * @return enum of LOGS_QRY_RES_ERR_CODE with result of query
  135. * @todo Cornercase if filename not found in DB? Return specific message?
  136. */
  137. const logs_qry_res_err_t *execute_logs_manag_query(logs_query_params_t *p_query_params);
  138. #ifdef ENABLE_LOGSMANAGEMENT_TESTS
  139. /* Used as public only for unit testing, normally defined as static */
  140. char *sanitise_string(char *s);
  141. #endif // ENABLE_LOGSMANAGEMENT_TESTS
  142. #endif // QUERY_H_