-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathout_rsyslog.go
117 lines (92 loc) · 2.19 KB
/
out_rsyslog.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
116
117
package main
import (
"C"
"encoding/json"
"fmt"
"log/syslog"
"unsafe"
"github.com/fluent/fluent-bit-go/output"
)
const (
pluginName = "rsyslog"
)
type Logger struct {
network string
addr string
tag string
writer *syslog.Writer
}
func (l *Logger) connect() error {
if l.writer != nil {
return nil
}
writer, err := syslog.Dial(l.network, l.addr, syslog.LOG_WARNING|syslog.LOG_DAEMON, l.tag)
if err != nil {
return err
}
l.writer = writer
return nil
}
func (l *Logger) Info(m string) error {
if err := l.connect(); err != nil {
return err
}
return l.writer.Info(m)
}
func NewLogger(network, addr, tag string) (*Logger, error) {
logger := Logger{
network: network,
addr: addr,
tag: tag,
}
return &logger, nil
}
//export FLBPluginRegister
func FLBPluginRegister(ctx unsafe.Pointer) int {
return output.FLBPluginRegister(ctx, pluginName, pluginName)
}
//export FLBPluginInit
func FLBPluginInit(plugin unsafe.Pointer) int {
network := output.FLBPluginConfigKey(plugin, "network")
addr := output.FLBPluginConfigKey(plugin, "addr")
tag := output.FLBPluginConfigKey(plugin, "tag")
fmt.Printf("plugin=%s network=%s addr=%s tag=%s\n", pluginName, network, addr, tag)
logger, err := NewLogger(network, addr, tag)
if err != nil {
return output.FLB_ERROR
}
output.FLBPluginSetContext(plugin, unsafe.Pointer(logger))
return output.FLB_OK
}
//export FLBPluginFlush
func FLBPluginFlush(data unsafe.Pointer, length C.int, tag *C.char) int {
return output.FLB_OK
}
//export FLBPluginFlushCtx
func FLBPluginFlushCtx(ctx, data unsafe.Pointer, length C.int, tag *C.char) int {
logger := (*Logger)(output.FLBPluginGetContext(ctx).(unsafe.Pointer))
dec := output.NewDecoder(data, int(length))
for {
ret, _, record := output.GetRecord(dec)
if ret != 0 {
break
}
m := map[string]interface{}{}
for k, v := range record {
m[fmt.Sprintf("%s", k)] = fmt.Sprintf("%s", v)
}
jsonBody, err := json.Marshal(m)
if err != nil {
fmt.Printf("plugin=%s err=%+v\n", pluginName, err)
return output.FLB_ERROR
}
logger.Info(string(jsonBody))
}
return output.FLB_OK
}
//export FLBPluginExit
func FLBPluginExit() int {
return output.FLB_OK
}
func main() {
}