sqlite_functions.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_SQLITE_FUNCTIONS_H
  3. #define NETDATA_SQLITE_FUNCTIONS_H
  4. #include "daemon/common.h"
  5. #include "sqlite3.h"
  6. // return a node list
  7. struct node_instance_list {
  8. uuid_t node_id;
  9. uuid_t host_id;
  10. char *hostname;
  11. int live;
  12. int queryable;
  13. int hops;
  14. };
  15. typedef enum db_check_action_type {
  16. DB_CHECK_NONE = 0x0000,
  17. DB_CHECK_INTEGRITY = 0x0001,
  18. DB_CHECK_FIX_DB = 0x0002,
  19. DB_CHECK_RECLAIM_SPACE = 0x0004,
  20. DB_CHECK_CONT = 0x00008
  21. } db_check_action_type_t;
  22. #define SQL_MAX_RETRY (100)
  23. #define SQLITE_INSERT_DELAY (50) // Insert delay in case of lock
  24. #define SQL_STORE_HOST "insert or replace into host (host_id,hostname,registry_hostname,update_every,os,timezone,tags, hops) " \
  25. "values (?1,?2,?3,?4,?5,?6,?7,?8);"
  26. #define SQL_STORE_CHART "insert or replace into chart (chart_id, host_id, type, id, " \
  27. "name, family, context, title, unit, plugin, module, priority, update_every , chart_type , memory_mode , " \
  28. "history_entries) values (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16);"
  29. #define SQL_FIND_CHART_UUID \
  30. "select chart_id from chart where host_id = @host and type=@type and id=@id and (name is null or name=@name);"
  31. #define SQL_STORE_ACTIVE_CHART \
  32. "insert or replace into chart_active (chart_id, date_created) values (@id, unixepoch());"
  33. #define SQL_STORE_DIMENSION \
  34. "INSERT OR REPLACE into dimension (dim_id, chart_id, id, name, multiplier, divisor , algorithm) values (?0001,?0002,?0003,?0004,?0005,?0006,?0007);"
  35. #define SQL_FIND_DIMENSION_UUID \
  36. "select dim_id from dimension where chart_id=@chart and id=@id and name=@name and length(dim_id)=16;"
  37. #define SQL_STORE_ACTIVE_DIMENSION \
  38. "insert or replace into dimension_active (dim_id, date_created) values (@id, unixepoch());"
  39. #define CHECK_SQLITE_CONNECTION(db_meta) \
  40. if (unlikely(!db_meta)) { \
  41. if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) { \
  42. return 1; \
  43. } \
  44. error_report("Database has not been initialized"); \
  45. return 1; \
  46. }
  47. extern int sql_init_database(db_check_action_type_t rebuild, int memory);
  48. extern void sql_close_database(void);
  49. extern int bind_text_null(sqlite3_stmt *res, int position, const char *text, bool can_be_null);
  50. extern int sql_store_host(uuid_t *guid, const char *hostname, const char *registry_hostname, int update_every, const char *os,
  51. const char *timezone, const char *tags, int hops);
  52. extern int sql_store_host_info(RRDHOST *host);
  53. extern int sql_store_chart(
  54. uuid_t *chart_uuid, uuid_t *host_uuid, const char *type, const char *id, const char *name, const char *family,
  55. const char *context, const char *title, const char *units, const char *plugin, const char *module, long priority,
  56. int update_every, int chart_type, int memory_mode, long history_entries);
  57. extern int sql_store_dimension(uuid_t *dim_uuid, uuid_t *chart_uuid, const char *id, const char *name, collected_number multiplier,
  58. collected_number divisor, int algorithm);
  59. extern int find_dimension_uuid(RRDSET *st, RRDDIM *rd, uuid_t *store_uuid);
  60. extern void store_active_dimension(uuid_t *dimension_uuid);
  61. extern uuid_t *find_chart_uuid(RRDHOST *host, const char *type, const char *id, const char *name);
  62. extern uuid_t *create_chart_uuid(RRDSET *st, const char *id, const char *name);
  63. extern int update_chart_metadata(uuid_t *chart_uuid, RRDSET *st, const char *id, const char *name);
  64. extern void store_active_chart(uuid_t *dimension_uuid);
  65. extern int find_uuid_type(uuid_t *uuid);
  66. extern void sql_rrdset2json(RRDHOST *host, BUFFER *wb);
  67. extern RRDHOST *sql_create_host_by_uuid(char *guid);
  68. extern int prepare_statement(sqlite3 *database, char *query, sqlite3_stmt **statement);
  69. extern int execute_insert(sqlite3_stmt *res);
  70. extern void db_execute(const char *cmd);
  71. extern int file_is_migrated(char *path);
  72. extern void add_migrated_file(char *path, uint64_t file_size);
  73. extern void db_unlock(void);
  74. extern void db_lock(void);
  75. extern void delete_dimension_uuid(uuid_t *dimension_uuid);
  76. extern void sql_store_chart_label(uuid_t *chart_uuid, int source_type, char *label, char *value);
  77. extern void sql_build_context_param_list(ONEWAYALLOC *owa, struct context_param **param_list, RRDHOST *host, char *context, char *chart);
  78. extern void store_claim_id(uuid_t *host_id, uuid_t *claim_id);
  79. extern int update_node_id(uuid_t *host_id, uuid_t *node_id);
  80. extern int get_node_id(uuid_t *host_id, uuid_t *node_id);
  81. extern int get_host_id(uuid_t *node_id, uuid_t *host_id);
  82. extern void invalidate_node_instances(uuid_t *host_id, uuid_t *claim_id);
  83. extern struct node_instance_list *get_node_list(void);
  84. extern void sql_load_node_id(RRDHOST *host);
  85. extern void compute_chart_hash(RRDSET *st);
  86. extern int sql_set_dimension_option(uuid_t *dim_uuid, char *option);
  87. char *get_hostname_by_node_id(char *node_id);
  88. void free_temporary_host(RRDHOST *host);
  89. int init_database_batch(sqlite3 *database, int rebuild, int init_type, const char *batch[]);
  90. void migrate_localhost(uuid_t *host_uuid);
  91. #endif //NETDATA_SQLITE_FUNCTIONS_H