sqlite_functions.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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) values (?1,?2,?3,?4,?5,?6,?7);"
  25. #define SQL_STORE_CHART "insert or replace into chart (chart_id, host_id, type, id, " \
  26. "name, family, context, title, unit, plugin, module, priority, update_every , chart_type , memory_mode , " \
  27. "history_entries) values (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?16);"
  28. #define SQL_FIND_CHART_UUID \
  29. "select chart_id from chart where host_id = @host and type=@type and id=@id and (name is null or name=@name);"
  30. #define SQL_STORE_ACTIVE_CHART \
  31. "insert or replace into chart_active (chart_id, date_created) values (@id, strftime('%s'));"
  32. #define SQL_STORE_DIMENSION \
  33. "INSERT OR REPLACE into dimension (dim_id, chart_id, id, name, multiplier, divisor , algorithm) values (?0001,?0002,?0003,?0004,?0005,?0006,?0007);"
  34. #define SQL_FIND_DIMENSION_UUID \
  35. "select dim_id from dimension where chart_id=@chart and id=@id and name=@name and length(dim_id)=16;"
  36. #define SQL_STORE_ACTIVE_DIMENSION \
  37. "insert or replace into dimension_active (dim_id, date_created) values (@id, strftime('%s'));"
  38. #define CHECK_SQLITE_CONNECTION(db_meta) \
  39. if (unlikely(!db_meta)) { \
  40. if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) { \
  41. return 1; \
  42. } \
  43. error_report("Database has not been initialized"); \
  44. return 1; \
  45. }
  46. extern int sql_init_database(db_check_action_type_t rebuild, int memory);
  47. extern void sql_close_database(void);
  48. extern int sql_store_host(uuid_t *guid, const char *hostname, const char *registry_hostname, int update_every, const char *os, const char *timezone, const char *tags);
  49. extern int sql_store_chart(
  50. uuid_t *chart_uuid, uuid_t *host_uuid, const char *type, const char *id, const char *name, const char *family,
  51. const char *context, const char *title, const char *units, const char *plugin, const char *module, long priority,
  52. int update_every, int chart_type, int memory_mode, long history_entries);
  53. extern int sql_store_dimension(uuid_t *dim_uuid, uuid_t *chart_uuid, const char *id, const char *name, collected_number multiplier,
  54. collected_number divisor, int algorithm);
  55. extern int find_dimension_uuid(RRDSET *st, RRDDIM *rd, uuid_t *store_uuid);
  56. extern void store_active_dimension(uuid_t *dimension_uuid);
  57. extern uuid_t *find_chart_uuid(RRDHOST *host, const char *type, const char *id, const char *name);
  58. extern uuid_t *create_chart_uuid(RRDSET *st, const char *id, const char *name);
  59. extern int update_chart_metadata(uuid_t *chart_uuid, RRDSET *st, const char *id, const char *name);
  60. extern void store_active_chart(uuid_t *dimension_uuid);
  61. extern int find_uuid_type(uuid_t *uuid);
  62. extern void sql_rrdset2json(RRDHOST *host, BUFFER *wb);
  63. extern RRDHOST *sql_create_host_by_uuid(char *guid);
  64. extern int prepare_statement(sqlite3 *database, char *query, sqlite3_stmt **statement);
  65. extern int execute_insert(sqlite3_stmt *res);
  66. extern void db_execute(const char *cmd);
  67. extern int file_is_migrated(char *path);
  68. extern void add_migrated_file(char *path, uint64_t file_size);
  69. extern void db_unlock(void);
  70. extern void db_lock(void);
  71. extern void delete_dimension_uuid(uuid_t *dimension_uuid);
  72. extern void sql_store_chart_label(uuid_t *chart_uuid, int source_type, char *label, char *value);
  73. extern void sql_build_context_param_list(ONEWAYALLOC *owa, struct context_param **param_list, RRDHOST *host, char *context, char *chart);
  74. extern void store_claim_id(uuid_t *host_id, uuid_t *claim_id);
  75. extern int update_node_id(uuid_t *host_id, uuid_t *node_id);
  76. extern int get_node_id(uuid_t *host_id, uuid_t *node_id);
  77. extern int get_host_id(uuid_t *node_id, uuid_t *host_id);
  78. extern void invalidate_node_instances(uuid_t *host_id, uuid_t *claim_id);
  79. extern struct node_instance_list *get_node_list(void);
  80. extern void sql_load_node_id(RRDHOST *host);
  81. extern void compute_chart_hash(RRDSET *st);
  82. extern int sql_set_dimension_option(uuid_t *dim_uuid, char *option);
  83. char *get_hostname_by_node_id(char *node_id);
  84. void free_temporary_host(RRDHOST *host);
  85. #endif //NETDATA_SQLITE_FUNCTIONS_H