-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzabbix_active_client.go
115 lines (96 loc) · 2.66 KB
/
zabbix_active_client.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
package active_zabbix
import (
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
"time"
)
type HostActiveKeys map[string]time.Duration
type ZabbixActiveClient struct {
conn net.Conn
addr *net.TCPAddr
ConnectTimeout time.Duration
ZabbixActiveProto
}
func NewZabbixActiveClient(addr string, receive_timeout uint, send_timeout uint) (zc ZabbixActiveClient, err error) {
addr = strings.Replace(addr, "zbx://", "", 1)
zc.addr, err = net.ResolveTCPAddr("tcp", addr)
zc.receive_timeout = time.Duration(receive_timeout) * time.Millisecond
zc.send_timeout = time.Duration(send_timeout) * time.Millisecond
return
}
func (zc *ZabbixActiveClient) getConn() (err error) {
if zc.conn == nil {
dialer := &net.Dialer{Timeout: zc.ConnectTimeout}
if zc.conn, err = dialer.Dial("tcp", zc.addr.String()); err != nil {
return
}
}
return
}
func (zc *ZabbixActiveClient) cleanupConn() {
if zc.conn != nil {
zc.conn.Close()
zc.conn = nil
}
}
func (zc *ZabbixActiveClient) ZabbixSendAndForget(data []byte) (err error) {
err = zc.ZabbixSend(data)
if err != nil {
return
}
zc.cleanupConn()
return
}
func (zc *ZabbixActiveClient) ZabbixSend(data []byte) (err error) {
err = zc.getConn()
if zc.conn == nil || err != nil {
return
}
return zc.zabbixSend(zc.conn, data)
}
func (zc *ZabbixActiveClient) ZabbixReceive() (result []byte, err error) {
err = zc.getConn()
if zc.conn == nil || err != nil {
return
}
defer zc.cleanupConn()
return zc.zabbixReceive(zc.conn)
}
func (zc *ZabbixActiveClient) FetchActiveChecks(host string) (hc HostActiveKeys, err error) {
msg := fmt.Sprintf("{\"request\":\"active checks\",\"host\":\"%s\"}", host)
data := []byte(msg)
hc = make(HostActiveKeys, 1)
if err = zc.ZabbixSend(data); err != nil {
return
} else {
var result []byte
if result, err = zc.ZabbixReceive(); err != nil {
return
} else {
// Parse json for key names
var unmarshalledResult ActiveCheckResponseJson
//Check what's the result on no keys
err = json.Unmarshal(result, &unmarshalledResult)
if err != nil {
return
}
// Push key names for the current host
for _, activeCheckKey := range unmarshalledResult.Data {
if fDelay, ok := activeCheckKey.Delay.(float64); ok {
hc[activeCheckKey.Key] = time.Duration(int(fDelay)) * time.Second
} else if sDelay, ok := activeCheckKey.Delay.(string); ok {
// Put 15 as delay if strconv doesn't work for now
if delay, conv_err := strconv.ParseInt(sDelay, 10, 32); conv_err != nil {
hc[activeCheckKey.Key] = time.Duration(15 * time.Second)
} else {
hc[activeCheckKey.Key] = time.Duration(int(delay)) * time.Second
}
}
}
}
}
return
}