From ede6c617dde2c6bc4b81954d14f39aa73c2ae247 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Fri, 29 May 2020 10:06:18 +0100 Subject: [PATCH 1/3] adding templates_clear --- host.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/host.go b/host.go index e1e6ceb..3fe3f25 100644 --- a/host.go +++ b/host.go @@ -38,9 +38,10 @@ type Host struct { UserMacros Macros `json:"macros,omitempty"` // Fields below used only when creating hosts - GroupIds HostGroupIDs `json:"groups,omitempty"` - Interfaces HostInterfaces `json:"interfaces,omitempty"` - TemplateIDs TemplateIDs `json:"templates,omitempty"` + GroupIds HostGroupIDs `json:"groups,omitempty"` + Interfaces HostInterfaces `json:"interfaces,omitempty"` + TemplateIDs TemplateIDs `json:"templates,omitempty"` + TemplateIDsClear TemplateIDs `json:"templates_clear,omitempty"` // templates are read back from this one ParentTemplateIDs TemplateIDs `json:"parentTemplates,omitempty"` ProxyID string `json:"proxy_hostid,omitempty"` From 656b157c72fcb32e5d6f729a55c02474b8025d18 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Fri, 29 May 2020 13:55:59 +0100 Subject: [PATCH 2/3] updates for v5 --- base.go | 7 ++++--- host.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++ host_interface.go | 33 ++++++++++++++++++++++++------- item.go | 1 + template.go | 5 +++-- 5 files changed, 83 insertions(+), 12 deletions(-) diff --git a/base.go b/base.go index 4f24d8f..e7e4340 100644 --- a/base.go +++ b/base.go @@ -78,7 +78,7 @@ type API struct { c http.Client id int32 ex sync.Mutex - config Config + Config Config } type Config struct { @@ -86,6 +86,7 @@ type Config struct { TlsNoVerify bool Log *log.Logger Serialize bool + Version int } // NewAPI Creates new API access object. @@ -98,7 +99,7 @@ func NewAPI(c Config) (api *API) { c: http.Client{}, UserAgent: "github.com/tpretz/go-zabbix-api", Logger: c.Log, - config: c, + Config: c, } if c.TlsNoVerify { @@ -144,7 +145,7 @@ func (api *API) callBytes(method string, params interface{}) (b []byte, err erro req.Header.Add("Content-Type", "application/json-rpc") req.Header.Add("User-Agent", api.UserAgent) - if api.config.Serialize { + if api.Config.Serialize { api.ex.Lock() defer api.ex.Unlock() } diff --git a/host.go b/host.go index 3fe3f25..226d823 100644 --- a/host.go +++ b/host.go @@ -1,5 +1,7 @@ package zabbix +import "encoding/json" + type ( // AvailableType (readonly) Availability of Zabbix agent // see "available" in: https://www.zabbix.com/documentation/3.2/manual/api/reference/host/object @@ -57,6 +59,33 @@ func (api *API) HostsGet(params Params) (res Hosts, err error) { params["output"] = "extend" } err = api.CallWithErrorParse("host.get", params, &res) + + // fix up host details if present + for i := 0; i < len(res); i++ { + h := res[i] + for j := 0; j < len(h.Interfaces); j++ { + in := h.Interfaces[j] + res[i].Interfaces[j].Details = nil + if len(in.RawDetails) == 0 { + continue + } + + asStr := string(in.RawDetails) + if asStr == "[]" { + continue + } + + out := HostInterfaceDetail{} + // assume singular, if api changes, this will fault + err := json.Unmarshal(in.RawDetails, &out) + if err != nil { + api.printf("got error during unmarshal %s", err) + panic(err) + } + res[i].Interfaces[j].Details = &out + } + + } return } @@ -106,9 +135,28 @@ func (api *API) HostGetByHost(host string) (res *Host, err error) { return } +// handle manual marshal +func prepHosts(hosts Hosts) { + for i := 0; i < len(hosts); i++ { + h := hosts[i] + for j := 0; j < len(h.Interfaces); j++ { + in := h.Interfaces[j] + + if in.Details == nil { + continue + } + + asB, _ := json.Marshal(in.Details) + hosts[i].Interfaces[j].RawDetails = json.RawMessage(asB) + } + + } +} + // HostsCreate Wrapper for host.create // https://www.zabbix.com/documentation/3.2/manual/api/reference/host/create func (api *API) HostsCreate(hosts Hosts) (err error) { + prepHosts(hosts) response, err := api.CallWithError("host.create", hosts) if err != nil { return @@ -125,6 +173,7 @@ func (api *API) HostsCreate(hosts Hosts) (err error) { // HostsUpdate Wrapper for host.update // https://www.zabbix.com/documentation/3.2/manual/api/reference/host/update func (api *API) HostsUpdate(hosts Hosts) (err error) { + prepHosts(hosts) _, err = api.CallWithError("host.update", hosts) return } diff --git a/host_interface.go b/host_interface.go index 6ed540e..d0c4507 100644 --- a/host_interface.go +++ b/host_interface.go @@ -1,5 +1,7 @@ package zabbix +import "encoding/json" + type ( // InterfaceType different interface type InterfaceType string @@ -22,14 +24,31 @@ const ( // HostInterface represents zabbix host interface type // https://www.zabbix.com/documentation/3.2/manual/api/reference/hostinterface/object type HostInterface struct { - InterfaceID string `json:"interfaceid,omitempty"` - DNS string `json:"dns"` - IP string `json:"ip"` - Main string `json:"main"` - Port string `json:"port"` - Type InterfaceType `json:"type"` - UseIP string `json:"useip"` + InterfaceID string `json:"interfaceid,omitempty"` + DNS string `json:"dns"` + IP string `json:"ip"` + Main string `json:"main"` + Port string `json:"port"` + Type InterfaceType `json:"type"` + UseIP string `json:"useip"` + RawDetails json.RawMessage `json:"details,omitempty"` + Details *HostInterfaceDetail `json:"-"` } // HostInterfaces is an array of HostInterface type HostInterfaces []HostInterface + +type HostInterfaceDetail struct { + Version string `json:"version,omitempty"` + Bulk string `json:"bulk,omitempty"` + Community string `json:"community,omitempty"` + SecurityName string `json:"securityname,omitempty"` + SecurityLevel string `json:"securitylevel,omitempty"` + AuthPassphrase string `json:"authpassphrase,omitempty"` + PrivPassphrase string `json:"privpassphrase,omitempty"` + AuthProtocol string `json:"authprotocol,omitempty"` + PrivProtocol string `json:"privprotocol,omitempty"` + ContextName string `json:"contextname,omitempty"` +} + +type HostInterfaceDetails []HostInterfaceDetail diff --git a/item.go b/item.go index e23d3a8..0bd8f49 100644 --- a/item.go +++ b/item.go @@ -58,6 +58,7 @@ const ( SNMPTrap ItemType = 17 Dependent ItemType = 18 HTTPAgent ItemType = 19 + SNMPAgent ItemType = 20 ) const ( diff --git a/template.go b/template.go index cebdb97..3b25c4c 100644 --- a/template.go +++ b/template.go @@ -9,8 +9,9 @@ type Template struct { Name string `json:"name,omitempty"` Groups HostGroupIDs `json:"groups"` UserMacros Macros `json:"macros"` - LinkedTemplates Templates `json:"templates,omitempty"` - TemplatesClear Templates `json:"templates_clear,omitempty"` + LinkedTemplates TemplateIDs `json:"templates,omitempty"` + ParentTemplates TemplateIDs `json:"parentTemplates,omitempty"` + TemplatesClear TemplateIDs `json:"templates_clear,omitempty"` LinkedHosts []string `json:"hosts,omitempty"` } From a0e56059481c0e3109b06b0fdb3c54273afab2b1 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Fri, 29 May 2020 13:57:12 +0100 Subject: [PATCH 3/3] adding a warning as we have odd customisations --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index be3b2b7..8f410f5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Go zabbix api +Note, this is not tested and is adjusted for use of tpretz/terraform-provider-zabbix + [![GoDoc](https://godoc.org/github.com/tpretz/go-zabbix-api?status.svg)](https://godoc.org/github.com/tpretz/go-zabbix-api) [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) [![Build Status](https://travis-ci.org/tpretz/go-zabbix-api.svg?branch=master)](https://travis-ci.org/tpretz/go-zabbix-api) This Go package provides access to Zabbix API.