-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog.go
53 lines (50 loc) · 936 Bytes
/
syslog.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
package main
import (
"context"
"fmt"
"log"
"net"
"os"
"strings"
"time"
)
var syslogCh chan string
var syslogCount = 0
func startSyslog(ctx context.Context) {
syslogCh = make(chan string, 2000)
dstList := strings.Split(syslogDst, ",")
dst := []net.Conn{}
for _, d := range dstList {
if !strings.Contains(d, ":") {
d += ":514"
}
s, err := net.Dial("udp", d)
if err != nil {
log.Fatal(err)
}
syslogCh <- fmt.Sprintf("start send syslog to %s", d)
dst = append(dst, s)
}
host, err := os.Hostname()
if err != nil {
host = "localhost"
}
defer func() {
for _, d := range dst {
d.Close()
}
}()
for {
select {
case <-ctx.Done():
log.Println("stop syslog")
return
case msg := <-syslogCh:
syslogCount++
s := fmt.Sprintf("<%d>%s %s twSdrPower: %s", 21*8+6, time.Now().Format("2006-01-02T15:04:05-07:00"), host, msg)
for _, d := range dst {
d.Write([]byte(s))
}
}
}
}