health.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "health.h"
  2. SILENCERS *silencers;
  3. /**
  4. * Create Silencer
  5. *
  6. * Allocate a new silencer to Netdata.
  7. *
  8. * @return It returns the address off the silencer on success and NULL otherwise
  9. */
  10. SILENCER *create_silencer(void) {
  11. SILENCER *t = callocz(1, sizeof(SILENCER));
  12. debug(D_HEALTH, "HEALTH command API: Created empty silencer");
  13. return t;
  14. }
  15. /**
  16. * Health Silencers add
  17. *
  18. * Add more one silencer to the list of silencers.
  19. *
  20. * @param silencer
  21. */
  22. void health_silencers_add(SILENCER *silencer) {
  23. // Add the created instance to the linked list in silencers
  24. silencer->next = silencers->silencers;
  25. silencers->silencers = silencer;
  26. debug(D_HEALTH, "HEALTH command API: Added silencer %s:%s:%s:%s:%s", silencer->alarms,
  27. silencer->charts, silencer->contexts, silencer->hosts, silencer->families
  28. );
  29. }
  30. /**
  31. * Silencers Add Parameter
  32. *
  33. * Create a new silencer and adjust the variables
  34. *
  35. * @param silencer a pointer to the silencer that will be adjusted
  36. * @param key the key value sent by client
  37. * @param value the value sent to the key
  38. *
  39. * @return It returns the silencer configured on success and NULL otherwise
  40. */
  41. SILENCER *health_silencers_addparam(SILENCER *silencer, char *key, char *value) {
  42. static uint32_t
  43. hash_alarm = 0,
  44. hash_template = 0,
  45. hash_chart = 0,
  46. hash_context = 0,
  47. hash_host = 0,
  48. hash_families = 0;
  49. if (unlikely(!hash_alarm)) {
  50. hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
  51. hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
  52. hash_chart = simple_uhash(HEALTH_CHART_KEY);
  53. hash_context = simple_uhash(HEALTH_CONTEXT_KEY);
  54. hash_host = simple_uhash(HEALTH_HOST_KEY);
  55. hash_families = simple_uhash(HEALTH_FAMILIES_KEY);
  56. }
  57. uint32_t hash = simple_uhash(key);
  58. if (unlikely(silencer == NULL)) {
  59. if (
  60. (hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) ||
  61. (hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) ||
  62. (hash == hash_chart && !strcasecmp(key, HEALTH_CHART_KEY)) ||
  63. (hash == hash_context && !strcasecmp(key, HEALTH_CONTEXT_KEY)) ||
  64. (hash == hash_host && !strcasecmp(key, HEALTH_HOST_KEY)) ||
  65. (hash == hash_families && !strcasecmp(key, HEALTH_FAMILIES_KEY))
  66. ) {
  67. silencer = create_silencer();
  68. if(!silencer) {
  69. error("Cannot add a new silencer to Netdata");
  70. return NULL;
  71. }
  72. }
  73. }
  74. if (hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
  75. silencer->alarms = strdupz(value);
  76. silencer->alarms_pattern = simple_pattern_create(silencer->alarms, NULL, SIMPLE_PATTERN_EXACT);
  77. } else if (hash == hash_chart && !strcasecmp(key, HEALTH_CHART_KEY)) {
  78. silencer->charts = strdupz(value);
  79. silencer->charts_pattern = simple_pattern_create(silencer->charts, NULL, SIMPLE_PATTERN_EXACT);
  80. } else if (hash == hash_context && !strcasecmp(key, HEALTH_CONTEXT_KEY)) {
  81. silencer->contexts = strdupz(value);
  82. silencer->contexts_pattern = simple_pattern_create(silencer->contexts, NULL, SIMPLE_PATTERN_EXACT);
  83. } else if (hash == hash_host && !strcasecmp(key, HEALTH_HOST_KEY)) {
  84. silencer->hosts = strdupz(value);
  85. silencer->hosts_pattern = simple_pattern_create(silencer->hosts, NULL, SIMPLE_PATTERN_EXACT);
  86. } else if (hash == hash_families && !strcasecmp(key, HEALTH_FAMILIES_KEY)) {
  87. silencer->families = strdupz(value);
  88. silencer->families_pattern = simple_pattern_create(silencer->families, NULL, SIMPLE_PATTERN_EXACT);
  89. }
  90. return silencer;
  91. }
  92. /**
  93. * JSON Read Callback
  94. *
  95. * Callback called by netdata to create the silencer.
  96. *
  97. * @param e the main json structure
  98. *
  99. * @return It always return 0.
  100. */
  101. int health_silencers_json_read_callback(JSON_ENTRY *e)
  102. {
  103. switch(e->type) {
  104. case JSON_OBJECT:
  105. #ifndef ENABLE_JSONC
  106. e->callback_function = health_silencers_json_read_callback;
  107. if(strcmp(e->name,"")) {
  108. // init silencer
  109. debug(D_HEALTH, "JSON: Got object with a name, initializing new silencer for %s",e->name);
  110. #endif
  111. e->callback_data = create_silencer();
  112. if(e->callback_data) {
  113. health_silencers_add(e->callback_data);
  114. }
  115. #ifndef ENABLE_JSONC
  116. }
  117. #endif
  118. break;
  119. case JSON_ARRAY:
  120. e->callback_function = health_silencers_json_read_callback;
  121. break;
  122. case JSON_STRING:
  123. if(!strcmp(e->name,"type")) {
  124. debug(D_HEALTH, "JSON: Processing type=%s",e->data.string);
  125. if (!strcmp(e->data.string,"SILENCE")) silencers->stype = STYPE_SILENCE_NOTIFICATIONS;
  126. else if (!strcmp(e->data.string,"DISABLE")) silencers->stype = STYPE_DISABLE_ALARMS;
  127. } else {
  128. debug(D_HEALTH, "JSON: Adding %s=%s", e->name, e->data.string);
  129. if (e->callback_data)
  130. (void)health_silencers_addparam(e->callback_data, e->name, e->data.string);
  131. }
  132. break;
  133. case JSON_BOOLEAN:
  134. debug(D_HEALTH, "JSON: Processing all_alarms");
  135. silencers->all_alarms=e->data.boolean?1:0;
  136. break;
  137. case JSON_NUMBER:
  138. case JSON_NULL:
  139. break;
  140. }
  141. return 0;
  142. }
  143. /**
  144. * Initialize Global Silencers
  145. *
  146. * Initialize the silencer for the whole netdata system.
  147. *
  148. * @return It returns 0 on success and -1 otherwise
  149. */
  150. int health_initialize_global_silencers() {
  151. silencers = mallocz(sizeof(SILENCERS));
  152. silencers->all_alarms=0;
  153. silencers->stype=STYPE_NONE;
  154. silencers->silencers=NULL;
  155. return 0;
  156. }