forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
203 lines (175 loc) · 5.67 KB
/
base.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package zabbix
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"sync/atomic"
)
type (
// Params Zabbix request param
Params map[string]interface{}
)
type request struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
Auth string `json:"auth,omitempty"`
ID int32 `json:"id"`
}
// Response format of zabbix api
type Response struct {
Jsonrpc string `json:"jsonrpc"`
Error *Error `json:"error"`
Result interface{} `json:"result"`
ID int32 `json:"id"`
}
// RawResponse format of zabbix api
type RawResponse struct {
Jsonrpc string `json:"jsonrpc"`
Error *Error `json:"error"`
Result json.RawMessage `json:"result"`
ID int32 `json:"id"`
}
// Error contains error data and code
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
}
func (e *Error) Error() string {
return fmt.Sprintf("%d (%s): %s", e.Code, e.Message, e.Data)
}
// ExpectedOneResult use to generate error when you expect one result
type ExpectedOneResult int
func (e *ExpectedOneResult) Error() string {
return fmt.Sprintf("Expected exactly one result, got %d.", *e)
}
// ExpectedMore use to generate error when you expect more element
type ExpectedMore struct {
Expected int
Got int
}
func (e *ExpectedMore) Error() string {
return fmt.Sprintf("Expected %d, got %d.", e.Expected, e.Got)
}
// API use to store connection information
type API struct {
Auth string // auth token, filled by Login()
Logger *log.Logger // request/response logger, nil by default
UserAgent string
url string
c http.Client
id int32
}
// NewAPI Creates new API access object.
// Typical URL is http://host/api_jsonrpc.php or http://host/zabbix/api_jsonrpc.php.
// It also may contain HTTP basic auth username and password like
// http://username:password@host/api_jsonrpc.php.
func NewAPI(url string) (api *API) {
return &API{url: url, c: http.Client{}, UserAgent: "github.com/claranet/zabbix"}
}
// SetClient Allows one to use specific http.Client, for example with InsecureSkipVerify transport.
func (api *API) SetClient(c *http.Client) {
api.c = *c
}
func (api *API) printf(format string, v ...interface{}) {
if api.Logger != nil {
api.Logger.Printf(format, v...)
}
}
func (api *API) callBytes(method string, params interface{}) (b []byte, err error) {
id := atomic.AddInt32(&api.id, 1)
jsonobj := request{"2.0", method, params, api.Auth, id}
b, err = json.Marshal(jsonobj)
if err != nil {
return
}
api.printf("Request (POST): %s", b)
req, err := http.NewRequest("POST", api.url, bytes.NewReader(b))
if err != nil {
return
}
req.ContentLength = int64(len(b))
req.Header.Add("Content-Type", "application/json-rpc")
req.Header.Add("User-Agent", api.UserAgent)
res, err := api.c.Do(req)
if err != nil {
api.printf("Error : %s", err)
return
}
defer res.Body.Close()
b, err = ioutil.ReadAll(res.Body)
api.printf("Response (%d): %s", res.StatusCode, b)
return
}
// Call Calls specified API method. Uses api.Auth if not empty.
// err is something network or marshaling related. Caller should inspect response.Error to get API error.
func (api *API) Call(method string, params interface{}) (response Response, err error) {
b, err := api.callBytes(method, params)
if err == nil {
err = json.Unmarshal(b, &response)
}
return
}
// CallWithError Uses Call() and then sets err to response.Error if former is nil and latter is not.
func (api *API) CallWithError(method string, params interface{}) (response Response, err error) {
response, err = api.Call(method, params)
if err == nil && response.Error != nil {
err = response.Error
}
return
}
// CallWithErrorParse Calls specified API method.
// Parse the response of the api in the result variable.
func (api *API) CallWithErrorParse(method string, params interface{}, result interface{}) (err error) {
var rawResult RawResponse
response, err := api.callBytes(method, params)
if err != nil {
return
}
err = json.Unmarshal(response, &rawResult)
if err != nil {
return
}
if rawResult.Error != nil {
return rawResult.Error
}
err = json.Unmarshal(rawResult.Result, &result)
return
}
// Login Calls "user.login" API method and fills api.Auth field.
// This method modifies API structure and should not be called concurrently with other methods.
func (api *API) Login(username, password string) (auth string, err error) {
params := map[string]string{"username": username, "password": password}
response, err := api.CallWithError("user.login", params)
if err != nil {
return
}
auth = response.Result.(string)
api.Auth = auth
return
}
// Version Calls "APIInfo.version" API method.
// This method temporary modifies API structure and should not be called concurrently with other methods.
func (api *API) Version() (v string, err error) {
// Remove auth from a temporary api clone for this method to succeed with Zabbix 2.4+
// See https://www.zabbix.com/documentation/2.4/manual/appendix/api/apiinfo/version
// And https://www.zabbix.com/documentation/4.4/manual/api/reference/apiinfo/version
// And https://www.zabbix.com/documentation/5.0/manual/api/reference/apiinfo/version
tempApi := *api
tempApi.Auth = ""
response, err := tempApi.CallWithError("APIInfo.version", Params{})
// Despite what documentation says, Zabbix 2.2 requires auth, so we try again
// See https://www.zabbix.com/documentation/2.2/manual/appendix/api/apiinfo/version
if e, ok := err.(*Error); ok && e.Code == -32602 {
response, err = api.CallWithError("APIInfo.version", Params{})
}
if err != nil {
return
}
v = response.Result.(string)
return
}