Allow a user defined field to be used as the graylog short_message (#6700)
This commit is contained in:
parent
ebebfd9573
commit
c6f8b273c0
|
@ -1,14 +1,18 @@
|
||||||
# Graylog Output Plugin
|
# Graylog Output Plugin
|
||||||
|
|
||||||
This plugin writes to a Graylog instance using the "gelf" format.
|
This plugin writes to a Graylog instance using the "[GELF][]" format.
|
||||||
|
|
||||||
It requires a `servers` name.
|
[GELF]: https://docs.graylog.org/en/3.1/pages/gelf.html#gelf-payload-specification
|
||||||
|
|
||||||
### Configuration:
|
### Configuration:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
# Send telegraf metrics to graylog(s)
|
|
||||||
[[outputs.graylog]]
|
[[outputs.graylog]]
|
||||||
## UDP endpoint for your graylog instance(s).
|
## UDP endpoint for your graylog instances.
|
||||||
servers = ["127.0.0.1:12201", "192.168.1.1:12201"]
|
servers = ["127.0.0.1:12201"]
|
||||||
|
|
||||||
|
## The field to use as the GELF short_message, if unset the static string
|
||||||
|
## "telegraf" will be used.
|
||||||
|
## example: short_message_field = "message"
|
||||||
|
# short_message_field = ""
|
||||||
```
|
```
|
||||||
|
|
|
@ -150,13 +150,19 @@ func (g *Gelf) send(b []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Graylog struct {
|
type Graylog struct {
|
||||||
Servers []string
|
Servers []string `toml:"servers"`
|
||||||
writer io.Writer
|
ShortMessageField string `toml:"short_message_field"`
|
||||||
|
writer io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
var sampleConfig = `
|
var sampleConfig = `
|
||||||
## UDP endpoint for your graylog instance.
|
## UDP endpoint for your graylog instance.
|
||||||
servers = ["127.0.0.1:12201", "192.168.1.1:12201"]
|
servers = ["127.0.0.1:12201"]
|
||||||
|
|
||||||
|
## The field to use as the GELF short_message, if unset the static string
|
||||||
|
## "telegraf" will be used.
|
||||||
|
## example: short_message_field = "message"
|
||||||
|
# short_message_field = ""
|
||||||
`
|
`
|
||||||
|
|
||||||
func (g *Graylog) Connect() error {
|
func (g *Graylog) Connect() error {
|
||||||
|
@ -184,16 +190,12 @@ func (g *Graylog) SampleConfig() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graylog) Description() string {
|
func (g *Graylog) Description() string {
|
||||||
return "Send telegraf metrics to graylog(s)"
|
return "Send telegraf metrics to graylog"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graylog) Write(metrics []telegraf.Metric) error {
|
func (g *Graylog) Write(metrics []telegraf.Metric) error {
|
||||||
if len(metrics) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, metric := range metrics {
|
for _, metric := range metrics {
|
||||||
values, err := serialize(metric)
|
values, err := g.serialize(metric)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -201,14 +203,14 @@ func (g *Graylog) Write(metrics []telegraf.Metric) error {
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
_, err := g.writer.Write([]byte(value))
|
_, err := g.writer.Write([]byte(value))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("FAILED to write message: %s, %s", value, err)
|
return fmt.Errorf("error writing message: %q, %v", value, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func serialize(metric telegraf.Metric) ([]string, error) {
|
func (g *Graylog) serialize(metric telegraf.Metric) ([]string, error) {
|
||||||
out := []string{}
|
out := []string{}
|
||||||
|
|
||||||
m := make(map[string]interface{})
|
m := make(map[string]interface{})
|
||||||
|
@ -217,7 +219,7 @@ func serialize(metric telegraf.Metric) ([]string, error) {
|
||||||
m["short_message"] = "telegraf"
|
m["short_message"] = "telegraf"
|
||||||
m["name"] = metric.Name()
|
m["name"] = metric.Name()
|
||||||
|
|
||||||
if host, ok := metric.Tags()["host"]; ok {
|
if host, ok := metric.GetTag("host"); ok {
|
||||||
m["host"] = host
|
m["host"] = host
|
||||||
} else {
|
} else {
|
||||||
host, err := os.Hostname()
|
host, err := os.Hostname()
|
||||||
|
@ -227,14 +229,18 @@ func serialize(metric telegraf.Metric) ([]string, error) {
|
||||||
m["host"] = host
|
m["host"] = host
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range metric.Tags() {
|
for _, tag := range metric.TagList() {
|
||||||
if key != "host" {
|
if tag.Key != "host" {
|
||||||
m["_"+key] = value
|
m["_"+tag.Key] = tag.Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range metric.Fields() {
|
for _, field := range metric.FieldList() {
|
||||||
m["_"+key] = value
|
if field.Key == g.ShortMessageField {
|
||||||
|
m["short_message"] = field.Value
|
||||||
|
} else {
|
||||||
|
m["_"+field.Key] = field.Value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
serialized, err := ejson.Marshal(m)
|
serialized, err := ejson.Marshal(m)
|
||||||
|
|
Loading…
Reference in New Issue