forked from Syncano/rabbitmq_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
263 lines (228 loc) · 5.62 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
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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "rabbitmq"
defaultConfigPath = "config.json"
)
var log = logrus.New()
// Listed available metrics
var (
connectionsTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "connections_total",
Help: "Total number of open connections.",
},
[]string{
// Which node was checked?
"node",
},
)
channelsTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "channels_total",
Help: "Total number of open channels.",
},
[]string{
"node",
},
)
queuesTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "queues_total",
Help: "Total number of queues in use.",
},
[]string{
"node",
},
)
consumersTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "consumers_total",
Help: "Total number of message consumers.",
},
[]string{
"node",
},
)
exchangesTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "exchanges_total",
Help: "Total number of exchanges in use.",
},
[]string{
"node",
},
)
messagesTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "messages_total",
Help: "Total number of messages in all queues.",
},
[]string{
"node",
},
)
)
type Config struct {
Nodes *[]Node `json:"nodes"`
Port string `json:"port"`
Interval string `json:"req_interval"`
}
type Node struct {
Name string `json:"name"`
Url string `json:"url"`
Uname string `json:"uname"`
Password string `json:"password"`
Interval string `json:"req_interval,omitempty"`
}
func sendApiRequest(hostname, username, password, query string) *json.Decoder {
client := &http.Client{}
req, err := http.NewRequest("GET", hostname+query, nil)
req.SetBasicAuth(username, password)
resp, err := client.Do(req)
if err != nil {
log.Error(err)
panic(err)
}
return json.NewDecoder(resp.Body)
}
func getOverview(hostname, username, password string) {
decoder := sendApiRequest(hostname, username, password, "/api/overview")
response := decodeObj(decoder)
metrics := make(map[string]float64)
for k, v := range response["object_totals"].(map[string]interface{}) {
metrics[k] = v.(float64)
}
nodename, _ := response["node"].(string)
channelsTotal.WithLabelValues(nodename).Set(metrics["channels"])
connectionsTotal.WithLabelValues(nodename).Set(metrics["connections"])
consumersTotal.WithLabelValues(nodename).Set(metrics["consumers"])
queuesTotal.WithLabelValues(nodename).Set(metrics["queues"])
exchangesTotal.WithLabelValues(nodename).Set(metrics["exchanges"])
}
func getNumberOfMessages(hostname, username, password string) {
decoder := sendApiRequest(hostname, username, password, "/api/queues")
response := decodeObjArray(decoder)
nodename := response[0]["node"].(string)
total_messages := 0.0
for _, v := range response {
total_messages += v["messages"].(float64)
}
messagesTotal.WithLabelValues(nodename).Set(total_messages)
}
func decodeObj(d *json.Decoder) map[string]interface{} {
var response map[string]interface{}
if err := d.Decode(&response); err != nil {
log.Error(err)
}
return response
}
func decodeObjArray(d *json.Decoder) []map[string]interface{} {
var response []map[string]interface{}
if err := d.Decode(&response); err != nil {
log.Error(err)
}
return response
}
func updateNodesStats(config *Config) {
for _, node := range *config.Nodes {
if len(node.Interval) == 0 {
node.Interval = config.Interval
}
go runRequestLoop(node)
}
}
func requestData(node Node) {
defer func() {
if r := recover(); r != nil {
dt := 10 * time.Second
time.Sleep(dt)
}
}()
getOverview(node.Url, node.Uname, node.Password)
getNumberOfMessages(node.Url, node.Uname, node.Password)
log.Info("Metrics updated successfully.")
dt, err := time.ParseDuration(node.Interval)
if err != nil {
log.Warn(err)
dt = 30 * time.Second
}
time.Sleep(dt)
}
func runRequestLoop(node Node) {
for {
requestData(node)
}
}
func loadConfig(path string, c *Config) bool {
defer func() {
if r := recover(); r != nil {
dt := 10 * time.Second
time.Sleep(dt)
}
}()
file, err := ioutil.ReadFile(path)
if err != nil {
log.Error(err)
panic(err)
}
err = json.Unmarshal(file, c)
if err != nil {
log.Error(err)
panic(err)
}
return true
}
func runLoadConfigLoop(path string, c *Config) {
for {
is_ok := loadConfig(path, c)
if is_ok == true {
break
}
}
}
func main() {
configPath := defaultConfigPath
if len(os.Args) > 1 {
configPath = os.Args[1]
}
log.Out = os.Stdout
var config Config
runLoadConfigLoop(configPath, &config)
updateNodesStats(&config)
http.Handle("/metrics", prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>RabbitMQ Exporter</title></head>
<body>
<h1>RabbitMQ Exporter</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>`))
})
log.Infof("Starting RabbitMQ exporter on port: %s.", config.Port)
http.ListenAndServe(":"+config.Port, nil)
}
// Register metrics to Prometheus
func init() {
prometheus.MustRegister(channelsTotal)
prometheus.MustRegister(connectionsTotal)
prometheus.MustRegister(queuesTotal)
prometheus.MustRegister(exchangesTotal)
prometheus.MustRegister(consumersTotal)
prometheus.MustRegister(messagesTotal)
}