master
c 189 lines 5.93 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #define RRDHOST_INTERNALS
4 #include "rrd.h"
5
6 // --------------------------------------------------------------------------------------------------------------------
7 // globals
8
9 /*
10 // if not zero it gives the time (in seconds) to remove un-updated dimensions
11 // DO NOT ENABLE
12 // if dimensions are removed, the chart generation will have to run again
13 int rrd_delete_unupdated_dimensions = 0;
14 */
15
16 #ifdef ENABLE_DBENGINE
17 RRD_DB_MODE default_rrd_memory_mode = RRD_DB_MODE_DBENGINE;
18 #else
19 RRD_DB_MODE default_rrd_memory_mode = RRD_DB_MODE_RAM;
20 #endif
21 int gap_when_lost_iterations_above = 1;
22
23
24 // --------------------------------------------------------------------------------------------------------------------
25 // RRD - string management
26
27 STRING *rrd_string_strdupz(const char *s) {
28 if(unlikely(!s || !*s)) return string_strdupz(s);
29
30 size_t len = strlen(s);
31 size_t dst_size = (len * 2) + 1;
32 char *buf = mallocz(dst_size);
33
34 // Sanitize the string, preserving valid UTF-8
35 text_sanitize((unsigned char *)buf, (const unsigned char *)s, dst_size,
36 rrd_string_allowed_chars, true, "", NULL);
37
38 STRING *result = string_strdupz(buf);
39 freez(buf);
40 return result;
41 }
42
43 // --------------------------------------------------------------------------------------------------------------------
44
45 inline long align_entries_to_pagesize(RRD_DB_MODE mode, long entries) {
46 if(mode == RRD_DB_MODE_DBENGINE) return 0;
47 if(mode == RRD_DB_MODE_NONE) return 5;
48
49 if(entries < 5) entries = 5;
50 if(entries > RRD_HISTORY_ENTRIES_MAX) entries = RRD_HISTORY_ENTRIES_MAX;
51
52 if(mode == RRD_DB_MODE_RAM) {
53 long header_size = 0;
54
55 long page = (long)sysconf(_SC_PAGESIZE);
56 long size = (long)(header_size + entries * sizeof(storage_number));
57 if (unlikely(size % page)) {
58 size -= (size % page);
59 size += page;
60
61 long n = (long)((size - header_size) / sizeof(storage_number));
62 return n;
63 }
64 }
65
66 return entries;
67 }
68
69 // --------------------------------------------------------------------------------------------------------------------
70
71 void api_v1_management_init(void);
72
73 // Thread-exit cleanup callbacks. Registered with libnetdata below;
74 // called once per thread at exit time, in registration order. All
75 // callbacks except rrdset_thread_rda_free are declared in their
76 // owning headers (included via rrd.h); rrdset_thread_rda_free has no
77 // public header so it is declared locally here.
78 void rrdset_thread_rda_free(void);
79
80 int rrd_init(const char *hostname, struct rrdhost_system_info *system_info, bool unittest) {
81 nd_thread_register_cleanup(rrd_collector_finished);
82 nd_thread_register_cleanup(sender_thread_buffer_free);
83 nd_thread_register_cleanup(rrdset_thread_rda_free);
84 nd_thread_register_cleanup(query_target_free);
85
86 rrdhost_init();
87
88 if (unlikely(sql_init_meta_database(DB_CHECK_NONE, system_info ? 0 : 1))) {
89 if (default_rrd_memory_mode == RRD_DB_MODE_DBENGINE) {
90 set_late_analytics_variables(system_info);
91 fatal("Failed to initialize SQLite");
92 }
93
94 nd_log(NDLS_DAEMON, NDLP_DEBUG,
95 "Skipping SQLITE metadata initialization since memory mode is not dbengine");
96 }
97
98 if (unlikely(sql_init_context_database(system_info ? 0 : 1))) {
99 error_report("Failed to initialize context metadata database");
100 }
101
102 if (unlikely(unittest)) {
103 dbengine_enabled = true;
104 }
105 else {
106 if (default_rrd_memory_mode == RRD_DB_MODE_DBENGINE || stream_conf_receiver_needs_dbengine()) {
107 nd_log(NDLS_DAEMON, NDLP_DEBUG,
108 "DBENGINE: Initializing ...");
109
110 netdata_conf_dbengine_init(hostname);
111 }
112 else
113 nd_profile.storage_tiers = 1;
114
115 if (!dbengine_enabled) {
116 if (nd_profile.storage_tiers > 1) {
117 nd_log(NDLS_DAEMON, NDLP_WARNING,
118 "dbengine is not enabled, but %zu tiers have been requested. Resetting tiers to 1",
119 nd_profile.storage_tiers);
120
121 nd_profile.storage_tiers = 1;
122 }
123
124 if (default_rrd_memory_mode == RRD_DB_MODE_DBENGINE) {
125 nd_log(NDLS_DAEMON, NDLP_WARNING,
126 "dbengine is not enabled, but it has been given as the default db mode. "
127 "Resetting db mode to alloc");
128
129 default_rrd_memory_mode = RRD_DB_MODE_ALLOC;
130 }
131 }
132 }
133
134 if(!unittest) {
135 metadata_sync_init();
136 health_load_config_defaults();
137 }
138
139 SYSTEM_TZ tz = system_tz_get();
140 localhost = rrdhost_create(
141 hostname
142 , registry_get_this_machine_hostname()
143 , machine_guid_get_txt()
144 , os_type
145 , tz.timezone
146 , tz.abbrev_timezone
147 , tz.utc_offset
148 , program_name
149 , NETDATA_VERSION
150 , nd_profile.update_every, default_rrd_history_entries
151 , default_rrd_memory_mode
152 , health_plugin_enabled()
153 , stream_send.enabled
154 , stream_send.parents.destination
155 , stream_send.api_key
156 , stream_send.send_charts_matching
157 , stream_receive.replication.enabled
158 , stream_receive.replication.period
159 , stream_receive.replication.step
160 , system_info
161 , 1
162 , 0
163 );
164 system_tz_free(&tz);
165 rrdhost_system_info_free(system_info);
166
167 if (unlikely(!localhost))
168 return 1;
169
170 rrdhost_flag_set(localhost, RRDHOST_FLAG_COLLECTOR_ONLINE);
171 object_state_activate(&localhost->state_id);
172 pulse_host_status(localhost, 0, 0); // this will detect the receiver status
173
174 ml_host_start(localhost);
175 dyncfg_host_init(localhost);
176
177 if(!unittest)
178 health_plugin_init();
179
180 global_functions_add();
181
182 if (likely(system_info)) {
183 detect_machine_guid_change(&localhost->host_id.uuid);
184 aclk_synchronization_init();
185 api_v1_management_init();
186 }
187
188 return 0;
189 }