Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 5 additions & 5 deletions examples/check_example2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ func main() {

check1 := result.NewPartialResult()

check1.Output = "Check1"
check1.SetOutput("Check1")
check1.SetState(check.OK)

check1.Perfdata.Add(&check.Perfdata{
check1.AddPerfdata(&check.Perfdata{
Label: "foo",
Value: 23,
})
Expand All @@ -24,14 +24,14 @@ func main() {

check2 := result.NewPartialResult()

check2.Output = "Check2"
check2.SetOutput("Check2")
check2.SetState(check.Warning)

check2.Perfdata.Add(&check.Perfdata{
check2.AddPerfdata(&check.Perfdata{
Label: "bar",
Value: 42,
})
check2.Perfdata.Add(&check.Perfdata{
check2.AddPerfdata(&check.Perfdata{
Label: "foo2 bar",
Value: 46,
})
Expand Down
144 changes: 1 addition & 143 deletions result/overall.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Overall struct {
func (o *Overall) Add(state check.Status, output string) {
var result PartialResult
result.SetState(state)
result.Output = output
result.SetOutput(output)
o.AddSubcheck(&result)
}

Expand Down Expand Up @@ -141,128 +141,6 @@ func (o *Overall) getStatusCount() statusCount {
return result
}

// PartialResult represents a sub-result for an Overall struct
type PartialResult struct {
Perfdata check.PerfdataList
PartialResults []*PartialResult
Output string

// Result state, either set explicitly or derived from partialResults
state check.Status
// Default result state, if no partial results are available and no state is set explicitly
defaultState check.Status

// stateSetExplicitly indicates that SetState was called directly. When true,
// GetStatus returns s.state unconditionally, bypassing PartialResults entirely.
stateSetExplicitly bool
// defaultStateSetExplicitly indicates that SetDefaultState was called. When true
// and no PartialResults exist and no explicit state is set, GetStatus returns
// s.defaultState instead of check.Unknown.
defaultStateSetExplicitly bool

mu sync.RWMutex
}

// NewPartialResult initializer with defaults. It is recommended to use NewPartialResult.
// The default compared to the nil object is the default state is set to Unknown.
func NewPartialResult() *PartialResult {
return &PartialResult{
stateSetExplicitly: false,
defaultState: check.Unknown,
}
}

// AddSubcheck adds a PartialResult to the PartialResult
func (s *PartialResult) AddSubcheck(subcheck *PartialResult) {
s.mu.Lock()
defer s.mu.Unlock()

s.PartialResults = append(s.PartialResults, subcheck)
}

// String returns the status and output of the PartialResult
func (s *PartialResult) String() string {
return fmt.Sprintf("[%s] %s", s.GetStatus(), strings.ReplaceAll(s.Output, check.PerfdataSeparatorSymbol, " "))
}

// SetDefaultState sets a new default state for a PartialResult
func (s *PartialResult) SetDefaultState(state check.Status) {
s.mu.Lock()
defer s.mu.Unlock()

s.defaultState = state
s.defaultStateSetExplicitly = true
}

// SetState sets a state for a PartialResult
func (s *PartialResult) SetState(state check.Status) {
s.mu.Lock()
defer s.mu.Unlock()

s.state = state
s.stateSetExplicitly = true
}

// GetStatus returns the current state (ok, warning, critical, unknown) of the PartialResult
func (s *PartialResult) GetStatus() check.Status {
s.mu.RLock()
defer s.mu.RUnlock()

if s.stateSetExplicitly {
return s.state
}

if len(s.PartialResults) == 0 {
if s.defaultStateSetExplicitly {
return s.defaultState
}

return check.Unknown
}

states := make([]check.Status, len(s.PartialResults))

for i := range s.PartialResults {
states[i] = s.PartialResults[i].GetStatus()
}

return check.WorstState(states...)
}

// getPerfdata returns all subsequent perfdata as a concatenated string
func (s *PartialResult) getPerfdata() string {
var output strings.Builder

if len(s.Perfdata) > 0 {
output.WriteString(s.Perfdata.String())
}

if s.PartialResults != nil {
for _, ss := range s.PartialResults {
output.WriteString(" " + ss.getPerfdata())
}
}

return strings.TrimSpace(output.String())
}

// getOutput generates indented output for all subsequent PartialResults
func (s *PartialResult) getOutput(indentLevel int) string {
var output strings.Builder
// The final result will look like this:
// [OK] Overall is OK
// \_ [OK] My PartialResult
output.WriteString(strings.Repeat(" ", indentLevel) + "\\_ " + s.String() + "\n")

if s.PartialResults != nil {
for _, ss := range s.PartialResults {
output.WriteString(ss.getOutput(indentLevel + indentationOffset))
}
}

return output.String()
}

// GetSummary returns a text representation of the current state of the Overall
func (o *Overall) getSummary() string {
checkState := o.GetStatus()
Expand Down Expand Up @@ -323,23 +201,3 @@ func (o *Overall) getGenericSummary() string {

return result
}

func (s *PartialResult) getPartialResultFailedOutput() string {
if len(s.PartialResults) == 0 {
// this is a leave node
return s.Output
}

result := ""
worstState := check.OK

// Get the worst non-ok PartialResults output
for _, partRes := range s.PartialResults {
if check.Compare(worstState, partRes.GetStatus()) > 0 {
result = partRes.getPartialResultFailedOutput()
worstState = partRes.GetStatus()
}
}

return result
}
Loading
Loading