Skip to content

Commit 4c60ef8

Browse files
committed
docs: document new feature
1 parent 7694c6b commit 4c60ef8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,27 @@ func main() {
3030
Creates this output:
3131

3232
`'BasicValue'=WARN 'FieldWithThresholds'=1024;800;1024;64;2048 'MyCustomField'=63.5 'Complex'=100;800;1024;64;2048`
33+
34+
## Automatic exit codes
35+
You can get an exit code based on performance data and the Min,Max,Warn and Crit tags. This is useful if you want to exit your program with the correct exit code. You can use the ExitCode() function for this. It will compare the performance data to the thresholds and return the correct exit code. If the performance data is not a number, it will return 3 (UNKNOWN).
36+
37+
```go
38+
package main
39+
40+
import (
41+
"os"
42+
)
43+
44+
func main() {
45+
type Check struct {
46+
MyField int64 `warn:"800" crit:"1024" min:"64" max:"2048"`
47+
}
48+
49+
status := Check{
50+
MyField: 1200,
51+
}
52+
53+
code := ExitCode(status)
54+
os.Exit(code) // exits with code 2, i.e. CRITICAL
55+
}
56+
```

exit_code.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ import (
77
"strconv"
88
)
99

10+
// Calculates the exit code for a given struct
11+
// The struct must have the following tags:
12+
// - warn: warning threshold
13+
// - crit: critical threshold
14+
// - min: minimum value
15+
// - max: maximum value
1016
func ExitCode(v any) (int, error) {
1117
return exitCode(v, OK)
1218
}
1319

20+
// Recursively walks through the struct and calculates the exit code
21+
// The exit code is the highest exit code of all fields
22+
// If a field is a struct, it will be walked through recursively
23+
// If a field is not a struct, it will be compared to the thresholds
1424
func exitCode(v any, code int) (int, error) {
1525
value := reflect.Indirect(reflect.ValueOf(v))
1626
log.Printf("%+v\n", reflect.DeepEqual(value, reflect.Value{}))

0 commit comments

Comments
 (0)