-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzabbix_active_server.go
63 lines (50 loc) · 1.13 KB
/
zabbix_active_server.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
package active_zabbix
import (
"net"
"time"
)
type ZabbixActiveServer struct {
listener net.Listener
addr *net.TCPAddr
ZabbixActiveProto
}
func NewZabbixActiveServer(addr string, receive_timeout uint, send_timeout uint) (zs ZabbixActiveServer, err error) {
zs.receive_timeout = time.Duration(receive_timeout) * time.Millisecond
zs.send_timeout = time.Duration(send_timeout) * time.Millisecond
zs.addr, err = net.ResolveTCPAddr("tcp", addr)
if err != nil {
return
}
zs.listener, err = net.ListenTCP("tcp", zs.addr)
if err != nil {
return
}
return
}
func (zs *ZabbixActiveServer) Close() {
zs.listener.Close()
}
func (zs *ZabbixActiveServer) Listen(data_chan chan []byte, stop_chan chan bool) error {
for {
select {
case <-stop_chan:
return nil
default:
conn, err := zs.listener.Accept()
if err == nil {
go zs.handle_connection(conn, data_chan)
}
}
}
return nil
}
func (zs *ZabbixActiveServer) handle_connection(conn net.Conn, data_chan chan []byte) error {
defer conn.Close()
data, err := zs.zabbixReceive(conn)
if err != nil {
return err
} else {
data_chan <- data
}
return err
}