-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzabbix.go
179 lines (135 loc) · 4.2 KB
/
zabbix.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
// Package zabbix-jmx provides methods for working with Zabbix Java Gateway
package jmx
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"io"
"net"
"time"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Client is Zabbix JMX client
type Client struct {
ConnectTimeout time.Duration
WriteTimeout time.Duration
ReadTimeout time.Duration
dialer *net.Dialer
addr *net.TCPAddr
}
// Request is basic request struct
type Request struct {
Server string
Port int
Username string
Password string
Keys []string
}
// Response contains response data
type Response []*ResponseData
// ResponseData contains value for requested key
type ResponseData struct {
Value string `json:"value"`
}
// ////////////////////////////////////////////////////////////////////////////////// //
type jmxRequest struct {
Request string `json:"request"`
Conn string `json:"conn"`
Port int `json:"port"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Endpoint string `json:"jmx_endpoint"`
Keys []string `json:"keys"`
}
type jmxResponse struct {
Data Response `json:"data"`
Error string `json:"error"`
Status string `json:"response"`
}
// ////////////////////////////////////////////////////////////////////////////////// //
// NewClient creates new client
func NewClient(address string) (*Client, error) {
addr, err := net.ResolveTCPAddr("tcp4", address)
if err != nil {
return nil, err
}
dialer := &net.Dialer{Timeout: time.Second * 5}
return &Client{addr: addr, dialer: dialer}, nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// Get fetches data from Java Gateway
func (c *Client) Get(r *Request) (Response, error) {
jr := convertRequest(r)
conn, err := connectToServer(c)
if err != nil {
return nil, err
}
defer conn.Close() // Zabbix doesn't support persistent connections
err = writeToConnection(conn, encodeRequest(jr), c.WriteTimeout)
if err != nil {
return nil, err
}
buf := make([]byte, 13)
err = readFromConnection(conn, buf, c.ReadTimeout)
if err != nil {
return nil, err
}
size, err := decodeMeta(buf)
if err != nil {
return nil, err
}
buf = make([]byte, size)
err = readFromConnection(conn, buf, c.ReadTimeout)
if err != nil {
return nil, err
}
resp, err := decodeResponse(buf)
if err != nil {
return nil, err
}
return resp.Data, nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// convertRequest convert request to jmx request
func convertRequest(r *Request) *jmxRequest {
return &jmxRequest{
Request: "java gateway jmx",
Conn: r.Server,
Port: r.Port,
Username: r.Username,
Password: r.Password,
Endpoint: fmt.Sprintf("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", r.Server, r.Port),
Keys: r.Keys,
}
}
// connectToServer makes connection to Zabbix server
func connectToServer(c *Client) (*net.TCPConn, error) {
if c.ConnectTimeout > 0 && c.dialer.Timeout != c.ConnectTimeout {
c.dialer.Timeout = c.ConnectTimeout
}
conn, err := c.dialer.Dial(c.addr.Network(), c.addr.String())
if err != nil {
return nil, err
}
return conn.(*net.TCPConn), nil
}
// readFromConnection reads data from connection
func readFromConnection(conn *net.TCPConn, buf []byte, timeout time.Duration) error {
if timeout > 0 {
conn.SetReadDeadline(time.Now().Add(timeout))
}
_, err := io.ReadFull(conn, buf)
return err
}
// writeToConnection writes data into connection
func writeToConnection(conn *net.TCPConn, data []byte, timeout time.Duration) error {
if timeout > 0 {
conn.SetWriteDeadline(time.Now().Add(timeout))
}
_, err := conn.Write(data)
return err
}