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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 100 additions & 10 deletions item.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zabbix

import (
"encoding/json"
"fmt"
)

Expand Down Expand Up @@ -119,9 +120,10 @@ 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"`
// list of strings on set, but list of objects on get
Applications json.RawMessage `json:"applications,omitempty"`

ItemParent Hosts `json:"hosts"`

Expand All @@ -138,18 +140,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
Expand Down Expand Up @@ -186,6 +192,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) {
Expand All @@ -202,11 +215,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
Expand All @@ -223,13 +253,30 @@ 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
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.
Expand All @@ -248,6 +295,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
Expand All @@ -262,6 +323,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
Expand All @@ -283,3 +355,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["prototypeids"].([]interface{})
if !ok {
itemids2 := result["prototypeids"].(map[string]interface{})
for _, id := range itemids2 {
itemids = append(itemids, id)
}
} else {
itemids = itemids1
}
return
}
201 changes: 201 additions & 0 deletions lld.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
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 LLDOperatorType `json:"operator,omitempty"`
}

type LLDRuleFilterConditions []LLDRuleFilterCondition

type LLDRuleFilter struct {
Conditions LLDRuleFilterConditions `json:"conditions"`
EvalType LLDEvalType `json:"evaltype"`
EvalFormula string `json:"eval_formula,omitempty"`
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 {
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"`
MasterItemID string `json:"master_itemid,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"`
MacroPaths LLDMacroPaths `json:"lld_macro_paths,omitempty"`
}

// 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["ruleids"].([]interface{})
if !ok {
itemids2 := result["ruleids"].(map[string]interface{})
for _, id := range itemids2 {
itemids = append(itemids, id)
}
} else {
itemids = itemids1
}
return
}
Loading