/
overview.go
153 lines (133 loc) · 3.95 KB
/
overview.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"github.com/jessevdk/go-flags"
)
type options struct {
Host string `short:"h" long:"host" description:"The host of the rabbitmq server being monitored. For clusters please use all the hostnames in a comma separated list." default:"localhost"`
Port string `short:"P" long:"port" description:"The port on which the server can be accessed." default:"15672"`
Username string `short:"u" long:"username" description:"The username used for accessing rabbitmq web api." default:"guest"`
Password string `short:"p" long:"password" description:"The password for the account used to access the web api." default:"guest"`
Warning string `short:"w" long:"warning" default:"10000,10000" description:"Threshold for warnings."`
Critical string `short:"c" long:"critical" default:"50000,50000" description:"Threshold for critical."`
Secure bool `short:"s" long:"secure" default:"false" description:"Use http or https when accessing the api."`
}
/*
Overview representation from the api
*/
type Overview struct {
QueueTotals QueueTotals `json:"queue_totals"`
}
/*
QueueTotals represents the queue_totals substructure
*/
type QueueTotals struct {
MessagesUnack int `json:"messages_unacknowledged"`
MessagesReady int `json:"messages_ready"`
}
/*
limitMap calculates the limits from a comma separated string
*/
func limitMap(str string) ([]int, error) {
warningLimits := strings.Split(str, ",")
warning := []int{}
if len(warningLimits) != 2 {
err := errors.New("A list of two integers is required for limits.")
return nil, err
}
for _, value := range warningLimits {
tmpWarning, err := strconv.Atoi(value)
if err != nil {
log.Println(err.Error())
return nil, err
}
warning = append(warning, tmpWarning)
}
return warning, nil
}
/*
processHost processes the host and returns the overview from it
*/
func processHost(opt *options, host string) (*Overview, error) {
prefix := "http"
if opt.Secure == true {
prefix = "https"
}
uri := prefix + "://" + host + ":" + opt.Port + "/api/overview"
client := http.Client{}
request, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
request.SetBasicAuth(opt.Username, opt.Password)
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
over := &Overview{}
err = json.Unmarshal(body, &over)
if err != nil {
return nil, err
}
return over, nil
}
/*
processOverview processes the overview thresholds
*/
func processOverview(over *Overview, warning, critical []int) {
rdy, unack := strconv.Itoa(over.QueueTotals.MessagesReady), strconv.Itoa(over.QueueTotals.MessagesUnack)
// check errors first
if over.QueueTotals.MessagesReady >= critical[0] {
fmt.Println("CRITICAL " + rdy + " messages ready")
} else if over.QueueTotals.MessagesReady >= warning[0] {
fmt.Println("WARNING " + rdy + " messages ready")
} else {
fmt.Println("OK " + rdy + " messages ready")
}
if over.QueueTotals.MessagesUnack >= critical[1] {
fmt.Println("CRITICAL " + unack + " messages unacknowledged")
} else if over.QueueTotals.MessagesUnack >= warning[1] {
fmt.Println("WARNING " + unack + " messages unacknowledged")
} else {
fmt.Println("OK " + unack + " messages unacknowledged")
}
}
func main() {
opt := &options{}
_, err := flags.Parse(opt)
if err != nil {
return
}
warningLimits, err := limitMap(opt.Warning)
if err != nil {
log.Println(err.Error())
return
}
criticalLimits, err := limitMap(opt.Critical)
if err != nil {
log.Println(err.Error())
return
}
hosts := strings.Split(opt.Host, ",")
// loop through all hosts and check if we can access the overview page
for _, value := range hosts {
over, err := processHost(opt, value)
if err != nil {
log.Println(err.Error())
return
}
processOverview(over, warningLimits, criticalLimits)
}
}