/
zmqforwarder.go
61 lines (51 loc) · 1.21 KB
/
zmqforwarder.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
package main
import(
"flag"
"fmt"
"os"
tail "github.com/ActiveState/tail"
zmq "github.com/pebbe/zmq4"
)
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func WatchFile(filepath string, shippingChan chan<- string) {
tailed, err := tail.TailFile(filepath,
tail.Config{
Location: &tail.SeekInfo{Whence: os.SEEK_END},
Follow: true,
ReOpen: true})
checkErr(err)
for line := range(tailed.Lines) {
if len(line.Text) > 0 {
shippingChan <- line.Text
}
}
}
func LogSender(serverAddress string, shippingChan <-chan string) {
server, err := zmq.NewSocket(zmq.PUB)
defer server.Close()
checkErr(err)
server.Bind(serverAddress)
for {
line := <-shippingChan
server.Send(line, 0)
}
}
func main() {
var serverAddress string
flag.StringVar(&serverAddress, "server", "tcp://localhost:2120", "address of PULL socket")
flag.Parse()
files := flag.Args()
if len(files) > 0 {
shippingChan := make(chan string, 1000)
for _, file := range(files) {
go WatchFile(file, shippingChan)
}
LogSender(serverAddress, shippingChan)
} else {
fmt.Println("usage: zmqforwarder [-server tcp://server:port] logfile [logfile ...]")
}
}