Add support for arbitrary date formats for netdata alerts.
This adds support to alarm-notify.sh to report dates in alert notifications using an arbitrary format string. The current default behavior (just using the default format fromm `date`, which is simply the ANSI C asctime() default) is preserved. In the event that the user specified format string doesn't work, the script will fall back to using the default output of the date command. THe config file includes comments listing a handful of widely used internet date formats as examples. This also removes the explicit quoting around the shell expansion used to get the date, as it's unneeded (shell expansion results done with $() is implicitly treated as a single string), and makes it very hard to get this working properly.
Austin S. Hemmelgarn committed
May 9, 2018 at 08:23 UTC
83b26b2dadffad3d10c64dc07c9ff43457d31ff7
2 files changed
+21
-2
conf.d/health_alarm_notify.conf
+17
@@ -43,6 +43,23 @@
43
#images_base_url="http://my.public.netdata.server:19999"
44
45
46
+#------------------------------------------------------------------------------
47
+# date handling
48
+#
49
+# You can configure netdata alerts to send dates in any format you want.
50
+# This uses standard `date` command format strings. See `man date` for
51
+# more info on what you can put in here. Note that this has to start with a '+', otherwise it won't work.
52
+#
53
+# For ISO 8601 dates, use '+%FT%T%z'
54
+# For RFC 5322 dates, use '+%a, %d %b %Y %H:%M:%S %z'
55
+# For RFC 3339 dates, use '+%F %T%:z'
56
+# For RFC 1123 dates, use '+%a, %d %b %Y %H:%M:%S %Z'
57
+# For RFC 1036 dates, use '+%A, %d-%b-%y %H:%M:%S %Z'
58
+# For a reasonably local date and time (in that order), use '+%x %X'
59
+# For the old default behavior (compatible with ANSI C's asctime() function), leave this empty.
60
+date_format=''
61
+
62
+
63
#------------------------------------------------------------------------------
64
# external commands
65
plugins.d/alarm-notify.sh
+4
-2
@@ -745,8 +745,10 @@ fi
745
# -----------------------------------------------------------------------------
746
# get the date the alarm happened
747
748
-date="$(date --date=@${when} 2>/dev/null)"
749
-[ -z "${date}" ] && date="$(date 2>/dev/null)"
748
+date=$(date --date=@${when} "${DATE_FORMAT}" 2>/dev/null)
749
+[ -z "${date}" ] && date=$(date "${DATE_FORMAT}" 2>/dev/null)
750
+[ -z "${date}" ] && date=$(date --date=@${when} 2>/dev/null)
751
+[ -z "${date}" ] && date=$(date 2>/dev/null)
752
753
# -----------------------------------------------------------------------------
754
# function to URL encode a string