/
syslog_output.go
115 lines (94 loc) · 2.25 KB
/
syslog_output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package syslogoutput
import (
"bytes"
"errors"
"fmt"
"github.com/mozilla-services/heka/pipeline"
"io"
"io/ioutil"
"log/syslog"
"strings"
)
type SyslogOutput struct {
*SyslogOutputConfig
Protocol string
Host string
Tag string
}
type SyslogOutputConfig struct {
Protocol string `toml:"protocol"`
Host string `toml:"host"`
Tag string `toml:"tag"`
}
func (s *SyslogOutput) ConfigStruct() interface{} {
return &SyslogOutputConfig{
Protocol: "udp",
Host: "33.33.33.20:514",
Tag: "DefaultValue",
}
}
func (s *SyslogOutput) Init(config interface{}) (err error) {
s.SyslogOutputConfig = config.(*SyslogOutputConfig)
switch s.SyslogOutputConfig.Protocol {
case "udp":
case "tcp":
default:
return fmt.Errorf("Value %s found, protocol must be 'udp' or 'tcp'", s.SyslogOutputConfig.Protocol)
}
hostParse := strings.Split(s.SyslogOutputConfig.Host, ":")
if hostParse[0] == "" || hostParse[1] == "" {
return fmt.Errorf("Host must be in <host>:<port> format.")
}
if s.SyslogOutputConfig.Tag == "" {
return fmt.Errorf("Tag must be a valid string.")
}
return
}
func (s *SyslogOutput) Run(or pipeline.OutputRunner, h pipeline.PluginHelper) (err error) {
if or.Encoder() == nil {
return errors.New("Encoder must be specified.")
}
var (
e error
outBytes []byte
)
inChan := or.InChan()
for pack := range inChan {
outBytes, e = or.Encode(pack)
pack.Recycle(e)
if e != nil {
or.LogError(e)
continue
}
if outBytes == nil {
continue
}
if e = s.tosyslog(or, outBytes); e != nil {
or.LogError(e)
}
}
return
}
func (s *SyslogOutput) tosyslog(or pipeline.OutputRunner, outBytes []byte) (err error) {
var (
reader io.Reader
readCloser io.ReadCloser
)
logger, err := syslog.Dial(s.SyslogOutputConfig.Protocol, s.SyslogOutputConfig.Host, syslog.LOG_LOCAL1, s.SyslogOutputConfig.Tag)
if err != nil {
fmt.Errorf("Could not dial remote syslog host: %s", err)
}
defer logger.Close()
reader = bytes.NewReader(outBytes)
readCloser = ioutil.NopCloser(reader)
msg := new(bytes.Buffer)
msg.ReadFrom(readCloser)
msg_string := msg.String()
logger.Info(msg_string)
return err
}
func init() {
pipeline.RegisterPlugin("SyslogOutput", func() interface{} {
return new(SyslogOutput)
})
}