pluginsd_parser.c 76 KB

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