forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
proxy.go
137 lines (117 loc) · 3.74 KB
/
proxy.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
package zabbix
import (
"github.com/AlekSi/reflector"
)
// https://www.zabbix.com/documentation/2.0/manual/appendix/api/proxy/definitions
type Proxy struct {
ProxyId string `json:"proxyid,omitempty"`
Host string `json:"host"`
Status StatusType `json:"status"`
LastAccess TimestampType `json:"lastaccess"`
// Fields below used only when creating proxies
Interfaces HostInterfaces `json:"interfaces,omitempty"`
// https://www.zabbix.com/documentation/2.2/manual/appendix/api/proxy/definitions
Interface HostInterface `json:"interface,omitempty"`
// https://www.zabbix.com/documentation/3.0/manual/appendix/api/proxy/definitions
Description string `json:"description"`
TlsConnect int `json:"tls_connect"`
TlsAccept int `json:"tls_accept"`
TlsPskIdentity string `json:"tls_psk_identity"`
TlsPsk string `json:"tls_psk"`
}
type Proxies []Proxy
// Wrapper for proxy.get: https://www.zabbix.com/documentation/2.0/manual/appendix/api/proxy/get
func (api *API) ProxiesGet(params Params) (res Proxies, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("proxy.get", params)
if err != nil {
return
}
reflector.MapsToStructs2(response.Result.([]interface{}), &res, reflector.Strconv, "json")
return
}
// Gets proxy by Id only if there is exactly 1 matching proxy.
func (api *API) ProxyGetById(id string) (res *Proxy, err error) {
proxies, err := api.ProxiesGet(Params{"proxyids": id})
if err != nil {
return
}
if len(proxies) == 1 {
res = &proxies[0]
} else {
e := ExpectedOneResult(len(proxies))
err = &e
}
return
}
// Gets proxy by Proxy only if there is exactly 1 matching proxy.
func (api *API) ProxyGetByProxy(proxy string) (res *Proxy, err error) {
proxies, err := api.ProxiesGet(Params{"filter": map[string]string{"proxy": proxy}})
if err != nil {
return
}
if len(proxies) == 1 {
res = &proxies[0]
} else {
e := ExpectedOneResult(len(proxies))
err = &e
}
return
}
// Wrapper for proxy.create: https://www.zabbix.com/documentation/2.2/manual/appendix/api/proxy/create
func (api *API) ProxiesCreate(proxies Proxies) (err error) {
response, err := api.CallWithError("proxy.create", proxies)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
proxyids := result["proxyids"].([]interface{})
for i, id := range proxyids {
proxies[i].ProxyId = id.(string)
}
return
}
func (api *API) ProxiesUpdate(proxies Proxies) (err error) {
_, err = api.CallWithError("proxy.update", proxies)
return
}
// Wrapper for proxy.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/proxy/delete
// Cleans ProxyId in all proxies elements if call succeed.
func (api *API) ProxiesDelete(proxies Proxies) (err error) {
ids := make([]string, len(proxies))
for i, proxy := range proxies {
ids[i] = proxy.ProxyId
}
err = api.ProxiesDeleteByIds(ids)
if err == nil {
for i := range proxies {
proxies[i].ProxyId = ""
}
}
return
}
// Wrapper for proxy.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/proxy/delete
func (api *API) ProxiesDeleteByIds(ids []string) (err error) {
proxyIds := make([]map[string]string, len(ids))
for i, id := range ids {
proxyIds[i] = map[string]string{"proxyid": id}
}
response, err := api.CallWithError("proxy.delete", proxyIds)
if err != nil {
// Zabbix 2.4 uses new syntax only
if e, ok := err.(*Error); ok && e.Code == ZbxApiErrorParameters {
response, err = api.CallWithError("proxy.delete", ids)
}
}
if err != nil {
return
}
result := response.Result.(map[string]interface{})
proxyids := result["proxyids"].([]interface{})
if len(ids) != len(proxyids) {
err = &ExpectedMore{len(ids), len(proxyids)}
}
return
}