-
Notifications
You must be signed in to change notification settings - Fork 8
/
checkers.go
87 lines (72 loc) · 1.48 KB
/
checkers.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
// Package checkers is utility for checker commands
package checkers
import (
"fmt"
"os"
)
// Status declare the result of the checker command
type Status int
// Statuses
const (
OK Status = iota
WARNING
CRITICAL
UNKNOWN
)
func (st Status) String() string {
switch st {
case OK:
return "OK"
case WARNING:
return "WARNING"
case CRITICAL:
return "CRITICAL"
default:
return "UNKNOWN"
}
}
// NewChecker returns new Checker
func NewChecker(st Status, msg string) *Checker {
return &Checker{
Status: st,
Message: msg,
}
}
// Checker is utility struct for check script
type Checker struct {
Name string
Status Status
Message string
}
var exit = os.Exit
// Exit with message
func (ckr *Checker) Exit() {
fmt.Println(ckr.String())
exit(int(ckr.Status))
}
func (ckr *Checker) ExitStatusAs(maps map[Status]Status) {
if _, ok := maps[ckr.Status]; ok {
ckr.Status = maps[ckr.Status]
}
fmt.Println(ckr.String())
exit(int(ckr.Status))
}
func (ckr *Checker) String() string {
return fmt.Sprintf("%s %s: %s", ckr.Name, ckr.Status, ckr.Message)
}
// Ok exiting with OK status
func Ok(msg string) *Checker {
return NewChecker(OK, msg)
}
// Warning exiting with WARNING status
func Warning(msg string) *Checker {
return NewChecker(WARNING, msg)
}
// Critical exiting with CRITICAL status
func Critical(msg string) *Checker {
return NewChecker(CRITICAL, msg)
}
// Unknown exiting with UNKNOWN status
func Unknown(msg string) *Checker {
return NewChecker(UNKNOWN, msg)
}