pluginsd_parser.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "pluginsd_parser.h"
  3. #define LOG_FUNCTIONS false
  4. static ssize_t send_to_plugin(const char *txt, void *data) {
  5. PARSER *parser = data;
  6. if(!txt || !*txt)
  7. return 0;
  8. errno = 0;
  9. spinlock_lock(&parser->writer.spinlock);
  10. ssize_t bytes = -1;
  11. #ifdef ENABLE_HTTPS
  12. NETDATA_SSL *ssl = parser->ssl_output;
  13. if(ssl) {
  14. if(SSL_connection(ssl))
  15. bytes = netdata_ssl_write(ssl, (void *) txt, strlen(txt));
  16. else
  17. error("PLUGINSD: cannot send command (SSL)");
  18. spinlock_unlock(&parser->writer.spinlock);
  19. return bytes;
  20. }
  21. #endif
  22. if(parser->fp_output) {
  23. bytes = fprintf(parser->fp_output, "%s", txt);
  24. if(bytes <= 0) {
  25. error("PLUGINSD: cannot send command (FILE)");
  26. bytes = -2;
  27. }
  28. else
  29. fflush(parser->fp_output);
  30. spinlock_unlock(&parser->writer.spinlock);
  31. return bytes;
  32. }
  33. if(parser->fd != -1) {
  34. bytes = 0;
  35. ssize_t total = (ssize_t)strlen(txt);
  36. ssize_t sent;
  37. do {
  38. sent = write(parser->fd, &txt[bytes], total - bytes);
  39. if(sent <= 0) {
  40. error("PLUGINSD: cannot send command (fd)");
  41. spinlock_unlock(&parser->writer.spinlock);
  42. return -3;
  43. }
  44. bytes += sent;
  45. }
  46. while(bytes < total);
  47. spinlock_unlock(&parser->writer.spinlock);
  48. return (int)bytes;
  49. }
  50. spinlock_unlock(&parser->writer.spinlock);
  51. error("PLUGINSD: cannot send command (no output socket/pipe/file given to plugins.d parser)");
  52. return -4;
  53. }
  54. static inline RRDHOST *pluginsd_require_host_from_parent(void *user, const char *cmd) {
  55. RRDHOST *host = ((PARSER_USER_OBJECT *) user)->host;
  56. if(unlikely(!host))
  57. error("PLUGINSD: command %s requires a host, but is not set.", cmd);
  58. return host;
  59. }
  60. static inline RRDSET *pluginsd_require_chart_from_parent(void *user, const char *cmd, const char *parent_cmd) {
  61. RRDSET *st = ((PARSER_USER_OBJECT *) user)->st;
  62. if(unlikely(!st))
  63. error("PLUGINSD: command %s requires a chart defined via command %s, but is not set.", cmd, parent_cmd);
  64. return st;
  65. }
  66. static inline RRDSET *pluginsd_get_chart_from_parent(void *user) {
  67. return ((PARSER_USER_OBJECT *) user)->st;
  68. }
  69. static inline void pluginsd_lock_rrdset_data_collection(void *user) {
  70. PARSER_USER_OBJECT *u = (PARSER_USER_OBJECT *) user;
  71. if(u->st && !u->v2.locked_data_collection) {
  72. spinlock_lock(&u->st->data_collection_lock);
  73. u->v2.locked_data_collection = true;
  74. }
  75. }
  76. static inline bool pluginsd_unlock_rrdset_data_collection(void *user) {
  77. PARSER_USER_OBJECT *u = (PARSER_USER_OBJECT *) user;
  78. if(u->st && u->v2.locked_data_collection) {
  79. spinlock_unlock(&u->st->data_collection_lock);
  80. u->v2.locked_data_collection = false;
  81. return true;
  82. }
  83. return false;
  84. }
  85. void pluginsd_rrdset_cleanup(RRDSET *st) {
  86. for(size_t i = 0; i < st->pluginsd.used ; i++) {
  87. if (st->pluginsd.rda[i]) {
  88. rrddim_acquired_release(st->pluginsd.rda[i]);
  89. st->pluginsd.rda[i] = NULL;
  90. }
  91. }
  92. freez(st->pluginsd.rda);
  93. st->pluginsd.rda = NULL;
  94. st->pluginsd.size = 0;
  95. st->pluginsd.used = 0;
  96. st->pluginsd.pos = 0;
  97. }
  98. static inline void pluginsd_unlock_previous_chart(void *user, const char *keyword, bool stale) {
  99. PARSER_USER_OBJECT *u = (PARSER_USER_OBJECT *) user;
  100. if(unlikely(pluginsd_unlock_rrdset_data_collection(user))) {
  101. if(stale)
  102. error("PLUGINSD: 'host:%s/chart:%s/' stale data collection lock found during %s; it has been unlocked",
  103. rrdhost_hostname(u->st->rrdhost), rrdset_id(u->st), keyword);
  104. }
  105. if(unlikely(u->v2.ml_locked)) {
  106. ml_chart_update_end(u->st);
  107. u->v2.ml_locked = false;
  108. if(stale)
  109. error("PLUGINSD: 'host:%s/chart:%s/' stale ML lock found during %s, it has been unlocked",
  110. rrdhost_hostname(u->st->rrdhost), rrdset_id(u->st), keyword);
  111. }
  112. }
  113. static inline void pluginsd_set_chart_from_parent(void *user, RRDSET *st, const char *keyword) {
  114. PARSER_USER_OBJECT *u = (PARSER_USER_OBJECT *) user;
  115. pluginsd_unlock_previous_chart(user, keyword, true);
  116. if(st) {
  117. size_t dims = dictionary_entries(st->rrddim_root_index);
  118. if(unlikely(st->pluginsd.size < dims)) {
  119. st->pluginsd.rda = reallocz(st->pluginsd.rda, dims * sizeof(RRDDIM_ACQUIRED *));
  120. st->pluginsd.size = dims;
  121. }
  122. if(st->pluginsd.pos > st->pluginsd.used && st->pluginsd.pos <= st->pluginsd.size)
  123. st->pluginsd.used = st->pluginsd.pos;
  124. st->pluginsd.pos = 0;
  125. }
  126. u->st = st;
  127. }
  128. static inline RRDDIM *pluginsd_acquire_dimension(RRDHOST *host, RRDSET *st, const char *dimension, const char *cmd) {
  129. if (unlikely(!dimension || !*dimension)) {
  130. error("PLUGINSD: 'host:%s/chart:%s' got a %s, without a dimension.",
  131. rrdhost_hostname(host), rrdset_id(st), cmd);
  132. return NULL;
  133. }
  134. RRDDIM_ACQUIRED *rda;
  135. if(likely(st->pluginsd.pos < st->pluginsd.used)) {
  136. rda = st->pluginsd.rda[st->pluginsd.pos];
  137. RRDDIM *rd = rrddim_acquired_to_rrddim(rda);
  138. if (likely(rd && string_strcmp(rd->id, dimension) == 0)) {
  139. st->pluginsd.pos++;
  140. return rd;
  141. }
  142. else {
  143. rrddim_acquired_release(rda);
  144. st->pluginsd.rda[st->pluginsd.pos] = NULL;
  145. }
  146. }
  147. rda = rrddim_find_and_acquire(st, dimension);
  148. if (unlikely(!rda)) {
  149. error("PLUGINSD: 'host:%s/chart:%s/dim:%s' got a %s but dimension does not exist.",
  150. rrdhost_hostname(host), rrdset_id(st), dimension, cmd);
  151. return NULL;
  152. }
  153. if(likely(st->pluginsd.pos < st->pluginsd.size))
  154. st->pluginsd.rda[st->pluginsd.pos++] = rda;
  155. return rrddim_acquired_to_rrddim(rda);
  156. }
  157. static inline RRDSET *pluginsd_find_chart(RRDHOST *host, const char *chart, const char *cmd) {
  158. if (unlikely(!chart || !*chart)) {
  159. error("PLUGINSD: 'host:%s' got a %s without a chart id.",
  160. rrdhost_hostname(host), cmd);
  161. return NULL;
  162. }
  163. RRDSET *st = rrdset_find(host, chart);
  164. if (unlikely(!st))
  165. error("PLUGINSD: 'host:%s/chart:%s' got a %s but chart does not exist.",
  166. rrdhost_hostname(host), chart, cmd);
  167. return st;
  168. }
  169. static inline PARSER_RC PLUGINSD_DISABLE_PLUGIN(void *user, const char *keyword, const char *msg) {
  170. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  171. if(keyword && msg) {
  172. error_limit_static_global_var(erl, 1, 0);
  173. error_limit(&erl, "PLUGINSD: keyword %s: %s", keyword, msg);
  174. }
  175. return PARSER_RC_ERROR;
  176. }
  177. PARSER_RC pluginsd_set(char **words, size_t num_words, void *user)
  178. {
  179. char *dimension = get_word(words, num_words, 1);
  180. char *value = get_word(words, num_words, 2);
  181. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_SET);
  182. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  183. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_SET, PLUGINSD_KEYWORD_CHART);
  184. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  185. RRDDIM *rd = pluginsd_acquire_dimension(host, st, dimension, PLUGINSD_KEYWORD_SET);
  186. if(!rd) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  187. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  188. debug(D_PLUGINSD, "PLUGINSD: 'host:%s/chart:%s/dim:%s' SET is setting value to '%s'",
  189. rrdhost_hostname(host), rrdset_id(st), dimension, value && *value ? value : "UNSET");
  190. if (value && *value)
  191. rrddim_set_by_pointer(st, rd, str2ll_encoded(value));
  192. return PARSER_RC_OK;
  193. }
  194. PARSER_RC pluginsd_begin(char **words, size_t num_words, void *user)
  195. {
  196. char *id = get_word(words, num_words, 1);
  197. char *microseconds_txt = get_word(words, num_words, 2);
  198. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_BEGIN);
  199. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  200. RRDSET *st = pluginsd_find_chart(host, id, PLUGINSD_KEYWORD_BEGIN);
  201. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  202. pluginsd_set_chart_from_parent(user, st, PLUGINSD_KEYWORD_BEGIN);
  203. usec_t microseconds = 0;
  204. if (microseconds_txt && *microseconds_txt) {
  205. long long t = str2ll(microseconds_txt, NULL);
  206. if(t >= 0)
  207. microseconds = t;
  208. }
  209. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  210. if(st->replay.log_next_data_collection) {
  211. st->replay.log_next_data_collection = false;
  212. internal_error(true,
  213. "REPLAY: 'host:%s/chart:%s' first BEGIN after replication, last collected %llu, last updated %llu, microseconds %llu",
  214. rrdhost_hostname(host), rrdset_id(st),
  215. st->last_collected_time.tv_sec * USEC_PER_SEC + st->last_collected_time.tv_usec,
  216. st->last_updated.tv_sec * USEC_PER_SEC + st->last_updated.tv_usec,
  217. microseconds
  218. );
  219. }
  220. #endif
  221. if (likely(st->counter_done)) {
  222. if (likely(microseconds)) {
  223. if (((PARSER_USER_OBJECT *)user)->trust_durations)
  224. rrdset_next_usec_unfiltered(st, microseconds);
  225. else
  226. rrdset_next_usec(st, microseconds);
  227. }
  228. else
  229. rrdset_next(st);
  230. }
  231. return PARSER_RC_OK;
  232. }
  233. PARSER_RC pluginsd_end(char **words, size_t num_words, void *user)
  234. {
  235. UNUSED(words);
  236. UNUSED(num_words);
  237. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_END);
  238. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  239. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_END, PLUGINSD_KEYWORD_BEGIN);
  240. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  241. if (unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  242. debug(D_PLUGINSD, "requested an END on chart '%s'", rrdset_id(st));
  243. pluginsd_set_chart_from_parent(user, NULL, PLUGINSD_KEYWORD_END);
  244. ((PARSER_USER_OBJECT *) user)->data_collections_count++;
  245. struct timeval now;
  246. now_realtime_timeval(&now);
  247. rrdset_timed_done(st, now, /* pending_rrdset_next = */ false);
  248. return PARSER_RC_OK;
  249. }
  250. static void pluginsd_host_define_cleanup(void *user) {
  251. PARSER_USER_OBJECT *u = user;
  252. string_freez(u->host_define.hostname);
  253. dictionary_destroy(u->host_define.rrdlabels);
  254. u->host_define.hostname = NULL;
  255. u->host_define.rrdlabels = NULL;
  256. u->host_define.parsing_host = false;
  257. }
  258. static inline bool pluginsd_validate_machine_guid(const char *guid, uuid_t *uuid, char *output) {
  259. if(uuid_parse(guid, *uuid))
  260. return false;
  261. uuid_unparse_lower(*uuid, output);
  262. return true;
  263. }
  264. static PARSER_RC pluginsd_host_define(char **words, size_t num_words, void *user) {
  265. PARSER_USER_OBJECT *u = user;
  266. char *guid = get_word(words, num_words, 1);
  267. char *hostname = get_word(words, num_words, 2);
  268. if(unlikely(!guid || !*guid || !hostname || !*hostname))
  269. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_HOST_DEFINE, "missing parameters");
  270. if(unlikely(u->host_define.parsing_host))
  271. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_HOST_DEFINE,
  272. "another host definition is already open - did you send " PLUGINSD_KEYWORD_HOST_DEFINE_END "?");
  273. if(!pluginsd_validate_machine_guid(guid, &u->host_define.machine_guid, u->host_define.machine_guid_str))
  274. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_HOST_DEFINE, "cannot parse MACHINE_GUID - is it a valid UUID?");
  275. u->host_define.hostname = string_strdupz(hostname);
  276. u->host_define.rrdlabels = rrdlabels_create();
  277. u->host_define.parsing_host = true;
  278. return PARSER_RC_OK;
  279. }
  280. static inline PARSER_RC pluginsd_host_dictionary(char **words, size_t num_words, void *user, DICTIONARY *dict, const char *keyword) {
  281. PARSER_USER_OBJECT *u = user;
  282. char *name = get_word(words, num_words, 1);
  283. char *value = get_word(words, num_words, 2);
  284. if(!name || !*name || !value)
  285. return PLUGINSD_DISABLE_PLUGIN(user, keyword, "missing parameters");
  286. if(!u->host_define.parsing_host || !dict)
  287. return PLUGINSD_DISABLE_PLUGIN(user, keyword, "host is not defined, send " PLUGINSD_KEYWORD_HOST_DEFINE " before this");
  288. rrdlabels_add(dict, name, value, RRDLABEL_SRC_CONFIG);
  289. return PARSER_RC_OK;
  290. }
  291. static PARSER_RC pluginsd_host_labels(char **words, size_t num_words, void *user) {
  292. PARSER_USER_OBJECT *u = user;
  293. return pluginsd_host_dictionary(words, num_words, user, u->host_define.rrdlabels, PLUGINSD_KEYWORD_HOST_LABEL);
  294. }
  295. static PARSER_RC pluginsd_host_define_end(char **words __maybe_unused, size_t num_words __maybe_unused, void *user) {
  296. PARSER_USER_OBJECT *u = user;
  297. if(!u->host_define.parsing_host)
  298. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_HOST_DEFINE_END, "missing initialization, send " PLUGINSD_KEYWORD_HOST_DEFINE " before this");
  299. RRDHOST *host = rrdhost_find_or_create(
  300. string2str(u->host_define.hostname),
  301. string2str(u->host_define.hostname),
  302. u->host_define.machine_guid_str,
  303. "Netdata Virtual Host 1.0",
  304. netdata_configured_timezone,
  305. netdata_configured_abbrev_timezone,
  306. netdata_configured_utc_offset,
  307. NULL,
  308. program_name,
  309. program_version,
  310. default_rrd_update_every,
  311. default_rrd_history_entries,
  312. default_rrd_memory_mode,
  313. default_health_enabled,
  314. default_rrdpush_enabled,
  315. default_rrdpush_destination,
  316. default_rrdpush_api_key,
  317. default_rrdpush_send_charts_matching,
  318. default_rrdpush_enable_replication,
  319. default_rrdpush_seconds_to_replicate,
  320. default_rrdpush_replication_step,
  321. rrdhost_labels_to_system_info(u->host_define.rrdlabels),
  322. false
  323. );
  324. rrdhost_option_set(host, RRDHOST_OPTION_VIRTUAL_HOST);
  325. if(host->rrdlabels) {
  326. rrdlabels_migrate_to_these(host->rrdlabels, u->host_define.rrdlabels);
  327. }
  328. else {
  329. host->rrdlabels = u->host_define.rrdlabels;
  330. u->host_define.rrdlabels = NULL;
  331. }
  332. pluginsd_host_define_cleanup(user);
  333. u->host = host;
  334. pluginsd_set_chart_from_parent(user, NULL, PLUGINSD_KEYWORD_HOST_DEFINE_END);
  335. rrdhost_flag_clear(host, RRDHOST_FLAG_ORPHAN);
  336. rrdcontext_host_child_connected(host);
  337. schedule_node_info_update(host);
  338. return PARSER_RC_OK;
  339. }
  340. static PARSER_RC pluginsd_host(char **words, size_t num_words, void *user) {
  341. PARSER_USER_OBJECT *u = user;
  342. char *guid = get_word(words, num_words, 1);
  343. if(!guid || !*guid || strcmp(guid, "localhost") == 0) {
  344. u->host = localhost;
  345. return PARSER_RC_OK;
  346. }
  347. uuid_t uuid;
  348. char uuid_str[UUID_STR_LEN];
  349. if(!pluginsd_validate_machine_guid(guid, &uuid, uuid_str))
  350. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_HOST, "cannot parse MACHINE_GUID - is it a valid UUID?");
  351. RRDHOST *host = rrdhost_find_by_guid(uuid_str);
  352. if(unlikely(!host))
  353. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_HOST, "cannot find a host with this machine guid - have you created it?");
  354. u->host = host;
  355. return PARSER_RC_OK;
  356. }
  357. PARSER_RC pluginsd_chart(char **words, size_t num_words, void *user)
  358. {
  359. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_CHART);
  360. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  361. char *type = get_word(words, num_words, 1);
  362. char *name = get_word(words, num_words, 2);
  363. char *title = get_word(words, num_words, 3);
  364. char *units = get_word(words, num_words, 4);
  365. char *family = get_word(words, num_words, 5);
  366. char *context = get_word(words, num_words, 6);
  367. char *chart = get_word(words, num_words, 7);
  368. char *priority_s = get_word(words, num_words, 8);
  369. char *update_every_s = get_word(words, num_words, 9);
  370. char *options = get_word(words, num_words, 10);
  371. char *plugin = get_word(words, num_words, 11);
  372. char *module = get_word(words, num_words, 12);
  373. // parse the id from type
  374. char *id = NULL;
  375. if (likely(type && (id = strchr(type, '.')))) {
  376. *id = '\0';
  377. id++;
  378. }
  379. // make sure we have the required variables
  380. if (unlikely((!type || !*type || !id || !*id)))
  381. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_CHART, "missing parameters");
  382. // parse the name, and make sure it does not include 'type.'
  383. if (unlikely(name && *name)) {
  384. // when data are streamed from child nodes
  385. // name will be type.name
  386. // so, we have to remove 'type.' from name too
  387. size_t len = strlen(type);
  388. if (strncmp(type, name, len) == 0 && name[len] == '.')
  389. name = &name[len + 1];
  390. // if the name is the same with the id,
  391. // or is just 'NULL', clear it.
  392. if (unlikely(strcmp(name, id) == 0 || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0))
  393. name = NULL;
  394. }
  395. int priority = 1000;
  396. if (likely(priority_s && *priority_s))
  397. priority = str2i(priority_s);
  398. int update_every = ((PARSER_USER_OBJECT *) user)->cd->update_every;
  399. if (likely(update_every_s && *update_every_s))
  400. update_every = str2i(update_every_s);
  401. if (unlikely(!update_every))
  402. update_every = ((PARSER_USER_OBJECT *) user)->cd->update_every;
  403. RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
  404. if (unlikely(chart))
  405. chart_type = rrdset_type_id(chart);
  406. if (unlikely(name && !*name))
  407. name = NULL;
  408. if (unlikely(family && !*family))
  409. family = NULL;
  410. if (unlikely(context && !*context))
  411. context = NULL;
  412. if (unlikely(!title))
  413. title = "";
  414. if (unlikely(!units))
  415. units = "unknown";
  416. debug(
  417. D_PLUGINSD,
  418. "creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d",
  419. type, id, name ? name : "", family ? family : "", context ? context : "", rrdset_type_name(chart_type),
  420. priority, update_every);
  421. RRDSET *st = NULL;
  422. st = rrdset_create(
  423. host, type, id, name, family, context, title, units,
  424. (plugin && *plugin) ? plugin : ((PARSER_USER_OBJECT *)user)->cd->filename,
  425. module, priority, update_every,
  426. chart_type);
  427. if (likely(st)) {
  428. if (options && *options) {
  429. if (strstr(options, "obsolete"))
  430. rrdset_is_obsolete(st);
  431. else
  432. rrdset_isnot_obsolete(st);
  433. if (strstr(options, "detail"))
  434. rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
  435. else
  436. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  437. if (strstr(options, "hidden"))
  438. rrdset_flag_set(st, RRDSET_FLAG_HIDDEN);
  439. else
  440. rrdset_flag_clear(st, RRDSET_FLAG_HIDDEN);
  441. if (strstr(options, "store_first"))
  442. rrdset_flag_set(st, RRDSET_FLAG_STORE_FIRST);
  443. else
  444. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  445. } else {
  446. rrdset_isnot_obsolete(st);
  447. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  448. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  449. }
  450. }
  451. pluginsd_set_chart_from_parent(user, st, PLUGINSD_KEYWORD_CHART);
  452. return PARSER_RC_OK;
  453. }
  454. PARSER_RC pluginsd_chart_definition_end(char **words, size_t num_words, void *user)
  455. {
  456. const char *first_entry_txt = get_word(words, num_words, 1);
  457. const char *last_entry_txt = get_word(words, num_words, 2);
  458. const char *wall_clock_time_txt = get_word(words, num_words, 3);
  459. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_CHART_DEFINITION_END);
  460. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  461. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_CHART_DEFINITION_END, PLUGINSD_KEYWORD_CHART);
  462. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  463. time_t first_entry_child = (first_entry_txt && *first_entry_txt) ? (time_t)str2ul(first_entry_txt) : 0;
  464. time_t last_entry_child = (last_entry_txt && *last_entry_txt) ? (time_t)str2ul(last_entry_txt) : 0;
  465. time_t child_wall_clock_time = (wall_clock_time_txt && *wall_clock_time_txt) ? (time_t)str2ul(wall_clock_time_txt) : now_realtime_sec();
  466. bool ok = true;
  467. if(!rrdset_flag_check(st, RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS)) {
  468. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  469. st->replay.start_streaming = false;
  470. st->replay.after = 0;
  471. st->replay.before = 0;
  472. #endif
  473. rrdset_flag_set(st, RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS);
  474. rrdset_flag_clear(st, RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED);
  475. rrdhost_receiver_replicating_charts_plus_one(st->rrdhost);
  476. PARSER *parser = ((PARSER_USER_OBJECT *)user)->parser;
  477. ok = replicate_chart_request(send_to_plugin, parser, host, st,
  478. first_entry_child, last_entry_child, child_wall_clock_time,
  479. 0, 0);
  480. }
  481. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  482. else {
  483. internal_error(true, "REPLAY: 'host:%s/chart:%s' not sending duplicate replication request",
  484. rrdhost_hostname(st->rrdhost), rrdset_id(st));
  485. }
  486. #endif
  487. return ok ? PARSER_RC_OK : PARSER_RC_ERROR;
  488. }
  489. PARSER_RC pluginsd_dimension(char **words, size_t num_words, void *user)
  490. {
  491. char *id = get_word(words, num_words, 1);
  492. char *name = get_word(words, num_words, 2);
  493. char *algorithm = get_word(words, num_words, 3);
  494. char *multiplier_s = get_word(words, num_words, 4);
  495. char *divisor_s = get_word(words, num_words, 5);
  496. char *options = get_word(words, num_words, 6);
  497. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_DIMENSION);
  498. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  499. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_DIMENSION, PLUGINSD_KEYWORD_CHART);
  500. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  501. if (unlikely(!id))
  502. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_DIMENSION, "missing dimension id");
  503. long multiplier = 1;
  504. if (multiplier_s && *multiplier_s) {
  505. multiplier = str2ll_encoded(multiplier_s);
  506. if (unlikely(!multiplier))
  507. multiplier = 1;
  508. }
  509. long divisor = 1;
  510. if (likely(divisor_s && *divisor_s)) {
  511. divisor = str2ll_encoded(divisor_s);
  512. if (unlikely(!divisor))
  513. divisor = 1;
  514. }
  515. if (unlikely(!algorithm || !*algorithm))
  516. algorithm = "absolute";
  517. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  518. debug(
  519. D_PLUGINSD,
  520. "creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'",
  521. rrdset_id(st), id, name ? name : "", rrd_algorithm_name(rrd_algorithm_id(algorithm)), multiplier, divisor,
  522. options ? options : "");
  523. RRDDIM *rd = rrddim_add(st, id, name, multiplier, divisor, rrd_algorithm_id(algorithm));
  524. int unhide_dimension = 1;
  525. rrddim_option_clear(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS);
  526. if (options && *options) {
  527. if (strstr(options, "obsolete") != NULL)
  528. rrddim_is_obsolete(st, rd);
  529. else
  530. rrddim_isnot_obsolete(st, rd);
  531. unhide_dimension = !strstr(options, "hidden");
  532. if (strstr(options, "noreset") != NULL)
  533. rrddim_option_set(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS);
  534. if (strstr(options, "nooverflow") != NULL)
  535. rrddim_option_set(rd, RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS);
  536. } else
  537. rrddim_isnot_obsolete(st, rd);
  538. bool should_update_dimension = false;
  539. if (likely(unhide_dimension)) {
  540. rrddim_option_clear(rd, RRDDIM_OPTION_HIDDEN);
  541. should_update_dimension = rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN);
  542. }
  543. else {
  544. rrddim_option_set(rd, RRDDIM_OPTION_HIDDEN);
  545. should_update_dimension = !rrddim_flag_check(rd, RRDDIM_FLAG_META_HIDDEN);
  546. }
  547. if (should_update_dimension) {
  548. rrddim_flag_set(rd, RRDDIM_FLAG_METADATA_UPDATE);
  549. rrdhost_flag_set(rd->rrdset->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  550. }
  551. return PARSER_RC_OK;
  552. }
  553. // ----------------------------------------------------------------------------
  554. // execution of functions
  555. struct inflight_function {
  556. int code;
  557. int timeout;
  558. BUFFER *destination_wb;
  559. STRING *function;
  560. void (*callback)(BUFFER *wb, int code, void *callback_data);
  561. void *callback_data;
  562. usec_t timeout_ut;
  563. usec_t started_ut;
  564. usec_t sent_ut;
  565. };
  566. static void inflight_functions_insert_callback(const DICTIONARY_ITEM *item, void *func, void *parser_ptr) {
  567. struct inflight_function *pf = func;
  568. PARSER *parser = parser_ptr;
  569. // leave this code as default, so that when the dictionary is destroyed this will be sent back to the caller
  570. pf->code = HTTP_RESP_GATEWAY_TIMEOUT;
  571. char buffer[2048 + 1];
  572. snprintfz(buffer, 2048, "FUNCTION %s %d \"%s\"\n",
  573. dictionary_acquired_item_name(item),
  574. pf->timeout,
  575. string2str(pf->function));
  576. // send the command to the plugin
  577. int ret = send_to_plugin(buffer, parser);
  578. pf->sent_ut = now_realtime_usec();
  579. if(ret < 0) {
  580. error("FUNCTION: failed to send function to plugin, error %d", ret);
  581. rrd_call_function_error(pf->destination_wb, "Failed to communicate with collector", HTTP_RESP_BACKEND_FETCH_FAILED);
  582. }
  583. else {
  584. internal_error(LOG_FUNCTIONS,
  585. "FUNCTION '%s' with transaction '%s' sent to collector (%d bytes, in %llu usec)",
  586. string2str(pf->function), dictionary_acquired_item_name(item), ret,
  587. pf->sent_ut - pf->started_ut);
  588. }
  589. }
  590. static bool inflight_functions_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func __maybe_unused, void *new_func, void *parser_ptr __maybe_unused) {
  591. struct inflight_function *pf = new_func;
  592. error("PLUGINSD_PARSER: duplicate UUID on pending function '%s' detected. Ignoring the second one.", string2str(pf->function));
  593. pf->code = rrd_call_function_error(pf->destination_wb, "This request is already in progress", HTTP_RESP_BAD_REQUEST);
  594. pf->callback(pf->destination_wb, pf->code, pf->callback_data);
  595. string_freez(pf->function);
  596. return false;
  597. }
  598. static void inflight_functions_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *func, void *parser_ptr __maybe_unused) {
  599. struct inflight_function *pf = func;
  600. internal_error(LOG_FUNCTIONS,
  601. "FUNCTION '%s' result of transaction '%s' received from collector (%zu bytes, request %llu usec, response %llu usec)",
  602. string2str(pf->function), dictionary_acquired_item_name(item),
  603. buffer_strlen(pf->destination_wb), pf->sent_ut - pf->started_ut, now_realtime_usec() - pf->sent_ut);
  604. pf->callback(pf->destination_wb, pf->code, pf->callback_data);
  605. string_freez(pf->function);
  606. }
  607. void inflight_functions_init(PARSER *parser) {
  608. parser->inflight.functions = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE, &dictionary_stats_category_functions, 0);
  609. dictionary_register_insert_callback(parser->inflight.functions, inflight_functions_insert_callback, parser);
  610. dictionary_register_delete_callback(parser->inflight.functions, inflight_functions_delete_callback, parser);
  611. dictionary_register_conflict_callback(parser->inflight.functions, inflight_functions_conflict_callback, parser);
  612. }
  613. static void inflight_functions_garbage_collect(PARSER *parser, usec_t now) {
  614. parser->inflight.smaller_timeout = 0;
  615. struct inflight_function *pf;
  616. dfe_start_write(parser->inflight.functions, pf) {
  617. if (pf->timeout_ut < now) {
  618. internal_error(true,
  619. "FUNCTION '%s' removing expired transaction '%s', after %llu usec.",
  620. string2str(pf->function), pf_dfe.name, now - pf->started_ut);
  621. if(!buffer_strlen(pf->destination_wb) || pf->code == HTTP_RESP_OK)
  622. pf->code = rrd_call_function_error(pf->destination_wb,
  623. "Timeout waiting for collector response.",
  624. HTTP_RESP_GATEWAY_TIMEOUT);
  625. dictionary_del(parser->inflight.functions, pf_dfe.name);
  626. }
  627. else if(!parser->inflight.smaller_timeout || pf->timeout_ut < parser->inflight.smaller_timeout)
  628. parser->inflight.smaller_timeout = pf->timeout_ut;
  629. }
  630. dfe_done(pf);
  631. }
  632. // this is the function that is called from
  633. // rrd_call_function_and_wait() and rrd_call_function_async()
  634. static int pluginsd_execute_function_callback(BUFFER *destination_wb, int timeout, const char *function, void *collector_data, void (*callback)(BUFFER *wb, int code, void *callback_data), void *callback_data) {
  635. PARSER *parser = collector_data;
  636. usec_t now = now_realtime_usec();
  637. struct inflight_function tmp = {
  638. .started_ut = now,
  639. .timeout_ut = now + timeout * USEC_PER_SEC,
  640. .destination_wb = destination_wb,
  641. .timeout = timeout,
  642. .function = string_strdupz(function),
  643. .callback = callback,
  644. .callback_data = callback_data,
  645. };
  646. uuid_t uuid;
  647. uuid_generate_time(uuid);
  648. char key[UUID_STR_LEN];
  649. uuid_unparse_lower(uuid, key);
  650. dictionary_write_lock(parser->inflight.functions);
  651. // if there is any error, our dictionary callbacks will call the caller callback to notify
  652. // the caller about the error - no need for error handling here.
  653. dictionary_set(parser->inflight.functions, key, &tmp, sizeof(struct inflight_function));
  654. if(!parser->inflight.smaller_timeout || tmp.timeout_ut < parser->inflight.smaller_timeout)
  655. parser->inflight.smaller_timeout = tmp.timeout_ut;
  656. // garbage collect stale inflight functions
  657. if(parser->inflight.smaller_timeout < now)
  658. inflight_functions_garbage_collect(parser, now);
  659. dictionary_write_unlock(parser->inflight.functions);
  660. return HTTP_RESP_OK;
  661. }
  662. PARSER_RC pluginsd_function(char **words, size_t num_words, void *user)
  663. {
  664. bool global = false;
  665. size_t i = 1;
  666. if(num_words >= 2 && strcmp(get_word(words, num_words, 1), "GLOBAL") == 0) {
  667. i++;
  668. global = true;
  669. }
  670. char *name = get_word(words, num_words, i++);
  671. char *timeout_s = get_word(words, num_words, i++);
  672. char *help = get_word(words, num_words, i++);
  673. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_FUNCTION);
  674. if(!host) return PARSER_RC_ERROR;
  675. RRDSET *st = (global)?NULL:pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_FUNCTION, PLUGINSD_KEYWORD_CHART);
  676. if(!st) global = true;
  677. if (unlikely(!timeout_s || !name || !help || (!global && !st))) {
  678. error("PLUGINSD: 'host:%s/chart:%s' got a FUNCTION, without providing the required data (global = '%s', name = '%s', timeout = '%s', help = '%s'). Ignoring it.",
  679. rrdhost_hostname(host),
  680. st?rrdset_id(st):"(unset)",
  681. global?"yes":"no",
  682. name?name:"(unset)",
  683. timeout_s?timeout_s:"(unset)",
  684. help?help:"(unset)"
  685. );
  686. return PARSER_RC_ERROR;
  687. }
  688. int timeout = PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT;
  689. if (timeout_s && *timeout_s) {
  690. timeout = str2i(timeout_s);
  691. if (unlikely(timeout <= 0))
  692. timeout = PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT;
  693. }
  694. PARSER *parser = ((PARSER_USER_OBJECT *) user)->parser;
  695. rrd_collector_add_function(host, st, name, timeout, help, false, pluginsd_execute_function_callback, parser);
  696. return PARSER_RC_OK;
  697. }
  698. static void pluginsd_function_result_end(struct parser *parser, void *action_data) {
  699. STRING *key = action_data;
  700. if(key)
  701. dictionary_del(parser->inflight.functions, string2str(key));
  702. string_freez(key);
  703. }
  704. PARSER_RC pluginsd_function_result_begin(char **words, size_t num_words, void *user)
  705. {
  706. char *key = get_word(words, num_words, 1);
  707. char *status = get_word(words, num_words, 2);
  708. char *format = get_word(words, num_words, 3);
  709. char *expires = get_word(words, num_words, 4);
  710. if (unlikely(!key || !*key || !status || !*status || !format || !*format || !expires || !*expires)) {
  711. error("got a " PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " without providing the required data (key = '%s', status = '%s', format = '%s', expires = '%s')."
  712. , key ? key : "(unset)"
  713. , status ? status : "(unset)"
  714. , format ? format : "(unset)"
  715. , expires ? expires : "(unset)"
  716. );
  717. }
  718. int code = (status && *status) ? str2i(status) : 0;
  719. if (code <= 0)
  720. code = HTTP_RESP_BACKEND_RESPONSE_INVALID;
  721. time_t expiration = (expires && *expires) ? str2l(expires) : 0;
  722. PARSER *parser = ((PARSER_USER_OBJECT *) user)->parser;
  723. struct inflight_function *pf = NULL;
  724. if(key && *key)
  725. pf = (struct inflight_function *)dictionary_get(parser->inflight.functions, key);
  726. if(!pf) {
  727. error("got a " PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " for transaction '%s', but the transaction is not found.", key?key:"(unset)");
  728. }
  729. else {
  730. if(format && *format)
  731. pf->destination_wb->content_type = functions_format_to_content_type(format);
  732. pf->code = code;
  733. pf->destination_wb->expires = expiration;
  734. if(expiration <= now_realtime_sec())
  735. buffer_no_cacheable(pf->destination_wb);
  736. else
  737. buffer_cacheable(pf->destination_wb);
  738. }
  739. parser->defer.response = (pf) ? pf->destination_wb : NULL;
  740. parser->defer.end_keyword = PLUGINSD_KEYWORD_FUNCTION_RESULT_END;
  741. parser->defer.action = pluginsd_function_result_end;
  742. parser->defer.action_data = string_strdupz(key); // it is ok is key is NULL
  743. parser->flags |= PARSER_DEFER_UNTIL_KEYWORD;
  744. return PARSER_RC_OK;
  745. }
  746. // ----------------------------------------------------------------------------
  747. PARSER_RC pluginsd_variable(char **words, size_t num_words, void *user)
  748. {
  749. char *name = get_word(words, num_words, 1);
  750. char *value = get_word(words, num_words, 2);
  751. NETDATA_DOUBLE v;
  752. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_VARIABLE);
  753. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  754. RRDSET *st = pluginsd_get_chart_from_parent(user);
  755. int global = (st) ? 0 : 1;
  756. if (name && *name) {
  757. if ((strcmp(name, "GLOBAL") == 0 || strcmp(name, "HOST") == 0)) {
  758. global = 1;
  759. name = get_word(words, num_words, 2);
  760. value = get_word(words, num_words, 3);
  761. } else if ((strcmp(name, "LOCAL") == 0 || strcmp(name, "CHART") == 0)) {
  762. global = 0;
  763. name = get_word(words, num_words, 2);
  764. value = get_word(words, num_words, 3);
  765. }
  766. }
  767. if (unlikely(!name || !*name))
  768. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_VARIABLE, "missing variable name");
  769. if (unlikely(!value || !*value))
  770. value = NULL;
  771. if (unlikely(!value)) {
  772. error("PLUGINSD: 'host:%s/chart:%s' cannot set %s VARIABLE '%s' to an empty value",
  773. rrdhost_hostname(host),
  774. st ? rrdset_id(st):"UNSET",
  775. (global) ? "HOST" : "CHART",
  776. name);
  777. return PARSER_RC_OK;
  778. }
  779. if (!global && !st)
  780. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_VARIABLE, "no chart is defined and no GLOBAL is given");
  781. char *endptr = NULL;
  782. v = (NETDATA_DOUBLE) str2ndd_encoded(value, &endptr);
  783. if (unlikely(endptr && *endptr)) {
  784. if (endptr == value)
  785. error("PLUGINSD: 'host:%s/chart:%s' the value '%s' of VARIABLE '%s' cannot be parsed as a number",
  786. rrdhost_hostname(host),
  787. st ? rrdset_id(st):"UNSET",
  788. value,
  789. name);
  790. else
  791. error("PLUGINSD: 'host:%s/chart:%s' the value '%s' of VARIABLE '%s' has leftovers: '%s'",
  792. rrdhost_hostname(host),
  793. st ? rrdset_id(st):"UNSET",
  794. value,
  795. name,
  796. endptr);
  797. }
  798. if (global) {
  799. const RRDVAR_ACQUIRED *rva = rrdvar_custom_host_variable_add_and_acquire(host, name);
  800. if (rva) {
  801. rrdvar_custom_host_variable_set(host, rva, v);
  802. rrdvar_custom_host_variable_release(host, rva);
  803. }
  804. else
  805. error("PLUGINSD: 'host:%s' cannot find/create HOST VARIABLE '%s'",
  806. rrdhost_hostname(host),
  807. name);
  808. } else {
  809. const RRDSETVAR_ACQUIRED *rsa = rrdsetvar_custom_chart_variable_add_and_acquire(st, name);
  810. if (rsa) {
  811. rrdsetvar_custom_chart_variable_set(st, rsa, v);
  812. rrdsetvar_custom_chart_variable_release(st, rsa);
  813. }
  814. else
  815. error("PLUGINSD: 'host:%s/chart:%s' cannot find/create CHART VARIABLE '%s'",
  816. rrdhost_hostname(host), rrdset_id(st), name);
  817. }
  818. return PARSER_RC_OK;
  819. }
  820. PARSER_RC pluginsd_flush(char **words __maybe_unused, size_t num_words __maybe_unused, void *user)
  821. {
  822. debug(D_PLUGINSD, "requested a " PLUGINSD_KEYWORD_FLUSH);
  823. pluginsd_set_chart_from_parent(user, NULL, PLUGINSD_KEYWORD_FLUSH);
  824. ((PARSER_USER_OBJECT *) user)->replay.start_time = 0;
  825. ((PARSER_USER_OBJECT *) user)->replay.end_time = 0;
  826. ((PARSER_USER_OBJECT *) user)->replay.start_time_ut = 0;
  827. ((PARSER_USER_OBJECT *) user)->replay.end_time_ut = 0;
  828. return PARSER_RC_OK;
  829. }
  830. PARSER_RC pluginsd_disable(char **words __maybe_unused, size_t num_words __maybe_unused, void *user __maybe_unused)
  831. {
  832. info("PLUGINSD: plugin called DISABLE. Disabling it.");
  833. ((PARSER_USER_OBJECT *) user)->enabled = 0;
  834. return PARSER_RC_STOP;
  835. }
  836. PARSER_RC pluginsd_label(char **words, size_t num_words, void *user)
  837. {
  838. const char *name = get_word(words, num_words, 1);
  839. const char *label_source = get_word(words, num_words, 2);
  840. const char *value = get_word(words, num_words, 3);
  841. if (!name || !label_source || !value)
  842. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_LABEL, "missing parameters");
  843. char *store = (char *)value;
  844. bool allocated_store = false;
  845. if(unlikely(num_words > 4)) {
  846. allocated_store = true;
  847. store = mallocz(PLUGINSD_LINE_MAX + 1);
  848. size_t remaining = PLUGINSD_LINE_MAX;
  849. char *move = store;
  850. char *word;
  851. for(size_t i = 3; i < num_words && remaining > 2 && (word = get_word(words, num_words, i)) ;i++) {
  852. if(i > 3) {
  853. *move++ = ' ';
  854. *move = '\0';
  855. remaining--;
  856. }
  857. size_t length = strlen(word);
  858. if (length > remaining)
  859. length = remaining;
  860. remaining -= length;
  861. memcpy(move, word, length);
  862. move += length;
  863. *move = '\0';
  864. }
  865. }
  866. if(unlikely(!((PARSER_USER_OBJECT *) user)->new_host_labels))
  867. ((PARSER_USER_OBJECT *) user)->new_host_labels = rrdlabels_create();
  868. rrdlabels_add(((PARSER_USER_OBJECT *)user)->new_host_labels,
  869. name,
  870. store,
  871. str2l(label_source));
  872. if (allocated_store)
  873. freez(store);
  874. return PARSER_RC_OK;
  875. }
  876. PARSER_RC pluginsd_overwrite(char **words __maybe_unused, size_t num_words __maybe_unused, void *user)
  877. {
  878. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_OVERWRITE);
  879. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  880. debug(D_PLUGINSD, "requested to OVERWRITE host labels");
  881. if(unlikely(!host->rrdlabels))
  882. host->rrdlabels = rrdlabels_create();
  883. rrdlabels_migrate_to_these(host->rrdlabels, (DICTIONARY *) (((PARSER_USER_OBJECT *)user)->new_host_labels));
  884. rrdhost_flag_set(host, RRDHOST_FLAG_METADATA_LABELS | RRDHOST_FLAG_METADATA_UPDATE);
  885. rrdlabels_destroy(((PARSER_USER_OBJECT *)user)->new_host_labels);
  886. ((PARSER_USER_OBJECT *)user)->new_host_labels = NULL;
  887. return PARSER_RC_OK;
  888. }
  889. PARSER_RC pluginsd_clabel(char **words, size_t num_words, void *user)
  890. {
  891. const char *name = get_word(words, num_words, 1);
  892. const char *value = get_word(words, num_words, 2);
  893. const char *label_source = get_word(words, num_words, 3);
  894. if (!name || !value || !*label_source) {
  895. error("Ignoring malformed or empty CHART LABEL command.");
  896. return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  897. }
  898. if(unlikely(!((PARSER_USER_OBJECT *) user)->chart_rrdlabels_linked_temporarily)) {
  899. RRDSET *st = pluginsd_get_chart_from_parent(user);
  900. ((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily = st->rrdlabels;
  901. rrdlabels_unmark_all(((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily);
  902. }
  903. rrdlabels_add(((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily,
  904. name, value, str2l(label_source));
  905. return PARSER_RC_OK;
  906. }
  907. PARSER_RC pluginsd_clabel_commit(char **words __maybe_unused, size_t num_words __maybe_unused, void *user)
  908. {
  909. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_CLABEL_COMMIT);
  910. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  911. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_CLABEL_COMMIT, PLUGINSD_KEYWORD_BEGIN);
  912. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  913. debug(D_PLUGINSD, "requested to commit chart labels");
  914. if(!((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily) {
  915. error("PLUGINSD: 'host:%s' got CLABEL_COMMIT, without a CHART or BEGIN. Ignoring it.",
  916. rrdhost_hostname(host));
  917. return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  918. }
  919. rrdlabels_remove_all_unmarked(((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily);
  920. rrdset_flag_set(st, RRDSET_FLAG_METADATA_UPDATE);
  921. rrdhost_flag_set(st->rrdhost, RRDHOST_FLAG_METADATA_UPDATE);
  922. ((PARSER_USER_OBJECT *)user)->chart_rrdlabels_linked_temporarily = NULL;
  923. return PARSER_RC_OK;
  924. }
  925. PARSER_RC pluginsd_replay_begin(char **words, size_t num_words, void *user) {
  926. char *id = get_word(words, num_words, 1);
  927. char *start_time_str = get_word(words, num_words, 2);
  928. char *end_time_str = get_word(words, num_words, 3);
  929. char *child_now_str = get_word(words, num_words, 4);
  930. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  931. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  932. RRDSET *st;
  933. if (likely(!id || !*id))
  934. st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_REPLAY_BEGIN, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  935. else
  936. st = pluginsd_find_chart(host, id, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  937. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  938. pluginsd_set_chart_from_parent(user, st, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  939. if(start_time_str && end_time_str) {
  940. time_t start_time = (time_t) str2ull_encoded(start_time_str);
  941. time_t end_time = (time_t) str2ull_encoded(end_time_str);
  942. time_t wall_clock_time = 0, tolerance;
  943. bool wall_clock_comes_from_child; (void)wall_clock_comes_from_child;
  944. if(child_now_str) {
  945. wall_clock_time = (time_t) str2ull_encoded(child_now_str);
  946. tolerance = st->update_every + 1;
  947. wall_clock_comes_from_child = true;
  948. }
  949. if(wall_clock_time <= 0) {
  950. wall_clock_time = now_realtime_sec();
  951. tolerance = st->update_every + 5;
  952. wall_clock_comes_from_child = false;
  953. }
  954. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  955. internal_error(
  956. (!st->replay.start_streaming && (end_time < st->replay.after || start_time > st->replay.before)),
  957. "REPLAY ERROR: 'host:%s/chart:%s' got a " PLUGINSD_KEYWORD_REPLAY_BEGIN " from %ld to %ld, which does not match our request (%ld to %ld).",
  958. rrdhost_hostname(st->rrdhost), rrdset_id(st), start_time, end_time, st->replay.after, st->replay.before);
  959. internal_error(
  960. true,
  961. "REPLAY: 'host:%s/chart:%s' got a " PLUGINSD_KEYWORD_REPLAY_BEGIN " from %ld to %ld, child wall clock is %ld (%s), had requested %ld to %ld",
  962. rrdhost_hostname(st->rrdhost), rrdset_id(st),
  963. start_time, end_time, wall_clock_time, wall_clock_comes_from_child ? "from child" : "parent time",
  964. st->replay.after, st->replay.before);
  965. #endif
  966. if(start_time && end_time && start_time < wall_clock_time + tolerance && end_time < wall_clock_time + tolerance && start_time < end_time) {
  967. if (unlikely(end_time - start_time != st->update_every))
  968. rrdset_set_update_every_s(st, end_time - start_time);
  969. st->last_collected_time.tv_sec = end_time;
  970. st->last_collected_time.tv_usec = 0;
  971. st->last_updated.tv_sec = end_time;
  972. st->last_updated.tv_usec = 0;
  973. st->counter++;
  974. st->counter_done++;
  975. // these are only needed for db mode RAM, SAVE, MAP, ALLOC
  976. st->db.current_entry++;
  977. if(st->db.current_entry >= st->db.entries)
  978. st->db.current_entry -= st->db.entries;
  979. ((PARSER_USER_OBJECT *) user)->replay.start_time = start_time;
  980. ((PARSER_USER_OBJECT *) user)->replay.end_time = end_time;
  981. ((PARSER_USER_OBJECT *) user)->replay.start_time_ut = (usec_t) start_time * USEC_PER_SEC;
  982. ((PARSER_USER_OBJECT *) user)->replay.end_time_ut = (usec_t) end_time * USEC_PER_SEC;
  983. ((PARSER_USER_OBJECT *) user)->replay.wall_clock_time = wall_clock_time;
  984. ((PARSER_USER_OBJECT *) user)->replay.rset_enabled = true;
  985. return PARSER_RC_OK;
  986. }
  987. error("PLUGINSD REPLAY ERROR: 'host:%s/chart:%s' got a " PLUGINSD_KEYWORD_REPLAY_BEGIN
  988. " from %ld to %ld, but timestamps are invalid "
  989. "(now is %ld [%s], tolerance %ld). Ignoring " PLUGINSD_KEYWORD_REPLAY_SET,
  990. rrdhost_hostname(st->rrdhost), rrdset_id(st), start_time, end_time,
  991. wall_clock_time, wall_clock_comes_from_child ? "child wall clock" : "parent wall clock", tolerance);
  992. }
  993. // the child sends an RBEGIN without any parameters initially
  994. // setting rset_enabled to false, means the RSET should not store any metrics
  995. // to store metrics, the RBEGIN needs to have timestamps
  996. ((PARSER_USER_OBJECT *) user)->replay.start_time = 0;
  997. ((PARSER_USER_OBJECT *) user)->replay.end_time = 0;
  998. ((PARSER_USER_OBJECT *) user)->replay.start_time_ut = 0;
  999. ((PARSER_USER_OBJECT *) user)->replay.end_time_ut = 0;
  1000. ((PARSER_USER_OBJECT *) user)->replay.wall_clock_time = 0;
  1001. ((PARSER_USER_OBJECT *) user)->replay.rset_enabled = false;
  1002. return PARSER_RC_OK;
  1003. }
  1004. static inline SN_FLAGS pluginsd_parse_storage_number_flags(const char *flags_str) {
  1005. SN_FLAGS flags = SN_FLAG_NONE;
  1006. char c;
  1007. while ((c = *flags_str++)) {
  1008. switch (c) {
  1009. case 'A':
  1010. flags |= SN_FLAG_NOT_ANOMALOUS;
  1011. break;
  1012. case 'R':
  1013. flags |= SN_FLAG_RESET;
  1014. break;
  1015. case 'E':
  1016. flags = SN_EMPTY_SLOT;
  1017. return flags;
  1018. default:
  1019. internal_error(true, "Unknown SN_FLAGS flag '%c'", c);
  1020. break;
  1021. }
  1022. }
  1023. return flags;
  1024. }
  1025. PARSER_RC pluginsd_replay_set(char **words, size_t num_words, void *user)
  1026. {
  1027. char *dimension = get_word(words, num_words, 1);
  1028. char *value_str = get_word(words, num_words, 2);
  1029. char *flags_str = get_word(words, num_words, 3);
  1030. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_REPLAY_SET);
  1031. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1032. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_REPLAY_SET, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  1033. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1034. PARSER_USER_OBJECT *u = user;
  1035. if(!u->replay.rset_enabled) {
  1036. error_limit_static_thread_var(erl, 1, 0);
  1037. error_limit(&erl, "PLUGINSD: 'host:%s/chart:%s' got a %s but it is disabled by %s errors",
  1038. rrdhost_hostname(host), rrdset_id(st), PLUGINSD_KEYWORD_REPLAY_SET, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  1039. // we have to return OK here
  1040. return PARSER_RC_OK;
  1041. }
  1042. RRDDIM *rd = pluginsd_acquire_dimension(host, st, dimension, PLUGINSD_KEYWORD_REPLAY_SET);
  1043. if(!rd) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1044. if (unlikely(!u->replay.start_time || !u->replay.end_time)) {
  1045. error("PLUGINSD: 'host:%s/chart:%s/dim:%s' got a %s with invalid timestamps %ld to %ld from a %s. Disabling it.",
  1046. rrdhost_hostname(host),
  1047. rrdset_id(st),
  1048. dimension,
  1049. PLUGINSD_KEYWORD_REPLAY_SET,
  1050. u->replay.start_time,
  1051. u->replay.end_time,
  1052. PLUGINSD_KEYWORD_REPLAY_BEGIN);
  1053. return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1054. }
  1055. if (unlikely(!value_str || !*value_str))
  1056. value_str = "NAN";
  1057. if(unlikely(!flags_str))
  1058. flags_str = "";
  1059. if (likely(value_str)) {
  1060. RRDDIM_FLAGS rd_flags = rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE | RRDDIM_FLAG_ARCHIVED);
  1061. if(!(rd_flags & RRDDIM_FLAG_ARCHIVED)) {
  1062. NETDATA_DOUBLE value = str2ndd_encoded(value_str, NULL);
  1063. SN_FLAGS flags = pluginsd_parse_storage_number_flags(flags_str);
  1064. if (!netdata_double_isnumber(value) || (flags == SN_EMPTY_SLOT)) {
  1065. value = NAN;
  1066. flags = SN_EMPTY_SLOT;
  1067. }
  1068. rrddim_store_metric(rd, u->replay.end_time_ut, value, flags);
  1069. rd->last_collected_time.tv_sec = u->replay.end_time;
  1070. rd->last_collected_time.tv_usec = 0;
  1071. rd->collections_counter++;
  1072. }
  1073. else {
  1074. error_limit_static_global_var(erl, 1, 0);
  1075. error_limit(&erl, "PLUGINSD: 'host:%s/chart:%s/dim:%s' has the ARCHIVED flag set, but it is replicated. Ignoring data.",
  1076. rrdhost_hostname(st->rrdhost), rrdset_id(st), rrddim_name(rd));
  1077. }
  1078. }
  1079. return PARSER_RC_OK;
  1080. }
  1081. PARSER_RC pluginsd_replay_rrddim_collection_state(char **words, size_t num_words, void *user)
  1082. {
  1083. if(((PARSER_USER_OBJECT *) user)->replay.rset_enabled == false)
  1084. return PARSER_RC_OK;
  1085. char *dimension = get_word(words, num_words, 1);
  1086. char *last_collected_ut_str = get_word(words, num_words, 2);
  1087. char *last_collected_value_str = get_word(words, num_words, 3);
  1088. char *last_calculated_value_str = get_word(words, num_words, 4);
  1089. char *last_stored_value_str = get_word(words, num_words, 5);
  1090. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE);
  1091. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1092. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  1093. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1094. RRDDIM *rd = pluginsd_acquire_dimension(host, st, dimension, PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE);
  1095. if(!rd) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1096. usec_t dim_last_collected_ut = (usec_t)rd->last_collected_time.tv_sec * USEC_PER_SEC + (usec_t)rd->last_collected_time.tv_usec;
  1097. usec_t last_collected_ut = last_collected_ut_str ? str2ull_encoded(last_collected_ut_str) : 0;
  1098. if(last_collected_ut > dim_last_collected_ut) {
  1099. rd->last_collected_time.tv_sec = (time_t)(last_collected_ut / USEC_PER_SEC);
  1100. rd->last_collected_time.tv_usec = (last_collected_ut % USEC_PER_SEC);
  1101. }
  1102. rd->last_collected_value = last_collected_value_str ? str2ll_encoded(last_collected_value_str) : 0;
  1103. rd->last_calculated_value = last_calculated_value_str ? str2ndd_encoded(last_calculated_value_str, NULL) : 0;
  1104. rd->last_stored_value = last_stored_value_str ? str2ndd_encoded(last_stored_value_str, NULL) : 0.0;
  1105. return PARSER_RC_OK;
  1106. }
  1107. PARSER_RC pluginsd_replay_rrdset_collection_state(char **words, size_t num_words, void *user)
  1108. {
  1109. if(((PARSER_USER_OBJECT *) user)->replay.rset_enabled == false)
  1110. return PARSER_RC_OK;
  1111. char *last_collected_ut_str = get_word(words, num_words, 1);
  1112. char *last_updated_ut_str = get_word(words, num_words, 2);
  1113. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE);
  1114. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1115. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  1116. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1117. usec_t chart_last_collected_ut = (usec_t)st->last_collected_time.tv_sec * USEC_PER_SEC + (usec_t)st->last_collected_time.tv_usec;
  1118. usec_t last_collected_ut = last_collected_ut_str ? str2ull_encoded(last_collected_ut_str) : 0;
  1119. if(last_collected_ut > chart_last_collected_ut) {
  1120. st->last_collected_time.tv_sec = (time_t)(last_collected_ut / USEC_PER_SEC);
  1121. st->last_collected_time.tv_usec = (last_collected_ut % USEC_PER_SEC);
  1122. }
  1123. usec_t chart_last_updated_ut = (usec_t)st->last_updated.tv_sec * USEC_PER_SEC + (usec_t)st->last_updated.tv_usec;
  1124. usec_t last_updated_ut = last_updated_ut_str ? str2ull_encoded(last_updated_ut_str) : 0;
  1125. if(last_updated_ut > chart_last_updated_ut) {
  1126. st->last_updated.tv_sec = (time_t)(last_updated_ut / USEC_PER_SEC);
  1127. st->last_updated.tv_usec = (last_updated_ut % USEC_PER_SEC);
  1128. }
  1129. st->counter++;
  1130. st->counter_done++;
  1131. return PARSER_RC_OK;
  1132. }
  1133. PARSER_RC pluginsd_replay_end(char **words, size_t num_words, void *user)
  1134. {
  1135. if (num_words < 7) { // accepts 7, but the 7th is optional
  1136. error("REPLAY: malformed " PLUGINSD_KEYWORD_REPLAY_END " command");
  1137. return PARSER_RC_ERROR;
  1138. }
  1139. const char *update_every_child_txt = get_word(words, num_words, 1);
  1140. const char *first_entry_child_txt = get_word(words, num_words, 2);
  1141. const char *last_entry_child_txt = get_word(words, num_words, 3);
  1142. const char *start_streaming_txt = get_word(words, num_words, 4);
  1143. const char *first_entry_requested_txt = get_word(words, num_words, 5);
  1144. const char *last_entry_requested_txt = get_word(words, num_words, 6);
  1145. const char *child_world_time_txt = get_word(words, num_words, 7); // optional
  1146. time_t update_every_child = (time_t) str2ull_encoded(update_every_child_txt);
  1147. time_t first_entry_child = (time_t) str2ull_encoded(first_entry_child_txt);
  1148. time_t last_entry_child = (time_t) str2ull_encoded(last_entry_child_txt);
  1149. bool start_streaming = (strcmp(start_streaming_txt, "true") == 0);
  1150. time_t first_entry_requested = (time_t) str2ull_encoded(first_entry_requested_txt);
  1151. time_t last_entry_requested = (time_t) str2ull_encoded(last_entry_requested_txt);
  1152. // the optional child world time
  1153. time_t child_world_time = (child_world_time_txt && *child_world_time_txt) ? (time_t) str2ull_encoded(
  1154. child_world_time_txt) : now_realtime_sec();
  1155. PARSER_USER_OBJECT *user_object = user;
  1156. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_REPLAY_END);
  1157. if(!host) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1158. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_REPLAY_END, PLUGINSD_KEYWORD_REPLAY_BEGIN);
  1159. if(!st) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1160. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  1161. internal_error(true,
  1162. "PLUGINSD REPLAY: 'host:%s/chart:%s': got a " PLUGINSD_KEYWORD_REPLAY_END " child db from %llu to %llu, start_streaming %s, had requested from %llu to %llu, wall clock %llu",
  1163. rrdhost_hostname(host), rrdset_id(st),
  1164. (unsigned long long)first_entry_child, (unsigned long long)last_entry_child,
  1165. start_streaming?"true":"false",
  1166. (unsigned long long)first_entry_requested, (unsigned long long)last_entry_requested,
  1167. (unsigned long long)child_world_time
  1168. );
  1169. #endif
  1170. ((PARSER_USER_OBJECT *) user)->data_collections_count++;
  1171. if(((PARSER_USER_OBJECT *) user)->replay.rset_enabled && st->rrdhost->receiver) {
  1172. time_t now = now_realtime_sec();
  1173. time_t started = st->rrdhost->receiver->replication_first_time_t;
  1174. time_t current = ((PARSER_USER_OBJECT *) user)->replay.end_time;
  1175. if(started && current > started) {
  1176. host->rrdpush_receiver_replication_percent = (NETDATA_DOUBLE) (current - started) * 100.0 / (NETDATA_DOUBLE) (now - started);
  1177. worker_set_metric(WORKER_RECEIVER_JOB_REPLICATION_COMPLETION,
  1178. host->rrdpush_receiver_replication_percent);
  1179. }
  1180. }
  1181. ((PARSER_USER_OBJECT *) user)->replay.start_time = 0;
  1182. ((PARSER_USER_OBJECT *) user)->replay.end_time = 0;
  1183. ((PARSER_USER_OBJECT *) user)->replay.start_time_ut = 0;
  1184. ((PARSER_USER_OBJECT *) user)->replay.end_time_ut = 0;
  1185. ((PARSER_USER_OBJECT *) user)->replay.wall_clock_time = 0;
  1186. ((PARSER_USER_OBJECT *) user)->replay.rset_enabled = false;
  1187. st->counter++;
  1188. st->counter_done++;
  1189. store_metric_collection_completed();
  1190. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  1191. st->replay.start_streaming = false;
  1192. st->replay.after = 0;
  1193. st->replay.before = 0;
  1194. if(start_streaming)
  1195. st->replay.log_next_data_collection = true;
  1196. #endif
  1197. if (start_streaming) {
  1198. if (st->update_every != update_every_child)
  1199. rrdset_set_update_every_s(st, update_every_child);
  1200. if(rrdset_flag_check(st, RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS)) {
  1201. rrdset_flag_set(st, RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED);
  1202. rrdset_flag_clear(st, RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS);
  1203. rrdset_flag_clear(st, RRDSET_FLAG_SYNC_CLOCK);
  1204. rrdhost_receiver_replicating_charts_minus_one(st->rrdhost);
  1205. }
  1206. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  1207. else
  1208. internal_error(true, "REPLAY ERROR: 'host:%s/chart:%s' got a " PLUGINSD_KEYWORD_REPLAY_END " with enable_streaming = true, but there is no replication in progress for this chart.",
  1209. rrdhost_hostname(host), rrdset_id(st));
  1210. #endif
  1211. pluginsd_set_chart_from_parent(user, NULL, PLUGINSD_KEYWORD_REPLAY_END);
  1212. host->rrdpush_receiver_replication_percent = 100.0;
  1213. worker_set_metric(WORKER_RECEIVER_JOB_REPLICATION_COMPLETION, host->rrdpush_receiver_replication_percent);
  1214. return PARSER_RC_OK;
  1215. }
  1216. pluginsd_set_chart_from_parent(user, NULL, PLUGINSD_KEYWORD_REPLAY_END);
  1217. rrdcontext_updated_retention_rrdset(st);
  1218. bool ok = replicate_chart_request(send_to_plugin, user_object->parser, host, st,
  1219. first_entry_child, last_entry_child, child_world_time,
  1220. first_entry_requested, last_entry_requested);
  1221. return ok ? PARSER_RC_OK : PARSER_RC_ERROR;
  1222. }
  1223. PARSER_RC pluginsd_begin_v2(char **words, size_t num_words, void *user) {
  1224. timing_init();
  1225. char *id = get_word(words, num_words, 1);
  1226. char *update_every_str = get_word(words, num_words, 2);
  1227. char *end_time_str = get_word(words, num_words, 3);
  1228. char *wall_clock_time_str = get_word(words, num_words, 4);
  1229. if(unlikely(!id || !update_every_str || !end_time_str || !wall_clock_time_str))
  1230. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_BEGIN_V2, "missing parameters");
  1231. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_BEGIN_V2);
  1232. if(unlikely(!host)) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1233. timing_step(TIMING_STEP_BEGIN2_PREPARE);
  1234. RRDSET *st = pluginsd_find_chart(host, id, PLUGINSD_KEYWORD_BEGIN_V2);
  1235. if(unlikely(!st)) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1236. pluginsd_set_chart_from_parent(user, st, PLUGINSD_KEYWORD_BEGIN_V2);
  1237. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE | RRDSET_FLAG_ARCHIVED)))
  1238. rrdset_isnot_obsolete(st);
  1239. timing_step(TIMING_STEP_BEGIN2_FIND_CHART);
  1240. // ------------------------------------------------------------------------
  1241. // parse the parameters
  1242. time_t update_every = (time_t) str2ull_encoded(update_every_str);
  1243. time_t end_time = (time_t) str2ull_encoded(end_time_str);
  1244. time_t wall_clock_time;
  1245. if(likely(*wall_clock_time_str == '#'))
  1246. wall_clock_time = end_time;
  1247. else
  1248. wall_clock_time = (time_t) str2ull_encoded(wall_clock_time_str);
  1249. if (unlikely(update_every != st->update_every))
  1250. rrdset_set_update_every_s(st, update_every);
  1251. timing_step(TIMING_STEP_BEGIN2_PARSE);
  1252. // ------------------------------------------------------------------------
  1253. // prepare our state
  1254. pluginsd_lock_rrdset_data_collection(user);
  1255. PARSER_USER_OBJECT *u = (PARSER_USER_OBJECT *) user;
  1256. u->v2.update_every = update_every;
  1257. u->v2.end_time = end_time;
  1258. u->v2.wall_clock_time = wall_clock_time;
  1259. u->v2.ml_locked = ml_chart_update_begin(st);
  1260. timing_step(TIMING_STEP_BEGIN2_ML);
  1261. // ------------------------------------------------------------------------
  1262. // propagate it forward in v2
  1263. if(!u->v2.stream_buffer.wb && rrdhost_has_rrdpush_sender_enabled(st->rrdhost))
  1264. u->v2.stream_buffer = rrdset_push_metric_initialize(u->st, wall_clock_time);
  1265. if(u->v2.stream_buffer.v2 && u->v2.stream_buffer.wb) {
  1266. // check if receiver and sender have the same number parsing capabilities
  1267. bool can_copy = stream_has_capability(u, STREAM_CAP_IEEE754) == stream_has_capability(&u->v2.stream_buffer, STREAM_CAP_IEEE754);
  1268. NUMBER_ENCODING encoding = stream_has_capability(&u->v2.stream_buffer, STREAM_CAP_IEEE754) ? NUMBER_ENCODING_BASE64 : NUMBER_ENCODING_HEX;
  1269. BUFFER *wb = u->v2.stream_buffer.wb;
  1270. buffer_need_bytes(wb, 1024);
  1271. if(unlikely(u->v2.stream_buffer.begin_v2_added))
  1272. buffer_fast_strcat(wb, PLUGINSD_KEYWORD_END_V2 "\n", sizeof(PLUGINSD_KEYWORD_END_V2) - 1 + 1);
  1273. buffer_fast_strcat(wb, PLUGINSD_KEYWORD_BEGIN_V2 " '", sizeof(PLUGINSD_KEYWORD_BEGIN_V2) - 1 + 2);
  1274. buffer_fast_strcat(wb, rrdset_id(st), string_strlen(st->id));
  1275. buffer_fast_strcat(wb, "' ", 2);
  1276. if(can_copy)
  1277. buffer_strcat(wb, update_every_str);
  1278. else
  1279. buffer_print_uint64_encoded(wb, encoding, update_every);
  1280. buffer_fast_strcat(wb, " ", 1);
  1281. if(can_copy)
  1282. buffer_strcat(wb, end_time_str);
  1283. else
  1284. buffer_print_uint64_encoded(wb, encoding, end_time);
  1285. buffer_fast_strcat(wb, " ", 1);
  1286. if(can_copy)
  1287. buffer_strcat(wb, wall_clock_time_str);
  1288. else
  1289. buffer_print_uint64_encoded(wb, encoding, wall_clock_time);
  1290. buffer_fast_strcat(wb, "\n", 1);
  1291. u->v2.stream_buffer.last_point_end_time_s = end_time;
  1292. u->v2.stream_buffer.begin_v2_added = true;
  1293. }
  1294. timing_step(TIMING_STEP_BEGIN2_PROPAGATE);
  1295. // ------------------------------------------------------------------------
  1296. // store it
  1297. st->last_collected_time.tv_sec = end_time;
  1298. st->last_collected_time.tv_usec = 0;
  1299. st->last_updated.tv_sec = end_time;
  1300. st->last_updated.tv_usec = 0;
  1301. st->counter++;
  1302. st->counter_done++;
  1303. // these are only needed for db mode RAM, SAVE, MAP, ALLOC
  1304. st->db.current_entry++;
  1305. if(st->db.current_entry >= st->db.entries)
  1306. st->db.current_entry -= st->db.entries;
  1307. timing_step(TIMING_STEP_BEGIN2_STORE);
  1308. return PARSER_RC_OK;
  1309. }
  1310. PARSER_RC pluginsd_set_v2(char **words, size_t num_words, void *user) {
  1311. timing_init();
  1312. char *dimension = get_word(words, num_words, 1);
  1313. char *collected_str = get_word(words, num_words, 2);
  1314. char *value_str = get_word(words, num_words, 3);
  1315. char *flags_str = get_word(words, num_words, 4);
  1316. if(unlikely(!dimension || !collected_str || !value_str || !flags_str))
  1317. return PLUGINSD_DISABLE_PLUGIN(user, PLUGINSD_KEYWORD_SET_V2, "missing parameters");
  1318. PARSER_USER_OBJECT *u = (PARSER_USER_OBJECT *) user;
  1319. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_SET_V2);
  1320. if(unlikely(!host)) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1321. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_SET_V2, PLUGINSD_KEYWORD_BEGIN_V2);
  1322. if(unlikely(!st)) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1323. timing_step(TIMING_STEP_SET2_PREPARE);
  1324. RRDDIM *rd = pluginsd_acquire_dimension(host, st, dimension, PLUGINSD_KEYWORD_SET_V2);
  1325. if(unlikely(!rd)) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1326. if(unlikely(rrddim_flag_check(rd, RRDDIM_FLAG_OBSOLETE | RRDDIM_FLAG_ARCHIVED)))
  1327. rrddim_isnot_obsolete(st, rd);
  1328. timing_step(TIMING_STEP_SET2_LOOKUP_DIMENSION);
  1329. // ------------------------------------------------------------------------
  1330. // parse the parameters
  1331. collected_number collected_value = (collected_number) str2ll_encoded(collected_str);
  1332. NETDATA_DOUBLE value;
  1333. if(*value_str == '#')
  1334. value = (NETDATA_DOUBLE)collected_value;
  1335. else
  1336. value = str2ndd_encoded(value_str, NULL);
  1337. SN_FLAGS flags = pluginsd_parse_storage_number_flags(flags_str);
  1338. timing_step(TIMING_STEP_SET2_PARSE);
  1339. // ------------------------------------------------------------------------
  1340. // check value and ML
  1341. if (unlikely(!netdata_double_isnumber(value) || (flags == SN_EMPTY_SLOT))) {
  1342. value = NAN;
  1343. flags = SN_EMPTY_SLOT;
  1344. if(u->v2.ml_locked)
  1345. ml_dimension_is_anomalous(rd, u->v2.end_time, 0, false);
  1346. }
  1347. else if(u->v2.ml_locked) {
  1348. if (ml_dimension_is_anomalous(rd, u->v2.end_time, value, true)) {
  1349. // clear anomaly bit: 0 -> is anomalous, 1 -> not anomalous
  1350. flags &= ~((storage_number) SN_FLAG_NOT_ANOMALOUS);
  1351. }
  1352. else
  1353. flags |= SN_FLAG_NOT_ANOMALOUS;
  1354. }
  1355. timing_step(TIMING_STEP_SET2_ML);
  1356. // ------------------------------------------------------------------------
  1357. // propagate it forward in v2
  1358. if(u->v2.stream_buffer.v2 && u->v2.stream_buffer.begin_v2_added && u->v2.stream_buffer.wb) {
  1359. // check if receiver and sender have the same number parsing capabilities
  1360. bool can_copy = stream_has_capability(u, STREAM_CAP_IEEE754) == stream_has_capability(&u->v2.stream_buffer, STREAM_CAP_IEEE754);
  1361. NUMBER_ENCODING integer_encoding = stream_has_capability(&u->v2.stream_buffer, STREAM_CAP_IEEE754) ? NUMBER_ENCODING_BASE64 : NUMBER_ENCODING_HEX;
  1362. NUMBER_ENCODING doubles_encoding = stream_has_capability(&u->v2.stream_buffer, STREAM_CAP_IEEE754) ? NUMBER_ENCODING_BASE64 : NUMBER_ENCODING_DECIMAL;
  1363. BUFFER *wb = u->v2.stream_buffer.wb;
  1364. buffer_need_bytes(wb, 1024);
  1365. buffer_fast_strcat(wb, PLUGINSD_KEYWORD_SET_V2 " '", sizeof(PLUGINSD_KEYWORD_SET_V2) - 1 + 2);
  1366. buffer_fast_strcat(wb, rrddim_id(rd), string_strlen(rd->id));
  1367. buffer_fast_strcat(wb, "' ", 2);
  1368. if(can_copy)
  1369. buffer_strcat(wb, collected_str);
  1370. else
  1371. buffer_print_int64_encoded(wb, integer_encoding, collected_value); // original v2 had hex
  1372. buffer_fast_strcat(wb, " ", 1);
  1373. if(can_copy)
  1374. buffer_strcat(wb, value_str);
  1375. else
  1376. buffer_print_netdata_double_encoded(wb, doubles_encoding, value); // original v2 had decimal
  1377. buffer_fast_strcat(wb, " ", 1);
  1378. buffer_print_sn_flags(wb, flags, true);
  1379. buffer_fast_strcat(wb, "\n", 1);
  1380. }
  1381. timing_step(TIMING_STEP_SET2_PROPAGATE);
  1382. // ------------------------------------------------------------------------
  1383. // store it
  1384. rrddim_store_metric(rd, u->v2.end_time * USEC_PER_SEC, value, flags);
  1385. rd->last_collected_time.tv_sec = u->v2.end_time;
  1386. rd->last_collected_time.tv_usec = 0;
  1387. rd->last_collected_value = collected_value;
  1388. rd->last_stored_value = value;
  1389. rd->last_calculated_value = value;
  1390. rd->collections_counter++;
  1391. rrddim_set_updated(rd);
  1392. timing_step(TIMING_STEP_SET2_STORE);
  1393. return PARSER_RC_OK;
  1394. }
  1395. void pluginsd_cleanup_v2(void *user) {
  1396. // this is called when the thread is stopped while processing
  1397. pluginsd_set_chart_from_parent(user, NULL, "THREAD CLEANUP");
  1398. }
  1399. PARSER_RC pluginsd_end_v2(char **words __maybe_unused, size_t num_words __maybe_unused, void *user) {
  1400. timing_init();
  1401. RRDHOST *host = pluginsd_require_host_from_parent(user, PLUGINSD_KEYWORD_END_V2);
  1402. if(unlikely(!host)) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1403. RRDSET *st = pluginsd_require_chart_from_parent(user, PLUGINSD_KEYWORD_END_V2, PLUGINSD_KEYWORD_BEGIN_V2);
  1404. if(unlikely(!st)) return PLUGINSD_DISABLE_PLUGIN(user, NULL, NULL);
  1405. PARSER_USER_OBJECT *u = (PARSER_USER_OBJECT *) user;
  1406. u->data_collections_count++;
  1407. timing_step(TIMING_STEP_END2_PREPARE);
  1408. // ------------------------------------------------------------------------
  1409. // propagate the whole chart update in v1
  1410. if(unlikely(!u->v2.stream_buffer.v2 && !u->v2.stream_buffer.begin_v2_added && u->v2.stream_buffer.wb))
  1411. rrdset_push_metrics_v1(&u->v2.stream_buffer, st);
  1412. timing_step(TIMING_STEP_END2_PUSH_V1);
  1413. // ------------------------------------------------------------------------
  1414. // unblock data collection
  1415. pluginsd_unlock_previous_chart(user, PLUGINSD_KEYWORD_END_V2, false);
  1416. rrdcontext_collected_rrdset(st);
  1417. store_metric_collection_completed();
  1418. timing_step(TIMING_STEP_END2_RRDSET);
  1419. // ------------------------------------------------------------------------
  1420. // propagate it forward
  1421. rrdset_push_metrics_finished(&u->v2.stream_buffer, st);
  1422. timing_step(TIMING_STEP_END2_PROPAGATE);
  1423. // ------------------------------------------------------------------------
  1424. // cleanup RRDSET / RRDDIM
  1425. RRDDIM *rd;
  1426. rrddim_foreach_read(rd, st) {
  1427. rd->calculated_value = 0;
  1428. rd->collected_value = 0;
  1429. rrddim_clear_updated(rd);
  1430. }
  1431. rrddim_foreach_done(rd);
  1432. // ------------------------------------------------------------------------
  1433. // reset state
  1434. u->v2 = (struct parser_user_object_v2){ 0 };
  1435. timing_step(TIMING_STEP_END2_STORE);
  1436. timing_report();
  1437. return PARSER_RC_OK;
  1438. }
  1439. void pluginsd_process_thread_cleanup(void *ptr) {
  1440. PARSER *parser = (PARSER *)ptr;
  1441. pluginsd_cleanup_v2(parser->user);
  1442. pluginsd_host_define_cleanup(parser->user);
  1443. rrd_collector_finished();
  1444. parser_destroy(parser);
  1445. }
  1446. // New plugins.d parser
  1447. inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp_plugin_input, FILE *fp_plugin_output, int trust_durations)
  1448. {
  1449. int enabled = cd->unsafe.enabled;
  1450. if (!fp_plugin_input || !fp_plugin_output || !enabled) {
  1451. cd->unsafe.enabled = 0;
  1452. return 0;
  1453. }
  1454. if (unlikely(fileno(fp_plugin_input) == -1)) {
  1455. error("input file descriptor given is not a valid stream");
  1456. cd->serial_failures++;
  1457. return 0;
  1458. }
  1459. if (unlikely(fileno(fp_plugin_output) == -1)) {
  1460. error("output file descriptor given is not a valid stream");
  1461. cd->serial_failures++;
  1462. return 0;
  1463. }
  1464. clearerr(fp_plugin_input);
  1465. clearerr(fp_plugin_output);
  1466. PARSER_USER_OBJECT user = {
  1467. .enabled = cd->unsafe.enabled,
  1468. .host = host,
  1469. .cd = cd,
  1470. .trust_durations = trust_durations
  1471. };
  1472. // fp_plugin_output = our input; fp_plugin_input = our output
  1473. PARSER *parser = parser_init(&user, fp_plugin_output, fp_plugin_input, -1,
  1474. PARSER_INPUT_SPLIT, NULL);
  1475. pluginsd_keywords_init(parser, PARSER_INIT_PLUGINSD);
  1476. rrd_collector_started();
  1477. // this keeps the parser with its current value
  1478. // so, parser needs to be allocated before pushing it
  1479. netdata_thread_cleanup_push(pluginsd_process_thread_cleanup, parser);
  1480. user.parser = parser;
  1481. char buffer[PLUGINSD_LINE_MAX + 1];
  1482. while (likely(!parser_next(parser, buffer, PLUGINSD_LINE_MAX))) {
  1483. if (unlikely(!service_running(SERVICE_COLLECTORS) || parser_action(parser, buffer)))
  1484. break;
  1485. }
  1486. // free parser with the pop function
  1487. netdata_thread_cleanup_pop(1);
  1488. cd->unsafe.enabled = user.enabled;
  1489. size_t count = user.data_collections_count;
  1490. if (likely(count)) {
  1491. cd->successful_collections += count;
  1492. cd->serial_failures = 0;
  1493. }
  1494. else
  1495. cd->serial_failures++;
  1496. return count;
  1497. }
  1498. PARSER_RC pluginsd_exit(char **words __maybe_unused, size_t num_words __maybe_unused, void *user __maybe_unused)
  1499. {
  1500. info("PLUGINSD: plugin called EXIT.");
  1501. return PARSER_RC_STOP;
  1502. }
  1503. static void pluginsd_keywords_init_internal(PARSER *parser, PLUGINSD_KEYWORDS types, void (*add_func)(PARSER *parser, char *keyword, keyword_function func)) {
  1504. if (types & PARSER_INIT_PLUGINSD) {
  1505. add_func(parser, PLUGINSD_KEYWORD_FLUSH, pluginsd_flush);
  1506. add_func(parser, PLUGINSD_KEYWORD_DISABLE, pluginsd_disable);
  1507. add_func(parser, PLUGINSD_KEYWORD_HOST_DEFINE, pluginsd_host_define);
  1508. add_func(parser, PLUGINSD_KEYWORD_HOST_DEFINE_END, pluginsd_host_define_end);
  1509. add_func(parser, PLUGINSD_KEYWORD_HOST_LABEL, pluginsd_host_labels);
  1510. add_func(parser, PLUGINSD_KEYWORD_HOST, pluginsd_host);
  1511. add_func(parser, PLUGINSD_KEYWORD_EXIT, pluginsd_exit);
  1512. }
  1513. if (types & (PARSER_INIT_PLUGINSD | PARSER_INIT_STREAMING)) {
  1514. // plugins.d plugins and streaming
  1515. add_func(parser, PLUGINSD_KEYWORD_CHART, pluginsd_chart);
  1516. add_func(parser, PLUGINSD_KEYWORD_DIMENSION, pluginsd_dimension);
  1517. add_func(parser, PLUGINSD_KEYWORD_VARIABLE, pluginsd_variable);
  1518. add_func(parser, PLUGINSD_KEYWORD_LABEL, pluginsd_label);
  1519. add_func(parser, PLUGINSD_KEYWORD_OVERWRITE, pluginsd_overwrite);
  1520. add_func(parser, PLUGINSD_KEYWORD_CLABEL_COMMIT, pluginsd_clabel_commit);
  1521. add_func(parser, PLUGINSD_KEYWORD_CLABEL, pluginsd_clabel);
  1522. add_func(parser, PLUGINSD_KEYWORD_FUNCTION, pluginsd_function);
  1523. add_func(parser, PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN, pluginsd_function_result_begin);
  1524. add_func(parser, PLUGINSD_KEYWORD_BEGIN, pluginsd_begin);
  1525. add_func(parser, PLUGINSD_KEYWORD_SET, pluginsd_set);
  1526. add_func(parser, PLUGINSD_KEYWORD_END, pluginsd_end);
  1527. inflight_functions_init(parser);
  1528. }
  1529. if (types & PARSER_INIT_STREAMING) {
  1530. add_func(parser, PLUGINSD_KEYWORD_CHART_DEFINITION_END, pluginsd_chart_definition_end);
  1531. // replication
  1532. add_func(parser, PLUGINSD_KEYWORD_REPLAY_BEGIN, pluginsd_replay_begin);
  1533. add_func(parser, PLUGINSD_KEYWORD_REPLAY_SET, pluginsd_replay_set);
  1534. add_func(parser, PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE, pluginsd_replay_rrddim_collection_state);
  1535. add_func(parser, PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE, pluginsd_replay_rrdset_collection_state);
  1536. add_func(parser, PLUGINSD_KEYWORD_REPLAY_END, pluginsd_replay_end);
  1537. // streaming metrics v2
  1538. add_func(parser, PLUGINSD_KEYWORD_BEGIN_V2, pluginsd_begin_v2);
  1539. add_func(parser, PLUGINSD_KEYWORD_SET_V2, pluginsd_set_v2);
  1540. add_func(parser, PLUGINSD_KEYWORD_END_V2, pluginsd_end_v2);
  1541. }
  1542. }
  1543. void pluginsd_keywords_init(PARSER *parser, PLUGINSD_KEYWORDS types) {
  1544. pluginsd_keywords_init_internal(parser, types, parser_add_keyword);
  1545. }
  1546. struct pluginsd_user_unittest {
  1547. size_t size;
  1548. const char **hashtable;
  1549. uint32_t (*hash)(const char *s);
  1550. size_t collisions;
  1551. };
  1552. void pluginsd_keyword_collision_check(PARSER *parser, char *keyword, keyword_function func __maybe_unused) {
  1553. struct pluginsd_user_unittest *u = parser->user;
  1554. uint32_t hash = u->hash(keyword);
  1555. uint32_t slot = hash % u->size;
  1556. if(u->hashtable[slot])
  1557. u->collisions++;
  1558. u->hashtable[slot] = keyword;
  1559. }
  1560. static struct {
  1561. const char *name;
  1562. uint32_t (*hash)(const char *s);
  1563. size_t slots_needed;
  1564. } hashers[] = {
  1565. { .name = "djb2_hash32(s)", djb2_hash32, .slots_needed = 0, },
  1566. { .name = "fnv1_hash32(s)", fnv1_hash32, .slots_needed = 0, },
  1567. { .name = "fnv1a_hash32(s)", fnv1a_hash32, .slots_needed = 0, },
  1568. { .name = "larson_hash32(s)", larson_hash32, .slots_needed = 0, },
  1569. { .name = "pluginsd_parser_hash32(s)", pluginsd_parser_hash32, .slots_needed = 0, },
  1570. // terminator
  1571. { .name = NULL, NULL, .slots_needed = 0, },
  1572. };
  1573. int pluginsd_parser_unittest(void) {
  1574. PARSER *p;
  1575. size_t slots_to_check = 1000;
  1576. size_t i, h;
  1577. // check for hashtable collisions
  1578. for(h = 0; hashers[h].name ;h++) {
  1579. hashers[h].slots_needed = slots_to_check * 1000000;
  1580. for (i = 10; i < slots_to_check; i++) {
  1581. struct pluginsd_user_unittest user = {
  1582. .hash = hashers[h].hash,
  1583. .size = i,
  1584. .hashtable = callocz(i, sizeof(const char *)),
  1585. .collisions = 0,
  1586. };
  1587. p = parser_init(&user, NULL, NULL, -1, PARSER_INPUT_SPLIT, NULL);
  1588. pluginsd_keywords_init_internal(p, PARSER_INIT_PLUGINSD | PARSER_INIT_STREAMING,
  1589. pluginsd_keyword_collision_check);
  1590. parser_destroy(p);
  1591. freez(user.hashtable);
  1592. if (!user.collisions) {
  1593. hashers[h].slots_needed = i;
  1594. break;
  1595. }
  1596. }
  1597. }
  1598. for(h = 0; hashers[h].name ;h++) {
  1599. if(hashers[h].slots_needed > 1000)
  1600. info("PARSER: hash function '%s' cannot be used without collisions under %zu slots", hashers[h].name, slots_to_check);
  1601. else
  1602. info("PARSER: hash function '%s' needs PARSER_KEYWORDS_HASHTABLE_SIZE (in parser.h) set to %zu", hashers[h].name, hashers[h].slots_needed);
  1603. }
  1604. p = parser_init(NULL, NULL, NULL, -1, PARSER_INPUT_SPLIT, NULL);
  1605. pluginsd_keywords_init(p, PARSER_INIT_PLUGINSD | PARSER_INIT_STREAMING);
  1606. parser_destroy(p);
  1607. return 0;
  1608. }