From 653a79f1323e43c127149dc203a433d1ff098421 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 1 Mar 2020 10:25:43 +0000 Subject: [PATCH 01/54] don't send error --- item.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/item.go b/item.go index 8b98b2a..f78cf41 100644 --- a/item.go +++ b/item.go @@ -112,7 +112,7 @@ type Item struct { DataType DataType `json:"data_type,string"` Delta DeltaType `json:"delta,string"` Description string `json:"description"` - Error string `json:"error"` + Error string `json:"error,omitempty"` History string `json:"history,omitempty"` Trends string `json:"trends,omitempty"` TrapperHosts string `json:"trapper_hosts,omitempty"` From 6849df0a4cb130f1726c7c8a8a6f284e04dbaa8f Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 1 Mar 2020 10:25:49 +0000 Subject: [PATCH 02/54] add module file --- go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go.mod diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f63ac78 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/tpretz/go-zabbix-api + +go 1.12 From 9d4202d03e4d7eb00fa46284a70a8b284fdcd451 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 1 Mar 2020 20:17:10 +0000 Subject: [PATCH 03/54] adjust --- README.md | 8 ++++---- application_test.go | 2 +- base.go | 2 +- base_test.go | 2 +- doc.go | 4 ++-- go.mod | 5 +++++ go.sum | 4 ++++ host_group_test.go | 2 +- host_test.go | 2 +- item_test.go | 2 +- template_test.go | 2 +- trigger.go | 9 +++++++++ trigger_test.go | 2 +- 13 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 go.sum diff --git a/README.md b/README.md index 8455b54..be3b2b7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Go zabbix api -[![GoDoc](https://godoc.org/github.com/claranet/go-zabbix-api?status.svg)](https://godoc.org/github.com/claranet/go-zabbix-api) [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) [![Build Status](https://travis-ci.org/claranet/go-zabbix-api.svg?branch=master)](https://travis-ci.org/claranet/go-zabbix-api) +[![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. @@ -10,7 +10,7 @@ This package aims to support multiple zabbix resources from its API like trigger ## Install -Install it: `go get github.com/claranet/go-zabbix-api` +Install it: `go get github.com/tpretz/go-zabbix-api` ## Getting started @@ -20,7 +20,7 @@ package main import ( "fmt" - "github.com/claranet/go-zabbix-api" + "github.com/tpretz/go-zabbix-api" ) func main() { @@ -67,7 +67,7 @@ go test -v ## References -Documentation is available on [godoc.org](https://godoc.org/github.com/claranet/go-zabbix-api). +Documentation is available on [godoc.org](https://godoc.org/github.com/tpretz/go-zabbix-api). Also, Rafael Fernandes dos Santos wrote a [great article](http://www.sourcecode.net.br/2014/02/zabbix-api-with-golang.html) about using and extending this package. License: Simplified BSD License (see [LICENSE](LICENSE)). diff --git a/application_test.go b/application_test.go index 3ab7cbf..3f6ef0c 100644 --- a/application_test.go +++ b/application_test.go @@ -6,7 +6,7 @@ import ( "reflect" "testing" - zapi "github.com/claranet/go-zabbix-api" + zapi "github.com/tpretz/go-zabbix-api" ) func CreateApplication(host *zapi.Host, t *testing.T) *zapi.Application { diff --git a/base.go b/base.go index c7b5902..27df16d 100644 --- a/base.go +++ b/base.go @@ -82,7 +82,7 @@ type API struct { // 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"} + return &API{url: url, c: http.Client{}, UserAgent: "github.com/tpretz/go-zabbix-api"} } // SetClient Allows one to use specific http.Client, for example with InsecureSkipVerify transport. diff --git a/base_test.go b/base_test.go index 2435d45..e7cbf5c 100644 --- a/base_test.go +++ b/base_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - zapi "github.com/claranet/go-zabbix-api" + zapi "github.com/tpretz/go-zabbix-api" ) var ( diff --git a/doc.go b/doc.go index 5f22e7f..cf45cd2 100644 --- a/doc.go +++ b/doc.go @@ -5,7 +5,7 @@ language and the Zabbix monitoring API. Tested on Zabbix 3.2 but should work since 2.0 version. This package aims to support multiple zabbix resources from its API like trigger, application, host group, host, item, template.. -Install it: `go get github.com/claranet/go-zabbix-api` +Install it: `go get github.com/tpretz/go-zabbix-api` Getting started @@ -14,7 +14,7 @@ Getting started import ( "fmt" - "github.com/claranet/go-zabbix-api" + "github.com/tpretz/go-zabbix-api" ) func main() { diff --git a/go.mod b/go.mod index f63ac78..e9b4a62 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module github.com/tpretz/go-zabbix-api go 1.12 + +require ( + github.com/AlekSi/reflector v0.4.1 // indirect + github.com/tpretz/go-zabbix-api v0.3.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3518cc6 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/AlekSi/reflector v0.4.1 h1:onP7kPCAV/EXgHwKz1PyU9K/udpVJ8dVcOMm4brKrJ4= +github.com/AlekSi/reflector v0.4.1/go.mod h1:K9Yt9Jbte9Xw4QZ3e/ovaWduyRSlKVmwnze4z3+bVxs= +github.com/tpretz/go-zabbix-api v0.3.1 h1:152d0lY3OlmnYVV7QSurfr/8ALOoF+Rdfc15/mOERQk= +github.com/tpretz/go-zabbix-api v0.3.1/go.mod h1:/Y2aKeFo5342GTp/glX2eU0xU4z0Uha4T2kOaQa6ggM= diff --git a/host_group_test.go b/host_group_test.go index 30a8c05..a12556f 100644 --- a/host_group_test.go +++ b/host_group_test.go @@ -6,7 +6,7 @@ import ( "reflect" "testing" - zapi "github.com/claranet/go-zabbix-api" + zapi "github.com/tpretz/go-zabbix-api" ) func CreateHostGroup(t *testing.T) *zapi.HostGroup { diff --git a/host_test.go b/host_test.go index dc29a6a..b8ffe3e 100644 --- a/host_test.go +++ b/host_test.go @@ -6,7 +6,7 @@ import ( "reflect" "testing" - zapi "github.com/claranet/go-zabbix-api" + zapi "github.com/tpretz/go-zabbix-api" ) func CreateHost(group *zapi.HostGroup, t *testing.T) *zapi.Host { diff --git a/item_test.go b/item_test.go index 23af6b8..3933c61 100644 --- a/item_test.go +++ b/item_test.go @@ -3,7 +3,7 @@ package zabbix_test import ( "testing" - zapi "github.com/claranet/go-zabbix-api" + zapi "github.com/tpretz/go-zabbix-api" ) func CreateItem(app *zapi.Application, t *testing.T) *zapi.Item { diff --git a/template_test.go b/template_test.go index f8e2027..d3a0ec8 100644 --- a/template_test.go +++ b/template_test.go @@ -3,7 +3,7 @@ package zabbix_test import ( "testing" - zapi "github.com/claranet/go-zabbix-api" + zapi "github.com/tpretz/go-zabbix-api" ) func CreateTemplate(hostGroup *zapi.HostGroup, t *testing.T) *zapi.Template { diff --git a/trigger.go b/trigger.go index 15676a0..ca3f8e7 100644 --- a/trigger.go +++ b/trigger.go @@ -60,6 +60,15 @@ type Trigger struct { //TemplateId string `json:"templateid"` //Value ValueType `json:""` + Opdata string `json:"opdata,omitempty"` + // Type int `json:"type"` + Url string `json:"url,omitempty"` + //RecoveryMode int `json:"recovery_mode"` + RecoveryExpression string `json:"recovery_expression,omitempty"` + //CorrelationMode int `json:"correlation_mode"` + CorrelationTag string `json:"correlation_tag,omitempty"` + //ManualClose int `json:"manual_close"` + Priority SeverityType `json:"priority,string"` Status StatusType `json:"status,string"` Dependencies Triggers `json:"dependencies,omitempty"` diff --git a/trigger_test.go b/trigger_test.go index fd8a444..ef63ecb 100644 --- a/trigger_test.go +++ b/trigger_test.go @@ -4,7 +4,7 @@ import ( "fmt" "testing" - zapi "github.com/claranet/go-zabbix-api" + zapi "github.com/tpretz/go-zabbix-api" ) func CreateTrigger(item *zapi.Item, host *zapi.Host, t *testing.T) *zapi.Trigger { From 4e2922ca38681f6110eb858a2b592eb3bfe9b486 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Mon, 2 Mar 2020 17:02:01 +0000 Subject: [PATCH 04/54] enable extra fields --- trigger.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/trigger.go b/trigger.go index ca3f8e7..b0f73ea 100644 --- a/trigger.go +++ b/trigger.go @@ -61,13 +61,13 @@ type Trigger struct { //Value ValueType `json:""` Opdata string `json:"opdata,omitempty"` - // Type int `json:"type"` + Type string `json:"type"` Url string `json:"url,omitempty"` - //RecoveryMode int `json:"recovery_mode"` + RecoveryMode string `json:"recovery_mode"` RecoveryExpression string `json:"recovery_expression,omitempty"` - //CorrelationMode int `json:"correlation_mode"` + CorrelationMode string `json:"correlation_mode"` CorrelationTag string `json:"correlation_tag,omitempty"` - //ManualClose int `json:"manual_close"` + ManualClose string `json:"manual_close"` Priority SeverityType `json:"priority,string"` Status StatusType `json:"status,string"` From c26772a9267d9621baed1089fcc8910d22ba9be1 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Tue, 3 Mar 2020 20:21:04 +0000 Subject: [PATCH 05/54] api returns strings --- host_interface.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/host_interface.go b/host_interface.go index f50b4aa..ddb3864 100644 --- a/host_interface.go +++ b/host_interface.go @@ -2,7 +2,7 @@ package zabbix type ( // InterfaceType different interface type - InterfaceType int + InterfaceType string ) const ( @@ -10,13 +10,13 @@ const ( // see "type" in https://www.zabbix.com/documentation/3.2/manual/api/reference/hostinterface/object // Agent type - Agent InterfaceType = 1 + Agent InterfaceType = "1" // SNMP type - SNMP InterfaceType = 2 + SNMP InterfaceType = "2" // IPMI type - IPMI InterfaceType = 3 + IPMI InterfaceType = "3" // JMX type - JMX InterfaceType = 4 + JMX InterfaceType = "4" ) // HostInterface represents zabbix host interface type @@ -24,10 +24,10 @@ const ( type HostInterface struct { DNS string `json:"dns"` IP string `json:"ip"` - Main int `json:"main"` + Main string `json:"main"` Port string `json:"port"` Type InterfaceType `json:"type"` - UseIP int `json:"useip"` + UseIP string `json:"useip"` } // HostInterfaces is an array of HostInterface From c7063e7c32b3ac033616659e4a0db5f132b697d3 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Tue, 3 Mar 2020 20:44:06 +0000 Subject: [PATCH 06/54] adding interfaceid --- host_interface.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/host_interface.go b/host_interface.go index ddb3864..6ed540e 100644 --- a/host_interface.go +++ b/host_interface.go @@ -22,12 +22,13 @@ const ( // HostInterface represents zabbix host interface type // https://www.zabbix.com/documentation/3.2/manual/api/reference/hostinterface/object type HostInterface struct { - 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"` } // HostInterfaces is an array of HostInterface From c610918f880c574a161d6c296c50f9b41a8bfe24 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Tue, 3 Mar 2020 20:44:22 +0000 Subject: [PATCH 07/54] accept just groupids --- template.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/template.go b/template.go index 21ea8e2..cebdb97 100644 --- a/template.go +++ b/template.go @@ -3,15 +3,15 @@ package zabbix // Template represent Zabbix Template type returned from Zabbix API // https://www.zabbix.com/documentation/3.2/manual/api/reference/template/object type Template struct { - TemplateID string `json:"templateid,omitempty"` - Host string `json:"host"` - Description string `json:"description,omitempty"` - Name string `json:"name,omitempty"` - Groups HostGroups `json:"groups"` - UserMacros Macros `json:"macros"` - LinkedTemplates Templates `json:"templates,omitempty"` - TemplatesClear Templates `json:"templates_clear,omitempty"` - LinkedHosts []string `json:"hosts,omitempty"` + TemplateID string `json:"templateid,omitempty"` + Host string `json:"host"` + Description string `json:"description,omitempty"` + Name string `json:"name,omitempty"` + Groups HostGroupIDs `json:"groups"` + UserMacros Macros `json:"macros"` + LinkedTemplates Templates `json:"templates,omitempty"` + TemplatesClear Templates `json:"templates_clear,omitempty"` + LinkedHosts []string `json:"hosts,omitempty"` } // Templates is an Array of Template structs. From 699a7b7d07a8dcdac76fc067b4f8ef46953f9b7a Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Wed, 4 Mar 2020 11:39:49 +0000 Subject: [PATCH 08/54] adding extra fields for http item --- item.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/item.go b/item.go index f78cf41..fb24c18 100644 --- a/item.go +++ b/item.go @@ -53,7 +53,10 @@ const ( // Calculated type Calculated ItemType = 15 // JMXAgent type - JMXAgent ItemType = 16 + JMXAgent ItemType = 16 + SNMPTrap ItemType = 17 + Dependent ItemType = 18 + HTTPAgent ItemType = 19 ) const ( @@ -121,6 +124,16 @@ type Item struct { ApplicationIds []string `json:"applications,omitempty"` ItemParent Hosts `json:"hosts"` + + // HTTP Agent Fields + Url string `json:"url,omitempty"` + RequestMethod string `json:"request_method,omitempty"` + PostType string `json:"post_type,omitempty"` + Posts string `json:"posts,omitempty"` + StatusCodes string `json:"status_codes,omitempty"` + Timeout string `json:"timeout,omitempty"` + VerifyHost string `json:"verify_host,omitemtpy"` + VerifyPeer string `json:"verify_peer,omitempty"` } // Items is an array of Item From 894f0e338ff3be8399a43c12c885248439e8ea41 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Wed, 4 Mar 2020 11:53:36 +0000 Subject: [PATCH 09/54] typo --- item.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/item.go b/item.go index fb24c18..0549e8f 100644 --- a/item.go +++ b/item.go @@ -132,7 +132,7 @@ type Item struct { Posts string `json:"posts,omitempty"` StatusCodes string `json:"status_codes,omitempty"` Timeout string `json:"timeout,omitempty"` - VerifyHost string `json:"verify_host,omitemtpy"` + VerifyHost string `json:"verify_host,omitempty"` VerifyPeer string `json:"verify_peer,omitempty"` } From 0b746f2c84b75db6577c0a8dd865df0b5970944a Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Wed, 4 Mar 2020 13:24:37 +0000 Subject: [PATCH 10/54] adding extra init options --- base.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/base.go b/base.go index 27df16d..9160f24 100644 --- a/base.go +++ b/base.go @@ -2,6 +2,7 @@ package zabbix import ( "bytes" + "crypto/tls" "encoding/json" "fmt" "io/ioutil" @@ -77,12 +78,37 @@ type API struct { id int32 } +type Config struct { + Url string + TlsNoVerify bool + Log *log.Logger +} + // 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/tpretz/go-zabbix-api"} +func NewAPI(c Config) (api *API) { + api = &API{ + url: c.Url, + c: http.Client{}, + UserAgent: "github.com/tpretz/go-zabbix-api", + Logger: c.Log, + } + + if c.TlsNoVerify { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + } + api.c = http.Client{ + Transport: tr, + } + api.printf("TLS running in insecure mode, do not use this configuration in production") + } + + return } // SetClient Allows one to use specific http.Client, for example with InsecureSkipVerify transport. From aca6851484c3397fbfe9c2bd47891498c4140d74 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Wed, 4 Mar 2020 15:28:51 +0000 Subject: [PATCH 11/54] adding support for serializing requests --- base.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/base.go b/base.go index 9160f24..4f24d8f 100644 --- a/base.go +++ b/base.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "net/http" + "sync" "sync/atomic" ) @@ -76,12 +77,15 @@ type API struct { url string c http.Client id int32 + ex sync.Mutex + config Config } type Config struct { Url string TlsNoVerify bool Log *log.Logger + Serialize bool } // NewAPI Creates new API access object. @@ -94,6 +98,7 @@ func NewAPI(c Config) (api *API) { c: http.Client{}, UserAgent: "github.com/tpretz/go-zabbix-api", Logger: c.Log, + config: c, } if c.TlsNoVerify { @@ -139,6 +144,11 @@ 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 { + api.ex.Lock() + defer api.ex.Unlock() + } + res, err := api.c.Do(req) if err != nil { api.printf("Error : %s", err) From 32eb4e4e57f5ea979f2938c9a172ad3f26836320 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Wed, 4 Mar 2020 17:16:14 +0000 Subject: [PATCH 12/54] adding preprocessors --- item.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/item.go b/item.go index 0549e8f..32f9ee7 100644 --- a/item.go +++ b/item.go @@ -125,6 +125,8 @@ type Item struct { ItemParent Hosts `json:"hosts"` + Preprocessors Preprocessors `json:"preprocessing,omitempty"` + // HTTP Agent Fields Url string `json:"url,omitempty"` RequestMethod string `json:"request_method,omitempty"` @@ -136,6 +138,15 @@ type Item struct { VerifyPeer string `json:"verify_peer,omitempty"` } +type Preprocessors []Preprocessor + +type Preprocessor struct { + Type string `json:"type,omitempty"` + Params string `json:"params"` + ErrorHandler string `json:"error_handler,omitempty"` + ErrorHandlerParams string `json:"error_handler_params"` +} + // Items is an array of Item type Items []Item From 99febacac587a489e6d6dfcf93f8e54239dbb769 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Wed, 4 Mar 2020 21:37:05 +0000 Subject: [PATCH 13/54] starting snmp fields --- item.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/item.go b/item.go index 32f9ee7..3a5e73c 100644 --- a/item.go +++ b/item.go @@ -136,6 +136,10 @@ type Item struct { Timeout string `json:"timeout,omitempty"` VerifyHost string `json:"verify_host,omitempty"` VerifyPeer string `json:"verify_peer,omitempty"` + + // SNMP Fields + SNMPOid string `json:"snmp_oid,omitempty"` + SNMPCommunity string `json:"snmp_community,omitempty"` } type Preprocessors []Preprocessor From ecf68966f43efb29ed11360e77a22e4f5f023f57 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Thu, 5 Mar 2020 20:04:06 +0000 Subject: [PATCH 14/54] adding fields --- item.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/item.go b/item.go index 3a5e73c..8cf9f7e 100644 --- a/item.go +++ b/item.go @@ -140,6 +140,13 @@ type Item struct { // SNMP Fields SNMPOid string `json:"snmp_oid,omitempty"` SNMPCommunity string `json:"snmp_community,omitempty"` + SNMPv3AuthPassphrase string `json:"snmpv3_authpassphrase,omitempty"` + SNMPv3AuthProtocol string `json:"snmpv3_authprotocol,omitempty"` + SNMPv3ContextName string `json:"snmpv3_contextname,omitempty"` + SNMPv3PrivPasshrase string `json:"snmpv3_privpassphrase,omitempty"` + SNMPv3PrivProtocol string `json:"snmpv3_privprotocol,omitempty"` + SNMPv3SecurityLevel string `json:"snmpv3_securitylevel,omitempty"` + SNMPv3SecurityName string `json:"snmpv3_securityname,omitempty"` } type Preprocessors []Preprocessor From c5d1c2f608176ffaceaa0b7350888ffd5cd704e8 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 7 Mar 2020 14:43:30 +0000 Subject: [PATCH 15/54] extra field --- host.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/host.go b/host.go index 37916eb..058be5a 100644 --- a/host.go +++ b/host.go @@ -40,6 +40,8 @@ type Host struct { GroupIds HostGroupIDs `json:"groups,omitempty"` Interfaces HostInterfaces `json:"interfaces,omitempty"` TemplateIDs TemplateIDs `json:"templates,omitempty"` + // templates are read back from this one + ParentTemplateIDs TemplateIDs `json:"parentTemplates,omitempty"` } // Hosts is an array of Host From a671104691cb5002bfa89618c7b47e6fd4410d30 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 7 Mar 2020 17:28:25 +0000 Subject: [PATCH 16/54] adding macros --- host.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/host.go b/host.go index 058be5a..61d21bb 100644 --- a/host.go +++ b/host.go @@ -29,12 +29,13 @@ const ( // Host represent Zabbix host object // https://www.zabbix.com/documentation/3.2/manual/api/reference/host/object type Host struct { - HostID string `json:"hostid,omitempty"` - Host string `json:"host"` - Available AvailableType `json:"available,string"` - Error string `json:"error"` - Name string `json:"name"` - Status StatusType `json:"status,string"` + HostID string `json:"hostid,omitempty"` + Host string `json:"host"` + Available AvailableType `json:"available,string"` + Error string `json:"error"` + Name string `json:"name"` + Status StatusType `json:"status,string"` + UserMacros Macros `json:"macros,omitempty"` // Fields below used only when creating hosts GroupIds HostGroupIDs `json:"groups,omitempty"` From a038a4156d823ee6da6fc1333ff22fd3e1f41ac5 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 7 Mar 2020 17:28:48 +0000 Subject: [PATCH 17/54] optional on create/global --- macro.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/macro.go b/macro.go index 5964155..a09108e 100644 --- a/macro.go +++ b/macro.go @@ -4,7 +4,7 @@ package zabbix // https://www.zabbix.com/documentation/3.2/manual/api/reference/usermacro/object type Macro struct { MacroID string `json:"hostmacroids,omitempty"` - HostID string `json:"hostid"` + HostID string `json:"hostid,omitempty"` MacroName string `json:"macro"` Value string `json:"value"` } From ded284a9bb321cc37d0a0421019ac2342235a3a8 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 7 Mar 2020 17:29:00 +0000 Subject: [PATCH 18/54] just unbox the IDs --- trigger.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/trigger.go b/trigger.go index b0f73ea..cfc5779 100644 --- a/trigger.go +++ b/trigger.go @@ -39,6 +39,13 @@ const ( Problem ValueType = 1 ) +type TriggerID struct { + TriggerID string `json:"triggerid"` +} + +// TemplateIDs is an Array of TemplateID structs. +type TriggerIDs []TriggerID + // TriggerFunction The function objects represents the functions used in the trigger expression type TriggerFunction struct { FunctionID string `json:"functionid"` @@ -60,18 +67,18 @@ type Trigger struct { //TemplateId string `json:"templateid"` //Value ValueType `json:""` - Opdata string `json:"opdata,omitempty"` + Opdata string `json:"opdata,omitempty"` Type string `json:"type"` - Url string `json:"url,omitempty"` - RecoveryMode string `json:"recovery_mode"` + Url string `json:"url,omitempty"` + RecoveryMode string `json:"recovery_mode"` RecoveryExpression string `json:"recovery_expression,omitempty"` - CorrelationMode string `json:"correlation_mode"` - CorrelationTag string `json:"correlation_tag,omitempty"` - ManualClose string `json:"manual_close"` + CorrelationMode string `json:"correlation_mode"` + CorrelationTag string `json:"correlation_tag,omitempty"` + ManualClose string `json:"manual_close"` Priority SeverityType `json:"priority,string"` Status StatusType `json:"status,string"` - Dependencies Triggers `json:"dependencies,omitempty"` + Dependencies TriggerIDs `json:"dependencies,omitempty"` Functions TriggerFunctions `json:"functions,omitempty"` // Items contained by the trigger in the items property. ContainedItems Items `json:"items,omitempty"` From 2cc044d25576f2e942ea94fb93bca2c597f47cc8 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 15 Mar 2020 08:38:52 +0000 Subject: [PATCH 19/54] adding tags --- trigger.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/trigger.go b/trigger.go index cfc5779..0b0a9f2 100644 --- a/trigger.go +++ b/trigger.go @@ -39,6 +39,13 @@ const ( Problem ValueType = 1 ) +type Tag struct { + Tag string `json:"tag"` + Value string `json:"value,omitempty"` +} + +type Tags []Tag + type TriggerID struct { TriggerID string `json:"triggerid"` } @@ -84,6 +91,7 @@ type Trigger struct { ContainedItems Items `json:"items,omitempty"` // Hosts that the trigger belongs to in the hosts property. ParentHosts Hosts `json:"hosts,omitempty"` + Tags Tags `json:"tags,omitempty"` } // Triggers is an array of Trigger From 42f96e2695faa8e92a827ab2fea31d958b8707fb Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 21 Mar 2020 09:35:29 +0000 Subject: [PATCH 20/54] adding dependent item field --- item.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/item.go b/item.go index 8cf9f7e..281362b 100644 --- a/item.go +++ b/item.go @@ -147,6 +147,9 @@ type Item struct { SNMPv3PrivProtocol string `json:"snmpv3_privprotocol,omitempty"` SNMPv3SecurityLevel string `json:"snmpv3_securitylevel,omitempty"` SNMPv3SecurityName string `json:"snmpv3_securityname,omitempty"` + + // Dependent Fields + MasterItemID string `json:"master_itemid,omitempty"` } type Preprocessors []Preprocessor From a286e126655b85015882622f0ed5fea17020d18f Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 28 Mar 2020 09:49:31 +0000 Subject: [PATCH 21/54] adding basic proxy support --- host.go | 1 + proxy.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 proxy.go diff --git a/host.go b/host.go index 61d21bb..e1e6ceb 100644 --- a/host.go +++ b/host.go @@ -43,6 +43,7 @@ type Host struct { TemplateIDs TemplateIDs `json:"templates,omitempty"` // templates are read back from this one ParentTemplateIDs TemplateIDs `json:"parentTemplates,omitempty"` + ProxyID string `json:"proxy_hostid,omitempty"` } // Hosts is an array of Host diff --git a/proxy.go b/proxy.go new file mode 100644 index 0000000..e4ab8ec --- /dev/null +++ b/proxy.go @@ -0,0 +1,22 @@ +package zabbix + +// Proxy represent Zabbix proxy object +// https://www.zabbix.com/documentation/3.2/manual/api/reference/proxy/object +type Proxy struct { + ProxyID string `json:"proxyid,omitempty"` + Host string `json:"host"` + // add rest later +} + +// Proxies is an array of Proxy +type Proxies []Proxy + +// ProxiesGet Wrapper for proxy.get +// https://www.zabbix.com/documentation/3.2/manual/api/reference/proxy/get +func (api *API) ProxiesGet(params Params) (res Proxies, err error) { + if _, present := params["output"]; !present { + params["output"] = "extend" + } + err = api.CallWithErrorParse("proxy.get", params, &res) + return +} From 6039d7e30fa510f14c3dd5dd95b62a5da750aa98 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 24 May 2020 12:36:25 +0100 Subject: [PATCH 22/54] quick lld rule addition --- lld.go | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 lld.go diff --git a/lld.go b/lld.go new file mode 100644 index 0000000..8e4123e --- /dev/null +++ b/lld.go @@ -0,0 +1,175 @@ +package zabbix + +type LLDRuleFilterCondition struct { + Macro string `json:"macro"` + Value string `json:"value"` + FormulaID string `json:"formulaid,omitempty"` + Operator string `json:"operator,omitempty"` +} + +type LLDRuleFilterConditions []LLDRuleFilterCondition + +type LLDRuleFilter struct { + Conditions LLDRuleFilterConditions `json:"conditions` +} + +// Item represent Zabbix lld object +// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/object +type LLDRule struct { + ItemID string `json:"itemid,omitempty"` + Delay string `json:"delay"` + HostID string `json:"hostid"` + InterfaceID string `json:"interfaceid,omitempty"` + Key string `json:"key_"` + Name string `json:"name"` + Type ItemType `json:"type,string"` + // ValueType ValueType `json:"value_type,string"` + // DataType DataType `json:"data_type,string"` + // Delta DeltaType `json:"delta,string"` + AuthType string `json:"authtype,omitempty"` + DelayFlex string `json:"delay_flex,omitempty"` + Description string `json:"description"` + Error string `json:"error,omitempty"` + IpmiSensor string `json:"ipmi_sensor,omitempty"` + LifeTime string `json:"lifetime,omitempty"` + Params string `json:"params,omitempty"` + PrivateKey string `json:"privatekey,omitempty"` + PublicKey string `json:"publickey,omitempty"` + Status string `json:"status,omitempty"` + TrapperHosts string `json:"trapper_hosts,omitempty"` + + // ssh / telnet + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` + Port string `json:"port,omitempty"` + + // HTTP Agent Fields + Url string `json:"url,omitempty"` + RequestMethod string `json:"request_method,omitempty"` + AllowTraps string `json:"allow_traps,omitempty"` + PostType string `json:"post_type,omitempty"` + Posts string `json:"posts,omitempty"` + StatusCodes string `json:"status_codes,omitempty"` + Timeout string `json:"timeout,omitempty"` + VerifyHost string `json:"verify_host,omitempty"` + VerifyPeer string `json:"verify_peer,omitempty"` + + // SNMP Fields + SNMPOid string `json:"snmp_oid,omitempty"` + SNMPCommunity string `json:"snmp_community,omitempty"` + SNMPv3AuthPassphrase string `json:"snmpv3_authpassphrase,omitempty"` + SNMPv3AuthProtocol string `json:"snmpv3_authprotocol,omitempty"` + SNMPv3ContextName string `json:"snmpv3_contextname,omitempty"` + SNMPv3PrivPasshrase string `json:"snmpv3_privpassphrase,omitempty"` + SNMPv3PrivProtocol string `json:"snmpv3_privprotocol,omitempty"` + SNMPv3SecurityLevel string `json:"snmpv3_securitylevel,omitempty"` + SNMPv3SecurityName string `json:"snmpv3_securityname,omitempty"` + + Preprocessors Preprocessors `json:"preprocessing,omitempty"` + Filter LLDRuleFilter `json:"filter"` +} + +// Items is an array of Item +type LLDRules []LLDRule + +// ItemsGet Wrapper for item.get +// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/get +func (api *API) LLDsGet(params Params) (res LLDRules, err error) { + if _, present := params["output"]; !present { + params["output"] = "extend" + } + err = api.CallWithErrorParse("discoveryrule.get", params, &res) + return +} + +// ItemGetByID Gets item by Id only if there is exactly 1 matching host. +func (api *API) LLDGetByID(id string) (res *LLDRule, err error) { + items, err := api.LLDsGet(Params{"itemids": id}) + if err != nil { + return + } + + if len(items) != 1 { + e := ExpectedOneResult(len(items)) + err = &e + return + } + res = &items[0] + return +} + +// ItemsCreate Wrapper for item.create +// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/create +func (api *API) LLDsCreate(items LLDRules) (err error) { + response, err := api.CallWithError("discoveryrule.create", items) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + itemids := result["itemids"].([]interface{}) + for i, id := range itemids { + items[i].ItemID = id.(string) + } + return +} + +// ItemsUpdate Wrapper for item.update +// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/update +func (api *API) LLDsUpdate(items LLDRules) (err error) { + _, err = api.CallWithError("discoveryrule.update", items) + return +} + +// ItemsDelete Wrapper for item.delete +// Cleans ItemId in all items elements if call succeed. +// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/delete +func (api *API) LLDsDelete(items LLDRules) (err error) { + ids := make([]string, len(items)) + for i, item := range items { + ids[i] = item.ItemID + } + + err = api.LLDDeleteByIds(ids) + if err == nil { + for i := range items { + items[i].ItemID = "" + } + } + return +} + +// ItemsDeleteByIds Wrapper for item.delete +// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/delete +func (api *API) LLDDeleteByIds(ids []string) (err error) { + deleteIds, err := api.LLDDeleteIDs(ids) + if err != nil { + return + } + l := len(deleteIds) + if len(ids) != l { + err = &ExpectedMore{len(ids), l} + } + return +} + +// ItemsDeleteIDs Wrapper for item.delete +// Delete the item and return the id of the deleted item +func (api *API) LLDDeleteIDs(ids []string) (itemids []interface{}, err error) { + response, err := api.CallWithError("discoveryrule.delete", ids) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + itemids1, ok := result["itemids"].([]interface{}) + if !ok { + itemids2 := result["itemids"].(map[string]interface{}) + for _, id := range itemids2 { + itemids = append(itemids, id) + } + } else { + itemids = itemids1 + } + return +} From da954ca9a047875881bad67bce1dad7fd717d99e Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 24 May 2020 12:39:56 +0100 Subject: [PATCH 23/54] missed a few attrs --- lld.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lld.go b/lld.go index 8e4123e..e81f4cc 100644 --- a/lld.go +++ b/lld.go @@ -10,7 +10,10 @@ type LLDRuleFilterCondition struct { type LLDRuleFilterConditions []LLDRuleFilterCondition type LLDRuleFilter struct { - Conditions LLDRuleFilterConditions `json:"conditions` + Conditions LLDRuleFilterConditions `json:"conditions` + EvalType string `json:"evaltype"` + EvalFormula string `json:"eval_formula"` + Formula string `json:"formula"` } // Item represent Zabbix lld object From f206516ff45e5018619423d7160e6a4d07052d4d Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 24 May 2020 21:15:48 +0100 Subject: [PATCH 24/54] fixes --- lld.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lld.go b/lld.go index e81f4cc..8d427c5 100644 --- a/lld.go +++ b/lld.go @@ -10,9 +10,9 @@ type LLDRuleFilterCondition struct { type LLDRuleFilterConditions []LLDRuleFilterCondition type LLDRuleFilter struct { - Conditions LLDRuleFilterConditions `json:"conditions` + Conditions LLDRuleFilterConditions `json:"conditions"` EvalType string `json:"evaltype"` - EvalFormula string `json:"eval_formula"` + EvalFormula string `json:"eval_formula,omitempty"` Formula string `json:"formula"` } From bc2e562d6112b02136ef532ba779f37ca5fc6261 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Mon, 25 May 2020 09:40:40 +0100 Subject: [PATCH 25/54] use types --- lld.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lld.go b/lld.go index 8d427c5..e90ee7c 100644 --- a/lld.go +++ b/lld.go @@ -1,17 +1,31 @@ package zabbix +type ( + LLDEvalType string + LLDOperatorType string +) + +const ( + LLDAndOr LLDEvalType = "0" + LLDAnd LLDEvalType = "1" + LLDOr LLDEvalType = "2" + LLDCustom LLDEvalType = "3" + LLDMatch LLDOperatorType = "8" + LLDNotMatch LLDOperatorType = "9" +) + type LLDRuleFilterCondition struct { - Macro string `json:"macro"` - Value string `json:"value"` - FormulaID string `json:"formulaid,omitempty"` - Operator string `json:"operator,omitempty"` + Macro string `json:"macro"` + Value string `json:"value"` + FormulaID string `json:"formulaid,omitempty"` + Operator LLDOperatorType `json:"operator,omitempty"` } type LLDRuleFilterConditions []LLDRuleFilterCondition type LLDRuleFilter struct { Conditions LLDRuleFilterConditions `json:"conditions"` - EvalType string `json:"evaltype"` + EvalType LLDEvalType `json:"evaltype"` EvalFormula string `json:"eval_formula,omitempty"` Formula string `json:"formula"` } From 0e28265a4de9af81a74aa5a5ef694413f72026d6 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Mon, 25 May 2020 14:08:03 +0100 Subject: [PATCH 26/54] proto item --- item.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++----- lld.go | 4 +-- 2 files changed, 98 insertions(+), 10 deletions(-) diff --git a/item.go b/item.go index 281362b..0952787 100644 --- a/item.go +++ b/item.go @@ -138,18 +138,22 @@ type Item struct { VerifyPeer string `json:"verify_peer,omitempty"` // SNMP Fields - SNMPOid string `json:"snmp_oid,omitempty"` - SNMPCommunity string `json:"snmp_community,omitempty"` + SNMPOid string `json:"snmp_oid,omitempty"` + SNMPCommunity string `json:"snmp_community,omitempty"` SNMPv3AuthPassphrase string `json:"snmpv3_authpassphrase,omitempty"` - SNMPv3AuthProtocol string `json:"snmpv3_authprotocol,omitempty"` - SNMPv3ContextName string `json:"snmpv3_contextname,omitempty"` - SNMPv3PrivPasshrase string `json:"snmpv3_privpassphrase,omitempty"` - SNMPv3PrivProtocol string `json:"snmpv3_privprotocol,omitempty"` - SNMPv3SecurityLevel string `json:"snmpv3_securitylevel,omitempty"` - SNMPv3SecurityName string `json:"snmpv3_securityname,omitempty"` + SNMPv3AuthProtocol string `json:"snmpv3_authprotocol,omitempty"` + SNMPv3ContextName string `json:"snmpv3_contextname,omitempty"` + SNMPv3PrivPasshrase string `json:"snmpv3_privpassphrase,omitempty"` + SNMPv3PrivProtocol string `json:"snmpv3_privprotocol,omitempty"` + SNMPv3SecurityLevel string `json:"snmpv3_securitylevel,omitempty"` + SNMPv3SecurityName string `json:"snmpv3_securityname,omitempty"` // Dependent Fields MasterItemID string `json:"master_itemid,omitempty"` + + // Prototype + RuleID string `json:"ruleid,omitempty"` + DiscoveryRule *LLDRule `json:"discoveryRule,omitEmpty"` } type Preprocessors []Preprocessor @@ -186,6 +190,13 @@ func (api *API) ItemsGet(params Params) (res Items, err error) { err = api.CallWithErrorParse("item.get", params, &res) return } +func (api *API) ProtoItemsGet(params Params) (res Items, err error) { + if _, present := params["output"]; !present { + params["output"] = "extend" + } + err = api.CallWithErrorParse("itemprototype.get", params, &res) + return +} // ItemGetByID Gets item by Id only if there is exactly 1 matching host. func (api *API) ItemGetByID(id string) (res *Item, err error) { @@ -202,11 +213,28 @@ func (api *API) ItemGetByID(id string) (res *Item, err error) { res = &items[0] return } +func (api *API) ProtoItemGetByID(id string) (res *Item, err error) { + items, err := api.ProtoItemsGet(Params{"itemids": id}) + if err != nil { + return + } + + if len(items) != 1 { + e := ExpectedOneResult(len(items)) + err = &e + return + } + res = &items[0] + return +} // ItemsGetByApplicationID Gets items by application Id. func (api *API) ItemsGetByApplicationID(id string) (res Items, err error) { return api.ItemsGet(Params{"applicationids": id}) } +func (api *API) ProtoItemsGetByApplicationID(id string) (res Items, err error) { + return api.ProtoItemsGet(Params{"applicationids": id}) +} // ItemsCreate Wrapper for item.create // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/create @@ -223,6 +251,19 @@ func (api *API) ItemsCreate(items Items) (err error) { } return } +func (api *API) ProtoItemsCreate(items Items) (err error) { + response, err := api.CallWithError("itemprototype.create", items) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + itemids := result["itemids"].([]interface{}) + for i, id := range itemids { + items[i].ItemID = id.(string) + } + return +} // ItemsUpdate Wrapper for item.update // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/update @@ -230,6 +271,10 @@ func (api *API) ItemsUpdate(items Items) (err error) { _, err = api.CallWithError("item.update", items) return } +func (api *API) ProtoItemsUpdate(items Items) (err error) { + _, err = api.CallWithError("itemprototype.update", items) + return +} // ItemsDelete Wrapper for item.delete // Cleans ItemId in all items elements if call succeed. @@ -248,6 +293,20 @@ func (api *API) ItemsDelete(items Items) (err error) { } return } +func (api *API) ProtoItemsDelete(items Items) (err error) { + ids := make([]string, len(items)) + for i, item := range items { + ids[i] = item.ItemID + } + + err = api.ProtoItemsDeleteByIds(ids) + if err == nil { + for i := range items { + items[i].ItemID = "" + } + } + return +} // ItemsDeleteByIds Wrapper for item.delete // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/delete @@ -262,6 +321,17 @@ func (api *API) ItemsDeleteByIds(ids []string) (err error) { } return } +func (api *API) ProtoItemsDeleteByIds(ids []string) (err error) { + deleteIds, err := api.ProtoItemsDeleteIDs(ids) + if err != nil { + return + } + l := len(deleteIds) + if len(ids) != l { + err = &ExpectedMore{len(ids), l} + } + return +} // ItemsDeleteIDs Wrapper for item.delete // Delete the item and return the id of the deleted item @@ -283,3 +353,21 @@ func (api *API) ItemsDeleteIDs(ids []string) (itemids []interface{}, err error) } return } +func (api *API) ProtoItemsDeleteIDs(ids []string) (itemids []interface{}, err error) { + response, err := api.CallWithError("itemprototype.delete", ids) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + itemids1, ok := result["itemids"].([]interface{}) + if !ok { + itemids2 := result["itemids"].(map[string]interface{}) + for _, id := range itemids2 { + itemids = append(itemids, id) + } + } else { + itemids = itemids1 + } + return +} diff --git a/lld.go b/lld.go index e90ee7c..ad9607a 100644 --- a/lld.go +++ b/lld.go @@ -179,9 +179,9 @@ func (api *API) LLDDeleteIDs(ids []string) (itemids []interface{}, err error) { } result := response.Result.(map[string]interface{}) - itemids1, ok := result["itemids"].([]interface{}) + itemids1, ok := result["ruleids"].([]interface{}) if !ok { - itemids2 := result["itemids"].(map[string]interface{}) + itemids2 := result["ruleids"].(map[string]interface{}) for _, id := range itemids2 { itemids = append(itemids, id) } From d674829bb182e1ceabb46b9a981db2c62e017f33 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Mon, 25 May 2020 15:02:56 +0100 Subject: [PATCH 27/54] adding trigger prototypes --- trigger.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/trigger.go b/trigger.go index 0b0a9f2..986346d 100644 --- a/trigger.go +++ b/trigger.go @@ -106,6 +106,13 @@ func (api *API) TriggersGet(params Params) (res Triggers, err error) { err = api.CallWithErrorParse("trigger.get", params, &res) return } +func (api *API) ProtoTriggersGet(params Params) (res Triggers, err error) { + if _, present := params["output"]; !present { + params["output"] = "extend" + } + err = api.CallWithErrorParse("triggerprototype.get", params, &res) + return +} // TriggerGetByID Gets trigger by Id only if there is exactly 1 matching host. func (api *API) TriggerGetByID(id string) (res *Trigger, err error) { @@ -122,6 +129,20 @@ func (api *API) TriggerGetByID(id string) (res *Trigger, err error) { res = &triggers[0] return } +func (api *API) ProtoTriggerGetByID(id string) (res *Trigger, err error) { + triggers, err := api.ProtoTriggersGet(Params{"triggerids": id}) + if err != nil { + return + } + + if len(triggers) != 1 { + e := ExpectedOneResult(len(triggers)) + err = &e + return + } + res = &triggers[0] + return +} // TriggersCreate Wrapper for trigger.create // https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/create @@ -138,6 +159,19 @@ func (api *API) TriggersCreate(triggers Triggers) (err error) { } return } +func (api *API) ProtoTriggersCreate(triggers Triggers) (err error) { + response, err := api.CallWithError("triggerprototype.create", triggers) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + triggerids := result["triggerids"].([]interface{}) + for i, id := range triggerids { + triggers[i].TriggerID = id.(string) + } + return +} // TriggersUpdate Wrapper for trigger.update // https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/update @@ -145,6 +179,10 @@ func (api *API) TriggersUpdate(triggers Triggers) (err error) { _, err = api.CallWithError("trigger.update", triggers) return } +func (api *API) ProtoTriggersUpdate(triggers Triggers) (err error) { + _, err = api.CallWithError("triggerprototype.update", triggers) + return +} // TriggersDelete Wrapper for trigger.delete // Cleans ItemId in all triggers elements if call succeed. @@ -163,6 +201,20 @@ func (api *API) TriggersDelete(triggers Triggers) (err error) { } return } +func (api *API) ProtoTriggersDelete(triggers Triggers) (err error) { + ids := make([]string, len(triggers)) + for i, trigger := range triggers { + ids[i] = trigger.TriggerID + } + + err = api.ProtoTriggersDeleteByIds(ids) + if err == nil { + for i := range triggers { + triggers[i].TriggerID = "" + } + } + return +} // TriggersDeleteByIds Wrapper for trigger.delete // https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/delete @@ -177,6 +229,17 @@ func (api *API) TriggersDeleteByIds(ids []string) (err error) { } return } +func (api *API) ProtoTriggersDeleteByIds(ids []string) (err error) { + deleteIds, err := api.ProtoTriggersDeleteIDs(ids) + if err != nil { + return + } + l := len(deleteIds) + if len(ids) != l { + err = &ExpectedMore{len(ids), l} + } + return +} // TriggersDeleteIDs Wrapper for trigger.delete // return the id of the deleted trigger @@ -198,3 +261,21 @@ func (api *API) TriggersDeleteIDs(ids []string) (triggerids []interface{}, err e } return } +func (api *API) ProtoTriggersDeleteIDs(ids []string) (triggerids []interface{}, err error) { + response, err := api.CallWithError("triggerprototype.delete", ids) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + triggerids1, ok := result["triggerids"].([]interface{}) + if !ok { + triggerids2 := result["triggerids"].(map[string]interface{}) + for _, id := range triggerids2 { + triggerids = append(triggerids, id) + } + } else { + triggerids = triggerids1 + } + return +} From 4a6ceca79b10d4b2b2e5fba12f5f6c7a05a1c8d6 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Mon, 25 May 2020 16:37:30 +0100 Subject: [PATCH 28/54] add params --- item.go | 1 + 1 file changed, 1 insertion(+) diff --git a/item.go b/item.go index 0952787..a00570f 100644 --- a/item.go +++ b/item.go @@ -119,6 +119,7 @@ type Item struct { History string `json:"history,omitempty"` Trends string `json:"trends,omitempty"` TrapperHosts string `json:"trapper_hosts,omitempty"` + Params string `json:"params,omitempty"` // Fields below used only when creating applications ApplicationIds []string `json:"applications,omitempty"` From d40d728183c6fd383005e6fa051b62f48e8e52e3 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Mon, 25 May 2020 18:23:10 +0100 Subject: [PATCH 29/54] dependent lld --- lld.go | 1 + 1 file changed, 1 insertion(+) diff --git a/lld.go b/lld.go index ad9607a..d84288c 100644 --- a/lld.go +++ b/lld.go @@ -54,6 +54,7 @@ type LLDRule struct { PublicKey string `json:"publickey,omitempty"` Status string `json:"status,omitempty"` TrapperHosts string `json:"trapper_hosts,omitempty"` + MasterItemID string `json:"master_itemid,omitempty"` // ssh / telnet Username string `json:"username,omitempty"` From 7f3820431e83cc1bc0e66cca3d8d48dea492e069 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Tue, 26 May 2020 11:00:51 +0100 Subject: [PATCH 30/54] adding macro paths --- lld.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lld.go b/lld.go index d84288c..d49d8d5 100644 --- a/lld.go +++ b/lld.go @@ -30,6 +30,13 @@ type LLDRuleFilter struct { Formula string `json:"formula"` } +type LLDMacroPath struct { + Macro string `json:"lld_macro"` + Path string `json:"path"` +} + +type LLDMacroPaths []LLDMacroPath + // Item represent Zabbix lld object // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/object type LLDRule struct { @@ -85,6 +92,7 @@ type LLDRule struct { Preprocessors Preprocessors `json:"preprocessing,omitempty"` Filter LLDRuleFilter `json:"filter"` + MacroPaths LLDMacroPaths `json:"lld_macro_paths,omitempty"` } // Items is an array of Item From b7f427bea22159ace9c978af6b148216c5dd2b35 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Tue, 26 May 2020 16:16:23 +0100 Subject: [PATCH 31/54] application --- item.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/item.go b/item.go index a00570f..e23d3a8 100644 --- a/item.go +++ b/item.go @@ -1,6 +1,7 @@ package zabbix import ( + "encoding/json" "fmt" ) @@ -121,8 +122,8 @@ type Item struct { TrapperHosts string `json:"trapper_hosts,omitempty"` Params string `json:"params,omitempty"` - // Fields below used only when creating applications - ApplicationIds []string `json:"applications,omitempty"` + // list of strings on set, but list of objects on get + Applications json.RawMessage `json:"applications,omitempty"` ItemParent Hosts `json:"hosts"` @@ -361,9 +362,9 @@ func (api *API) ProtoItemsDeleteIDs(ids []string) (itemids []interface{}, err er } result := response.Result.(map[string]interface{}) - itemids1, ok := result["itemids"].([]interface{}) + itemids1, ok := result["prototypeids"].([]interface{}) if !ok { - itemids2 := result["itemids"].(map[string]interface{}) + itemids2 := result["prototypeids"].(map[string]interface{}) for _, id := range itemids2 { itemids = append(itemids, id) } From 42187739dd0f9a44e073638f601673b7baead903 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Tue, 26 May 2020 19:58:33 +0100 Subject: [PATCH 32/54] initial graph --- graph.go | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 graph.go diff --git a/graph.go b/graph.go new file mode 100644 index 0000000..9b9da7c --- /dev/null +++ b/graph.go @@ -0,0 +1,164 @@ +package zabbix + +type ( + GraphType string + GraphAxis string + + GraphItemFunc string + GraphItemDraw string + GraphItemType string + GraphItemSide string +) + +const ( + GraphNormal GraphType = "0" + GraphStacked GraphType = "1" + GraphPie GraphType = "2" + GraphExploded GraphType = "3" + + GraphCalculated GraphAxis = "0" + GraphFixed GraphAxis = "1" + GraphItem GraphAxis = "2" + + GraphItemMin GraphItemFunc = "1" + GraphItemAvg GraphItemFunc = "2" + GraphItemMax GraphItemFunc = "4" + GraphItemAll GraphItemFunc = "7" + GraphItemLast GraphItemFunc = "9" + + GraphItemLine GraphItemDraw = "0" + GraphItemFilled GraphItemDraw = "1" + GraphItemBold GraphItemDraw = "2" + GraphItemDot GraphItemDraw = "3" + GraphItemDashed GraphItemDraw = "4" + GraphItemGradient GraphItemDraw = "5" + + GraphItemSimple GraphItemType = "0" + GraphItemSum GraphItemType = "2" + + GraphItemLeft GraphItemSide = "0" + GraphItemRight GraphItemSide = "1" +) + +type GraphItem struct { + GItemID string `json:"gitemid,omitempty"` + GraphID string `json:"graphid,omitempty"` + Color string `json:"color"` + ItemID string `json:"itemid"` + CalcFunc GraphItemFunc `json:"calc_fnc,omitempty"` + DrawType GraphItemDraw `json:"drawtype,omitempty"` + SortOrder string `json:"sortorder,omitempty"` + Type GraphItemType `json:"type,omitempty"` + YAxisSide GraphItemSide `json:"yaxisside,omitempty"` +} + +type GraphItems []GraphItem + +// Graph represent Zabbix Graph object +// https://www.zabbix.com/documentation/3.2/manual/api/reference/graph/object +type Graph struct { + GraphID string `json:"graphid,omitempty"` + Name string `json:"name"` + Height string `json:"height"` + Width string `json:"width"` + GraphType GraphType `json:"graphtype,omitempty"` + PercentLeft string `json:"percent_left,omitempty"` + PercentRight string `json:"percent_right,omitempty"` + Show3d string `json:"show_3d,omitempty"` + ShowLegend string `json:"show_legend,omitempty"` + ShowWorkPeriod string `json:"show_work_period,omitempty"` + YAxisMax string `json:"yaxismax,omitempty"` + YMaxItemId string `json:"ymax_itemid,omitempty"` + YMaxType string `json:"ymax_type,omitempty"` + YAxisMin string `json:"yaxismin,omitempty"` + YMinItemId string `json:"ymin_itemid,omitempty"` + YMinType string `json:"ymin_type,omitempty"` + + GItems GraphItems `json:"gitems,omitempty"` +} + +// HostGroups is an array of HostGroup +type Graphs []Graph + +// GraphsGet Wrapper for graph.get +// https://www.zabbix.com/documentation/3.2/manual/api/reference/graph/get +func (api *API) GraphsGet(params Params) (res Graphs, err error) { + if _, present := params["output"]; !present { + params["output"] = "extend" + } + err = api.CallWithErrorParse("graphs.get", params, &res) + return +} + +// GraphGetByID Gets host group by Id only if there is exactly 1 matching host group. +func (api *API) GraphGetByID(id string) (res *Graph, err error) { + groups, err := api.HostGroupsGet(Params{"graphids": id}) + if err != nil { + return + } + + if len(groups) == 1 { + res = &groups[0] + } else { + e := ExpectedOneResult(len(groups)) + err = &e + } + return +} + +// GraphsCreate Wrapper for graph.create +// https://www.zabbix.com/documentation/3.2/manual/api/reference/graph/create +func (api *API) GraphsCreate(hostGroups Graphs) (err error) { + response, err := api.CallWithError("graph.create", hostGroups) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + groupids := result["graphids"].([]interface{}) + for i, id := range groupids { + hostGroups[i].GraphID = id.(string) + } + return +} + +// GraphsUpdate Wrapper for graph.update +// https://www.zabbix.com/documentation/3.2/manual/api/reference/graph/update +func (api *API) GraphsUpdate(hostGroups Graphs) (err error) { + _, err = api.CallWithError("graph.update", hostGroups) + return +} + +// HostGroupsDelete Wrapper for hostgroup.delete +// Cleans GroupId in all hostGroups elements if call succeed. +// https://www.zabbix.com/documentation/3.2/manual/api/reference/hostgroup/delete +func (api *API) GraphsDelete(hostGroups Graphs) (err error) { + ids := make([]string, len(hostGroups)) + for i, group := range hostGroups { + ids[i] = group.GraphID + } + + err = api.GraphsDeleteByIds(ids) + if err == nil { + for i := range hostGroups { + hostGroups[i].GraphID = "" + } + } + return +} + +// HostGroupsDeleteByIds Wrapper for hostgroup.delete +// https://www.zabbix.com/documentation/3.2/manual/api/reference/hostgroup/delete +func (api *API) GraphsDeleteByIds(ids []string) (err error) { + response, err := api.CallWithError("graph.delete", ids) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + groupids := result["graphids"].([]interface{}) + if len(ids) != len(groupids) { + err = &ExpectedMore{len(ids), len(groupids)} + } + return +} From f1dd659e15debb517c2f02ce2bbff8e0b85b4a19 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Tue, 26 May 2020 20:02:23 +0100 Subject: [PATCH 33/54] adding prototype --- graph.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/graph.go b/graph.go index 9b9da7c..02bd742 100644 --- a/graph.go +++ b/graph.go @@ -86,13 +86,34 @@ func (api *API) GraphsGet(params Params) (res Graphs, err error) { if _, present := params["output"]; !present { params["output"] = "extend" } - err = api.CallWithErrorParse("graphs.get", params, &res) + err = api.CallWithErrorParse("graph.get", params, &res) + return +} +func (api *API) GraphProtosGet(params Params) (res Graphs, err error) { + if _, present := params["output"]; !present { + params["output"] = "extend" + } + err = api.CallWithErrorParse("graphprototype.get", params, &res) return } // GraphGetByID Gets host group by Id only if there is exactly 1 matching host group. func (api *API) GraphGetByID(id string) (res *Graph, err error) { - groups, err := api.HostGroupsGet(Params{"graphids": id}) + groups, err := api.GraphsGet(Params{"graphids": id}) + if err != nil { + return + } + + if len(groups) == 1 { + res = &groups[0] + } else { + e := ExpectedOneResult(len(groups)) + err = &e + } + return +} +func (api *API) GraphProtoGetByID(id string) (res *Graph, err error) { + groups, err := api.GraphProtosGet(Params{"graphids": id}) if err != nil { return } @@ -121,6 +142,19 @@ func (api *API) GraphsCreate(hostGroups Graphs) (err error) { } return } +func (api *API) GraphProtossCreate(hostGroups Graphs) (err error) { + response, err := api.CallWithError("graphprototype.create", hostGroups) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + groupids := result["graphids"].([]interface{}) + for i, id := range groupids { + hostGroups[i].GraphID = id.(string) + } + return +} // GraphsUpdate Wrapper for graph.update // https://www.zabbix.com/documentation/3.2/manual/api/reference/graph/update @@ -128,6 +162,10 @@ func (api *API) GraphsUpdate(hostGroups Graphs) (err error) { _, err = api.CallWithError("graph.update", hostGroups) return } +func (api *API) GraphProtosUpdate(hostGroups Graphs) (err error) { + _, err = api.CallWithError("graphprototype.update", hostGroups) + return +} // HostGroupsDelete Wrapper for hostgroup.delete // Cleans GroupId in all hostGroups elements if call succeed. @@ -146,6 +184,20 @@ func (api *API) GraphsDelete(hostGroups Graphs) (err error) { } return } +func (api *API) GraphProtossDelete(hostGroups Graphs) (err error) { + ids := make([]string, len(hostGroups)) + for i, group := range hostGroups { + ids[i] = group.GraphID + } + + err = api.GraphProtosDeleteByIds(ids) + if err == nil { + for i := range hostGroups { + hostGroups[i].GraphID = "" + } + } + return +} // HostGroupsDeleteByIds Wrapper for hostgroup.delete // https://www.zabbix.com/documentation/3.2/manual/api/reference/hostgroup/delete @@ -162,3 +214,16 @@ func (api *API) GraphsDeleteByIds(ids []string) (err error) { } return } +func (api *API) GraphProtosDeleteByIds(ids []string) (err error) { + response, err := api.CallWithError("graphprototype.delete", ids) + if err != nil { + return + } + + result := response.Result.(map[string]interface{}) + groupids := result["graphids"].([]interface{}) + if len(ids) != len(groupids) { + err = &ExpectedMore{len(ids), len(groupids)} + } + return +} From cd7892fda685636a7cbc2a4093629d3743b65072 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Tue, 26 May 2020 21:34:53 +0100 Subject: [PATCH 34/54] graph --- graph.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/graph.go b/graph.go index 02bd742..ec2f16e 100644 --- a/graph.go +++ b/graph.go @@ -16,9 +16,9 @@ const ( GraphPie GraphType = "2" GraphExploded GraphType = "3" - GraphCalculated GraphAxis = "0" - GraphFixed GraphAxis = "1" - GraphItem GraphAxis = "2" + GraphAxisCalculated GraphAxis = "0" + GraphAxisFixed GraphAxis = "1" + GraphAxisItem GraphAxis = "2" GraphItemMin GraphItemFunc = "1" GraphItemAvg GraphItemFunc = "2" @@ -61,20 +61,20 @@ type Graph struct { Name string `json:"name"` Height string `json:"height"` Width string `json:"width"` - GraphType GraphType `json:"graphtype,omitempty"` + Type GraphType `json:"graphtype,omitempty"` PercentLeft string `json:"percent_left,omitempty"` PercentRight string `json:"percent_right,omitempty"` Show3d string `json:"show_3d,omitempty"` ShowLegend string `json:"show_legend,omitempty"` ShowWorkPeriod string `json:"show_work_period,omitempty"` - YAxisMax string `json:"yaxismax,omitempty"` + YMax string `json:"yaxismax,omitempty"` YMaxItemId string `json:"ymax_itemid,omitempty"` - YMaxType string `json:"ymax_type,omitempty"` - YAxisMin string `json:"yaxismin,omitempty"` + YMaxType GraphAxis `json:"ymax_type,omitempty"` + YMin string `json:"yaxismin,omitempty"` YMinItemId string `json:"ymin_itemid,omitempty"` - YMinType string `json:"ymin_type,omitempty"` + YMinType GraphAxis `json:"ymin_type,omitempty"` - GItems GraphItems `json:"gitems,omitempty"` + GraphItems GraphItems `json:"gitems,omitempty"` } // HostGroups is an array of HostGroup @@ -142,7 +142,7 @@ func (api *API) GraphsCreate(hostGroups Graphs) (err error) { } return } -func (api *API) GraphProtossCreate(hostGroups Graphs) (err error) { +func (api *API) GraphProtosCreate(hostGroups Graphs) (err error) { response, err := api.CallWithError("graphprototype.create", hostGroups) if err != nil { return @@ -184,7 +184,7 @@ func (api *API) GraphsDelete(hostGroups Graphs) (err error) { } return } -func (api *API) GraphProtossDelete(hostGroups Graphs) (err error) { +func (api *API) GraphProtosDelete(hostGroups Graphs) (err error) { ids := make([]string, len(hostGroups)) for i, group := range hostGroups { ids[i] = group.GraphID From ede6c617dde2c6bc4b81954d14f39aa73c2ae247 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Fri, 29 May 2020 10:06:18 +0100 Subject: [PATCH 35/54] 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 36/54] 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 37/54] 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. From c4a6fad2e92a02581c710aace570ec7a85985ca3 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 30 May 2020 21:15:35 +0100 Subject: [PATCH 38/54] auth fields --- item.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/item.go b/item.go index 0bd8f49..5117f6c 100644 --- a/item.go +++ b/item.go @@ -139,6 +139,9 @@ type Item struct { Timeout string `json:"timeout,omitempty"` VerifyHost string `json:"verify_host,omitempty"` VerifyPeer string `json:"verify_peer,omitempty"` + AuthType string `json:"authtype,omitempty"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` // SNMP Fields SNMPOid string `json:"snmp_oid,omitempty"` From 267ba9cc2cf114b8b742494f19682d04fbdd87d3 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 31 May 2020 13:29:24 +0100 Subject: [PATCH 39/54] headers support --- item.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++--------- lld.go | 62 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 111 insertions(+), 20 deletions(-) diff --git a/item.go b/item.go index 5117f6c..bce6156 100644 --- a/item.go +++ b/item.go @@ -103,6 +103,8 @@ const ( Delta DeltaType = 2 ) +type HttpHeaders map[string]string + // Item represent Zabbix item object // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/object type Item struct { @@ -131,17 +133,19 @@ type Item struct { Preprocessors Preprocessors `json:"preprocessing,omitempty"` // HTTP Agent Fields - Url string `json:"url,omitempty"` - RequestMethod string `json:"request_method,omitempty"` - PostType string `json:"post_type,omitempty"` - Posts string `json:"posts,omitempty"` - StatusCodes string `json:"status_codes,omitempty"` - Timeout string `json:"timeout,omitempty"` - VerifyHost string `json:"verify_host,omitempty"` - VerifyPeer string `json:"verify_peer,omitempty"` - AuthType string `json:"authtype,omitempty"` - Username string `json:"username,omitempty"` - Password string `json:"password,omitempty"` + Url string `json:"url,omitempty"` + RequestMethod string `json:"request_method,omitempty"` + PostType string `json:"post_type,omitempty"` + Posts string `json:"posts,omitempty"` + StatusCodes string `json:"status_codes,omitempty"` + Timeout string `json:"timeout,omitempty"` + VerifyHost string `json:"verify_host,omitempty"` + VerifyPeer string `json:"verify_peer,omitempty"` + AuthType string `json:"authtype,omitempty"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` + Headers HttpHeaders `json:"-"` + RawHeaders json.RawMessage `json:"headers,omitempty"` // SNMP Fields SNMPOid string `json:"snmp_oid,omitempty"` @@ -194,6 +198,7 @@ func (api *API) ItemsGet(params Params) (res Items, err error) { params["output"] = "extend" } err = api.CallWithErrorParse("item.get", params, &res) + api.itemsHeadersUnmarshal(res) return } func (api *API) ProtoItemsGet(params Params) (res Items, err error) { @@ -201,9 +206,47 @@ func (api *API) ProtoItemsGet(params Params) (res Items, err error) { params["output"] = "extend" } err = api.CallWithErrorParse("itemprototype.get", params, &res) + api.itemsHeadersUnmarshal(res) return } +func (api *API) itemsHeadersUnmarshal(item Items) { + for i := 0; i < len(item); i++ { + h := item[i] + + item[i].Headers = HttpHeaders{} + + if len(h.RawHeaders) == 0 { + continue + } + + asStr := string(h.RawHeaders) + if asStr == "[]" { + continue + } + + out := HttpHeaders{} + err := json.Unmarshal(h.RawHeaders, &out) + if err != nil { + api.printf("got error during unmarshal %s", err) + panic(err) + } + item[i].Headers = out + } +} + +func prepItems(item Items) { + for i := 0; i < len(item); i++ { + h := item[i] + + if h.Headers == nil { + continue + } + asB, _ := json.Marshal(h.Headers) + item[i].RawHeaders = json.RawMessage(asB) + } +} + // ItemGetByID Gets item by Id only if there is exactly 1 matching host. func (api *API) ItemGetByID(id string) (res *Item, err error) { items, err := api.ItemsGet(Params{"itemids": id}) @@ -245,6 +288,7 @@ func (api *API) ProtoItemsGetByApplicationID(id string) (res Items, err error) { // ItemsCreate Wrapper for item.create // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/create func (api *API) ItemsCreate(items Items) (err error) { + prepItems(items) response, err := api.CallWithError("item.create", items) if err != nil { return @@ -258,6 +302,7 @@ func (api *API) ItemsCreate(items Items) (err error) { return } func (api *API) ProtoItemsCreate(items Items) (err error) { + prepItems(items) response, err := api.CallWithError("itemprototype.create", items) if err != nil { return @@ -274,10 +319,12 @@ func (api *API) ProtoItemsCreate(items Items) (err error) { // ItemsUpdate Wrapper for item.update // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/update func (api *API) ItemsUpdate(items Items) (err error) { + prepItems(items) _, err = api.CallWithError("item.update", items) return } func (api *API) ProtoItemsUpdate(items Items) (err error) { + prepItems(items) _, err = api.CallWithError("itemprototype.update", items) return } diff --git a/lld.go b/lld.go index d49d8d5..27fa64d 100644 --- a/lld.go +++ b/lld.go @@ -1,5 +1,7 @@ package zabbix +import "encoding/json" + type ( LLDEvalType string LLDOperatorType string @@ -69,15 +71,17 @@ type LLDRule struct { Port string `json:"port,omitempty"` // HTTP Agent Fields - Url string `json:"url,omitempty"` - RequestMethod string `json:"request_method,omitempty"` - AllowTraps string `json:"allow_traps,omitempty"` - PostType string `json:"post_type,omitempty"` - Posts string `json:"posts,omitempty"` - StatusCodes string `json:"status_codes,omitempty"` - Timeout string `json:"timeout,omitempty"` - VerifyHost string `json:"verify_host,omitempty"` - VerifyPeer string `json:"verify_peer,omitempty"` + Url string `json:"url,omitempty"` + RequestMethod string `json:"request_method,omitempty"` + AllowTraps string `json:"allow_traps,omitempty"` + PostType string `json:"post_type,omitempty"` + Posts string `json:"posts,omitempty"` + StatusCodes string `json:"status_codes,omitempty"` + Timeout string `json:"timeout,omitempty"` + VerifyHost string `json:"verify_host,omitempty"` + VerifyPeer string `json:"verify_peer,omitempty"` + Headers HttpHeaders `json:"-"` + RawHeaders json.RawMessage `json:"headers,omitempty"` // SNMP Fields SNMPOid string `json:"snmp_oid,omitempty"` @@ -98,6 +102,43 @@ type LLDRule struct { // Items is an array of Item type LLDRules []LLDRule +func (api *API) lldsHeadersUnmarshal(item LLDRules) { + for i := 0; i < len(item); i++ { + h := item[i] + + item[i].Headers = HttpHeaders{} + + if len(h.RawHeaders) == 0 { + continue + } + + asStr := string(h.RawHeaders) + if asStr == "[]" { + continue + } + + out := HttpHeaders{} + err := json.Unmarshal(h.RawHeaders, &out) + if err != nil { + api.printf("got error during unmarshal %s", err) + panic(err) + } + item[i].Headers = out + } +} + +func prepLLDs(item LLDRules) { + for i := 0; i < len(item); i++ { + h := item[i] + + if h.Headers == nil { + continue + } + asB, _ := json.Marshal(h.Headers) + item[i].RawHeaders = json.RawMessage(asB) + } +} + // ItemsGet Wrapper for item.get // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/get func (api *API) LLDsGet(params Params) (res LLDRules, err error) { @@ -105,6 +146,7 @@ func (api *API) LLDsGet(params Params) (res LLDRules, err error) { params["output"] = "extend" } err = api.CallWithErrorParse("discoveryrule.get", params, &res) + api.lldsHeadersUnmarshal(res) return } @@ -127,6 +169,7 @@ func (api *API) LLDGetByID(id string) (res *LLDRule, err error) { // ItemsCreate Wrapper for item.create // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/create func (api *API) LLDsCreate(items LLDRules) (err error) { + prepLLDs(items) response, err := api.CallWithError("discoveryrule.create", items) if err != nil { return @@ -143,6 +186,7 @@ func (api *API) LLDsCreate(items LLDRules) (err error) { // ItemsUpdate Wrapper for item.update // https://www.zabbix.com/documentation/3.2/manual/api/reference/item/update func (api *API) LLDsUpdate(items LLDRules) (err error) { + prepLLDs(items) _, err = api.CallWithError("discoveryrule.update", items) return } From 3d3c296c8fcfc9f8c0e9238b89c6d57047b25080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herout?= Date: Mon, 30 Nov 2020 15:37:40 +0100 Subject: [PATCH 40/54] fixed tests --- base_test.go | 14 ++++++-------- go.mod | 5 ----- go.sum | 4 ---- host_test.go | 3 ++- item.go | 3 --- item_test.go | 9 ++++----- template_test.go | 9 ++++++++- trigger.go | 8 ++++---- 8 files changed, 24 insertions(+), 31 deletions(-) diff --git a/base_test.go b/base_test.go index e7cbf5c..3aae2e5 100644 --- a/base_test.go +++ b/base_test.go @@ -42,7 +42,12 @@ func getAPI(t *testing.T) *zapi.API { } url, user, password := os.Getenv("TEST_ZABBIX_URL"), os.Getenv("TEST_ZABBIX_USER"), os.Getenv("TEST_ZABBIX_PASSWORD") - _api = zapi.NewAPI(url) + + // Zabbix client connection configuration + var c zapi.Config + c.Url = url + + _api = zapi.NewAPI(c) _api.SetClient(http.DefaultClient) v := os.Getenv("TEST_ZABBIX_VERBOSE") if v != "" && v != "0" { @@ -84,10 +89,3 @@ func TestVersion(t *testing.T) { t.Errorf("Unexpected version: %s", v) } } - -func ExampleAPI_Call() { - api := zapi.NewAPI("http://host/api_jsonrpc.php") - api.Login("user", "password") - res, _ := api.Call("item.get", zapi.Params{"itemids": "23970", "output": "extend"}) - log.Print(res) -} diff --git a/go.mod b/go.mod index e9b4a62..f63ac78 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,3 @@ module github.com/tpretz/go-zabbix-api go 1.12 - -require ( - github.com/AlekSi/reflector v0.4.1 // indirect - github.com/tpretz/go-zabbix-api v0.3.1 -) diff --git a/go.sum b/go.sum index 3518cc6..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +0,0 @@ -github.com/AlekSi/reflector v0.4.1 h1:onP7kPCAV/EXgHwKz1PyU9K/udpVJ8dVcOMm4brKrJ4= -github.com/AlekSi/reflector v0.4.1/go.mod h1:K9Yt9Jbte9Xw4QZ3e/ovaWduyRSlKVmwnze4z3+bVxs= -github.com/tpretz/go-zabbix-api v0.3.1 h1:152d0lY3OlmnYVV7QSurfr/8ALOoF+Rdfc15/mOERQk= -github.com/tpretz/go-zabbix-api v0.3.1/go.mod h1:/Y2aKeFo5342GTp/glX2eU0xU4z0Uha4T2kOaQa6ggM= diff --git a/host_test.go b/host_test.go index b8ffe3e..7ef6670 100644 --- a/host_test.go +++ b/host_test.go @@ -11,7 +11,7 @@ import ( func CreateHost(group *zapi.HostGroup, t *testing.T) *zapi.Host { name := fmt.Sprintf("%s-%d", getHost(), rand.Int()) - iface := zapi.HostInterface{DNS: name, Port: "42", Type: zapi.Agent, UseIP: 0, Main: 1} + iface := zapi.HostInterface{DNS: name, Port: "42", Type: zapi.Agent, UseIP: "0", Main: "1"} hosts := zapi.Hosts{{ Host: name, Name: "Name for " + name, @@ -53,6 +53,7 @@ func TestHosts(t *testing.T) { } host.GroupIds = nil host.Interfaces = nil + host.ProxyID = "0" newName := fmt.Sprintf("%s-%d", getHost(), rand.Int()) host.Host = newName diff --git a/item.go b/item.go index bce6156..7b66611 100644 --- a/item.go +++ b/item.go @@ -125,9 +125,6 @@ type Item struct { TrapperHosts string `json:"trapper_hosts,omitempty"` Params string `json:"params,omitempty"` - // list of strings on set, but list of objects on get - Applications json.RawMessage `json:"applications,omitempty"` - ItemParent Hosts `json:"hosts"` Preprocessors Preprocessors `json:"preprocessing,omitempty"` diff --git a/item_test.go b/item_test.go index 3933c61..1041cbc 100644 --- a/item_test.go +++ b/item_test.go @@ -8,11 +8,10 @@ import ( func CreateItem(app *zapi.Application, t *testing.T) *zapi.Item { items := zapi.Items{{ - HostID: app.HostID, - Key: "key.lala.laa", - Name: "name for key", - Type: zapi.ZabbixTrapper, - ApplicationIds: []string{app.ApplicationID}, + HostID: app.HostID, + Key: "key.lala.laa", + Name: "name for key", + Type: zapi.ZabbixTrapper, }} err := getAPI(t).ItemsCreate(items) if err != nil { diff --git a/template_test.go b/template_test.go index d3a0ec8..06ad46f 100644 --- a/template_test.go +++ b/template_test.go @@ -7,9 +7,16 @@ import ( ) func CreateTemplate(hostGroup *zapi.HostGroup, t *testing.T) *zapi.Template { + + group := zapi.HostGroupID{ + GroupID: hostGroup.GroupID, + } + + groups := []zapi.HostGroupID{group} + template := zapi.Templates{zapi.Template{ Host: "template name", - Groups: zapi.HostGroups{*hostGroup}, + Groups: groups, }} err := getAPI(t).TemplatesCreate(template) if err != nil { diff --git a/trigger.go b/trigger.go index 986346d..1633d74 100644 --- a/trigger.go +++ b/trigger.go @@ -75,13 +75,13 @@ type Trigger struct { //Value ValueType `json:""` Opdata string `json:"opdata,omitempty"` - Type string `json:"type"` + Type int `json:"type"` Url string `json:"url,omitempty"` - RecoveryMode string `json:"recovery_mode"` + RecoveryMode int `json:"recovery_mode"` RecoveryExpression string `json:"recovery_expression,omitempty"` - CorrelationMode string `json:"correlation_mode"` + CorrelationMode int `json:"correlation_mode"` CorrelationTag string `json:"correlation_tag,omitempty"` - ManualClose string `json:"manual_close"` + ManualClose int `json:"manual_close"` Priority SeverityType `json:"priority,string"` Status StatusType `json:"status,string"` From 09c7f100ec1dcb58d83c2083789183706e364dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herout?= Date: Tue, 1 Dec 2020 09:08:45 +0100 Subject: [PATCH 41/54] inventory proof of concept --- host.go | 1 + inventory.go | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 inventory.go diff --git a/host.go b/host.go index 226d823..e22fa78 100644 --- a/host.go +++ b/host.go @@ -38,6 +38,7 @@ type Host struct { Name string `json:"name"` Status StatusType `json:"status,string"` UserMacros Macros `json:"macros,omitempty"` + Inventory Inventory `json:"inventory,omitempty"` // Fields below used only when creating hosts GroupIds HostGroupIDs `json:"groups,omitempty"` diff --git a/inventory.go b/inventory.go new file mode 100644 index 0000000..c4a9152 --- /dev/null +++ b/inventory.go @@ -0,0 +1,9 @@ +package zabbix + +// https://www.zabbix.com/documentation/5.0/manual/api/reference/host/object#host_inventory +type Inventory struct { + Location string `json:"location,omitempty"` + Model string `json:"model,omitempty"` + Name string `json:"name,omitempty"` + Notes string `json:"notes,omitempty"` +} From 73da6b64ba11223dbfa58cf20633c5889a475b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herout?= Date: Tue, 1 Dec 2020 09:20:02 +0100 Subject: [PATCH 42/54] module name --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index f63ac78..4d05dce 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/tpretz/go-zabbix-api +module github.com/tomasherout/go-zabbix-api go 1.12 From 08cea48afb26fae53fbe5ddec331c1391a107074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herout?= Date: Tue, 1 Dec 2020 10:16:14 +0100 Subject: [PATCH 43/54] invetory as json.RawMessage --- host.go | 4 +++- inventory.go | 9 --------- 2 files changed, 3 insertions(+), 10 deletions(-) delete mode 100644 inventory.go diff --git a/host.go b/host.go index e22fa78..2cfa812 100644 --- a/host.go +++ b/host.go @@ -38,7 +38,9 @@ type Host struct { Name string `json:"name"` Status StatusType `json:"status,string"` UserMacros Macros `json:"macros,omitempty"` - Inventory Inventory `json:"inventory,omitempty"` + // for empty inventory Zabbix returns empty array + // otherwise returns object + Inventory json.RawMessage `json:"inventory,omitempty"` // Fields below used only when creating hosts GroupIds HostGroupIDs `json:"groups,omitempty"` diff --git a/inventory.go b/inventory.go deleted file mode 100644 index c4a9152..0000000 --- a/inventory.go +++ /dev/null @@ -1,9 +0,0 @@ -package zabbix - -// https://www.zabbix.com/documentation/5.0/manual/api/reference/host/object#host_inventory -type Inventory struct { - Location string `json:"location,omitempty"` - Model string `json:"model,omitempty"` - Name string `json:"name,omitempty"` - Notes string `json:"notes,omitempty"` -} From 8ef9acbff6ed07fc313247a19399532e42f45c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herout?= Date: Tue, 1 Dec 2020 13:00:37 +0100 Subject: [PATCH 44/54] inventory struct --- inventory.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 inventory.go diff --git a/inventory.go b/inventory.go new file mode 100644 index 0000000..c4a9152 --- /dev/null +++ b/inventory.go @@ -0,0 +1,9 @@ +package zabbix + +// https://www.zabbix.com/documentation/5.0/manual/api/reference/host/object#host_inventory +type Inventory struct { + Location string `json:"location,omitempty"` + Model string `json:"model,omitempty"` + Name string `json:"name,omitempty"` + Notes string `json:"notes,omitempty"` +} From 7fe0cc81bf2be727169b798d5a6418f6a170f26d Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 5 Dec 2020 14:41:48 +0000 Subject: [PATCH 45/54] what about this --- host.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/host.go b/host.go index e22fa78..eafcdcc 100644 --- a/host.go +++ b/host.go @@ -38,7 +38,9 @@ type Host struct { Name string `json:"name"` Status StatusType `json:"status,string"` UserMacros Macros `json:"macros,omitempty"` - Inventory Inventory `json:"inventory,omitempty"` + + RawInventory json.RawMessage `json:"inventory,omitempty"` + Inventory *Inventory `json:"-"` // Fields below used only when creating hosts GroupIds HostGroupIDs `json:"groups,omitempty"` @@ -61,7 +63,7 @@ func (api *API) HostsGet(params Params) (res Hosts, err error) { } err = api.CallWithErrorParse("host.get", params, &res) - // fix up host details if present + // fix up host interface details if present for i := 0; i < len(res); i++ { h := res[i] for j := 0; j < len(h.Interfaces); j++ { @@ -86,7 +88,26 @@ func (api *API) HostsGet(params Params) (res Hosts, err error) { res[i].Interfaces[j].Details = &out } + // fix up host inventory if present + if len(h.RawInventory) == 0 { + continue + } + + // if its an empty array + asStr := string(h.RawInventory) + if asStr == "[]" { + continue + } + + // lets unbox + var inv Inventory + if err := json.Unmarshal(h.RawInventory, &inv); err != nil { + api.printf("got error during unmarshal %s", err) + panic(err) + } + h.Inventory = &inv } + return } @@ -150,7 +171,10 @@ func prepHosts(hosts Hosts) { asB, _ := json.Marshal(in.Details) hosts[i].Interfaces[j].RawDetails = json.RawMessage(asB) } - + if h.Inventory != nil { + asB, _ := json.Marshal(h.Inventory) + hosts[i].RawInventory = json.RawMessage(asB) + } } } From 7a4edcff4e6d1c5a5931fa83eb07988dbd23d579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herout?= Date: Tue, 8 Dec 2020 06:46:54 +0100 Subject: [PATCH 46/54] rename module back to original name; fix in HostsGet func --- go.mod | 4 +++- go.sum | 4 ++++ host.go | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 4d05dce..d2d5db9 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ -module github.com/tomasherout/go-zabbix-api +module github.com/tpretz/go-zabbix-api go 1.12 + +require github.com/AlekSi/reflector v0.4.1 // indirect diff --git a/go.sum b/go.sum index e69de29..ff2e10c 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/AlekSi/reflector v0.4.1/go.mod h1:K9Yt9Jbte9Xw4QZ3e/ovaWduyRSlKVmwnze4z3+bVxs= +github.com/tpretz/go-zabbix-api v0.3.1/go.mod h1:vCzbm8q1QYUHmeWisZSfJzt5MfPj+JeRoNW5o4sG72o= +github.com/tpretz/go-zabbix-api v0.8.0 h1:5xlz9QCcwApIvrPru/WzTUkPltlo+x8tBpbExW9mrjU= +github.com/tpretz/go-zabbix-api v0.8.0/go.mod h1:SFemU2FNM7aeT3/NinjDw0SQEZGVOoU5d+CqXKTetbw= diff --git a/host.go b/host.go index eafcdcc..b73e6df 100644 --- a/host.go +++ b/host.go @@ -63,7 +63,7 @@ func (api *API) HostsGet(params Params) (res Hosts, err error) { } err = api.CallWithErrorParse("host.get", params, &res) - // fix up host interface details if present + // fix up host details if present for i := 0; i < len(res); i++ { h := res[i] for j := 0; j < len(h.Interfaces); j++ { @@ -105,7 +105,7 @@ func (api *API) HostsGet(params Params) (res Hosts, err error) { api.printf("got error during unmarshal %s", err) panic(err) } - h.Inventory = &inv + res[i].Inventory = &inv } return From 3d580a1503ba75e34049e2122d753f75686c6027 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Wed, 30 Dec 2020 11:02:51 +0000 Subject: [PATCH 47/54] Inventory Changes --- host.go | 17 +++++++++++++---- inventory.go | 7 +------ item.go | 26 ++++++++++++++++++++++++++ trigger.go | 8 ++++---- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/host.go b/host.go index b73e6df..39ee626 100644 --- a/host.go +++ b/host.go @@ -10,6 +10,8 @@ type ( // StatusType Status and function of the host. // see "status" in: https://www.zabbix.com/documentation/3.2/manual/api/reference/host/object StatusType int + + InventoryMode int ) const ( @@ -21,6 +23,12 @@ const ( Unavailable AvailableType = 2 ) +const ( + InventoryDisabled InventoryMode = -1 + InventoryManual InventoryMode = 0 + InventoryAutomatic InventoryMode = 1 +) + const ( // Monitored monitored host(default) Monitored StatusType = 0 @@ -39,8 +47,9 @@ type Host struct { Status StatusType `json:"status,string"` UserMacros Macros `json:"macros,omitempty"` - RawInventory json.RawMessage `json:"inventory,omitempty"` - Inventory *Inventory `json:"-"` + RawInventory json.RawMessage `json:"inventory,omitempty"` + Inventory Inventory `json:"-"` + InventoryMode InventoryMode `json:"inventory_mode,string"` // Fields below used only when creating hosts GroupIds HostGroupIDs `json:"groups,omitempty"` @@ -95,7 +104,7 @@ func (api *API) HostsGet(params Params) (res Hosts, err error) { // if its an empty array asStr := string(h.RawInventory) - if asStr == "[]" { + if asStr == "[]" || asStr == "{}" { continue } @@ -105,7 +114,7 @@ func (api *API) HostsGet(params Params) (res Hosts, err error) { api.printf("got error during unmarshal %s", err) panic(err) } - res[i].Inventory = &inv + res[i].Inventory = inv } return diff --git a/inventory.go b/inventory.go index c4a9152..0fa8f6f 100644 --- a/inventory.go +++ b/inventory.go @@ -1,9 +1,4 @@ package zabbix // https://www.zabbix.com/documentation/5.0/manual/api/reference/host/object#host_inventory -type Inventory struct { - Location string `json:"location,omitempty"` - Model string `json:"model,omitempty"` - Name string `json:"name,omitempty"` - Notes string `json:"notes,omitempty"` -} +type Inventory map[string]string diff --git a/item.go b/item.go index 7b66611..3b733de 100644 --- a/item.go +++ b/item.go @@ -125,6 +125,10 @@ type Item struct { TrapperHosts string `json:"trapper_hosts,omitempty"` Params string `json:"params,omitempty"` + // list of strings on set, but list of objects on get + RawApplications json.RawMessage `json:"applications,omitempty"` + Applications []string `json:"-"` + ItemParent Hosts `json:"hosts"` Preprocessors Preprocessors `json:"preprocessing,omitempty"` @@ -211,6 +215,22 @@ func (api *API) itemsHeadersUnmarshal(item Items) { for i := 0; i < len(item); i++ { h := item[i] + if len(h.RawApplications) != 0 { + asStr := string(h.RawApplications) + if asStr != "[]" { + var applications Applications + err := json.Unmarshal(h.RawApplications, &applications) + if err != nil { + panic(err) + } + ids := []string{} + for _, a := range applications { + ids = append(ids, a.ApplicationID) + } + item[i].Applications = ids + } + } + item[i].Headers = HttpHeaders{} if len(h.RawHeaders) == 0 { @@ -236,6 +256,12 @@ func prepItems(item Items) { for i := 0; i < len(item); i++ { h := item[i] + if h.Applications != nil { + text, _ := json.Marshal(h.Applications) + raw := json.RawMessage(text) + h.RawApplications = raw + } + if h.Headers == nil { continue } diff --git a/trigger.go b/trigger.go index 1633d74..3601aac 100644 --- a/trigger.go +++ b/trigger.go @@ -75,13 +75,13 @@ type Trigger struct { //Value ValueType `json:""` Opdata string `json:"opdata,omitempty"` - Type int `json:"type"` + Type int `json:"type,string"` Url string `json:"url,omitempty"` - RecoveryMode int `json:"recovery_mode"` + RecoveryMode int `json:"recovery_mode,string"` RecoveryExpression string `json:"recovery_expression,omitempty"` - CorrelationMode int `json:"correlation_mode"` + CorrelationMode int `json:"correlation_mode,string"` CorrelationTag string `json:"correlation_tag,omitempty"` - ManualClose int `json:"manual_close"` + ManualClose int `json:"manual_close,string"` Priority SeverityType `json:"priority,string"` Status StatusType `json:"status,string"` From ae56f0041c040f7079caa9294beac6f6d1612536 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sat, 14 Aug 2021 19:00:05 +0100 Subject: [PATCH 48/54] inventory mode omitted when default on 5.4, abstract --- host.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/host.go b/host.go index 39ee626..cfc852b 100644 --- a/host.go +++ b/host.go @@ -49,7 +49,9 @@ type Host struct { RawInventory json.RawMessage `json:"inventory,omitempty"` Inventory Inventory `json:"-"` - InventoryMode InventoryMode `json:"inventory_mode,string"` + + RawInventoryMode *InventoryMode `json:"inventory_mode,string,omitempty"` + InventoryMode InventoryMode `json:"-"` // Fields below used only when creating hosts GroupIds HostGroupIDs `json:"groups,omitempty"` @@ -97,24 +99,30 @@ func (api *API) HostsGet(params Params) (res Hosts, err error) { res[i].Interfaces[j].Details = &out } - // fix up host inventory if present - if len(h.RawInventory) == 0 { - continue + // omitted = disabled + if h.RawInventoryMode == nil { + res[i].InventoryMode = InventoryDisabled + } else { + res[i].InventoryMode = *h.RawInventoryMode } - // if its an empty array - asStr := string(h.RawInventory) - if asStr == "[]" || asStr == "{}" { - continue - } + // fix up host inventory if present + if len(h.RawInventory) != 0 { + // if its an empty array + asStr := string(h.RawInventory) + if asStr == "[]" || asStr == "{}" { + continue + } - // lets unbox - var inv Inventory - if err := json.Unmarshal(h.RawInventory, &inv); err != nil { - api.printf("got error during unmarshal %s", err) - panic(err) + // lets unbox + var inv Inventory + if err := json.Unmarshal(h.RawInventory, &inv); err != nil { + api.printf("got error during unmarshal %s", err) + panic(err) + } + res[i].Inventory = inv } - res[i].Inventory = inv + } return @@ -184,6 +192,8 @@ func prepHosts(hosts Hosts) { asB, _ := json.Marshal(h.Inventory) hosts[i].RawInventory = json.RawMessage(asB) } + invMode := h.InventoryMode + h.RawInventoryMode = &invMode } } From 2a42ec0298f0d3d6c96e72beca2c301a827a5e67 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 15 Aug 2021 13:59:23 +0100 Subject: [PATCH 49/54] adding tags to host --- host.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/host.go b/host.go index cfc852b..90f4209 100644 --- a/host.go +++ b/host.go @@ -47,11 +47,11 @@ type Host struct { Status StatusType `json:"status,string"` UserMacros Macros `json:"macros,omitempty"` - RawInventory json.RawMessage `json:"inventory,omitempty"` - Inventory Inventory `json:"-"` + RawInventory json.RawMessage `json:"inventory,omitempty"` + Inventory Inventory `json:"-"` - RawInventoryMode *InventoryMode `json:"inventory_mode,string,omitempty"` - InventoryMode InventoryMode `json:"-"` + RawInventoryMode *InventoryMode `json:"inventory_mode,string,omitempty"` + InventoryMode InventoryMode `json:"-"` // Fields below used only when creating hosts GroupIds HostGroupIDs `json:"groups,omitempty"` @@ -61,6 +61,7 @@ type Host struct { // templates are read back from this one ParentTemplateIDs TemplateIDs `json:"parentTemplates,omitempty"` ProxyID string `json:"proxy_hostid,omitempty"` + Tags Tags `json:"tags,omitempty"` } // Hosts is an array of Host From ab23bac98ec90a83627a4f14ccc928ad6e628302 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Sun, 15 Aug 2021 14:15:04 +0100 Subject: [PATCH 50/54] adding tags to item --- item.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/item.go b/item.go index 3b733de..8ab6f7e 100644 --- a/item.go +++ b/item.go @@ -165,6 +165,8 @@ type Item struct { // Prototype RuleID string `json:"ruleid,omitempty"` DiscoveryRule *LLDRule `json:"discoveryRule,omitEmpty"` + + Tags Tags `json:"tags,omitempty"` } type Preprocessors []Preprocessor From be2af5393dd5b0430abe77534e861e2f87aa6ff0 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Fri, 8 Oct 2021 11:54:46 +0100 Subject: [PATCH 51/54] adding http proxy --- item.go | 1 + lld.go | 1 + 2 files changed, 2 insertions(+) diff --git a/item.go b/item.go index 8ab6f7e..771cd1f 100644 --- a/item.go +++ b/item.go @@ -147,6 +147,7 @@ type Item struct { Password string `json:"password,omitempty"` Headers HttpHeaders `json:"-"` RawHeaders json.RawMessage `json:"headers,omitempty"` + Proxy string `json:"http_proxy,omitempty"` // SNMP Fields SNMPOid string `json:"snmp_oid,omitempty"` diff --git a/lld.go b/lld.go index 27fa64d..d60cdd7 100644 --- a/lld.go +++ b/lld.go @@ -82,6 +82,7 @@ type LLDRule struct { VerifyPeer string `json:"verify_peer,omitempty"` Headers HttpHeaders `json:"-"` RawHeaders json.RawMessage `json:"headers,omitempty"` + Proxy string `json:"http_proxy,omitempty"` // SNMP Fields SNMPOid string `json:"snmp_oid,omitempty"` From 46aefc8f573809299671f297600f9a62cbb7a8f9 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Fri, 8 Oct 2021 12:10:34 +0100 Subject: [PATCH 52/54] adding retrievemode --- item.go | 1 + lld.go | 1 + 2 files changed, 2 insertions(+) diff --git a/item.go b/item.go index 771cd1f..dff32e4 100644 --- a/item.go +++ b/item.go @@ -137,6 +137,7 @@ type Item struct { Url string `json:"url,omitempty"` RequestMethod string `json:"request_method,omitempty"` PostType string `json:"post_type,omitempty"` + RetrieveMode string `json:"retrieve_mode,omitempty"` Posts string `json:"posts,omitempty"` StatusCodes string `json:"status_codes,omitempty"` Timeout string `json:"timeout,omitempty"` diff --git a/lld.go b/lld.go index d60cdd7..eea9a93 100644 --- a/lld.go +++ b/lld.go @@ -75,6 +75,7 @@ type LLDRule struct { RequestMethod string `json:"request_method,omitempty"` AllowTraps string `json:"allow_traps,omitempty"` PostType string `json:"post_type,omitempty"` + RetrieveMode string `json:"retrieve_mode,omitempty"` Posts string `json:"posts,omitempty"` StatusCodes string `json:"status_codes,omitempty"` Timeout string `json:"timeout,omitempty"` From 9aeefba4f469c2d0b0fef41232cb4e1a92670d82 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Fri, 8 Oct 2021 16:19:54 +0100 Subject: [PATCH 53/54] adding follow redirects --- item.go | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/item.go b/item.go index dff32e4..780e87a 100644 --- a/item.go +++ b/item.go @@ -134,21 +134,22 @@ type Item struct { Preprocessors Preprocessors `json:"preprocessing,omitempty"` // HTTP Agent Fields - Url string `json:"url,omitempty"` - RequestMethod string `json:"request_method,omitempty"` - PostType string `json:"post_type,omitempty"` - RetrieveMode string `json:"retrieve_mode,omitempty"` - Posts string `json:"posts,omitempty"` - StatusCodes string `json:"status_codes,omitempty"` - Timeout string `json:"timeout,omitempty"` - VerifyHost string `json:"verify_host,omitempty"` - VerifyPeer string `json:"verify_peer,omitempty"` - AuthType string `json:"authtype,omitempty"` - Username string `json:"username,omitempty"` - Password string `json:"password,omitempty"` - Headers HttpHeaders `json:"-"` - RawHeaders json.RawMessage `json:"headers,omitempty"` - Proxy string `json:"http_proxy,omitempty"` + Url string `json:"url,omitempty"` + RequestMethod string `json:"request_method,omitempty"` + PostType string `json:"post_type,omitempty"` + RetrieveMode string `json:"retrieve_mode,omitempty"` + Posts string `json:"posts,omitempty"` + StatusCodes string `json:"status_codes,omitempty"` + Timeout string `json:"timeout,omitempty"` + VerifyHost string `json:"verify_host,omitempty"` + VerifyPeer string `json:"verify_peer,omitempty"` + AuthType string `json:"authtype,omitempty"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` + Headers HttpHeaders `json:"-"` + RawHeaders json.RawMessage `json:"headers,omitempty"` + Proxy string `json:"http_proxy,omitempty"` + FollowRedirects string `json:"follow_redirects,omitempty"` // SNMP Fields SNMPOid string `json:"snmp_oid,omitempty"` From 520558d8d2c3c8c2ee73ba4fd75e9c3e243e2d13 Mon Sep 17 00:00:00 2001 From: Thomas Pressnell Date: Fri, 8 Oct 2021 16:22:23 +0100 Subject: [PATCH 54/54] lld as well --- lld.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lld.go b/lld.go index eea9a93..ee223ab 100644 --- a/lld.go +++ b/lld.go @@ -71,19 +71,20 @@ type LLDRule struct { Port string `json:"port,omitempty"` // HTTP Agent Fields - Url string `json:"url,omitempty"` - RequestMethod string `json:"request_method,omitempty"` - AllowTraps string `json:"allow_traps,omitempty"` - PostType string `json:"post_type,omitempty"` - RetrieveMode string `json:"retrieve_mode,omitempty"` - Posts string `json:"posts,omitempty"` - StatusCodes string `json:"status_codes,omitempty"` - Timeout string `json:"timeout,omitempty"` - VerifyHost string `json:"verify_host,omitempty"` - VerifyPeer string `json:"verify_peer,omitempty"` - Headers HttpHeaders `json:"-"` - RawHeaders json.RawMessage `json:"headers,omitempty"` - Proxy string `json:"http_proxy,omitempty"` + Url string `json:"url,omitempty"` + RequestMethod string `json:"request_method,omitempty"` + AllowTraps string `json:"allow_traps,omitempty"` + PostType string `json:"post_type,omitempty"` + RetrieveMode string `json:"retrieve_mode,omitempty"` + Posts string `json:"posts,omitempty"` + StatusCodes string `json:"status_codes,omitempty"` + Timeout string `json:"timeout,omitempty"` + VerifyHost string `json:"verify_host,omitempty"` + VerifyPeer string `json:"verify_peer,omitempty"` + Headers HttpHeaders `json:"-"` + RawHeaders json.RawMessage `json:"headers,omitempty"` + Proxy string `json:"http_proxy,omitempty"` + FollowRedirects string `json:"follow_redirects,omitempty"` // SNMP Fields SNMPOid string `json:"snmp_oid,omitempty"`