From 516b4f20451e84924f0189243493c6a9c608f850 Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Thu, 4 Jun 2020 17:53:10 +0300 Subject: [PATCH] fix expired token renewal --- pkg/datasource/zabbix.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/datasource/zabbix.go b/pkg/datasource/zabbix.go index c2c3382..d8032c4 100644 --- a/pkg/datasource/zabbix.go +++ b/pkg/datasource/zabbix.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "regexp" + "strings" "time" "github.com/alexanderzobnin/grafana-zabbix/pkg/zabbixapi" @@ -96,7 +97,7 @@ func (ds *ZabbixDatasourceInstance) ZabbixRequest(ctx context.Context, method st } result, err = ds.zabbixAPI.Request(ctx, method, params) - if err == zabbixapi.ErrNotAuthenticated { + if err == zabbixapi.ErrNotAuthenticated || isNotAuthorized(err) { err = ds.login(ctx) if err != nil { return nil, err @@ -530,3 +531,14 @@ func parseFilter(filter string) (*regexp.Regexp, error) { return regexp.Compile(pattern) } + +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.") +}