Skip to content

Commit

Permalink
Merge pull request #4 from seletskiy/web-scenarios
Browse files Browse the repository at this point in the history
list web scenarios as well as web items
  • Loading branch information
kovetskiy committed Sep 21, 2016
2 parents bed9f8a + 175e6fa commit 151afcf
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 16 deletions.
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} {
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 {
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

0 comments on commit 151afcf

Please sign in to comment.