-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
272 lines (238 loc) · 7.96 KB
/
check.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package sknchk
/*
go-shinken-check
Copyright © 2020 pandaoc-io <nicolas.bertaina@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"fmt"
"os"
"strings"
)
//Standard Shinken/Nagios-like Return Code
const (
RcOk Status = iota
RcWarning
RcCritical
RcUnknwon
)
//Prefix used for the final output
const (
PrefixCliOk string = "[OK]"
PrefixCliWarning string = "[WARNING]"
PrefixCliCritical string = "[CRITICAL]"
PrefixCliUnknown string = "[UNKNOWN]"
PrefixHTMLOk string = `<span style="align-items: center; background-color: #28a745; border-radius: 4px; color: white; display: inline-flex; font-size: 12px; height: 2rem; justify-content: center; line-height: 1.5; padding-left: .75rem; padding-right: .75rem; white-space: nowrap; margin-top: 0.25rem; margin-left: .25rem;">OK</span>`
PrefixHTMLWarning string = `<span style="align-items: center; background-color: #ffc107; border-radius: 4px; color: #212529; display: inline-flex; font-size: 12px; height: 2rem; justify-content: center; line-height: 1.5; padding-left: .75rem; padding-right: .75rem; white-space: nowrap; margin-top: 0.25rem; margin-left: .25rem;">Warning</span>`
PrefixHTMLCritical string = `<span style="align-items: center; background-color: #dc3545; border-radius: 4px; color: white; display: inline-flex; font-size: 12px; height: 2rem; justify-content: center; line-height: 1.5; padding-left: .75rem; padding-right: .75rem; white-space: nowrap; margin-top: 0.25rem; margin-left: .25rem;">Critical</span>`
PrefixHTMLUnknown string = `<span style="align-items: center; background-color: #6c757d; border-radius: 4px; color: white; display: inline-flex; font-size: 12px; height: 2rem; justify-content: center; line-height: 1.5; padding-left: .75rem; padding-right: .75rem; white-space: nowrap; margin-top: 0.25rem; margin-left: .25rem;">Unknown</span>`
)
//Status type used to define the status of the check
type Status int
//OutputMode to display the check result, it can be 'cli' or 'html'
type OutputMode struct {
mode string
newLine string
bullet string
classOk string
classWarning string
classCritical string
}
//Output is a global variable that define the type of output, 'cli' by default
var Output *OutputMode = &OutputMode{
mode: "cli",
newLine: "",
bullet: " - ",
classOk: "",
classWarning: "",
classCritical: "",
}
//Check struct
type Check struct {
short []string
long []string
perfData []*PerfData
rc []Status
}
//Mode return the current output mode type
func (o *OutputMode) Mode() string {
return o.mode
}
//SetHTML will set the output format to html format
func (o *OutputMode) SetHTML() {
Output.mode = "html"
Output.newLine = "<br />"
Output.bullet = " • "
Output.classOk = "color: #28a745!important;"
Output.classWarning = "color: #947600!important;"
Output.classCritical = "color: #dc3545!important;"
}
//SetDebug will set the output format to debug format
func (o *OutputMode) SetDebug() {
Output.mode = "debug"
Output.newLine = "\n"
Output.bullet = "- "
Output.classOk = ""
Output.classWarning = ""
Output.classCritical = ""
}
func fmtOutput(str string, class string) string {
if Output.mode == "html" {
return fmt.Sprintf(`<span style="%v">%v</span>`, class, str)
}
return str
}
//FmtOk will format the OK output string depending on the output choosen mode
func FmtOk(str string) string {
return fmtOutput(str, Output.classOk)
}
//FmtWarning will format the Warning output string depending on the output choosen mode
func FmtWarning(str string) string {
return fmtOutput(str, Output.classWarning)
}
//FmtCritical will format the Critical output string depending on the output choosen mode
func FmtCritical(str string) string {
return fmtOutput(str, Output.classCritical)
}
//AddShort add a new string to the short output
func (c *Check) AddShort(short string, bullet bool) {
if bullet {
c.short = append(c.short, Output.bullet+short)
} else {
c.short = append(c.short, short)
}
}
//PrependShort add a new string at the begining of the short output
func (c *Check) PrependShort(short string, bullet bool) {
if bullet {
c.short = append([]string{Output.bullet + short}, c.short...)
} else {
c.short = append([]string{short}, c.short...)
}
}
//AddLong add a new string to the long output
func (c *Check) AddLong(long string, bullet bool) {
if bullet {
c.long = append(c.long, Output.bullet+long)
} else {
c.long = append(c.long, long)
}
}
//AddOk will add an Ok status to the Check structure
//to prepare the final output/RC
func (c *Check) AddOk() {
c.rc = append(c.rc, RcOk)
}
//AddWarning will add an Warning status to the Check structure
//to prepare the final output/RC
func (c *Check) AddWarning() {
c.rc = append(c.rc, RcWarning)
}
//AddCritical will add an Critical status with some short and long information to the Check structure
//to prepare the final output/RC
func (c *Check) AddCritical() {
c.rc = append(c.rc, RcCritical)
}
//AddUnknown will add an Unknown status with some short and long information to the Check structure
//to prepare the final output/RC
func (c *Check) AddUnknown() {
c.rc = append(c.rc, RcUnknwon)
}
//Ok will exit the program with the OK status
func Ok(short string, long string) {
var sLong []string
if len(long) > 0 {
sLong = append(sLong, long)
}
check := &Check{[]string{short}, sLong, nil, []Status{RcOk}}
Exit(check)
}
//Warning will exit the program with the Warning status
func Warning(short string, long string) {
var sLong []string
if len(long) > 0 {
sLong = append(sLong, long)
}
check := &Check{[]string{short}, sLong, nil, []Status{RcWarning}}
Exit(check)
}
//Critical will exit the program with the Critical status
func Critical(short string, long string) {
var sLong []string
if len(long) > 0 {
sLong = append(sLong, long)
}
check := &Check{[]string{short}, sLong, nil, []Status{RcCritical}}
Exit(check)
}
//Unknown will exit the program with the Unknown status
func Unknown(short string, long string) {
var sLong []string
if len(long) > 0 {
sLong = append(sLong, long)
}
check := &Check{[]string{short}, sLong, nil, []Status{RcUnknwon}}
Exit(check)
}
//ForceRc will force the return code of the check to the one given in argument
func (c *Check) ForceRc(rc Status) {
c.rc = []Status{rc}
}
//Rc return the Return Code of the check
func (c *Check) Rc() Status {
var maxRc Status
for _, value := range c.rc {
if value > maxRc {
maxRc = value
}
}
return maxRc
}
//Exit quit the program displaying the short and long output with the
func Exit(c *Check) {
rc := c.Rc()
var prefix string
if Output.mode == "html" {
switch rc {
case RcOk:
prefix = PrefixHTMLOk
case RcWarning:
prefix = PrefixHTMLWarning
case RcCritical:
prefix = PrefixHTMLCritical
case RcUnknwon:
prefix = PrefixHTMLUnknown
}
} else {
switch rc {
case RcOk:
prefix = PrefixCliOk
case RcWarning:
prefix = PrefixCliWarning
case RcCritical:
prefix = PrefixCliCritical
case RcUnknwon:
prefix = PrefixCliUnknown
}
}
perfStr := ""
if len(c.perfData) > 0 {
perfStr = "|" + generatePerfOutput(c.perfData)
}
if len(c.long) > 0 {
c.AddShort(Output.newLine+"For more details see long output.", false)
fmt.Fprintf(os.Stdout, "%v %v\n%v%v", prefix, strings.Join(c.short, Output.newLine), strings.Join(c.long, Output.newLine), perfStr)
} else {
fmt.Fprintf(os.Stdout, "%v %v%v", prefix, strings.Join(c.short, Output.newLine), perfStr)
}
os.Exit(int(rc))
}