Skip to content

Commit baa774b

Browse files
committed
git init
0 parents  commit baa774b

File tree

9 files changed

+678
-0
lines changed

9 files changed

+678
-0
lines changed

Diff for: .github/workflows/go.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Go
2+
on:
3+
push:
4+
branches: [ master ]
5+
pull_request:
6+
branches: [ master ]
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
go:
15+
- '1.13'
16+
- '1.14'
17+
18+
steps:
19+
- name: Set up Go ${{ matrix.go }}
20+
uses: actions/setup-go@v1
21+
with:
22+
go-version: ${{ matrix.go }}
23+
id: go
24+
25+
- name: Check out code into the Go module directory
26+
uses: actions/checkout@v1
27+
28+
- name: Test
29+
run: go test -v ./...

Diff for: .gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Editors
2+
/.idea/
3+
[._]*.sw[a-p]
4+
*~
5+
6+
# Binaries for programs and plugins
7+
/build/
8+
*.exe
9+
*.exe~
10+
*.dll
11+
*.so
12+
*.dylib
13+
14+
# Test binary, built with `go test -c`
15+
*.test
16+
17+
# Output of the go coverage tool, specifically when used with LiteIDE
18+
*.out

Diff for: COPYING

+339
Large diffs are not rendered by default.

Diff for: go.mod

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/NETWAYS/go-check
2+
3+
go 1.14
4+
5+
require (
6+
github.com/stretchr/testify v1.5.1
7+
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect
8+
golang.org/x/tools v0.0.0-20200512001501-aaeff5de670a // indirect
9+
)

Diff for: go.sum

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
7+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
8+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
9+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
10+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
11+
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
12+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
13+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
14+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
15+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
16+
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f h1:QBjCr1Fz5kw158VqdE9JfI9cJnl/ymnJWAdMuinqL7Y=
17+
golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
18+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
20+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
21+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
22+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
23+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
24+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
25+
golang.org/x/tools v0.0.0-20200512001501-aaeff5de670a h1:vAa2fXRLbiVN3N/xCnodIT36K4QKZQNyQFq3hQJfQ1U=
26+
golang.org/x/tools v0.0.0-20200512001501-aaeff5de670a/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
27+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
28+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
29+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
30+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
31+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
32+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
33+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
34+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

Diff for: result/overall.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// result tries to
2+
package result
3+
4+
import (
5+
"fmt"
6+
"github.com/NETWAYS/go-check/status"
7+
"strings"
8+
)
9+
10+
type Overall struct {
11+
OKs int
12+
Warnings int
13+
Criticals int
14+
Unknowns int
15+
Summary string
16+
Outputs []string
17+
}
18+
19+
func (o *Overall) Add(state int, output string) {
20+
switch state {
21+
case status.OK:
22+
o.OKs++
23+
case status.Warning:
24+
o.Warnings++
25+
case status.Critical:
26+
o.Criticals++
27+
default:
28+
o.Unknowns++
29+
}
30+
31+
o.Outputs = append(o.Outputs, fmt.Sprintf("[%s] %s", status.String(state), output))
32+
}
33+
34+
func (o *Overall) GetStatus() int {
35+
if o.Criticals > 0 {
36+
return status.Critical
37+
} else if o.Unknowns > 0 {
38+
return status.Unknown
39+
} else if o.Warnings > 0 {
40+
return status.Warning
41+
} else if o.OKs > 0 {
42+
return status.OK
43+
} else {
44+
return status.Unknown
45+
}
46+
}
47+
48+
func (o *Overall) GetSummary() string {
49+
if o.Summary == "" {
50+
if o.Criticals > 0 {
51+
o.Summary += fmt.Sprintf("critical=%d ", o.Criticals)
52+
}
53+
if o.Unknowns > 0 {
54+
o.Summary += fmt.Sprintf("unknown=%d ", o.Unknowns)
55+
}
56+
if o.Warnings > 0 {
57+
o.Summary += fmt.Sprintf("warning=%d ", o.Warnings)
58+
}
59+
if o.OKs > 0 {
60+
o.Summary += fmt.Sprintf("ok=%d ", o.OKs)
61+
}
62+
if o.Summary == "" {
63+
o.Summary = "No status information"
64+
} else {
65+
o.Summary = "states: " + strings.TrimSpace(o.Summary)
66+
}
67+
}
68+
69+
return o.Summary
70+
}
71+
72+
func (o *Overall) GetOutput() string {
73+
output := o.GetSummary() + "\n"
74+
75+
for _, extra := range o.Outputs {
76+
output += extra + "\n"
77+
}
78+
79+
return output
80+
}

