From f6b4a0bcc128b51173e447062feb0733376fc8b5 Mon Sep 17 00:00:00 2001 From: Yuuki Enomoto Date: Mon, 5 Sep 2022 14:35:24 +0900 Subject: [PATCH] Update Zabbix login API calling process to over 5.4 Since Zabbix 5.4, "user.login" parameter "user" is changed to "username". If POST request with "user" parameter, Zabbix Web frontend show the following error in error log of Web server. Parameter "/user" is deprecated. --- session.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/session.go b/session.go index 6ebe0d5..f3103fb 100644 --- a/session.go +++ b/session.go @@ -7,11 +7,20 @@ import ( "fmt" "io/ioutil" "net/http" + "strconv" ) // ErrNotFound describes an empty result set for an API call. var ErrNotFound = errors.New("No results were found matching the given search parameters") +// LoginParam is used for user.login API. +// Use Username in over Zabbix 5.4, otherwise use User for username. +type LoginParam struct { + User string `json:"user,omitempty"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` +} + // A Session is an authenticated Zabbix JSON-RPC API client. It must be // initialized and connected with NewSession. type Session struct { @@ -45,16 +54,24 @@ func NewSession(url string, username string, password string) (session *Session, func (c *Session) login(username, password string) error { // get Zabbix API version - _, err := c.GetVersion() + v, err := c.GetVersion() if err != nil { return fmt.Errorf("Failed to retrieve Zabbix API version: %v", err) } + version, err := strconv.ParseFloat(v[0:3], 64) + if err != nil { + return fmt.Errorf("Failed to convert Zabbix version string to float: %v", err) + } + // login to API - params := map[string]string{ - "user": username, - "password": password, + var params LoginParam + if version > 5.2 { + params.Username = username + } else { + params.User = username } + params.Password = password res, err := c.Do(NewRequest("user.login", params)) if err != nil {