Refactor: zabbix client

This commit is contained in:
Alexander Zobnin
2021-05-19 13:17:46 +03:00
parent 762ea252b2
commit badecc3aae
14 changed files with 705 additions and 371 deletions

55
pkg/zabbix/cache.go Normal file
View File

@@ -0,0 +1,55 @@
package zabbix
import (
"crypto/sha1"
"encoding/hex"
"time"
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
)
var cachedMethods = map[string]bool{
"hostgroup.get": true,
"host.get": true,
"application.get": true,
"item.get": true,
"service.get": true,
"usermacro.get": true,
"proxy.get": true,
}
func IsCachedRequest(method string) bool {
_, ok := cachedMethods[method]
return ok
}
// ZabbixCache is a cache for datasource instance.
type ZabbixCache struct {
cache *cache.Cache
}
// NewZabbixCache creates a DatasourceCache with expiration(ttl) time and cleanupInterval.
func NewZabbixCache(ttl time.Duration, cleanupInterval time.Duration) *ZabbixCache {
return &ZabbixCache{
cache.NewCache(ttl, cleanupInterval),
}
}
// GetAPIRequest gets request response from cache
func (c *ZabbixCache) GetAPIRequest(request *ZabbixAPIRequest) (interface{}, bool) {
requestHash := HashString(request.String())
return c.cache.Get(requestHash)
}
// SetAPIRequest writes request response to cache
func (c *ZabbixCache) SetAPIRequest(request *ZabbixAPIRequest, response interface{}) {
requestHash := HashString(request.String())
c.cache.Set(requestHash, response)
}
// HashString converts the given text string to hash string
func HashString(text string) string {
hash := sha1.New()
hash.Write([]byte(text))
return hex.EncodeToString(hash.Sum(nil))
}

225
pkg/zabbix/methods.go Normal file
View File

