rrdpush.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrdpush.h"
  3. /*
  4. * rrdpush
  5. *
  6. * 3 threads are involved for all stream operations
  7. *
  8. * 1. a random data collection thread, calling rrdset_done_push()
  9. * this is called for each chart.
  10. *
  11. * the output of this work is kept in a thread BUFFER
  12. * the sender thread is signalled via a pipe (in RRDHOST)
  13. *
  14. * 2. a sender thread running at the sending netdata
  15. * this is spawned automatically on the first chart to be pushed
  16. *
  17. * It tries to push the metrics to the remote netdata, as fast
  18. * as possible (i.e. immediately after they are collected).
  19. *
  20. * 3. a receiver thread, running at the receiving netdata
  21. * this is spawned automatically when the sender connects to
  22. * the receiver.
  23. *
  24. */
  25. struct config stream_config = {
  26. .first_section = NULL,
  27. .last_section = NULL,
  28. .mutex = NETDATA_MUTEX_INITIALIZER,
  29. .index = {
  30. .avl_tree = {
  31. .root = NULL,
  32. .compar = appconfig_section_compare
  33. },
  34. .rwlock = AVL_LOCK_INITIALIZER
  35. }
  36. };
  37. unsigned int default_rrdpush_enabled = 0;
  38. STREAM_CAPABILITIES globally_disabled_capabilities = STREAM_CAP_NONE;
  39. unsigned int default_rrdpush_compression_enabled = 1;
  40. char *default_rrdpush_destination = NULL;
  41. char *default_rrdpush_api_key = NULL;
  42. char *default_rrdpush_send_charts_matching = NULL;
  43. bool default_rrdpush_enable_replication = true;
  44. time_t default_rrdpush_seconds_to_replicate = 86400;
  45. time_t default_rrdpush_replication_step = 600;
  46. #ifdef ENABLE_HTTPS
  47. char *netdata_ssl_ca_path = NULL;
  48. char *netdata_ssl_ca_file = NULL;
  49. #endif
  50. static void load_stream_conf() {
  51. errno = 0;
  52. char *filename = strdupz_path_subpath(netdata_configured_user_config_dir, "stream.conf");
  53. if(!appconfig_load(&stream_config, filename, 0, NULL)) {
  54. nd_log_daemon(NDLP_NOTICE, "CONFIG: cannot load user config '%s'. Will try stock config.", filename);
  55. freez(filename);
  56. filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "stream.conf");
  57. if(!appconfig_load(&stream_config, filename, 0, NULL))
  58. nd_log_daemon(NDLP_NOTICE, "CONFIG: cannot load stock config '%s'. Running with internal defaults.", filename);
  59. }
  60. freez(filename);
  61. }
  62. bool rrdpush_receiver_needs_dbengine() {
  63. struct section *co;
  64. for(co = stream_config.first_section; co; co = co->next) {
  65. if(strcmp(co->name, "stream") == 0)
  66. continue; // the first section is not relevant
  67. char *s;
  68. s = appconfig_get_by_section(co, "enabled", NULL);
  69. if(!s || !appconfig_test_boolean_value(s))
  70. continue;
  71. s = appconfig_get_by_section(co, "default memory mode", NULL);
  72. if(s && strcmp(s, "dbengine") == 0)
  73. return true;
  74. s = appconfig_get_by_section(co, "memory mode", NULL);
  75. if(s && strcmp(s, "dbengine") == 0)
  76. return true;
  77. }
  78. return false;
  79. }
  80. int rrdpush_init() {
  81. // --------------------------------------------------------------------
  82. // load stream.conf
  83. load_stream_conf();
  84. default_rrdpush_enabled = (unsigned int)appconfig_get_boolean(&stream_config, CONFIG_SECTION_STREAM, "enabled", default_rrdpush_enabled);
  85. default_rrdpush_destination = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "destination", "");
  86. default_rrdpush_api_key = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "api key", "");
  87. default_rrdpush_send_charts_matching = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "send charts matching", "*");
  88. default_rrdpush_enable_replication = config_get_boolean(CONFIG_SECTION_DB, "enable replication", default_rrdpush_enable_replication);
  89. default_rrdpush_seconds_to_replicate = config_get_number(CONFIG_SECTION_DB, "seconds to replicate", default_rrdpush_seconds_to_replicate);
  90. default_rrdpush_replication_step = config_get_number(CONFIG_SECTION_DB, "seconds per replication step", default_rrdpush_replication_step);
  91. rrdhost_free_orphan_time_s = config_get_number(CONFIG_SECTION_DB, "cleanup orphan hosts after secs", rrdhost_free_orphan_time_s);
  92. default_rrdpush_compression_enabled = (unsigned int)appconfig_get_boolean(&stream_config, CONFIG_SECTION_STREAM,
  93. "enable compression", default_rrdpush_compression_enabled);
  94. rrdpush_compression_levels[COMPRESSION_ALGORITHM_BROTLI] = (int)appconfig_get_number(
  95. &stream_config, CONFIG_SECTION_STREAM, "brotli compression level",
  96. rrdpush_compression_levels[COMPRESSION_ALGORITHM_BROTLI]);
  97. rrdpush_compression_levels[COMPRESSION_ALGORITHM_ZSTD] = (int)appconfig_get_number(
  98. &stream_config, CONFIG_SECTION_STREAM, "zstd compression level",
  99. rrdpush_compression_levels[COMPRESSION_ALGORITHM_ZSTD]);
  100. rrdpush_compression_levels[COMPRESSION_ALGORITHM_LZ4] = (int)appconfig_get_number(
  101. &stream_config, CONFIG_SECTION_STREAM, "lz4 compression acceleration",
  102. rrdpush_compression_levels[COMPRESSION_ALGORITHM_LZ4]);
  103. rrdpush_compression_levels[COMPRESSION_ALGORITHM_GZIP] = (int)appconfig_get_number(
  104. &stream_config, CONFIG_SECTION_STREAM, "gzip compression level",
  105. rrdpush_compression_levels[COMPRESSION_ALGORITHM_GZIP]);
  106. if(default_rrdpush_enabled && (!default_rrdpush_destination || !*default_rrdpush_destination || !default_rrdpush_api_key || !*default_rrdpush_api_key)) {
  107. nd_log_daemon(NDLP_WARNING, "STREAM [send]: cannot enable sending thread - information is missing.");
  108. default_rrdpush_enabled = 0;
  109. }
  110. #ifdef ENABLE_HTTPS
  111. netdata_ssl_validate_certificate_sender = !appconfig_get_boolean(&stream_config, CONFIG_SECTION_STREAM, "ssl skip certificate verification", !netdata_ssl_validate_certificate);
  112. if(!netdata_ssl_validate_certificate_sender)
  113. nd_log_daemon(NDLP_NOTICE, "SSL: streaming senders will skip SSL certificates verification.");
  114. netdata_ssl_ca_path = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "CApath", NULL);
  115. netdata_ssl_ca_file = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "CAfile", NULL);
  116. #endif
  117. return default_rrdpush_enabled;
  118. }
  119. // data collection happens from multiple threads
  120. // each of these threads calls rrdset_done()
  121. // which in turn calls rrdset_done_push()
  122. // which uses this pipe to notify the streaming thread
  123. // that there are more data ready to be sent
  124. #define PIPE_READ 0
  125. #define PIPE_WRITE 1
  126. // to have the remote netdata re-sync the charts
  127. // to its current clock, we send for this many
  128. // iterations a BEGIN line without microseconds
  129. // this is for the first iterations of each chart
  130. unsigned int remote_clock_resync_iterations = 60;
  131. static inline bool should_send_chart_matching(RRDSET *st, RRDSET_FLAGS flags) {
  132. if(!(flags & RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED))
  133. return false;
  134. if(unlikely(!(flags & (RRDSET_FLAG_UPSTREAM_SEND | RRDSET_FLAG_UPSTREAM_IGNORE)))) {
  135. RRDHOST *host = st->rrdhost;
  136. if (flags & RRDSET_FLAG_ANOMALY_DETECTION) {
  137. if(ml_streaming_enabled())
  138. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_SEND);
  139. else
  140. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  141. }
  142. else if(simple_pattern_matches_string(host->rrdpush_send_charts_matching, st->id) ||
  143. simple_pattern_matches_string(host->rrdpush_send_charts_matching, st->name))
  144. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_SEND);
  145. else
  146. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  147. // get the flags again, to know how to respond
  148. flags = rrdset_flag_check(st, RRDSET_FLAG_UPSTREAM_SEND|RRDSET_FLAG_UPSTREAM_IGNORE);
  149. }
  150. return flags & RRDSET_FLAG_UPSTREAM_SEND;
  151. }
  152. int configured_as_parent() {
  153. struct section *section = NULL;
  154. int is_parent = 0;
  155. appconfig_wrlock(&stream_config);
  156. for (section = stream_config.first_section; section; section = section->next) {
  157. uuid_t uuid;
  158. if (uuid_parse(section->name, uuid) != -1 &&
  159. appconfig_get_boolean_by_section(section, "enabled", 0)) {
  160. is_parent = 1;
  161. break;
  162. }
  163. }
  164. appconfig_unlock(&stream_config);
  165. return is_parent;
  166. }
  167. // chart labels
  168. static int send_clabels_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
  169. BUFFER *wb = (BUFFER *)data;
  170. buffer_sprintf(wb, "CLABEL \"%s\" \"%s\" %d\n", name, value, ls & ~(RRDLABEL_FLAG_INTERNAL));
  171. return 1;
  172. }
  173. static void rrdpush_send_clabels(BUFFER *wb, RRDSET *st) {
  174. if (st->rrdlabels) {
  175. if(rrdlabels_walkthrough_read(st->rrdlabels, send_clabels_callback, wb) > 0)
  176. buffer_sprintf(wb, "CLABEL_COMMIT\n");
  177. }
  178. }
  179. // Send the current chart definition.
  180. // Assumes that collector thread has already called sender_start for mutex / buffer state.
  181. static inline bool rrdpush_send_chart_definition(BUFFER *wb, RRDSET *st) {
  182. uint32_t version = rrdset_metadata_version(st);
  183. RRDHOST *host = st->rrdhost;
  184. NUMBER_ENCODING integer_encoding = stream_has_capability(host->sender, STREAM_CAP_IEEE754) ? NUMBER_ENCODING_BASE64 : NUMBER_ENCODING_HEX;
  185. bool with_slots = stream_has_capability(host->sender, STREAM_CAP_SLOTS) ? true : false;
  186. bool replication_progress = false;
  187. // properly set the name for the remote end to parse it
  188. char *name = "";
  189. if(likely(st->name)) {
  190. if(unlikely(st->id != st->name)) {
  191. // they differ
  192. name = strchr(rrdset_name(st), '.');
  193. if(name)
  194. name++;
  195. else
  196. name = "";
  197. }
  198. }
  199. buffer_fast_strcat(wb, PLUGINSD_KEYWORD_CHART, sizeof(PLUGINSD_KEYWORD_CHART) - 1);
  200. if(with_slots) {
  201. buffer_fast_strcat(wb, " "PLUGINSD_KEYWORD_SLOT":", sizeof(PLUGINSD_KEYWORD_SLOT) - 1 + 2);
  202. buffer_print_uint64_encoded(wb, integer_encoding, st->rrdpush.sender.chart_slot);
  203. }
  204. // send the chart
  205. buffer_sprintf(
  206. wb
  207. , " \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" %d %d \"%s %s %s %s\" \"%s\" \"%s\"\n"
  208. , rrdset_id(st)
  209. , name
  210. , rrdset_title(st)
  211. , rrdset_units(st)
  212. , rrdset_family(st)
  213. , rrdset_context(st)
  214. , rrdset_type_name(st->chart_type)
  215. , st->priority
  216. , st->update_every
  217. , rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)?"obsolete":""
  218. , rrdset_flag_check(st, RRDSET_FLAG_DETAIL)?"detail":""
  219. , rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)?"store_first":""
  220. , rrdset_flag_check(st, RRDSET_FLAG_HIDDEN)?"hidden":""
  221. , rrdset_plugin_name(st)
  222. , rrdset_module_name(st)
  223. );
  224. // send the chart labels
  225. if (stream_has_capability(host->sender, STREAM_CAP_CLABELS))
  226. rrdpush_send_clabels(wb, st);
  227. // send the dimensions
  228. RRDDIM *rd;
  229. rrddim_foreach_read(rd, st) {
  230. buffer_fast_strcat(wb, PLUGINSD_KEYWORD_DIMENSION, sizeof(PLUGINSD_KEYWORD_DIMENSION) - 1);
  231. if(with_slots) {
  232. buffer_fast_strcat(wb, " "PLUGINSD_KEYWORD_SLOT":", sizeof(PLUGINSD_KEYWORD_SLOT) - 1 + 2);
  233. buffer_print_uint64_encoded(wb, integer_encoding, rd->rrdpush.sender.dim_slot);
  234. }
  235. buffer_sprintf(
  236. wb
  237. , " \"%s\" \"%s\" \"%s\" %d %d \"%s %s %s\"\n"
  238. , rrddim_id(rd)
  239. , rrddim_name(rd)
  240. , rrd_algorithm_name(rd->algorithm)
  241. , rd->multiplier
  242. , rd->divisor
  243. , rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)?"obsolete":""
  244. , rrddim_option_check(rd, RRDDIM_OPTION_HIDDEN)?"hidden":""
  245. , rrddim_option_check(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS)?"noreset":""
  246. );
  247. }
  248. rrddim_foreach_done(rd);
  249. // send the chart functions
  250. if(stream_has_capability(host->sender, STREAM_CAP_FUNCTIONS))
  251. rrd_functions_expose_rrdpush(st, wb);
  252. // send the chart local custom variables
  253. rrdsetvar_print_to_streaming_custom_chart_variables(st, wb);
  254. if (stream_has_capability(host->sender, STREAM_CAP_REPLICATION)) {
  255. time_t db_first_time_t, db_last_time_t;
  256. time_t now = now_realtime_sec();
  257. rrdset_get_retention_of_tier_for_collected_chart(st, &db_first_time_t, &db_last_time_t, now, 0);
  258. buffer_sprintf(wb, PLUGINSD_KEYWORD_CHART_DEFINITION_END " %llu %llu %llu\n",
  259. (unsigned long long)db_first_time_t,
  260. (unsigned long long)db_last_time_t,
  261. (unsigned long long)now);
  262. if(!rrdset_flag_check(st, RRDSET_FLAG_SENDER_REPLICATION_IN_PROGRESS)) {
  263. rrdset_flag_set(st, RRDSET_FLAG_SENDER_REPLICATION_IN_PROGRESS);
  264. rrdset_flag_clear(st, RRDSET_FLAG_SENDER_REPLICATION_FINISHED);
  265. rrdhost_sender_replicating_charts_plus_one(st->rrdhost);
  266. }
  267. replication_progress = true;
  268. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  269. internal_error(true, "REPLAY: 'host:%s/chart:%s' replication starts",
  270. rrdhost_hostname(st->rrdhost), rrdset_id(st));
  271. #endif
  272. }
  273. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_METADATA);
  274. // we can set the exposed flag, after we commit the buffer
  275. // because replication may pick it up prematurely
  276. rrddim_foreach_read(rd, st) {
  277. rrddim_metadata_exposed_upstream(rd, version);
  278. }
  279. rrddim_foreach_done(rd);
  280. rrdset_metadata_exposed_upstream(st, version);
  281. st->rrdpush.sender.resync_time_s = st->last_collected_time.tv_sec + (remote_clock_resync_iterations * st->update_every);
  282. return replication_progress;
  283. }
  284. // sends the current chart dimensions
  285. static void rrdpush_send_chart_metrics(BUFFER *wb, RRDSET *st, struct sender_state *s __maybe_unused, RRDSET_FLAGS flags) {
  286. buffer_fast_strcat(wb, "BEGIN \"", 7);
  287. buffer_fast_strcat(wb, rrdset_id(st), string_strlen(st->id));
  288. buffer_fast_strcat(wb, "\" ", 2);
  289. if(st->last_collected_time.tv_sec > st->rrdpush.sender.resync_time_s)
  290. buffer_print_uint64(wb, st->usec_since_last_update);
  291. else
  292. buffer_fast_strcat(wb, "0", 1);
  293. buffer_fast_strcat(wb, "\n", 1);
  294. RRDDIM *rd;
  295. rrddim_foreach_read(rd, st) {
  296. if(unlikely(!rrddim_check_updated(rd)))
  297. continue;
  298. if(likely(rrddim_check_upstream_exposed_collector(rd))) {
  299. buffer_fast_strcat(wb, "SET \"", 5);
  300. buffer_fast_strcat(wb, rrddim_id(rd), string_strlen(rd->id));
  301. buffer_fast_strcat(wb, "\" = ", 4);
  302. buffer_print_int64(wb, rd->collector.collected_value);
  303. buffer_fast_strcat(wb, "\n", 1);
  304. }
  305. else {
  306. internal_error(true, "STREAM: 'host:%s/chart:%s/dim:%s' flag 'exposed' is updated but not exposed",
  307. rrdhost_hostname(st->rrdhost), rrdset_id(st), rrddim_id(rd));
  308. // we will include it in the next iteration
  309. rrddim_metadata_updated(rd);
  310. }
  311. }
  312. rrddim_foreach_done(rd);
  313. if(unlikely(flags & RRDSET_FLAG_UPSTREAM_SEND_VARIABLES))
  314. rrdsetvar_print_to_streaming_custom_chart_variables(st, wb);
  315. buffer_fast_strcat(wb, "END\n", 4);
  316. }
  317. static void rrdpush_sender_thread_spawn(RRDHOST *host);
  318. // Called from the internal collectors to mark a chart obsolete.
  319. bool rrdset_push_chart_definition_now(RRDSET *st) {
  320. RRDHOST *host = st->rrdhost;
  321. if(unlikely(!rrdhost_can_send_definitions_to_parent(host)
  322. || !should_send_chart_matching(st, rrdset_flag_get(st)))) {
  323. return false;
  324. }
  325. BUFFER *wb = sender_start(host->sender);
  326. rrdpush_send_chart_definition(wb, st);
  327. sender_thread_buffer_free();
  328. return true;
  329. }
  330. void rrdset_push_metrics_v1(RRDSET_STREAM_BUFFER *rsb, RRDSET *st) {
  331. RRDHOST *host = st->rrdhost;
  332. rrdpush_send_chart_metrics(rsb->wb, st, host->sender, rsb->rrdset_flags);
  333. }
  334. void rrddim_push_metrics_v2(RRDSET_STREAM_BUFFER *rsb, RRDDIM *rd, usec_t point_end_time_ut, NETDATA_DOUBLE n, SN_FLAGS flags) {
  335. if(!rsb->wb || !rsb->v2 || !netdata_double_isnumber(n) || !does_storage_number_exist(flags))
  336. return;
  337. bool with_slots = stream_has_capability(rsb, STREAM_CAP_SLOTS) ? true : false;
  338. NUMBER_ENCODING integer_encoding = stream_has_capability(rsb, STREAM_CAP_IEEE754) ? NUMBER_ENCODING_BASE64 : NUMBER_ENCODING_HEX;
  339. NUMBER_ENCODING doubles_encoding = stream_has_capability(rsb, STREAM_CAP_IEEE754) ? NUMBER_ENCODING_BASE64 : NUMBER_ENCODING_DECIMAL;
  340. BUFFER *wb = rsb->wb;
  341. time_t point_end_time_s = (time_t)(point_end_time_ut / USEC_PER_SEC);
  342. if(unlikely(rsb->last_point_end_time_s != point_end_time_s)) {
  343. if(unlikely(rsb->begin_v2_added))
  344. buffer_fast_strcat(wb, PLUGINSD_KEYWORD_END_V2 "\n", sizeof(PLUGINSD_KEYWORD_END_V2) - 1 + 1);
  345. buffer_fast_strcat(wb, PLUGINSD_KEYWORD_BEGIN_V2, sizeof(PLUGINSD_KEYWORD_BEGIN_V2) - 1);
  346. if(with_slots) {
  347. buffer_fast_strcat(wb, " "PLUGINSD_KEYWORD_SLOT":", sizeof(PLUGINSD_KEYWORD_SLOT) - 1 + 2);
  348. buffer_print_uint64_encoded(wb, integer_encoding, rd->rrdset->rrdpush.sender.chart_slot);
  349. }
  350. buffer_fast_strcat(wb, " '", 2);
  351. buffer_fast_strcat(wb, rrdset_id(rd->rrdset), string_strlen(rd->rrdset->id));
  352. buffer_fast_strcat(wb, "' ", 2);
  353. buffer_print_uint64_encoded(wb, integer_encoding, rd->rrdset->update_every);
  354. buffer_fast_strcat(wb, " ", 1);
  355. buffer_print_uint64_encoded(wb, integer_encoding, point_end_time_s);
  356. buffer_fast_strcat(wb, " ", 1);
  357. if(point_end_time_s == rsb->wall_clock_time)
  358. buffer_fast_strcat(wb, "#", 1);
  359. else
  360. buffer_print_uint64_encoded(wb, integer_encoding, rsb->wall_clock_time);
  361. buffer_fast_strcat(wb, "\n", 1);
  362. rsb->last_point_end_time_s = point_end_time_s;
  363. rsb->begin_v2_added = true;
  364. }
  365. buffer_fast_strcat(wb, PLUGINSD_KEYWORD_SET_V2, sizeof(PLUGINSD_KEYWORD_SET_V2) - 1);
  366. if(with_slots) {
  367. buffer_fast_strcat(wb, " "PLUGINSD_KEYWORD_SLOT":", sizeof(PLUGINSD_KEYWORD_SLOT) - 1 + 2);
  368. buffer_print_uint64_encoded(wb, integer_encoding, rd->rrdpush.sender.dim_slot);
  369. }
  370. buffer_fast_strcat(wb, " '", 2);
  371. buffer_fast_strcat(wb, rrddim_id(rd), string_strlen(rd->id));
  372. buffer_fast_strcat(wb, "' ", 2);
  373. buffer_print_int64_encoded(wb, integer_encoding, rd->collector.last_collected_value);
  374. buffer_fast_strcat(wb, " ", 1);
  375. if((NETDATA_DOUBLE)rd->collector.last_collected_value == n)
  376. buffer_fast_strcat(wb, "#", 1);
  377. else
  378. buffer_print_netdata_double_encoded(wb, doubles_encoding, n);
  379. buffer_fast_strcat(wb, " ", 1);
  380. buffer_print_sn_flags(wb, flags, true);
  381. buffer_fast_strcat(wb, "\n", 1);
  382. }
  383. void rrdset_push_metrics_finished(RRDSET_STREAM_BUFFER *rsb, RRDSET *st) {
  384. if(!rsb->wb)
  385. return;
  386. if(rsb->v2 && rsb->begin_v2_added) {
  387. if(unlikely(rsb->rrdset_flags & RRDSET_FLAG_UPSTREAM_SEND_VARIABLES))
  388. rrdsetvar_print_to_streaming_custom_chart_variables(st, rsb->wb);
  389. buffer_fast_strcat(rsb->wb, PLUGINSD_KEYWORD_END_V2 "\n", sizeof(PLUGINSD_KEYWORD_END_V2) - 1 + 1);
  390. }
  391. sender_commit(st->rrdhost->sender, rsb->wb, STREAM_TRAFFIC_TYPE_DATA);
  392. *rsb = (RRDSET_STREAM_BUFFER){ .wb = NULL, };
  393. }
  394. // TODO enable this macro before release
  395. #define bail_if_no_cap(cap) \
  396. if(unlikely(!stream_has_capability(host->sender, cap))) { \
  397. return; \
  398. }
  399. #define dyncfg_check_can_push(host) \
  400. if(unlikely(!rrdhost_can_send_definitions_to_parent(host))) \
  401. return; \
  402. bail_if_no_cap(STREAM_CAP_DYNCFG)
  403. // assumes job is locked and acquired!!!
  404. void rrdpush_send_job_status_update(RRDHOST *host, const char *plugin_name, const char *module_name, struct job *job) {
  405. dyncfg_check_can_push(host);
  406. BUFFER *wb = sender_start(host->sender);
  407. buffer_sprintf(wb, PLUGINSD_KEYWORD_REPORT_JOB_STATUS " %s %s %s %s %d", plugin_name, module_name, job->name, job_status2str(job->status), job->state);
  408. if (job->reason && strlen(job->reason))
  409. buffer_sprintf(wb, " \"%s\"", job->reason);
  410. buffer_strcat(wb, "\n");
  411. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_DYNCFG);
  412. sender_thread_buffer_free();
  413. job->dirty = 0;
  414. }
  415. void rrdpush_send_job_deleted(RRDHOST *host, const char *plugin_name, const char *module_name, const char *job_name) {
  416. dyncfg_check_can_push(host);
  417. BUFFER *wb = sender_start(host->sender);
  418. buffer_sprintf(wb, PLUGINSD_KEYWORD_DELETE_JOB " %s %s %s\n", plugin_name, module_name, job_name);
  419. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_DYNCFG);
  420. sender_thread_buffer_free();
  421. }
  422. RRDSET_STREAM_BUFFER rrdset_push_metric_initialize(RRDSET *st, time_t wall_clock_time) {
  423. RRDHOST *host = st->rrdhost;
  424. // fetch the flags we need to check with one atomic operation
  425. RRDHOST_FLAGS host_flags = __atomic_load_n(&host->flags, __ATOMIC_SEQ_CST);
  426. // check if we are not connected
  427. if(unlikely(!(host_flags & RRDHOST_FLAG_RRDPUSH_SENDER_READY_4_METRICS))) {
  428. if(unlikely(!(host_flags & (RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN | RRDHOST_FLAG_RRDPUSH_RECEIVER_DISCONNECTED))))
  429. rrdpush_sender_thread_spawn(host);
  430. if(unlikely(!(host_flags & RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS))) {
  431. rrdhost_flag_set(host, RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS);
  432. nd_log_daemon(NDLP_NOTICE, "STREAM %s [send]: not ready - collected metrics are not sent to parent.", rrdhost_hostname(host));
  433. }
  434. return (RRDSET_STREAM_BUFFER) { .wb = NULL, };
  435. }
  436. else if(unlikely(host_flags & RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS)) {
  437. nd_log_daemon(NDLP_INFO, "STREAM %s [send]: sending metrics to parent...", rrdhost_hostname(host));
  438. rrdhost_flag_clear(host, RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS);
  439. }
  440. if(unlikely(host_flags & RRDHOST_FLAG_GLOBAL_FUNCTIONS_UPDATED)) {
  441. BUFFER *wb = sender_start(host->sender);
  442. rrd_functions_expose_global_rrdpush(host, wb);
  443. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_FUNCTIONS);
  444. }
  445. bool exposed_upstream = rrdset_check_upstream_exposed(st);
  446. RRDSET_FLAGS rrdset_flags = rrdset_flag_get(st);
  447. bool replication_in_progress = !(rrdset_flags & RRDSET_FLAG_SENDER_REPLICATION_FINISHED);
  448. if(unlikely((exposed_upstream && replication_in_progress) ||
  449. !should_send_chart_matching(st, rrdset_flags)))
  450. return (RRDSET_STREAM_BUFFER) { .wb = NULL, };
  451. if(unlikely(!exposed_upstream)) {
  452. BUFFER *wb = sender_start(host->sender);
  453. replication_in_progress = rrdpush_send_chart_definition(wb, st);
  454. }
  455. if(replication_in_progress)
  456. return (RRDSET_STREAM_BUFFER) { .wb = NULL, };
  457. return (RRDSET_STREAM_BUFFER) {
  458. .capabilities = host->sender->capabilities,
  459. .v2 = stream_has_capability(host->sender, STREAM_CAP_INTERPOLATED),
  460. .rrdset_flags = rrdset_flags,
  461. .wb = sender_start(host->sender),
  462. .wall_clock_time = wall_clock_time,
  463. };
  464. }
  465. // labels
  466. static int send_labels_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data) {
  467. BUFFER *wb = (BUFFER *)data;
  468. buffer_sprintf(wb, "LABEL \"%s\" = %d \"%s\"\n", name, ls, value);
  469. return 1;
  470. }
  471. void rrdpush_send_host_labels(RRDHOST *host) {
  472. if(unlikely(!rrdhost_can_send_definitions_to_parent(host)
  473. || !stream_has_capability(host->sender, STREAM_CAP_HLABELS)))
  474. return;
  475. BUFFER *wb = sender_start(host->sender);
  476. rrdlabels_walkthrough_read(host->rrdlabels, send_labels_callback, wb);
  477. buffer_sprintf(wb, "OVERWRITE %s\n", "labels");
  478. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_METADATA);
  479. sender_thread_buffer_free();
  480. }
  481. void rrdpush_send_global_functions(RRDHOST *host) {
  482. if(!stream_has_capability(host->sender, STREAM_CAP_FUNCTIONS))
  483. return;
  484. if(unlikely(!rrdhost_can_send_definitions_to_parent(host)))
  485. return;
  486. BUFFER *wb = sender_start(host->sender);
  487. rrd_functions_expose_global_rrdpush(host, wb);
  488. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_FUNCTIONS);
  489. sender_thread_buffer_free();
  490. }
  491. void rrdpush_send_dyncfg(RRDHOST *host) {
  492. dyncfg_check_can_push(host);
  493. BUFFER *wb = sender_start(host->sender);
  494. DICTIONARY *plugins_dict = host->configurable_plugins;
  495. struct configurable_plugin *plug;
  496. dfe_start_read(plugins_dict, plug) {
  497. buffer_sprintf(wb, PLUGINSD_KEYWORD_DYNCFG_ENABLE " %s\n", plug->name);
  498. struct module *mod;
  499. dfe_start_read(plug->modules, mod) {
  500. buffer_sprintf(wb, PLUGINSD_KEYWORD_DYNCFG_REGISTER_MODULE " %s %s %s\n", plug->name, mod->name, module_type2str(mod->type));
  501. struct job *job;
  502. dfe_start_read(mod->jobs, job) {
  503. pthread_mutex_lock(&job->lock);
  504. buffer_sprintf(wb, PLUGINSD_KEYWORD_DYNCFG_REGISTER_JOB " %s %s %s %s %"PRIu32"\n", plug->name, mod->name, job->name, job_type2str(job->type), job->flags);
  505. buffer_sprintf(wb, PLUGINSD_KEYWORD_REPORT_JOB_STATUS " %s %s %s %s %d", plug->name, mod->name, job->name, job_status2str(job->status), job->state);
  506. if (job->reason)
  507. buffer_sprintf(wb, " \"%s\"", job->reason);
  508. buffer_sprintf(wb, "\n");
  509. job->dirty = 0;
  510. pthread_mutex_unlock(&job->lock);
  511. } dfe_done(job);
  512. } dfe_done(mod);
  513. }
  514. dfe_done(plug);
  515. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_DYNCFG);
  516. sender_thread_buffer_free();
  517. }
  518. void rrdpush_send_dyncfg_enable(RRDHOST *host, const char *plugin_name)
  519. {
  520. dyncfg_check_can_push(host);
  521. BUFFER *wb = sender_start(host->sender);
  522. buffer_sprintf(wb, PLUGINSD_KEYWORD_DYNCFG_ENABLE " %s\n", plugin_name);
  523. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_METADATA);
  524. sender_thread_buffer_free();
  525. }
  526. void rrdpush_send_dyncfg_reg_module(RRDHOST *host, const char *plugin_name, const char *module_name, enum module_type type)
  527. {
  528. dyncfg_check_can_push(host);
  529. BUFFER *wb = sender_start(host->sender);
  530. buffer_sprintf(wb, PLUGINSD_KEYWORD_DYNCFG_REGISTER_MODULE " %s %s %s\n", plugin_name, module_name, module_type2str(type));
  531. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_DYNCFG);
  532. sender_thread_buffer_free();
  533. }
  534. void rrdpush_send_dyncfg_reg_job(RRDHOST *host, const char *plugin_name, const char *module_name, const char *job_name, enum job_type type, uint32_t flags)
  535. {
  536. dyncfg_check_can_push(host);
  537. BUFFER *wb = sender_start(host->sender);
  538. buffer_sprintf(wb, PLUGINSD_KEYWORD_DYNCFG_REGISTER_JOB " %s %s %s %s %"PRIu32"\n", plugin_name, module_name, job_name, job_type2str(type), flags);
  539. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_DYNCFG);
  540. sender_thread_buffer_free();
  541. }
  542. void rrdpush_send_dyncfg_reset(RRDHOST *host, const char *plugin_name)
  543. {
  544. dyncfg_check_can_push(host);
  545. BUFFER *wb = sender_start(host->sender);
  546. buffer_sprintf(wb, PLUGINSD_KEYWORD_DYNCFG_RESET " %s\n", plugin_name);
  547. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_METADATA);
  548. sender_thread_buffer_free();
  549. }
  550. void rrdpush_send_claimed_id(RRDHOST *host) {
  551. if(!stream_has_capability(host->sender, STREAM_CAP_CLAIM))
  552. return;
  553. if(unlikely(!rrdhost_can_send_definitions_to_parent(host)))
  554. return;
  555. BUFFER *wb = sender_start(host->sender);
  556. rrdhost_aclk_state_lock(host);
  557. buffer_sprintf(wb, "CLAIMED_ID %s %s\n", host->machine_guid, (host->aclk_state.claimed_id ? host->aclk_state.claimed_id : "NULL") );
  558. rrdhost_aclk_state_unlock(host);
  559. sender_commit(host->sender, wb, STREAM_TRAFFIC_TYPE_METADATA);
  560. sender_thread_buffer_free();
  561. }
  562. int connect_to_one_of_destinations(
  563. RRDHOST *host,
  564. int default_port,
  565. struct timeval *timeout,
  566. size_t *reconnects_counter,
  567. char *connected_to,
  568. size_t connected_to_size,
  569. struct rrdpush_destinations **destination)
  570. {
  571. int sock = -1;
  572. for (struct rrdpush_destinations *d = host->destinations; d; d = d->next) {
  573. time_t now = now_realtime_sec();
  574. if(d->postpone_reconnection_until > now)
  575. continue;
  576. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  577. "STREAM %s: connecting to '%s' (default port: %d)...",
  578. rrdhost_hostname(host), string2str(d->destination), default_port);
  579. if (reconnects_counter)
  580. *reconnects_counter += 1;
  581. d->since = now;
  582. d->attempts++;
  583. sock = connect_to_this(string2str(d->destination), default_port, timeout);
  584. if (sock != -1) {
  585. if (connected_to && connected_to_size)
  586. strncpyz(connected_to, string2str(d->destination), connected_to_size);
  587. *destination = d;
  588. // move the current item to the end of the list
  589. // without this, this destination will break the loop again and again
  590. // not advancing the destinations to find one that may work
  591. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(host->destinations, d, prev, next);
  592. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(host->destinations, d, prev, next);
  593. break;
  594. }
  595. }
  596. return sock;
  597. }
  598. struct destinations_init_tmp {
  599. RRDHOST *host;
  600. struct rrdpush_destinations *list;
  601. int count;
  602. };
  603. bool destinations_init_add_one(char *entry, void *data) {
  604. struct destinations_init_tmp *t = data;
  605. struct rrdpush_destinations *d = callocz(1, sizeof(struct rrdpush_destinations));
  606. char *colon_ssl = strstr(entry, ":SSL");
  607. if(colon_ssl) {
  608. *colon_ssl = '\0';
  609. d->ssl = true;
  610. }
  611. else
  612. d->ssl = false;
  613. d->destination = string_strdupz(entry);
  614. __atomic_add_fetch(&netdata_buffers_statistics.rrdhost_senders, sizeof(struct rrdpush_destinations), __ATOMIC_RELAXED);
  615. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(t->list, d, prev, next);
  616. t->count++;
  617. nd_log_daemon(NDLP_INFO, "STREAM: added streaming destination No %d: '%s' to host '%s'", t->count, string2str(d->destination), rrdhost_hostname(t->host));
  618. return false; // we return false, so that we will get all defined destinations
  619. }
  620. void rrdpush_destinations_init(RRDHOST *host) {
  621. if(!host->rrdpush_send_destination) return;
  622. rrdpush_destinations_free(host);
  623. struct destinations_init_tmp t = {
  624. .host = host,
  625. .list = NULL,
  626. .count = 0,
  627. };
  628. foreach_entry_in_connection_string(host->rrdpush_send_destination, destinations_init_add_one, &t);
  629. host->destinations = t.list;
  630. }
  631. void rrdpush_destinations_free(RRDHOST *host) {
  632. while (host->destinations) {
  633. struct rrdpush_destinations *tmp = host->destinations;
  634. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(host->destinations, tmp, prev, next);
  635. string_freez(tmp->destination);
  636. freez(tmp);
  637. __atomic_sub_fetch(&netdata_buffers_statistics.rrdhost_senders, sizeof(struct rrdpush_destinations), __ATOMIC_RELAXED);
  638. }
  639. host->destinations = NULL;
  640. }
  641. // ----------------------------------------------------------------------------
  642. // rrdpush sender thread
  643. // Either the receiver lost the connection or the host is being destroyed.
  644. // The sender mutex guards thread creation, any spurious data is wiped on reconnection.
  645. void rrdpush_sender_thread_stop(RRDHOST *host, STREAM_HANDSHAKE reason, bool wait) {
  646. if (!host->sender)
  647. return;
  648. sender_lock(host->sender);
  649. if(rrdhost_flag_check(host, RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN)) {
  650. host->sender->exit.shutdown = true;
  651. host->sender->exit.reason = reason;
  652. // signal it to cancel
  653. netdata_thread_cancel(host->rrdpush_sender_thread);
  654. }
  655. sender_unlock(host->sender);
  656. if(wait) {
  657. sender_lock(host->sender);
  658. while(host->sender->tid) {
  659. sender_unlock(host->sender);
  660. sleep_usec(10 * USEC_PER_MS);
  661. sender_lock(host->sender);
  662. }
  663. sender_unlock(host->sender);
  664. }
  665. }
  666. // ----------------------------------------------------------------------------
  667. // rrdpush receiver thread
  668. static void rrdpush_sender_thread_spawn(RRDHOST *host) {
  669. sender_lock(host->sender);
  670. if(!rrdhost_flag_check(host, RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN)) {
  671. char tag[NETDATA_THREAD_TAG_MAX + 1];
  672. snprintfz(tag, NETDATA_THREAD_TAG_MAX, THREAD_TAG_STREAM_SENDER "[%s]", rrdhost_hostname(host));
  673. if(netdata_thread_create(&host->rrdpush_sender_thread, tag, NETDATA_THREAD_OPTION_DEFAULT, rrdpush_sender_thread, (void *) host->sender))
  674. nd_log_daemon(NDLP_ERR, "STREAM %s [send]: failed to create new thread for client.", rrdhost_hostname(host));
  675. else
  676. rrdhost_flag_set(host, RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN);
  677. }
  678. sender_unlock(host->sender);
  679. }
  680. int rrdpush_receiver_permission_denied(struct web_client *w) {
  681. // we always respond with the same message and error code
  682. // to prevent an attacker from gaining info about the error
  683. buffer_flush(w->response.data);
  684. buffer_strcat(w->response.data, START_STREAMING_ERROR_NOT_PERMITTED);
  685. return HTTP_RESP_UNAUTHORIZED;
  686. }
  687. int rrdpush_receiver_too_busy_now(struct web_client *w) {
  688. // we always respond with the same message and error code
  689. // to prevent an attacker from gaining info about the error
  690. buffer_flush(w->response.data);
  691. buffer_strcat(w->response.data, START_STREAMING_ERROR_BUSY_TRY_LATER);
  692. return HTTP_RESP_SERVICE_UNAVAILABLE;
  693. }
  694. static void rrdpush_receiver_takeover_web_connection(struct web_client *w, struct receiver_state *rpt) {
  695. rpt->fd = w->ifd;
  696. #ifdef ENABLE_HTTPS
  697. rpt->ssl.conn = w->ssl.conn;
  698. rpt->ssl.state = w->ssl.state;
  699. w->ssl = NETDATA_SSL_UNSET_CONNECTION;
  700. #endif
  701. WEB_CLIENT_IS_DEAD(w);
  702. if(web_server_mode == WEB_SERVER_MODE_STATIC_THREADED) {
  703. web_client_flag_set(w, WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET);
  704. }
  705. else {
  706. if(w->ifd == w->ofd)
  707. w->ifd = w->ofd = -1;
  708. else
  709. w->ifd = -1;
  710. }
  711. buffer_flush(w->response.data);
  712. }
  713. void *rrdpush_receiver_thread(void *ptr);
  714. int rrdpush_receiver_thread_spawn(struct web_client *w, char *decoded_query_string, void *h2o_ctx) {
  715. if(!service_running(ABILITY_STREAMING_CONNECTIONS))
  716. return rrdpush_receiver_too_busy_now(w);
  717. struct receiver_state *rpt = callocz(1, sizeof(*rpt));
  718. rpt->last_msg_t = now_monotonic_sec();
  719. rpt->hops = 1;
  720. rpt->capabilities = STREAM_CAP_INVALID;
  721. #ifdef ENABLE_H2O
  722. rpt->h2o_ctx = h2o_ctx;
  723. #endif
  724. __atomic_add_fetch(&netdata_buffers_statistics.rrdhost_receivers, sizeof(*rpt), __ATOMIC_RELAXED);
  725. __atomic_add_fetch(&netdata_buffers_statistics.rrdhost_allocations_size, sizeof(struct rrdhost_system_info), __ATOMIC_RELAXED);
  726. rpt->system_info = callocz(1, sizeof(struct rrdhost_system_info));
  727. rpt->system_info->hops = rpt->hops;
  728. rpt->fd = -1;
  729. rpt->client_ip = strdupz(w->client_ip);
  730. rpt->client_port = strdupz(w->client_port);
  731. #ifdef ENABLE_HTTPS
  732. rpt->ssl = NETDATA_SSL_UNSET_CONNECTION;
  733. #endif
  734. rpt->config.update_every = default_rrd_update_every;
  735. // parse the parameters and fill rpt and rpt->system_info
  736. while(decoded_query_string) {
  737. char *value = strsep_skip_consecutive_separators(&decoded_query_string, "&");
  738. if(!value || !*value) continue;
  739. char *name = strsep_skip_consecutive_separators(&value, "=");
  740. if(!name || !*name) continue;
  741. if(!value || !*value) continue;
  742. if(!strcmp(name, "key") && !rpt->key)
  743. rpt->key = strdupz(value);
  744. else if(!strcmp(name, "hostname") && !rpt->hostname)
  745. rpt->hostname = strdupz(value);
  746. else if(!strcmp(name, "registry_hostname") && !rpt->registry_hostname)
  747. rpt->registry_hostname = strdupz(value);
  748. else if(!strcmp(name, "machine_guid") && !rpt->machine_guid)
  749. rpt->machine_guid = strdupz(value);
  750. else if(!strcmp(name, "update_every"))
  751. rpt->config.update_every = (int)strtoul(value, NULL, 0);
  752. else if(!strcmp(name, "os") && !rpt->os)
  753. rpt->os = strdupz(value);
  754. else if(!strcmp(name, "timezone") && !rpt->timezone)
  755. rpt->timezone = strdupz(value);
  756. else if(!strcmp(name, "abbrev_timezone") && !rpt->abbrev_timezone)
  757. rpt->abbrev_timezone = strdupz(value);
  758. else if(!strcmp(name, "utc_offset"))
  759. rpt->utc_offset = (int32_t)strtol(value, NULL, 0);
  760. else if(!strcmp(name, "hops"))
  761. rpt->hops = rpt->system_info->hops = (uint16_t) strtoul(value, NULL, 0);
  762. else if(!strcmp(name, "ml_capable"))
  763. rpt->system_info->ml_capable = strtoul(value, NULL, 0);
  764. else if(!strcmp(name, "ml_enabled"))
  765. rpt->system_info->ml_enabled = strtoul(value, NULL, 0);
  766. else if(!strcmp(name, "mc_version"))
  767. rpt->system_info->mc_version = strtoul(value, NULL, 0);
  768. else if(!strcmp(name, "tags") && !rpt->tags)
  769. rpt->tags = strdupz(value);
  770. else if(!strcmp(name, "ver") && (rpt->capabilities & STREAM_CAP_INVALID))
  771. rpt->capabilities = convert_stream_version_to_capabilities(strtoul(value, NULL, 0), NULL, false);
  772. else {
  773. // An old Netdata child does not have a compatible streaming protocol, map to something sane.
  774. if (!strcmp(name, "NETDATA_SYSTEM_OS_NAME"))
  775. name = "NETDATA_HOST_OS_NAME";
  776. else if (!strcmp(name, "NETDATA_SYSTEM_OS_ID"))
  777. name = "NETDATA_HOST_OS_ID";
  778. else if (!strcmp(name, "NETDATA_SYSTEM_OS_ID_LIKE"))
  779. name = "NETDATA_HOST_OS_ID_LIKE";
  780. else if (!strcmp(name, "NETDATA_SYSTEM_OS_VERSION"))
  781. name = "NETDATA_HOST_OS_VERSION";
  782. else if (!strcmp(name, "NETDATA_SYSTEM_OS_VERSION_ID"))
  783. name = "NETDATA_HOST_OS_VERSION_ID";
  784. else if (!strcmp(name, "NETDATA_SYSTEM_OS_DETECTION"))
  785. name = "NETDATA_HOST_OS_DETECTION";
  786. else if(!strcmp(name, "NETDATA_PROTOCOL_VERSION") && (rpt->capabilities & STREAM_CAP_INVALID))
  787. rpt->capabilities = convert_stream_version_to_capabilities(1, NULL, false);
  788. if (unlikely(rrdhost_set_system_info_variable(rpt->system_info, name, value))) {
  789. nd_log_daemon(NDLP_NOTICE, "STREAM '%s' [receive from [%s]:%s]: "
  790. "request has parameter '%s' = '%s', which is not used."
  791. , (rpt->hostname && *rpt->hostname) ? rpt->hostname : "-"
  792. , rpt->client_ip, rpt->client_port
  793. , name, value);
  794. }
  795. }
  796. }
  797. if (rpt->capabilities & STREAM_CAP_INVALID)
  798. // no version is supplied, assume version 0;
  799. rpt->capabilities = convert_stream_version_to_capabilities(0, NULL, false);
  800. // find the program name and version
  801. if(w->user_agent && w->user_agent[0]) {
  802. char *t = strchr(w->user_agent, '/');
  803. if(t && *t) {
  804. *t = '\0';
  805. t++;
  806. }
  807. rpt->program_name = strdupz(w->user_agent);
  808. if(t && *t) rpt->program_version = strdupz(t);
  809. }
  810. // check if we should accept this connection
  811. if(!rpt->key || !*rpt->key) {
  812. rrdpush_receive_log_status(
  813. rpt, "request without an API key, rejecting connection",
  814. RRDPUSH_STATUS_NO_API_KEY, NDLP_WARNING);
  815. receiver_state_free(rpt);
  816. return rrdpush_receiver_permission_denied(w);
  817. }
  818. if(!rpt->hostname || !*rpt->hostname) {
  819. rrdpush_receive_log_status(
  820. rpt, "request without a hostname, rejecting connection",
  821. RRDPUSH_STATUS_NO_HOSTNAME, NDLP_WARNING);
  822. receiver_state_free(rpt);
  823. return rrdpush_receiver_permission_denied(w);
  824. }
  825. if(!rpt->registry_hostname)
  826. rpt->registry_hostname = strdupz(rpt->hostname);
  827. if(!rpt->machine_guid || !*rpt->machine_guid) {
  828. rrdpush_receive_log_status(
  829. rpt, "request without a machine GUID, rejecting connection",
  830. RRDPUSH_STATUS_NO_MACHINE_GUID, NDLP_WARNING);
  831. receiver_state_free(rpt);
  832. return rrdpush_receiver_permission_denied(w);
  833. }
  834. {
  835. char buf[GUID_LEN + 1];
  836. if (regenerate_guid(rpt->key, buf) == -1) {
  837. rrdpush_receive_log_status(
  838. rpt, "API key is not a valid UUID (use the command uuidgen to generate one)",
  839. RRDPUSH_STATUS_INVALID_API_KEY, NDLP_WARNING);
  840. receiver_state_free(rpt);
  841. return rrdpush_receiver_permission_denied(w);
  842. }
  843. if (regenerate_guid(rpt->machine_guid, buf) == -1) {
  844. rrdpush_receive_log_status(
  845. rpt, "machine GUID is not a valid UUID",
  846. RRDPUSH_STATUS_INVALID_MACHINE_GUID, NDLP_WARNING);
  847. receiver_state_free(rpt);
  848. return rrdpush_receiver_permission_denied(w);
  849. }
  850. }
  851. const char *api_key_type = appconfig_get(&stream_config, rpt->key, "type", "api");
  852. if(!api_key_type || !*api_key_type) api_key_type = "unknown";
  853. if(strcmp(api_key_type, "api") != 0) {
  854. rrdpush_receive_log_status(
  855. rpt, "API key is a machine GUID",
  856. RRDPUSH_STATUS_INVALID_API_KEY, NDLP_WARNING);
  857. receiver_state_free(rpt);
  858. return rrdpush_receiver_permission_denied(w);
  859. }
  860. if(!appconfig_get_boolean(&stream_config, rpt->key, "enabled", 0)) {
  861. rrdpush_receive_log_status(
  862. rpt, "API key is not enabled",
  863. RRDPUSH_STATUS_API_KEY_DISABLED, NDLP_WARNING);
  864. receiver_state_free(rpt);
  865. return rrdpush_receiver_permission_denied(w);
  866. }
  867. {
  868. SIMPLE_PATTERN *key_allow_from = simple_pattern_create(
  869. appconfig_get(&stream_config, rpt->key, "allow from", "*"),
  870. NULL, SIMPLE_PATTERN_EXACT, true);
  871. if(key_allow_from) {
  872. if(!simple_pattern_matches(key_allow_from, w->client_ip)) {
  873. simple_pattern_free(key_allow_from);
  874. rrdpush_receive_log_status(
  875. rpt, "API key is not allowed from this IP",
  876. RRDPUSH_STATUS_NOT_ALLOWED_IP, NDLP_WARNING);
  877. receiver_state_free(rpt);
  878. return rrdpush_receiver_permission_denied(w);
  879. }
  880. simple_pattern_free(key_allow_from);
  881. }
  882. }
  883. {
  884. const char *machine_guid_type = appconfig_get(&stream_config, rpt->machine_guid, "type", "machine");
  885. if (!machine_guid_type || !*machine_guid_type) machine_guid_type = "unknown";
  886. if (strcmp(machine_guid_type, "machine") != 0) {
  887. rrdpush_receive_log_status(
  888. rpt, "machine GUID is an API key",
  889. RRDPUSH_STATUS_INVALID_MACHINE_GUID, NDLP_WARNING);
  890. receiver_state_free(rpt);
  891. return rrdpush_receiver_permission_denied(w);
  892. }
  893. }
  894. if(!appconfig_get_boolean(&stream_config, rpt->machine_guid, "enabled", 1)) {
  895. rrdpush_receive_log_status(
  896. rpt, "machine GUID is not enabled",
  897. RRDPUSH_STATUS_MACHINE_GUID_DISABLED, NDLP_WARNING);
  898. receiver_state_free(rpt);
  899. return rrdpush_receiver_permission_denied(w);
  900. }
  901. {
  902. SIMPLE_PATTERN *machine_allow_from = simple_pattern_create(
  903. appconfig_get(&stream_config, rpt->machine_guid, "allow from", "*"),
  904. NULL, SIMPLE_PATTERN_EXACT, true);
  905. if(machine_allow_from) {
  906. if(!simple_pattern_matches(machine_allow_from, w->client_ip)) {
  907. simple_pattern_free(machine_allow_from);
  908. rrdpush_receive_log_status(
  909. rpt, "machine GUID is not allowed from this IP",
  910. RRDPUSH_STATUS_NOT_ALLOWED_IP, NDLP_WARNING);
  911. receiver_state_free(rpt);
  912. return rrdpush_receiver_permission_denied(w);
  913. }
  914. simple_pattern_free(machine_allow_from);
  915. }
  916. }
  917. if (strcmp(rpt->machine_guid, localhost->machine_guid) == 0) {
  918. rrdpush_receiver_takeover_web_connection(w, rpt);
  919. rrdpush_receive_log_status(
  920. rpt, "machine GUID is my own",
  921. RRDPUSH_STATUS_LOCALHOST, NDLP_DEBUG);
  922. char initial_response[HTTP_HEADER_SIZE + 1];
  923. snprintfz(initial_response, HTTP_HEADER_SIZE, "%s", START_STREAMING_ERROR_SAME_LOCALHOST);
  924. if(send_timeout(
  925. #ifdef ENABLE_HTTPS
  926. &rpt->ssl,
  927. #endif
  928. rpt->fd, initial_response, strlen(initial_response), 0, 60) != (ssize_t)strlen(initial_response)) {
  929. nd_log_daemon(NDLP_ERR, "STREAM '%s' [receive from [%s]:%s]: "
  930. "failed to reply."
  931. , rpt->hostname
  932. , rpt->client_ip, rpt->client_port
  933. );
  934. }
  935. receiver_state_free(rpt);
  936. return HTTP_RESP_OK;
  937. }
  938. if(unlikely(web_client_streaming_rate_t > 0)) {
  939. static SPINLOCK spinlock = NETDATA_SPINLOCK_INITIALIZER;
  940. static time_t last_stream_accepted_t = 0;
  941. time_t now = now_realtime_sec();
  942. spinlock_lock(&spinlock);
  943. if(unlikely(last_stream_accepted_t == 0))
  944. last_stream_accepted_t = now;
  945. if(now - last_stream_accepted_t < web_client_streaming_rate_t) {
  946. spinlock_unlock(&spinlock);
  947. char msg[100 + 1];
  948. snprintfz(msg, sizeof(msg) - 1,
  949. "rate limit, will accept new connection in %ld secs",
  950. (long)(web_client_streaming_rate_t - (now - last_stream_accepted_t)));
  951. rrdpush_receive_log_status(
  952. rpt, msg,
  953. RRDPUSH_STATUS_RATE_LIMIT, NDLP_NOTICE);
  954. receiver_state_free(rpt);
  955. return rrdpush_receiver_too_busy_now(w);
  956. }
  957. last_stream_accepted_t = now;
  958. spinlock_unlock(&spinlock);
  959. }
  960. /*
  961. * Quick path for rejecting multiple connections. The lock taken is fine-grained - it only protects the receiver
  962. * pointer within the host (if a host exists). This protects against multiple concurrent web requests hitting
  963. * separate threads within the web-server and landing here. The lock guards the thread-shutdown sequence that
  964. * detaches the receiver from the host. If the host is being created (first time-access) then we also use the
  965. * lock to prevent race-hazard (two threads try to create the host concurrently, one wins and the other does a
  966. * lookup to the now-attached structure).
  967. */
  968. {
  969. time_t age = 0;
  970. bool receiver_stale = false;
  971. bool receiver_working = false;
  972. rrd_rdlock();
  973. RRDHOST *host = rrdhost_find_by_guid(rpt->machine_guid);
  974. if (unlikely(host && rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))) /* Ignore archived hosts. */
  975. host = NULL;
  976. if (host) {
  977. netdata_mutex_lock(&host->receiver_lock);
  978. if (host->receiver) {
  979. age = now_monotonic_sec() - host->receiver->last_msg_t;
  980. if (age < 30)
  981. receiver_working = true;
  982. else
  983. receiver_stale = true;
  984. }
  985. netdata_mutex_unlock(&host->receiver_lock);
  986. }
  987. rrd_unlock();
  988. if (receiver_stale && stop_streaming_receiver(host, STREAM_HANDSHAKE_DISCONNECT_STALE_RECEIVER)) {
  989. // we stopped the receiver
  990. // we can proceed with this connection
  991. receiver_stale = false;
  992. nd_log_daemon(NDLP_NOTICE, "STREAM '%s' [receive from [%s]:%s]: "
  993. "stopped previous stale receiver to accept this one."
  994. , rpt->hostname
  995. , rpt->client_ip, rpt->client_port
  996. );
  997. }
  998. if (receiver_working || receiver_stale) {
  999. // another receiver is already connected
  1000. // try again later
  1001. char msg[200 + 1];
  1002. snprintfz(msg, sizeof(msg) - 1,
  1003. "multiple connections for same host, "
  1004. "old connection was last used %ld secs ago%s",
  1005. age, receiver_stale ? " (signaled old receiver to stop)" : " (new connection not accepted)");
  1006. rrdpush_receive_log_status(
  1007. rpt, msg,
  1008. RRDPUSH_STATUS_ALREADY_CONNECTED, NDLP_DEBUG);
  1009. // Have not set WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET - caller should clean up
  1010. buffer_flush(w->response.data);
  1011. buffer_strcat(w->response.data, START_STREAMING_ERROR_ALREADY_STREAMING);
  1012. receiver_state_free(rpt);
  1013. return HTTP_RESP_CONFLICT;
  1014. }
  1015. }
  1016. rrdpush_receiver_takeover_web_connection(w, rpt);
  1017. char tag[NETDATA_THREAD_TAG_MAX + 1];
  1018. snprintfz(tag, NETDATA_THREAD_TAG_MAX, THREAD_TAG_STREAM_RECEIVER "[%s]", rpt->hostname);
  1019. tag[NETDATA_THREAD_TAG_MAX] = '\0';
  1020. if(netdata_thread_create(&rpt->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, rrdpush_receiver_thread, (void *)rpt)) {
  1021. rrdpush_receive_log_status(
  1022. rpt, "can't create receiver thread",
  1023. RRDPUSH_STATUS_INTERNAL_SERVER_ERROR, NDLP_ERR);
  1024. buffer_flush(w->response.data);
  1025. buffer_strcat(w->response.data, "Can't handle this request");
  1026. receiver_state_free(rpt);
  1027. return HTTP_RESP_INTERNAL_SERVER_ERROR;
  1028. }
  1029. // prevent the caller from closing the streaming socket
  1030. return HTTP_RESP_OK;
  1031. }
  1032. void rrdpush_reset_destinations_postpone_time(RRDHOST *host) {
  1033. uint32_t wait = (host->sender) ? host->sender->reconnect_delay : 5;
  1034. time_t now = now_realtime_sec();
  1035. for (struct rrdpush_destinations *d = host->destinations; d; d = d->next)
  1036. d->postpone_reconnection_until = now + wait;
  1037. }
  1038. static struct {
  1039. STREAM_HANDSHAKE err;
  1040. const char *str;
  1041. } handshake_errors[] = {
  1042. { STREAM_HANDSHAKE_OK_V3, "CONNECTED" },
  1043. { STREAM_HANDSHAKE_OK_V2, "CONNECTED" },
  1044. { STREAM_HANDSHAKE_OK_V1, "CONNECTED" },
  1045. { STREAM_HANDSHAKE_NEVER, "" },
  1046. { STREAM_HANDSHAKE_ERROR_BAD_HANDSHAKE, "BAD HANDSHAKE" },
  1047. { STREAM_HANDSHAKE_ERROR_LOCALHOST, "LOCALHOST" },
  1048. { STREAM_HANDSHAKE_ERROR_ALREADY_CONNECTED, "ALREADY CONNECTED" },
  1049. { STREAM_HANDSHAKE_ERROR_DENIED, "DENIED" },
  1050. { STREAM_HANDSHAKE_ERROR_SEND_TIMEOUT, "SEND TIMEOUT" },
  1051. { STREAM_HANDSHAKE_ERROR_RECEIVE_TIMEOUT, "RECEIVE TIMEOUT" },
  1052. { STREAM_HANDSHAKE_ERROR_INVALID_CERTIFICATE, "INVALID CERTIFICATE" },
  1053. { STREAM_HANDSHAKE_ERROR_SSL_ERROR, "SSL ERROR" },
  1054. { STREAM_HANDSHAKE_ERROR_CANT_CONNECT, "CANT CONNECT" },
  1055. { STREAM_HANDSHAKE_BUSY_TRY_LATER, "BUSY TRY LATER" },
  1056. { STREAM_HANDSHAKE_INTERNAL_ERROR, "INTERNAL ERROR" },
  1057. { STREAM_HANDSHAKE_INITIALIZATION, "REMOTE IS INITIALIZING" },
  1058. { STREAM_HANDSHAKE_DISCONNECT_HOST_CLEANUP, "DISCONNECTED HOST CLEANUP" },
  1059. { STREAM_HANDSHAKE_DISCONNECT_STALE_RECEIVER, "DISCONNECTED STALE RECEIVER" },
  1060. { STREAM_HANDSHAKE_DISCONNECT_SHUTDOWN, "DISCONNECTED SHUTDOWN REQUESTED" },
  1061. { STREAM_HANDSHAKE_DISCONNECT_NETDATA_EXIT, "DISCONNECTED NETDATA EXIT" },
  1062. { STREAM_HANDSHAKE_DISCONNECT_PARSER_EXIT, "DISCONNECTED PARSE ENDED" },
  1063. {STREAM_HANDSHAKE_DISCONNECT_UNKNOWN_SOCKET_READ_ERROR, "DISCONNECTED UNKNOWN SOCKET READ ERROR" },
  1064. { STREAM_HANDSHAKE_DISCONNECT_PARSER_FAILED, "DISCONNECTED PARSE ERROR" },
  1065. { STREAM_HANDSHAKE_DISCONNECT_RECEIVER_LEFT, "DISCONNECTED RECEIVER LEFT" },
  1066. { STREAM_HANDSHAKE_DISCONNECT_ORPHAN_HOST, "DISCONNECTED ORPHAN HOST" },
  1067. { STREAM_HANDSHAKE_NON_STREAMABLE_HOST, "NON STREAMABLE HOST" },
  1068. { STREAM_HANDSHAKE_DISCONNECT_NOT_SUFFICIENT_READ_BUFFER, "DISCONNECTED NOT SUFFICIENT READ BUFFER" },
  1069. {STREAM_HANDSHAKE_DISCONNECT_SOCKET_EOF, "DISCONNECTED SOCKET EOF" },
  1070. {STREAM_HANDSHAKE_DISCONNECT_SOCKET_READ_FAILED, "DISCONNECTED SOCKET READ FAILED" },
  1071. {STREAM_HANDSHAKE_DISCONNECT_SOCKET_READ_TIMEOUT, "DISCONNECTED SOCKET READ TIMEOUT" },
  1072. { 0, NULL },
  1073. };
  1074. const char *stream_handshake_error_to_string(STREAM_HANDSHAKE handshake_error) {
  1075. if(handshake_error >= STREAM_HANDSHAKE_OK_V1)
  1076. // handshake_error is the whole version / capabilities number
  1077. return "CONNECTED";
  1078. for(size_t i = 0; handshake_errors[i].str ; i++) {
  1079. if(handshake_error == handshake_errors[i].err)
  1080. return handshake_errors[i].str;
  1081. }
  1082. return "UNKNOWN";
  1083. }
  1084. static struct {
  1085. STREAM_CAPABILITIES cap;
  1086. const char *str;
  1087. } capability_names[] = {
  1088. {STREAM_CAP_V1, "V1" },
  1089. {STREAM_CAP_V2, "V2" },
  1090. {STREAM_CAP_VN, "VN" },
  1091. {STREAM_CAP_VCAPS, "VCAPS" },
  1092. {STREAM_CAP_HLABELS, "HLABELS" },
  1093. {STREAM_CAP_CLAIM, "CLAIM" },
  1094. {STREAM_CAP_CLABELS, "CLABELS" },
  1095. {STREAM_CAP_LZ4, "LZ4" },
  1096. {STREAM_CAP_FUNCTIONS, "FUNCTIONS" },
  1097. {STREAM_CAP_REPLICATION, "REPLICATION" },
  1098. {STREAM_CAP_BINARY, "BINARY" },
  1099. {STREAM_CAP_INTERPOLATED, "INTERPOLATED" },
  1100. {STREAM_CAP_IEEE754, "IEEE754" },
  1101. {STREAM_CAP_DATA_WITH_ML, "ML" },
  1102. {STREAM_CAP_DYNCFG, "DYNCFG" },
  1103. {STREAM_CAP_SLOTS, "SLOTS" },
  1104. {STREAM_CAP_ZSTD, "ZSTD" },
  1105. {STREAM_CAP_GZIP, "GZIP" },
  1106. {STREAM_CAP_BROTLI, "BROTLI" },
  1107. {0 , NULL },
  1108. };
  1109. void stream_capabilities_to_string(BUFFER *wb, STREAM_CAPABILITIES caps) {
  1110. for(size_t i = 0; capability_names[i].str ; i++) {
  1111. if(caps & capability_names[i].cap) {
  1112. buffer_strcat(wb, capability_names[i].str);
  1113. buffer_strcat(wb, " ");
  1114. }
  1115. }
  1116. }
  1117. void stream_capabilities_to_json_array(BUFFER *wb, STREAM_CAPABILITIES caps, const char *key) {
  1118. if(key)
  1119. buffer_json_member_add_array(wb, key);
  1120. else
  1121. buffer_json_add_array_item_array(wb);
  1122. for(size_t i = 0; capability_names[i].str ; i++) {
  1123. if(caps & capability_names[i].cap)
  1124. buffer_json_add_array_item_string(wb, capability_names[i].str);
  1125. }
  1126. buffer_json_array_close(wb);
  1127. }
  1128. void log_receiver_capabilities(struct receiver_state *rpt) {
  1129. BUFFER *wb = buffer_create(100, NULL);
  1130. stream_capabilities_to_string(wb, rpt->capabilities);
  1131. nd_log_daemon(NDLP_INFO, "STREAM %s [receive from [%s]:%s]: established link with negotiated capabilities: %s",
  1132. rrdhost_hostname(rpt->host), rpt->client_ip, rpt->client_port, buffer_tostring(wb));
  1133. buffer_free(wb);
  1134. }
  1135. void log_sender_capabilities(struct sender_state *s) {
  1136. BUFFER *wb = buffer_create(100, NULL);
  1137. stream_capabilities_to_string(wb, s->capabilities);
  1138. nd_log_daemon(NDLP_INFO, "STREAM %s [send to %s]: established link with negotiated capabilities: %s",
  1139. rrdhost_hostname(s->host), s->connected_to, buffer_tostring(wb));
  1140. buffer_free(wb);
  1141. }
  1142. STREAM_CAPABILITIES stream_our_capabilities(RRDHOST *host, bool sender) {
  1143. STREAM_CAPABILITIES disabled_capabilities = globally_disabled_capabilities;
  1144. if(host && sender) {
  1145. // we have DATA_WITH_ML capability
  1146. // we should remove the DATA_WITH_ML capability if our database does not have anomaly info
  1147. // this can happen under these conditions: 1. we don't run ML, and 2. we don't receive ML
  1148. netdata_mutex_lock(&host->receiver_lock);
  1149. if(!ml_host_running(host) && !stream_has_capability(host->receiver, STREAM_CAP_DATA_WITH_ML))
  1150. disabled_capabilities |= STREAM_CAP_DATA_WITH_ML;
  1151. netdata_mutex_unlock(&host->receiver_lock);
  1152. if(host->sender)
  1153. disabled_capabilities |= host->sender->disabled_capabilities;
  1154. }
  1155. return (STREAM_CAP_V1 |
  1156. STREAM_CAP_V2 |
  1157. STREAM_CAP_VN |
  1158. STREAM_CAP_VCAPS |
  1159. STREAM_CAP_HLABELS |
  1160. STREAM_CAP_CLAIM |
  1161. STREAM_CAP_CLABELS |
  1162. STREAM_CAP_FUNCTIONS |
  1163. STREAM_CAP_REPLICATION |
  1164. STREAM_CAP_BINARY |
  1165. STREAM_CAP_INTERPOLATED |
  1166. STREAM_CAP_SLOTS |
  1167. STREAM_CAP_COMPRESSIONS_AVAILABLE |
  1168. #ifdef NETDATA_TEST_DYNCFG
  1169. STREAM_CAP_DYNCFG |
  1170. #endif
  1171. STREAM_CAP_IEEE754 |
  1172. STREAM_CAP_DATA_WITH_ML |
  1173. 0) & ~disabled_capabilities;
  1174. }
  1175. STREAM_CAPABILITIES convert_stream_version_to_capabilities(int32_t version, RRDHOST *host, bool sender) {
  1176. STREAM_CAPABILITIES caps = 0;
  1177. if(version <= 1) caps = STREAM_CAP_V1;
  1178. else if(version < STREAM_OLD_VERSION_CLAIM) caps = STREAM_CAP_V2 | STREAM_CAP_HLABELS;
  1179. else if(version <= STREAM_OLD_VERSION_CLAIM) caps = STREAM_CAP_VN | STREAM_CAP_HLABELS | STREAM_CAP_CLAIM;
  1180. else if(version <= STREAM_OLD_VERSION_CLABELS) caps = STREAM_CAP_VN | STREAM_CAP_HLABELS | STREAM_CAP_CLAIM | STREAM_CAP_CLABELS;
  1181. else if(version <= STREAM_OLD_VERSION_LZ4) caps = STREAM_CAP_VN | STREAM_CAP_HLABELS | STREAM_CAP_CLAIM | STREAM_CAP_CLABELS | STREAM_CAP_LZ4_AVAILABLE;
  1182. else caps = version;
  1183. if(caps & STREAM_CAP_VCAPS)
  1184. caps &= ~(STREAM_CAP_V1|STREAM_CAP_V2|STREAM_CAP_VN);
  1185. if(caps & STREAM_CAP_VN)
  1186. caps &= ~(STREAM_CAP_V1|STREAM_CAP_V2);
  1187. if(caps & STREAM_CAP_V2)
  1188. caps &= ~(STREAM_CAP_V1);
  1189. STREAM_CAPABILITIES common_caps = caps & stream_our_capabilities(host, sender);
  1190. if(!(common_caps & STREAM_CAP_INTERPOLATED))
  1191. // DATA WITH ML requires INTERPOLATED
  1192. common_caps &= ~STREAM_CAP_DATA_WITH_ML;
  1193. return common_caps;
  1194. }
  1195. int32_t stream_capabilities_to_vn(uint32_t caps) {
  1196. if(caps & STREAM_CAP_LZ4) return STREAM_OLD_VERSION_LZ4;
  1197. if(caps & STREAM_CAP_CLABELS) return STREAM_OLD_VERSION_CLABELS;
  1198. return STREAM_OLD_VERSION_CLAIM; // if(caps & STREAM_CAP_CLAIM)
  1199. }