Diff for: result/overall_test.go

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package result
2+
3+
import (
4+
"fmt"
5+
"github.com/NETWAYS/go-check/status"
6+
"github.com/stretchr/testify/assert"
7+
"testing"
8+
)
9+
10+
func TestOverall_AddOK(t *testing.T) {
11+
overall := Overall{}
12+
overall.Add(0, "test ok")
13+
14+
assert.Equal(t, 1, overall.OKs)
15+
assert.ElementsMatch(t, overall.Outputs, []string{"[OK] test ok"})
16+
}
17+
18+
func TestOverall_AddWarning(t *testing.T) {
19+
overall := Overall{}
20+
overall.Add(1, "test warning")
21+
22+
assert.Equal(t, 1, overall.Warnings)
23+
assert.ElementsMatch(t, overall.Outputs, []string{"[WARNING] test warning"})
24+
}
25+
26+
func TestOverall_AddCritical(t *testing.T) {
27+
overall := Overall{}
28+
overall.Add(2, "test critical")
29+
30+
assert.Equal(t, 1, overall.Criticals)
31+
assert.ElementsMatch(t, overall.Outputs, []string{"[CRITICAL] test critical"})
32+
}
33+
34+
func TestOverall_AddUnknown(t *testing.T) {
35+
overall := Overall{}
36+
overall.Add(3, "test unknown")
37+
38+
assert.Equal(t, 1, overall.Unknowns)
39+
assert.ElementsMatch(t, overall.Outputs, []string{"[UNKNOWN] test unknown"})
40+
}
41+
42+
func TestOverall_GetStatus_GetSummary(t *testing.T) {
43+
var overall Overall
44+
45+
overall = Overall{}
46+
assert.Equal(t, 3, overall.GetStatus())
47+
assert.Equal(t, "No status information", overall.GetSummary())
48+
49+
overall = Overall{OKs: 1}
50+
assert.Equal(t, 0, overall.GetStatus())
51+
assert.Equal(t, "states: ok=1", overall.GetSummary())
52+
53+
overall = Overall{Criticals: 2, OKs: 1, Warnings: 2, Unknowns: 1}
54+
assert.Equal(t, 2, overall.GetStatus())
55+
assert.Equal(t, "states: critical=2 unknown=1 warning=2 ok=1", overall.GetSummary())
56+
57+
overall = Overall{Unknowns: 2, OKs: 1, Warnings: 2}
58+
assert.Equal(t, 3, overall.GetStatus())
59+
assert.Equal(t, "states: unknown=2 warning=2 ok=1", overall.GetSummary())
60+
61+
overall = Overall{OKs: 1, Warnings: 2}
62+
assert.Equal(t, 1, overall.GetStatus())
63+
assert.Equal(t, "states: warning=2 ok=1", overall.GetSummary())
64+
}
65+
66+
func TestOverall_GetOutput(t *testing.T) {
67+
var overall Overall
68+
69+
overall = Overall{}
70+
overall.Add(0, "First OK")
71+
overall.Add(0, "Second OK")
72+
73+
assert.Equal(t, "states: ok=2\n[OK] First OK\n[OK] Second OK\n", overall.GetOutput())
74+
75+
overall = Overall{}
76+
overall.Add(0, "State OK")
77+
// TODO: compress when only one state
78+
assert.Equal(t, "states: ok=1\n[OK] State OK\n", overall.GetOutput())
79+
80+
overall = Overall{}
81+
overall.Add(0, "First OK")
82+
overall.Add(2, "Second Critical")
83+
overall.Summary = "Custom Summary"
84+
assert.Equal(t, "Custom Summary\n[OK] First OK\n[CRITICAL] Second Critical\n", overall.GetOutput())
85+
}
86+
87+
func ExampleOverall_Add() {
88+
overall := Overall{}
89+
overall.Add(status.OK, "One element is good")
90+
overall.Add(status.Critical, "The other is critical")
91+
92+
fmt.Println(overall)
93+
// Output: {1 0 1 0 [[OK] One element is good [CRITICAL] The other is critical]}
94+
}
95+
96+
func ExampleOverall_GetOutput() {
97+
overall := Overall{}
98+
overall.Add(status.OK, "One element is good")
99+
overall.Add(status.Critical, "The other is critical")
100+
101+
fmt.Println(overall.GetOutput())
102+
// Output:
103+
// states: critical=1 ok=1
104+
// [OK] One element is good
105+
// [CRITICAL] The other is critical
106+
}
107+
108+
func ExampleOverall_GetStatus() {
109+
overall := Overall{}
110+
overall.Add(status.OK, "One element is good")
111+
overall.Add(status.Critical, "The other is critical")
112+
113+
fmt.Println(overall.GetStatus())
114+
// Output: 2
115+
}

Diff for: status/status.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package status
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
const (
9+
// OK means everything is fine
10+
OK = 0
11+
// Warning means there is a problem the admin should review
12+
Warning = 1
13+
// Critical means there is a problem that requires immediate action
14+
Critical = 2
15+
// Unknown means the status can not be determined, probably due to an error or something missing
16+
Unknown = 3
17+
)
18+
19+
// String returns the string corresponding to a state
20+
func String(status int) string {
21+
switch status {
22+
case OK:
23+
return "OK"
24+
case Warning:
25+
return "WARNING"
26+
case Critical:
27+
return "CRITICAL"
28+
case Unknown:
29+
}
30+
return "UNKNOWN"
31+
}
32+
33+
// Exit does plugin exit with specified rc and output
34+
func Exit(rc int, output string, args ...interface{}) {
35+
fmt.Println(String(rc), "-", fmt.Sprintf(output, args...))
36+
os.Exit(rc)
37+
}
38+
39+
// ExitError exists with an Unknown state while reporting the error
40+
//
41+
// TODO: more information about the error
42+
func ExitError(err error) {
43+
Exit(Unknown, err.Error())
44+
}

Diff for: status/status_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package status
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestString(t *testing.T) {
9+
assert.Equal(t, "OK", String(OK))
10+
}

0 commit comments

Comments
 (0)