@@ -0,0 +1,225 @@
package zabbix
import (
"context"
"encoding/json"
"github.com/bitly/go-simplejson"
)
func (ds *Zabbix) GetItems(ctx context.Context, groupFilter string, hostFilter string, appFilter string, itemFilter string, itemType string) (Items, error) {
hosts, err := ds.GetHosts(ctx, groupFilter, hostFilter)
if err != nil {
return nil, err
}
var hostids []string
for _, k := range hosts {
hostids = append(hostids, k["hostid"].(string))
}
apps, err := ds.GetApps(ctx, groupFilter, hostFilter, appFilter)
// Apps not supported in Zabbix 5.4 and higher
if isAppMethodNotFoundError(err) {
apps = []map[string]interface{}{}
} else if err != nil {
return nil, err
}
var appids []string
for _, l := range apps {
appids = append(appids, l["applicationid"].(string))
}
var allItems *simplejson.Json
if len(hostids) > 0 {
allItems, err = ds.GetAllItems(ctx, hostids, nil, itemType)
} else if len(appids) > 0 {
allItems, err = ds.GetAllItems(ctx, nil, appids, itemType)
}
var items Items
if allItems == nil {
items = Items{}
} else {
itemsJSON, err := allItems.MarshalJSON()
if err != nil {
return nil, err
}
err = json.Unmarshal(itemsJSON, &items)
if err != nil {
return nil, err
}
}
re, err := parseFilter(itemFilter)
if err != nil {
return nil, err
}
filteredItems := Items{}
for _, item := range items {
itemName := item.ExpandItem()
if item.Status == "0" {
if re != nil {
if re.MatchString(itemName) {
filteredItems = append(filteredItems, item)
}
} else if itemName == itemFilter {
filteredItems = append(filteredItems, item)
}
}
}
return filteredItems, nil
}
func (ds *Zabbix) GetApps(ctx context.Context, groupFilter string, hostFilter string, appFilter string) ([]map[string]interface{}, error) {
hosts, err := ds.GetHosts(ctx, groupFilter, hostFilter)
if err != nil {
return nil, err
}
var hostids []string
for _, k := range hosts {
hostids = append(hostids, k["hostid"].(string))
}
allApps, err := ds.GetAllApps(ctx, hostids)
if err != nil {
return nil, err
}
re, err := parseFilter(appFilter)
if err != nil {
return nil, err
}
var apps []map[string]interface{}
for _, i := range allApps.MustArray() {
name := i.(map[string]interface{})["name"].(string)
if re != nil {
if re.MatchString(name) {
apps = append(apps, i.(map[string]interface{}))
}
} else if name == appFilter {
apps = append(apps, i.(map[string]interface{}))
}
}
return apps, nil
}
func (ds *Zabbix) GetHosts(ctx context.Context, groupFilter string, hostFilter string) ([]map[string]interface{}, error) {
groups, err := ds.GetGroups(ctx, groupFilter)
if err != nil {
return nil, err
}
var groupids []string
for _, k := range groups {
groupids = append(groupids, k["groupid"].(string))
}
allHosts, err := ds.GetAllHosts(ctx, groupids)
if err != nil {
return nil, err
}
re, err := parseFilter(hostFilter)
if err != nil {
return nil, err
}
var hosts []map[string]interface{}
for _, i := range allHosts.MustArray() {
name := i.(map[string]interface{})["name"].(string)
if re != nil {
if re.MatchString(name) {
hosts = append(hosts, i.(map[string]interface{}))
}
} else if name == hostFilter {
hosts = append(hosts, i.(map[string]interface{}))
}
}
return hosts, nil
}
func (ds *Zabbix) GetGroups(ctx context.Context, groupFilter string) ([]map[string]interface{}, error) {
allGroups, err := ds.GetAllGroups(ctx)
if err != nil {
return nil, err
}
re, err := parseFilter(groupFilter)
if err != nil {
return nil, err
}
var groups []map[string]interface{}
for _, i := range allGroups.MustArray() {
name := i.(map[string]interface{})["name"].(string)
if re != nil {
if re.MatchString(name) {
groups = append(groups, i.(map[string]interface{}))
}
} else if name == groupFilter {
groups = append(groups, i.(map[string]interface{}))
}
}
return groups, nil
}
func (ds *Zabbix) GetAllItems(ctx context.Context, hostids []string, appids []string, itemtype string) (*simplejson.Json, error) {
params := ZabbixAPIParams{
"output": []string{"itemid", "name", "key_", "value_type", "hostid", "status", "state"},
"sortfield": "name",
"webitems": true,
"filter": map[string]interface{}{},
"selectHosts": []string{"hostid", "name"},
"hostids": hostids,
"applicationids": appids,
}
filter := params["filter"].(map[string]interface{})
if itemtype == "num" {
filter["value_type"] = []int{0, 3}
} else if itemtype == "text" {
filter["value_type"] = []int{1, 2, 4}
}
return ds.Request(ctx, &ZabbixAPIRequest{Method: "item.get", Params: params})
}
func (ds *Zabbix) GetAllApps(ctx context.Context, hostids []string) (*simplejson.Json, error) {
params := ZabbixAPIParams{
"output": "extend",
"hostids": hostids,
}
return ds.Request(ctx, &ZabbixAPIRequest{Method: "application.get", Params: params})
}
func (ds *Zabbix) GetAllHosts(ctx context.Context, groupids []string) (*simplejson.Json, error) {
params := ZabbixAPIParams{
"output": []string{"name", "host"},
"sortfield": "name",
"groupids": groupids,
}
return ds.Request(ctx, &ZabbixAPIRequest{Method: "host.get", Params: params})
}
func (ds *Zabbix) GetAllGroups(ctx context.Context) (*simplejson.Json, error) {
params := ZabbixAPIParams{
"output": []string{"name"},
"sortfield": "name",
"real_hosts": true,
}
return ds.Request(ctx, &ZabbixAPIRequest{Method: "hostgroup.get", Params: params})
}
func isAppMethodNotFoundError(err error) bool {
if err == nil {
return false
}
message := err.Error()
return message == `Method not found. Incorrect API "application".`
}

76
pkg/zabbix/models.go Normal file
View File

