-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprotocol.go
69 lines (52 loc) · 1.85 KB
/
protocol.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
package jmx
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// zabbixHeader is Zabbix header
var zabbixHeader = []byte("ZBXD\x01")
// ////////////////////////////////////////////////////////////////////////////////// //
// encodeRequest encodes request
func encodeRequest(r *jmxRequest) []byte {
payload, _ := json.Marshal(r)
return encodePayload(payload)
}
// encodePayload encodes payload
func encodePayload(payload []byte) []byte {
size := uint64(len(payload))
var buf bytes.Buffer
sizeBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(sizeBuf, size)
buf.Write(zabbixHeader)
buf.Write(sizeBuf)
buf.Write(payload)
return buf.Bytes()
}
// decodeMeta decodes response meta
func decodeMeta(data []byte) (int, error) {
if len(data) < 5 || !bytes.Equal(data[:5], zabbixHeader) {
return -1, errors.New("Wrong header format")
}
return int(binary.LittleEndian.Uint64(data[5:])), nil
}
// decodeResponse decodes response
func decodeResponse(data []byte) (*jmxResponse, error) {
resp := &jmxResponse{}
err := json.Unmarshal(data, resp)
if err != nil {
return nil, errors.New("Can't unmarshal response data: " + err.Error())
}
if resp.Status != "success" {
return nil, errors.New(resp.Error)
}
return resp, nil
}