diff --git a/README.md b/README.md index 718da4e..23e475d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +# yo000 Fork +An update to v7 API, where authentication is done via Authorization header. +Add item support + # nxs-go-zabbix This Go package provides access to Zabbix API v5.0. @@ -104,4 +108,4 @@ For support and feedback please contact me: ## License -nxs-go-zabbix is released under the [MIT License](LICENSE). \ No newline at end of file +nxs-go-zabbix is released under the [MIT License](LICENSE). diff --git a/host.go b/host.go index b7fac43..ce4b9f4 100644 --- a/host.go +++ b/host.go @@ -105,6 +105,14 @@ const ( HostTagOperatorEquals = 1 ) +// For `MonitoredBy` field: `Operator` +const ( + MonitoredByZabbixServer = 0 + MonitoredByProxy = 1 + MonitoredByProxyGroup = 2 +) + + // HostObject struct is used to store host operations results // // see: https://www.zabbix.com/documentation/5.0/manual/api/reference/host/object#host @@ -135,7 +143,9 @@ type HostObject struct { MaintenanceType int `json:"maintenance_type,omitempty"` // has defined consts, see above MaintenanceID int `json:"maintenanceid,omitempty"` Name string `json:"name,omitempty"` + MonitoredBy int `json:"monitored_by,omitempty"` // has defined consts, see above ProxyHostID int `json:"proxy_hostid,omitempty"` + ProxyGroupID int `json:"proxy_groupid,omitempty"` SnmpAvailable int `json:"snmp_available,omitempty"` // has defined consts, see above SnmpDisableUntil int `json:"snmp_disable_until,omitempty"` SnmpError string `json:"snmp_error,omitempty"` @@ -184,6 +194,7 @@ type HostGetParams struct { MaintenanceIDs []int `json:"maintenanceids,omitempty"` MonitoredHosts bool `json:"monitored_hosts,omitempty"` ProxyHosts bool `json:"proxy_hosts,omitempty"` + ProxyGroupIDs []int `json:"proxy_groupids,omitempty"` ProxyIDs []int `json:"proxyids,omitempty"` TemplatedHosts bool `json:"templated_hosts,omitempty"` TemplateIDs []int `json:"templateids,omitempty"` diff --git a/item.go b/item.go new file mode 100644 index 0000000..e5101e5 --- /dev/null +++ b/item.go @@ -0,0 +1,183 @@ +package zabbix + +// For `ItemObject` field: `ItemType` +const ( + ItemTypeZabbixAgent = 0 + ItemTypeZabbixTrapper = 2 + ItemTypeSimpleCheck = 3 + ItemTypeZabbixInternal = 5 + ItemTypeZabbixAgentActive = 7 + ItemTypeWebItem = 9 + ItemTypeExternalCheck = 10 + ItemTypeDBMonitor = 11 + ItemTypeIPMIAgent = 12 + ItemTypeSSHAgent = 13 + ItemTypeTelnetAgent = 14 + ItemTypeCalculated = 15 + ItemTypeJMXAgent = 16 + ItemTypeSNMPTrap = 17 + ItemTypeDependentItem = 18 + ItemTypeHTTPAgent = 19 + ItemTypeSNMPAgent = 20 + ItemTypeScript = 21 + ItemTypeBrowser = 22 +) + +// For `ItemObject` field: `ValueType` +const ( + ItemValueTypeNumericFloat = 0 + ItemValueTypeCharacter = 1 + ItemValueTypeLog = 2 + ItemValueTypeNumericUnsigned = 3 + ItemValueTypeText = 4 + ItemValueTypeBinary = 5 +) + +// For `ItemObject` field: `Status` +const ( + ItemStatusEnabled = 0 + ItemStatusDisabled = 1 +) + +// For `ItemGetParams` field: `Evaltype` +const ( + ItemEvaltypeAndOr = 0 + ItemEvaltypeOr = 2 +) + +// TemplateObject struct is used to store template operations results +// +// see: https://www.zabbix.com/documentation/7.0/manual/api/reference/template/object +type ItemObject struct { + ItemID int `json:"itemid,omitempty"` + Delay string `json:"delay,omitempty"` + HostID int `json:"hostid,omitempty"` + InterfaceID int `json:"interfaceid,omitempty"` + Key string `json:"key_,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + ItemType int `json:"type,omitempty"` + ValueType int `json:"value_type,omitempty"` + SnmpOID string `json:"snmp_oid,omitempty"` + Status int `json:"status,omitempty"` + TemplateID int `json:"templateid,omitempty"` + UUID string `json:"uuid,omitempty"` + + Groups []HostgroupObject `json:"groups,omitempty"` + Tags []ItemTagObject `json:"tags,omitempty"` + Templates []TemplateObject `json:"templates,omitempty"` + ParentTemplates []TemplateObject `json:"parentTemplates,omitempty"` + Macros []UsermacroObject `json:"macros,omitempty"` + Hosts []HostObject `json:"hosts,omitempty"` +} + +// ItemTagObject struct is used to store item tag data +// +// see: https://www.zabbix.com/documentation/7.0/manual/api/reference/template/object#template_tag +type ItemTagObject struct { + Tag string `json:"tag,omitempty"` + Value string `json:"value,omitempty"` +} + +// TemplateGetParams struct is used for template get requests +// +// see: https://www.zabbix.com/documentation/5.0/manual/api/reference/template/get#parameters +type ItemGetParams struct { + GetParameters + + ItemIDs []int `json:"itemids,omitempty"` + GroupIDs []int `json:"groupids,omitempty"` + TemplateIDs []int `json:"templateids,omitempty"` + HostIDs []int `json:"hostids,omitempty"` + ProxyIDs []int `json:"proxyids,omitempty"` + GraphIDs []int `json:"graphids,omitempty"` + TriggerIDs []int `json:"triggerids,omitempty"` + + Inherited bool `json:"inherited,omitempty"` + Templated bool `json:"templated,omitempty"` + Monitored bool `json:"monitored,omitempty"` + Group string `json:"group,omitempty"` + Host string `json:"host,omitempty"` + WithItems bool `json:"with_items,omitempty"` + WithTriggers bool `json:"with_triggers,omitempty"` + Evaltype int `json:"evaltype,omitempty"` // has defined consts, see above + Tags []ItemTagObject `json:"tags,omitempty"` + + SelectTags SelectQuery `json:"selectTags,omitempty"` + SelectHosts SelectQuery `json:"selectHosts,omitempty"` + SelectInterfaces SelectQuery `json:"selectInterfaces,omitempty"` + selectTriggers SelectQuery `json:"selectTriggers,omitempty"` + + // SelectHttpTests SelectQuery `json:"selectHttpTests,omitempty"` // not implemented yet + // SelectItems SelectQuery `json:"selectItems,omitempty"` // not implemented yet + // SelectDiscoveries SelectQuery `json:"selectDiscoveries,omitempty"` // not implemented yet + // SelectTriggers SelectQuery `json:"selectTriggers,omitempty"` // not implemented yet + // SelectGraphs SelectQuery `json:"selectGraphs,omitempty"` // not implemented yet + // SelectApplications SelectQuery `json:"selectApplications,omitempty"` // not implemented yet + // SelectScreens SelectQuery `json:"selectScreens,omitempty"` // not implemented yet +} + +// Structure to store creation result +type itemCreateResult struct { + ItemIDs []int `json:"itemids"` +} + +type itemUpdateResult itemCreateResult +type itemDeleteResult itemCreateResult + +/*// Structure to store deletion result +type itemDeleteResult struct { + ItemIDs []int `json:"itemids"` +}*/ + +// ItemGet gets items +func (z *Context) ItemGet(params ItemGetParams) ([]ItemObject, int, error) { + + var result []ItemObject + + status, err := z.request("item.get", params, &result) + if err != nil { + return nil, status, err + } + + return result, status, nil +} + +// ItemCreate creates items +func (z *Context) ItemCreate(params []ItemObject) ([]int, int, error) { + + var result itemCreateResult + + status, err := z.request("item.create", params, &result) + if err != nil { + return nil, status, err + } + + return result.ItemIDs, status, nil +} + +// ItemUpdate updates items +func (z *Context) ItemUpdate(params []ItemObject) ([]int, int, error) { + + var result itemUpdateResult + + status, err := z.request("item.update", params, &result) + if err != nil { + return nil, status, err + } + + return result.ItemIDs, status, nil +} + +// TemplateDelete deletes templates +func (z *Context) ItemDelete(itemIDs []int) ([]int, int, error) { + + var result itemDeleteResult + + status, err := z.request("item.delete", itemIDs, &result) + if err != nil { + return nil, status, err + } + + return result.ItemIDs, status, nil +} diff --git a/itemprototype.go b/itemprototype.go new file mode 100644 index 0000000..2c13300 --- /dev/null +++ b/itemprototype.go @@ -0,0 +1,183 @@ +package zabbix + +// For `ItemPrototypeObject` field: `ItemPrototypeType` +const ( + ItemPrototypeTypeZabbixAgent = 0 + ItemPrototypeTypeZabbixTrapper = 2 + ItemPrototypeTypeSimpleCheck = 3 + ItemPrototypeTypeZabbixInternal = 5 + ItemPrototypeTypeZabbixAgentActive = 7 + ItemPrototypeTypeExternalCheck = 10 + ItemPrototypeTypeDBMonitor = 11 + ItemPrototypeTypeIPMIAgent = 12 + ItemPrototypeTypeSSHAgent = 13 + ItemPrototypeTypeTelnetAgent = 14 + ItemPrototypeTypeCalculated = 15 + ItemPrototypeTypeJMXAgent = 16 + ItemPrototypeTypeSNMPTrap = 17 + ItemPrototypeTypeDependentItem = 18 + ItemPrototypeTypeHTTPAgent = 19 + ItemPrototypeTypeSNMPAgent = 20 + ItemPrototypeTypeScript = 21 + ItemPrototypeTypeBrowser = 22 +) + +// For `ItemPrototypeObject` field: `ValueType` +const ( + ItemPrototypeValueTypeNumericFloat = 0 + ItemPrototypeValueTypeCharacter = 1 + ItemPrototypeValueTypeLog = 2 + ItemPrototypeValueTypeNumericUnsigned = 3 + ItemPrototypeValueTypeText = 4 + ItemPrototypeValueTypeBinary = 5 +) + +// For `ItemPrototypeObject` field: `Status` +const ( + ItemPrototypeStatusEnabled = 0 + ItemPrototypeStatusDisabled = 1 +) + +// For `ItemPrototypeGetParams` field: `Evaltype` +const ( + ItemPrototypeEvaltypeAndOr = 0 + ItemPrototypeEvaltypeOr = 2 +) + +// TemplateObject struct is used to store template operations results +// +// see: https://www.zabbix.com/documentation/7.0/en/manual/api/reference/itemprototype/object +type ItemPrototypeObject struct { + ItemPrototypeID int `json:"itemid,omitempty"` + Delay string `json:"delay,omitempty"` + HostID int `json:"hostid,omitempty"` + InterfaceID int `json:"interfaceid,omitempty"` + Key string `json:"key_,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + ItemPrototypeType int `json:"type,omitempty"` + ValueType int `json:"value_type,omitempty"` + SnmpOID string `json:"snmp_oid,omitempty"` + Status int `json:"status,omitempty"` + TemplateID int `json:"templateid,omitempty"` + UUID string `json:"uuid,omitempty"` + + Groups []HostgroupObject `json:"groups,omitempty"` + Tags []ItemPrototypeTagObject `json:"tags,omitempty"` + Templates []TemplateObject `json:"templates,omitempty"` + ParentTemplates []TemplateObject `json:"parentTemplates,omitempty"` + Macros []UsermacroObject `json:"macros,omitempty"` + Hosts []HostObject `json:"hosts,omitempty"` +} + +// ItemPrototypeTagObject struct is used to store item tag data +// +// see: https://www.zabbix.com/documentation/7.0/manual/api/reference/template/object#template_tag +type ItemPrototypeTagObject struct { + Tag string `json:"tag,omitempty"` + Value string `json:"value,omitempty"` +} + +// TemplateGetParams struct is used for template get requests +// +// see: https://www.zabbix.com/documentation/7.0/en/manual/api/reference/itemprototype/get#parameters +type ItemPrototypeGetParams struct { + GetParameters + + ItemPrototypeIDs []int `json:"itemids,omitempty"` + DiscoveryIDs []int `json:"discoveryids,omitempty"` + GroupIDs []int `json:"groupids,omitempty"` + TemplateIDs []int `json:"templateids,omitempty"` + HostIDs []int `json:"hostids,omitempty"` + ProxyIDs []int `json:"proxyids,omitempty"` + GraphIDs []int `json:"graphids,omitempty"` + TriggerIDs []int `json:"triggerids,omitempty"` + + Inherited bool `json:"inherited,omitempty"` + Templated bool `json:"templated,omitempty"` + Monitored bool `json:"monitored,omitempty"` + Group string `json:"group,omitempty"` + Host string `json:"host,omitempty"` + WithItemPrototypes bool `json:"with_items,omitempty"` + WithTriggers bool `json:"with_triggers,omitempty"` + Evaltype int `json:"evaltype,omitempty"` // has defined consts, see above + Tags []ItemPrototypeTagObject `json:"tags,omitempty"` + + SelectTags SelectQuery `json:"selectTags,omitempty"` + SelectHosts SelectQuery `json:"selectHosts,omitempty"` + SelectInterfaces SelectQuery `json:"selectInterfaces,omitempty"` + selectTriggers SelectQuery `json:"selectTriggers,omitempty"` + + // SelectHttpTests SelectQuery `json:"selectHttpTests,omitempty"` // not implemented yet + // SelectItemPrototypes SelectQuery `json:"selectItemPrototypes,omitempty"` // not implemented yet + // SelectDiscoveries SelectQuery `json:"selectDiscoveries,omitempty"` // not implemented yet + // SelectTriggers SelectQuery `json:"selectTriggers,omitempty"` // not implemented yet + // SelectGraphs SelectQuery `json:"selectGraphs,omitempty"` // not implemented yet + // SelectApplications SelectQuery `json:"selectApplications,omitempty"` // not implemented yet + // SelectScreens SelectQuery `json:"selectScreens,omitempty"` // not implemented yet +} + +// Structure to store creation result +type itemPrototypeCreateResult struct { + ItemPrototypeIDs []int `json:"itemids"` +} + +type itemPrototypeUpdateResult itemPrototypeCreateResult +type itemPrototypeDeleteResult itemPrototypeCreateResult + +/*// Structure to store deletion result +type itemDeleteResult struct { + ItemPrototypeIDs []int `json:"itemids"` +}*/ + +// ItemPrototypeGet gets items +func (z *Context) ItemPrototypeGet(params ItemPrototypeGetParams) ([]ItemPrototypeObject, int, error) { + + var result []ItemPrototypeObject + + status, err := z.request("itemprototype.get", params, &result) + if err != nil { + return nil, status, err + } + + return result, status, nil +} + +// ItemPrototypeCreate creates items +func (z *Context) ItemPrototypeCreate(params []ItemPrototypeObject) ([]int, int, error) { + + var result itemPrototypeCreateResult + + status, err := z.request("itemprototype.create", params, &result) + if err != nil { + return nil, status, err + } + + return result.ItemPrototypeIDs, status, nil +} + +// ItemPrototypeUpdate updates items +func (z *Context) ItemPrototypeUpdate(params []ItemPrototypeObject) ([]int, int, error) { + + var result itemPrototypeUpdateResult + + status, err := z.request("itemprototype.update", params, &result) + if err != nil { + return nil, status, err + } + + return result.ItemPrototypeIDs, status, nil +} + +// TemplateDelete deletes templates +func (z *Context) ItemPrototypeDelete(itemIDs []int) ([]int, int, error) { + + var result itemPrototypeDeleteResult + + status, err := z.request("itemprototype.delete", itemIDs, &result) + if err != nil { + return nil, status, err + } + + return result.ItemPrototypeIDs, status, nil +} diff --git a/proxygroup.go b/proxygroup.go new file mode 100644 index 0000000..bbfbd75 --- /dev/null +++ b/proxygroup.go @@ -0,0 +1,92 @@ +package zabbix + +// For `ProxygroupObject` field: `Status` +const ( + ProxygroupFlagsPlain = 0 + ProxygroupFlagsDiscrovered = 4 +) + +// For `ProxygroupObject` field: `Internal` +const ( + ProxygroupStateUnknown = 0 + ProxygroupStateOffline = 1 + ProxygroupStateRecovering = 2 + ProxygroupStateOnline = 3 + ProxygroupStateDegrading = 4 +) + +// ProxygroupObject struct is used to store Proxygroup operations results +// +// see: https://www.zabbix.com/documentation/current/en/manual/api/reference/proxygroup/object +type ProxygroupObject struct { + ProxyGroupID int `json:"proxy_groupid,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + FailoverDelay string `json:"failover_delay,omitempty"` + MinOnline string `json:"min_online,omitempty"` + State int `json:"state,omitempty"` +} + +// ProxygroupGetParams struct is used for Proxygroup get requests +// +// see: https://www.zabbix.com/documentation/current/en/manual/api/reference/proxygroup/get#parameters +type ProxygroupGetParams struct { + GetParameters + + ProxyGroupIDs []int `json:"proxy_groupids,omitempty"` + ProxyIDs []int `json:"proxyids,omitempty"` + MaintenanceIDs []int `json:"maintenanceids,omitempty"` + + // SelectDiscoveryRule SelectQuery `json:"selectDiscoveryRule,omitempty"` // not implemented yet + // SelectGroupDiscovery SelectQuery `json:"selectGroupDiscovery,omitempty"` // not implemented yet + SelectProxies SelectQuery `json:"selectProxies,omitempty"` +} + +// Structure to store creation result +type ProxygroupCreateResult struct { + ProxyGroupIDs []int `json:"ProxyGroupIDs"` +} + +// Structure to store deletion result +type ProxygroupDeleteResult struct { + ProxyGroupIDs []int `json:"ProxyGroupIDs"` +} + +// ProxygroupGet gets Proxygroups +func (z *Context) ProxygroupGet(params ProxygroupGetParams) ([]ProxygroupObject, int, error) { + + var result []ProxygroupObject + + status, err := z.request("proxygroup.get", params, &result) + if err != nil { + return nil, status, err + } + + return result, status, nil +} + +// ProxygroupCreate creates Proxygroups +func (z *Context) ProxygroupCreate(params []ProxygroupObject) ([]int, int, error) { + + var result ProxygroupCreateResult + + status, err := z.request("proxygroup.create", params, &result) + if err != nil { + return nil, status, err + } + + return result.ProxyGroupIDs, status, nil +} + +// ProxygroupDelete deletes Proxygroups +func (z *Context) ProxygroupDelete(ProxyGroupIDs []int) ([]int, int, error) { + + var result ProxygroupDeleteResult + + status, err := z.request("proxygroup.delete", ProxyGroupIDs, &result) + if err != nil { + return nil, status, err + } + + return result.ProxyGroupIDs, status, nil +} diff --git a/user.go b/user.go index 69f38a2..95c7b3e 100644 --- a/user.go +++ b/user.go @@ -74,7 +74,7 @@ type MediaObject struct { // // see: https://www.zabbix.com/documentation/5.0/manual/api/reference/user/login#parameters type UserLoginParams struct { - User string `json:"user"` + UserName string `json:"username"` Password string `json:"password"` UserData string `json:"userData,omitempty"` } diff --git a/zabbix.go b/zabbix.go index d1f9b1d..8e9dce9 100644 --- a/zabbix.go +++ b/zabbix.go @@ -81,7 +81,7 @@ func (z *Context) Login(host, user, password string) error { z.host = host r := UserLoginParams{ - User: user, + UserName: user, Password: password, } @@ -116,7 +116,8 @@ func (z *Context) request(method string, params interface{}, result interface{}) JSONRPC: "2.0", Method: method, Params: params, - Auth: z.sessionKey, + // in v7 sessionKey is passed in Authorization: Bearer header + //Auth: z.sessionKey, ID: 1, } @@ -145,6 +146,10 @@ func (z *Context) httpPost(in interface{}, out interface{}) (int, error) { // Set headers req.Header.Add("Content-Type", "application/json-rpc") + // in v7 sessionKey is passed in Authorization: Bearer header + if len(z.sessionKey) > 0 { + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", z.sessionKey)) + } // Make request res, err := http.DefaultClient.Do(req)