Welcome! Log In Create A New Profile

Advanced

syslog with timezone

Tomas Hulata
June 18, 2018 12:10PM
According rfc5424 there could or should be time zone specified in syslog
message. There are problems for example with graylog, so my patch is
adding optional switch to enable different datetime formatting with
timezone support to syslog module.

# HG changeset patch
# User Tomas Hulata <tomas.hulata@netbox.cz>
# Date 1528838460 -7200
#      Tue Jun 12 23:21:00 2018 +0200
# Node ID 309407bf691d77df2d095282b3050c3d9ad3c4ae
# Parent  8e6bb4e6045f7197923717831d2ddf414aa0f443
new option for timezone in syslog message

diff -r 8e6bb4e6045f -r 309407bf691d src/core/ngx_syslog.c
--- a/src/core/ngx_syslog.c    Thu Jun 07 20:04:22 2018 +0300
+++ b/src/core/ngx_syslog.c    Tue Jun 12 23:21:00 2018 +0200
@@ -211,6 +211,9 @@
         } else if (len == 10 && ngx_strncmp(p, "nohostname", 10) == 0) {
             peer->nohostname = 1;

+        } else if (len == 2 && ngx_strncmp(p, "tz", 2) == 0) {
+            peer->tz = 1;
+
         } else {
             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                "unknown syslog parameter \"%s\"", p);
@@ -238,11 +241,13 @@
     pri = peer->facility * 8 + peer->severity;

     if (peer->nohostname) {
-        return ngx_sprintf(buf, "<%ui>%V %V: ", pri,
&ngx_cached_syslog_time,
+        return ngx_sprintf(buf, "<%ui>%V %V: ", pri,
+                           peer->tz ? &ngx_cached_syslog_time_tz :
&ngx_cached_syslog_time,
                            &peer->tag);
     }

-    return ngx_sprintf(buf, "<%ui>%V %V %V: ", pri,
&ngx_cached_syslog_time,
+    return ngx_sprintf(buf, "<%ui>%V %V %V: ", pri,
+                       peer->tz ? &ngx_cached_syslog_time_tz :
&ngx_cached_syslog_time,
                        &ngx_cycle->hostname, &peer->tag);
 }

diff -r 8e6bb4e6045f -r 309407bf691d src/core/ngx_syslog.h
--- a/src/core/ngx_syslog.h    Thu Jun 07 20:04:22 2018 +0300
+++ b/src/core/ngx_syslog.h    Tue Jun 12 23:21:00 2018 +0200
@@ -17,6 +17,7 @@
     ngx_connection_t  conn;
     unsigned          busy:1;
     unsigned          nohostname:1;
+    unsigned          tz:1;
 } ngx_syslog_peer_t;


diff -r 8e6bb4e6045f -r 309407bf691d src/core/ngx_times.c
--- a/src/core/ngx_times.c    Thu Jun 07 20:04:22 2018 +0300
+++ b/src/core/ngx_times.c    Tue Jun 12 23:21:00 2018 +0200
@@ -33,6 +33,7 @@
 volatile ngx_str_t       ngx_cached_http_log_time;
 volatile ngx_str_t       ngx_cached_http_log_iso8601;
 volatile ngx_str_t       ngx_cached_syslog_time;
+volatile ngx_str_t       ngx_cached_syslog_time_tz;

 #if !(NGX_WIN32)

@@ -56,6 +57,9 @@
[sizeof("1970-09-28T12:00:00+06:00")];
 static u_char            cached_syslog_time[NGX_TIME_SLOTS]
                                     [sizeof("Sep 28 12:00:00")];
+static u_char            cached_syslog_time_tz[NGX_TIME_SLOTS]
+ [sizeof("1970-09-28T12:00:00+06:00")];
+


 static char  *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat" };
@@ -70,6 +74,7 @@
     ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00
+0600") - 1;
     ngx_cached_http_log_iso8601.len =
sizeof("1970-09-28T12:00:00+06:00") - 1;
     ngx_cached_syslog_time.len = sizeof("Sep 28 12:00:00") - 1;
