dyn_conf.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef DYN_CONF_H
  3. #define DYN_CONF_H
  4. #include "../libnetdata.h"
  5. enum module_type {
  6. MOD_TYPE_UNKNOWN = 0,
  7. MOD_TYPE_ARRAY,
  8. MOD_TYPE_SINGLE
  9. };
  10. static inline enum module_type str2_module_type(const char *type_name)
  11. {
  12. if (strcmp(type_name, "job_array") == 0)
  13. return MOD_TYPE_ARRAY;
  14. else if (strcmp(type_name, "single") == 0)
  15. return MOD_TYPE_SINGLE;
  16. return MOD_TYPE_UNKNOWN;
  17. }
  18. struct dyncfg_config {
  19. void *data;
  20. size_t data_size;
  21. };
  22. typedef struct dyncfg_config dyncfg_config_t;
  23. struct configurable_plugin;
  24. struct module;
  25. enum job_status {
  26. JOB_STATUS_UNKNOWN = 0, // State used until plugin reports first status
  27. JOB_STATUS_STOPPED,
  28. JOB_STATUS_RUNNING,
  29. JOB_STATUS_ERROR
  30. };
  31. inline enum job_status str2job_state(const char *state_name) {
  32. if (strcmp(state_name, "stopped") == 0)
  33. return JOB_STATUS_STOPPED;
  34. else if (strcmp(state_name, "running") == 0)
  35. return JOB_STATUS_RUNNING;
  36. else if (strcmp(state_name, "error") == 0)
  37. return JOB_STATUS_ERROR;
  38. return JOB_STATUS_UNKNOWN;
  39. }
  40. enum set_config_result {
  41. SET_CONFIG_ACCEPTED = 0,
  42. SET_CONFIG_REJECTED,
  43. SET_CONFIG_DEFFER
  44. };
  45. struct job
  46. {
  47. char *name;
  48. //state reported by config
  49. enum job_status status; // reported by plugin, enum as this has to be interpreted by UI
  50. int state; // code reported by plugin which can mean anything plugin wants
  51. char *reason; // reported by plugin, can be NULL (optional)
  52. usec_t last_state_update;
  53. struct module *module;
  54. };
  55. struct module
  56. {
  57. pthread_mutex_t lock;
  58. char *name;
  59. enum module_type type;
  60. struct configurable_plugin *plugin;
  61. // module config
  62. enum set_config_result (*set_config_cb)(void *usr_ctx, const char *module_name, dyncfg_config_t *cfg);
  63. dyncfg_config_t (*get_config_cb)(void *usr_ctx, const char *name);
  64. dyncfg_config_t (*get_config_schema_cb)(void *usr_ctx, const char *name);
  65. void *config_cb_usr_ctx;
  66. DICTIONARY *jobs;
  67. // jobs config
  68. dyncfg_config_t (*get_job_config_cb)(void *usr_ctx, const char *module_name, const char *job_name);
  69. dyncfg_config_t (*get_job_config_schema_cb)(void *usr_ctx, const char *module_name);
  70. enum set_config_result (*set_job_config_cb)(void *usr_ctx, const char *module_name, const char *job_name, dyncfg_config_t *cfg);
  71. enum set_config_result (*delete_job_cb)(void *usr_ctx, const char *module_name, const char *job_name);
  72. void *job_config_cb_usr_ctx;
  73. };
  74. struct configurable_plugin {
  75. pthread_mutex_t lock;
  76. char *name;
  77. DICTIONARY *modules;
  78. const char *schema;
  79. dyncfg_config_t (*get_config_cb)(void *usr_ctx);
  80. dyncfg_config_t (*get_config_schema_cb)(void *usr_ctx);
  81. enum set_config_result (*set_config_cb)(void *usr_ctx, dyncfg_config_t *cfg);
  82. void *cb_usr_ctx; // context for all callbacks (split if needed in future)
  83. };
  84. // API to be used by plugins
  85. const DICTIONARY_ITEM *register_plugin(struct configurable_plugin *plugin);
  86. void unregister_plugin(const DICTIONARY_ITEM *plugin);
  87. int register_module(struct configurable_plugin *plugin, struct module *module);
  88. void report_job_status(struct configurable_plugin *plugin, const char *module_name, const char *job_name, enum job_status status, int status_code, char *reason);
  89. // API to be used by the web server(s)
  90. json_object *get_list_of_plugins_json();
  91. struct configurable_plugin *get_plugin_by_name(const char *name);
  92. json_object *get_list_of_modules_json(struct configurable_plugin *plugin);
  93. struct module *get_module_by_name(struct configurable_plugin *plugin, const char *module_name);
  94. // helper struct to make interface between internal webserver and h2o same
  95. struct uni_http_response {
  96. int status;
  97. char *content;
  98. size_t content_length;
  99. HTTP_CONTENT_TYPE content_type;
  100. void (*content_free)(void *);
  101. };
  102. struct uni_http_response dyn_conf_process_http_request(int method, const char *plugin, const char *module, const char *job_id, void *payload, size_t payload_size);
  103. // API to be used by main netdata process, initialization and destruction etc.
  104. int dyn_conf_init(void);
  105. void freez_dyncfg(void *ptr);
  106. void *dyncfg_main(void *in);
  107. #endif //DYN_CONF_H