-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add special syslog timestamp parser that uses current year #4190
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ var timeLayouts = map[string]string{ | |
// will get handled in the ParseLine function. | ||
"ts-epoch": "EPOCH", | ||
"ts-epochnano": "EPOCH_NANO", | ||
"ts-syslog": "SYSLOG_TIMESTAMP", | ||
"ts": "GENERIC_TIMESTAMP", // try parsing all known timestamp layouts. | ||
} | ||
|
||
|
@@ -44,6 +45,7 @@ const ( | |
DROP = "drop" | ||
EPOCH = "EPOCH" | ||
EPOCH_NANO = "EPOCH_NANO" | ||
SYSLOG_TIMESTAMP = "SYSLOG_TIMESTAMP" | ||
GENERIC_TIMESTAMP = "GENERIC_TIMESTAMP" | ||
) | ||
|
||
|
@@ -112,6 +114,7 @@ type Parser struct { | |
// layouts. | ||
foundTsLayouts []string | ||
|
||
timeFunc func() time.Time | ||
g *grok.Grok | ||
tsModder *tsModder | ||
} | ||
|
@@ -174,6 +177,10 @@ func (p *Parser) Compile() error { | |
p.loc, _ = time.LoadLocation("UTC") | ||
} | ||
|
||
if p.timeFunc == nil { | ||
p.timeFunc = time.Now | ||
} | ||
|
||
return p.compileCustomPatterns() | ||
} | ||
|
||
|
@@ -285,6 +292,16 @@ func (p *Parser) ParseLine(line string) (telegraf.Metric, error) { | |
} else { | ||
timestamp = time.Unix(0, iv) | ||
} | ||
case SYSLOG_TIMESTAMP: | ||
ts, err := time.ParseInLocation("Jan 02 15:04:05", v, p.loc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @danielnelson There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think maybe it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we try to parse "Sep 2 09:01:55 value=42" in test function TestSyslogTimestampParser(), Does this the example count? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, the test was "passing" even though the parsing failed. Here is the fix: #4334 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That fix looks nice. Thank you. |
||
if err == nil { | ||
if ts.Year() == 0 { | ||
ts = ts.AddDate(timestamp.Year(), 0, 0) | ||
} | ||
timestamp = ts | ||
} else { | ||
log.Printf("E! Error parsing %s to time layout [%s]: %s", v, t, err) | ||
} | ||
case GENERIC_TIMESTAMP: | ||
var foundTs bool | ||
// first try timestamp layouts that we've already found | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danielnelson this is the BSD syslog timestamp format (RFC3164) not the syslog RFC5424 timestamp (basically a RFC3339 micro timestamp).
So probably you want to call it
ts-bsd-syslog
, and maybe also add another format forts-syslog
.Just my 2 cents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that both of these RFCs describe the wire format for syslog messages, and not how the time might be written to logfiles. Is there any documention that describes how the timestamp in, for instance,
/var/log/syslog
are written?Another concern I had, and maybe you can help with, is if these dates are commonly localized?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The timestamp depends on the format (so RFC) chosen.
For example you can configure
RSYSLOG
to useRSYSLOG_SyslogProtocol23Format
format, which meansRFC5424
. With this setting/var/log/*
will containsRFC3339MICRO
timestamps (as perRFC5424
). Without this setting it will default to the old (RFC3164
) BSD syslog (and timestamp) format.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding localization.
RFC5425
withRFC3339MICRO
timestamps is not affected.Rather, timestamps of syslog messages following the old
RFC3164
are not localized (must be always in english) (ref).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll leave this as
ts-syslog
since it pairs with the built in grok patternSYSLOGTIMESTAMP
and there isn't any reason to add a special pattern for rfc5424 style timestamps since we already havets-rfc3339
andts-rfc3339nano
. We only need a special type here to inject the year.