summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/rabbitmq/collect.go
blob: 665dfdfc89e7cd6e8c587b14c5e6d2dbc0233673 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package rabbitmq

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"path/filepath"

	"github.com/netdata/netdata/go/go.d.plugin/pkg/stm"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
)

const (
	urlPathAPIOverview = "/api/overview"
	urlPathAPINodes    = "/api/nodes/"
	urlPathAPIVhosts   = "/api/vhosts"
	urlPathAPIQueues   = "/api/queues"
)

// TODO: there is built-in prometheus collector since v3.8.0 (https://www.rabbitmq.com/prometheus.html).
// Should use it (in addition?), it is the recommended option  according to the docs.
func (r *RabbitMQ) collect() (map[string]int64, error) {
	mx := make(map[string]int64)

	if err := r.collectOverviewStats(mx); err != nil {
		return nil, err
	}
	if err := r.collectNodeStats(mx); err != nil {
		return mx, err
	}
	if err := r.collectVhostsStats(mx); err != nil {
		return mx, err
	}
	if r.CollectQueues {
		if err := r.collectQueuesStats(mx); err != nil {
			return mx, err
		}
	}

	return mx, nil
}

func (r *RabbitMQ) collectOverviewStats(mx map[string]int64) error {
	var stats overviewStats
	if err := r.doOKDecode(urlPathAPIOverview, &stats); err != nil {
		return err
	}

	if r.nodeName == "" {
		r.nodeName = stats.Node
	}

	for k, v := range stm.ToMap(stats) {
		mx[k] = v
	}

	return nil
}

func (r *RabbitMQ) collectNodeStats(mx map[string]int64) error {
	if r.nodeName == "" {
		return nil
	}

	var stats nodeStats
	if err := r.doOKDecode(filepath.Join(urlPathAPINodes, r.nodeName), &stats); err != nil {
		return err
	}

	for k, v := range stm.ToMap(stats) {
		mx[k] = v
	}
	mx["proc_available"] = int64(stats.ProcTotal - stats.ProcUsed)

	return nil
}

func (r *RabbitMQ) collectVhostsStats(mx map[string]int64) error {
	var stats []vhostStats
	if err := r.doOKDecode(urlPathAPIVhosts, &stats); err != nil {
		return err
	}

	seen := make(map[string]bool)

	for _, vhost := range stats {
		seen[vhost.Name] = true
		for k, v := range stm.ToMap(vhost) {
			mx[fmt.Sprintf("vhost_%s_%s", vhost.Name, k)] = v
		}
	}

	for name := range seen {
		if !r.vhosts[name] {
			r.vhosts[name] = true
			r.Debugf("new vhost name='%s': creating charts", name)
			r.addVhostCharts(name)
		}
	}
	for name := range r.vhosts {
		if !seen[name] {
			delete(r.vhosts, name)
			r.Debugf("stale vhost name='%s': removing charts", name)
			r.removeVhostCharts(name)
		}
	}

	return nil
}

func (r *RabbitMQ) collectQueuesStats(mx map[string]int64) error {
	var stats []queueStats
	if err := r.doOKDecode(urlPathAPIQueues, &stats); err != nil {
		return err
	}

	seen := make(map[string]queueCache)

	for _, queue := range stats {
		seen[queue.Name+"|"+queue.Vhost] = queueCache{name: queue.Name, vhost: queue.Vhost}
		for k, v := range stm.ToMap(queue) {
			mx[fmt.Sprintf("queue_%s_vhost_%s_%s", queue.Name, queue.Vhost, k)] = v
		}
	}

	for key, queue := range seen {
		if _, ok := r.queues[key]; !ok {
			r.queues[key] = queue
			r.Debugf("new queue name='%s', vhost='%s': creating charts", queue.name, queue.vhost)
			r.addQueueCharts(queue.name, queue.vhost)
		}
	}
	for key, queue := range r.queues {
		if _, ok := seen[key]; !ok {
			delete(r.queues, key)
			r.Debugf("stale queue name='%s', vhost='%s': removing charts", queue.name, queue.vhost)
			r.removeQueueCharts(queue.name, queue.vhost)
		}
	}

	return nil
}

func (r *RabbitMQ) doOKDecode(urlPath string, in interface{}) error {
	req, err := web.NewHTTPRequest(r.Request.Copy())
	if err != nil {
		return fmt.Errorf("error on creating request: %v", err)
	}

	req.URL.Path = urlPath

	r.Debugf("doing HTTP %s to '%s'", req.Method, req.URL)
	resp, err := r.httpClient.Do(req)
	if err != nil {
		return fmt.Errorf("error on request to %s: %v", req.URL, err)
	}

	defer closeBody(resp)

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("%s returned HTTP status %d (%s)", req.URL, resp.StatusCode, resp.Status)
	}

	if err = json.NewDecoder(resp.Body).Decode(&in); err != nil {
		return fmt.Errorf("error on decoding response from %s: %v", req.URL, err)
	}

	return nil
}

func closeBody(resp *http.Response) {
	if resp != nil && resp.Body != nil {
		_, _ = io.Copy(io.Discard, resp.Body)
		_ = resp.Body.Close()
	}
}