forked from Syncano/rabbitmq_exporter
/
main.go
194 lines (171 loc) · 4.38 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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "rabbitmq"
configPath = "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{
// Which node was checked?
"node",
},
)
queuesTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "queues_total",
Help: "Total number of queues in use.",
},
[]string{
// Which node was checked?
"node",
},
)
consumersTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "consumers_total",
Help: "Total number of message consumers.",
},
[]string{
// Which node was checked?
"node",
},
)
exchangesTotal = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "exchanges_total",
Help: "Total number of exchanges in use.",
},
[]string{
// Which node was checked?
"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 unpackMetrics(d *json.Decoder) (map[string]float64, string) {
var output map[string]interface{}
if err := d.Decode(&output); err != nil {
log.Error(err)
}
metrics := make(map[string]float64)
for k, v := range output["object_totals"].(map[string]interface{}) {
metrics[k] = v.(float64)
}
nodename, _ := output["node"].(string)
return metrics, nodename
}
func getOverview(hostname, username, password string) *json.Decoder {
client := &http.Client{}
req, err := http.NewRequest("GET", hostname+"/api/overview", nil)
req.SetBasicAuth(username, password)
resp, err := client.Do(req)
if err != nil {
log.Error(err)
}
return json.NewDecoder(resp.Body)
}
func updateNodesStats(config *Config) {
for _, node := range *config.Nodes {
if len(node.Interval) == 0 {
node.Interval = config.Interval
}
go runRequestLoop(node)
}
}
func runRequestLoop(node Node) {
for {
decoder := getOverview(node.Url, node.Uname, node.Password)
metrics, nodename := unpackMetrics(decoder)
updateMetrics(metrics, nodename)
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 updateMetrics(metrics map[string]float64, nodename 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 newConfig(path string) (*Config, error) {
var config Config
file, err := ioutil.ReadFile(path)
if err != nil {
log.Error(err)
return nil, err
}
err = json.Unmarshal(file, &config)
return &config, err
}
func main() {
log.Out = os.Stdout
config, _ := newConfig(configPath)
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)
}