Skip to content

Commit

Permalink
substitute '$1' in item names
Browse files Browse the repository at this point in the history
  • Loading branch information
seletskiy committed Jul 19, 2016
1 parent 919bc08 commit a270c18
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Item struct {
Name string `json:"name"`
LastValue string `json:"lastvalue"`
LastChange string `json:"lastclock"`
Key string `json:"key_"`
}

func (item *Item) DateTime() string {
Expand Down
29 changes: 28 additions & 1 deletion zabbix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
"regexp"
"strings"
"sync/atomic"
"time"
Expand All @@ -19,6 +21,10 @@ const (
ZabbixSessionTTL = 900 - 60
)

var (
reItemKeyParams = regexp.MustCompile(`\[([^\]]+)\]`)
)

type Params map[string]interface{}

type Request struct {
Expand Down Expand Up @@ -220,7 +226,14 @@ func (zabbix *Zabbix) GetItems(params Params) ([]Item, error) {
return nil, err
}

return response.Data, nil
result := make([]Item, len(response.Data))
for i, item := range response.Data {
item.Name = expandKeyArgumentsInName(item.Name, item.Key)

result[i] = item
}

return result, nil
}

func (zabbix *Zabbix) GetUsersGroups(params Params) ([]UserGroup, error) {
Expand Down Expand Up @@ -452,3 +465,17 @@ func unshuffle(target interface{}) []interface{} {

return values
}

func expandKeyArgumentsInName(name string, key string) string {
match := reItemKeyParams.FindStringSubmatch(key)
if len(match) == 0 {
return name
}

args := strings.Split(match[1], ",")
for index, arg := range args {
name = strings.Replace(name, fmt.Sprintf(`$%d`, index+1), arg, -1)
}

return name
}

0 comments on commit a270c18

Please sign in to comment.