diff --git a/item.go b/item.go index 281362b..e23d3a8 100644 --- a/item.go +++ b/item.go @@ -1,6 +1,7 @@ package zabbix import ( + "encoding/json" "fmt" ) @@ -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"` @@ -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 @@ -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) { @@ -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 @@ -223,6 +253,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 +273,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 +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 @@ -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 @@ -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 +} diff --git a/lld.go b/lld.go new file mode 100644 index 0000000..d49d8d5 --- /dev/null +++ b/lld.go @@ -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 +} 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 +}