query_scope.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "internal.h"
  3. ssize_t query_scope_foreach_host(SIMPLE_PATTERN *scope_hosts_sp, SIMPLE_PATTERN *hosts_sp,
  4. foreach_host_cb_t cb, void *data,
  5. struct query_versions *versions,
  6. char *host_node_id_str) {
  7. char uuid[UUID_STR_LEN];
  8. if(!host_node_id_str) host_node_id_str = uuid;
  9. host_node_id_str[0] = '\0';
  10. RRDHOST *host;
  11. ssize_t added = 0;
  12. uint64_t v_hash = 0;
  13. uint64_t h_hash = 0;
  14. uint64_t a_hash = 0;
  15. uint64_t t_hash = 0;
  16. dfe_start_read(rrdhost_root_index, host) {
  17. if(host->node_id)
  18. uuid_unparse_lower(*host->node_id, host_node_id_str);
  19. else
  20. host_node_id_str[0] = '\0';
  21. SIMPLE_PATTERN_RESULT match = SP_MATCHED_POSITIVE;
  22. if(scope_hosts_sp) {
  23. match = simple_pattern_matches_string_extract(scope_hosts_sp, host->hostname, NULL, 0);
  24. if(match == SP_NOT_MATCHED) {
  25. match = simple_pattern_matches_extract(scope_hosts_sp, host->machine_guid, NULL, 0);
  26. if(match == SP_NOT_MATCHED && *host_node_id_str)
  27. match = simple_pattern_matches_extract(scope_hosts_sp, host_node_id_str, NULL, 0);
  28. }
  29. }
  30. if(match != SP_MATCHED_POSITIVE)
  31. continue;
  32. dfe_unlock(host);
  33. if(hosts_sp) {
  34. match = simple_pattern_matches_string_extract(hosts_sp, host->hostname, NULL, 0);
  35. if(match == SP_NOT_MATCHED) {
  36. match = simple_pattern_matches_extract(hosts_sp, host->machine_guid, NULL, 0);
  37. if(match == SP_NOT_MATCHED && *host_node_id_str)
  38. match = simple_pattern_matches_extract(hosts_sp, host_node_id_str, NULL, 0);
  39. }
  40. }
  41. bool queryable_host = (match == SP_MATCHED_POSITIVE);
  42. v_hash += dictionary_version(host->rrdctx.contexts);
  43. h_hash += dictionary_version(host->rrdctx.hub_queue);
  44. a_hash += dictionary_version(host->rrdcalc_root_index);
  45. t_hash += __atomic_load_n(&host->health_transitions, __ATOMIC_RELAXED);
  46. ssize_t ret = cb(data, host, queryable_host);
  47. if(ret < 0) {
  48. added = ret;
  49. break;
  50. }
  51. added += ret;
  52. }
  53. dfe_done(host);
  54. if(versions) {
  55. versions->contexts_hard_hash = v_hash;
  56. versions->contexts_soft_hash = h_hash;
  57. versions->alerts_hard_hash = a_hash;
  58. versions->alerts_soft_hash = t_hash;
  59. }
  60. return added;
  61. }
  62. ssize_t query_scope_foreach_context(RRDHOST *host, const char *scope_contexts, SIMPLE_PATTERN *scope_contexts_sp,
  63. SIMPLE_PATTERN *contexts_sp, foreach_context_cb_t cb, bool queryable_host, void *data) {
  64. if(unlikely(!host->rrdctx.contexts))
  65. return 0;
  66. ssize_t added = 0;
  67. RRDCONTEXT_ACQUIRED *rca = NULL;
  68. if(scope_contexts)
  69. rca = (RRDCONTEXT_ACQUIRED *)dictionary_get_and_acquire_item(host->rrdctx.contexts, scope_contexts);
  70. if(likely(rca)) {
  71. // we found it!
  72. bool queryable_context = queryable_host;
  73. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  74. if(queryable_context && contexts_sp && !simple_pattern_matches_string(contexts_sp, rc->id))
  75. queryable_context = false;
  76. added = cb(data, rca, queryable_context);
  77. rrdcontext_release(rca);
  78. }
  79. else {
  80. // Probably it is a pattern, we need to search for it...
  81. RRDCONTEXT *rc;
  82. dfe_start_read(host->rrdctx.contexts, rc) {
  83. if(scope_contexts_sp && !simple_pattern_matches_string(scope_contexts_sp, rc->id))
  84. continue;
  85. dfe_unlock(rc);
  86. bool queryable_context = queryable_host;
  87. if(queryable_context && contexts_sp && !simple_pattern_matches_string(contexts_sp, rc->id))
  88. queryable_context = false;
  89. ssize_t ret = cb(data, (RRDCONTEXT_ACQUIRED *)rc_dfe.item, queryable_context);
  90. if(ret < 0) {
  91. added = ret;
  92. break;
  93. }
  94. added += ret;
  95. }
  96. dfe_done(rc);
  97. }
  98. return added;
  99. }