rrd_api.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /** @file rrd_api.h
  2. */
  3. #ifndef RRD_API_H_
  4. #define RRD_API_H_
  5. #include "daemon/common.h"
  6. #include "../circular_buffer.h"
  7. #include "../helper.h"
  8. struct Chart_meta;
  9. struct Chart_str {
  10. const char *type;
  11. const char *id;
  12. const char *title;
  13. const char *units;
  14. const char *family;
  15. const char *context;
  16. const char *chart_type;
  17. long priority;
  18. int update_every;
  19. };
  20. #include "rrd_api_generic.h"
  21. #include "rrd_api_web_log.h"
  22. #include "rrd_api_kernel.h"
  23. #include "rrd_api_systemd.h"
  24. #include "rrd_api_docker_ev.h"
  25. #include "rrd_api_mqtt.h"
  26. #define CHART_TITLE_TOTAL_COLLECTED_LOGS "Total collected log records"
  27. #define CHART_TITLE_RATE_COLLECTED_LOGS "Rate of collected log records"
  28. #define NETDATA_CHART_PRIO_LOGS_INCR 100 /**< PRIO increment step from one log source to another **/
  29. typedef struct Chart_data_cus {
  30. char *id;
  31. struct chart_data_cus_dim {
  32. char *name;
  33. collected_number val;
  34. unsigned long long *p_counter;
  35. } *dims;
  36. int dims_size;
  37. struct Chart_data_cus *next;
  38. } Chart_data_cus_t ;
  39. struct Chart_meta {
  40. enum log_src_type_t type;
  41. long base_prio;
  42. union {
  43. chart_data_generic_t *chart_data_generic;
  44. chart_data_web_log_t *chart_data_web_log;
  45. chart_data_kernel_t *chart_data_kernel;
  46. chart_data_systemd_t *chart_data_systemd;
  47. chart_data_docker_ev_t *chart_data_docker_ev;
  48. chart_data_mqtt_t *chart_data_mqtt;
  49. };
  50. Chart_data_cus_t *chart_data_cus_arr;
  51. void (*init)(struct File_info *p_file_info);
  52. void (*update)(struct File_info *p_file_info);
  53. };
  54. static inline struct Chart_str lgs_mng_create_chart(const char *type,
  55. const char *id,
  56. const char *title,
  57. const char *units,
  58. const char *family,
  59. const char *context,
  60. const char *chart_type,
  61. long priority,
  62. int update_every){
  63. struct Chart_str cs = {
  64. .type = type,
  65. .id = id,
  66. .title = title,
  67. .units = units,
  68. .family = family ? family : "",
  69. .context = context ? context : "",
  70. .chart_type = chart_type ? chart_type : "",
  71. .priority = priority,
  72. .update_every = update_every
  73. };
  74. printf("CHART '%s.%s' '' '%s' '%s' '%s' '%s' '%s' %ld %d '' '" LOGS_MANAGEMENT_PLUGIN_STR "' ''\n",
  75. cs.type,
  76. cs.id,
  77. cs.title,
  78. cs.units,
  79. cs.family,
  80. cs.context,
  81. cs.chart_type,
  82. cs.priority,
  83. cs.update_every
  84. );
  85. return cs;
  86. }
  87. static inline void lgs_mng_add_dim( const char *id,
  88. const char *algorithm,
  89. collected_number multiplier,
  90. collected_number divisor){
  91. printf("DIMENSION '%s' '' '%s' %lld %lld\n", id, algorithm, multiplier, divisor);
  92. }
  93. static inline void lgs_mng_add_dim_post_init( struct Chart_str *cs,
  94. const char *dim_id,
  95. const char *algorithm,
  96. collected_number multiplier,
  97. collected_number divisor){
  98. printf("CHART '%s.%s' '' '%s' '%s' '%s' '%s' '%s' %ld %d '' '" LOGS_MANAGEMENT_PLUGIN_STR "' ''\n",
  99. cs->type,
  100. cs->id,
  101. cs->title,
  102. cs->units,
  103. cs->family,
  104. cs->context,
  105. cs->chart_type,
  106. cs->priority,
  107. cs->update_every
  108. );
  109. lgs_mng_add_dim(dim_id, algorithm, multiplier, divisor);
  110. }
  111. static inline void lgs_mng_update_chart_begin(const char *type, const char *id){
  112. printf("BEGIN '%s.%s'\n", type, id);
  113. }
  114. static inline void lgs_mng_update_chart_set(const char *id, collected_number val){
  115. printf("SET '%s' = %lld\n", id, val);
  116. }
  117. static inline void lgs_mng_update_chart_end(time_t sec){
  118. printf("END %" PRId64 " 0 1\n", sec);
  119. }
  120. #define lgs_mng_do_num_of_logs_charts_init(p_file_info, chart_prio){ \
  121. \
  122. /* Number of collected logs total - initialise */ \
  123. if(p_file_info->parser_config->chart_config & CHART_COLLECTED_LOGS_TOTAL){ \
  124. lgs_mng_create_chart( \
  125. (char *) p_file_info->chartname /* type */ \
  126. , "collected_logs_total" /* id */ \
  127. , CHART_TITLE_TOTAL_COLLECTED_LOGS /* title */ \
  128. , "log records" /* units */ \
  129. , "collected_logs" /* family */ \
  130. , NULL /* context */ \
  131. , RRDSET_TYPE_AREA_NAME /* chart_type */ \
  132. , ++chart_prio /* priority */ \
  133. , p_file_info->update_every /* update_every */ \
  134. ); \
  135. lgs_mng_add_dim("total records", RRD_ALGORITHM_ABSOLUTE_NAME, 1, 1); \
  136. } \
  137. \
  138. /* Number of collected logs rate - initialise */ \
  139. if(p_file_info->parser_config->chart_config & CHART_COLLECTED_LOGS_RATE){ \
  140. lgs_mng_create_chart( \
  141. (char *) p_file_info->chartname /* type */ \
  142. , "collected_logs_rate" /* id */ \
  143. , CHART_TITLE_RATE_COLLECTED_LOGS /* title */ \
  144. , "log records" /* units */ \
  145. , "collected_logs" /* family */ \
  146. , NULL /* context */ \
  147. , RRDSET_TYPE_LINE_NAME /* chart_type */ \
  148. , ++chart_prio /* priority */ \
  149. , p_file_info->update_every /* update_every */ \
  150. ); \
  151. lgs_mng_add_dim("records", RRD_ALGORITHM_INCREMENTAL_NAME, 1, 1); \
  152. } \
  153. \
  154. } \
  155. #define lgs_mng_do_num_of_logs_charts_update(p_file_info, lag_in_sec, chart_data){ \
  156. \
  157. /* Number of collected logs total - update previous values */ \
  158. if(p_file_info->parser_config->chart_config & CHART_COLLECTED_LOGS_TOTAL){ \
  159. for(time_t sec = p_file_info->parser_metrics->last_update - lag_in_sec; \
  160. sec < p_file_info->parser_metrics->last_update; \
  161. sec++){ \
  162. lgs_mng_update_chart_begin(p_file_info->chartname, "collected_logs_total"); \
  163. lgs_mng_update_chart_set("total records", chart_data->num_lines); \
  164. lgs_mng_update_chart_end(sec); \
  165. } \
  166. } \
  167. \
  168. /* Number of collected logs rate - update previous values */ \
  169. if(p_file_info->parser_config->chart_config & CHART_COLLECTED_LOGS_RATE){ \
  170. for(time_t sec = p_file_info->parser_metrics->last_update - lag_in_sec; \
  171. sec < p_file_info->parser_metrics->last_update; \
  172. sec++){ \
  173. lgs_mng_update_chart_begin(p_file_info->chartname, "collected_logs_rate"); \
  174. lgs_mng_update_chart_set("records", chart_data->num_lines); \
  175. lgs_mng_update_chart_end(sec); \
  176. } \
  177. } \
  178. \
  179. chart_data->num_lines = p_file_info->parser_metrics->num_lines; \
  180. \
  181. /* Number of collected logs total - update */ \
  182. if(p_file_info->parser_config->chart_config & CHART_COLLECTED_LOGS_TOTAL){ \
  183. lgs_mng_update_chart_begin( (char *) p_file_info->chartname, "collected_logs_total"); \
  184. lgs_mng_update_chart_set("total records", chart_data->num_lines); \
  185. lgs_mng_update_chart_end(p_file_info->parser_metrics->last_update); \
  186. } \
  187. \
  188. /* Number of collected logs rate - update */ \
  189. if(p_file_info->parser_config->chart_config & CHART_COLLECTED_LOGS_RATE){ \
  190. lgs_mng_update_chart_begin( (char *) p_file_info->chartname, "collected_logs_rate"); \
  191. lgs_mng_update_chart_set("records", chart_data->num_lines); \
  192. lgs_mng_update_chart_end(p_file_info->parser_metrics->last_update); \
  193. } \
  194. }
  195. #define lgs_mng_do_custom_charts_init(p_file_info) { \
  196. \
  197. for(int cus_off = 0; p_file_info->parser_cus_config[cus_off]; cus_off++){ \
  198. \
  199. Chart_data_cus_t *cus; \
  200. Chart_data_cus_t **p_cus = &p_file_info->chart_meta->chart_data_cus_arr; \
  201. \
  202. for(cus = p_file_info->chart_meta->chart_data_cus_arr; \
  203. cus; \
  204. cus = cus->next){ \
  205. \
  206. if(!strcmp(cus->id, p_file_info->parser_cus_config[cus_off]->chartname)) \
  207. break; \
  208. \
  209. p_cus = &(cus->next); \
  210. } \
  211. \
  212. if(!cus){ \
  213. cus = callocz(1, sizeof(Chart_data_cus_t)); \
  214. *p_cus = cus; \
  215. \
  216. cus->id = p_file_info->parser_cus_config[cus_off]->chartname; \
  217. \
  218. lgs_mng_create_chart( \
  219. (char *) p_file_info->chartname /* type */ \
  220. , cus->id /* id */ \
  221. , cus->id /* title */ \
  222. , "matches" /* units */ \
  223. , "custom_charts" /* family */ \
  224. , NULL /* context */ \
  225. , RRDSET_TYPE_AREA_NAME /* chart_type */ \
  226. , p_file_info->chart_meta->base_prio + 1000 + cus_off /* priority */ \
  227. , p_file_info->update_every /* update_every */ \
  228. ); \
  229. } \
  230. \
  231. cus->dims = reallocz(cus->dims, ++cus->dims_size * sizeof(struct chart_data_cus_dim)); \
  232. cus->dims[cus->dims_size - 1].name = \
  233. p_file_info->parser_cus_config[cus_off]->regex_name; \
  234. cus->dims[cus->dims_size - 1].val = 0; \
  235. cus->dims[cus->dims_size - 1].p_counter = \
  236. &p_file_info->parser_metrics->parser_cus[cus_off]->count; \
  237. \
  238. lgs_mng_add_dim(cus->dims[cus->dims_size - 1].name, \
  239. RRD_ALGORITHM_INCREMENTAL_NAME, 1, 1); \
  240. \
  241. } \
  242. }
  243. #define lgs_mng_do_custom_charts_update(p_file_info, lag_in_sec) { \
  244. \
  245. for(time_t sec = p_file_info->parser_metrics->last_update - lag_in_sec; \
  246. sec < p_file_info->parser_metrics->last_update; \
  247. sec++){ \
  248. \
  249. for(Chart_data_cus_t *cus = p_file_info->chart_meta->chart_data_cus_arr; \
  250. cus; \
  251. cus = cus->next){ \
  252. \
  253. lgs_mng_update_chart_begin(p_file_info->chartname, cus->id); \
  254. \
  255. for(int d_idx = 0; d_idx < cus->dims_size; d_idx++) \
  256. lgs_mng_update_chart_set(cus->dims[d_idx].name, cus->dims[d_idx].val); \
  257. \
  258. lgs_mng_update_chart_end(sec); \
  259. } \
  260. \
  261. } \
  262. \
  263. for(Chart_data_cus_t *cus = p_file_info->chart_meta->chart_data_cus_arr; \
  264. cus; \
  265. cus = cus->next){ \
  266. \
  267. lgs_mng_update_chart_begin(p_file_info->chartname, cus->id); \
  268. \
  269. for(int d_idx = 0; d_idx < cus->dims_size; d_idx++){ \
  270. \
  271. cus->dims[d_idx].val += *(cus->dims[d_idx].p_counter); \
  272. *(cus->dims[d_idx].p_counter) = 0; \
  273. \
  274. lgs_mng_update_chart_set(cus->dims[d_idx].name, cus->dims[d_idx].val); \
  275. } \
  276. \
  277. lgs_mng_update_chart_end(p_file_info->parser_metrics->last_update); \
  278. } \
  279. }
  280. #endif // RRD_API_H_