-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexit.go
More file actions
114 lines (91 loc) · 3.08 KB
/
Copy pathexit.go
File metadata and controls
114 lines (91 loc) · 3.08 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package check
import (
"fmt"
"os"
"runtime/debug"
"strings"
)
// AllowExit lets you disable the call to os.Exit() in the BaseExit function of this package.
// This should be used carefully and most likely only for testing.
var AllowExit = true
// PrintStack prints the error stack when recovering from a panic with CatchPanic()
var PrintStack = true
// Exit exits the process with a given return code determined from the given Status
// and the provided text output to stdout.
// To include performance that is recommended to use ExitWithPerfdata instead
// Note that, the text output is not sanitized and will be printed as is.
//
// Example: [OK] - everything is fine
// exit 0
// Example: [UNKNOWN] - not sure what happened
// exit 3
func Exit(rc Status, output ...string) {
var text strings.Builder
text.WriteString("[" + rc.String() + "] -")
for _, s := range output {
text.WriteString(" " + s)
}
text.WriteString("\n")
_, _ = os.Stdout.WriteString(text.String())
BaseExit(rc)
}
// ExitWithPerfdata exits the process with a given return code determined from the Status,
// the performance data and the text output to stdout.
//
// The provided text output will be sanitized to avoid multiple performance data
// separators (|).
//
// Example: [OK] - everything is fine | mylabel=1
// exit 0
func ExitWithPerfdata(rc Status, perfdata PerfdataList, output ...string) {
var text strings.Builder
text.WriteString("[" + rc.String() + "] -")
for _, s := range output {
text.WriteString(" " + strings.ReplaceAll(s, PerfdataSeparatorSymbol, " "))
}
text.WriteString(PerfdataSeparatorSymbol)
text.WriteString(perfdata.String())
text.WriteString("\n")
_, _ = os.Stdout.WriteString(text.String())
BaseExit(rc)
}
// BaseExit exits the process with a given return code.
//
// Can be controlled with the global AllowExit.
// This should be used carefully and most likely only for testing.
func BaseExit(rc Status) {
if AllowExit {
os.Exit(int(rc))
}
o := fmt.Sprintf("would exit with code %d\n", rc)
_, _ = os.Stdout.WriteString(o)
}
// ExitError exits with an Unknown state while reporting the error
// The Unknown state is used, since the plugin likely could not determine
// the actual status of whatever was meant to be checked.
func ExitError(err error) {
// Catches Panic if error is nil and outputs the full stacktrace
defer CatchPanic()
Exit(Unknown, fmt.Sprintf("%s (%T)", err.Error(), err))
}
// CatchPanic is a general function for defer, to capture any panic that occurred during runtime of a check
//
// The function will recover from the condition and exit with a proper UNKNOWN status, while showing error
// and the call stack.
func CatchPanic() {
// This can be enabled when working with a debugger
// ppid := os.Getppid()
// if parent, err := ps.FindProcess(ppid); err == nil {
// if parent.Executable() == "dlv" {
// // seems to be a debugger, don't continue with recover
// return
// }
// }
if r := recover(); r != nil {
output := fmt.Sprint("Golang encountered a panic: ", r)
if PrintStack {
output += "\n\n" + string(debug.Stack())
}
Exit(Unknown, output)
}
}