Skip to content

Commit

Permalink
icinga2: rename integer consts to Go-like names
Browse files Browse the repository at this point in the history
  • Loading branch information
oxzi committed Jan 9, 2024
1 parent 9924d84 commit df259de
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 72 deletions.
61 changes: 30 additions & 31 deletions internal/icinga2/api_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,37 @@ func (t *UnixFloat) UnmarshalJSON(data []byte) error {

// The following const values are representing constant integer values, e.g., 0 for an OK state service.
const (
// ACKNOWLEDGEMENT_* consts are describing an acknowledgement, e.g., from HostServiceRuntimeAttributes.
ACKNOWLEDGEMENT_NONE = 0
ACKNOWLEDGEMENT_NORMAL = 1
ACKNOWLEDGEMENT_STICKY = 2
// Acknowledgement* consts are describing an acknowledgement, e.g., from HostServiceRuntimeAttributes.
AcknowledgementNone = 0
AcknowledgementNormal = 1
AcknowledgementSticky = 2

// ENTRY_TYPE_* consts are describing an entry_type, e.g., from Comment.
ENTRY_TYPE_USER = 1
ENTRY_TYPE_DOWNTIME = 2
ENTRY_TYPE_FLAPPING = 3
ENTRY_TYPE_ACKNOWLEDGEMENT = 4
// EntryType* consts are describing an entry_type, e.g., from Comment.
EntryTypeUser = 1
EntryTypeDowntime = 2
EntryTypeFlapping = 3
EntryTypeAcknowledgement = 4

// STATE_HOST_* consts are describing a host state, e.g., from CheckResult.
STATE_HOST_UP = 0
STATE_HOST_DOWN = 1
// StateHost* consts are describing a host state, e.g., from CheckResult.
StateHostUp = 0
StateHostDown = 1

// STATE_SERVICE_* consts are describing a service state, e.g., from CheckResult.
STATE_SERVICE_OK = 0
STATE_SERVICE_WARNING = 1
STATE_SERVICE_CRITICAL = 2
STATE_SERVICE_UNKNOWN = 3
// StateService* consts are describing a service state, e.g., from CheckResult.
StateServiceOk = 0
StateServiceWarning = 1
StateServiceCritical = 2
StateServiceUnknown = 3

// STATE_TYPE_* consts are describing a state type, e.g., from HostServiceRuntimeAttributes.
STATE_TYPE_SOFT = 0
STATE_TYPE_HARD = 1
// StateType* consts are describing a state type, e.g., from HostServiceRuntimeAttributes.
StateTypeSoft = 0
StateTypeHard = 1
)

// Comment represents the Icinga 2 API Comment object.
//
// NOTE:
// - An empty Service field indicates a host comment.
// - The optional EntryType should be User = ENTRY_TYPE_USER, Downtime = ENTRY_TYPE_DOWNTIME,
// Flapping = ENTRY_TYPE_FLAPPING, Acknowledgement = ENTRY_TYPE_ACKNOWLEDGEMENT (ENTRY_TYPE_* consts)
// - The optional EntryType should be represented by one of the EntryType* consts.
//
// https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#objecttype-comment
type Comment struct {
Expand Down Expand Up @@ -102,9 +101,9 @@ type Downtime struct {
// NOTE:
// - Name is either the Host or the Service name.
// - Host is empty for Host objects; Host contains the Service's Host object name for Services.
// - State might be STATE_HOST_{UP,DOWN} for hosts or STATE_SERVICE_{OK,WARNING,CRITICAL,UNKNOWN} for services.
// - StateType might be STATE_TYPE_SOFT or STATE_TYPE_HARD.
// - Acknowledgement type might be ACKNOWLEDGEMENT_{NONE,NORMAL,STICKY}.
// - State might be StateHost{Up,Down} for hosts or StateService{Ok,Warning,Critical,Unknown} for services.
// - StateType might be StateTypeSoft or StateTypeHard.
// - Acknowledgement type might be acknowledgement{None,Normal,Sticky}.
//
// https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#host
// https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#service
Expand Down Expand Up @@ -148,8 +147,8 @@ const (
//
// NOTE:
// - An empty Service field indicates a host state change.
// - State might be STATE_HOST_{UP,DOWN} for hosts or STATE_SERVICE_{OK,WARNING,CRITICAL,UNKNOWN} for services.
// - StateType might be STATE_TYPE_SOFT or STATE_TYPE_HARD.
// - State might be StateHost{Up,Down} for hosts or StateService{Ok,Warning,Critical,Unknown} for services.
// - StateType might be StateTypeSoft or StateTypeHard.
//
// https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#event-stream-type-statechange
type StateChange struct {
Expand All @@ -167,8 +166,8 @@ type StateChange struct {
//
// NOTE:
// - An empty Service field indicates a host acknowledgement.
// - State might be STATE_HOST_{UP,DOWN} for hosts or STATE_SERVICE_{OK,WARNING,CRITICAL,UNKNOWN} for services.
// - StateType might be STATE_TYPE_SOFT or STATE_TYPE_HARD.
// - State might be StateHost{Up,Down} for hosts or StateService{Ok,Warning,Critical,Unknown} for services.
// - StateType might be StateTypeSoft or StateTypeHard.
//
// https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#event-stream-type-acknowledgementset
type AcknowledgementSet struct {
Expand All @@ -185,8 +184,8 @@ type AcknowledgementSet struct {
//
// NOTE:
// - An empty Service field indicates a host acknowledgement.
// - State might be STATE_HOST_{UP,DOWN} for hosts or STATE_SERVICE_{OK,WARNING,CRITICAL,UNKNOWN} for services.
// - StateType might be STATE_TYPE_SOFT or STATE_TYPE_HARD.
// - State might be StateHost{Up,Down} for hosts or StateService{Ok,Warning,Critical,Unknown} for services.
// - StateType might be StateTypeSoft or StateTypeHard.
//
// https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#event-stream-type-acknowledgementcleared
type AcknowledgementCleared struct {
Expand Down
70 changes: 36 additions & 34 deletions internal/icinga2/api_responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Author: "icingaadmin",
Text: "foo bar",
EntryTime: UnixFloat(time.UnixMicro(1697454753536457)),
EntryType: ENTRY_TYPE_USER,
EntryType: EntryTypeUser,
},
},
},
Expand All @@ -114,7 +114,7 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Service: "ping6",
Author: "icingaadmin",
Text: "adfadsfasdfasdf",
EntryType: ENTRY_TYPE_USER,
EntryType: EntryTypeUser,
EntryTime: UnixFloat(time.UnixMicro(1697197701307516)),
},
},
Expand Down Expand Up @@ -161,18 +161,18 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Attrs: HostServiceRuntimeAttributes{
Name: "dummy-244",
Groups: []string{"app-network", "department-dev", "env-qa", "location-rome"},
State: STATE_HOST_UP,
StateType: STATE_TYPE_HARD,
State: StateHostUp,
StateType: StateTypeHard,
LastCheckResult: CheckResult{
ExitStatus: 0,
Output: "If you think last Tuesday was a drag, wait till you see what happens tomorrow!",
State: STATE_HOST_UP,
State: StateHostUp,
ExecutionStart: UnixFloat(time.UnixMicro(1697459643863147)),
ExecutionEnd: UnixFloat(time.UnixMicro(1697459643868893)),
},
LastStateChange: UnixFloat(time.UnixMicro(1697099900637215)),
DowntimeDepth: 0,
Acknowledgement: ACKNOWLEDGEMENT_NONE,
Acknowledgement: AcknowledgementNone,
AcknowledgementLastChange: UnixFloat(time.UnixMicro(0)),
},
},
Expand All @@ -189,18 +189,18 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Name: "ssh",
Host: "docker-master",
Groups: []string{},
State: STATE_SERVICE_CRITICAL,
StateType: STATE_TYPE_HARD,
State: StateServiceCritical,
StateType: StateTypeHard,
LastCheckResult: CheckResult{
ExitStatus: 2,
Output: "connect to address 127.0.0.1 and port 22: Connection refused",
State: STATE_SERVICE_CRITICAL,
State: StateServiceCritical,
ExecutionStart: UnixFloat(time.UnixMicro(1697460711130247)),
ExecutionEnd: UnixFloat(time.UnixMicro(1697460711134875)),
},
LastStateChange: UnixFloat(time.UnixMicro(1697099896120829)),
DowntimeDepth: 0,
Acknowledgement: ACKNOWLEDGEMENT_NORMAL,
Acknowledgement: AcknowledgementNormal,
AcknowledgementLastChange: UnixFloat(time.UnixMicro(1697460655878141)),
},
},
Expand All @@ -217,18 +217,18 @@ func TestObjectQueriesResult_UnmarshalJSON(t *testing.T) {
Name: "icinga",
Host: "docker-master",
Groups: []string{},
State: STATE_SERVICE_OK,
StateType: STATE_TYPE_HARD,
State: StateServiceOk,
StateType: StateTypeHard,
LastCheckResult: CheckResult{
ExitStatus: 0,
Output: "Icinga 2 has been running for 26 seconds. Version: v2.14.0-35-g31b1294ac",
State: STATE_SERVICE_OK,
State: StateServiceOk,
ExecutionStart: UnixFloat(time.UnixMicro(1698673636068106)),
ExecutionEnd: UnixFloat(time.UnixMicro(1698673636071483)),
},
LastStateChange: UnixFloat(time.UnixMicro(1697704135756310)),
DowntimeDepth: 0,
Acknowledgement: ACKNOWLEDGEMENT_NONE,
Acknowledgement: AcknowledgementNone,
AcknowledgementLastChange: UnixFloat(time.UnixMicro(0)),
},
},
Expand Down Expand Up @@ -271,12 +271,14 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &StateChange{
Timestamp: UnixFloat(time.UnixMicro(1697188278203504)),
Host: "dummy-158",
State: STATE_HOST_DOWN,
StateType: STATE_TYPE_SOFT,
State: StateHostDown,
StateType: StateTypeSoft,
CheckResult: CheckResult{
ExitStatus: 2,
Output: "If two people love each other, there can be no happy end to it.\n\t\t-- Ernest Hemingway",
State: 2, // check returned an invalid state for a host check
ExitStatus: 2,
Output: "If two people love each other, there can be no happy end to it.\n\t\t-- Ernest Hemingway",
// The State will be mapped to StateHostDown within Icinga 2, as shown in the outer StateChange
// State field. https://github.com/Icinga/icinga2/blob/v2.14.1/lib/icinga/host.cpp#L141-L155
State: StateServiceCritical,
ExecutionStart: UnixFloat(time.UnixMicro(1697188278194409)),
ExecutionEnd: UnixFloat(time.UnixMicro(1697188278202986)),
},
Expand All @@ -291,12 +293,12 @@ func TestApiResponseUnmarshal(t *testing.T) {
Timestamp: UnixFloat(time.UnixMicro(1697184778612108)),
Host: "dummy-280",
Service: "random fortune",
State: STATE_SERVICE_CRITICAL,
StateType: STATE_TYPE_SOFT,
State: StateServiceCritical,
StateType: StateTypeSoft,
CheckResult: CheckResult{
ExitStatus: 2,
Output: "You're growing out of some of your problems, but there are others that\nyou're growing into.",
State: STATE_SERVICE_CRITICAL,
State: StateServiceCritical,
ExecutionStart: UnixFloat(time.UnixMicro(1697184778600973)),
ExecutionEnd: UnixFloat(time.UnixMicro(1697184778611465)),
},
Expand All @@ -310,8 +312,8 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &AcknowledgementSet{
Timestamp: UnixFloat(time.UnixMicro(1697201074579106)),
Host: "dummy-805",
State: STATE_HOST_DOWN,
StateType: STATE_TYPE_HARD,
State: StateHostDown,
StateType: StateTypeHard,
Author: "icingaadmin",
Comment: "working on it",
},
Expand All @@ -323,8 +325,8 @@ func TestApiResponseUnmarshal(t *testing.T) {
Timestamp: UnixFloat(time.UnixMicro(1697201107647920)),
Host: "docker-master",
Service: "ssh",
State: STATE_SERVICE_CRITICAL,
StateType: STATE_TYPE_HARD,
State: StateServiceCritical,
StateType: StateTypeHard,
Author: "icingaadmin",
Comment: "will be fixed soon",
},
Expand All @@ -335,8 +337,8 @@ func TestApiResponseUnmarshal(t *testing.T) {
expected: &AcknowledgementCleared{
Timestamp: UnixFloat(time.UnixMicro(1697201082440148)),
Host: "dummy-805",
State: STATE_HOST_DOWN,
StateType: STATE_TYPE_HARD,
State: StateHostDown,
StateType: StateTypeHard,
},
},
{
Expand All @@ -346,8 +348,8 @@ func TestApiResponseUnmarshal(t *testing.T) {
Timestamp: UnixFloat(time.UnixMicro(1697201110220349)),
Host: "docker-master",
Service: "ssh",
State: STATE_SERVICE_CRITICAL,
StateType: STATE_TYPE_HARD,
State: StateServiceCritical,
StateType: StateTypeHard,
},
},
{
Expand All @@ -359,7 +361,7 @@ func TestApiResponseUnmarshal(t *testing.T) {
Host: "dummy-912",
Author: "icingaadmin",
Text: "oh noes",
EntryType: ENTRY_TYPE_USER,
EntryType: EntryTypeUser,
EntryTime: UnixFloat(time.UnixMicro(1697191791097852)),
},
},
Expand All @@ -374,7 +376,7 @@ func TestApiResponseUnmarshal(t *testing.T) {
Service: "ping4",
Author: "icingaadmin",
Text: "if in doubt, check ticket #23",
EntryType: ENTRY_TYPE_USER,
EntryType: EntryTypeUser,
EntryTime: UnixFloat(time.UnixMicro(1697197990035889)),
},
},
Expand All @@ -388,7 +390,7 @@ func TestApiResponseUnmarshal(t *testing.T) {
Host: "dummy-912",
Author: "icingaadmin",
Text: "oh noes",
EntryType: ENTRY_TYPE_USER,
EntryType: EntryTypeUser,
EntryTime: UnixFloat(time.UnixMicro(1697191791097852)),
},
},
Expand All @@ -403,7 +405,7 @@ func TestApiResponseUnmarshal(t *testing.T) {
Service: "ping4",
Author: "icingaadmin",
Text: "if in doubt, check ticket #23",
EntryType: ENTRY_TYPE_USER,
EntryType: EntryTypeUser,
EntryTime: UnixFloat(time.UnixMicro(1697197990035889)),
},
},
Expand Down
10 changes: 5 additions & 5 deletions internal/icinga2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,20 @@ func (client *Client) buildHostServiceEvent(ctx context.Context, result CheckRes

if service != "" {
switch state {
case STATE_SERVICE_OK:
case StateServiceOk:
eventSeverity = event.SeverityOK
case STATE_SERVICE_WARNING:
case StateServiceWarning:
eventSeverity = event.SeverityWarning
case STATE_SERVICE_CRITICAL:
case StateServiceCritical:
eventSeverity = event.SeverityCrit
default: // UNKNOWN or faulty
eventSeverity = event.SeverityErr
}
} else {
switch state {
case STATE_HOST_UP:
case StateHostUp:
eventSeverity = event.SeverityOK
case STATE_HOST_DOWN:
case StateHostDown:
eventSeverity = event.SeverityCrit
default: // faulty
eventSeverity = event.SeverityErr
Expand Down
4 changes: 2 additions & 2 deletions internal/icinga2/client_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (client *Client) checkMissedChanges(ctx context.Context, objType string, ev
}

// Only process HARD states
if objQueriesResult.Attrs.StateType == STATE_TYPE_SOFT {
if objQueriesResult.Attrs.StateType == StateTypeSoft {
client.Logger.Debugf("Skipping SOFT event, %#v", objQueriesResult.Attrs)
continue
}
Expand Down Expand Up @@ -414,7 +414,7 @@ func (client *Client) listenEventStream() error {
switch respT := resp.(type) {
case *StateChange:
// Only process HARD states
if respT.StateType == STATE_TYPE_SOFT {
if respT.StateType == StateTypeSoft {
client.Logger.Debugf("Skipping SOFT State Change, %#v", respT)
continue
}
Expand Down

0 comments on commit df259de

Please sign in to comment.