forked from nixys/nxs-go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbix.go
187 lines (149 loc) · 4.5 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
180
181
182
183
184
185
186
187
package zabbix
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/mitchellh/mapstructure"
)
// Zabbix select constants
const (
SelectExtendedOutput = "extend"
SelectCount = "count"
)
// For `GetParameters` field: `SortOrder`
const (
GetParametersSortOrderASC = "ASC"
GetParametersSortOrderDESC = "DESC"
)
// Context struct is used for store settings to communicate with Zabbix API
type Context struct {
sessionKey string
host string
}
// GetParameters struct is used as embedded struct for some other structs within package
//
// see for details: https://www.zabbix.com/documentation/5.0/manual/api/reference_commentary#common_get_method_parameters
type GetParameters struct {
CountOutput bool `json:"countOutput,omitempty"`
Editable bool `json:"editable,omitempty"`
ExcludeSearch bool `json:"excludeSearch,omitempty"`
Filter map[string]interface{} `json:"filter,omitempty"`
Limit int `json:"limit,omitempty"`
Output SelectQuery `json:"output,omitempty"`
PreserveKeys bool `json:"preservekeys,omitempty"`
Search map[string]string `json:"search,omitempty"`
SearchByAny bool `json:"searchByAny,omitempty"`
SearchWildcardsEnabled bool `json:"searchWildcardsEnabled,omitempty"`
SortField []string `json:"sortfield,omitempty"`
SortOrder []string `json:"sortorder,omitempty"` // has defined consts, see above
StartSearch bool `json:"startSearch,omitempty"`
}
// SelectQuery is used as field type in some structs
type SelectQuery interface{}
// SelectFields is used as field type in some structs
type SelectFields []string
type requestData struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params,omitempty"`
Auth string `json:"auth,omitempty"`
ID int `json:"id"`
}
type responseData struct {
JSONRPC string `json:"jsonrpc"`
Result interface{} `json:"result"`
Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
} `json:"error"`
ID int `json:"id"`
}
// Login gets the Zabbix session
func (z *Context) Login(host, user, password string) error {
var err error
z.host = host
r := UserLoginParams{
User: user,
Password: password,
}
if z.sessionKey, _, err = z.userLogin(r); err != nil {
return err
}
return nil
}
// Logout destroys the Zabbix session
func (z *Context) Logout() error {
_, _, err := z.userLogout()
z.sessionKey = ""
if err != nil {
return err
}
return nil
}
func (z *Context) request(method string, params interface{}, result interface{}) (int, error) {
resp := responseData{
Result: result,
}
req := requestData{
JSONRPC: "2.0",
Method: method,
Params: params,
Auth: z.sessionKey,
ID: 1,
}
status, err := z.httpPost(req, &resp)
if err != nil {
return status, err
}
if resp.Error.Code != 0 {
return status, errors.New(resp.Error.Data + " " + resp.Error.Message)
}
return status, nil
}
func (z *Context) httpPost(in interface{}, out interface{}) (int, error) {
s, err := json.Marshal(in)
if err != nil {
return 0, err
}
req, err := http.NewRequest("POST", z.host, strings.NewReader(string(s)))
if err != nil {
return 0, err
}
// Set headers
req.Header.Add("Content-Type", "application/json-rpc")
// Make request
res, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
if bodyBytes, err := ioutil.ReadAll(res.Body); err == nil {
return res.StatusCode, errors.New(string(bodyBytes))
}
} else {
if out != nil {
rawConf := make(map[string]interface{})
dJ := json.NewDecoder(res.Body)
if err := dJ.Decode(&rawConf); err != nil {
return res.StatusCode, fmt.Errorf("json decode error: %v", err)
}
dM, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
WeaklyTypedInput: true,
Result: out,
TagName: "json",
})
if err != nil {
return res.StatusCode, fmt.Errorf("mapstructure create decoder error: %v", err)
}
if err := dM.Decode(rawConf); err != nil {
return res.StatusCode, fmt.Errorf("mapstructure decode error: %v", err)
}
}
}
return res.StatusCode, nil
}