Skip to content

Commit

Permalink
fix for #18, zbx with ELK returns nubmers instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kovetskiy committed Feb 7, 2019
1 parent 4abc40e commit 99886f3
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/zabbixctl
/zabbixctl.test
32 changes: 22 additions & 10 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions zabbix.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func NewZabbix(
return nil, karma.Format(
err,
"can't save zabbix session to file '%s'",
sessionFile,
)
}
}
Expand Down
183 changes: 183 additions & 0 deletions zabbix_issue18_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 99886f3

Please sign in to comment.