Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions go_nagios.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
}
)

//--------------------------------------------------------------
// A type representing a Nagios check status. The Value is a the exit code
// expected for the check and the Message is the specific output string.
type NagiosStatus struct {
Expand All @@ -44,6 +45,61 @@ func (status *NagiosStatus) Aggregate(otherStatuses []*NagiosStatus) {
}
}

// Construct the Nagios message
func (status *NagiosStatus) constructedNagiosMessage() string {
return valMessages[status.Value] + " " + status.Message
}

// NagiosStatus: Issue a Nagios message to stdout and exit with appropriate Nagios code
func (status *NagiosStatus) NagiosExit() {
fmt.Fprintln(os.Stdout, status.constructedNagiosMessage())
os.Exit(int(status.Value))
}

//--------------------------------------------------------------
// A type representing a Nagios performance data value.
// https://nagios-plugins.org/doc/guidelines.html#AEN200
// http://docs.pnp4nagios.org/pnp-0.6/about#system_requirements
type NagiosPerformanceVal struct {
Label string
Value string
Uom string
WarnThreshold string
CritThreshold string
MinValue string
MaxValue string
}

//--------------------------------------------------------------
// A type representing a Nagios check status and performance data.
type NagiosStatusWithPerformanceData struct {
*NagiosStatus
Perfdata NagiosPerformanceVal
}

// Construct the Nagios message with performance data
func (status *NagiosStatusWithPerformanceData) constructedNagiosMessage() string {
msg := fmt.Sprintf("%s %s | '%s'=%s%s;%s;%s;%s;%s",
valMessages[status.Value],
status.Message,
status.Perfdata.Label,
status.Perfdata.Value,
status.Perfdata.Uom,
status.Perfdata.WarnThreshold,
status.Perfdata.CritThreshold,
status.Perfdata.MinValue,
status.Perfdata.MaxValue)
return msg
}

// Issue a Nagios message (with performance data) to stdout and exit with appropriate Nagios code
func (status *NagiosStatusWithPerformanceData) NagiosExit() {
fmt.Fprintln(os.Stdout, status.constructedNagiosMessage())
os.Exit(int(status.Value))
}

//--------------------------------------------------------------

// Exit with an UNKNOWN status and appropriate message
func Unknown(output string) {
ExitWithStatus(&NagiosStatus{output, NAGIOS_UNKNOWN})
Expand All @@ -66,6 +122,5 @@ func Ok(output string) {

// Exit with a particular NagiosStatus
func ExitWithStatus(status *NagiosStatus) {
fmt.Fprintln(os.Stdout, valMessages[status.Value], status.Message)
os.Exit(int(status.Value))
status.NagiosExit()
}
38 changes: 38 additions & 0 deletions go_nagios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,41 @@ func TestValMessages(t *testing.T) {
So(valMessages[NAGIOS_OK], ShouldEqual, "OK:")
})
}

func TestConstructedNagiosMessage(t *testing.T) {
Convey("Constructs a Nagios message without performance data", t, func() {
status_unknown := &NagiosStatus{"Shrug dunno", NAGIOS_UNKNOWN}
So(status_unknown.constructedNagiosMessage(), ShouldEqual, "UNKNOWN: Shrug dunno")

status_critical := &NagiosStatus{"Uh oh", NAGIOS_CRITICAL}
So(status_critical.constructedNagiosMessage(), ShouldEqual, "CRITICAL: Uh oh")

status_warning := &NagiosStatus{"Not so bad", NAGIOS_WARNING}
So(status_warning.constructedNagiosMessage(), ShouldEqual, "WARNING: Not so bad")

status_ok := &NagiosStatus{"ok", NAGIOS_OK}
So(status_ok.constructedNagiosMessage(), ShouldEqual, "OK: ok")
})

Convey("Constructs a Nagios message with performance data", t, func() {
status_unknown := &NagiosStatus{"Shrug dunno", NAGIOS_UNKNOWN}
perfdata1 := NagiosPerformanceVal{"metric", "1234", "ms", "12", "3400", "0", "99999"}
status_unknown_perf := &NagiosStatusWithPerformanceData{status_unknown, perfdata1}
So(status_unknown_perf.constructedNagiosMessage(), ShouldEqual, "UNKNOWN: Shrug dunno | 'metric'=1234ms;12;3400;0;99999")

status_critical := &NagiosStatus{"Uh oh", NAGIOS_CRITICAL}
perfdata2 := NagiosPerformanceVal{"metric", "1234", "ms", "12", "3400", "", ""}
status_critical_perf := &NagiosStatusWithPerformanceData{status_critical, perfdata2}
So(status_critical_perf.constructedNagiosMessage(), ShouldEqual, "CRITICAL: Uh oh | 'metric'=1234ms;12;3400;;")

status_warning := &NagiosStatus{"Not so bad", NAGIOS_WARNING}
perfdata3 := NagiosPerformanceVal{"metric", "1234", "ms", "", "", "0", "99999"}
status_warning_perf := &NagiosStatusWithPerformanceData{status_warning, perfdata3}
So(status_warning_perf.constructedNagiosMessage(), ShouldEqual, "WARNING: Not so bad | 'metric'=1234ms;;;0;99999")

status_ok := &NagiosStatus{"ok", NAGIOS_OK}
perfdata4 := NagiosPerformanceVal{"metric", "1234", "", "12", "3400", "0", "99999"}
status_ok_perf := &NagiosStatusWithPerformanceData{status_ok, perfdata4}
So(status_ok_perf.constructedNagiosMessage(), ShouldEqual, "OK: ok | 'metric'=1234;12;3400;0;99999")
})
}