Skip to content

Commit 59ea065

Browse files
committed
Merge branch 'zabbix-7.2' into develop
2 parents b887cea + e3418e1 commit 59ea065

File tree

13 files changed

+63
-28
lines changed

13 files changed

+63
-28
lines changed

.envrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export ZBX_VERSION="6.4"
1+
export ZBX_VERSION="7.2"
22
export ZBX_VERSION_SUFFIX="-latest"
33
export ZBX_DEBUG=1

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ jobs:
5353
- version: "7.0"
5454
suffix: "-latest"
5555
postgress_version: "16-alpine"
56+
- version: "7.2"
57+
suffix: "-latest"
58+
postgress_version: "16-alpine"
5659

5760
name: "Zabbix ${{ matrix.zabbix.version }} Integration Tests"
5861

client_builder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package zabbix_test
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66
"testing"
77

88
"github.com/fabiang/go-zabbix"
@@ -22,7 +22,7 @@ func init() {
2222
}
2323

2424
func prepareTemporaryDir(t *testing.T) (dir string, success bool) {
25-
tempDir, err := ioutil.TempDir("", "zabbix-session-test")
25+
tempDir, err := os.MkdirTemp("", "zabbix-session-test")
2626

2727
if err != nil {
2828
t.Fatalf("cannot create a temporary dir for session cache: %v", err)

event.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,14 @@ type EventGetParams struct {
200200

201201
// SelectAlerts causes Alerts generated by each Event to be attached in the
202202
// search results.
203+
//
204+
// Deprecated: Depcreated since Zabbix 6.4 and removed in Zabbix 7.2
203205
SelectAlerts SelectQuery `json:"select_alerts,omitempty"`
204206

205207
// SelectAcknowledgements causes Acknowledgments for each Event to be
206208
// attached in the search results in reverse chronological order.
209+
//
210+
// Deprecated: Depcreated since Zabbix 6.4 and removed in Zabbix 7.2
207211
SelectAcknowledgements SelectQuery `json:"select_acknowledges,omitempty"`
208212
}
209213

file_cache.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package zabbix
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"time"
98
)
@@ -12,14 +11,15 @@ import (
1211
cachedSessionData represents a model of cached session.
1312
1413
Example:
15-
{
16-
"createdAt": 1530056885,
17-
"session": {
18-
"url": "...",
19-
"token": "...",
20-
"apiVersion": "..."
14+
15+
{
16+
"createdAt": 1530056885,
17+
"session": {
18+
"url": "...",
19+
"token": "...",
20+
"apiVersion": "..."
21+
}
2122
}
22-
}
2323
*/
2424
type cachedSessionContainer struct {
2525
CreatedAt int64 `json:"createdAt"`
@@ -63,12 +63,12 @@ func (c *SessionFileCache) SaveSession(session *Session) error {
6363
return err
6464
}
6565

66-
return ioutil.WriteFile(c.filePath, []byte(serialized), os.FileMode(c.filePermissions))
66+
return os.WriteFile(c.filePath, []byte(serialized), os.FileMode(c.filePermissions))
6767
}
6868

6969
// GetSession returns cached Zabbix session
7070
func (c *SessionFileCache) GetSession() (*Session, error) {
71-
contents, err := ioutil.ReadFile(c.filePath)
71+
contents, err := os.ReadFile(c.filePath)
7272

7373
if err != nil {
7474
return nil, err

host.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ type HostGetParams struct {
159159

160160
// SelectGroups causes the Host Groups that each Host belongs to to be
161161
// attached in the search results.
162+
//
163+
// Deprecated: Depcreated since Zabbix 6.4 and removed in Zabbix 7.2
162164
SelectGroups SelectQuery `json:"selectGroups,omitempty"`
163165

164166
// SelectApplications causes the Applications from each Host to be attached
@@ -217,7 +219,7 @@ func (c *Session) CountHosts(params HostGetParams) (int, error) {
217219
params.GetParameters.CountOutput = true
218220

219221
req := NewRequest("host.get", &params)
220-
resp, err := c.Do(req)
222+
resp, err := c.Do(req, false)
221223
if err != nil {
222224
return 0, err
223225
}

host_interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,6 @@ func (c *Session) UpdateHostInterface(inter HostInterface) ([]string, error) {
104104

105105
func (c *Session) DeleteHostInterface(inter HostInterface) (err error) {
106106
req := NewRequest("hostinterface.delete", []string{inter.InterfaceID})
107-
_, err = c.Do(req)
107+
_, err = c.Do(req, false)
108108
return
109109
}

hostgroup.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ type HostgroupGetParams struct {
4040
MaintenanceIDs []string `json:"maintenanceids,omitempty"`
4141

4242
// Return only host groups that contain monitored hosts
43+
//
44+
// Deprecated: Depcreated since Zabbix 6.4 and removed in Zabbix 7.2
4345
MonitoredHosts int `json:"monitored_hosts,omitempty"`
4446

4547
// Return only host groups that contain hosts
48+
//
49+
// Deprecated: Depcreated since Zabbix 6.4 and removed in Zabbix 7.2
4650
RealHosts int `json:"real_hosts,omitempty"`
4751

4852
// Return only host groups that contain templates

maintenance.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@ type MaintenanceGetParams struct {
4949
SelectHosts SelectQuery `json:"selectHosts,omitempty"`
5050

5151
// Return host groups assigned to the maintenance in the groups property.
52+
// Deprecated: Depcreated since Zabbix 6.4 and removed in Zabbix 7.2
5253
SelectGroups SelectQuery `json:"selectGroups,omitempty"`
5354

5455
// Return only maintenances with the given IDs.
5556
Maintenanceids []string `json:"maintenanceids,omitempty"`
5657

5758
// Return only maintenances that are assigned to the given hosts.
59+
//
60+
// Deprecated: Depcreated since Zabbix 6.4 and removed in Zabbix 7.2
5861
Hostids []string `json:"hostids,omitempty"`
5962

6063
// Return only maintenances that are assigned to the given host groups.
64+
//
65+
// Deprecated: Depcreated since Zabbix 6.4 and removed in Zabbix 7.2
6166
Groupids []string `json:"groupids,omitempty"`
6267
}
6368

session.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99

1010
"github.com/fabiang/go-zabbix/types"
@@ -14,10 +14,12 @@ import (
1414
var (
1515
ErrNotFound = &NotFoundError{"No results were found matching the given search parameters"}
1616
zabbixVersion600 *types.ZBXVersion
17+
zabbixVersion640 *types.ZBXVersion
1718
)
1819

1920
func init() {
2021
zabbixVersion600, _ = types.NewZBXVersion("6.0.0")
22+
zabbixVersion640, _ = types.NewZBXVersion("6.4.0")
2123
}
2224

2325
// A Session is an authenticated Zabbix JSON-RPC API client. It must be
@@ -70,7 +72,7 @@ func (c *Session) login(username, password string) error {
7072
params["username"] = username
7173
}
7274

73-
res, err := c.Do(NewRequest("user.login", params))
75+
res, err := c.Do(NewRequest("user.login", params), true)
7476
if err != nil {
7577
return fmt.Errorf("Error logging in to Zabbix API: %v", err)
7678
}
@@ -87,7 +89,7 @@ func (c *Session) login(username, password string) error {
8789
func (c *Session) GetVersion() (*types.ZBXVersion, error) {
8890
if c.APIVersion == nil {
8991
// get Zabbix API version
90-
res, err := c.Do(NewRequest("apiinfo.version", nil))
92+
res, err := c.Do(NewRequest("apiinfo.version", nil), true)
9193
if err != nil {
9294
return nil, err
9395
}
@@ -116,9 +118,21 @@ func (c *Session) AuthToken() string {
116118
// When err is nil, resp always contains a non-nil resp.Body.
117119
//
118120
// Generally Get or a wrapper function will be used instead of Do.
119-
func (c *Session) Do(req *Request) (resp *Response, err error) {
120-
// configure request
121-
req.AuthToken = c.Token
121+
func (c *Session) Do(req *Request, noAuthRequired bool) (resp *Response, err error) {
122+
if noAuthRequired == false {
123+
// get Zabbix API version
124+
ver, err := c.GetVersion()
125+
if err != nil {
126+
return nil, fmt.Errorf("Failed to retrieve Zabbix API version: %v", err)
127+
}
128+
129+
// Zabbix 6.4 uses `Authorization` header, therefore "auth" parameter
130+
// has been deprecated and was removed in 7.2
131+
// See: https://www.zabbix.com/documentation/7.2/en/manual/api/changes
132+
if ver.Compare(zabbixVersion640) < 0 {
133+
req.AuthToken = c.Token
134+
}
135+
}
122136

123137
// encode request as json
124138
b, err := json.Marshal(req)
@@ -135,6 +149,9 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
135149
}
136150
r.ContentLength = int64(len(b))
137151
r.Header.Add("Content-Type", "application/json-rpc")
152+
if noAuthRequired == false {
153+
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token))
154+
}
138155

139156
// send request
140157
client := c.client
@@ -149,7 +166,7 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
149166
defer res.Body.Close()
150167

151168
// read response body
152-
b, err = ioutil.ReadAll(res.Body)
169+
b, err = io.ReadAll(res.Body)
153170
if err != nil {
154171
return nil, fmt.Errorf("Error reading response: %v", err)
155172
}
@@ -181,7 +198,7 @@ func (c *Session) Do(req *Request) (resp *Response, err error) {
181198
// An error is return if a transport, marshalling or API error happened.
182199
func (c *Session) Get(method string, params interface{}, v interface{}) error {
183200
req := NewRequest(method, params)
184-
resp, err := c.Do(req)
201+
resp, err := c.Do(req, false)
185202
if err != nil {
186203
return err
187204
}

0 commit comments

Comments
 (0)