rrdpush.c 49 KB

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