@cryptotaxi247 / netdata-1 / commits / f70a97206

Add install type info to `-W buildinfo` output. (#12010)

* Add install type info to `-W buildinfo` ouptut. By reading it from the `.install-type` file and presenting it properly. * Move get_value_from_key from daemon/analytics to libnetdata. It will be used also in the buildinfo code. * Restructure install type handling for buildinfo. * Restructure to make code more reusable. Allowing for deduplication and also enabling other potential callers. * Fix incorrect variable name in analytics changes.

Austin S. Hemmelgarn committed Jan 24, 2022 at 12:25 UTC f70a97206e99654c08e0714aeff3f5290f2f40c4
3 files changed +86 -37
daemon/analytics.c
+13 -36
@@ -1,6 +1,7 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "common.h"
4 +#include "buildinfo.h"
5
6 struct analytics_data analytics_data;
7 extern void analytics_exporting_connectors (BUFFER *b);
@@ -359,47 +360,23 @@ void analytics_alarms_notifications(void)
360 buffer_free(b);
361 }
362
362 -char *get_value_from_key(char *buffer, char *key)
363 -{
364 - char *s = NULL, *t = NULL;
365 - s = t = buffer + strlen(key) + 2;
366 - if (s) {
367 - while (*s == '\'')
368 - s++;
369 - while (*++t != '\0');
370 - while (--t > s && *t == '\'')
371 - *t = '\0';
372 - }
373 - return s;
374 -}
375 -
376 -/*
377 - * Checks for the existence of .install_type file and reads it
378 - */
363 void analytics_get_install_type(void)
364 {
381 - char *install_type_filename;
382 - analytics_set_data_str(&analytics_data.netdata_install_type, "");
383 - analytics_set_data_str(&analytics_data.netdata_prebuilt_distro, "");
365 + struct install_type_info t = get_install_type();
366
385 - int install_type_filename_len = (strlen(netdata_configured_user_config_dir) + strlen(".install-type") + 3);
386 - install_type_filename = mallocz(sizeof(char) * install_type_filename_len);
387 - snprintfz(install_type_filename, install_type_filename_len - 1, "%s/%s", netdata_configured_user_config_dir, ".install-type");
367 + if (t.install_type == NULL) {
368 + analytics_set_data_str(&analytics_data.netdata_install_type, "unknown");
369 + } else {
370 + analytics_set_data_str(&analytics_data.netdata_install_type, t.install_type);
371 + }
372
389 - FILE *fp = fopen(install_type_filename, "r");
390 - if (fp) {
391 - char *s, buf[256 + 1];
392 - size_t len = 0;
393 -
394 - while ((s = fgets_trim_len(buf, 256, fp, &len))) {
395 - if (!strncmp(buf, "INSTALL_TYPE='", 14))
396 - analytics_set_data_str(&analytics_data.netdata_install_type, (char *)get_value_from_key(buf, "INSTALL_TYPE"));
397 - else if (!strncmp(buf, "PREBUILT_DISTRO='", 17))
398 - analytics_set_data_str(&analytics_data.netdata_prebuilt_distro, (char *)get_value_from_key(buf, "PREBUILT_DISTRO"));
399 - }
400 - fclose(fp);
373 + if (t.prebuilt_distro != NULL) {
374 + analytics_set_data_str(&analytics_data.netdata_prebuilt_distro, t.prebuilt_distro);
375 }
402 - freez(install_type_filename);
376 +
377 + freez(t.prebuilt_arch);
378 + freez(t.prebuilt_distro);
379 + freez(t.install_type);
380 }
381
382 /*
daemon/buildinfo.c
+63 -1
@@ -3,6 +3,7 @@
3 #include <stdio.h>
4 #include "./config.h"
5 #include "common.h"
6 +#include "buildinfo.h"
7
8 // Optional features
9
@@ -207,9 +208,71 @@
208
209 #define FEAT_YES_NO(x) ((x) ? "YES" : "NO")
210
211 +
212 +char *get_value_from_key(char *buffer, char *key) {
213 + char *s = NULL, *t = NULL;
214 + s = t = buffer + strlen(key) + 2;
215 + if (s) {
216 + while (*s == '\'')
217 + s++;
218 + while (*++t != '\0');
219 + while (--t > s && *t == '\'')
220 + *t = '\0';
221 + }
222 + return s;
223 +}
224 +
225 +struct install_type_info get_install_type() {
226 + char *install_type_filename;
227 + struct install_type_info ret = {.install_type = NULL, .prebuilt_arch = NULL, .prebuilt_distro = NULL};
228 +
229 + int install_type_filename_len = (strlen(netdata_configured_user_config_dir) + strlen(".install-type") + 3);
230 + install_type_filename = mallocz(sizeof(char) * install_type_filename_len);
231 + snprintfz(install_type_filename, install_type_filename_len - 1, "%s/%s", netdata_configured_user_config_dir, ".install-type");
232 +
233 + FILE *fp = fopen(install_type_filename, "r");
234 + if (fp) {
235 + char *s, buf[256 + 1];
236 + size_t len = 0;
237 +
238 + while ((s = fgets_trim_len(buf, 256, fp, &len))) {
239 + if (!strncmp(buf, "INSTALL_TYPE='", 14))
240 + ret.install_type = strdupz((char *)get_value_from_key(buf, "INSTALL_TYPE"));
241 + else if (!strncmp(buf, "PREBUILT_ARCH='", 15))
242 + ret.prebuilt_arch = strdupz((char *)get_value_from_key(buf, "PREBUILT_ARCH"));
243 + else if (!strncmp(buf, "PREBUILT_DISTRO='", 17))
244 + ret.prebuilt_distro = strdupz((char *)get_value_from_key(buf, "PREBUILT_DISTRO"));
245 + }
246 + fclose(fp);
247 + }
248 + freez(install_type_filename);
249 +
250 + return ret;
251 +}
252 +
253 void print_build_info(void) {
254 + struct install_type_info t = get_install_type();
255 +
256 printf("Configure options: %s\n", CONFIGURE_COMMAND);
257
258 + if (t.install_type == NULL) {
259 + printf("Install type: unknown\n");
260 + } else {
261 + printf("Install type: %s\n", t.install_type);
262 + }
263 +
264 + if (t.prebuilt_arch != NULL) {
265 + printf(" Binary architecture: %s\n", t.prebuilt_arch);
266 + }
267 +
268 + if (t.prebuilt_distro != NULL) {
269 + printf(" Packaging distro: %s\n", t.prebuilt_distro);
270 + }
271 +
272 + freez(t.install_type);
273 + freez(t.prebuilt_arch);
274 + freez(t.prebuilt_distro);
275 +
276 printf("Features:\n");
277 printf(" dbengine: %s\n", FEAT_YES_NO(FEAT_DBENGINE));
278 printf(" Native HTTPS: %s\n", FEAT_YES_NO(FEAT_NATIVE_HTTPS));
@@ -250,7 +313,6 @@ void print_build_info(void) {
313 printf(" Prometheus Remote Write: %s\n", FEAT_YES_NO(FEAT_REMOTE_WRITE));
314 };
315
253 -
316 #define FEAT_JSON_BOOL(x) ((x) ? "true" : "false")
317 // This intentionally does not use JSON-C so it works even if JSON-C is not present
318 // This is used for anonymous statistics reporting, so it intentionally
daemon/buildinfo.h
+10
@@ -3,8 +3,18 @@
3 #ifndef NETDATA_BUILDINFO_H
4 #define NETDATA_BUILDINFO_H 1
5
6 +struct install_type_info {
7 + char *install_type;
8 + char *prebuilt_arch;
9 + char *prebuilt_distro;
10 +};
11 +
12 extern void print_build_info(void);
13
14 extern void print_build_info_json(void);
15
16 +extern char *get_value_from_key(char *buffer, char *key);
17 +
18 +extern struct install_type_info get_install_type();
19 +
20 #endif // NETDATA_BUILDINFO_H