Skip to content

Commit

Permalink
Merge pull request #8 from miraclelinux/develop/fix-deprecated-login-…
Browse files Browse the repository at this point in the history
…parameter

Update Zabbix login API calling process to over 5.4
  • Loading branch information
user340 committed Oct 28, 2022
2 parents 2a49475 + f6b4a0b commit 0dee953
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0dee953

Please sign in to comment.