Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
Add support for templating syslog lines
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpett committed Feb 23, 2018
1 parent dc8e84e commit 444751b
Show file tree
Hide file tree
Showing 14 changed files with 835 additions and 20 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,29 @@ The `syslog` input has three parameters:
- `input.syslog.listenfamily` (Required): The address family of the server. Valid values depend on platform, but include `tcp` and `udp`. On Linux and certain other systems, `unix` is also available.
- `input.syslog.listenaddr` (Required): The listen specification, for example `:1514` to listen on all interfaces on port 1514.
- `input.syslog.format` (Optional, default `autodetect`): The format of the syslog messages. Defaults to automatic detection, can also be set to `rfc3164`, `rfc5424` or `rfc6587`.
- `input.syslog.linetemplate` (Optional, default `[message][content]`): The template to use to for lines passed to the pattern matcher. `[]`-delimited words are substituted with the corresponding fields from the syslog message. Non-existing fields will be ignored. The following fields are available:

All formats:
- `client`
- `facility`
- `hostname`
- `priority`
- `severity`
- `timestamp`
- `tls_peer`

RFC3164 only:
- `content`
- `tag`

RFC5424 only:
- `app_name`
- `message`
- `msg_id`
- `proc_id`
- `version`

Since non-existing fields are ignored, the pattern `[message][content]` will contain the main part of the line regardless of which format is used.

## Named pipe
Creates a named pipe to which lines can be written. Only available on Linux.
Expand Down
1 change: 1 addition & 0 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func main() {
done = true
break
}
log.Debugf("Got line %q", line)
for _, m := range metrics {
t := time.Now()
m.MatchLine(line)
Expand Down
37 changes: 23 additions & 14 deletions input/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"errors"
"flag"
"fmt"
"io"

"github.com/prometheus/common/log"
"github.com/valyala/fasttemplate"
"gopkg.in/mcuadros/go-syslog.v2"
"gopkg.in/mcuadros/go-syslog.v2/format"
)
Expand All @@ -14,12 +16,14 @@ type SyslogInput struct {
listenAddr string
listenFamily string
format format.Format
template *fasttemplate.Template
}

var (
syslogListenFamily = flag.String("input.syslog.listenfamily", "", "Listening protocol family (tcp/udp/unix)")
syslogListenAddr = flag.String("input.syslog.listenaddr", "", "Listening address of syslog server")
syslogFormatFlag = flag.String("input.syslog.format", "autodetect", "Format of incoming syslog data (rfc3164/rfc5424/rfc6587/autodetect)")
syslogLineTemplate = flag.String("input.syslog.linetemplate", "[message][content]", "Template for data to pass to pattern matcher")
)

func init() {
Expand Down Expand Up @@ -57,6 +61,7 @@ func newSyslogInput() (StreamInput, error) {
listenAddr: *syslogListenAddr,
listenFamily: *syslogListenFamily,
format: syslogFormat,
template: fasttemplate.New(*syslogLineTemplate, "[", "]"),
}, nil
}

Expand Down Expand Up @@ -92,23 +97,27 @@ func (input SyslogInput) StartStream(ch chan<- string) {

go func(lineIn syslog.LogPartsChannel) {
for parts := range lineIn {
ch <- fmt.Sprintf("%v", parts["content"])
/* parts is a map with the following keys:
-hostname
-tag
-content (Message part)
-facility
-tls_peer
-timestamp
-severity
-client
-priority
Should these be joined up? Should it be possible to ask for a particular field? User-specified Sprintf-format?
For now, just send the message itself.
*/
line := input.template.ExecuteFuncString(func(w io.Writer, tag string) (int, error) { return tmplTagFunc(w, tag, parts) })
ch <- line
}
}(syslogChannel)

server.Wait()
log.Info("Syslog server shutting down")
}

func tmplTagFunc(w io.Writer, tag string, m map[string]interface{}) (int, error) {
v := m[tag]
if v == nil {
return 0, nil
}
switch value := v.(type) {
case []byte:
return w.Write(value)
case string:
return w.Write([]byte(value))
default:
// TODO: Performance test
return w.Write([]byte(fmt.Sprintf("%v", v)))
}
}
22 changes: 22 additions & 0 deletions vendor/github.com/valyala/bytebufferpool/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/valyala/bytebufferpool/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions vendor/github.com/valyala/bytebufferpool/bytebuffer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/valyala/bytebufferpool/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 444751b

Please sign in to comment.