Skip to content

Commit

Permalink
Merge pull request #145 from atc0005/i140-add-support-for-setting-pre…
Browse files Browse the repository at this point in the history
…ferred-output

Add support for setting preferred output target
  • Loading branch information
atc0005 authored Sep 16, 2022
2 parents 28d1e0a + c3be59d commit 3400877
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions nagios.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package nagios
import (
"errors"
"fmt"
"io"
"os"
"runtime/debug"
"strings"
Expand Down Expand Up @@ -188,6 +189,9 @@ type ExitCallBackFunc func() string
// application, including the most recent error and the final intended plugin
// state.
type ExitState struct {
// outputSink is the user-specified or fallback target for Nagios plugin
// output.
outputSink io.Writer

// LastError is the last error encountered which should be reported as
// part of ending the service check (e.g., "Failed to connect to XYZ to
Expand Down Expand Up @@ -348,8 +352,9 @@ func (es *ExitState) ReturnCheckResults() {

es.handlePerformanceData(&output)

// Emit all collected output.
fmt.Print(output.String())
// Emit all collected plugin output using user-specified or fallback
// output target.
es.emitOutput(output.String())

os.Exit(es.ExitStatusCode)
}
Expand Down Expand Up @@ -384,3 +389,27 @@ func (es *ExitState) AddPerfData(skipValidate bool, pd ...PerformanceData) error
func (es *ExitState) AddError(err ...error) {
es.Errors = append(es.Errors, err...)
}

// SetOutputTarget assigns a target for Nagios plugin output. By default
// output is emitted to os.Stdout.
func (es *ExitState) SetOutputTarget(w io.Writer) {
// Guard against potential nil argument.
if w == nil {
es.outputSink = os.Stdout
}

es.outputSink = w
}

// emitOutput writes final plugin output to the previously set output target.
// No further modifications to plugin output are performed.
func (es ExitState) emitOutput(pluginOutput string) {

// Emit all collected output using user-specified output target. Fall back
// to standard output if not set.
if es.outputSink == nil {
es.outputSink = os.Stdout
}

fmt.Fprint(es.outputSink, pluginOutput)
}

0 comments on commit 3400877

Please sign in to comment.