Merge branch 'multidatasource-pr' of github.com:dneth/grafana-zabbix into dneth-multidatasource-pr
This commit is contained in:
15
pkg/cache.go
15
pkg/cache.go
@@ -3,8 +3,10 @@ package main
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
@@ -30,9 +32,18 @@ func (c *Cache) Get(request string) (interface{}, bool) {
|
||||
return c.cache.Get(request)
|
||||
}
|
||||
|
||||
// Hash converts the given text string to hash string
|
||||
func Hash(text string) string {
|
||||
// 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))
|
||||
}
|
||||
|
||||
// HashDatasourceInfo converts the given datasource info to hash string
|
||||
func HashDatasourceInfo(dsInfo *datasource.DatasourceInfo) string {
|
||||
digester := sha1.New()
|
||||
if err := json.NewEncoder(digester).Encode(dsInfo); err != nil {
|
||||
panic(err) // This shouldn't be possible but just in case DatasourceInfo changes
|
||||
}
|
||||
return hex.EncodeToString(digester.Sum(nil))
|
||||
}
|
||||
|
||||
@@ -1,3 +1,68 @@
|
||||
package main
|
||||
|
||||
// Dummy test file for now
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
func TestHashDatasourceInfo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
dsInfo *datasource.DatasourceInfo
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Normal Datasource Info",
|
||||
dsInfo: &datasource.DatasourceInfo{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
Name: "Zabbix",
|
||||
Type: "alexanderzobnin-zabbix-datasource",
|
||||
Url: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JsonData: "{}",
|
||||
DecryptedSecureJsonData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
want: "ed161f89179c46d9a578e4d7e92ff95444222e0a",
|
||||
},
|
||||
// Can't find a case where the input causes the encoder to fail
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := HashDatasourceInfo(tt.dsInfo)
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHashDatasourceInfo(b *testing.B) {
|
||||
benches := []struct {
|
||||
name string
|
||||
dsInfo *datasource.DatasourceInfo
|
||||
}{
|
||||
{
|
||||
"Normal Datasource Info",
|
||||
&datasource.DatasourceInfo{
|
||||
Id: 4,
|
||||
OrgId: 6,
|
||||
Name: "MyZabbixDatasource",
|
||||
Type: "alexanderzobnin-zabbix-datasource",
|
||||
Url: "https://localhost:3306/zabbix/api_jsonrpc.php",
|
||||
JsonData: `{ "addThresholds": true, "disableReadOnlyUsersAck": true }`,
|
||||
DecryptedSecureJsonData: map[string]string{
|
||||
"username": "grafanaZabbixUser",
|
||||
"password": "$uper$ecr3t!!!",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, bt := range benches {
|
||||
b.Run(bt.name, func(b *testing.B) {
|
||||
HashDatasourceInfo(bt.dsInfo)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
simplejson "github.com/bitly/go-simplejson"
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
@@ -10,39 +12,95 @@ import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type ZabbixDatasource struct {
|
||||
// ZabbixBackend implements the Grafana backend interface and forwards queries to the ZabbixDatasource
|
||||
type ZabbixBackend struct {
|
||||
plugin.NetRPCUnsupportedPlugin
|
||||
logger hclog.Logger
|
||||
logger hclog.Logger
|
||||
datasourceCache *Cache
|
||||
}
|
||||
|
||||
func (ds *ZabbixDatasource) Query(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
|
||||
func (b *ZabbixBackend) newZabbixDatasource() *ZabbixDatasource {
|
||||
ds := NewZabbixDatasource()
|
||||
ds.logger = b.logger
|
||||
return ds
|
||||
}
|
||||
|
||||
// Query receives requests from the Grafana backend. Requests are filtered by query type and sent to the
|
||||
// applicable ZabbixDatasource.
|
||||
func (b *ZabbixBackend) Query(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
|
||||
zabbixDs := b.getCachedDatasource(tsdbReq)
|
||||
|
||||
queryType, err := GetQueryType(tsdbReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dsInfo := tsdbReq.GetDatasource()
|
||||
ds.logger.Debug("createRequest", "dsInfo", dsInfo)
|
||||
|
||||
ds.logger.Debug("createRequest", "queryType", queryType)
|
||||
|
||||
switch queryType {
|
||||
case "zabbixAPI":
|
||||
return ds.ZabbixAPIQuery(ctx, tsdbReq)
|
||||
return zabbixDs.ZabbixAPIQuery(ctx, tsdbReq)
|
||||
case "connectionTest":
|
||||
return zabbixDs.TestConnection(ctx, tsdbReq)
|
||||
default:
|
||||
return nil, errors.New("Query is not implemented yet")
|
||||
err = errors.New("Query not implemented")
|
||||
return BuildErrorResponse(err), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *ZabbixBackend) getCachedDatasource(tsdbReq *datasource.DatasourceRequest) *ZabbixDatasource {
|
||||
dsInfoHash := HashDatasourceInfo(tsdbReq.GetDatasource())
|
||||
|
||||
if cachedData, ok := b.datasourceCache.Get(dsInfoHash); ok {
|
||||
if cachedDS, ok := cachedData.(*ZabbixDatasource); ok {
|
||||
return cachedDS
|
||||
}
|
||||
}
|
||||
|
||||
if b.logger.IsDebug() {
|
||||
dsInfo := tsdbReq.GetDatasource()
|
||||
b.logger.Debug(fmt.Sprintf("Datasource cache miss (Org %d Id %d '%s' %s)", dsInfo.GetOrgId(), dsInfo.GetId(), dsInfo.GetName(), dsInfoHash))
|
||||
}
|
||||
return b.newZabbixDatasource()
|
||||
}
|
||||
|
||||
// GetQueryType determines the query type from a query or list of queries
|
||||
func GetQueryType(tsdbReq *datasource.DatasourceRequest) (string, error) {
|
||||
queryType := "query"
|
||||
if len(tsdbReq.Queries) > 0 {
|
||||
firstQuery := tsdbReq.Queries[0]
|
||||
queryJson, err := simplejson.NewJson([]byte(firstQuery.ModelJson))
|
||||
queryJSON, err := simplejson.NewJson([]byte(firstQuery.ModelJson))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
queryType = queryJson.Get("queryType").MustString("query")
|
||||
queryType = queryJSON.Get("queryType").MustString("query")
|
||||
}
|
||||
return queryType, nil
|
||||
}
|
||||
|
||||
// BuildResponse transforms a Zabbix API response to a DatasourceResponse
|
||||
func BuildResponse(responseData interface{}) (*datasource.DatasourceResponse, error) {
|
||||
jsonBytes, err := json.Marshal(responseData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &datasource.DatasourceResponse{
|
||||
Results: []*datasource.QueryResult{
|
||||
&datasource.QueryResult{
|
||||
RefId: "zabbixAPI",
|
||||
MetaJson: string(jsonBytes),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// BuildErrorResponse creates a QueryResult that forwards an error to the front-end
|
||||
func BuildErrorResponse(err error) *datasource.DatasourceResponse {
|
||||
return &datasource.DatasourceResponse{
|
||||
Results: []*datasource.QueryResult{
|
||||
&datasource.QueryResult{
|
||||
RefId: "zabbixAPI",
|
||||
Error: err.Error(),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
143
pkg/datasource_test.go
Normal file
143
pkg/datasource_test.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
simplejson "github.com/bitly/go-simplejson"
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
"gotest.tools/assert"
|
||||
"gotest.tools/assert/cmp"
|
||||
)
|
||||
|
||||
func TestZabbixBackend_getCachedDatasource(t *testing.T) {
|
||||
basicDatasourceInfo := &datasource.DatasourceInfo{
|
||||
Id: 1,
|
||||
Name: "TestDatasource",
|
||||
}
|
||||
basicDatasourceInfoHash := HashDatasourceInfo(basicDatasourceInfo)
|
||||
|
||||
modifiedDatasource := NewZabbixDatasource()
|
||||
modifiedDatasource.authToken = "AB404F1234"
|
||||
|
||||
altDatasourceInfo := &datasource.DatasourceInfo{
|
||||
Id: 2,
|
||||
Name: "AnotherDatasource",
|
||||
}
|
||||
altDatasourceInfoHash := HashDatasourceInfo(altDatasourceInfo)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cache *cache.Cache
|
||||
request *datasource.DatasourceRequest
|
||||
want *ZabbixDatasource
|
||||
}{
|
||||
{
|
||||
name: "Uncached Datasource (nothing in cache)",
|
||||
request: &datasource.DatasourceRequest{
|
||||
Datasource: basicDatasourceInfo,
|
||||
},
|
||||
want: NewZabbixDatasource(),
|
||||
},
|
||||
{
|
||||
name: "Uncached Datasource (cache miss)",
|
||||
cache: cache.NewFrom(cache.NoExpiration, cache.NoExpiration, map[string]cache.Item{
|
||||
basicDatasourceInfoHash: cache.Item{Object: modifiedDatasource},
|
||||
}),
|
||||
request: &datasource.DatasourceRequest{
|
||||
Datasource: altDatasourceInfo,
|
||||
},
|
||||
want: NewZabbixDatasource(),
|
||||
},
|
||||
{
|
||||
name: "Cached Datasource",
|
||||
cache: cache.NewFrom(cache.NoExpiration, cache.NoExpiration, map[string]cache.Item{
|
||||
altDatasourceInfoHash: cache.Item{Object: NewZabbixDatasource()},
|
||||
basicDatasourceInfoHash: cache.Item{Object: modifiedDatasource},
|
||||
}),
|
||||
request: &datasource.DatasourceRequest{
|
||||
Datasource: basicDatasourceInfo,
|
||||
},
|
||||
want: modifiedDatasource,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.cache == nil {
|
||||
tt.cache = cache.New(cache.NoExpiration, cache.NoExpiration)
|
||||
}
|
||||
b := &ZabbixBackend{
|
||||
logger: hclog.New(&hclog.LoggerOptions{
|
||||
Name: "TestZabbixBackend_getCachedDatasource",
|
||||
Level: hclog.LevelFromString("DEBUG"),
|
||||
}),
|
||||
datasourceCache: &Cache{cache: tt.cache},
|
||||
}
|
||||
got := b.getCachedDatasource(tt.request)
|
||||
|
||||
// Only checking the authToken, being the easiest value to, and guarantee equality for
|
||||
assert.Equal(t, tt.want.authToken, got.authToken)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildResponse(t *testing.T) {
|
||||
jsonData := simplejson.New()
|
||||
jsonData.Set("testing", []int{5, 12, 75})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
responseData interface{}
|
||||
want *datasource.DatasourceResponse
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "simplejson Response",
|
||||
responseData: jsonData,
|
||||
want: &datasource.DatasourceResponse{
|
||||
Results: []*datasource.QueryResult{
|
||||
&datasource.QueryResult{
|
||||
RefId: "zabbixAPI",
|
||||
MetaJson: `{"testing":[5,12,75]}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Connetion Status Response",
|
||||
responseData: connectionTestResponse{
|
||||
ZabbixVersion: "2.4",
|
||||
DbConnectorStatus: &dbConnectionStatus{
|
||||
DsType: "mysql",
|
||||
DsName: "MyDatabase",
|
||||
},
|
||||
},
|
||||
want: &datasource.DatasourceResponse{
|
||||
Results: []*datasource.QueryResult{
|
||||
&datasource.QueryResult{
|
||||
RefId: "zabbixAPI",
|
||||
MetaJson: `{"zabbixVersion":"2.4","dbConnectorStatus":{"dsType":"mysql","dsName":"MyDatabase"}}`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Unmarshalable",
|
||||
responseData: 2i,
|
||||
wantErr: "json: unsupported type: complex128",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := BuildResponse(tt.responseData)
|
||||
if tt.wantErr != "" {
|
||||
assert.Error(t, err, tt.wantErr)
|
||||
assert.Assert(t, cmp.Nil(got))
|
||||
return
|
||||
}
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, got, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1 +1,11 @@
|
||||
package main
|
||||
|
||||
type connectionTestResponse struct {
|
||||
ZabbixVersion string `json:"zabbixVersion"`
|
||||
DbConnectorStatus *dbConnectionStatus `json:"dbConnectorStatus"`
|
||||
}
|
||||
|
||||
type dbConnectionStatus struct {
|
||||
DsType string `json:"dsType"`
|
||||
DsName string `json:"dsName"`
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
plugin "github.com/hashicorp/go-plugin"
|
||||
@@ -22,8 +24,9 @@ func main() {
|
||||
MagicCookieValue: "datasource",
|
||||
},
|
||||
Plugins: map[string]plugin.Plugin{
|
||||
"zabbix-backend-datasource": &datasource.DatasourcePluginImpl{Plugin: &ZabbixDatasource{
|
||||
logger: pluginLogger,
|
||||
"zabbix-backend-datasource": &datasource.DatasourcePluginImpl{Plugin: &ZabbixBackend{
|
||||
datasourceCache: NewCache(10*time.Minute, 10*time.Minute),
|
||||
logger: pluginLogger,
|
||||
}},
|
||||
},
|
||||
|
||||
|
||||
@@ -15,35 +15,47 @@ import (
|
||||
|
||||
simplejson "github.com/bitly/go-simplejson"
|
||||
"github.com/grafana/grafana_plugin_model/go/datasource"
|
||||
hclog "github.com/hashicorp/go-hclog"
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/net/context/ctxhttp"
|
||||
)
|
||||
|
||||
var httpClient = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
Renegotiation: tls.RenegotiateFreelyAsClient,
|
||||
},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
DualStack: true,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
},
|
||||
Timeout: time.Duration(time.Second * 30),
|
||||
// ZabbixDatasource stores state about a specific datasource and provides methods to make
|
||||
// requests to the Zabbix API
|
||||
type ZabbixDatasource struct {
|
||||
queryCache *Cache
|
||||
logger hclog.Logger
|
||||
httpClient *http.Client
|
||||
authToken string
|
||||
}
|
||||
|
||||
var queryCache = NewCache(10*time.Minute, 10*time.Minute)
|
||||
|
||||
var zabbixAuth string = ""
|
||||
// NewZabbixDatasource returns an initialized ZabbixDatasource
|
||||
func NewZabbixDatasource() *ZabbixDatasource {
|
||||
return &ZabbixDatasource{
|
||||
queryCache: NewCache(10*time.Minute, 10*time.Minute),
|
||||
httpClient: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
Renegotiation: tls.RenegotiateFreelyAsClient,
|
||||
},
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: 30 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
MaxIdleConns: 100,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
},
|
||||
Timeout: time.Duration(time.Second * 30),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ZabbixAPIQuery handles query requests to Zabbix
|
||||
func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
|
||||
result, queryExistInCache := queryCache.Get(Hash(tsdbReq.String()))
|
||||
result, queryExistInCache := ds.queryCache.Get(HashString(tsdbReq.String()))
|
||||
|
||||
if !queryExistInCache {
|
||||
dsInfo := tsdbReq.GetDatasource()
|
||||
@@ -52,7 +64,7 @@ func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasou
|
||||
for _, query := range tsdbReq.Queries {
|
||||
json, err := simplejson.NewJson([]byte(query.ModelJson))
|
||||
apiMethod := json.GetPath("target", "method").MustString()
|
||||
apiParams := json.GetPath("target", "params")
|
||||
apiParams := json.GetPath("target", "params").MustMap()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -69,11 +81,11 @@ func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasou
|
||||
|
||||
jsonQuery := jsonQueries[0].Get("target")
|
||||
apiMethod := jsonQuery.Get("method").MustString()
|
||||
apiParams := jsonQuery.Get("params")
|
||||
apiParams := jsonQuery.Get("params").MustMap()
|
||||
|
||||
var err error
|
||||
result, err = ds.ZabbixRequest(ctx, dsInfo, apiMethod, apiParams)
|
||||
queryCache.Set(Hash(tsdbReq.String()), result)
|
||||
response, err := ds.ZabbixRequest(ctx, dsInfo, apiMethod, apiParams)
|
||||
ds.queryCache.Set(HashString(tsdbReq.String()), response)
|
||||
result = response
|
||||
if err != nil {
|
||||
ds.logger.Debug("ZabbixAPIQuery", "error", err)
|
||||
return nil, errors.New("ZabbixAPIQuery is not implemented yet")
|
||||
@@ -83,44 +95,55 @@ func (ds *ZabbixDatasource) ZabbixAPIQuery(ctx context.Context, tsdbReq *datasou
|
||||
resultByte, _ := result.(*simplejson.Json).MarshalJSON()
|
||||
ds.logger.Debug("ZabbixAPIQuery", "result", string(resultByte))
|
||||
|
||||
return ds.BuildResponse(result.(*simplejson.Json))
|
||||
return BuildResponse(result)
|
||||
}
|
||||
|
||||
func (ds *ZabbixDatasource) BuildResponse(result *simplejson.Json) (*datasource.DatasourceResponse, error) {
|
||||
resultByte, err := result.MarshalJSON()
|
||||
// TestConnection checks authentication and version of the Zabbix API and returns that info
|
||||
func (ds *ZabbixDatasource) TestConnection(ctx context.Context, tsdbReq *datasource.DatasourceRequest) (*datasource.DatasourceResponse, error) {
|
||||
dsInfo := tsdbReq.GetDatasource()
|
||||
|
||||
auth, err := ds.loginWithDs(ctx, dsInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return BuildErrorResponse(fmt.Errorf("Authentication failed: %w", err)), nil
|
||||
}
|
||||
ds.authToken = auth
|
||||
|
||||
response, err := ds.zabbixAPIRequest(ctx, dsInfo.GetUrl(), "apiinfo.version", map[string]interface{}{}, "")
|
||||
if err != nil {
|
||||
ds.logger.Debug("TestConnection", "error", err)
|
||||
return BuildErrorResponse(fmt.Errorf("Version check failed: %w", err)), nil
|
||||
}
|
||||
|
||||
return &datasource.DatasourceResponse{
|
||||
Results: []*datasource.QueryResult{
|
||||
&datasource.QueryResult{
|
||||
RefId: "zabbixAPI",
|
||||
MetaJson: string(resultByte),
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
resultByte, _ := response.MarshalJSON()
|
||||
ds.logger.Debug("TestConnection", "result", string(resultByte))
|
||||
|
||||
testResponse := connectionTestResponse{
|
||||
ZabbixVersion: response.MustString(),
|
||||
}
|
||||
|
||||
return BuildResponse(testResponse)
|
||||
}
|
||||
|
||||
func (ds *ZabbixDatasource) ZabbixRequest(ctx context.Context, dsInfo *datasource.DatasourceInfo, method string, params *simplejson.Json) (*simplejson.Json, error) {
|
||||
// ZabbixRequest checks authentication and makes a request to the Zabbix API
|
||||
func (ds *ZabbixDatasource) ZabbixRequest(ctx context.Context, dsInfo *datasource.DatasourceInfo, method string, params map[string]interface{}) (*simplejson.Json, error) {
|
||||
zabbixURL := dsInfo.GetUrl()
|
||||
|
||||
var result *simplejson.Json
|
||||
var err error
|
||||
|
||||
for attempt := 0; attempt <= 3; attempt++ {
|
||||
if zabbixAuth == "" {
|
||||
if ds.authToken == "" {
|
||||
// Authenticate
|
||||
zabbixAuth, err = ds.loginWithDs(ctx, dsInfo)
|
||||
ds.authToken, err = ds.loginWithDs(ctx, dsInfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
result, err = ds.zabbixAPIRequest(ctx, zabbixURL, method, params, zabbixAuth)
|
||||
result, err = ds.zabbixAPIRequest(ctx, zabbixURL, method, params, ds.authToken)
|
||||
if err == nil || (err != nil && !isNotAuthorized(err.Error())) {
|
||||
break
|
||||
} else {
|
||||
zabbixAuth = ""
|
||||
ds.authToken = ""
|
||||
}
|
||||
}
|
||||
return result, err
|
||||
@@ -162,12 +185,7 @@ func (ds *ZabbixDatasource) login(ctx context.Context, apiURL string, username s
|
||||
"user": username,
|
||||
"password": password,
|
||||
}
|
||||
paramsJSON, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
data, _ := simplejson.NewJson(paramsJSON)
|
||||
auth, err := ds.zabbixAPIRequest(ctx, apiURL, "user.login", data, "")
|
||||
auth, err := ds.zabbixAPIRequest(ctx, apiURL, "user.login", params, "")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -175,7 +193,7 @@ func (ds *ZabbixDatasource) login(ctx context.Context, apiURL string, username s
|
||||
return auth.MustString(), nil
|
||||
}
|
||||
|
||||
func (ds *ZabbixDatasource) zabbixAPIRequest(ctx context.Context, apiURL string, method string, params *simplejson.Json, auth string) (*simplejson.Json, error) {
|
||||
func (ds *ZabbixDatasource) zabbixAPIRequest(ctx context.Context, apiURL string, method string, params map[string]interface{}, auth string) (*simplejson.Json, error) {
|
||||
zabbixURL, err := url.Parse(apiURL)
|
||||
|
||||
// TODO: inject auth token (obtain from 'user.login' first)
|
||||
@@ -211,7 +229,7 @@ func (ds *ZabbixDatasource) zabbixAPIRequest(ctx context.Context, apiURL string,
|
||||
Body: rc,
|
||||
}
|
||||
|
||||
response, err := makeHTTPRequest(ctx, req)
|
||||
response, err := makeHTTPRequest(ctx, ds.httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -234,7 +252,7 @@ func handleAPIResult(response []byte) (*simplejson.Json, error) {
|
||||
return jsonResult, nil
|
||||
}
|
||||
|
||||
func makeHTTPRequest(ctx context.Context, req *http.Request) ([]byte, error) {
|
||||
func makeHTTPRequest(ctx context.Context, httpClient *http.Client, req *http.Request) ([]byte, error) {
|
||||
res, err := ctxhttp.Do(ctx, httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
1
pkg/zabbix_api_test.go
Normal file
1
pkg/zabbix_api_test.go
Normal file
@@ -0,0 +1 @@
|
||||
package main
|
||||
Reference in New Issue
Block a user