From 99886f355c2c3723cb021a053160a3d53eb69853 Mon Sep 17 00:00:00 2001 From: Egor Kovetskiy Date: Thu, 7 Feb 2019 17:41:47 +0300 Subject: [PATCH] fix for #18, zbx with ELK returns nubmers instead of strings --- .gitignore | 1 + item.go | 32 ++++--- zabbix.go | 1 + zabbix_issue18_test.go | 183 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 10 deletions(-) create mode 100644 zabbix_issue18_test.go diff --git a/.gitignore b/.gitignore index bb3ed66..cd232ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /zabbixctl +/zabbixctl.test diff --git a/item.go b/item.go index 9217161..dfba5a3 100644 --- a/item.go +++ b/item.go @@ -13,26 +13,38 @@ var ( ) type Item struct { - ID string `json:"itemid"` - HostID string `json:"hostid"` - Name string `json:"name"` - ValueType string `json:"value_type"` - LastValue string `json:"lastvalue"` - LastChange string `json:"lastclock"` - Key string `json:"key_"` - Type ItemType `json:"type"` + ID string `json:"itemid"` + HostID string `json:"hostid"` + Name string `json:"name"` + ValueType string `json:"value_type"` + LastValue string `json:"lastvalue"` + LastClock interface{} `json:"lastclock"` + Key string `json:"key_"` + Type ItemType `json:"type"` } func (item *Item) DateTime() string { - if item.LastChange == "0" { + if item.getLastClock() == "0" { return "-" } return item.date().Format("2006-01-02 15:04:05") } +func (item *Item) getLastClock() string { + switch typed := item.LastClock.(type) { + case string: + return typed + case float64: + return fmt.Sprint(int64(typed)) + default: + panic("asdasdasd") + return "0" + } +} + func (item *Item) date() time.Time { - date, _ := strconv.ParseInt(item.LastChange, 10, 64) + date, _ := strconv.ParseInt(item.getLastClock(), 10, 64) return time.Unix(date, 0) } diff --git a/zabbix.go b/zabbix.go index 611cddd..1187f21 100644 --- a/zabbix.go +++ b/zabbix.go @@ -91,6 +91,7 @@ func NewZabbix( return nil, karma.Format( err, "can't save zabbix session to file '%s'", + sessionFile, ) } } diff --git a/zabbix_issue18_test.go b/zabbix_issue18_test.go new file mode 100644 index 0000000..bb9260c --- /dev/null +++ b/zabbix_issue18_test.go @@ -0,0 +1,183 @@ +package main + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +const ( + issue18_data = ` +{ + "jsonrpc": "2.0", + "result": [ + { + "itemid": "28494", + "type": "2", + "snmp_community": "", + "snmp_oid": "", + "hostid": "10084", + "name": "Number of csvs today", + "key_": "csv.today.count", + "delay": "0", + "history": "90d", + "trends": "365d", + "status": "0", + "value_type": "3", + "trapper_hosts": "", + "units": "", + "snmpv3_securityname": "", + "snmpv3_securitylevel": "0", + "snmpv3_authpassphrase": "", + "snmpv3_privpassphrase": "", + "formula": "", + "error": "", + "lastlogsize": "0", + "logtimefmt": "", + "templateid": "28381", + "valuemapid": "0", + "params": "", + "ipmi_sensor": "", + "authtype": "0", + "username": "", + "password": "", + "publickey": "", + "privatekey": "", + "mtime": "0", + "flags": "0", + "interfaceid": "0", + "port": "", + "description": "", + "inventory_link": "0", + "lifetime": "30d", + "snmpv3_authprotocol": "0", + "snmpv3_privprotocol": "0", + "state": "0", + "snmpv3_contextname": "", + "evaltype": "0", + "jmx_endpoint": "", + "master_itemid": "0", + "timeout": "3s", + "url": "", + "query_fields": [], + "posts": "", + "status_codes": "200", + "follow_redirects": "1", + "post_type": "0", + "http_proxy": "", + "headers": [], + "retrieve_mode": "0", + "request_method": "1", + "output_format": "0", + "ssl_cert_file": "", + "ssl_key_file": "", + "ssl_key_password": "", + "verify_peer": "0", + "verify_host": "0", + "allow_traps": "0", + "lastclock": 1548924066, + "lastns": 388228365, + "lastvalue": "6", + "prevvalue": "6" + }, + { + "itemid": "28461", + "type": "0", + "snmp_community": "", + "snmp_oid": "", + "hostid": "10084", + "name": "Indices count", + "key_": "elastizabbix[cluster,indices.count]", + "delay": "60", + "history": "7d", + "trends": "365d", + "status": "0", + "value_type": "3", + "trapper_hosts": "", + "units": "", + "snmpv3_securityname": "", + "snmpv3_securitylevel": "0", + "snmpv3_authpassphrase": "", + "snmpv3_privpassphrase": "", + "formula": "", + "error": "Unsupported item key.", + "lastlogsize": "0", + "logtimefmt": "", + "templateid": "28351", + "valuemapid": "0", + "params": "", + "ipmi_sensor": "", + "authtype": "0", + "username": "", + "password": "", + "publickey": "", + "privatekey": "", + "mtime": "0", + "flags": "0", + "interfaceid": "1", + "port": "", + "description": "", + "inventory_link": "0", + "lifetime": "30d", + "snmpv3_authprotocol": "0", + "snmpv3_privprotocol": "0", + "state": "1", + "snmpv3_contextname": "", + "evaltype": "0", + "jmx_endpoint": "", + "master_itemid": "0", + "timeout": "3s", + "url": "", + "query_fields": [], + "posts": "", + "status_codes": "200", + "follow_redirects": "1", + "post_type": "0", + "http_proxy": "", + "headers": [], + "retrieve_mode": "0", + "request_method": "1", + "output_format": "0", + "ssl_cert_file": "", + "ssl_key_file": "", + "ssl_key_password": "", + "verify_peer": "0", + "verify_host": "0", + "allow_traps": "0", + "lastclock": "0", + "lastns": "0", + "lastvalue": "0", + "prevvalue": "0" + } + ] +} +` +) + +func TestIssue18(t *testing.T) { + test := assert.New(t) + + testserver := httptest.NewServer(http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, issue18_data) + }, + )) + defer testserver.Close() + + zabbix := &Zabbix{} + zabbix.client = testserver.Client() + zabbix.apiURL = testserver.URL + + items, err := zabbix.GetItems(Params{"hostids": []string{"10084"}}) + test.NoError(err) + test.Len(items, 2) + + test.Equal("1548924066", items[0].getLastClock()) + test.Equal("0", items[1].getLastClock()) + + test.Equal("2019-01-31 11:41:06", items[0].DateTime()) + test.Equal("-", items[1].DateTime()) +}