rrdpush.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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 BUFFER in RRDHOST
  12. * the sender thread is signalled via a pipe (also 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. #define STREAMING_PROTOCOL_VERSION "1.1"
  26. #define START_STREAMING_PROMPT "Hit me baby, push them over..."
  27. #define START_STREAMING_PROMPT_V2 "Hit me baby, push them over and bring the host labels..."
  28. #define START_STREAMING_PROMPT_VN "Hit me baby, push them over with the version="
  29. typedef enum {
  30. RRDPUSH_MULTIPLE_CONNECTIONS_ALLOW,
  31. RRDPUSH_MULTIPLE_CONNECTIONS_DENY_NEW
  32. } RRDPUSH_MULTIPLE_CONNECTIONS_STRATEGY;
  33. typedef struct {
  34. char *os_name;
  35. char *os_id;
  36. char *os_version;
  37. char *kernel_name;
  38. char *kernel_version;
  39. } stream_encoded_t;
  40. static struct config stream_config = {
  41. .first_section = NULL,
  42. .last_section = NULL,
  43. .mutex = NETDATA_MUTEX_INITIALIZER,
  44. .index = {
  45. .avl_tree = {
  46. .root = NULL,
  47. .compar = appconfig_section_compare
  48. },
  49. .rwlock = AVL_LOCK_INITIALIZER
  50. }
  51. };
  52. unsigned int default_rrdpush_enabled = 0;
  53. char *default_rrdpush_destination = NULL;
  54. char *default_rrdpush_api_key = NULL;
  55. char *default_rrdpush_send_charts_matching = NULL;
  56. #ifdef ENABLE_HTTPS
  57. int netdata_use_ssl_on_stream = NETDATA_SSL_OPTIONAL;
  58. char *netdata_ssl_ca_path = NULL;
  59. char *netdata_ssl_ca_file = NULL;
  60. #endif
  61. static void load_stream_conf() {
  62. errno = 0;
  63. char *filename = strdupz_path_subpath(netdata_configured_user_config_dir, "stream.conf");
  64. if(!appconfig_load(&stream_config, filename, 0, NULL)) {
  65. info("CONFIG: cannot load user config '%s'. Will try stock config.", filename);
  66. freez(filename);
  67. filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "stream.conf");
  68. if(!appconfig_load(&stream_config, filename, 0, NULL))
  69. info("CONFIG: cannot load stock config '%s'. Running with internal defaults.", filename);
  70. }
  71. freez(filename);
  72. }
  73. int rrdpush_init() {
  74. // --------------------------------------------------------------------
  75. // load stream.conf
  76. load_stream_conf();
  77. default_rrdpush_enabled = (unsigned int)appconfig_get_boolean(&stream_config, CONFIG_SECTION_STREAM, "enabled", default_rrdpush_enabled);
  78. default_rrdpush_destination = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "destination", "");
  79. default_rrdpush_api_key = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "api key", "");
  80. default_rrdpush_send_charts_matching = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "send charts matching", "*");
  81. rrdhost_free_orphan_time = config_get_number(CONFIG_SECTION_GLOBAL, "cleanup orphan hosts after seconds", rrdhost_free_orphan_time);
  82. if(default_rrdpush_enabled && (!default_rrdpush_destination || !*default_rrdpush_destination || !default_rrdpush_api_key || !*default_rrdpush_api_key)) {
  83. error("STREAM [send]: cannot enable sending thread - information is missing.");
  84. default_rrdpush_enabled = 0;
  85. }
  86. #ifdef ENABLE_HTTPS
  87. if (netdata_use_ssl_on_stream == NETDATA_SSL_OPTIONAL) {
  88. if (default_rrdpush_destination){
  89. char *test = strstr(default_rrdpush_destination,":SSL");
  90. if(test){
  91. *test = 0X00;
  92. netdata_use_ssl_on_stream = NETDATA_SSL_FORCE;
  93. }
  94. }
  95. }
  96. char *invalid_certificate = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "ssl skip certificate verification", "no");
  97. if ( !strcmp(invalid_certificate,"yes")){
  98. if (netdata_validate_server == NETDATA_SSL_VALID_CERTIFICATE){
  99. info("Netdata is configured to accept invalid SSL certificate.");
  100. netdata_validate_server = NETDATA_SSL_INVALID_CERTIFICATE;
  101. }
  102. }
  103. netdata_ssl_ca_path = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "CApath", "/etc/ssl/certs/");
  104. netdata_ssl_ca_file = appconfig_get(&stream_config, CONFIG_SECTION_STREAM, "CAfile", "/etc/ssl/certs/certs.pem");
  105. #endif
  106. return default_rrdpush_enabled;
  107. }
  108. #define CONNECTED_TO_SIZE 100
  109. // data collection happens from multiple threads
  110. // each of these threads calls rrdset_done()
  111. // which in turn calls rrdset_done_push()
  112. // which uses this pipe to notify the streaming thread
  113. // that there are more data ready to be sent
  114. #define PIPE_READ 0
  115. #define PIPE_WRITE 1
  116. // to have the remote netdata re-sync the charts
  117. // to its current clock, we send for this many
  118. // iterations a BEGIN line without microseconds
  119. // this is for the first iterations of each chart
  120. unsigned int remote_clock_resync_iterations = 60;
  121. #define rrdpush_buffer_lock(host) netdata_mutex_lock(&((host)->rrdpush_sender_buffer_mutex))
  122. #define rrdpush_buffer_unlock(host) netdata_mutex_unlock(&((host)->rrdpush_sender_buffer_mutex))
  123. static inline int should_send_chart_matching(RRDSET *st) {
  124. if(unlikely(!rrdset_flag_check(st, RRDSET_FLAG_ENABLED))) {
  125. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND);
  126. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  127. }
  128. else if(!rrdset_flag_check(st, RRDSET_FLAG_UPSTREAM_SEND|RRDSET_FLAG_UPSTREAM_IGNORE)) {
  129. RRDHOST *host = st->rrdhost;
  130. if(simple_pattern_matches(host->rrdpush_send_charts_matching, st->id) ||
  131. simple_pattern_matches(host->rrdpush_send_charts_matching, st->name)) {
  132. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  133. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_SEND);
  134. }
  135. else {
  136. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_SEND);
  137. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_IGNORE);
  138. }
  139. }
  140. return(rrdset_flag_check(st, RRDSET_FLAG_UPSTREAM_SEND));
  141. }
  142. int configured_as_master() {
  143. struct section *section = NULL;
  144. int is_master = 0;
  145. appconfig_wrlock(&stream_config);
  146. for (section = stream_config.first_section; section; section = section->next) {
  147. uuid_t uuid;
  148. if (uuid_parse(section->name, uuid) != -1 &&
  149. appconfig_get_boolean_by_section(section, "enabled", 0)) {
  150. is_master = 1;
  151. break;
  152. }
  153. }
  154. appconfig_unlock(&stream_config);
  155. return is_master;
  156. }
  157. // checks if the current chart definition has been sent
  158. static inline int need_to_send_chart_definition(RRDSET *st) {
  159. rrdset_check_rdlock(st);
  160. if(unlikely(!(rrdset_flag_check(st, RRDSET_FLAG_UPSTREAM_EXPOSED))))
  161. return 1;
  162. RRDDIM *rd;
  163. rrddim_foreach_read(rd, st) {
  164. if(unlikely(!rd->exposed)) {
  165. #ifdef NETDATA_INTERNAL_CHECKS
  166. info("host '%s', chart '%s', dimension '%s' flag 'exposed' triggered chart refresh to upstream", st->rrdhost->hostname, st->id, rd->id);
  167. #endif
  168. return 1;
  169. }
  170. }
  171. return 0;
  172. }
  173. // sends the current chart definition
  174. static inline void rrdpush_send_chart_definition_nolock(RRDSET *st) {
  175. RRDHOST *host = st->rrdhost;
  176. rrdset_flag_set(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  177. // properly set the name for the remote end to parse it
  178. char *name = "";
  179. if(likely(st->name)) {
  180. if(unlikely(strcmp(st->id, st->name))) {
  181. // they differ
  182. name = strchr(st->name, '.');
  183. if(name)
  184. name++;
  185. else
  186. name = "";
  187. }
  188. }
  189. // info("CHART '%s' '%s'", st->id, name);
  190. // send the chart
  191. buffer_sprintf(
  192. host->rrdpush_sender_buffer
  193. , "CHART \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" %ld %d \"%s %s %s %s\" \"%s\" \"%s\"\n"
  194. , st->id
  195. , name
  196. , st->title
  197. , st->units
  198. , st->family
  199. , st->context
  200. , rrdset_type_name(st->chart_type)
  201. , st->priority
  202. , st->update_every
  203. , rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE)?"obsolete":""
  204. , rrdset_flag_check(st, RRDSET_FLAG_DETAIL)?"detail":""
  205. , rrdset_flag_check(st, RRDSET_FLAG_STORE_FIRST)?"store_first":""
  206. , rrdset_flag_check(st, RRDSET_FLAG_HIDDEN)?"hidden":""
  207. , (st->plugin_name)?st->plugin_name:""
  208. , (st->module_name)?st->module_name:""
  209. );
  210. // send the dimensions
  211. RRDDIM *rd;
  212. rrddim_foreach_read(rd, st) {
  213. buffer_sprintf(
  214. host->rrdpush_sender_buffer
  215. , "DIMENSION \"%s\" \"%s\" \"%s\" " COLLECTED_NUMBER_FORMAT " " COLLECTED_NUMBER_FORMAT " \"%s %s %s\"\n"
  216. , rd->id
  217. , rd->name
  218. , rrd_algorithm_name(rd->algorithm)
  219. , rd->multiplier
  220. , rd->divisor
  221. , rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE)?"obsolete":""
  222. , rrddim_flag_check(rd, RRDDIM_FLAG_HIDDEN)?"hidden":""
  223. , rrddim_flag_check(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS)?"noreset":""
  224. );
  225. rd->exposed = 1;
  226. }
  227. // send the chart local custom variables
  228. RRDSETVAR *rs;
  229. for(rs = st->variables; rs ;rs = rs->next) {
  230. if(unlikely(rs->type == RRDVAR_TYPE_CALCULATED && rs->options & RRDVAR_OPTION_CUSTOM_CHART_VAR)) {
  231. calculated_number *value = (calculated_number *) rs->value;
  232. buffer_sprintf(
  233. host->rrdpush_sender_buffer
  234. , "VARIABLE CHART %s = " CALCULATED_NUMBER_FORMAT "\n"
  235. , rs->variable
  236. , *value
  237. );
  238. }
  239. }
  240. st->upstream_resync_time = st->last_collected_time.tv_sec + (remote_clock_resync_iterations * st->update_every);
  241. }
  242. // sends the current chart dimensions
  243. static inline void rrdpush_send_chart_metrics_nolock(RRDSET *st) {
  244. RRDHOST *host = st->rrdhost;
  245. buffer_sprintf(host->rrdpush_sender_buffer, "BEGIN \"%s\" %llu\n", st->id, (st->last_collected_time.tv_sec > st->upstream_resync_time)?st->usec_since_last_update:0);
  246. RRDDIM *rd;
  247. rrddim_foreach_read(rd, st) {
  248. if(rd->updated && rd->exposed)
  249. buffer_sprintf(host->rrdpush_sender_buffer
  250. , "SET \"%s\" = " COLLECTED_NUMBER_FORMAT "\n"
  251. , rd->id
  252. , rd->collected_value
  253. );
  254. }
  255. buffer_strcat(host->rrdpush_sender_buffer, "END\n");
  256. }
  257. static void rrdpush_sender_thread_spawn(RRDHOST *host);
  258. void rrdset_push_chart_definition_now(RRDSET *st) {
  259. RRDHOST *host = st->rrdhost;
  260. if(unlikely(!host->rrdpush_send_enabled || !should_send_chart_matching(st)))
  261. return;
  262. rrdset_rdlock(st);
  263. rrdpush_buffer_lock(host);
  264. rrdpush_send_chart_definition_nolock(st);
  265. rrdpush_buffer_unlock(host);
  266. rrdset_unlock(st);
  267. }
  268. void rrdset_done_push(RRDSET *st) {
  269. if(unlikely(!should_send_chart_matching(st)))
  270. return;
  271. RRDHOST *host = st->rrdhost;
  272. rrdpush_buffer_lock(host);
  273. if(unlikely(host->rrdpush_send_enabled && !host->rrdpush_sender_spawn))
  274. rrdpush_sender_thread_spawn(host);
  275. if(unlikely(!host->rrdpush_sender_buffer || !host->rrdpush_sender_connected)) {
  276. if(unlikely(!host->rrdpush_sender_error_shown))
  277. error("STREAM %s [send]: not ready - discarding collected metrics.", host->hostname);
  278. host->rrdpush_sender_error_shown = 1;
  279. rrdpush_buffer_unlock(host);
  280. return;
  281. }
  282. else if(unlikely(host->rrdpush_sender_error_shown)) {
  283. info("STREAM %s [send]: sending metrics...", host->hostname);
  284. host->rrdpush_sender_error_shown = 0;
  285. }
  286. if(need_to_send_chart_definition(st))
  287. rrdpush_send_chart_definition_nolock(st);
  288. rrdpush_send_chart_metrics_nolock(st);
  289. // signal the sender there are more data
  290. if(host->rrdpush_sender_pipe[PIPE_WRITE] != -1 && write(host->rrdpush_sender_pipe[PIPE_WRITE], " ", 1) == -1)
  291. error("STREAM %s [send]: cannot write to internal pipe", host->hostname);
  292. rrdpush_buffer_unlock(host);
  293. }
  294. // labels
  295. void rrdpush_send_labels(RRDHOST *host) {
  296. if (!host->labels || !(host->labels_flag & LABEL_FLAG_UPDATE_STREAM) || (host->labels_flag & LABEL_FLAG_STOP_STREAM))
  297. return;
  298. rrdpush_buffer_lock(host);
  299. rrdhost_rdlock(host);
  300. netdata_rwlock_rdlock(&host->labels_rwlock);
  301. struct label *labels = host->labels;
  302. while(labels) {
  303. buffer_sprintf(host->rrdpush_sender_buffer
  304. , "LABEL \"%s\" = %d %s\n"
  305. , labels->key
  306. , (int)labels->label_source
  307. , labels->value);
  308. labels = labels->next;
  309. }
  310. buffer_sprintf(host->rrdpush_sender_buffer
  311. , "OVERWRITE %s\n", "labels");
  312. netdata_rwlock_unlock(&host->labels_rwlock);
  313. rrdhost_unlock(host);
  314. if(host->rrdpush_sender_pipe[PIPE_WRITE] != -1 && write(host->rrdpush_sender_pipe[PIPE_WRITE], " ", 1) == -1)
  315. error("STREAM %s [send]: cannot write to internal pipe", host->hostname);
  316. rrdpush_buffer_unlock(host);
  317. host->labels_flag &= ~LABEL_FLAG_UPDATE_STREAM;
  318. }
  319. // ----------------------------------------------------------------------------
  320. // rrdpush sender thread
  321. static inline void rrdpush_sender_add_host_variable_to_buffer_nolock(RRDHOST *host, RRDVAR *rv) {
  322. calculated_number *value = (calculated_number *)rv->value;
  323. buffer_sprintf(
  324. host->rrdpush_sender_buffer
  325. , "VARIABLE HOST %s = " CALCULATED_NUMBER_FORMAT "\n"
  326. , rv->name
  327. , *value
  328. );
  329. debug(D_STREAM, "RRDVAR pushed HOST VARIABLE %s = " CALCULATED_NUMBER_FORMAT, rv->name, *value);
  330. }
  331. void rrdpush_sender_send_this_host_variable_now(RRDHOST *host, RRDVAR *rv) {
  332. if(host->rrdpush_send_enabled && host->rrdpush_sender_spawn && host->rrdpush_sender_connected) {
  333. rrdpush_buffer_lock(host);
  334. rrdpush_sender_add_host_variable_to_buffer_nolock(host, rv);
  335. rrdpush_buffer_unlock(host);
  336. }
  337. }
  338. static int rrdpush_sender_thread_custom_host_variables_callback(void *rrdvar_ptr, void *host_ptr) {
  339. RRDVAR *rv = (RRDVAR *)rrdvar_ptr;
  340. RRDHOST *host = (RRDHOST *)host_ptr;
  341. if(unlikely(rv->options & RRDVAR_OPTION_CUSTOM_HOST_VAR && rv->type == RRDVAR_TYPE_CALCULATED)) {
  342. rrdpush_sender_add_host_variable_to_buffer_nolock(host, rv);
  343. // return 1, so that the traversal will return the number of variables sent
  344. return 1;
  345. }
  346. // returning a negative number will break the traversal
  347. return 0;
  348. }
  349. static void rrdpush_sender_thread_send_custom_host_variables(RRDHOST *host) {
  350. int ret = rrdvar_callback_for_all_host_variables(host, rrdpush_sender_thread_custom_host_variables_callback, host);
  351. (void)ret;
  352. debug(D_STREAM, "RRDVAR sent %d VARIABLES", ret);
  353. }
  354. // resets all the chart, so that their definitions
  355. // will be resent to the central netdata
  356. static void rrdpush_sender_thread_reset_all_charts(RRDHOST *host) {
  357. rrdhost_rdlock(host);
  358. RRDSET *st;
  359. rrdset_foreach_read(st, host) {
  360. rrdset_flag_clear(st, RRDSET_FLAG_UPSTREAM_EXPOSED);
  361. st->upstream_resync_time = 0;
  362. rrdset_rdlock(st);
  363. RRDDIM *rd;
  364. rrddim_foreach_read(rd, st)
  365. rd->exposed = 0;
  366. rrdset_unlock(st);
  367. }
  368. rrdhost_unlock(host);
  369. }
  370. static inline void rrdpush_sender_thread_data_flush(RRDHOST *host) {
  371. rrdpush_buffer_lock(host);
  372. if(buffer_strlen(host->rrdpush_sender_buffer))
  373. error("STREAM %s [send]: discarding %zu bytes of metrics already in the buffer.", host->hostname, buffer_strlen(host->rrdpush_sender_buffer));
  374. buffer_flush(host->rrdpush_sender_buffer);
  375. rrdpush_sender_thread_reset_all_charts(host);
  376. rrdpush_sender_thread_send_custom_host_variables(host);
  377. rrdpush_buffer_unlock(host);
  378. }
  379. void rrdpush_sender_thread_stop(RRDHOST *host) {
  380. rrdpush_buffer_lock(host);
  381. rrdhost_wrlock(host);
  382. netdata_thread_t thr = 0;
  383. if(host->rrdpush_sender_spawn) {
  384. info("STREAM %s [send]: signaling sending thread to stop...", host->hostname);
  385. // signal the thread that we want to join it
  386. host->rrdpush_sender_join = 1;
  387. // copy the thread id, so that we will be waiting for the right one
  388. // even if a new one has been spawn
  389. thr = host->rrdpush_sender_thread;
  390. // signal it to cancel
  391. netdata_thread_cancel(host->rrdpush_sender_thread);
  392. }
  393. rrdhost_unlock(host);
  394. rrdpush_buffer_unlock(host);
  395. if(thr != 0) {
  396. info("STREAM %s [send]: waiting for the sending thread to stop...", host->hostname);
  397. void *result;
  398. netdata_thread_join(thr, &result);
  399. info("STREAM %s [send]: sending thread has exited.", host->hostname);
  400. }
  401. }
  402. static inline void rrdpush_sender_thread_close_socket(RRDHOST *host) {
  403. host->rrdpush_sender_connected = 0;
  404. if(host->rrdpush_sender_socket != -1) {
  405. close(host->rrdpush_sender_socket);
  406. host->rrdpush_sender_socket = -1;
  407. }
  408. }
  409. static inline void rrdpush_set_flags_to_newest_stream(RRDHOST *host) {
  410. host->labels_flag |= LABEL_FLAG_UPDATE_STREAM;
  411. host->labels_flag &= ~LABEL_FLAG_STOP_STREAM;
  412. }
  413. void rrdpush_encode_variable(stream_encoded_t *se, RRDHOST *host)
  414. {
  415. se->os_name = (host->system_info->host_os_name)?url_encode(host->system_info->host_os_name):"";
  416. se->os_id = (host->system_info->host_os_id)?url_encode(host->system_info->host_os_id):"";
  417. se->os_version = (host->system_info->host_os_version)?url_encode(host->system_info->host_os_version):"";
  418. se->kernel_name = (host->system_info->kernel_name)?url_encode(host->system_info->kernel_name):"";
  419. se->kernel_version = (host->system_info->kernel_version)?url_encode(host->system_info->kernel_version):"";
  420. }
  421. void rrdpush_clean_encoded(stream_encoded_t *se)
  422. {
  423. if (se->os_name)
  424. freez(se->os_name);
  425. if (se->os_id)
  426. freez(se->os_id);
  427. if (se->os_version)
  428. freez(se->os_version);
  429. if (se->kernel_name)
  430. freez(se->kernel_name);
  431. if (se->kernel_version)
  432. freez(se->kernel_version);
  433. }
  434. //called from client side
  435. static int rrdpush_sender_thread_connect_to_master(RRDHOST *host, int default_port, int timeout, size_t *reconnects_counter, char *connected_to, size_t connected_to_size) {
  436. struct timeval tv = {
  437. .tv_sec = timeout,
  438. .tv_usec = 0
  439. };
  440. // make sure the socket is closed
  441. rrdpush_sender_thread_close_socket(host);
  442. debug(D_STREAM, "STREAM: Attempting to connect...");
  443. info("STREAM %s [send to %s]: connecting...", host->hostname, host->rrdpush_send_destination);
  444. host->rrdpush_sender_socket = connect_to_one_of(
  445. host->rrdpush_send_destination
  446. , default_port
  447. , &tv
  448. , reconnects_counter
  449. , connected_to
  450. , connected_to_size
  451. );
  452. if(unlikely(host->rrdpush_sender_socket == -1)) {
  453. error("STREAM %s [send to %s]: failed to connect", host->hostname, host->rrdpush_send_destination);
  454. return 0;
  455. }
  456. info("STREAM %s [send to %s]: initializing communication...", host->hostname, connected_to);
  457. #ifdef ENABLE_HTTPS
  458. if( netdata_client_ctx ){
  459. host->ssl.flags = NETDATA_SSL_START;
  460. if (!host->ssl.conn){
  461. host->ssl.conn = SSL_new(netdata_client_ctx);
  462. if(!host->ssl.conn){
  463. error("Failed to allocate SSL structure.");
  464. host->ssl.flags = NETDATA_SSL_NO_HANDSHAKE;
  465. }
  466. }
  467. else{
  468. SSL_clear(host->ssl.conn);
  469. }
  470. if (host->ssl.conn)
  471. {
  472. if (SSL_set_fd(host->ssl.conn, host->rrdpush_sender_socket) != 1) {
  473. error("Failed to set the socket to the SSL on socket fd %d.", host->rrdpush_sender_socket);
  474. host->ssl.flags = NETDATA_SSL_NO_HANDSHAKE;
  475. } else{
  476. host->ssl.flags = NETDATA_SSL_HANDSHAKE_COMPLETE;
  477. }
  478. }
  479. }
  480. else {
  481. host->ssl.flags = NETDATA_SSL_NO_HANDSHAKE;
  482. }
  483. #endif
  484. /* TODO: During the implementation of #7265 switch the set of variables to HOST_* and CONTAINER_* if the
  485. version negotiation resulted in a high enough version.
  486. */
  487. stream_encoded_t se;
  488. rrdpush_encode_variable(&se, host);
  489. #define HTTP_HEADER_SIZE 8192
  490. char http[HTTP_HEADER_SIZE + 1];
  491. int eol = snprintfz(http, HTTP_HEADER_SIZE,
  492. "STREAM key=%s&hostname=%s&registry_hostname=%s&machine_guid=%s&update_every=%d&os=%s&timezone=%s&tags=%s&ver=%u"
  493. "&NETDATA_SYSTEM_OS_NAME=%s"
  494. "&NETDATA_SYSTEM_OS_ID=%s"
  495. "&NETDATA_SYSTEM_OS_ID_LIKE=%s"
  496. "&NETDATA_SYSTEM_OS_VERSION=%s"
  497. "&NETDATA_SYSTEM_OS_VERSION_ID=%s"
  498. "&NETDATA_SYSTEM_OS_DETECTION=%s"
  499. "&NETDATA_SYSTEM_KERNEL_NAME=%s"
  500. "&NETDATA_SYSTEM_KERNEL_VERSION=%s"
  501. "&NETDATA_SYSTEM_ARCHITECTURE=%s"
  502. "&NETDATA_SYSTEM_VIRTUALIZATION=%s"
  503. "&NETDATA_SYSTEM_VIRT_DETECTION=%s"
  504. "&NETDATA_SYSTEM_CONTAINER=%s"
  505. "&NETDATA_SYSTEM_CONTAINER_DETECTION=%s"
  506. "&NETDATA_CONTAINER_OS_NAME=%s"
  507. "&NETDATA_CONTAINER_OS_ID=%s"
  508. "&NETDATA_CONTAINER_OS_ID_LIKE=%s"
  509. "&NETDATA_CONTAINER_OS_VERSION=%s"
  510. "&NETDATA_CONTAINER_OS_VERSION_ID=%s"
  511. "&NETDATA_CONTAINER_OS_DETECTION=%s"
  512. "&NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT=%s"
  513. "&NETDATA_SYSTEM_CPU_FREQ=%s"
  514. "&NETDATA_SYSTEM_TOTAL_RAM=%s"
  515. "&NETDATA_SYSTEM_TOTAL_DISK_SIZE=%s"
  516. "&NETDATA_PROTOCOL_VERSION=%s"
  517. " HTTP/1.1\r\n"
  518. "User-Agent: %s/%s\r\n"
  519. "Accept: */*\r\n\r\n"
  520. , host->rrdpush_send_api_key
  521. , host->hostname
  522. , host->registry_hostname
  523. , host->machine_guid
  524. , default_rrd_update_every
  525. , host->os
  526. , host->timezone
  527. , (host->tags) ? host->tags : ""
  528. , STREAMING_PROTOCOL_CURRENT_VERSION
  529. , se.os_name
  530. , se.os_id
  531. , (host->system_info->host_os_id_like) ? host->system_info->host_os_id_like : ""
  532. , se.os_version
  533. , (host->system_info->host_os_version_id) ? host->system_info->host_os_version_id : ""
  534. , (host->system_info->host_os_detection) ? host->system_info->host_os_detection : ""
  535. , se.kernel_name
  536. , se.kernel_version
  537. , (host->system_info->architecture) ? host->system_info->architecture : ""
  538. , (host->system_info->virtualization) ? host->system_info->virtualization : ""
  539. , (host->system_info->virt_detection) ? host->system_info->virt_detection : ""
  540. , (host->system_info->container) ? host->system_info->container : ""
  541. , (host->system_info->container_detection) ? host->system_info->container_detection : ""
  542. , (host->system_info->container_os_name) ? host->system_info->container_os_name : ""
  543. , (host->system_info->container_os_id) ? host->system_info->container_os_id : ""
  544. , (host->system_info->container_os_id_like) ? host->system_info->container_os_id_like : ""
  545. , (host->system_info->container_os_version) ? host->system_info->container_os_version : ""
  546. , (host->system_info->container_os_version_id) ? host->system_info->container_os_version_id : ""
  547. , (host->system_info->container_os_detection) ? host->system_info->container_os_detection : ""
  548. , (host->system_info->host_cores) ? host->system_info->host_cores : ""
  549. , (host->system_info->host_cpu_freq) ? host->system_info->host_cpu_freq : ""
  550. , (host->system_info->host_ram_total) ? host->system_info->host_ram_total : ""
  551. , (host->system_info->host_disk_space) ? host->system_info->host_disk_space : ""
  552. , STREAMING_PROTOCOL_VERSION
  553. , host->program_name
  554. , host->program_version
  555. );
  556. http[eol] = 0x00;
  557. rrdpush_clean_encoded(&se);
  558. #ifdef ENABLE_HTTPS
  559. if (!host->ssl.flags) {
  560. ERR_clear_error();
  561. SSL_set_connect_state(host->ssl.conn);
  562. int err = SSL_connect(host->ssl.conn);
  563. if (err != 1){
  564. err = SSL_get_error(host->ssl.conn, err);
  565. error("SSL cannot connect with the server: %s ",ERR_error_string((long)SSL_get_error(host->ssl.conn,err),NULL));
  566. if (netdata_use_ssl_on_stream == NETDATA_SSL_FORCE) {
  567. rrdpush_sender_thread_close_socket(host);
  568. return 0;
  569. }else {
  570. host->ssl.flags = NETDATA_SSL_NO_HANDSHAKE;
  571. }
  572. }
  573. else {
  574. if (netdata_use_ssl_on_stream == NETDATA_SSL_FORCE) {
  575. if (netdata_validate_server == NETDATA_SSL_VALID_CERTIFICATE) {
  576. if ( security_test_certificate(host->ssl.conn)) {
  577. error("Closing the stream connection, because the server SSL certificate is not valid.");
  578. rrdpush_sender_thread_close_socket(host);
  579. return 0;
  580. }
  581. }
  582. }
  583. }
  584. }
  585. if(send_timeout(&host->ssl,host->rrdpush_sender_socket, http, strlen(http), 0, timeout) == -1) {
  586. #else
  587. if(send_timeout(host->rrdpush_sender_socket, http, strlen(http), 0, timeout) == -1) {
  588. #endif
  589. error("STREAM %s [send to %s]: failed to send HTTP header to remote netdata.", host->hostname, connected_to);
  590. rrdpush_sender_thread_close_socket(host);
  591. return 0;
  592. }
  593. info("STREAM %s [send to %s]: waiting response from remote netdata...", host->hostname, connected_to);
  594. ssize_t received;
  595. #ifdef ENABLE_HTTPS
  596. received = recv_timeout(&host->ssl,host->rrdpush_sender_socket, http, HTTP_HEADER_SIZE, 0, timeout);
  597. if(received == -1) {
  598. #else
  599. received = recv_timeout(host->rrdpush_sender_socket, http, HTTP_HEADER_SIZE, 0, timeout);
  600. if(received == -1) {
  601. #endif
  602. error("STREAM %s [send to %s]: remote netdata does not respond.", host->hostname, connected_to);
  603. rrdpush_sender_thread_close_socket(host);
  604. return 0;
  605. }
  606. http[received] = '\0';
  607. int answer = -1;
  608. char *version_start = strchr(http, '=');
  609. uint32_t version;
  610. if(version_start) {
  611. version_start++;
  612. version = (uint32_t)strtol(version_start, NULL, 10);
  613. answer = memcmp(http, START_STREAMING_PROMPT_VN, (size_t)(version_start - http));
  614. if(!answer) {
  615. rrdpush_set_flags_to_newest_stream(host);
  616. host->stream_version = version;
  617. }
  618. } else {
  619. answer = memcmp(http, START_STREAMING_PROMPT_V2, strlen(START_STREAMING_PROMPT_V2));
  620. if(!answer) {
  621. version = 1;
  622. rrdpush_set_flags_to_newest_stream(host);
  623. }
  624. else {
  625. answer = memcmp(http, START_STREAMING_PROMPT, strlen(START_STREAMING_PROMPT));
  626. if(!answer) {
  627. version = 0;
  628. host->labels_flag |= LABEL_FLAG_STOP_STREAM;
  629. host->labels_flag &= ~LABEL_FLAG_UPDATE_STREAM;
  630. }
  631. }
  632. }
  633. if(answer != 0) {
  634. error("STREAM %s [send to %s]: server is not replying properly (is it a netdata?).", host->hostname, connected_to);
  635. rrdpush_sender_thread_close_socket(host);
  636. return 0;
  637. }
  638. info("STREAM %s [send to %s]: established communication with a master using protocol version %u - ready to send metrics..."
  639. , host->hostname
  640. , connected_to
  641. , version);
  642. if(sock_setnonblock(host->rrdpush_sender_socket) < 0)
  643. error("STREAM %s [send to %s]: cannot set non-blocking mode for socket.", host->hostname, connected_to);
  644. if(sock_enlarge_out(host->rrdpush_sender_socket) < 0)
  645. error("STREAM %s [send to %s]: cannot enlarge the socket buffer.", host->hostname, connected_to);
  646. debug(D_STREAM, "STREAM: Connected on fd %d...", host->rrdpush_sender_socket);
  647. return 1;
  648. }
  649. static void rrdpush_sender_thread_cleanup_callback(void *ptr) {
  650. RRDHOST *host = (RRDHOST *)ptr;
  651. rrdpush_buffer_lock(host);
  652. rrdhost_wrlock(host);
  653. info("STREAM %s [send]: sending thread cleans up...", host->hostname);
  654. rrdpush_sender_thread_close_socket(host);
  655. // close the pipe
  656. if(host->rrdpush_sender_pipe[PIPE_READ] != -1) {
  657. close(host->rrdpush_sender_pipe[PIPE_READ]);
  658. host->rrdpush_sender_pipe[PIPE_READ] = -1;
  659. }
  660. if(host->rrdpush_sender_pipe[PIPE_WRITE] != -1) {
  661. close(host->rrdpush_sender_pipe[PIPE_WRITE]);
  662. host->rrdpush_sender_pipe[PIPE_WRITE] = -1;
  663. }
  664. buffer_free(host->rrdpush_sender_buffer);
  665. host->rrdpush_sender_buffer = NULL;
  666. if(!host->rrdpush_sender_join) {
  667. info("STREAM %s [send]: sending thread detaches itself.", host->hostname);
  668. netdata_thread_detach(netdata_thread_self());
  669. }
  670. host->rrdpush_sender_spawn = 0;
  671. info("STREAM %s [send]: sending thread now exits.", host->hostname);
  672. rrdhost_unlock(host);
  673. rrdpush_buffer_unlock(host);
  674. }
  675. void *rrdpush_sender_thread(void *ptr) {
  676. RRDHOST *host = (RRDHOST *)ptr;
  677. if(!host->rrdpush_send_enabled || !host->rrdpush_send_destination || !*host->rrdpush_send_destination || !host->rrdpush_send_api_key || !*host->rrdpush_send_api_key) {
  678. error("STREAM %s [send]: thread created (task id %d), but host has streaming disabled.", host->hostname, gettid());
  679. return NULL;
  680. }
  681. #ifdef ENABLE_HTTPS
  682. if (netdata_use_ssl_on_stream & NETDATA_SSL_FORCE ){
  683. security_start_ssl(NETDATA_SSL_CONTEXT_STREAMING);
  684. security_location_for_context(netdata_client_ctx, netdata_ssl_ca_file, netdata_ssl_ca_path);
  685. }
  686. #endif
  687. info("STREAM %s [send]: thread created (task id %d)", host->hostname, gettid());
  688. int timeout = (int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "timeout seconds", 60);
  689. int default_port = (int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "default port", 19999);
  690. size_t max_size = (size_t)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "buffer size bytes", 1024 * 1024);
  691. unsigned int reconnect_delay = (unsigned int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "reconnect delay seconds", 5);
  692. remote_clock_resync_iterations = (unsigned int)appconfig_get_number(&stream_config, CONFIG_SECTION_STREAM, "initial clock resync iterations", remote_clock_resync_iterations);
  693. char connected_to[CONNECTED_TO_SIZE + 1] = "";
  694. // initialize rrdpush globals
  695. host->rrdpush_sender_buffer = buffer_create(1);
  696. host->rrdpush_sender_connected = 0;
  697. if(pipe(host->rrdpush_sender_pipe) == -1) fatal("STREAM %s [send]: cannot create required pipe.", host->hostname);
  698. // initialize local variables
  699. size_t begin = 0;
  700. size_t reconnects_counter = 0;
  701. size_t sent_bytes = 0;
  702. size_t sent_bytes_on_this_connection = 0;
  703. size_t send_attempts = 0;
  704. time_t last_sent_t = 0;
  705. struct pollfd fds[2], *ifd, *ofd;
  706. nfds_t fdmax;
  707. ifd = &fds[0];
  708. ofd = &fds[1];
  709. size_t not_connected_loops = 0;
  710. netdata_thread_cleanup_push(rrdpush_sender_thread_cleanup_callback, host);
  711. for(; host->rrdpush_send_enabled && !netdata_exit ;) {
  712. // check for outstanding cancellation requests
  713. netdata_thread_testcancel();
  714. // if we don't have socket open, lets wait a bit
  715. if(unlikely(host->rrdpush_sender_socket == -1)) {
  716. send_attempts = 0;
  717. if(not_connected_loops == 0 && sent_bytes_on_this_connection > 0) {
  718. // fast re-connection on first disconnect
  719. sleep_usec(USEC_PER_MS * 500); // milliseconds
  720. }
  721. else {
  722. // slow re-connection on repeating errors
  723. sleep_usec(USEC_PER_SEC * reconnect_delay); // seconds
  724. }
  725. if(rrdpush_sender_thread_connect_to_master(host, default_port, timeout, &reconnects_counter, connected_to, CONNECTED_TO_SIZE)) {
  726. last_sent_t = now_monotonic_sec();
  727. // reset the buffer, to properly send charts and metrics
  728. rrdpush_sender_thread_data_flush(host);
  729. // send from the beginning
  730. begin = 0;
  731. // make sure the next reconnection will be immediate
  732. not_connected_loops = 0;
  733. // reset the bytes we have sent for this session
  734. sent_bytes_on_this_connection = 0;
  735. // let the data collection threads know we are ready
  736. host->rrdpush_sender_connected = 1;
  737. }
  738. else {
  739. // increase the failed connections counter
  740. not_connected_loops++;
  741. // reset the number of bytes sent
  742. sent_bytes_on_this_connection = 0;
  743. }
  744. // loop through
  745. continue;
  746. }
  747. else if(unlikely(now_monotonic_sec() - last_sent_t > timeout)) {
  748. error("STREAM %s [send to %s]: could not send metrics for %d seconds - closing connection - we have sent %zu bytes on this connection via %zu send attempts.", host->hostname, connected_to, timeout, sent_bytes_on_this_connection, send_attempts);
  749. rrdpush_sender_thread_close_socket(host);
  750. }
  751. ifd->fd = host->rrdpush_sender_pipe[PIPE_READ];
  752. ifd->events = POLLIN;
  753. ifd->revents = 0;
  754. ofd->fd = host->rrdpush_sender_socket;
  755. ofd->revents = 0;
  756. if(ofd->fd != -1 && begin < buffer_strlen(host->rrdpush_sender_buffer)) {
  757. debug(D_STREAM, "STREAM: Requesting data output on streaming socket %d...", ofd->fd);
  758. ofd->events = POLLOUT;
  759. fdmax = 2;
  760. send_attempts++;
  761. }
  762. else {
  763. debug(D_STREAM, "STREAM: Not requesting data output on streaming socket %d (nothing to send now)...", ofd->fd);
  764. ofd->events = 0;
  765. fdmax = 1;
  766. }
  767. debug(D_STREAM, "STREAM: Waiting for poll() events (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_sender_buffer));
  768. if(unlikely(netdata_exit)) break;
  769. int retval = poll(fds, fdmax, 1000);
  770. if(unlikely(netdata_exit)) break;
  771. if(unlikely(retval == -1)) {
  772. debug(D_STREAM, "STREAM: poll() failed (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_sender_buffer));
  773. if(errno == EAGAIN || errno == EINTR) {
  774. debug(D_STREAM, "STREAM: poll() failed with EAGAIN or EINTR...");
  775. }
  776. else {
  777. error("STREAM %s [send to %s]: failed to poll(). Closing socket.", host->hostname, connected_to);
  778. rrdpush_sender_thread_close_socket(host);
  779. }
  780. continue;
  781. }
  782. else if(likely(retval)) {
  783. if (ifd->revents & POLLIN || ifd->revents & POLLPRI) {
  784. debug(D_STREAM, "STREAM: Data added to send buffer (current buffer length %zu bytes)...", buffer_strlen(host->rrdpush_sender_buffer));
  785. char buffer[1000 + 1];
  786. if (read(host->rrdpush_sender_pipe[PIPE_READ], buffer, 1000) == -1)
  787. error("STREAM %s [send to %s]: cannot read from internal pipe.", host->hostname, connected_to);
  788. }
  789. if (ofd->revents & POLLOUT) {
  790. rrdpush_send_labels(host);
  791. if (begin < buffer_strlen(host->rrdpush_sender_buffer)) {
  792. debug(D_STREAM, "STREAM: Sending data (current buffer length %zu bytes, begin = %zu)...", buffer_strlen(host->rrdpush_sender_buffer), begin);
  793. // BEGIN RRDPUSH LOCKED SESSION
  794. // during this session, data collectors
  795. // will not be able to append data to our buffer
  796. // but the socket is in non-blocking mode
  797. // so, we will not block at send()
  798. netdata_thread_disable_cancelability();
  799. debug(D_STREAM, "STREAM: Getting exclusive lock on host...");
  800. rrdpush_buffer_lock(host);
  801. debug(D_STREAM, "STREAM: Sending data, starting from %zu, size %zu...", begin, buffer_strlen(host->rrdpush_sender_buffer));
  802. ssize_t ret;
  803. #ifdef ENABLE_HTTPS
  804. SSL *conn = host->ssl.conn ;
  805. if(conn && !host->ssl.flags) {
  806. ret = SSL_write(conn,&host->rrdpush_sender_buffer->buffer[begin], buffer_strlen(host->rrdpush_sender_buffer) - begin);
  807. } else {
  808. ret = send(host->rrdpush_sender_socket, &host->rrdpush_sender_buffer->buffer[begin], buffer_strlen(host->rrdpush_sender_buffer) - begin, MSG_DONTWAIT);
  809. }
  810. #else
  811. ret = send(host->rrdpush_sender_socket, &host->rrdpush_sender_buffer->buffer[begin], buffer_strlen(host->rrdpush_sender_buffer) - begin, MSG_DONTWAIT);
  812. #endif
  813. if (unlikely(ret == -1)) {
  814. if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) {
  815. debug(D_STREAM, "STREAM: Send failed - closing socket...");
  816. error("STREAM %s [send to %s]: failed to send metrics - closing connection - we have sent %zu bytes on this connection.", host->hostname, connected_to, sent_bytes_on_this_connection);
  817. rrdpush_sender_thread_close_socket(host);
  818. }
  819. else {
  820. debug(D_STREAM, "STREAM: Send failed - will retry...");
  821. }
  822. }
  823. else if (likely(ret > 0)) {
  824. // DEBUG - dump the string to see it
  825. //char c = host->rrdpush_sender_buffer->buffer[begin + ret];
  826. //host->rrdpush_sender_buffer->buffer[begin + ret] = '\0';
  827. //debug(D_STREAM, "STREAM: sent from %zu to %zd:\n%s\n", begin, ret, &host->rrdpush_sender_buffer->buffer[begin]);
  828. //host->rrdpush_sender_buffer->buffer[begin + ret] = c;
  829. sent_bytes_on_this_connection += ret;
  830. sent_bytes += ret;
  831. begin += ret;
  832. if (begin == buffer_strlen(host->rrdpush_sender_buffer)) {
  833. // we send it all
  834. debug(D_STREAM, "STREAM: Sent %zd bytes (the whole buffer)...", ret);
  835. buffer_flush(host->rrdpush_sender_buffer);
  836. begin = 0;
  837. }
  838. else {
  839. debug(D_STREAM, "STREAM: Sent %zd bytes (part of the data buffer)...", ret);
  840. }
  841. last_sent_t = now_monotonic_sec();
  842. }
  843. else {
  844. debug(D_STREAM, "STREAM: send() returned %zd - closing the socket...", ret);
  845. error("STREAM %s [send to %s]: failed to send metrics (send() returned %zd) - closing connection - we have sent %zu bytes on this connection.",
  846. host->hostname, connected_to, ret, sent_bytes_on_this_connection);
  847. rrdpush_sender_thread_close_socket(host);
  848. }
  849. debug(D_STREAM, "STREAM: Releasing exclusive lock on host...");
  850. rrdpush_buffer_unlock(host);
  851. netdata_thread_enable_cancelability();
  852. // END RRDPUSH LOCKED SESSION
  853. }
  854. else {
  855. debug(D_STREAM, "STREAM: we have sent the entire buffer, but we received POLLOUT...");
  856. }
  857. }
  858. if(host->rrdpush_sender_socket != -1) {
  859. char *error = NULL;
  860. if (unlikely(ofd->revents & POLLERR))
  861. error = "socket reports errors (POLLERR)";
  862. else if (unlikely(ofd->revents & POLLHUP))
  863. error = "connection closed by remote end (POLLHUP)";
  864. else if (unlikely(ofd->revents & POLLNVAL))
  865. error = "connection is invalid (POLLNVAL)";
  866. if(unlikely(error)) {
  867. debug(D_STREAM, "STREAM: %s - closing socket...", error);
  868. error("STREAM %s [send to %s]: %s - reopening socket - we have sent %zu bytes on this connection.", host->hostname, connected_to, error, sent_bytes_on_this_connection);
  869. rrdpush_sender_thread_close_socket(host);
  870. }
  871. }
  872. }
  873. else {
  874. debug(D_STREAM, "STREAM: poll() timed out.");
  875. }
  876. // protection from overflow
  877. if(buffer_strlen(host->rrdpush_sender_buffer) > max_size) {
  878. debug(D_STREAM, "STREAM: Buffer is too big (%zu bytes), bigger than the max (%zu) - flushing it...", buffer_strlen(host->rrdpush_sender_buffer), max_size);
  879. errno = 0;
  880. error("STREAM %s [send to %s]: too many data pending - buffer is %zu bytes long, %zu unsent - we have sent %zu bytes in total, %zu on this connection. Closing connection to flush the data.", host->hostname, connected_to, host->rrdpush_sender_buffer->len, host->rrdpush_sender_buffer->len - begin, sent_bytes, sent_bytes_on_this_connection);
  881. rrdpush_sender_thread_close_socket(host);
  882. }
  883. }
  884. netdata_thread_cleanup_pop(1);
  885. return NULL;
  886. }
  887. // ----------------------------------------------------------------------------
  888. // rrdpush receiver thread
  889. static 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) {
  890. 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);
  891. }
  892. static int rrdpush_receive(int fd
  893. , const char *key
  894. , const char *hostname
  895. , const char *registry_hostname
  896. , const char *machine_guid
  897. , const char *os
  898. , const char *timezone
  899. , const char *tags
  900. , const char *program_name
  901. , const char *program_version
  902. , struct rrdhost_system_info *system_info
  903. , int update_every
  904. , char *client_ip
  905. , char *client_port
  906. , uint32_t stream_version
  907. #ifdef ENABLE_HTTPS
  908. , struct netdata_ssl *ssl
  909. #endif
  910. ) {
  911. RRDHOST *host;
  912. int history = default_rrd_history_entries;
  913. RRD_MEMORY_MODE mode = default_rrd_memory_mode;
  914. int health_enabled = default_health_enabled;
  915. int rrdpush_enabled = default_rrdpush_enabled;
  916. char *rrdpush_destination = default_rrdpush_destination;
  917. char *rrdpush_api_key = default_rrdpush_api_key;
  918. char *rrdpush_send_charts_matching = default_rrdpush_send_charts_matching;
  919. time_t alarms_delay = 60;
  920. update_every = (int)appconfig_get_number(&stream_config, machine_guid, "update every", update_every);
  921. if(update_every < 0) update_every = 1;
  922. history = (int)appconfig_get_number(&stream_config, key, "default history", history);
  923. history = (int)appconfig_get_number(&stream_config, machine_guid, "history", history);
  924. if(history < 5) history = 5;
  925. mode = rrd_memory_mode_id(appconfig_get(&stream_config, key, "default memory mode", rrd_memory_mode_name(mode)));
  926. mode = rrd_memory_mode_id(appconfig_get(&stream_config, machine_guid, "memory mode", rrd_memory_mode_name(mode)));
  927. health_enabled = appconfig_get_boolean_ondemand(&stream_config, key, "health enabled by default", health_enabled);
  928. health_enabled = appconfig_get_boolean_ondemand(&stream_config, machine_guid, "health enabled", health_enabled);
  929. alarms_delay = appconfig_get_number(&stream_config, key, "default postpone alarms on connect seconds", alarms_delay);
  930. alarms_delay = appconfig_get_number(&stream_config, machine_guid, "postpone alarms on connect seconds", alarms_delay);
  931. rrdpush_enabled = appconfig_get_boolean(&stream_config, key, "default proxy enabled", rrdpush_enabled);
  932. rrdpush_enabled = appconfig_get_boolean(&stream_config, machine_guid, "proxy enabled", rrdpush_enabled);
  933. rrdpush_destination = appconfig_get(&stream_config, key, "default proxy destination", rrdpush_destination);
  934. rrdpush_destination = appconfig_get(&stream_config, machine_guid, "proxy destination", rrdpush_destination);
  935. rrdpush_api_key = appconfig_get(&stream_config, key, "default proxy api key", rrdpush_api_key);
  936. rrdpush_api_key = appconfig_get(&stream_config, machine_guid, "proxy api key", rrdpush_api_key);
  937. rrdpush_send_charts_matching = appconfig_get(&stream_config, key, "default proxy send charts matching", rrdpush_send_charts_matching);
  938. rrdpush_send_charts_matching = appconfig_get(&stream_config, machine_guid, "proxy send charts matching", rrdpush_send_charts_matching);
  939. tags = appconfig_set_default(&stream_config, machine_guid, "host tags", (tags)?tags:"");
  940. if(tags && !*tags) tags = NULL;
  941. if (strcmp(machine_guid, localhost->machine_guid) == 0) {
  942. log_stream_connection(client_ip, client_port, key, machine_guid, hostname, "DENIED - ATTEMPT TO RECEIVE METRICS FROM MACHINE_GUID IDENTICAL TO MASTER");
  943. error("STREAM %s [receive from %s:%s]: denied to receive metrics, machine GUID [%s] is my own. Did you copy the master/proxy machine guid to a slave?", hostname, client_ip, client_port, machine_guid);
  944. close(fd);
  945. return 1;
  946. }
  947. /*
  948. * Quick path for rejecting multiple connections. Don't take any locks so that progress is made. The same
  949. * condition will be checked again below, while holding the global and host writer locks. Any potential false
  950. * positives will not cause harm. Data hazards with host deconstruction will be handled when reference counting
  951. * is implemented.
  952. */
  953. host = rrdhost_find_by_guid(machine_guid, 0);
  954. if(host && host->connected_senders > 0) {
  955. log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "REJECTED - ALREADY CONNECTED");
  956. info("STREAM %s [receive from [%s]:%s]: multiple streaming connections for the same host detected. Rejecting new connection.", host->hostname, client_ip, client_port);
  957. close(fd);
  958. return 0;
  959. }
  960. host = rrdhost_find_or_create(
  961. hostname
  962. , registry_hostname
  963. , machine_guid
  964. , os
  965. , timezone
  966. , tags
  967. , program_name
  968. , program_version
  969. , update_every
  970. , history
  971. , mode
  972. , (unsigned int)(health_enabled != CONFIG_BOOLEAN_NO)
  973. , (unsigned int)(rrdpush_enabled && rrdpush_destination && *rrdpush_destination && rrdpush_api_key && *rrdpush_api_key)
  974. , rrdpush_destination
  975. , rrdpush_api_key
  976. , rrdpush_send_charts_matching
  977. , system_info
  978. );
  979. if(!host) {
  980. close(fd);
  981. log_stream_connection(client_ip, client_port, key, machine_guid, hostname, "FAILED - CANNOT ACQUIRE HOST");
  982. error("STREAM %s [receive from [%s]:%s]: failed to find/create host structure.", hostname, client_ip, client_port);
  983. return 1;
  984. }
  985. #ifdef NETDATA_INTERNAL_CHECKS
  986. info("STREAM %s [receive from [%s]:%s]: client willing to stream metrics for host '%s' with machine_guid '%s': update every = %d, history = %ld, memory mode = %s, health %s, tags '%s'"
  987. , hostname
  988. , client_ip
  989. , client_port
  990. , host->hostname
  991. , host->machine_guid
  992. , host->rrd_update_every
  993. , host->rrd_history_entries
  994. , rrd_memory_mode_name(host->rrd_memory_mode)
  995. , (health_enabled == CONFIG_BOOLEAN_NO)?"disabled":((health_enabled == CONFIG_BOOLEAN_YES)?"enabled":"auto")
  996. , host->tags?host->tags:""
  997. );
  998. #endif // NETDATA_INTERNAL_CHECKS
  999. struct plugind cd = {
  1000. .enabled = 1,
  1001. .update_every = default_rrd_update_every,
  1002. .pid = 0,
  1003. .serial_failures = 0,
  1004. .successful_collections = 0,
  1005. .obsolete = 0,
  1006. .started_t = now_realtime_sec(),
  1007. .next = NULL,
  1008. };
  1009. // put the client IP and port into the buffers used by plugins.d
  1010. snprintfz(cd.id, CONFIG_MAX_NAME, "%s:%s", client_ip, client_port);
  1011. snprintfz(cd.filename, FILENAME_MAX, "%s:%s", client_ip, client_port);
  1012. snprintfz(cd.fullfilename, FILENAME_MAX, "%s:%s", client_ip, client_port);
  1013. snprintfz(cd.cmd, PLUGINSD_CMD_MAX, "%s:%s", client_ip, client_port);
  1014. info("STREAM %s [receive from [%s]:%s]: initializing communication...", host->hostname, client_ip, client_port);
  1015. char initial_response[HTTP_HEADER_SIZE];
  1016. if (stream_version > 1) {
  1017. info("STREAM %s [receive from [%s]:%s]: Netdata is using the stream version %u.", host->hostname, client_ip, client_port, stream_version);
  1018. sprintf(initial_response, "%s%u", START_STREAMING_PROMPT_VN, stream_version);
  1019. } else if (stream_version == 1) {
  1020. info("STREAM %s [receive from [%s]:%s]: Netdata is using the stream version %u.", host->hostname, client_ip, client_port, stream_version);
  1021. sprintf(initial_response, "%s", START_STREAMING_PROMPT_V2);
  1022. } else {
  1023. info("STREAM %s [receive from [%s]:%s]: Netdata is using first stream protocol.", host->hostname, client_ip, client_port);
  1024. sprintf(initial_response, "%s", START_STREAMING_PROMPT);
  1025. }
  1026. #ifdef ENABLE_HTTPS
  1027. host->stream_ssl.conn = ssl->conn;
  1028. host->stream_ssl.flags = ssl->flags;
  1029. if(send_timeout(ssl, fd, initial_response, strlen(initial_response), 0, 60) != (ssize_t)strlen(initial_response)) {
  1030. #else
  1031. if(send_timeout(fd, initial_response, strlen(initial_response), 0, 60) != strlen(initial_response)) {
  1032. #endif
  1033. log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "FAILED - CANNOT REPLY");
  1034. error("STREAM %s [receive from [%s]:%s]: cannot send ready command.", host->hostname, client_ip, client_port);
  1035. close(fd);
  1036. return 0;
  1037. }
  1038. // remove the non-blocking flag from the socket
  1039. if(sock_delnonblock(fd) < 0)
  1040. error("STREAM %s [receive from [%s]:%s]: cannot remove the non-blocking flag from socket %d", host->hostname, client_ip, client_port, fd);
  1041. // convert the socket to a FILE *
  1042. FILE *fp = fdopen(fd, "r");
  1043. if(!fp) {
  1044. log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "FAILED - SOCKET ERROR");
  1045. error("STREAM %s [receive from [%s]:%s]: failed to get a FILE for FD %d.", host->hostname, client_ip, client_port, fd);
  1046. close(fd);
  1047. return 0;
  1048. }
  1049. rrdhost_wrlock(host);
  1050. if(host->connected_senders > 0) {
  1051. rrdhost_unlock(host);
  1052. log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "REJECTED - ALREADY CONNECTED");
  1053. info("STREAM %s [receive from [%s]:%s]: multiple streaming connections for the same host detected. Rejecting new connection.", host->hostname, client_ip, client_port);
  1054. fclose(fp);
  1055. return 0;
  1056. }
  1057. rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
  1058. host->connected_senders++;
  1059. host->senders_disconnected_time = 0;
  1060. host->labels_flag = (stream_version > 0)?LABEL_FLAG_UPDATE_STREAM:LABEL_FLAG_STOP_STREAM;
  1061. if(health_enabled != CONFIG_BOOLEAN_NO) {
  1062. if(alarms_delay > 0) {
  1063. host->health_delay_up_to = now_realtime_sec() + alarms_delay;
  1064. info("Postponing health checks for %ld seconds, on host '%s', because it was just connected."
  1065. , alarms_delay
  1066. , host->hostname
  1067. );
  1068. }
  1069. }
  1070. rrdhost_unlock(host);
  1071. // call the plugins.d processor to receive the metrics
  1072. info("STREAM %s [receive from [%s]:%s]: receiving metrics...", host->hostname, client_ip, client_port);
  1073. log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "CONNECTED");
  1074. size_t count = pluginsd_process(host, &cd, fp, 1);
  1075. log_stream_connection(client_ip, client_port, key, host->machine_guid, host->hostname, "DISCONNECTED");
  1076. error("STREAM %s [receive from [%s]:%s]: disconnected (completed %zu updates).", host->hostname, client_ip, client_port, count);
  1077. rrdhost_wrlock(host);
  1078. host->senders_disconnected_time = now_realtime_sec();
  1079. host->connected_senders--;
  1080. if(!host->connected_senders) {
  1081. rrdhost_flag_set(host, RRDHOST_FLAG_ORPHAN);
  1082. if(health_enabled == CONFIG_BOOLEAN_AUTO)
  1083. host->health_enabled = 0;
  1084. }
  1085. rrdhost_unlock(host);
  1086. if(host->connected_senders == 0)
  1087. rrdpush_sender_thread_stop(host);
  1088. // cleanup
  1089. fclose(fp);
  1090. return (int)count;
  1091. }
  1092. struct rrdpush_thread {
  1093. int fd;
  1094. char *key;
  1095. char *hostname;
  1096. char *registry_hostname;
  1097. char *machine_guid;
  1098. char *os;
  1099. char *timezone;
  1100. char *tags;
  1101. char *client_ip;
  1102. char *client_port;
  1103. char *program_name;
  1104. char *program_version;
  1105. struct rrdhost_system_info *system_info;
  1106. int update_every;
  1107. uint32_t stream_version;
  1108. #ifdef ENABLE_HTTPS
  1109. struct netdata_ssl ssl;
  1110. #endif
  1111. };
  1112. static void rrdpush_receiver_thread_cleanup(void *ptr) {
  1113. static __thread int executed = 0;
  1114. if(!executed) {
  1115. executed = 1;
  1116. struct rrdpush_thread *rpt = (struct rrdpush_thread *) ptr;
  1117. info("STREAM %s [receive from [%s]:%s]: receive thread ended (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
  1118. freez(rpt->key);
  1119. freez(rpt->hostname);
  1120. freez(rpt->registry_hostname);
  1121. freez(rpt->machine_guid);
  1122. freez(rpt->os);
  1123. freez(rpt->timezone);
  1124. freez(rpt->tags);
  1125. freez(rpt->client_ip);
  1126. freez(rpt->client_port);
  1127. freez(rpt->program_name);
  1128. freez(rpt->program_version);
  1129. #ifdef ENABLE_HTTPS
  1130. if(rpt->ssl.conn){
  1131. SSL_free(rpt->ssl.conn);
  1132. }
  1133. #endif
  1134. freez(rpt);
  1135. }
  1136. }
  1137. static void *rrdpush_receiver_thread(void *ptr) {
  1138. netdata_thread_cleanup_push(rrdpush_receiver_thread_cleanup, ptr);
  1139. struct rrdpush_thread *rpt = (struct rrdpush_thread *)ptr;
  1140. info("STREAM %s [%s]:%s: receive thread created (task id %d)", rpt->hostname, rpt->client_ip, rpt->client_port, gettid());
  1141. rrdpush_receive(
  1142. rpt->fd
  1143. , rpt->key
  1144. , rpt->hostname
  1145. , rpt->registry_hostname
  1146. , rpt->machine_guid
  1147. , rpt->os
  1148. , rpt->timezone
  1149. , rpt->tags
  1150. , rpt->program_name
  1151. , rpt->program_version
  1152. , rpt->system_info
  1153. , rpt->update_every
  1154. , rpt->client_ip
  1155. , rpt->client_port
  1156. , rpt->stream_version
  1157. #ifdef ENABLE_HTTPS
  1158. , &rpt->ssl
  1159. #endif
  1160. );
  1161. netdata_thread_cleanup_pop(1);
  1162. return NULL;
  1163. }
  1164. static void rrdpush_sender_thread_spawn(RRDHOST *host) {
  1165. rrdhost_wrlock(host);
  1166. if(!host->rrdpush_sender_spawn) {
  1167. char tag[NETDATA_THREAD_TAG_MAX + 1];
  1168. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "STREAM_SENDER[%s]", host->hostname);
  1169. if(netdata_thread_create(&host->rrdpush_sender_thread, tag, NETDATA_THREAD_OPTION_JOINABLE, rrdpush_sender_thread, (void *) host))
  1170. error("STREAM %s [send]: failed to create new thread for client.", host->hostname);
  1171. else
  1172. host->rrdpush_sender_spawn = 1;
  1173. }
  1174. rrdhost_unlock(host);
  1175. }
  1176. int rrdpush_receiver_permission_denied(struct web_client *w) {
  1177. // we always respond with the same message and error code
  1178. // to prevent an attacker from gaining info about the error
  1179. buffer_flush(w->response.data);
  1180. buffer_sprintf(w->response.data, "You are not permitted to access this. Check the logs for more info.");
  1181. return 401;
  1182. }
  1183. int rrdpush_receiver_too_busy_now(struct web_client *w) {
  1184. // we always respond with the same message and error code
  1185. // to prevent an attacker from gaining info about the error
  1186. buffer_flush(w->response.data);
  1187. buffer_sprintf(w->response.data, "The server is too busy now to accept this request. Try later.");
  1188. return 503;
  1189. }
  1190. int rrdpush_receiver_thread_spawn(RRDHOST *host, struct web_client *w, char *url) {
  1191. (void)host;
  1192. info("clients wants to STREAM metrics.");
  1193. char *key = NULL, *hostname = NULL, *registry_hostname = NULL, *machine_guid = NULL, *os = "unknown", *timezone = "unknown", *tags = NULL;
  1194. int update_every = default_rrd_update_every;
  1195. uint32_t stream_version = UINT_MAX;
  1196. char buf[GUID_LEN + 1];
  1197. struct rrdhost_system_info *system_info = callocz(1, sizeof(struct rrdhost_system_info));
  1198. while(url) {
  1199. char *value = mystrsep(&url, "&");
  1200. if(!value || !*value) continue;
  1201. char *name = mystrsep(&value, "=");
  1202. if(!name || !*name) continue;
  1203. if(!value || !*value) continue;
  1204. if(!strcmp(name, "key"))
  1205. key = value;
  1206. else if(!strcmp(name, "hostname"))
  1207. hostname = value;
  1208. else if(!strcmp(name, "registry_hostname"))
  1209. registry_hostname = value;
  1210. else if(!strcmp(name, "machine_guid"))
  1211. machine_guid = value;
  1212. else if(!strcmp(name, "update_every"))
  1213. update_every = (int)strtoul(value, NULL, 0);
  1214. else if(!strcmp(name, "os"))
  1215. os = value;
  1216. else if(!strcmp(name, "timezone"))
  1217. timezone = value;
  1218. else if(!strcmp(name, "tags"))
  1219. tags = value;
  1220. else if(!strcmp(name, "ver"))
  1221. stream_version = MIN((uint32_t) strtoul(value, NULL, 0), STREAMING_PROTOCOL_CURRENT_VERSION);
  1222. else {
  1223. // An old Netdata slave does not have a compatible streaming protocol, map to something sane.
  1224. if (!strcmp(name, "NETDATA_SYSTEM_OS_NAME"))
  1225. name = "NETDATA_HOST_OS_NAME";
  1226. else if (!strcmp(name, "NETDATA_SYSTEM_OS_ID"))
  1227. name = "NETDATA_HOST_OS_ID";
  1228. else if (!strcmp(name, "NETDATA_SYSTEM_OS_ID_LIKE"))
  1229. name = "NETDATA_HOST_OS_ID_LIKE";
  1230. else if (!strcmp(name, "NETDATA_SYSTEM_OS_VERSION"))
  1231. name = "NETDATA_HOST_OS_VERSION";
  1232. else if (!strcmp(name, "NETDATA_SYSTEM_OS_VERSION_ID"))
  1233. name = "NETDATA_HOST_OS_VERSION_ID";
  1234. else if (!strcmp(name, "NETDATA_SYSTEM_OS_DETECTION"))
  1235. name = "NETDATA_HOST_OS_DETECTION";
  1236. else if(!strcmp(name, "NETDATA_PROTOCOL_VERSION") && stream_version == UINT_MAX) {
  1237. stream_version = 1;
  1238. }
  1239. if (unlikely(rrdhost_set_system_info_variable(system_info, name, value))) {
  1240. info("STREAM [receive from [%s]:%s]: request has parameter '%s' = '%s', which is not used.",
  1241. w->client_ip, w->client_port, name, value);
  1242. }
  1243. }
  1244. }
  1245. if (stream_version == UINT_MAX)
  1246. stream_version = 0;
  1247. if(!key || !*key) {
  1248. rrdhost_system_info_free(system_info);
  1249. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO KEY");
  1250. error("STREAM [receive from [%s]:%s]: request without an API key. Forbidding access.", w->client_ip, w->client_port);
  1251. return rrdpush_receiver_permission_denied(w);
  1252. }
  1253. if(!hostname || !*hostname) {
  1254. rrdhost_system_info_free(system_info);
  1255. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO HOSTNAME");
  1256. error("STREAM [receive from [%s]:%s]: request without a hostname. Forbidding access.", w->client_ip, w->client_port);
  1257. return rrdpush_receiver_permission_denied(w);
  1258. }
  1259. if(!machine_guid || !*machine_guid) {
  1260. rrdhost_system_info_free(system_info);
  1261. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - NO MACHINE GUID");
  1262. error("STREAM [receive from [%s]:%s]: request without a machine GUID. Forbidding access.", w->client_ip, w->client_port);
  1263. return rrdpush_receiver_permission_denied(w);
  1264. }
  1265. if(regenerate_guid(key, buf) == -1) {
  1266. rrdhost_system_info_free(system_info);
  1267. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - INVALID KEY");
  1268. error("STREAM [receive from [%s]:%s]: API key '%s' is not valid GUID (use the command uuidgen to generate one). Forbidding access.", w->client_ip, w->client_port, key);
  1269. return rrdpush_receiver_permission_denied(w);
  1270. }
  1271. if(regenerate_guid(machine_guid, buf) == -1) {
  1272. rrdhost_system_info_free(system_info);
  1273. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - INVALID MACHINE GUID");
  1274. error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not GUID. Forbidding access.", w->client_ip, w->client_port, machine_guid);
  1275. return rrdpush_receiver_permission_denied(w);
  1276. }
  1277. if(!appconfig_get_boolean(&stream_config, key, "enabled", 0)) {
  1278. rrdhost_system_info_free(system_info);
  1279. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - KEY NOT ENABLED");
  1280. error("STREAM [receive from [%s]:%s]: API key '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, key);
  1281. return rrdpush_receiver_permission_denied(w);
  1282. }
  1283. {
  1284. SIMPLE_PATTERN *key_allow_from = simple_pattern_create(appconfig_get(&stream_config, key, "allow from", "*"), NULL, SIMPLE_PATTERN_EXACT);
  1285. if(key_allow_from) {
  1286. if(!simple_pattern_matches(key_allow_from, w->client_ip)) {
  1287. simple_pattern_free(key_allow_from);
  1288. rrdhost_system_info_free(system_info);
  1289. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname) ? hostname : "-", "ACCESS DENIED - KEY NOT ALLOWED FROM THIS IP");
  1290. error("STREAM [receive from [%s]:%s]: API key '%s' is not permitted from this IP. Forbidding access.", w->client_ip, w->client_port, key);
  1291. return rrdpush_receiver_permission_denied(w);
  1292. }
  1293. simple_pattern_free(key_allow_from);
  1294. }
  1295. }
  1296. if(!appconfig_get_boolean(&stream_config, machine_guid, "enabled", 1)) {
  1297. rrdhost_system_info_free(system_info);
  1298. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname)?hostname:"-", "ACCESS DENIED - MACHINE GUID NOT ENABLED");
  1299. error("STREAM [receive from [%s]:%s]: machine GUID '%s' is not allowed. Forbidding access.", w->client_ip, w->client_port, machine_guid);
  1300. return rrdpush_receiver_permission_denied(w);
  1301. }
  1302. {
  1303. SIMPLE_PATTERN *machine_allow_from = simple_pattern_create(appconfig_get(&stream_config, machine_guid, "allow from", "*"), NULL, SIMPLE_PATTERN_EXACT);
  1304. if(machine_allow_from) {
  1305. if(!simple_pattern_matches(machine_allow_from, w->client_ip)) {
  1306. simple_pattern_free(machine_allow_from);
  1307. rrdhost_system_info_free(system_info);
  1308. log_stream_connection(w->client_ip, w->client_port, (key && *key)?key:"-", (machine_guid && *machine_guid)?machine_guid:"-", (hostname && *hostname) ? hostname : "-", "ACCESS DENIED - MACHINE GUID NOT ALLOWED FROM THIS IP");
  1309. error("STREAM [receive from [%s]:%s]: Machine GUID '%s' is not permitted from this IP. Forbidding access.", w->client_ip, w->client_port, machine_guid);
  1310. return rrdpush_receiver_permission_denied(w);
  1311. }
  1312. simple_pattern_free(machine_allow_from);
  1313. }
  1314. }
  1315. if(unlikely(web_client_streaming_rate_t > 0)) {
  1316. static netdata_mutex_t stream_rate_mutex = NETDATA_MUTEX_INITIALIZER;
  1317. static volatile time_t last_stream_accepted_t = 0;
  1318. netdata_mutex_lock(&stream_rate_mutex);
  1319. time_t now = now_realtime_sec();
  1320. if(unlikely(last_stream_accepted_t == 0))
  1321. last_stream_accepted_t = now;
  1322. if(now - last_stream_accepted_t < web_client_streaming_rate_t) {
  1323. netdata_mutex_unlock(&stream_rate_mutex);
  1324. rrdhost_system_info_free(system_info);
  1325. error("STREAM [receive from [%s]:%s]: too busy to accept new streaming request. Will be allowed in %ld secs.", w->client_ip, w->client_port, (long)(web_client_streaming_rate_t - (now - last_stream_accepted_t)));
  1326. return rrdpush_receiver_too_busy_now(w);
  1327. }
  1328. last_stream_accepted_t = now;
  1329. netdata_mutex_unlock(&stream_rate_mutex);
  1330. }
  1331. struct rrdpush_thread *rpt = callocz(1, sizeof(struct rrdpush_thread));
  1332. rpt->fd = w->ifd;
  1333. rpt->key = strdupz(key);
  1334. rpt->hostname = strdupz(hostname);
  1335. rpt->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
  1336. rpt->machine_guid = strdupz(machine_guid);
  1337. rpt->os = strdupz(os);
  1338. rpt->timezone = strdupz(timezone);
  1339. rpt->tags = (tags)?strdupz(tags):NULL;
  1340. rpt->client_ip = strdupz(w->client_ip);
  1341. rpt->client_port = strdupz(w->client_port);
  1342. rpt->update_every = update_every;
  1343. rpt->system_info = system_info;
  1344. rpt->stream_version = stream_version;
  1345. #ifdef ENABLE_HTTPS
  1346. rpt->ssl.conn = w->ssl.conn;
  1347. rpt->ssl.flags = w->ssl.flags;
  1348. w->ssl.conn = NULL;
  1349. w->ssl.flags = NETDATA_SSL_START;
  1350. #endif
  1351. if(w->user_agent && w->user_agent[0]) {
  1352. char *t = strchr(w->user_agent, '/');
  1353. if(t && *t) {
  1354. *t = '\0';
  1355. t++;
  1356. }
  1357. rpt->program_name = strdupz(w->user_agent);
  1358. if(t && *t) rpt->program_version = strdupz(t);
  1359. }
  1360. netdata_thread_t thread;
  1361. debug(D_SYSTEM, "starting STREAM receive thread.");
  1362. char tag[FILENAME_MAX + 1];
  1363. snprintfz(tag, FILENAME_MAX, "STREAM_RECEIVER[%s,[%s]:%s]", rpt->hostname, w->client_ip, w->client_port);
  1364. if(netdata_thread_create(&thread, tag, NETDATA_THREAD_OPTION_DEFAULT, rrdpush_receiver_thread, (void *)rpt))
  1365. error("Failed to create new STREAM receive thread for client.");
  1366. // prevent the caller from closing the streaming socket
  1367. if(web_server_mode == WEB_SERVER_MODE_STATIC_THREADED) {
  1368. web_client_flag_set(w, WEB_CLIENT_FLAG_DONT_CLOSE_SOCKET);
  1369. }
  1370. else {
  1371. if(w->ifd == w->ofd)
  1372. w->ifd = w->ofd = -1;
  1373. else
  1374. w->ifd = -1;
  1375. }
  1376. buffer_flush(w->response.data);
  1377. return 200;
  1378. }