Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list web scenarios as well as web items #4

Merged
merged 3 commits into from Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 53 additions & 9 deletions handle_latest_data.go
Expand Up @@ -68,18 +68,49 @@ func handleLatestData(

debugf("* hosts identifiers: %s", identifiers)

params := Params{
"hostids": identifiers,
}
var (
items []Item
webchecks []HTTPTest
)

var items []Item
err = withSpinner(
":: Requesting information about hosts items",
":: Requesting information about hosts items & web scenarios",
func() error {
items, err = zabbix.GetItems(params)
return err
errs := make(chan error, 0)

go func() {
var err error

items, err = zabbix.GetItems(Params{
"hostids": identifiers,
"webitems": "1",
})

errs <- err
}()

go func() {
var err error

webchecks, err = zabbix.GetHTTPTests(Params{
"hostids": identifiers,
"expandName": "1",
"selectSteps": "extend",
})

errs <- err
}()

for _, err := range []error{<-errs, <-errs} {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just for err := range errs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am talking about the second part of range, why there is such ambigious statement as []error{<-errs, <-errs}, why not just range errs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kovetskiy: because range errs will never end. Range over channel waits till channel is closed, which will never happen in that case.

if err != nil {
return err
}
}

return nil
},
)

if err != nil {
return hierr.Errorf(
err,
Expand All @@ -91,8 +122,8 @@ func handleLatestData(

for _, item := range items {
line := fmt.Sprintf(
"%s\t%s\t%s\t%-10s",
hash[item.HostID].Name, item.Format(),
"%s\t%s\t%s\t%s\t%-10s",
hash[item.HostID].Name, item.Type.String(), item.Format(),
item.DateTime(), item.LastValue,
)

Expand All @@ -111,6 +142,19 @@ func handleLatestData(
matchedItemIDs = append(matchedItemIDs, item.ID)
}

for _, check := range webchecks {
line := fmt.Sprintf(
"%s\t%s\t%s",
hash[check.HostID].Name, `scenario`, check.Format(),
)

if pattern != "" && !matchPattern(pattern, line) {
continue
}

fmt.Fprintln(table, line)
}

switch {
case stackedGraph:
fmt.Println(zabbix.GetStackedGraphURL(matchedItemIDs))
Expand Down
50 changes: 50 additions & 0 deletions httptest.go
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"strconv"
"time"
)

// HTTPTestStep represents single step in the web scenario.
type HTTPTestStep struct {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add doc to this structures? they have ambigious names (associated with some test purposes for me)

ID string `json:"httpstepid"`
TestID string `json:"httptestid"`
URL string `json:"url"`
}

// HTTPTest represents web scenario, which often used for simple step-by-step
// external monitoring of websites via HTTP.
type HTTPTest struct {
ID string `json:"httptestid"`
HostID string `json:"hostid"`
Name string `json:"name"`
Delay string `json:"delay"`
NextCheck string `json:"nextcheck"`
TemplateID string `json:"templateid"`

Steps []HTTPTestStep `json:"steps"`
}

func (check *HTTPTest) DateTime() string {
if check.NextCheck == "0" {
return "-"
}

return check.date().Format("2006-01-02 15:04:05")
}

func (check *HTTPTest) date() time.Time {
date, _ := strconv.ParseInt(check.NextCheck, 10, 64)
return time.Unix(date, 0)
}

func (check *HTTPTest) Format() string {
return fmt.Sprintf(
"%s (%d steps every %s seconds)\t%s (next)\t",
check.Name,
len(check.Steps),
check.Delay,
check.DateTime(),
)
}
15 changes: 8 additions & 7 deletions item.go
Expand Up @@ -13,13 +13,14 @@ 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_"`
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"`
}

func (item *Item) DateTime() string {
Expand Down
88 changes: 88 additions & 0 deletions item_type.go
@@ -0,0 +1,88 @@
package main

import (
"encoding/json"
"strconv"
)

type ItemType int

const (
ItemTypeAgent ItemType = iota
ItemTypeSNMPv1
ItemTypeTrapper
ItemTypeSimpleCheck
ItemTypeSNMPv2
ItemTypeInternal
ItemTypeSNMPv3
ItemTypeAgentActive
ItemTypeAggregate
ItemTypeWeb
ItemTypeExternalCheck
ItemTypeDatabaseMonitor
ItemTypeIPMI
ItemTypeSSH
ItemTypeTELNET
ItemTypeCalculated
ItemTypeJMX
ItemTypeSNMPTrap
)

func (type_ *ItemType) UnmarshalJSON(data []byte) error {
var stringValue string

err := json.Unmarshal(data, &stringValue)
if err != nil {
return err
}

intValue, err := strconv.ParseInt(stringValue, 10, 64)
if err != nil {
return err
}

*type_ = ItemType(intValue)

return nil
}

func (type_ ItemType) String() string {
switch type_ {
case ItemTypeAgent:
return "agent"
case ItemTypeTrapper:
return "trapper"
case ItemTypeSimpleCheck:
return "check"
case ItemTypeSNMPv2:
return "snmp2"
case ItemTypeInternal:
return "internal"
case ItemTypeSNMPv3:
return "snmp3"
case ItemTypeAgentActive:
return "active"
case ItemTypeAggregate:
return "aggregate"
case ItemTypeWeb:
return "web"
case ItemTypeExternalCheck:
return "external"
case ItemTypeDatabaseMonitor:
return "dbmon"
case ItemTypeIPMI:
return "ipmi"
case ItemTypeSSH:
return "ssh"
case ItemTypeTELNET:
return "telnet"
case ItemTypeCalculated:
return "calc"
case ItemTypeJMX:
return "jmx"
case ItemTypeSNMPTrap:
return "snmp"
default:
return "unknown"
}
}
5 changes: 5 additions & 0 deletions responses.go
Expand Up @@ -41,6 +41,11 @@ type ResponseItems struct {
Data []Item `json:"result"`
}

type ResponseHTTPTests struct {
ResponseRaw
Data []HTTPTest `json:"result"`
}

type ResponseHosts struct {
ResponseRaw
Data []Host `json:"result"`
Expand Down
12 changes: 12 additions & 0 deletions zabbix.go
Expand Up @@ -225,6 +225,18 @@ func (zabbix *Zabbix) GetItems(params Params) ([]Item, error) {
return response.Data, nil
}

func (zabbix *Zabbix) GetHTTPTests(params Params) ([]HTTPTest, error) {
debugln("* retrieving web scenarios list")

var response ResponseHTTPTests
err := zabbix.call("httptest.get", params, &response)
if err != nil {
return nil, err
}

return response.Data, nil
}

func (zabbix *Zabbix) GetUsersGroups(params Params) ([]UserGroup, error) {
debugln("* retrieving users groups list")

Expand Down