Zabbix API refactoring
This commit is contained in:
8
pkg/zabbixapi/models.go
Normal file
8
pkg/zabbixapi/models.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package zabbixapi
|
||||
|
||||
type ZabbixAPIRequest struct {
|
||||
Method string `json:"method"`
|
||||
Params ZabbixAPIParams `json:"params,omitempty"`
|
||||
}
|
||||
|
||||
type ZabbixAPIParams = map[string]interface{}
|
||||
199
pkg/zabbixapi/zabbix_api.go
Normal file
199
pkg/zabbixapi/zabbix_api.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package zabbixapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/bitly/go-simplejson"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
"golang.org/x/net/context/ctxhttp"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotAuthenticated = errors.New("zabbix api: not authenticated")
|
||||
)
|
||||
|
||||
type ZabbixAPI struct {
|
||||
url *url.URL
|
||||
httpClient *http.Client
|
||||
logger log.Logger
|
||||
auth string
|
||||
}
|
||||
|
||||
func newHttpClient() *http.Client {
|
||||
return &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),
|
||||
}
|
||||
}
|
||||
|
||||
// New returns new ZabbixAPI instance initialized with given URL or error.
|
||||
func New(api_url string) (*ZabbixAPI, error) {
|
||||
apiLogger := log.New()
|
||||
zabbixURL, err := url.Parse(api_url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ZabbixAPI{
|
||||
url: zabbixURL,
|
||||
logger: apiLogger,
|
||||
httpClient: newHttpClient(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetUrl gets new API URL
|
||||
func (api *ZabbixAPI) GetUrl() *url.URL {
|
||||
return api.url
|
||||
}
|
||||
|
||||
// SetUrl sets new API URL
|
||||
func (api *ZabbixAPI) SetUrl(api_url string) error {
|
||||
zabbixURL, err := url.Parse(api_url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
api.url = zabbixURL
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetAuth sets API authentication token
|
||||
func (api *ZabbixAPI) SetAuth(auth string) {
|
||||
api.auth = auth
|
||||
}
|
||||
|
||||
// Request performs API request
|
||||
func (api *ZabbixAPI) Request(ctx context.Context, method string, params ZabbixAPIParams) (*simplejson.Json, error) {
|
||||
if api.auth == "" {
|
||||
return nil, ErrNotAuthenticated
|
||||
}
|
||||
|
||||
return api.request(ctx, method, params, api.auth)
|
||||
}
|
||||
|
||||
// Request performs API request without authentication token
|
||||
func (api *ZabbixAPI) RequestUnauthenticated(ctx context.Context, method string, params ZabbixAPIParams) (*simplejson.Json, error) {
|
||||
return api.request(ctx, method, params, "")
|
||||
}
|
||||
|
||||
func (api *ZabbixAPI) request(ctx context.Context, method string, params ZabbixAPIParams, auth string) (*simplejson.Json, error) {
|
||||
apiRequest := map[string]interface{}{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2,
|
||||
"method": method,
|
||||
"params": params,
|
||||
}
|
||||
|
||||
if auth != "" {
|
||||
apiRequest["auth"] = auth
|
||||
}
|
||||
|
||||
reqBodyJSON, err := json.Marshal(apiRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var body io.Reader
|
||||
body = bytes.NewReader(reqBodyJSON)
|
||||
rc, ok := body.(io.ReadCloser)
|
||||
if !ok && body != nil {
|
||||
rc = ioutil.NopCloser(body)
|
||||
}
|
||||
|
||||
req := &http.Request{
|
||||
Method: "POST",
|
||||
URL: api.url,
|
||||
Header: map[string][]string{
|
||||
"Content-Type": {"application/json"},
|
||||
},
|
||||
Body: rc,
|
||||
}
|
||||
|
||||
response, err := makeHTTPRequest(ctx, api.httpClient, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return handleAPIResult(response)
|
||||
}
|
||||
|
||||
// Login performs API authentication and returns authentication token.
|
||||
func (api *ZabbixAPI) Login(ctx context.Context, username string, password string) (string, error) {
|
||||
params := ZabbixAPIParams{
|
||||
"user": username,
|
||||
"password": password,
|
||||
}
|
||||
|
||||
auth, err := api.request(ctx, "user.login", params, "")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return auth.MustString(), nil
|
||||
}
|
||||
|
||||
// Authenticate performs API authentication and sets authentication token.
|
||||
func (api *ZabbixAPI) Authenticate(ctx context.Context, username string, password string) error {
|
||||
auth, err := api.Login(ctx, username, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
api.SetAuth(auth)
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleAPIResult(response []byte) (*simplejson.Json, error) {
|
||||
jsonResp, err := simplejson.NewJson([]byte(response))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if errJSON, isError := jsonResp.CheckGet("error"); isError {
|
||||
errMessage := fmt.Sprintf("%s %s", errJSON.Get("message").MustString(), errJSON.Get("data").MustString())
|
||||
return nil, errors.New(errMessage)
|
||||
}
|
||||
jsonResult := jsonResp.Get("result")
|
||||
return jsonResult, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("invalid status code. status: %v", res.Status)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
Reference in New Issue
Block a user