Просмотр исходного кода

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 3 лет назад
Родитель
Сommit
9f40c4b12c

+ 40 - 0
daemon/analytics.c

@@ -696,6 +696,46 @@ static void get_system_timezone(void)
         timezone = "unknown";
 
     netdata_configured_timezone = config_get(CONFIG_SECTION_GLOBAL, "timezone", timezone);
+
+    //get the utc offset, and the timezone as returned by strftime
+    //will be sent to the cloud
+    //Note: This will need an agent restart to get new offset on time change (dst, etc).
+    {
+        time_t t;
+        struct tm *tmp, tmbuf;
+        char zone[FILENAME_MAX + 1];
+        char sign[2], hh[3], mm[3];
+
+        t = now_realtime_sec();
+        tmp = localtime_r(&t, &tmbuf);
+
+        if (tmp != NULL) {
+            if (strftime(zone, FILENAME_MAX, "%Z", tmp) == 0) {
+                netdata_configured_abbrev_timezone = strdupz("UTC");
+            } else
+                netdata_configured_abbrev_timezone = strdupz(zone);
+
+            if (strftime(zone, FILENAME_MAX, "%z", tmp) == 0) {
+                netdata_configured_utc_offset = 0;
+            } else {
+                sign[0] = zone[0] == '-' || zone[0] == '+' ? zone[0] : '0';
+                sign[1] = '\0';
+                hh[0] = isdigit(zone[1]) ? zone[1] : '0';
+                hh[1] = isdigit(zone[2]) ? zone[2] : '0';
+                hh[2] = '\0';
+                mm[0] = isdigit(zone[3]) ? zone[3] : '0';
+                mm[1] = isdigit(zone[4]) ? zone[4] : '0';
+                mm[2] = '\0';
+
+                netdata_configured_utc_offset = (str2i(hh) * 3600) + (str2i(mm) * 60);
+                netdata_configured_utc_offset =
+                    sign[0] == '-' ? -netdata_configured_utc_offset : netdata_configured_utc_offset;
+            }
+        } else {
+            netdata_configured_abbrev_timezone = strdupz("UTC");
+            netdata_configured_utc_offset = 0;
+        }
+    }
 }
 
 void set_global_environment()

+ 2 - 0
daemon/common.c

@@ -14,6 +14,8 @@ char *netdata_configured_lock_dir            = NULL;
 char *netdata_configured_home_dir            = VARLIB_DIR;
 char *netdata_configured_host_prefix         = NULL;
 char *netdata_configured_timezone            = NULL;
+char *netdata_configured_abbrev_timezone     = NULL;
+int32_t netdata_configured_utc_offset        = 0;
 int netdata_ready;
 int netdata_cloud_setting;
 

+ 2 - 0
daemon/common.h

@@ -97,6 +97,8 @@ extern char *netdata_configured_lock_dir;
 extern char *netdata_configured_home_dir;
 extern char *netdata_configured_host_prefix;
 extern char *netdata_configured_timezone;
+extern char *netdata_configured_abbrev_timezone;
+extern int32_t netdata_configured_utc_offset;
 extern int netdata_zero_metrics_enabled;
 extern int netdata_anonymous_statistics_enabled;
 

+ 2 - 0
daemon/unit_test.c

@@ -1500,6 +1500,8 @@ static RRDHOST *dbengine_rrdhost_find_or_create(char *name)
             , name
             , os_type
             , netdata_configured_timezone
+            , netdata_configured_abbrev_timezone
+            , netdata_configured_utc_offset
             , config_get(CONFIG_SECTION_BACKEND, "host tags", "")
             , program_name
             , program_version

+ 13 - 5
database/rrd.h

