metalogpluginsd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "metadatalog.h"
  4. #include "metalogpluginsd.h"
  5. extern struct config stream_config;
  6. PARSER_RC metalog_pluginsd_host_action(
  7. void *user, char *machine_guid, char *hostname, char *registry_hostname, int update_every, char *os, char *timezone,
  8. char *tags)
  9. {
  10. struct metalog_pluginsd_state *state = ((PARSER_USER_OBJECT *)user)->private;
  11. RRDHOST *host = rrdhost_find_by_guid(machine_guid, 0);
  12. if (host) {
  13. if (unlikely(host->rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)) {
  14. error("Archived host '%s' has memory mode '%s', but the archived one is '%s'. Ignoring archived state.",
  15. host->hostname, rrd_memory_mode_name(host->rrd_memory_mode),
  16. rrd_memory_mode_name(RRD_MEMORY_MODE_DBENGINE));
  17. ((PARSER_USER_OBJECT *) user)->host = NULL; /* Ignore objects if memory mode is not dbengine */
  18. }
  19. ((PARSER_USER_OBJECT *) user)->host = host;
  20. return PARSER_RC_OK;
  21. }
  22. if (strcmp(machine_guid, registry_get_this_machine_guid()) == 0) {
  23. ((PARSER_USER_OBJECT *) user)->host = host;
  24. return PARSER_RC_OK;
  25. }
  26. if (likely(!uuid_parse(machine_guid, state->host_uuid))) {
  27. int rc = sql_store_host(&state->host_uuid, hostname, registry_hostname, update_every, os, timezone, tags);
  28. if (unlikely(rc)) {
  29. errno = 0;
  30. error("Failed to store host %s with UUID %s in the database", hostname, machine_guid);
  31. }
  32. }
  33. else {
  34. errno = 0;
  35. error("Host machine GUID %s is not valid", machine_guid);
  36. }
  37. return PARSER_RC_OK;
  38. }
  39. PARSER_RC metalog_pluginsd_chart_action(void *user, char *type, char *id, char *name, char *family, char *context,
  40. char *title, char *units, char *plugin, char *module, int priority,
  41. int update_every, RRDSET_TYPE chart_type, char *options)
  42. {
  43. UNUSED(options);
  44. struct metalog_pluginsd_state *state = ((PARSER_USER_OBJECT *)user)->private;
  45. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  46. if (unlikely(uuid_is_null(state->host_uuid))) {
  47. debug(D_METADATALOG, "Ignoring chart belonging to missing or ignored host.");
  48. return PARSER_RC_OK;
  49. }
  50. uuid_copy(state->chart_uuid, state->uuid);
  51. uuid_clear(state->uuid); /* Consume UUID */
  52. (void) sql_store_chart(&state->chart_uuid, &state->host_uuid,
  53. type, id, name, family, context, title, units,
  54. plugin, module, priority, update_every,
  55. chart_type, RRD_MEMORY_MODE_DBENGINE, host ? host->rrd_history_entries : 1);
  56. ((PARSER_USER_OBJECT *)user)->st_exists = 1;
  57. return PARSER_RC_OK;
  58. }
  59. PARSER_RC metalog_pluginsd_dimension_action(void *user, RRDSET *st, char *id, char *name, char *algorithm,
  60. long multiplier, long divisor, char *options, RRD_ALGORITHM algorithm_type)
  61. {
  62. struct metalog_pluginsd_state *state = ((PARSER_USER_OBJECT *)user)->private;
  63. UNUSED(user);
  64. UNUSED(options);
  65. UNUSED(algorithm);
  66. UNUSED(st);
  67. if (unlikely(uuid_is_null(state->chart_uuid))) {
  68. debug(D_METADATALOG, "Ignoring dimension belonging to missing or ignored chart.");
  69. info("Ignoring dimension belonging to missing or ignored chart.");
  70. return PARSER_RC_OK;
  71. }
  72. if (unlikely(uuid_is_null(state->uuid))) {
  73. debug(D_METADATALOG, "Ignoring dimension without unknown UUID");
  74. info("Ignoring dimension without unknown UUID");
  75. return PARSER_RC_OK;
  76. }
  77. (void) sql_store_dimension(&state->uuid, &state->chart_uuid, id, name, multiplier, divisor, algorithm_type);
  78. uuid_clear(state->uuid); /* Consume UUID */
  79. return PARSER_RC_OK;
  80. }
  81. PARSER_RC metalog_pluginsd_guid_action(void *user, uuid_t *uuid)
  82. {
  83. struct metalog_pluginsd_state *state = ((PARSER_USER_OBJECT *)user)->private;
  84. uuid_copy(state->uuid, *uuid);
  85. return PARSER_RC_OK;
  86. }
  87. PARSER_RC metalog_pluginsd_context_action(void *user, uuid_t *uuid)
  88. {
  89. struct metalog_pluginsd_state *state = ((PARSER_USER_OBJECT *)user)->private;
  90. int rc = find_uuid_type(uuid);
  91. if (rc == 1) {
  92. uuid_copy(state->host_uuid, *uuid);
  93. ((PARSER_USER_OBJECT *)user)->st_exists = 0;
  94. ((PARSER_USER_OBJECT *)user)->host_exists = 1;
  95. } else if (rc == 2) {
  96. uuid_copy(state->chart_uuid, *uuid);
  97. ((PARSER_USER_OBJECT *)user)->st_exists = 1;
  98. } else
  99. uuid_copy(state->uuid, *uuid);
  100. return PARSER_RC_OK;
  101. }
  102. PARSER_RC metalog_pluginsd_tombstone_action(void *user, uuid_t *uuid)
  103. {
  104. UNUSED(user);
  105. UNUSED(uuid);
  106. return PARSER_RC_OK;
  107. }
  108. void metalog_pluginsd_state_init(struct metalog_pluginsd_state *state, struct metalog_instance *ctx)
  109. {
  110. state->ctx = ctx;
  111. state->skip_record = 0;
  112. uuid_clear(state->uuid);
  113. state->metalogfile = NULL;
  114. }