Provide UTC offset in seconds and edit health config command (#11051)
* add abbreviated timezone, utc offset in seconds, and edit health alarm command rebased * formating * use str2i instead of atoi
Emmanuel Vasilakis committed
May 31, 2021 at 16:29 UTC
9f40c4b12c1047c3489ba07dcf1c1e9b647d1835
14 files changed
+137
-14
daemon/analytics.c
+40
@@ -696,6 +696,46 @@ static void get_system_timezone(void)
696
timezone = "unknown";
697
698
netdata_configured_timezone = config_get(CONFIG_SECTION_GLOBAL, "timezone", timezone);
699
+
700
+ //get the utc offset, and the timezone as returned by strftime
701
+ //will be sent to the cloud
702
+ //Note: This will need an agent restart to get new offset on time change (dst, etc).
703
+ {
704
+ time_t t;
705
+ struct tm *tmp, tmbuf;
706
+ char zone[FILENAME_MAX + 1];
707
+ char sign[2], hh[3], mm[3];
708
+
709
+ t = now_realtime_sec();
710
+ tmp = localtime_r(&t, &tmbuf);
711
+
712
+ if (tmp != NULL) {
713
+ if (strftime(zone, FILENAME_MAX, "%Z", tmp) == 0) {
714
+ netdata_configured_abbrev_timezone = strdupz("UTC");
715
+ } else
716
+ netdata_configured_abbrev_timezone = strdupz(zone);
717
+
718
+ if (strftime(zone, FILENAME_MAX, "%z", tmp) == 0) {
719
+ netdata_configured_utc_offset = 0;
720
+ } else {
721
+ sign[0] = zone[0] == '-' || zone[0] == '+' ? zone[0] : '0';
722
+ sign[1] = '\0';
723
+ hh[0] = isdigit(zone[1]) ? zone[1] : '0';
724
+ hh[1] = isdigit(zone[2]) ? zone[2] : '0';
725
+ hh[2] = '\0';
726
+ mm[0] = isdigit(zone[3]) ? zone[3] : '0';
727
+ mm[1] = isdigit(zone[4]) ? zone[4] : '0';
728
+ mm[2] = '\0';
729
+
730
+ netdata_configured_utc_offset = (str2i(hh) * 3600) + (str2i(mm) * 60);
731
+ netdata_configured_utc_offset =
732
+ sign[0] == '-' ? -netdata_configured_utc_offset : netdata_configured_utc_offset;
733
+ }
734
+ } else {
735
+ netdata_configured_abbrev_timezone = strdupz("UTC");
736
+ netdata_configured_utc_offset = 0;
737
+ }
738
+ }
739
}
740
741
void set_global_environment()
daemon/common.c
+2
@@ -14,6 +14,8 @@ char *netdata_configured_lock_dir = NULL;
14
char *netdata_configured_home_dir = VARLIB_DIR;
15
char *netdata_configured_host_prefix = NULL;
16
char *netdata_configured_timezone = NULL;
17
+char *netdata_configured_abbrev_timezone = NULL;
18
+int32_t netdata_configured_utc_offset = 0;
19
int netdata_ready;
20
int netdata_cloud_setting;
21
daemon/common.h
+2
@@ -97,6 +97,8 @@ extern char *netdata_configured_lock_dir;
97
extern char *netdata_configured_home_dir;
98
extern char *netdata_configured_host_prefix;
99
extern char *netdata_configured_timezone;
100
+extern char *netdata_configured_abbrev_timezone;
101
+extern int32_t netdata_configured_utc_offset;
102
extern int netdata_zero_metrics_enabled;
103
extern int netdata_anonymous_statistics_enabled;
104
daemon/unit_test.c
+2
@@ -1500,6 +1500,8 @@ static RRDHOST *dbengine_rrdhost_find_or_create(char *name)
1500
, name
1501
, os_type
1502
, netdata_configured_timezone
1503
+ , netdata_configured_abbrev_timezone
1504
+ , netdata_configured_utc_offset
1505
, config_get(CONFIG_SECTION_BACKEND, "host tags", "")
1506
, program_name
1507
, program_version
database/rrd.h
+13
-5
@@ -764,10 +764,14 @@ struct rrdhost {
764
const char *os; // the O/S type of the host
765
const char *tags; // tags for this host
766
const char *timezone; // the timezone of the host
767
+
768
#ifdef ENABLE_ACLK
769
long obsolete_count;
770
#endif
771
772
+ const char *abbrev_timezone; // the abbriviated timezone of the host
773
+ int32_t utc_offset; // the offset in seconds from utc
774
+
775
RRDHOST_FLAGS flags; // flags about this RRDHOST
776
RRDHOST_FLAGS *exporting_flags; // array of flags for exporting connector instances
777
@@ -938,6 +942,8 @@ extern RRDHOST *rrdhost_find_or_create(
942
, const char *guid
943
, const char *os
944
, const char *timezone
945
+ , const char *abbrev_timezone
946
+ , int32_t utc_offset
947
, const char *tags
948
, const char *program_name
949
, const char *program_version
@@ -958,6 +964,8 @@ extern void rrdhost_update(RRDHOST *host
964
, const char *guid
965
, const char *os
966
, const char *timezone
967
+ , const char *abbrev_timezone
968
+ , int32_t utc_offset
969
, const char *tags
970
, const char *program_name
971
, const char *program_version
@@ -1325,17 +1333,17 @@ extern void rrdset_delete_obsolete_dimensions(RRDSET *st);
1333
extern void rrdhost_cleanup_obsolete_charts(RRDHOST *host);
1334
extern RRDHOST *rrdhost_create(
1335
const char *hostname, const char *registry_hostname, const char *guid, const char *os, const char *timezone,
1328
- const char *tags, const char *program_name, const char *program_version, int update_every, long entries,
1329
- RRD_MEMORY_MODE memory_mode, unsigned int health_enabled, unsigned int rrdpush_enabled, char *rrdpush_destination,
1330
- char *rrdpush_api_key, char *rrdpush_send_charts_matching, struct rrdhost_system_info *system_info,
1336
+ const char *abbrev_timezone, int32_t utc_offset,const char *tags, const char *program_name, const char *program_version,
1337
+ int update_every, long entries, RRD_MEMORY_MODE memory_mode, unsigned int health_enabled, unsigned int rrdpush_enabled,
1338
+ char *rrdpush_destination, char *rrdpush_api_key, char *rrdpush_send_charts_matching, struct rrdhost_system_info *system_info,
1339
int is_localhost); //TODO: Remove , int is_archived);
1340
1341
#endif /* NETDATA_RRD_INTERNALS */
1342
1343
extern void set_host_properties(
1344
RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *hostname, const char *registry_hostname,
1337
- const char *guid, const char *os, const char *tags, const char *tzone, const char *program_name,
1338
- const char *program_version);
1345
+ const char *guid, const char *os, const char *tags, const char *tzone, const char *abbrev_tzone, int32_t utc_offset,
1346
+ const char *program_name, const char *program_version);
1347
1348
// ----------------------------------------------------------------------------
1349
// RRD DB engine declarations
database/rrdhost.c
+27
-6
@@ -88,13 +88,20 @@ static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
88
freez(old);
89
}
90
91
-static inline void rrdhost_init_timezone(RRDHOST *host, const char *timezone) {
92
- if(host->timezone && timezone && !strcmp(host->timezone, timezone))
91
+static inline void rrdhost_init_timezone(RRDHOST *host, const char *timezone, const char *abbrev_timezone, int32_t utc_offset) {
92
+ if (host->timezone && timezone && !strcmp(host->timezone, timezone) && host->abbrev_timezone && abbrev_timezone &&
93
+ !strcmp(host->abbrev_timezone, abbrev_timezone) && host->utc_offset == utc_offset)
94
return;
95
96
void *old = (void *)host->timezone;
97
host->timezone = strdupz((timezone && *timezone)?timezone:"unknown");
98
freez(old);
99
+
100
+ old = (void *)host->abbrev_timezone;
101
+ host->abbrev_timezone = strdupz((abbrev_timezone && *abbrev_timezone) ? abbrev_timezone : "UTC");
102
+ freez(old);
103
+
104
+ host->utc_offset = utc_offset;
105
}
106
107
static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
@@ -105,7 +112,8 @@ static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_
112
113
void set_host_properties(RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *hostname,
114
const char *registry_hostname, const char *guid, const char *os, const char *tags,
108
- const char *tzone, const char *program_name, const char *program_version)
115
+ const char *tzone, const char *abbrev_tzone, int32_t utc_offset, const char *program_name,
116
+ const char *program_version)
117
{
118
119
host->rrd_update_every = update_every;
@@ -116,7 +124,7 @@ void set_host_properties(RRDHOST *host, int update_every, RRD_MEMORY_MODE memory
124
rrdhost_init_machine_guid(host, guid);
125
126
rrdhost_init_os(host, os);
119
- rrdhost_init_timezone(host, tzone);
127
+ rrdhost_init_timezone(host, tzone, abbrev_tzone, utc_offset);
128
rrdhost_init_tags(host, tags);
129
130
host->program_name = strdupz((program_name && *program_name) ? program_name : "unknown");
@@ -133,6 +141,8 @@ RRDHOST *rrdhost_create(const char *hostname,
141
const char *guid,
142
const char *os,
143
const char *timezone,
144
+ const char *abbrev_timezone,
145
+ int32_t utc_offset,
146
const char *tags,
147
const char *program_name,
148
const char *program_version,
@@ -160,7 +170,7 @@ RRDHOST *rrdhost_create(const char *hostname,
170
RRDHOST *host = callocz(1, sizeof(RRDHOST));
171
172
set_host_properties(host, (update_every > 0)?update_every:1, memory_mode, hostname, registry_hostname, guid, os,
163
- tags, timezone, program_name, program_version);
173
+ tags, timezone, abbrev_timezone, utc_offset, program_name, program_version);
174
175
host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries);
176
host->health_enabled = ((memory_mode == RRD_MEMORY_MODE_NONE)) ? 0 : health_enabled;
@@ -408,6 +418,8 @@ void rrdhost_update(RRDHOST *host
418
, const char *guid
419
, const char *os
420
, const char *timezone
421
+ , const char *abbrev_timezone
422
+ , int32_t utc_offset
423
, const char *tags
424
, const char *program_name
425
, const char *program_version
@@ -435,7 +447,7 @@ void rrdhost_update(RRDHOST *host
447
host->system_info = system_info;
448
449
rrdhost_init_os(host, os);
438
- rrdhost_init_timezone(host, timezone);
450
+ rrdhost_init_timezone(host, timezone, abbrev_timezone, utc_offset);
451
452
freez(host->registry_hostname);
453
host->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
@@ -510,6 +522,8 @@ RRDHOST *rrdhost_find_or_create(
522
, const char *guid
523
, const char *os
524
, const char *timezone
525
+ , const char *abbrev_timezone
526
+ , int32_t utc_offset
527
, const char *tags
528
, const char *program_name
529
, const char *program_version
@@ -541,6 +555,8 @@ RRDHOST *rrdhost_find_or_create(
555
, guid
556
, os
557
, timezone
558
+ , abbrev_timezone
559
+ , utc_offset
560
, tags
561
, program_name
562
, program_version
@@ -563,6 +579,8 @@ RRDHOST *rrdhost_find_or_create(
579
, guid
580
, os
581
, timezone
582
+ , abbrev_timezone
583
+ , utc_offset
584
, tags
585
, program_name
586
, program_version
@@ -654,6 +672,8 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
672
, registry_get_this_machine_guid()
673
, os_type
674
, netdata_configured_timezone
675
+ , netdata_configured_abbrev_timezone
676
+ , netdata_configured_utc_offset
677
, config_get(CONFIG_SECTION_BACKEND, "host tags", "")
678
, program_name
679
, program_version
@@ -883,6 +903,7 @@ void rrdhost_free(RRDHOST *host) {
903
free_label_list(host->labels.head);
904
freez((void *)host->os);
905
freez((void *)host->timezone);
906
+ freez((void *)host->abbrev_timezone);
907
freez(host->program_version);
908
freez(host->program_name);
909
rrdhost_system_info_free(host->system_info);
database/sqlite/sqlite_functions.c
+1
-1
@@ -984,7 +984,7 @@ RRDHOST *sql_create_host_by_uuid(char *hostname)
984
set_host_properties(host, sqlite3_column_int(res, 2), RRD_MEMORY_MODE_DBENGINE, hostname,
985
(char *) sqlite3_column_text(res, 1), (const char *) uuid_str,
986
(char *) sqlite3_column_text(res, 3), (char *) sqlite3_column_text(res, 5),
987
- (char *) sqlite3_column_text(res, 4), NULL, NULL);
987
+ (char *) sqlite3_column_text(res, 4), NULL, 0, NULL, NULL);
988
989
uuid_copy(host->host_uuid, *((uuid_t *) sqlite3_column_blob(res, 0)));
990
health/health.h
+2
@@ -96,6 +96,8 @@ extern void *health_cmdapi_thread(void *ptr);
96
97
extern void health_label_log_save(RRDHOST *host);
98
99
+extern char *health_edit_command_from_source(const char *source);
100
+
101
extern SIMPLE_PATTERN *health_pattern_from_foreach(char *s);
102
103
#endif //NETDATA_HEALTH_H
health/health_config.c
+23
@@ -473,6 +473,29 @@ static inline char *health_source_file(size_t line, const char *file) {
473
return strdupz(buffer);
474
}
475
476
+char *health_edit_command_from_source(const char *source)
477
+{
478
+ char buffer[FILENAME_MAX + 1];
479
+ char *temp = strdupz(source);
480
+ char *line_num = strchr(temp, '@');
481
+ char *file_no_path = strrchr(temp, '/');
482
+
483
+ if (likely(file_no_path && line_num)) {
484
+ *line_num = '\0';
485
+ snprintfz(
486
+ buffer,
487
+ FILENAME_MAX,
488
+ "sudo %s/edit-config health.d/%s=%s",
489
+ netdata_configured_user_config_dir,
490
+ file_no_path + 1,
491
+ temp);
492
+ } else
493
+ buffer[0] = '\0';
494
+
495
+ freez(temp);
496
+ return strdupz(buffer);
497
+}
498
+
499
static inline void strip_quotes(char *s) {
500
while(*s) {
501
if(*s == '\'' || *s == '"') *s = ' ';
health/health_json.c
+9
@@ -14,9 +14,13 @@ void health_string2json(BUFFER *wb, const char *prefix, const char *label, const
14
}
15
16
void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host) {
17
+ char *edit_command = ae->source ? health_edit_command_from_source(ae->source) : strdupz("UNKNOWN=0");
18
+
19
buffer_sprintf(wb,
20
"\n\t{\n"
21
"\t\t\"hostname\": \"%s\",\n"
22
+ "\t\t\"utc_offset\": %d,\n"
23
+ "\t\t\"timezone\": \"%s\",\n"
24
"\t\t\"unique_id\": %u,\n"
25
"\t\t\"alarm_id\": %u,\n"
26
"\t\t\"alarm_event_id\": %u,\n"
@@ -34,6 +38,7 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
38
"\t\t\"recipient\": \"%s\",\n"
39
"\t\t\"exec_code\": %d,\n"
40
"\t\t\"source\": \"%s\",\n"
41
+ "\t\t\"command\": \"%s\",\n"
42
"\t\t\"units\": \"%s\",\n"
43
"\t\t\"when\": %lu,\n"
44
"\t\t\"duration\": %lu,\n"
@@ -49,6 +54,8 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
54
"\t\t\"last_repeat\": \"%lu\",\n"
55
"\t\t\"silenced\": \"%s\",\n"
56
, host->hostname
57
+ , host->utc_offset
58
+ , host->abbrev_timezone
59
, ae->unique_id
60
, ae->alarm_id
61
, ae->alarm_event_id
@@ -66,6 +73,7 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
73
, ae->recipient?ae->recipient:host->health_default_recipient
74
, ae->exec_code
75
, ae->source
76
+ , edit_command
77
, ae->units?ae->units:""
78
, (unsigned long)ae->when
79
, (unsigned long)ae->duration
@@ -114,6 +122,7 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
122
buffer_strcat(wb, "\t}");
123
124
freez(replaced_info);
125
+ freez(edit_command);
126
}
127
128
void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart) {
streaming/receiver.c
+3
@@ -11,6 +11,7 @@ void destroy_receiver_state(struct receiver_state *rpt) {
11
freez(rpt->machine_guid);
12
freez(rpt->os);
13
freez(rpt->timezone);
14
+ freez(rpt->abbrev_timezone);
15
freez(rpt->tags);
16
freez(rpt->client_ip);
17
freez(rpt->client_port);
@@ -307,6 +308,8 @@ static int rrdpush_receive(struct receiver_state *rpt)
308
, rpt->machine_guid
309
, rpt->os
310
, rpt->timezone
311
+ , rpt->abbrev_timezone
312
+ , rpt->utc_offset
313
, rpt->tags
314
, rpt->program_name
315
, rpt->program_version
streaming/rrdpush.c
+8
-1
@@ -464,7 +464,8 @@ void *rrdpush_receiver_thread(void *ptr);
464
int rrdpush_receiver_thread_spawn(struct web_client *w, char *url) {
465
info("clients wants to STREAM metrics.");
466
467
- char *key = NULL, *hostname = NULL, *registry_hostname = NULL, *machine_guid = NULL, *os = "unknown", *timezone = "unknown", *tags = NULL;
467
+ char *key = NULL, *hostname = NULL, *registry_hostname = NULL, *machine_guid = NULL, *os = "unknown", *timezone = "unknown", *abbrev_timezone = "UTC", *tags = NULL;
468
+ int32_t utc_offset = 0;
469
int update_every = default_rrd_update_every;
470
uint32_t stream_version = UINT_MAX;
471
char buf[GUID_LEN + 1];
@@ -493,6 +494,10 @@ int rrdpush_receiver_thread_spawn(struct web_client *w, char *url) {
494
os = value;
495
else if(!strcmp(name, "timezone"))
496
timezone = value;
497
+ else if(!strcmp(name, "abbrev_timezone"))
498
+ abbrev_timezone = value;
499
+ else if(!strcmp(name, "utc_offset"))
500
+ utc_offset = (int32_t)strtol(value, NULL, 0);
501
else if(!strcmp(name, "tags"))
502
tags = value;
503
else if(!strcmp(name, "ver"))
@@ -680,6 +685,8 @@ int rrdpush_receiver_thread_spawn(struct web_client *w, char *url) {
685
rpt->machine_guid = strdupz(machine_guid);
686
rpt->os = strdupz(os);
687
rpt->timezone = strdupz(timezone);
688
+ rpt->abbrev_timezone = strdupz(abbrev_timezone);
689
+ rpt->utc_offset = utc_offset;
690
rpt->tags = (tags)?strdupz(tags):NULL;
691
rpt->client_ip = strdupz(w->client_ip);
692
rpt->client_port = strdupz(w->client_port);
streaming/rrdpush.h
+2
@@ -72,6 +72,8 @@ struct receiver_state {
72
char *machine_guid;
73
char *os;
74
char *timezone; // Unused?
75
+ char *abbrev_timezone;
76
+ int32_t utc_offset;
77
char *tags;
78
char *client_ip; // Duplicated in pluginsd
79
char *client_port; // Duplicated in pluginsd
streaming/sender.c
+3
-1
@@ -214,7 +214,7 @@ static int rrdpush_sender_thread_connect_to_parent(RRDHOST *host, int default_po
214
215
char http[HTTP_HEADER_SIZE + 1];
216
int eol = snprintfz(http, HTTP_HEADER_SIZE,
217
- "STREAM key=%s&hostname=%s®istry_hostname=%s&machine_guid=%s&update_every=%d&os=%s&timezone=%s&tags=%s&ver=%u"
217
+ "STREAM key=%s&hostname=%s®istry_hostname=%s&machine_guid=%s&update_every=%d&os=%s&timezone=%s&abbrev_timezone=%s&utc_offset=%d&tags=%s&ver=%u"
218
"&NETDATA_SYSTEM_OS_NAME=%s"
219
"&NETDATA_SYSTEM_OS_ID=%s"
220
"&NETDATA_SYSTEM_OS_ID_LIKE=%s"
@@ -250,6 +250,8 @@ static int rrdpush_sender_thread_connect_to_parent(RRDHOST *host, int default_po
250
, default_rrd_update_every
251
, host->os
252
, host->timezone
253
+ , host->abbrev_timezone
254
+ , host->utc_offset
255
, (host->tags) ? host->tags : ""
256
, STREAMING_PROTOCOL_CURRENT_VERSION
257
, se.os_name