Skip to content

Commit

Permalink
Merge pull request #9 from nixys/master
Browse files Browse the repository at this point in the history
 feat(PR #7): Add API method for Problem
  • Loading branch information
borisershov authored Sep 19, 2023
2 parents 8ce82d4 + 1f5c96d commit fa1bfdd
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/.project
/.vscode
.project
.vscode
.idea
.env
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ Run:
ZABBIX_HOST="https://zabbix.yourdomain.com/api_jsonrpc.php" ZABBIX_USERNAME="Admin" ZABBIX_PASSWORD="PASSWORD" go run main.go
```

Test:

```
ZABBIX_HOST="https://zabbix.yourdomain.com/api_jsonrpc.php" ZABBIX_USERNAME="Admin" ZABBIX_PASSWORD="PASSWORD" go test -v -run TestProblemGet
```


## Feedback

For support and feedback please contact me:
Expand Down
15 changes: 15 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package zabbix

type EventAcknowledgeActionType int64

const (
EventAcknowledgeActionTypeClose EventAcknowledgeActionType = 1
EventAcknowledgeActionTypeAck EventAcknowledgeActionType = 2
EventAcknowledgeActionTypeAddMessage EventAcknowledgeActionType = 4
EventAcknowledgeActionTypeChangeSeverity EventAcknowledgeActionType = 8
EventAcknowledgeActionTypeUnack EventAcknowledgeActionType = 16
EventAcknowledgeActionTypeSuppress EventAcknowledgeActionType = 32
EventAcknowledgeActionTypeUnsuppress EventAcknowledgeActionType = 64
EventAcknowledgeActionTypeChangeEventRankCause EventAcknowledgeActionType = 128
EventAcknowledgeActionTypeChangeEventRankSymptom EventAcknowledgeActionType = 256
)
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/nixys/nxs-go-zabbix/v5

go 1.14
go 1.20

require github.com/mitchellh/mapstructure v1.3.0
require github.com/mitchellh/mapstructure v1.5.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/mitchellh/mapstructure v1.3.0 h1:iDwIio/3gk2QtLLEsqU5lInaMzos0hDTz8a6lazSFVw=
github.com/mitchellh/mapstructure v1.3.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
2 changes: 1 addition & 1 deletion history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func testHistoryGet(t *testing.T, z Context) []HistoryFloatObject {

hObjects, _, err := z.HistoryGet(HistoryGetParams{
History: HistoryObjectTypeFloat,
ItemIDs: []int{testHistoryItemID},
//ItemIDs: []int{testHistoryItemID},
GetParameters: GetParameters{
Limit: 1,
},
Expand Down
148 changes: 148 additions & 0 deletions problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package zabbix

type ProblemSourceType int64

const (
ProblemSourceTypeTrigger ProblemSourceType = 0
ProblemSourceTypeInternal ProblemSourceType = 3
ProblemSourceTypeServiceStatusUpdate ProblemSourceType = 4
)

type ProblemObjectType int64

const (
ProblemObjectTypeTrigger ProblemObjectType = 0
ProblemObjectTypeItem ProblemObjectType = 4
ProblemObjectTypeLLDRule ProblemObjectType = 5
ProblemObjectTypeService ProblemObjectType = 6
)

type ProblemAcknowledgeType int64

const (
ProblemAcknowledgeTypeFalse ProblemAcknowledgeType = 0
ProblemAcknowledgeTypeTrue ProblemAcknowledgeType = 1
)

type ProblemSeverityType int64

const (
ProblemSeverityTypeNotClassified ProblemSeverityType = 0
ProblemSeverityTypeInformation ProblemSeverityType = 1
ProblemSeverityTypeWarning ProblemSeverityType = 2
ProblemSeverityTypeAverage ProblemSeverityType = 3
ProblemSeverityTypeHigh ProblemSeverityType = 4
ProblemSeverityTypeDisaster ProblemSeverityType = 5
)

type ProblemSuppressedType int64

const (
ProblemSuppressedTypeNormalState ProblemSuppressedType = 0
ProblemSuppressedTypeSuppressed ProblemSuppressedType = 1
)

type ProblemEvalType int64

const (
ProblemEvalTypeAndOr ProblemEvalType = 0
ProblemEvalTypeOR ProblemEvalType = 2
)

// ProblemObject struct is used to store problem operations results
//
// see: https://www.zabbix.com/documentation/5.0/en/manual/api/reference/problem/object
type ProblemObject struct {
EventID int64 `json:"eventid,omitempty"`
Source ProblemSourceType `json:"source,omitempty"`
Object ProblemObjectType `json:"object,omitempty"`
ObjectID int64 `json:"objectid,omitempty"`
Clock int64 `json:"clock,omitempty"`
Ns int64 `json:"ns,omitempty"`
REventID int64 `json:"r_eventid,omitempty"`
RClock int64 `json:"r_clock,omitempty"`
RNs int64 `json:"r_ns,omitempty"`
CauseEventID int64 `json:"cause_eventid,omitempty"`
CorrelationID int64 `json:"correlationid,omitempty"`
UserID int64 `json:"userid,omitempty"`
Name string `json:"name,omitempty"`
Acknowledged ProblemAcknowledgeType `json:"acknowledged,omitempty"`
Severity ProblemSeverityType `json:"severity,omitempty"`
Suppressed ProblemSuppressedType `json:"suppressed,omitempty"`
OpData string `json:"opdata,omitempty"`
URLs []ProblemMediaTypeURLObject `json:"urls,omitempty"`
Acknowledges []ProblemAcknowledgeObject `json:"acknowledges,omitempty"`
Tags []ProblemTagObject `json:"tags,omitempty"`
Suppression []ProblemSuppressionObject `json:"suppression_data,omitempty"`
}

type ProblemAcknowledgeObject struct {
AcknowledgeID int64 `json:"acknowledgeid,omitempty"`
UserID int64 `json:"userid,omitempty"`
EventID int64 `json:"eventid,omitempty"`
Clock int64 `json:"clock,omitempty"`
Message string `json:"message,omitempty"`
Action EventAcknowledgeActionType `json:"action,omitempty"`
OldSeverity ProblemSeverityType `json:"old_severity,omitempty"`
NewSeverity ProblemSeverityType `json:"new_severity,omitempty"`
SuppressUntil int64 `json:"suppress_until,omitempty"`
TaskID int64 `json:"taskid,omitempty"`
}

type ProblemMediaTypeURLObject struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
}

type ProblemTagObject struct {
Tag string `json:"tag,omitempty"`
Value string `json:"value,omitempty"`
}

type ProblemSuppressionObject struct {
MaintenanceID int64 `json:"maintenanceid,omitempty"`
UserID int64 `json:"userid,omitempty"`
SuppressUntil int64 `json:"suppress_until,omitempty"`
}

// ProblemGetParams struct is used for problem get requests
//
// see: https://www.zabbix.com/documentation/5.0/en/manual/api/reference/problem/get#parameters
type ProblemGetParams struct {
GetParameters

EventIDs []int64 `json:"eventids,omitempty"`
GroupIDs []int64 `json:"groupids,omitempty"`
HostIDs []int64 `json:"hostids,omitempty"`
ObjectIDs []int64 `json:"objectids,omitempty"`
ApplicationIDs []int64 `json:"applicationids,omitempty"`
Source ProblemSourceType `json:"source,omitempty"`
Object ProblemObjectType `json:"object,omitempty"`
Acknowledged bool `json:"acknowledged,omitempty"`
Suppressed bool `json:"suppressed,omitempty"`
Severities []ProblemSeverityType `json:"severities,omitempty"`
Evaltype ProblemEvalType `json:"evaltype,omitempty"`
Tags []ProblemTagObject `json:"tags,omitempty"`
Recent bool `json:"recent,omitempty"`
EventIDFrom int64 `json:"eventid_from,omitempty"`
EventIDTill int64 `json:"eventid_till,omitempty"`
TimeFrom int64 `json:"time_from,omitempty"`
TimeTill int64 `json:"time_till,omitempty"`
SelectAcknowledges SelectQuery `json:"selectAcknowledges,omitempty"`
SelectTags SelectQuery `json:"selectTags,omitempty"`
SelectSuppressionData SelectQuery `json:"selectSuppressionData,omitempty"`
SortField []string `json:"sortfield,omitempty"`
}

// ProblemGet gets problems
func (z *Context) ProblemGet(params ProblemGetParams) ([]ProblemObject, int, error) {

var result []ProblemObject

status, err := z.request("problem.get", params, &result)
if err != nil {
return nil, status, err
}

return result, status, nil
}
35 changes: 35 additions & 0 deletions problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package zabbix

import (
"testing"
)

func TestProblemCRUD(t *testing.T) {

var z Context

// Login
loginTest(&z, t)
defer logoutTest(&z, t)

// Get
testProblemGet(t, z)
}

func testProblemGet(t *testing.T, z Context) {

pObjects, _, err := z.ProblemGet(ProblemGetParams{
//ObjectIDs: []int{20143},
// ... Add other fields as needed
})

if err != nil {
t.Error("Problem get error:", err)
} else {
if len(pObjects) == 0 {
t.Error("Problem get error: unable to find problems")
} else {
t.Logf("Problem get: success")
}
}
}
4 changes: 2 additions & 2 deletions zabbix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

Expand Down Expand Up @@ -155,7 +155,7 @@ func (z *Context) httpPost(in interface{}, out interface{}) (int, error) {
defer res.Body.Close()

if res.StatusCode != 200 {
if bodyBytes, err := ioutil.ReadAll(res.Body); err == nil {
if bodyBytes, err := io.ReadAll(res.Body); err == nil {
return res.StatusCode, errors.New(string(bodyBytes))
}
} else {
Expand Down

0 comments on commit fa1bfdd

Please sign in to comment.