-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyslog_handler.go
279 lines (251 loc) · 5.44 KB
/
syslog_handler.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package delayed_job
import (
"bytes"
"errors"
"flag"
"fmt"
"net"
"os"
"strings"
"time"
)
var (
Hostname, _ = os.Hostname()
default_facility = flag.String("syslog.facility", "user", "the facility of syslog message.")
default_severity = flag.String("syslog.severity", "info", "the severity of syslog message.")
default_tag = flag.String("syslog.tag", "tpt", "the tag of syslog message.")
)
const (
F_Kernel int = iota
F_User
F_Mail
F_Daemon
F_Auth
F_Syslog
F_Lpr
F_News
F_Uucp
F_Cron
F_Authpriv
F_System0
F_System1
F_System2
F_System3
F_System4
F_Local0
F_Local1
F_Local2
F_Local3
F_Local4
F_Local5
F_Local6
F_Local7
)
var facility_2_string = [...]string{
"kernel",
"user",
"mail",
"daemon",
"auth",
"syslog",
"lpr",
"news",
"uucp",
"cron",
"authpriv",
"system0",
"system1",
"system2",
"system3",
"system4",
"local0",
"local1",
"local2",
"local3",
"local4",
"local5",
"local6",
"local7",
}
var string_2_facility = map[string]int{
"kernel": F_Kernel,
"user": F_User,
"mail": F_Mail,
"daemon": F_Daemon,
"auth": F_Auth,
"syslog": F_Syslog,
"lpr": F_Lpr,
"news": F_News,
"uucp": F_Uucp,
"cron": F_Cron,
"authpriv": F_Authpriv,
"system0": F_System0,
"system1": F_System1,
"system2": F_System2,
"system3": F_System3,
"system4": F_System4,
"local0": F_Local0,
"local1": F_Local1,
"local2": F_Local2,
"local3": F_Local3,
"local4": F_Local4,
"local5": F_Local5,
"local6": F_Local6,
"local7": F_Local7,
}
const (
S_Emerg int = iota
S_Alert
S_Crit
S_Err
S_Warning
S_Notice
S_Info
S_Debug
)
var severity_2_string = [...]string{
"emerg",
"alert",
"crit",
"err",
"warning",
"notice",
"info",
"debug",
}
var string_2_severity = map[string]int{
"emerg": S_Emerg,
"alert": S_Alert,
"crit": S_Crit,
"err": S_Err,
"warning": S_Warning,
"notice": S_Notice,
"info": S_Info,
"debug": S_Debug,
}
func message_printf(facility int,
severity int,
timestamp time.Time, // optional
hostname string, // optional
tag string, // message tag as defined in RFC 3164
content string) string { // message content as defined in RFC 3164
return fmt.Sprintf("<%d>%s %s %s %s", facility*8+severity,
timestamp.Format(time.Stamp), hostname, tag, content)
}
type syslogHandler struct {
to []*net.UDPAddr
message string
}
// <property name="facility">
// <property name="severity">
// <property name="tag">
// <property name="content">
func newSyslogHandler(ctx, params map[string]interface{}) (Handler, error) {
if nil == params {
return nil, errors.New("params is nil")
}
to := stringsWithDefault(params, "to_address", ",", nil)
if nil == to || 0 == len(to) {
return nil, errors.New("'to_address' is required.")
}
to_addr := make([]*net.UDPAddr, 0, len(to))
for _, t := range to {
reparse:
addr, e := net.ResolveUDPAddr("udp", t)
if nil == e && nil != addr {
to_addr = append(to_addr, addr)
} else if ip := net.ParseIP(t); nil != ip {
t += ":514"
goto reparse
}
}
if 0 == len(to_addr) {
return nil, errors.New("'to_address' is empty or invalid.")
}
facility_s := stringWithDefault(params, "facility", *default_facility)
if 0 == len(facility_s) {
return nil, errors.New("'facility' is required")
}
facility, ok := string_2_facility[facility_s]
if !ok {
return nil, errors.New("'facility' is invalid - '" + facility_s + "'.")
}
severity_s := stringWithDefault(params, "severity", *default_severity)
if 0 == len(severity_s) {
return nil, errors.New("'severity' is required")
}
severity, ok := string_2_severity[severity_s]
if !ok {
return nil, errors.New("'severity' is invalid - '" + severity_s + "'.")
}
timestamp := timeWithDefault(params, "timestamp", time.Now())
hostname := stringWithDefault(params, "hostname", Hostname)
if 0 == len(hostname) {
return nil, errors.New("'hostname' is required")
}
if strings.ContainsAny(hostname, " \t\r\n") {
return nil, errors.New("'hostname' is invalid - '" + hostname + "'")
}
tag := stringWithDefault(params, "tag", *default_tag)
//if 0 == len(tag) {
// return nil, errors.New("'tag' is required.")
//}
content := stringWithDefault(params, "content", "")
if 0 == len(content) {
return nil, errors.New("'content' is required")
}
if args, ok := params["arguments"]; ok {
args = preprocessArgs(args)
if props, ok := args.(map[string]interface{}); ok {
if _, ok := props["self"]; !ok {
props["self"] = params
defer delete(props, "self")
}
}
var e error
content, e = genText(content, args)
if nil != e {
return nil, e
}
}
return &syslogHandler{to: to_addr, message: message_printf(facility,
severity,
timestamp, // optional
hostname, // optional
tag, // message tag as defined in RFC 3164
content)}, nil
}
func (self *syslogHandler) Perform() error {
buf := bytes.NewBuffer(make([]byte, 0, 1000))
hasOk := false
for _, to := range self.to {
e := self.send(to)
if nil == e {
hasOk = true
} else {
buf.WriteString(e.Error())
buf.WriteString("\r\n")
}
}
if hasOk {
return nil
}
if buf.Len() > 2 {
buf.Truncate(buf.Len() - 2)
}
return errors.New(buf.String())
}
func (self *syslogHandler) send(to *net.UDPAddr) error {
c, e := net.DialUDP("udp", nil, to)
if nil != e {
return e
}
defer c.Close()
fmt.Println(c.RemoteAddr(), self.message)
_, e = c.Write([]byte(self.message))
return e
}
func init() {
Handlers["syslog"] = newSyslogHandler
Handlers["syslog_command"] = newSyslogHandler
}