Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
7 changes: 4 additions & 3 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ type API struct {
c http.Client
id int32
ex sync.Mutex
config Config
Config Config
}

type Config struct {
Url string
TlsNoVerify bool
Log *log.Logger
Serialize bool
Version int
}

// NewAPI Creates new API access object.
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
56 changes: 53 additions & 3 deletions host.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,9 +40,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"`
Expand All @@ -56,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
}

Expand Down Expand Up @@ -105,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
Expand All @@ -124,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
}
Expand Down
33 changes: 26 additions & 7 deletions host_interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package zabbix

import "encoding/json"

type (
// InterfaceType different interface type
InterfaceType string
Expand All @@ -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
1 change: 1 addition & 0 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const (
SNMPTrap ItemType = 17
Dependent ItemType = 18
HTTPAgent ItemType = 19
SNMPAgent ItemType = 20
)

const (
Expand Down
5 changes: 3 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down