-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (127 loc) · 2.78 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"bytes"
)
var (
flagUrl = flag.String("host", "http://localhost:15672", "Management API root url")
flagVhost = flag.String("vhost", "/", "Virtual host")
flagPattern= flag.String("queue", ".+", "Check queue pattern")
flagExclude = flag.String("exclude", "", "Exclude queue pattern")
flagErr = flag.Int("error", 3, "Error message threshold")
flagWarn = flag.Int("warn", 2, "Warning message threshold")
flagUser = flag.String("user", "guest", "Username")
flagPassword = flag.String("password", "guest", "Password")
)
const pathApiQueues = "/api/queues/"
type Queue struct {
Name string
Messages_Ready int
Consumers int
}
func (q Queue) Println() {
fmt.Printf("%+v\n", q)
}
func (q Queue) ToString() string {
return fmt.Sprintf("%+v\n", q)
}
func Fatal(i ...interface{}) {
fmt.Println(i...)
os.Exit(3)
}
func Get(url *url.URL, username string, password string) *http.Response {
client := &http.Client{}
r, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
Fatal(err)
}
r.SetBasicAuth(username, password)
resp, err := client.Do(r)
if err != nil {
Fatal(err)
}
return resp
}
func LoadQueues(resp *http.Response) []Queue {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Fatal(err)
}
var queues []Queue
err = json.Unmarshal(body, &queues)
if err != nil {
Fatal(err)
}
return queues
}
func UrlJoin(base string, path string) *url.URL {
b, err := url.Parse(base)
if err != nil {
Fatal(err)
}
var resp_url *url.URL
p, err := url.Parse(path)
if err != nil {
Fatal(err)
}
resp_url = b.ResolveReference(p)
if err != nil {
Fatal(err)
}
return resp_url
}
func CheckQueue(q Queue, warn int, err int) int {
if q.Messages_Ready >= err {
return 2
} else if q.Messages_Ready >= warn {
return 1
}
return 0
}
func Check(queues []Queue, pattern string, exclude string, warn int, err int) (int, string) {
exitCode := 0
var buf bytes.Buffer
r := regexp.MustCompilePOSIX(pattern)
e := regexp.MustCompilePOSIX(exclude)
for _, q := range queues {
if exclude != "" && e.MatchString(q.Name) == true {
continue
}
if r.MatchString(q.Name) == true {
r := CheckQueue(q, warn, err)
if r > 0 {
buf.WriteString(q.ToString())
}
if r > exitCode {
exitCode = r
}
}
}
if buf.Len() == 0 {
buf.WriteString("OK\n")
}
return exitCode, buf.String()
}
func main() {
flag.Parse()
apiUrl := UrlJoin(*flagUrl, pathApiQueues + url.QueryEscape(*flagVhost))
resp := Get(apiUrl, *flagUser, *flagPassword)
if resp.StatusCode != 200 {
Fatal(apiUrl, "-", resp.Status)
}
queues := LoadQueues(resp)
exitCode, out := Check(queues,
*flagPattern,
*flagExclude,
*flagWarn,
*flagErr)
fmt.Print(out)
os.Exit(exitCode)
}