@@ -764,10 +764,14 @@ struct rrdhost {
     const char *os;                                 // the O/S type of the host
     const char *tags;                               // tags for this host
     const char *timezone;                           // the timezone of the host
+
 #ifdef ENABLE_ACLK
     long    obsolete_count;
 #endif
 
+    const char *abbrev_timezone;                    // the abbriviated timezone of the host
+    int32_t utc_offset;                             // the offset in seconds from utc
+
     RRDHOST_FLAGS flags;                            // flags about this RRDHOST
     RRDHOST_FLAGS *exporting_flags;                 // array of flags for exporting connector instances
 
@@ -938,6 +942,8 @@ extern RRDHOST *rrdhost_find_or_create(
         , const char *guid
         , const char *os
         , const char *timezone
+        , const char *abbrev_timezone
+        , int32_t utc_offset
         , const char *tags
         , const char *program_name
         , const char *program_version
@@ -958,6 +964,8 @@ extern void rrdhost_update(RRDHOST *host
     , const char *guid
     , const char *os
     , const char *timezone
+    , const char *abbrev_timezone
+    , int32_t utc_offset
     , const char *tags
     , const char *program_name
     , const char *program_version
@@ -1325,17 +1333,17 @@ extern void rrdset_delete_obsolete_dimensions(RRDSET *st);
 extern void rrdhost_cleanup_obsolete_charts(RRDHOST *host);
 extern RRDHOST *rrdhost_create(
     const char *hostname, const char *registry_hostname, const char *guid, const char *os, const char *timezone,
-    const char *tags, const char *program_name, const char *program_version, int update_every, long entries,
-    RRD_MEMORY_MODE memory_mode, unsigned int health_enabled, unsigned int rrdpush_enabled, char *rrdpush_destination,
-    char *rrdpush_api_key, char *rrdpush_send_charts_matching, struct rrdhost_system_info *system_info,
+    const char *abbrev_timezone, int32_t utc_offset,const char *tags, const char *program_name, const char *program_version,
+    int update_every, long entries, RRD_MEMORY_MODE memory_mode, unsigned int health_enabled, unsigned int rrdpush_enabled,
+    char *rrdpush_destination, char *rrdpush_api_key, char *rrdpush_send_charts_matching, struct rrdhost_system_info *system_info,
     int is_localhost); //TODO: Remove , int is_archived);
 
 #endif /* NETDATA_RRD_INTERNALS */
 
 extern void set_host_properties(
     RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *hostname, const char *registry_hostname,
-    const char *guid, const char *os, const char *tags, const char *tzone, const char *program_name,
-    const char *program_version);
+    const char *guid, const char *os, const char *tags, const char *tzone, const char *abbrev_tzone, int32_t utc_offset,
+    const char *program_name, const char *program_version);
 
 // ----------------------------------------------------------------------------
 // RRD DB engine declarations

+ 27 - 6
database/rrdhost.c

@@ -88,13 +88,20 @@ static inline void rrdhost_init_os(RRDHOST *host, const char *os) {
     freez(old);
 }
 
-static inline void rrdhost_init_timezone(RRDHOST *host, const char *timezone) {
-    if(host->timezone && timezone && !strcmp(host->timezone, timezone))
+static inline void rrdhost_init_timezone(RRDHOST *host, const char *timezone, const char *abbrev_timezone, int32_t utc_offset) {
+    if (host->timezone && timezone && !strcmp(host->timezone, timezone) && host->abbrev_timezone && abbrev_timezone &&
+        !strcmp(host->abbrev_timezone, abbrev_timezone) && host->utc_offset == utc_offset)
         return;
 
     void *old = (void *)host->timezone;
     host->timezone = strdupz((timezone && *timezone)?timezone:"unknown");
     freez(old);
+
+    old = (void *)host->abbrev_timezone;
+    host->abbrev_timezone = strdupz((abbrev_timezone && *abbrev_timezone) ? abbrev_timezone : "UTC");
+    freez(old);
+
+    host->utc_offset = utc_offset;
 }
 
 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_
 
 void set_host_properties(RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *hostname,
                          const char *registry_hostname, const char *guid, const char *os, const char *tags,
-                         const char *tzone, const char *program_name, const char *program_version)
+                         const char *tzone, const char *abbrev_tzone, int32_t utc_offset, const char *program_name,
+                         const char *program_version)
 {
 
     host->rrd_update_every = update_every;
@@ -116,7 +124,7 @@ void set_host_properties(RRDHOST *host, int update_every, RRD_MEMORY_MODE memory
     rrdhost_init_machine_guid(host, guid);
 
     rrdhost_init_os(host, os);
-    rrdhost_init_timezone(host, tzone);
+    rrdhost_init_timezone(host, tzone, abbrev_tzone, utc_offset);
     rrdhost_init_tags(host, tags);
 
     host->program_name = strdupz((program_name && *program_name) ? program_name : "unknown");
@@ -133,6 +141,8 @@ RRDHOST *rrdhost_create(const char *hostname,
                         const char *guid,
                         const char *os,
                         const char *timezone,
+                        const char *abbrev_timezone,
+                        int32_t utc_offset,
                         const char *tags,
                         const char *program_name,
                         const char *program_version,
@@ -160,7 +170,7 @@ RRDHOST *rrdhost_create(const char *hostname,
     RRDHOST *host = callocz(1, sizeof(RRDHOST));
 
     set_host_properties(host, (update_every > 0)?update_every:1, memory_mode, hostname, registry_hostname, guid, os,
-                        tags, timezone, program_name, program_version);
+                        tags, timezone, abbrev_timezone, utc_offset, program_name, program_version);
 
     host->rrd_history_entries = align_entries_to_pagesize(memory_mode, entries);
     host->health_enabled      = ((memory_mode == RRD_MEMORY_MODE_NONE)) ? 0 : health_enabled;
@@ -408,6 +418,8 @@ void rrdhost_update(RRDHOST *host
                     , const char *guid
                     , const char *os
                     , const char *timezone
+                    , const char *abbrev_timezone
+                    , int32_t utc_offset
                     , const char *tags
                     , const char *program_name
                     , const char *program_version
@@ -435,7 +447,7 @@ void rrdhost_update(RRDHOST *host
     host->system_info = system_info;
 
     rrdhost_init_os(host, os);
-    rrdhost_init_timezone(host, timezone);
+    rrdhost_init_timezone(host, timezone, abbrev_timezone, utc_offset);
 
     freez(host->registry_hostname);
     host->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname);
@@ -510,6 +522,8 @@ RRDHOST *rrdhost_find_or_create(
         , const char *guid
         , const char *os
         , const char *timezone
+        , const char *abbrev_timezone
+        , int32_t utc_offset
         , const char *tags
         , const char *program_name
         , const char *program_version
@@ -541,6 +555,8 @@ RRDHOST *rrdhost_find_or_create(
                 , guid
                 , os
                 , timezone
+                , abbrev_timezone
+                , utc_offset
                 , tags
                 , program_name
                 , program_version
@@ -563,6 +579,8 @@ RRDHOST *rrdhost_find_or_create(
            , guid
            , os
            , timezone
+           , abbrev_timezone
+           , utc_offset
            , tags
            , program_name
            , program_version
@@ -654,6 +672,8 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
             , registry_get_this_machine_guid()
             , os_type
             , netdata_configured_timezone
+            , netdata_configured_abbrev_timezone
+            , netdata_configured_utc_offset
             , config_get(CONFIG_SECTION_BACKEND, "host tags", "")
             , program_name
             , program_version
@@ -883,6 +903,7 @@ void rrdhost_free(RRDHOST *host) {
     free_label_list(host->labels.head);
     freez((void *)host->os);
     freez((void *)host->timezone);
+    freez((void *)host->abbrev_timezone);
     freez(host->program_version);
     freez(host->program_name);
     rrdhost_system_info_free(host->system_info);

+ 1 - 1
database/sqlite/sqlite_functions.c

@@ -984,7 +984,7 @@ RRDHOST *sql_create_host_by_uuid(char *hostname)
     set_host_properties(host, sqlite3_column_int(res, 2), RRD_MEMORY_MODE_DBENGINE, hostname,
                         (char *) sqlite3_column_text(res, 1), (const char *) uuid_str,
                         (char *) sqlite3_column_text(res, 3), (char *) sqlite3_column_text(res, 5),
-                        (char *) sqlite3_column_text(res, 4), NULL, NULL);
+                        (char *) sqlite3_column_text(res, 4), NULL, 0, NULL, NULL);
 
     uuid_copy(host->host_uuid, *((uuid_t *) sqlite3_column_blob(res, 0)));
 

+ 2 - 0
health/health.h

@@ -96,6 +96,8 @@ extern void *health_cmdapi_thread(void *ptr);
 
 extern void health_label_log_save(RRDHOST *host);
 
+extern char *health_edit_command_from_source(const char *source);
+
 extern SIMPLE_PATTERN *health_pattern_from_foreach(char *s);
 
 #endif //NETDATA_HEALTH_H

+ 23 - 0
health/health_config.c

@@ -473,6 +473,29 @@ static inline char *health_source_file(size_t line, const char *file) {
     return strdupz(buffer);
 }
 
+char *health_edit_command_from_source(const char *source)
+{
+    char buffer[FILENAME_MAX + 1];
+    char *temp = strdupz(source);
+    char *line_num = strchr(temp, '@');
+    char *file_no_path = strrchr(temp, '/');
+
+    if (likely(file_no_path && line_num)) {
+        *line_num = '\0';
+        snprintfz(
+            buffer,
+            FILENAME_MAX,
+            "sudo %s/edit-config health.d/%s=%s",
+            netdata_configured_user_config_dir,
+            file_no_path + 1,
+            temp);
+    } else
+        buffer[0] = '\0';
+
+    freez(temp);
+    return strdupz(buffer);
+}
+
 static inline void strip_quotes(char *s) {
     while(*s) {
         if(*s == '\'' || *s == '"') *s = ' ';

+ 9 - 0
health/health_json.c

@@ -14,9 +14,13 @@ void health_string2json(BUFFER *wb, const char *prefix, const char *label, const
 }
 
 void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host) {
+    char *edit_command = ae->source ? health_edit_command_from_source(ae->source) : strdupz("UNKNOWN=0");
+
     buffer_sprintf(wb,
             "\n\t{\n"
                     "\t\t\"hostname\": \"%s\",\n"
+                    "\t\t\"utc_offset\": %d,\n"
+                    "\t\t\"timezone\": \"%s\",\n"
                     "\t\t\"unique_id\": %u,\n"
                     "\t\t\"alarm_id\": %u,\n"
                     "\t\t\"alarm_event_id\": %u,\n"
@@ -34,6 +38,7 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
                     "\t\t\"recipient\": \"%s\",\n"
                     "\t\t\"exec_code\": %d,\n"
                     "\t\t\"source\": \"%s\",\n"
+                    "\t\t\"command\": \"%s\",\n"
                     "\t\t\"units\": \"%s\",\n"
                     "\t\t\"when\": %lu,\n"
                     "\t\t\"duration\": %lu,\n"
@@ -49,6 +54,8 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
                     "\t\t\"last_repeat\": \"%lu\",\n"
                     "\t\t\"silenced\": \"%s\",\n"
                    , host->hostname
+                   , host->utc_offset
+                   , host->abbrev_timezone
                    , ae->unique_id
                    , ae->alarm_id
                    , ae->alarm_event_id
@@ -66,6 +73,7 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
                    , ae->recipient?ae->recipient:host->health_default_recipient
                    , ae->exec_code
                    , ae->source
+                   , edit_command
                    , ae->units?ae->units:""
                    , (unsigned long)ae->when
                    , (unsigned long)ae->duration
@@ -114,6 +122,7 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
     buffer_strcat(wb, "\t}");
 
     freez(replaced_info);
+    freez(edit_command);
 }
 
 void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart) {

Некоторые файлы не были показаны из-за большого количества измененных файлов