+    ngx_cached_syslog_time_tz.len = sizeof("1970-09-28T12:00:00+06:00")
- 1;

     ngx_cached_time = &cached_time[0];

@@ -80,7 +85,7 @@
 void
 ngx_time_update(void)
 {
-    u_char          *p0, *p1, *p2, *p3, *p4;
+    u_char          *p0, *p1, *p2, *p3, *p4, *p5;
     ngx_tm_t         tm, gmt;
     time_t           sec;
     ngx_uint_t       msec;
@@ -179,6 +184,15 @@
                        months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
                        tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);

+    p5 = &cached_syslog_time_tz[slot][0];
+
+    (void) ngx_sprintf(p5, "%4d-%02d-%02dT%02d:%02d:%02d%c%02i:%02i",
+                       tm.ngx_tm_year, tm.ngx_tm_mon,
+                       tm.ngx_tm_mday, tm.ngx_tm_hour,
+                       tm.ngx_tm_min, tm.ngx_tm_sec,
+                       tp->gmtoff < 0 ? '-' : '+',
+                       ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
+
     ngx_memory_barrier();

     ngx_cached_time = tp;
@@ -187,6 +201,7 @@
     ngx_cached_http_log_time.data = p2;
     ngx_cached_http_log_iso8601.data = p3;
     ngx_cached_syslog_time.data = p4;
+    ngx_cached_syslog_time_tz.data = p5;

     ngx_unlock(&ngx_time_lock);
 }
@@ -222,7 +237,7 @@
 void
 ngx_time_sigsafe_update(void)
 {
-    u_char          *p, *p2;
+    u_char          *p, *p2, *p3;
     ngx_tm_t         tm;
     time_t           sec;
     ngx_time_t      *tp;
@@ -268,10 +283,20 @@
                        months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
                        tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);

+    p3 = &cached_syslog_time_tz[slot][0];
+
+    (void) ngx_sprintf(p3, "%4d-%02d-%02dT%02d:%02d:%02d%c%02i:%02i",
+                       tm.ngx_tm_year, tm.ngx_tm_mon,
+                       tm.ngx_tm_mday, tm.ngx_tm_hour,
+                       tm.ngx_tm_min, tm.ngx_tm_sec,
+                       tp->gmtoff < 0 ? '-' : '+',
+                       ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
+
     ngx_memory_barrier();

     ngx_cached_err_log_time.data = p;
     ngx_cached_syslog_time.data = p2;
+    ngx_cached_syslog_time_tz.data = p3;

     ngx_unlock(&ngx_time_lock);
 }
diff -r 8e6bb4e6045f -r 309407bf691d src/core/ngx_times.h
--- a/src/core/ngx_times.h    Thu Jun 07 20:04:22 2018 +0300
+++ b/src/core/ngx_times.h    Tue Jun 12 23:21:00 2018 +0200
@@ -41,6 +41,7 @@
 extern volatile ngx_str_t    ngx_cached_http_log_time;
 extern volatile ngx_str_t    ngx_cached_http_log_iso8601;
 extern volatile ngx_str_t    ngx_cached_syslog_time;
+extern volatile ngx_str_t    ngx_cached_syslog_time_tz;

 /*
  * milliseconds elapsed since some unspecified point in the past

--
S pozdravom
Tomas Hulata
/vedoucí úseku systemových inženýrů/
tel: +420 733 163 238
NETBOX

SMART Comp. a.s., Kubíčkova 8, 635 00, Brno
Tel./fax: +420 544 423 411, www.netbox.cz

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

syslog with timezone Attachments

Tomas Hulata 657 June 18, 2018 12:10PM

Re: syslog with timezone

Maxim Dounin 201 June 18, 2018 01:38PM

Re: syslog with timezone Attachments

Tomas Hulata 206 June 18, 2018 06:26PM

Re: syslog with timezone

Maxim Dounin 156 June 20, 2018 08:48AM

Re: syslog with timezone Attachments

Tomas Hulata 200 June 20, 2018 06:58PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 185
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready