diff --git a/.gitignore b/.gitignore index d1542d4..53fa8a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ -/.project -/.vscode +.project +.vscode +.idea +.env diff --git a/README.md b/README.md index 7e9a000..718da4e 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/event.go b/event.go new file mode 100644 index 0000000..9a9863e --- /dev/null +++ b/event.go @@ -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 +) diff --git a/go.mod b/go.mod index 778278d..584136d 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 43df515..35c0981 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/history_test.go b/history_test.go index 2fd47ef..c85cf66 100644 --- a/history_test.go +++ b/history_test.go @@ -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, }, diff --git a/problem.go b/problem.go new file mode 100644 index 0000000..f429df5 --- /dev/null +++ b/problem.go @@ -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 +} diff --git a/problem_test.go b/problem_test.go new file mode 100644 index 0000000..5251649 --- /dev/null +++ b/problem_test.go @@ -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") + } + } +} diff --git a/zabbix.go b/zabbix.go index aebb29a..d1f9b1d 100644 --- a/zabbix.go +++ b/zabbix.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "strings" @@ -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 {