-
Notifications
You must be signed in to change notification settings - Fork 475
/
Copy pathnagios.go
66 lines (56 loc) · 1.74 KB
/
nagios.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package outputs
import (
"fmt"
"io"
"strconv"
"time"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/util"
)
type Nagios struct{}
func (r Nagios) Output(w io.Writer, results <-chan []resource.TestResult,
startTime time.Time, outConfig util.OutputConfig) (exitCode int) {
var testCount, failed, skipped int
var perfdata, verbose bool
perfdata = util.IsValueInList("perfdata", outConfig.FormatOptions)
verbose = util.IsValueInList("verbose", outConfig.FormatOptions)
var summary map[int]string
summary = make(map[int]string)
for resultGroup := range results {
for _, testResult := range resultGroup {
switch testResult.Result {
case resource.FAIL:
if util.IsValueInList("verbose", outConfig.FormatOptions) {
summary[failed] = "Fail " + strconv.Itoa(failed+1) + " - " + humanizeResult2(testResult) + "\n"
}
failed++
case resource.SKIP:
skipped++
}
testCount++
}
}
duration := time.Since(startTime)
if failed > 0 {
fmt.Fprintf(w, "GOSS CRITICAL - Count: %d, Failed: %d, Skipped: %d, Duration: %.3fs", testCount, failed, skipped, duration.Seconds())
if perfdata {
fmt.Fprintf(w, "|total=%d failed=%d skipped=%d duration=%.3fs", testCount, failed, skipped, duration.Seconds())
}
fmt.Fprint(w, "\n")
if verbose {
for i := 0; i < failed; i++ {
fmt.Fprintf(w, "%s", summary[i])
}
}
return 2
}
fmt.Fprintf(w, "GOSS OK - Count: %d, Failed: %d, Skipped: %d, Duration: %.3fs", testCount, failed, skipped, duration.Seconds())
if perfdata {
fmt.Fprintf(w, "|total=%d failed=%d skipped=%d duration=%.3fs", testCount, failed, skipped, duration.Seconds())
}
fmt.Fprint(w, "\n")
return 0
}
func init() {
RegisterOutputer("nagios", &Nagios{}, []string{"perfdata", "verbose"})
}