sqlite_functions.h 5.5 KB

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