@@ -0,0 +1,76 @@
package zabbix
import (
"encoding/json"
"time"
)
type ZabbixDatasourceSettingsDTO struct {
Trends bool `json:"trends"`
TrendsFrom string `json:"trendsFrom"`
TrendsRange string `json:"trendsRange"`
CacheTTL string `json:"cacheTTL"`
Timeout string `json:"timeout"`
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
}
type ZabbixDatasourceSettings struct {
Trends bool
TrendsFrom time.Duration
TrendsRange time.Duration
CacheTTL time.Duration
Timeout time.Duration
DisableReadOnlyUsersAck bool `json:"disableReadOnlyUsersAck"`
}
type ZabbixAPIParams = map[string]interface{}
type ZabbixAPIRequest struct {
Method string `json:"method"`
Params ZabbixAPIParams `json:"params,omitempty"`
}
func (r *ZabbixAPIRequest) String() string {
jsonRequest, _ := json.Marshal(r.Params)
return r.Method + string(jsonRequest)
}
type Items []Item
type Item struct {
ID string `json:"itemid,omitempty"`
Key string `json:"key_,omitempty"`
Name string `json:"name,omitempty"`
ValueType int `json:"value_type,omitempty,string"`
HostID string `json:"hostid,omitempty"`
Hosts []ItemHost `json:"hosts,omitempty"`
Status string `json:"status,omitempty"`
State string `json:"state,omitempty"`
}
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"`
Num string `json:"num,omitempty"`
ValueMin string `json:"value_min,omitempty"`
ValueAvg string `json:"value_avg,omitempty"`
ValueMax string `json:"value_max,omitempty"`
}
type History []HistoryPoint
type HistoryPoint struct {
ItemID string `json:"itemid,omitempty"`
Clock int64 `json:"clock,omitempty,string"`
Value float64 `json:"value,omitempty,string"`
NS int64 `json:"ns,omitempty,string"`
}

64
pkg/zabbix/settings.go Normal file
View File

