Data queries (alerting)

This commit is contained in:
Alexander Zobnin
2020-05-29 13:36:28 +03:00
parent 9f344cb867
commit 61fee6ea12
7 changed files with 294 additions and 179 deletions

View File

@@ -1,6 +1,12 @@
package zabbix
import (
"fmt"
"strings"
)
type Items []Item
type Item struct {
ID string `json:"itemid,omitempty"`
Key string `json:"key_,omitempty"`
@@ -11,12 +17,65 @@ type Item struct {
Status string `json:"status,omitempty"`
State string `json:"state,omitempty"`
}
func (item *Item) ExpandItem() string {
name := item.Name
key := item.Key
if strings.Index(key, "[") == -1 {
return name
}
keyRunes := []rune(item.Key)
keyParamsStr := string(keyRunes[strings.Index(key, "[")+1 : strings.LastIndex(key, "]")])
keyParams := splitKeyParams(keyParamsStr)
for i := len(keyParams); i >= 1; i-- {
name = strings.ReplaceAll(name, fmt.Sprintf("$%v", i), keyParams[i-1])
}
return name
}
func splitKeyParams(paramStr string) []string {
paramRunes := []rune(paramStr)
params := []string{}
quoted := false
inArray := false
splitSymbol := ","
param := ""
for _, r := range paramRunes {
symbol := string(r)
if symbol == `"` && inArray {
param += symbol
} else if symbol == `"` && quoted {
quoted = false
} else if symbol == `"` && !quoted {
quoted = true
} else if symbol == "[" && !quoted {
inArray = true
} else if symbol == "]" && !quoted {
inArray = false
} else if symbol == splitSymbol && !quoted && !inArray {
params = append(params, param)
param = ""
} else {
param += symbol
}
}
params = append(params, param)
return params
}
type ItemHost struct {
ID string `json:"hostid,omitempty"`
Name string `json:"name,omitempty"`
}
type Trend []TrendPoint
type TrendPoint struct {
ItemID string `json:"itemid,omitempty"`
Clock int64 `json:"clock,omitempty,string"`
@@ -27,6 +86,7 @@ type TrendPoint struct {
}
type History []HistoryPoint
type HistoryPoint struct {
ItemID string `json:"itemid,omitempty"`
Clock int64 `json:"clock,omitempty,string"`