dyncfg-echo.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "dyncfg-internals.h"
  3. #include "dyncfg.h"
  4. // ----------------------------------------------------------------------------
  5. // echo is when we send requests to plugins without any caller
  6. // it is used for:
  7. // 1. the first enable/disable requests we send, and also
  8. // 2. updates to stock or user configurations
  9. // 3. saved dynamic jobs we need to add to templates
  10. struct dyncfg_echo {
  11. const DICTIONARY_ITEM *item;
  12. DYNCFG *df;
  13. BUFFER *wb;
  14. };
  15. void dyncfg_echo_cb(BUFFER *wb __maybe_unused, int code __maybe_unused, void *result_cb_data) {
  16. struct dyncfg_echo *e = result_cb_data;
  17. buffer_free(e->wb);
  18. dictionary_acquired_item_release(dyncfg_globals.nodes, e->item);
  19. e->wb = NULL;
  20. e->df = NULL;
  21. e->item = NULL;
  22. freez(e);
  23. }
  24. void dyncfg_echo(const DICTIONARY_ITEM *item, DYNCFG *df, const char *id __maybe_unused, DYNCFG_CMDS cmd) {
  25. if(!(df->cmds & cmd))
  26. return;
  27. const char *cmd_str = dyncfg_id2cmd_one(cmd);
  28. if(!cmd_str) {
  29. nd_log(NDLS_DAEMON, NDLP_ERR, "DYNCFG: command given does not resolve to a known command");
  30. return;
  31. }
  32. struct dyncfg_echo *e = callocz(1, sizeof(struct dyncfg_echo));
  33. e->item = dictionary_acquired_item_dup(dyncfg_globals.nodes, item);
  34. e->wb = buffer_create(0, NULL);
  35. e->df = df;
  36. char buf[string_strlen(df->function) + strlen(cmd_str) + 20];
  37. snprintfz(buf, sizeof(buf), "%s %s", string2str(df->function), cmd_str);
  38. rrd_function_run(df->host, e->wb, 10, HTTP_ACCESS_ADMIN, buf, false, NULL,
  39. dyncfg_echo_cb, e,
  40. NULL, NULL,
  41. NULL, NULL,
  42. NULL, NULL);
  43. }
  44. static void dyncfg_echo_payload(const DICTIONARY_ITEM *item, DYNCFG *df, const char *id __maybe_unused, const char *cmd) {
  45. if(!df->payload)
  46. return;
  47. struct dyncfg_echo *e = callocz(1, sizeof(struct dyncfg_echo));
  48. e->item = dictionary_acquired_item_dup(dyncfg_globals.nodes, item);
  49. e->wb = buffer_create(0, NULL);
  50. e->df = df;
  51. char buf[string_strlen(df->function) + strlen(cmd) + 20];
  52. snprintfz(buf, sizeof(buf), "%s %s", string2str(df->function), cmd);
  53. rrd_function_run(df->host, e->wb, 10, HTTP_ACCESS_ADMIN, buf, false, NULL,
  54. dyncfg_echo_cb, e,
  55. NULL, NULL,
  56. NULL, NULL,
  57. df->payload, NULL);
  58. }
  59. void dyncfg_echo_update(const DICTIONARY_ITEM *item, DYNCFG *df, const char *id) {
  60. dyncfg_echo_payload(item, df, id, "update");
  61. }
  62. void dyncfg_echo_add(const DICTIONARY_ITEM *template_item, DYNCFG *template_df, const char *template_id, const char *job_name) {
  63. char buf[strlen(job_name) + 20];
  64. snprintfz(buf, sizeof(buf), "add %s", job_name);
  65. dyncfg_echo_payload(template_item, template_df, template_id, buf);
  66. }