@@ -0,0 +1,64 @@
package zabbix
import (
"encoding/json"
"errors"
"strconv"
"time"
"github.com/alexanderzobnin/grafana-zabbix/pkg/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
func readZabbixSettings(dsInstanceSettings *backend.DataSourceInstanceSettings) (*ZabbixDatasourceSettings, error) {
zabbixSettingsDTO := &ZabbixDatasourceSettingsDTO{}
err := json.Unmarshal(dsInstanceSettings.JSONData, &zabbixSettingsDTO)
if err != nil {
return nil, err
}
if zabbixSettingsDTO.TrendsFrom == "" {
zabbixSettingsDTO.TrendsFrom = "7d"
}
if zabbixSettingsDTO.TrendsRange == "" {
zabbixSettingsDTO.TrendsRange = "4d"
}
if zabbixSettingsDTO.CacheTTL == "" {
zabbixSettingsDTO.CacheTTL = "1h"
}
if zabbixSettingsDTO.Timeout == "" {
zabbixSettingsDTO.Timeout = "30"
}
trendsFrom, err := gtime.ParseInterval(zabbixSettingsDTO.TrendsFrom)
if err != nil {
return nil, err
}
trendsRange, err := gtime.ParseInterval(zabbixSettingsDTO.TrendsRange)
if err != nil {
return nil, err
}
cacheTTL, err := gtime.ParseInterval(zabbixSettingsDTO.CacheTTL)
if err != nil {
return nil, err
}
timeout, err := strconv.Atoi(zabbixSettingsDTO.Timeout)
if err != nil {
return nil, errors.New("failed to parse timeout: " + err.Error())
}
zabbixSettings := &ZabbixDatasourceSettings{
Trends: zabbixSettingsDTO.Trends,
TrendsFrom: trendsFrom,
TrendsRange: trendsRange,
CacheTTL: cacheTTL,
Timeout: time.Duration(timeout) * time.Second,
}
return zabbixSettings, nil
}

31
pkg/zabbix/testing.go Normal file
View File

@@ -0,0 +1,31 @@
package zabbix
import (
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
func MockZabbixClient(dsInfo *backend.DataSourceInstanceSettings, body string, statusCode int) (*Zabbix, error) {
zabbixAPI, err := zabbixapi.MockZabbixAPI(body, statusCode)
if err != nil {
return nil, err
}
client, err := New(dsInfo, zabbixAPI)
if err != nil {
return nil, err
}
return client, nil
}
func MockZabbixAPI(client *Zabbix, body string, statusCode int) (*Zabbix, error) {
zabbixAPI, err := zabbixapi.MockZabbixAPI(body, statusCode)
if err != nil {
return nil, err
}
client.api = zabbixAPI
return client, nil
}

80
pkg/zabbix/utils.go Normal file
View File

@@ -0,0 +1,80 @@
package zabbix
import (
"fmt"
"regexp"
"strings"
)
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
}
func parseFilter(filter string) (*regexp.Regexp, error) {
regex := regexp.MustCompile(`^/(.+)/(.*)$`)
flagRE := regexp.MustCompile("[imsU]+")
matches := regex.FindStringSubmatch(filter)
if len(matches) <= 1 {
return nil, nil
}
pattern := ""
if matches[2] != "" {
if flagRE.MatchString(matches[2]) {
pattern += "(?" + matches[2] + ")"
} else {
return nil, fmt.Errorf("error parsing regexp: unsupported flags `%s` (expected [imsU])", matches[2])
}
}
pattern += matches[1]
return regexp.Compile(pattern)
}

135
pkg/zabbix/zabbix.go Normal file
View File

@@ -0,0 +1,135 @@
package zabbix
import (
"context"
"strings"
"time"
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi"
"github.com/bitly/go-simplejson"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
)
// Zabbix is a wrapper for Zabbix API. It wraps Zabbix API queries and performs authentication, adds caching,
// deduplication and other performance optimizations.
type Zabbix struct {
api *zabbixapi.ZabbixAPI
dsInfo *backend.DataSourceInstanceSettings
cache *ZabbixCache
logger log.Logger
}
// New returns new instance of Zabbix client.
func New(dsInfo *backend.DataSourceInstanceSettings, zabbixAPI *zabbixapi.ZabbixAPI) (*Zabbix, error) {
logger := log.New()
zabbixSettings, err := readZabbixSettings(dsInfo)
if err != nil {
logger.Error("Error parsing Zabbix settings", "error", err)
return nil, err
}
zabbixCache := NewZabbixCache(zabbixSettings.CacheTTL, 10*time.Minute)
return &Zabbix{
api: zabbixAPI,
dsInfo: dsInfo,
cache: zabbixCache,
logger: logger,
}, nil
}
func (zabbix *Zabbix) GetAPI() *zabbixapi.ZabbixAPI {
return zabbix.api
}
// Request wraps request with cache
func (ds *Zabbix) Request(ctx context.Context, apiReq *ZabbixAPIRequest) (*simplejson.Json, error) {
var resultJson *simplejson.Json
var err error
cachedResult, queryExistInCache := ds.cache.GetAPIRequest(apiReq)
if !queryExistInCache {
resultJson, err = ds.request(ctx, apiReq.Method, apiReq.Params)
if err != nil {
return nil, err
}
if IsCachedRequest(apiReq.Method) {
ds.logger.Debug("Writing result to cache", "method", apiReq.Method)
ds.cache.SetAPIRequest(apiReq, resultJson)
}
} else {
var ok bool
resultJson, ok = cachedResult.(*simplejson.Json)
if !ok {
resultJson = simplejson.New()
}
}
return resultJson, nil
}
// request checks authentication and makes a request to the Zabbix API.
func (zabbix *Zabbix) request(ctx context.Context, method string, params ZabbixAPIParams) (*simplejson.Json, error) {
zabbix.logger.Debug("Zabbix request", "method", method)
// Skip auth for methods that are not required it
if method == "apiinfo.version" {
return zabbix.api.RequestUnauthenticated(ctx, method, params)
}
result, err := zabbix.api.Request(ctx, method, params)
notAuthorized := isNotAuthorized(err)
if err == zabbixapi.ErrNotAuthenticated || notAuthorized {
if notAuthorized {
zabbix.logger.Debug("Authentication token expired, performing re-login")
}
err = zabbix.Login(ctx)
if err != nil {
return nil, err
}
return zabbix.request(ctx, method, params)
} else if err != nil {
return nil, err
}
return result, err
}
func (zabbix *Zabbix) Login(ctx context.Context) error {
jsonData, err := simplejson.NewJson(zabbix.dsInfo.JSONData)
if err != nil {
return err
}
zabbixLogin := jsonData.Get("username").MustString()
var zabbixPassword string
if securePassword, exists := zabbix.dsInfo.DecryptedSecureJSONData["password"]; exists {
zabbixPassword = securePassword
} else {
// Fallback
zabbixPassword = jsonData.Get("password").MustString()
}
err = zabbix.api.Authenticate(ctx, zabbixLogin, zabbixPassword)
if err != nil {
zabbix.logger.Error("Zabbix authentication error", "error", err)
return err
}
zabbix.logger.Debug("Successfully authenticated", "url", zabbix.api.GetUrl().String(), "user", zabbixLogin)
return nil
}
func isNotAuthorized(err error) bool {
if err == nil {
return false
}
message := err.Error()
return strings.Contains(message, "Session terminated, re-login, please.") ||
strings.Contains(message, "Not authorised.") ||
strings.Contains(message, "Not authorized.")
}