json.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "json.h"
  3. /**
  4. * Initialize JSON connector instance
  5. *
  6. * @param instance an instance data structure.
  7. * @return Returns 0 on success, 1 on failure.
  8. */
  9. int init_json_instance(struct instance *instance)
  10. {
  11. instance->worker = simple_connector_worker;
  12. struct simple_connector_config *connector_specific_config = callocz(1, sizeof(struct simple_connector_config));
  13. instance->config.connector_specific_config = (void *)connector_specific_config;
  14. connector_specific_config->default_port = 5448;
  15. instance->start_batch_formatting = NULL;
  16. instance->start_host_formatting = format_host_labels_json_plaintext;
  17. instance->start_chart_formatting = NULL;
  18. if (EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AS_COLLECTED)
  19. instance->metric_formatting = format_dimension_collected_json_plaintext;
  20. else
  21. instance->metric_formatting = format_dimension_stored_json_plaintext;
  22. instance->end_chart_formatting = NULL;
  23. instance->end_host_formatting = flush_host_labels;
  24. instance->end_batch_formatting = simple_connector_update_buffered_bytes;
  25. instance->send_header = NULL;
  26. instance->check_response = exporting_discard_response;
  27. instance->buffer = (void *)buffer_create(0);
  28. if (!instance->buffer) {
  29. error("EXPORTING: cannot create buffer for json exporting connector instance %s", instance->config.name);
  30. return 1;
  31. }
  32. if (uv_mutex_init(&instance->mutex))
  33. return 1;
  34. if (uv_cond_init(&instance->cond_var))
  35. return 1;
  36. return 0;
  37. }
  38. /**
  39. * Format host labels for JSON connector
  40. *
  41. * @param instance an instance data structure.
  42. * @param host a data collecting host.
  43. * @return Always returns 0.
  44. */
  45. int format_host_labels_json_plaintext(struct instance *instance, RRDHOST *host)
  46. {
  47. if (!instance->labels)
  48. instance->labels = buffer_create(1024);
  49. if (unlikely(!sending_labels_configured(instance)))
  50. return 0;
  51. buffer_strcat(instance->labels, "\"labels\":{");
  52. int count = 0;
  53. rrdhost_check_rdlock(host);
  54. netdata_rwlock_rdlock(&host->labels_rwlock);
  55. for (struct label *label = host->labels; label; label = label->next) {
  56. if (!should_send_label(instance, label))
  57. continue;
  58. char value[CONFIG_MAX_VALUE * 2 + 1];
  59. sanitize_json_string(value, label->value, CONFIG_MAX_VALUE);
  60. if (count > 0)
  61. buffer_strcat(instance->labels, ",");
  62. buffer_sprintf(instance->labels, "\"%s\":\"%s\"", label->key, value);
  63. count++;
  64. }
  65. netdata_rwlock_unlock(&host->labels_rwlock);
  66. buffer_strcat(instance->labels, "},");
  67. return 0;
  68. }
  69. /**
  70. * Format dimension using collected data for JSON connector
  71. *
  72. * @param instance an instance data structure.
  73. * @param rd a dimension.
  74. * @return Always returns 0.
  75. */
  76. int format_dimension_collected_json_plaintext(struct instance *instance, RRDDIM *rd)
  77. {
  78. struct engine *engine = instance->engine;
  79. RRDSET *st = rd->rrdset;
  80. RRDHOST *host = st->rrdhost;
  81. const char *tags_pre = "", *tags_post = "", *tags = host->tags;
  82. if (!tags)
  83. tags = "";
  84. if (*tags) {
  85. if (*tags == '{' || *tags == '[' || *tags == '"') {
  86. tags_pre = "\"host_tags\":";
  87. tags_post = ",";
  88. } else {
  89. tags_pre = "\"host_tags\":\"";
  90. tags_post = "\",";
  91. }
  92. }
  93. buffer_sprintf(
  94. instance->buffer,
  95. "{"
  96. "\"prefix\":\"%s\","
  97. "\"hostname\":\"%s\","
  98. "%s%s%s"
  99. "%s"
  100. "\"chart_id\":\"%s\","
  101. "\"chart_name\":\"%s\","
  102. "\"chart_family\":\"%s\","
  103. "\"chart_context\":\"%s\","
  104. "\"chart_type\":\"%s\","
  105. "\"units\":\"%s\","
  106. "\"id\":\"%s\","
  107. "\"name\":\"%s\","
  108. "\"value\":" COLLECTED_NUMBER_FORMAT ","
  109. "\"timestamp\":%llu}\n",
  110. engine->config.prefix,
  111. (host == localhost) ? engine->config.hostname : host->hostname,
  112. tags_pre,
  113. tags,
  114. tags_post,
  115. instance->labels ? buffer_tostring(instance->labels) : "",
  116. st->id,
  117. st->name,
  118. st->family,
  119. st->context,
  120. st->type,
  121. st->units,
  122. rd->id,
  123. rd->name,
  124. rd->last_collected_value,
  125. (unsigned long long)rd->last_collected_time.tv_sec);
  126. return 0;
  127. }
  128. /**
  129. * Format dimension using a calculated value from stored data for JSON connector
  130. *
  131. * @param instance an instance data structure.
  132. * @param rd a dimension.
  133. * @return Always returns 0.
  134. */
  135. int format_dimension_stored_json_plaintext(struct instance *instance, RRDDIM *rd)
  136. {
  137. struct engine *engine = instance->engine;
  138. RRDSET *st = rd->rrdset;
  139. RRDHOST *host = st->rrdhost;
  140. time_t last_t;
  141. calculated_number value = exporting_calculate_value_from_stored_data(instance, rd, &last_t);
  142. if(isnan(value))
  143. return 0;
  144. const char *tags_pre = "", *tags_post = "", *tags = host->tags;
  145. if (!tags)
  146. tags = "";
  147. if (*tags) {
  148. if (*tags == '{' || *tags == '[' || *tags == '"') {
  149. tags_pre = "\"host_tags\":";
  150. tags_post = ",";
  151. } else {
  152. tags_pre = "\"host_tags\":\"";
  153. tags_post = "\",";
  154. }
  155. }
  156. buffer_sprintf(
  157. instance->buffer,
  158. "{"
  159. "\"prefix\":\"%s\","
  160. "\"hostname\":\"%s\","
  161. "%s%s%s"
  162. "%s"
  163. "\"chart_id\":\"%s\","
  164. "\"chart_name\":\"%s\","
  165. "\"chart_family\":\"%s\","
  166. "\"chart_context\": \"%s\","
  167. "\"chart_type\":\"%s\","
  168. "\"units\": \"%s\","
  169. "\"id\":\"%s\","
  170. "\"name\":\"%s\","
  171. "\"value\":" CALCULATED_NUMBER_FORMAT ","
  172. "\"timestamp\": %llu}\n",
  173. engine->config.prefix,
  174. (host == localhost) ? engine->config.hostname : host->hostname,
  175. tags_pre,
  176. tags,
  177. tags_post,
  178. instance->labels ? buffer_tostring(instance->labels) : "",
  179. st->id,
  180. st->name,
  181. st->family,
  182. st->context,
  183. st->type,
  184. st->units,
  185. rd->id,
  186. rd->name,
  187. value,
  188. (unsigned long long)last_t);
  189. return 0;
  190. }