-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini-syslog-receiver.go
194 lines (182 loc) · 4.24 KB
/
mini-syslog-receiver.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
// Copyright (c) 2024, DCSO GmbH
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
"github.com/mcuadros/go-syslog"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "mini-syslog-receiver",
Usage: "receive and dump syslog data",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Aliases: []string{"l"},
Value: "0.0.0.0",
Usage: "address to listen on (0.0.0.0 means all interfaces)",
Action: func(ctx *cli.Context, v string) error {
if val := net.ParseIP(v); val == nil {
return fmt.Errorf("IP value %v not parseable", v)
}
return nil
},
},
&cli.Uint64Flag{
Name: "port",
Aliases: []string{"p"},
Value: 514,
Usage: "port to listen on",
Action: func(ctx *cli.Context, v uint64) error {
if v >= 65536 {
return fmt.Errorf("port value %v out of range[0-65535]", v)
}
return nil
},
},
&cli.Uint64Flag{
Name: "sample",
Aliases: []string{"m"},
Value: 1000,
Usage: "sample up to <value> log entries, then exit",
},
&cli.BoolFlag{
Name: "tcp",
Aliases: []string{"t"},
Value: false,
Usage: "use TCP instead of UDP",
},
&cli.BoolFlag{
Name: "tls",
Aliases: []string{"s"},
Value: false,
Usage: "use TLS for TCP server",
},
&cli.StringFlag{
Name: "tls-key",
Value: "",
Usage: "PEM private key file to use for TCP/TLS server",
},
&cli.StringFlag{
Name: "tls-chain",
Value: "",
Usage: "PEM certificate chain file to use for TCP/TLS server",
},
&cli.StringFlag{
Name: "outfile",
Aliases: []string{"o"},
Value: "",
Usage: "file to write output to (print to console if empty)",
},
},
Action: func(ctx *cli.Context) error {
channel := make(syslog.LogPartsChannel)
handler := syslog.NewChannelHandler(channel)
var err error
server := syslog.NewServer()
server.SetFormat(syslog.Automatic)
server.SetHandler(handler)
addr := fmt.Sprintf("%s:%d", ctx.String("listen"), ctx.Uint64("port"))
if ctx.Bool("tcp") {
if ctx.Bool("tls") {
tlsC := ctx.String("tls-chain")
if len(tlsC) == 0 {
log.Fatal("TLS chain missing")
}
tlsK := ctx.String("tls-key")
if len(tlsK) == 0 {
log.Fatal("TLS key missing")
}
var cert tls.Certificate
cert, err = tls.LoadX509KeyPair(tlsC, tlsK)
if err != nil {
log.Fatal(err)
}
cfg := &tls.Config{
Certificates: []tls.Certificate{cert},
}
log.Println("using TCP/TLS", addr)
err = server.ListenTCPTLS(addr, cfg)
} else {
log.Println("using TCP", addr)
err = server.ListenTCP(addr)
}
} else {
log.Println("using UDP", addr)
err = server.ListenUDP(addr)
}
if err != nil {
log.Fatal(err)
}
outfile := ctx.String("outfile")
var logFunc func(val string)
var f *os.File
if len(outfile) > 0 {
log.Println("writing to file", outfile)
// log to file
f, err = os.OpenFile(outfile, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
logFunc = func(val string) {
if _, err := f.WriteString(val + "\n"); err != nil {
log.Println(err)
}
}
} else {
// log to console
logFunc = func(val string) {
fmt.Println(val)
}
}
err = server.Boot()
if err != nil {
log.Fatal(err)
}
go func(channel syslog.LogPartsChannel) {
var i uint64
limit := ctx.Uint64("sample")
for logParts := range channel {
j, err := json.Marshal(logParts)
if err != nil {
log.Println(err)
continue
}
logFunc(string(j))
i += 1
if limit > 0 && i >= limit {
log.Printf("sample limit of %d log entries reached\n", limit)
if f != nil {
log.Println("closing output file")
f.Close()
}
os.Exit(0)
}
}
}(channel)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if f != nil {
log.Println("closing output file")
f.Close()
}
os.Exit(0)
}()
server.Wait()
return nil
},
}
app.HideHelpCommand = true
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}