master
c 780 lines 40.3 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #define RRDHOST_SYSTEM_INFO_INTERNALS
4 #include "rrdhost-system-info.h"
5 #include "aclk/schema-wrappers/node_info.h"
6 #include "daemon/win_system-info.h"
7
8 // coverity[ +tainted_string_sanitize_content : arg-0 ]
9 static inline void coverity_remove_taint(char *s __maybe_unused) { }
10
11 void rrdhost_system_info_swap(struct rrdhost_system_info *a, struct rrdhost_system_info *b) {
12 if(a && b)
13 SWAP(*a, *b);
14 }
15
16 // ----------------------------------------------------------------------------
17 // RRDHOST - set system info from environment variables
18 // system_info fields must be heap allocated or NULL
19 int rrdhost_system_info_set_by_name(struct rrdhost_system_info *system_info, char *name, char *value) {
20 int res = 0;
21
22 if (!strcmp(name, "NETDATA_PROTOCOL_VERSION"))
23 return res;
24
25 else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_TYPE")){
26 freez(system_info->cloud_provider_type);
27 system_info->cloud_provider_type = strdupz(value);
28 }
29 else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE")){
30 freez(system_info->cloud_instance_type);
31 system_info->cloud_instance_type = strdupz(value);
32 }
33 else if(!strcmp(name, "NETDATA_INSTANCE_CLOUD_INSTANCE_REGION")){
34 freez(system_info->cloud_instance_region);
35 system_info->cloud_instance_region = strdupz(value);
36 }
37 else if(!strcmp(name, "NETDATA_CONTAINER_OS_NAME")){
38 freez(system_info->container_os_name);
39 system_info->container_os_name = strdupz(value);
40 }
41 else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID")){
42 freez(system_info->container_os_id);
43 system_info->container_os_id = strdupz(value);
44 }
45 else if(!strcmp(name, "NETDATA_CONTAINER_OS_ID_LIKE")){
46 freez(system_info->container_os_id_like);
47 system_info->container_os_id_like = strdupz(value);
48 }
49 else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION")){
50 freez(system_info->container_os_version);
51 system_info->container_os_version = strdupz(value);
52 }
53 else if(!strcmp(name, "NETDATA_CONTAINER_OS_VERSION_ID")){
54 freez(system_info->container_os_version_id);
55 system_info->container_os_version_id = strdupz(value);
56 }
57 else if(!strcmp(name, "NETDATA_CONTAINER_OS_DETECTION")){
58 freez(system_info->container_os_detection);
59 system_info->container_os_detection = strdupz(value);
60 }
61 else if(!strcmp(name, "NETDATA_HOST_OS_NAME")){
62 freez(system_info->host_os_name);
63 size_t len = strlen(value);
64 size_t dst_size = (len * 2) + 1;
65 system_info->host_os_name = mallocz(dst_size);
66 text_sanitize((unsigned char *)system_info->host_os_name, (const unsigned char *)value,
67 dst_size, rrd_string_allowed_chars, true, "", NULL);
68 }
69 else if(!strcmp(name, "NETDATA_HOST_OS_ID")){
70 freez(system_info->host_os_id);
71 system_info->host_os_id = strdupz(value);
72 }
73 else if(!strcmp(name, "NETDATA_HOST_OS_ID_LIKE")){
74 freez(system_info->host_os_id_like);
75 system_info->host_os_id_like = strdupz(value);
76 }
77 else if(!strcmp(name, "NETDATA_HOST_OS_VERSION")){
78 freez(system_info->host_os_version);
79 system_info->host_os_version = strdupz(value);
80 }
81 else if(!strcmp(name, "NETDATA_HOST_OS_VERSION_ID")){
82 freez(system_info->host_os_version_id);
83 system_info->host_os_version_id = strdupz(value);
84 }
85 else if(!strcmp(name, "NETDATA_HOST_OS_DETECTION")){
86 freez(system_info->host_os_detection);
87 system_info->host_os_detection = strdupz(value);
88 }
89 else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_NAME")){
90 freez(system_info->kernel_name);
91 system_info->kernel_name = strdupz(value);
92 }
93 else if(!strcmp(name, "NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT")){
94 freez(system_info->host_cores);
95 system_info->host_cores = strdupz(value);
96 }
97 else if(!strcmp(name, "NETDATA_SYSTEM_CPU_FREQ")){
98 freez(system_info->host_cpu_freq);
99 system_info->host_cpu_freq = strdupz(value);
100 }
101 else if (!strcmp(name, "NETDATA_SYSTEM_CPU_MODEL")){
102 freez(system_info->host_cpu_model);
103 system_info->host_cpu_model = strdupz(value);
104 }
105 else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_RAM")){
106 freez(system_info->host_ram_total);
107 system_info->host_ram_total = strdupz(value);
108 }
109 else if(!strcmp(name, "NETDATA_SYSTEM_TOTAL_DISK_SIZE")){
110 freez(system_info->host_disk_space);
111 system_info->host_disk_space = strdupz(value);
112 }
113 else if(!strcmp(name, "NETDATA_SYSTEM_KERNEL_VERSION")){
114 freez(system_info->kernel_version);
115 system_info->kernel_version = strdupz(value);
116 }
117 else if(!strcmp(name, "NETDATA_SYSTEM_ARCHITECTURE")){
118 freez(system_info->architecture);
119 system_info->architecture = strdupz(value);
120 }
121 else if(!strcmp(name, "NETDATA_SYSTEM_VIRTUALIZATION")){
122 freez(system_info->virtualization);
123 system_info->virtualization = strdupz(value);
124 }
125 else if(!strcmp(name, "NETDATA_SYSTEM_VIRT_DETECTION")){
126 freez(system_info->virt_detection);
127 system_info->virt_detection = strdupz(value);
128 }
129 else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER")){
130 freez(system_info->container);
131 system_info->container = strdupz(value);
132 }
133 else if(!strcmp(name, "NETDATA_SYSTEM_CONTAINER_DETECTION")){
134 freez(system_info->container_detection);
135 system_info->container_detection = strdupz(value);
136 }
137 else if(!strcmp(name, "NETDATA_HOST_IS_K8S_NODE")){
138 freez(system_info->is_k8s_node);
139 system_info->is_k8s_node = strdupz(value);
140 }
141 else if(!strcmp(name, "NETDATA_SYSTEM_DEFAULT_INTERFACE_NAME")){
142 freez(system_info->network_default_iface);
143 system_info->network_default_iface = strdupz(value);
144 }
145 else if(!strcmp(name, "NETDATA_SYSTEM_DEFAULT_INTERFACE_IP")){
146 freez(system_info->network_default_iface_ip);
147 system_info->network_default_iface_ip = strdupz(value);
148 }
149 else if(!strcmp(name, "NETDATA_SYSTEM_DEFAULT_INTERFACE_DETECTION")){
150 freez(system_info->network_default_iface_detection);
151 system_info->network_default_iface_detection = strdupz(value);
152 }
153 else if (!strcmp(name, "NETDATA_SYSTEM_CPU_VENDOR"))
154 return res;
155 else if (!strcmp(name, "NETDATA_SYSTEM_CPU_DETECTION"))
156 return res;
157 else if (!strcmp(name, "NETDATA_SYSTEM_RAM_DETECTION"))
158 return res;
159 else if (!strcmp(name, "NETDATA_SYSTEM_DISK_DETECTION"))
160 return res;
161 else if (!strcmp(name, "NETDATA_CONTAINER_IS_OFFICIAL_IMAGE"))
162 return res;
163 else {
164 res = 1;
165 }
166
167 return res;
168 }
169
170 struct rrdhost_system_info *rrdhost_system_info_from_host_labels(RRDLABELS *labels) {
171 struct rrdhost_system_info *info = rrdhost_system_info_create();
172 info->hops = 1;
173
174 rrdlabels_get_value_strdup_or_null(labels, &info->cloud_provider_type, "_cloud_provider_type");
175 rrdlabels_get_value_strdup_or_null(labels, &info->cloud_instance_type, "_cloud_instance_type");
176 rrdlabels_get_value_strdup_or_null(labels, &info->cloud_instance_region, "_cloud_instance_region");
177 rrdlabels_get_value_strdup_or_null(labels, &info->host_os_name, "_os_name");
178 rrdlabels_get_value_strdup_or_null(labels, &info->host_os_version, "_os_version");
179 rrdlabels_get_value_strdup_or_null(labels, &info->kernel_version, "_kernel_version");
180 rrdlabels_get_value_strdup_or_null(labels, &info->host_cores, "_system_cores");
181 rrdlabels_get_value_strdup_or_null(labels, &info->host_cpu_freq, "_system_cpu_freq");
182 rrdlabels_get_value_strdup_or_null(labels, &info->host_cpu_model, "_system_cpu_model");
183 rrdlabels_get_value_strdup_or_null(labels, &info->host_ram_total, "_system_ram_total");
184 rrdlabels_get_value_strdup_or_null(labels, &info->host_disk_space, "_system_disk_space");
185 rrdlabels_get_value_strdup_or_null(labels, &info->architecture, "_architecture");
186 rrdlabels_get_value_strdup_or_null(labels, &info->virtualization, "_virtualization");
187 rrdlabels_get_value_strdup_or_null(labels, &info->container, "_container");
188 rrdlabels_get_value_strdup_or_null(labels, &info->container_detection, "_container_detection");
189 rrdlabels_get_value_strdup_or_null(labels, &info->virt_detection, "_virt_detection");
190 rrdlabels_get_value_strdup_or_null(labels, &info->is_k8s_node, "_is_k8s_node");
191 rrdlabels_get_value_strdup_or_null(labels, &info->install_type, "_install_type");
192 rrdlabels_get_value_strdup_or_null(labels, &info->prebuilt_arch, "_prebuilt_arch");
193 rrdlabels_get_value_strdup_or_null(labels, &info->prebuilt_dist, "_prebuilt_dist");
194 rrdlabels_get_value_strdup_or_null(labels, &info->network_default_iface, "_net_default_iface");
195 rrdlabels_get_value_strdup_or_null(labels, &info->network_default_iface_ip, "_net_default_iface_ip");
196 rrdlabels_get_value_strdup_or_null(labels, &info->network_default_iface_detection, "_net_default_iface_detection");
197 rrdlabels_get_value_strdup_or_null(labels, &info->hw_product_name, "_hw_product_name");
198 rrdlabels_get_value_strdup_or_null(labels, &info->hw_sys_vendor, "_hw_sys_vendor");
199 rrdlabels_get_value_strdup_or_null(labels, &info->hw_product_type, "_hw_product_type");
200 return info;
201 }
202
203 void rrdhost_system_info_to_rrdlabels(struct rrdhost_system_info *system_info, RRDLABELS *labels) {
204 if (system_info->cloud_provider_type)
205 rrdlabels_add(labels, "_cloud_provider_type", system_info->cloud_provider_type, RRDLABEL_SRC_AUTO);
206
207 if (system_info->cloud_instance_type)
208 rrdlabels_add(labels, "_cloud_instance_type", system_info->cloud_instance_type, RRDLABEL_SRC_AUTO);
209
210 if (system_info->cloud_instance_region)
211 rrdlabels_add(labels, "_cloud_instance_region", system_info->cloud_instance_region, RRDLABEL_SRC_AUTO);
212
213 if (system_info->host_os_name)
214 rrdlabels_add(labels, "_os_name", system_info->host_os_name, RRDLABEL_SRC_AUTO);
215
216 if (system_info->host_os_version)
217 rrdlabels_add(labels, "_os_version", system_info->host_os_version, RRDLABEL_SRC_AUTO);
218
219 if (system_info->kernel_version)
220 rrdlabels_add(labels, "_kernel_version", system_info->kernel_version, RRDLABEL_SRC_AUTO);
221
222 if (system_info->host_cores)
223 rrdlabels_add(labels, "_system_cores", system_info->host_cores, RRDLABEL_SRC_AUTO);
224
225 if (system_info->host_cpu_freq)
226 rrdlabels_add(labels, "_system_cpu_freq", system_info->host_cpu_freq, RRDLABEL_SRC_AUTO);
227
228 if (system_info->host_cpu_model)
229 rrdlabels_add(labels, "_system_cpu_model", system_info->host_cpu_model, RRDLABEL_SRC_AUTO);
230
231 if (system_info->host_ram_total)
232 rrdlabels_add(labels, "_system_ram_total", system_info->host_ram_total, RRDLABEL_SRC_AUTO);
233
234 if (system_info->host_disk_space)
235 rrdlabels_add(labels, "_system_disk_space", system_info->host_disk_space, RRDLABEL_SRC_AUTO);
236
237 if (system_info->architecture)
238 rrdlabels_add(labels, "_architecture", system_info->architecture, RRDLABEL_SRC_AUTO);
239
240 if (system_info->virtualization)
241 rrdlabels_add(labels, "_virtualization", system_info->virtualization, RRDLABEL_SRC_AUTO);
242
243 if (system_info->container)
244 rrdlabels_add(labels, "_container", system_info->container, RRDLABEL_SRC_AUTO);
245
246 if (system_info->container_detection)
247 rrdlabels_add(labels, "_container_detection", system_info->container_detection, RRDLABEL_SRC_AUTO);
248
249 if (system_info->virt_detection)
250 rrdlabels_add(labels, "_virt_detection", system_info->virt_detection, RRDLABEL_SRC_AUTO);
251
252 if (system_info->is_k8s_node)
253 rrdlabels_add(labels, "_is_k8s_node", system_info->is_k8s_node, RRDLABEL_SRC_AUTO);
254
255 if (system_info->install_type)
256 rrdlabels_add(labels, "_install_type", system_info->install_type, RRDLABEL_SRC_AUTO);
257
258 if (system_info->prebuilt_arch)
259 rrdlabels_add(labels, "_prebuilt_arch", system_info->prebuilt_arch, RRDLABEL_SRC_AUTO);
260
261 if (system_info->prebuilt_dist)
262 rrdlabels_add(labels, "_prebuilt_dist", system_info->prebuilt_dist, RRDLABEL_SRC_AUTO);
263
264 if (system_info->network_default_iface)
265 rrdlabels_add(labels, "_net_default_iface", system_info->network_default_iface, RRDLABEL_SRC_AUTO);
266
267 if (system_info->network_default_iface_ip)
268 rrdlabels_add(labels, "_net_default_iface_ip", system_info->network_default_iface_ip, RRDLABEL_SRC_AUTO);
269
270 if (system_info->network_default_iface_detection)
271 rrdlabels_add(labels, "_net_default_iface_detection", system_info->network_default_iface_detection, RRDLABEL_SRC_AUTO);
272
273 if (system_info->hw_product_name)
274 rrdlabels_add(labels, "_hw_product_name", system_info->hw_product_name, RRDLABEL_SRC_AUTO);
275
276 if (system_info->hw_sys_vendor)
277 rrdlabels_add(labels, "_hw_sys_vendor", system_info->hw_sys_vendor, RRDLABEL_SRC_AUTO);
278
279 if (system_info->hw_product_type)
280 rrdlabels_add(labels, "_hw_product_type", system_info->hw_product_type, RRDLABEL_SRC_AUTO);
281 }
282
283 int rrdhost_system_info_detect(struct rrdhost_system_info *system_info) {
284 if (unlikely(!system_info)) {
285 netdata_log_error("SYSTEM INFO: System info structure is NULL.");
286 return 1;
287 }
288
289 // Populate hardware product fields from the daemon status file when it is available/initialized.
290 {
291 const char *product_name = daemon_status_file_get_product_name();
292 if (product_name && *product_name) {
293 freez(system_info->hw_product_name);
294 system_info->hw_product_name = strdupz(product_name);
295 }
296
297 const char *sys_vendor = daemon_status_file_get_sys_vendor();
298 if (sys_vendor && *sys_vendor) {
299 freez(system_info->hw_sys_vendor);
300 system_info->hw_sys_vendor = strdupz(sys_vendor);
301 }
302
303 const char *product_type = daemon_status_file_get_product_type();
304 if (product_type && *product_type) {
305 freez(system_info->hw_product_type);
306 system_info->hw_product_type = strdupz(product_type);
307 }
308 }
309
310 #if !defined(OS_WINDOWS)
311 CLEAN_BUFFER *script = buffer_create(0, NULL);
312 buffer_sprintf(script, "%s/system-info.sh", netdata_configured_primary_plugins_dir);
313
314 POPEN_INSTANCE *instance = NULL;
315 int ret = 1;
316
317 // Check if script exists and is readable
318 if (unlikely(access(buffer_tostring(script), R_OK) != 0)) {
319 netdata_log_error("SYSTEM INFO: System info script %s not found or not readable.",
320 buffer_tostring(script));
321 goto cleanup;
322 }
323
324 // Run the script
325 instance = spawn_popen_run(buffer_tostring(script));
326 if (unlikely(!instance)) {
327 netdata_log_error("SYSTEM INFO: Failed to execute system info script %s.",
328 buffer_tostring(script));
329 goto cleanup;
330 }
331
332 char line[1024];
333 FILE *fp = spawn_popen_stdout(instance);
334 if (unlikely(!fp)) {
335 netdata_log_error("SYSTEM INFO: Failed to get stdout from system info script.");
336 goto cleanup;
337 }
338
339 // Process each line from the script output
340 while (fgets(line, sizeof(line) - 1, fp) != NULL) {
341 // Ensure null-termination
342 line[sizeof(line) - 1] = '\0';
343
344 // Find the equals sign separator
345 char *value = strchr(line, '=');
346 if (unlikely(!value)) {
347 // Skip lines without an equal sign
348 nd_log(NDLS_DAEMON, NDLP_ERR,
349 "SYSTEM INFO: Skipping malformed line from system-info.sh (no '=' found): '%s'",
350 line);
351 continue;
352 }
353
354 // Split the name and value
355 *value = '\0';
356 value++;
357
358 // Trim any trailing newline from the value
359 char *end = strchr(value, '\n');
360 if (end) *end = '\0';
361
362 // Remove any carriage return that might be present (especially for macOS)
363 end = strchr(value, '\r');
364 if (end) *end = '\0';
365
366 // Validate name and value
367 if (unlikely(!*line || !*value)) {
368 nd_log(NDLS_DAEMON, NDLP_WARNING,
369 "SYSTEM INFO: Skipping empty name or value from system-info.sh: '%s=%s'",
370 line, value);
371 continue;
372 }
373
374 // Process the name-value pair
375 coverity_remove_taint(line);
376 coverity_remove_taint(value);
377
378 if (unlikely(rrdhost_system_info_set_by_name(system_info, line, value))) {
379 nd_log(NDLS_DAEMON, NDLP_ERR,
380 "SYSTEM INFO: Unexpected variable '%s=%s'",
381 line, value);
382 } else {
383 // Only set as environment variable if it was successfully processed
384 nd_setenv(line, value, 1);
385 }
386 }
387
388 // Everything succeeded
389 ret = 0;
390
391 cleanup:
392 // Clean up resources
393 if (instance)
394 spawn_popen_wait(instance);
395
396 return ret;
397 #else
398 netdata_windows_get_system_info(system_info);
399 return 0;
400 #endif
401 }
402
403 void rrdhost_system_info_free(struct rrdhost_system_info *system_info) {
404 if(likely(system_info)) {
405 __atomic_sub_fetch(&netdata_buffers_statistics.rrdhost_allocations_size, sizeof(struct rrdhost_system_info), __ATOMIC_RELAXED);
406
407 freez(system_info->cloud_provider_type);
408 freez(system_info->cloud_instance_type);
409 freez(system_info->cloud_instance_region);
410 freez(system_info->host_os_name);
411 freez(system_info->host_os_id);
412 freez(system_info->host_os_id_like);
413 freez(system_info->host_os_version);
414 freez(system_info->host_os_version_id);
415 freez(system_info->host_os_detection);
416 freez(system_info->host_cores);
417 freez(system_info->host_cpu_freq);
418 freez(system_info->host_cpu_model);
419 freez(system_info->host_ram_total);
420 freez(system_info->host_disk_space);
421 freez(system_info->container_os_name);
422 freez(system_info->container_os_id);
423 freez(system_info->container_os_id_like);
424 freez(system_info->container_os_version);
425 freez(system_info->container_os_version_id);
426 freez(system_info->container_os_detection);
427 freez(system_info->kernel_name);
428 freez(system_info->kernel_version);
429 freez(system_info->architecture);
430 freez(system_info->virtualization);
431 freez(system_info->virt_detection);
432 freez(system_info->container);
433 freez(system_info->container_detection);
434 freez(system_info->is_k8s_node);
435 freez(system_info->install_type);
436 freez(system_info->prebuilt_arch);
437 freez(system_info->prebuilt_dist);
438 freez(system_info->network_default_iface);
439 freez(system_info->network_default_iface_ip);
440 freez(system_info->network_default_iface_detection);
441 freez(system_info->hw_product_name);
442 freez(system_info->hw_sys_vendor);
443 freez(system_info->hw_product_type);
444 freez(system_info);
445 }
446 }
447
448 struct rrdhost_system_info *rrdhost_system_info_create(void) {
449 struct rrdhost_system_info *system_info = callocz(1, sizeof(struct rrdhost_system_info));
450 __atomic_sub_fetch(&netdata_buffers_statistics.rrdhost_allocations_size, sizeof(struct rrdhost_system_info), __ATOMIC_RELAXED);
451 return system_info;
452 }
453
454 const char *rrdhost_system_info_install_type(struct rrdhost_system_info *si) {
455 return si->install_type;
456 }
457
458 const char *rrdhost_system_info_prebuilt_dist(struct rrdhost_system_info *si) {
459 return si->prebuilt_dist;
460 }
461
462 int16_t rrdhost_system_info_hops(struct rrdhost_system_info *si) {
463 if(!si) return 0;
464 return si->hops;
465 }
466
467 void rrdhost_system_info_hops_set(struct rrdhost_system_info *si, int16_t hops) {
468 si->hops = hops;
469 }
470
471 void rrdhost_system_info_to_json_v1(BUFFER *wb, struct rrdhost_system_info *system_info) {
472 if(!system_info) return;
473
474 buffer_json_member_add_string_or_empty(wb, "os_name", system_info->host_os_name);
475 buffer_json_member_add_string_or_empty(wb, "os_id", system_info->host_os_id);
476 buffer_json_member_add_string_or_empty(wb, "os_id_like", system_info->host_os_id_like);
477 buffer_json_member_add_string_or_empty(wb, "os_version", system_info->host_os_version);
478 buffer_json_member_add_string_or_empty(wb, "os_version_id", system_info->host_os_version_id);
479 buffer_json_member_add_string_or_empty(wb, "os_detection", system_info->host_os_detection);
480 buffer_json_member_add_string_or_empty(wb, "cores_total", system_info->host_cores);
481 buffer_json_member_add_string_or_empty(wb, "total_disk_space", system_info->host_disk_space);
482 buffer_json_member_add_string_or_empty(wb, "cpu_freq", system_info->host_cpu_freq);
483 buffer_json_member_add_string_or_empty(wb, "ram_total", system_info->host_ram_total);
484
485 buffer_json_member_add_string_or_omit(wb, "container_os_name", system_info->container_os_name);
486 buffer_json_member_add_string_or_omit(wb, "container_os_id", system_info->container_os_id);
487 buffer_json_member_add_string_or_omit(wb, "container_os_id_like", system_info->container_os_id_like);
488 buffer_json_member_add_string_or_omit(wb, "container_os_version", system_info->container_os_version);
489 buffer_json_member_add_string_or_omit(wb, "container_os_version_id", system_info->container_os_version_id);
490 buffer_json_member_add_string_or_omit(wb, "container_os_detection", system_info->container_os_detection);
491 buffer_json_member_add_string_or_omit(wb, "is_k8s_node", system_info->is_k8s_node);
492
493 buffer_json_member_add_string_or_empty(wb, "kernel_name", system_info->kernel_name);
494 buffer_json_member_add_string_or_empty(wb, "kernel_version", system_info->kernel_version);
495 buffer_json_member_add_string_or_empty(wb, "architecture", system_info->architecture);
496 buffer_json_member_add_string_or_empty(wb, "virtualization", system_info->virtualization);
497 buffer_json_member_add_string_or_empty(wb, "virt_detection", system_info->virt_detection);
498 buffer_json_member_add_string_or_empty(wb, "container", system_info->container);
499 buffer_json_member_add_string_or_empty(wb, "container_detection", system_info->container_detection);
500
501 buffer_json_member_add_string_or_omit(wb, "cloud_provider_type", system_info->cloud_provider_type);
502 buffer_json_member_add_string_or_omit(wb, "cloud_instance_type", system_info->cloud_instance_type);
503 buffer_json_member_add_string_or_omit(wb, "cloud_instance_region", system_info->cloud_instance_region);
504 }
505
506 void rrdhost_system_info_to_json_v2(BUFFER *wb, struct rrdhost_system_info *system_info) {
507 if(!system_info) return;
508
509 buffer_json_member_add_object(wb, "hw");
510 {
511 buffer_json_member_add_string_or_empty(wb, "architecture", system_info->architecture);
512 buffer_json_member_add_string_or_empty(wb, "cpu_frequency", system_info->host_cpu_freq);
513 buffer_json_member_add_string_or_empty(wb, "cpus", system_info->host_cores);
514 buffer_json_member_add_string_or_empty(wb, "memory", system_info->host_ram_total);
515 buffer_json_member_add_string_or_empty(wb, "disk_space", system_info->host_disk_space);
516 buffer_json_member_add_string_or_empty(wb, "virtualization", system_info->virtualization);
517 buffer_json_member_add_string_or_empty(wb, "container", system_info->container);
518 }
519 buffer_json_object_close(wb);
520
521 buffer_json_member_add_object(wb, "os");
522 {
523 buffer_json_member_add_string_or_empty(wb, "id", system_info->host_os_id);
524 buffer_json_member_add_string_or_empty(wb, "nm", system_info->host_os_name);
525 buffer_json_member_add_string_or_empty(wb, "v", system_info->host_os_version);
526 buffer_json_member_add_object(wb, "kernel");
527 buffer_json_member_add_string_or_empty(wb, "nm", system_info->kernel_name);
528 buffer_json_member_add_string_or_empty(wb, "v", system_info->kernel_version);
529 buffer_json_object_close(wb);
530 }
531 buffer_json_object_close(wb);
532 }
533
534 void rrdhost_system_info_ml_capable_set(struct rrdhost_system_info *system_info, bool capable) {
535 system_info->ml_capable = capable;
536 }
537
538 void rrdhost_system_info_ml_enabled_set(struct rrdhost_system_info *system_info, bool enabled) {
539 system_info->ml_enabled = enabled;
540 }
541
542 void rrdhost_system_info_mc_version_set(struct rrdhost_system_info *system_info, int version) {
543 system_info->mc_version = version;
544 }
545
546 int rrdhost_system_info_foreach(struct rrdhost_system_info *system_info, add_host_sysinfo_key_value_t cb, nd_uuid_t *uuid) {
547 int ret = 0;
548
549 ret += cb("NETDATA_CONTAINER_OS_NAME", system_info->container_os_name, uuid);
550 ret += cb("NETDATA_CONTAINER_OS_ID", system_info->container_os_id, uuid);
551 ret += cb("NETDATA_CONTAINER_OS_ID_LIKE", system_info->container_os_id_like, uuid);
552 ret += cb("NETDATA_CONTAINER_OS_VERSION", system_info->container_os_version, uuid);
553 ret += cb("NETDATA_CONTAINER_OS_VERSION_ID", system_info->container_os_version_id, uuid);
554 ret += cb("NETDATA_CONTAINER_OS_DETECTION", system_info->host_os_detection, uuid);
555 ret += cb("NETDATA_HOST_OS_NAME", system_info->host_os_name, uuid);
556 ret += cb("NETDATA_HOST_OS_ID", system_info->host_os_id, uuid);
557 ret += cb("NETDATA_HOST_OS_ID_LIKE", system_info->host_os_id_like, uuid);
558 ret += cb("NETDATA_HOST_OS_VERSION", system_info->host_os_version, uuid);
559 ret += cb("NETDATA_HOST_OS_VERSION_ID", system_info->host_os_version_id, uuid);
560 ret += cb("NETDATA_HOST_OS_DETECTION", system_info->host_os_detection, uuid);
561 ret += cb("NETDATA_SYSTEM_KERNEL_NAME", system_info->kernel_name, uuid);
562 ret += cb("NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT", system_info->host_cores, uuid);
563 ret += cb("NETDATA_SYSTEM_CPU_FREQ", system_info->host_cpu_freq, uuid);
564 ret += cb("NETDATA_SYSTEM_TOTAL_RAM", system_info->host_ram_total, uuid);
565 ret += cb("NETDATA_SYSTEM_TOTAL_DISK_SIZE", system_info->host_disk_space, uuid);
566 ret += cb("NETDATA_SYSTEM_KERNEL_VERSION", system_info->kernel_version, uuid);
567 ret += cb("NETDATA_SYSTEM_ARCHITECTURE", system_info->architecture, uuid);
568 ret += cb("NETDATA_SYSTEM_VIRTUALIZATION", system_info->virtualization, uuid);
569 ret += cb("NETDATA_SYSTEM_VIRT_DETECTION", system_info->virt_detection, uuid);
570 ret += cb("NETDATA_SYSTEM_CONTAINER", system_info->container, uuid);
571 ret += cb("NETDATA_SYSTEM_CONTAINER_DETECTION", system_info->container_detection, uuid);
572 ret += cb("NETDATA_HOST_IS_K8S_NODE", system_info->is_k8s_node, uuid);
573 ret += cb("NETDATA_SYSTEM_DEFAULT_INTERFACE_NAME", system_info->network_default_iface, uuid);
574 ret += cb("NETDATA_SYSTEM_DEFAULT_INTERFACE_IP", system_info->network_default_iface_ip, uuid);
575 ret += cb("NETDATA_SYSTEM_DEFAULT_INTERFACE_DETECTION", system_info->network_default_iface_detection, uuid);
576 return ret;
577 }
578
579 void rrdhost_system_info_to_url_encode_stream(BUFFER *wb, struct rrdhost_system_info *system_info) {
580 buffer_sprintf(wb, "&ml_capable=%d", system_info->ml_capable ? 1 : 0);
581 buffer_sprintf(wb, "&ml_enabled=%d", system_info->ml_enabled ? 1 : 0);
582 buffer_sprintf(wb, "&mc_version=%d", system_info->mc_version);
583 buffer_key_value_urlencode(wb, "&NETDATA_INSTANCE_CLOUD_TYPE", system_info->cloud_provider_type);
584 buffer_key_value_urlencode(wb, "&NETDATA_INSTANCE_CLOUD_INSTANCE_TYPE", system_info->cloud_instance_type);
585 buffer_key_value_urlencode(wb, "&NETDATA_INSTANCE_CLOUD_INSTANCE_REGION", system_info->cloud_instance_region);
586 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_OS_NAME", system_info->host_os_name);
587 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_OS_ID", system_info->host_os_id);
588 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_OS_ID_LIKE", system_info->host_os_id_like);
589 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_OS_VERSION", system_info->host_os_version);
590 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_OS_VERSION_ID", system_info->host_os_version_id);
591 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_OS_DETECTION", system_info->host_os_detection);
592 buffer_key_value_urlencode(wb, "&NETDATA_HOST_IS_K8S_NODE", system_info->is_k8s_node);
593 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_KERNEL_NAME", system_info->kernel_name);
594 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_KERNEL_VERSION", system_info->kernel_version);
595 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_ARCHITECTURE", system_info->architecture);
596 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_VIRTUALIZATION", system_info->virtualization);
597 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_VIRT_DETECTION", system_info->virt_detection);
598 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_CONTAINER", system_info->container);
599 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_CONTAINER_DETECTION", system_info->container_detection);
600 buffer_key_value_urlencode(wb, "&NETDATA_CONTAINER_OS_NAME", system_info->container_os_name);
601 buffer_key_value_urlencode(wb, "&NETDATA_CONTAINER_OS_ID", system_info->container_os_id);
602 buffer_key_value_urlencode(wb, "&NETDATA_CONTAINER_OS_ID_LIKE", system_info->container_os_id_like);
603 buffer_key_value_urlencode(wb, "&NETDATA_CONTAINER_OS_VERSION", system_info->container_os_version);
604 buffer_key_value_urlencode(wb, "&NETDATA_CONTAINER_OS_VERSION_ID", system_info->container_os_version_id);
605 buffer_key_value_urlencode(wb, "&NETDATA_CONTAINER_OS_DETECTION", system_info->container_os_detection);
606 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT", system_info->host_cores);
607 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_CPU_FREQ", system_info->host_cpu_freq);
608 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_TOTAL_RAM", system_info->host_ram_total);
609 buffer_key_value_urlencode(wb, "&NETDATA_SYSTEM_TOTAL_DISK_SIZE", system_info->host_disk_space);
610 }
611
612 void rrdhost_system_info_to_node_info(struct rrdhost_system_info *system_info, struct update_node_info *node_info) {
613 node_info->data.os_name = system_info->host_os_name;
614 node_info->data.os_version = system_info->host_os_version;
615 node_info->data.kernel_name = system_info->kernel_name;
616 node_info->data.kernel_version = system_info->kernel_version;
617 node_info->data.architecture = system_info->architecture;
618 node_info->data.cpus = system_info->host_cores ? str2uint32_t(system_info->host_cores, NULL) : 0;
619 node_info->data.cpu_frequency = system_info->host_cpu_freq ? system_info->host_cpu_freq : "0";
620 node_info->data.memory = system_info->host_ram_total ? system_info->host_ram_total : "0";
621 node_info->data.disk_space = system_info->host_disk_space ? system_info->host_disk_space : "0";
622 node_info->data.virtualization_type = system_info->virtualization ? system_info->virtualization : "unknown";
623 node_info->data.container_type = system_info->container ? system_info->container : "unknown";
624 node_info->data.ml_info.ml_capable = system_info->ml_capable;
625 node_info->data.ml_info.ml_enabled = system_info->ml_enabled;
626 }
627
628 // emit system info as named key-value pairs into an open JSON object
629 void rrdhost_system_info_to_json_object_fields(BUFFER *wb, struct rrdhost_system_info *si) {
630 buffer_json_member_add_string(wb, "os_name", si && si->host_os_name ? si->host_os_name : "");
631 buffer_json_member_add_string(wb, "os_id", si && si->host_os_id ? si->host_os_id : "");
632 buffer_json_member_add_string(wb, "os_id_like", si && si->host_os_id_like ? si->host_os_id_like : "");
633 buffer_json_member_add_string(wb, "os_version", si && si->host_os_version ? si->host_os_version : "");
634 buffer_json_member_add_string(wb, "os_version_id", si && si->host_os_version_id ? si->host_os_version_id : "");
635 buffer_json_member_add_string(wb, "os_detection", si && si->host_os_detection ? si->host_os_detection : "");
636 buffer_json_member_add_uint64(wb, "cpu_cores", si && si->host_cores ? str2uint64_t(si->host_cores, NULL) : 0);
637 buffer_json_member_add_uint64(wb, "disk_space", si && si->host_disk_space ? str2uint64_t(si->host_disk_space, NULL) : 0);
638 buffer_json_member_add_uint64(wb, "cpu_freq", si && si->host_cpu_freq ? str2uint64_t(si->host_cpu_freq, NULL) : 0);
639 buffer_json_member_add_uint64(wb, "ram_total", si && si->host_ram_total ? str2uint64_t(si->host_ram_total, NULL) : 0);
640 buffer_json_member_add_string(wb, "container_os_name", si && si->container_os_name ? si->container_os_name : "");
641 buffer_json_member_add_string(wb, "container_os_id", si && si->container_os_id ? si->container_os_id : "");
642 buffer_json_member_add_string(wb, "container_os_id_like", si && si->container_os_id_like ? si->container_os_id_like : "");
643 buffer_json_member_add_string(wb, "container_os_version", si && si->container_os_version ? si->container_os_version : "");
644 buffer_json_member_add_string(wb, "container_os_version_id",si && si->container_os_version_id ? si->container_os_version_id : "");
645 buffer_json_member_add_string(wb, "container_os_detection", si && si->container_os_detection ? si->container_os_detection : "");
646 buffer_json_member_add_string(wb, "is_k8s_node", si && si->is_k8s_node ? si->is_k8s_node : "");
647 buffer_json_member_add_string(wb, "kernel_name", si && si->kernel_name ? si->kernel_name : "");
648 buffer_json_member_add_string(wb, "kernel_version", si && si->kernel_version ? si->kernel_version : "");
649 buffer_json_member_add_string(wb, "architecture", si && si->architecture ? si->architecture : "");
650 buffer_json_member_add_string(wb, "virtualization", si && si->virtualization ? si->virtualization : "");
651 buffer_json_member_add_string(wb, "virt_detection", si && si->virt_detection ? si->virt_detection : "");
652 buffer_json_member_add_string(wb, "container_type", si && si->container ? si->container : "");
653 buffer_json_member_add_string(wb, "container_detection", si && si->container_detection ? si->container_detection : "");
654 buffer_json_member_add_string(wb, "cloud_provider_type", si && si->cloud_provider_type ? si->cloud_provider_type : "");
655 buffer_json_member_add_string(wb, "cloud_instance_type", si && si->cloud_instance_type ? si->cloud_instance_type : "");
656 buffer_json_member_add_string(wb, "cloud_instance_region", si && si->cloud_instance_region ? si->cloud_instance_region : "");
657 }
658
659 void rrdhost_system_info_to_streaming_function_array(BUFFER *wb, struct rrdhost_system_info *system_info) {
660 if(system_info) {
661 buffer_json_add_array_item_string(wb, system_info->host_os_name ? system_info->host_os_name : "");
662 buffer_json_add_array_item_string(wb, system_info->host_os_id ? system_info->host_os_id : "");
663 buffer_json_add_array_item_string(wb, system_info->host_os_id_like ? system_info->host_os_id_like : "");
664 buffer_json_add_array_item_string(wb, system_info->host_os_version ? system_info->host_os_version : "");
665 buffer_json_add_array_item_string(wb, system_info->host_os_version_id ? system_info->host_os_version_id : "");
666 buffer_json_add_array_item_string(wb, system_info->host_os_detection ? system_info->host_os_detection : "");
667 buffer_json_add_array_item_string(wb, system_info->host_cores ? system_info->host_cores : "");
668 buffer_json_add_array_item_string(wb, system_info->host_disk_space ? system_info->host_disk_space : "");
669 buffer_json_add_array_item_string(wb, system_info->host_cpu_freq ? system_info->host_cpu_freq : "");
670 buffer_json_add_array_item_string(wb, system_info->host_ram_total ? system_info->host_ram_total : "");
671 buffer_json_add_array_item_string(wb, system_info->container_os_name ? system_info->container_os_name : "");
672 buffer_json_add_array_item_string(wb, system_info->container_os_id ? system_info->container_os_id : "");
673 buffer_json_add_array_item_string(wb, system_info->container_os_id_like ? system_info->container_os_id_like : "");
674 buffer_json_add_array_item_string(wb, system_info->container_os_version ? system_info->container_os_version : "");
675 buffer_json_add_array_item_string(wb, system_info->container_os_version_id ? system_info->container_os_version_id : "");
676 buffer_json_add_array_item_string(wb, system_info->container_os_detection ? system_info->container_os_detection : "");
677 buffer_json_add_array_item_string(wb, system_info->is_k8s_node ? system_info->is_k8s_node : "");
678 buffer_json_add_array_item_string(wb, system_info->kernel_name ? system_info->kernel_name : "");
679 buffer_json_add_array_item_string(wb, system_info->kernel_version ? system_info->kernel_version : "");
680 buffer_json_add_array_item_string(wb, system_info->architecture ? system_info->architecture : "");
681 buffer_json_add_array_item_string(wb, system_info->virtualization ? system_info->virtualization : "");
682 buffer_json_add_array_item_string(wb, system_info->virt_detection ? system_info->virt_detection : "");
683 buffer_json_add_array_item_string(wb, system_info->container ? system_info->container : "");
684 buffer_json_add_array_item_string(wb, system_info->container_detection ? system_info->container_detection : "");
685 buffer_json_add_array_item_string(wb, system_info->cloud_provider_type ? system_info->cloud_provider_type : "");
686 buffer_json_add_array_item_string(wb, system_info->cloud_instance_type ? system_info->cloud_instance_type : "");
687 buffer_json_add_array_item_string(wb, system_info->cloud_instance_region ? system_info->cloud_instance_region : "");
688 }
689 else {
690 buffer_json_add_array_item_string(wb, "");
691 buffer_json_add_array_item_string(wb, "");
692 buffer_json_add_array_item_string(wb, "");
693 buffer_json_add_array_item_string(wb, "");
694 buffer_json_add_array_item_string(wb, "");
695 buffer_json_add_array_item_string(wb, "");
696 buffer_json_add_array_item_string(wb, "");
697 buffer_json_add_array_item_string(wb, "");
698 buffer_json_add_array_item_string(wb, "");
699 buffer_json_add_array_item_string(wb, "");
700 buffer_json_add_array_item_string(wb, "");
701 buffer_json_add_array_item_string(wb, "");
702 buffer_json_add_array_item_string(wb, "");
703 buffer_json_add_array_item_string(wb, "");
704 buffer_json_add_array_item_string(wb, "");
705 buffer_json_add_array_item_string(wb, "");
706 buffer_json_add_array_item_string(wb, "");
707 buffer_json_add_array_item_string(wb, "");
708 buffer_json_add_array_item_string(wb, "");
709 buffer_json_add_array_item_string(wb, "");
710 buffer_json_add_array_item_string(wb, "");
711 buffer_json_add_array_item_string(wb, "");
712 buffer_json_add_array_item_string(wb, "");
713 buffer_json_add_array_item_string(wb, "");
714 buffer_json_add_array_item_string(wb, "");
715 buffer_json_add_array_item_string(wb, "");
716 buffer_json_add_array_item_string(wb, "");
717 }
718 }
719
720 bool get_daemon_status_fields_from_system_info(DAEMON_STATUS_FILE *ds) {
721 if(ds->read_system_info)
722 return false;
723
724 struct rrdhost_system_info *ri = (localhost && localhost->system_info) ? localhost->system_info : NULL;
725 if(!ri) {
726 // nothing we can do, let it be
727 return false;
728 }
729
730 if(ri->architecture)
731 strncpyz(ds->architecture, ri->architecture, sizeof(ds->architecture) - 1);
732
733 if(ri->virtualization)
734 strncpyz(ds->virtualization, ri->virtualization, sizeof(ds->virtualization) - 1);
735
736 if(ri->container)
737 strncpyz(ds->container, ri->container, sizeof(ds->container) - 1);
738
739 if(ri->kernel_version)
740 strncpyz(ds->kernel_version, ri->kernel_version, sizeof(ds->kernel_version) - 1);
741
742 if(ri->host_os_name)
743 strncpyz(ds->os_name, ri->host_os_name, sizeof(ds->os_name) - 1);
744
745 if(ri->host_os_version)
746 strncpyz(ds->os_version, ri->host_os_version, sizeof(ds->os_version) - 1);
747
748 if(ri->host_os_id)
749 strncpyz(ds->os_id, ri->host_os_id, sizeof(ds->os_id) - 1);
750
751 if(ri->host_os_id_like)
752 strncpyz(ds->os_id_like, ri->host_os_id_like, sizeof(ds->os_id_like) - 1);
753
754 if(ri->is_k8s_node) {
755 if (strcmp(ri->is_k8s_node, "true") == 0)
756 ds->kubernetes = true;
757 else
758 ds->kubernetes = false;
759 }
760
761 if(ri->cloud_provider_type && strcasecmp(ri->cloud_provider_type, "unknown") != 0)
762 strncpyz(ds->cloud_provider_type, ri->cloud_provider_type, sizeof(ds->cloud_provider_type) - 1);
763
764 if(ri->cloud_instance_type && strcasecmp(ri->cloud_instance_type, "unknown") != 0)
765 strncpyz(ds->cloud_instance_type, ri->cloud_instance_type, sizeof(ds->cloud_instance_type) - 1);
766
767 if(ri->cloud_instance_region && strcasecmp(ri->cloud_instance_region, "unknown") != 0)
768 strncpyz(ds->cloud_instance_region, ri->cloud_instance_region, sizeof(ds->cloud_instance_region) - 1);
769
770 ds->read_system_info = true;
771
772 return true;
773 }
774
775 bool localhost_is_docker() {
776 if (localhost && localhost->system_info) {
777 return (localhost->system_info->container && strcmp(localhost->system_info->container, "docker") == 0);
778 }
779 return false;
780 };