2015-07-21 17:29:25 +00:00
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-11-01 08:22:54 +00:00
|
|
|
"strconv"
|
2015-12-15 17:08:13 +00:00
|
|
|
"time"
|
2015-07-21 17:29:25 +00:00
|
|
|
|
2016-01-20 20:19:03 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2015-07-21 17:29:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultUsername = "guest"
|
|
|
|
const DefaultPassword = "guest"
|
|
|
|
const DefaultURL = "http://localhost:15672"
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
type RabbitMQ struct {
|
2015-07-21 17:29:25 +00:00
|
|
|
URL string
|
2015-09-11 23:21:43 +00:00
|
|
|
Name string
|
2015-07-21 17:29:25 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Nodes []string
|
2015-11-01 08:22:54 +00:00
|
|
|
Queues []string
|
2015-07-21 17:29:25 +00:00
|
|
|
|
|
|
|
Client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type OverviewResponse struct {
|
|
|
|
MessageStats *MessageStats `json:"message_stats"`
|
|
|
|
ObjectTotals *ObjectTotals `json:"object_totals"`
|
|
|
|
QueueTotals *QueueTotals `json:"queue_totals"`
|
|
|
|
}
|
|
|
|
|
2015-11-01 08:22:54 +00:00
|
|
|
type Details struct {
|
|
|
|
Rate float64
|
|
|
|
}
|
|
|
|
|
2015-07-21 17:29:25 +00:00
|
|
|
type MessageStats struct {
|
2015-11-01 08:22:54 +00:00
|
|
|
Ack int64
|
|
|
|
AckDetails Details `json:"ack_details"`
|
|
|
|
Deliver int64
|
|
|
|
DeliverDetails Details `json:"deliver_details"`
|
|
|
|
DeliverGet int64
|
|
|
|
DeliverGetDetails Details `json:"deliver_get_details"`
|
|
|
|
Publish int64
|
|
|
|
PublishDetails Details `json:"publish_details"`
|
|
|
|
Redeliver int64
|
|
|
|
RedeliverDetails Details `json:"redeliver_details"`
|
2015-07-21 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectTotals struct {
|
|
|
|
Channels int64
|
|
|
|
Connections int64
|
|
|
|
Consumers int64
|
|
|
|
Exchanges int64
|
|
|
|
Queues int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueueTotals struct {
|
|
|
|
Messages int64
|
|
|
|
MessagesReady int64 `json:"messages_ready"`
|
|
|
|
MessagesUnacknowledged int64 `json:"messages_unacknowledged"`
|
|
|
|
}
|
|
|
|
|
2015-11-01 08:22:54 +00:00
|
|
|
type Queue struct {
|
|
|
|
QueueTotals // just to not repeat the same code
|
|
|
|
MessageStats `json:"message_stats"`
|
|
|
|
Memory int64
|
|
|
|
Consumers int64
|
|
|
|
ConsumerUtilisation float64 `json:"consumer_utilisation"`
|
|
|
|
Name string
|
|
|
|
Node string
|
|
|
|
Vhost string
|
|
|
|
Durable bool
|
|
|
|
AutoDelete bool `json:"auto_delete"`
|
|
|
|
}
|
|
|
|
|
2015-07-21 17:29:25 +00:00
|
|
|
type Node struct {
|
|
|
|
Name string
|
|
|
|
|
|
|
|
DiskFree int64 `json:"disk_free"`
|
|
|
|
DiskFreeLimit int64 `json:"disk_free_limit"`
|
|
|
|
FdTotal int64 `json:"fd_total"`
|
|
|
|
FdUsed int64 `json:"fd_used"`
|
|
|
|
MemLimit int64 `json:"mem_limit"`
|
|
|
|
MemUsed int64 `json:"mem_used"`
|
|
|
|
ProcTotal int64 `json:"proc_total"`
|
|
|
|
ProcUsed int64 `json:"proc_used"`
|
|
|
|
RunQueue int64 `json:"run_queue"`
|
|
|
|
SocketsTotal int64 `json:"sockets_total"`
|
|
|
|
SocketsUsed int64 `json:"sockets_used"`
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
type gatherFunc func(r *RabbitMQ, acc inputs.Accumulator, errChan chan error)
|
2015-11-01 08:22:54 +00:00
|
|
|
|
|
|
|
var gatherFunctions = []gatherFunc{gatherOverview, gatherNodes, gatherQueues}
|
|
|
|
|
2015-07-21 17:29:25 +00:00
|
|
|
var sampleConfig = `
|
2015-12-20 03:26:18 +00:00
|
|
|
url = "http://localhost:15672" # required
|
2015-10-15 21:53:29 +00:00
|
|
|
# name = "rmq-server-1" # optional tag
|
|
|
|
# username = "guest"
|
|
|
|
# password = "guest"
|
|
|
|
|
|
|
|
# A list of nodes to pull metrics about. If not specified, metrics for
|
|
|
|
# all nodes are gathered.
|
|
|
|
# nodes = ["rabbit@node1", "rabbit@node2"]
|
2015-07-21 17:29:25 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (r *RabbitMQ) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RabbitMQ) Description() string {
|
|
|
|
return "Read metrics from one or many RabbitMQ servers via the management API"
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func (r *RabbitMQ) Gather(acc inputs.Accumulator) error {
|
2015-07-21 17:29:25 +00:00
|
|
|
if r.Client == nil {
|
|
|
|
r.Client = &http.Client{}
|
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
var errChan = make(chan error, len(gatherFunctions))
|
2015-07-21 17:29:25 +00:00
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
for _, f := range gatherFunctions {
|
|
|
|
go f(r, acc, errChan)
|
2015-11-01 08:22:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
for i := 1; i <= len(gatherFunctions); i++ {
|
2015-11-01 08:22:54 +00:00
|
|
|
err := <-errChan
|
2015-07-21 17:29:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
func (r *RabbitMQ) requestJSON(u string, target interface{}) error {
|
|
|
|
u = fmt.Sprintf("%s%s", r.URL, u)
|
2015-11-01 08:22:54 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", u, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
username := r.Username
|
2015-11-01 08:22:54 +00:00
|
|
|
if username == "" {
|
|
|
|
username = DefaultUsername
|
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
password := r.Password
|
2015-11-01 08:22:54 +00:00
|
|
|
if password == "" {
|
|
|
|
password = DefaultPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetBasicAuth(username, password)
|
|
|
|
|
|
|
|
resp, err := r.Client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
json.NewDecoder(resp.Body).Decode(target)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func gatherOverview(r *RabbitMQ, acc inputs.Accumulator, errChan chan error) {
|
2015-07-21 17:29:25 +00:00
|
|
|
overview := &OverviewResponse{}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
err := r.requestJSON("/api/overview", &overview)
|
2015-07-21 17:29:25 +00:00
|
|
|
if err != nil {
|
2015-11-01 08:22:54 +00:00
|
|
|
errChan <- err
|
|
|
|
return
|
2015-07-21 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 18:15:12 +00:00
|
|
|
if overview.QueueTotals == nil || overview.ObjectTotals == nil || overview.MessageStats == nil {
|
2015-11-01 08:22:54 +00:00
|
|
|
errChan <- fmt.Errorf("Wrong answer from rabbitmq. Probably auth issue")
|
|
|
|
return
|
2015-10-13 18:15:12 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
tags := map[string]string{"url": r.URL}
|
|
|
|
if r.Name != "" {
|
|
|
|
tags["name"] = r.Name
|
2015-09-11 23:21:43 +00:00
|
|
|
}
|
2015-12-15 17:08:13 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"messages": overview.QueueTotals.Messages,
|
|
|
|
"messages_ready": overview.QueueTotals.MessagesReady,
|
|
|
|
"messages_unacked": overview.QueueTotals.MessagesUnacknowledged,
|
|
|
|
"channels": overview.ObjectTotals.Channels,
|
|
|
|
"connections": overview.ObjectTotals.Connections,
|
|
|
|
"consumers": overview.ObjectTotals.Consumers,
|
|
|
|
"exchanges": overview.ObjectTotals.Exchanges,
|
|
|
|
"queues": overview.ObjectTotals.Queues,
|
|
|
|
"messages_acked": overview.MessageStats.Ack,
|
|
|
|
"messages_delivered": overview.MessageStats.Deliver,
|
|
|
|
"messages_published": overview.MessageStats.Publish,
|
|
|
|
}
|
|
|
|
acc.AddFields("rabbitmq_overview", fields, tags)
|
2015-07-21 17:29:25 +00:00
|
|
|
|
2015-11-01 08:22:54 +00:00
|
|
|
errChan <- nil
|
|
|
|
}
|
2015-07-21 17:29:25 +00:00
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func gatherNodes(r *RabbitMQ, acc inputs.Accumulator, errChan chan error) {
|
2015-11-01 08:22:54 +00:00
|
|
|
nodes := make([]Node, 0)
|
|
|
|
// Gather information about nodes
|
2015-12-20 03:26:18 +00:00
|
|
|
err := r.requestJSON("/api/nodes", &nodes)
|
2015-07-21 17:29:25 +00:00
|
|
|
if err != nil {
|
2015-11-01 08:22:54 +00:00
|
|
|
errChan <- err
|
|
|
|
return
|
2015-07-21 17:29:25 +00:00
|
|
|
}
|
2015-12-15 17:08:13 +00:00
|
|
|
now := time.Now()
|
2015-07-21 17:29:25 +00:00
|
|
|
|
|
|
|
for _, node := range nodes {
|
2015-12-20 03:26:18 +00:00
|
|
|
if !r.shouldGatherNode(node) {
|
2015-07-21 17:29:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
tags := map[string]string{"url": r.URL}
|
2015-09-11 23:21:43 +00:00
|
|
|
tags["node"] = node.Name
|
2015-07-21 17:29:25 +00:00
|
|
|
|
2015-12-15 17:08:13 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"disk_free": node.DiskFree,
|
|
|
|
"disk_free_limit": node.DiskFreeLimit,
|
|
|
|
"fd_total": node.FdTotal,
|
|
|
|
"fd_used": node.FdUsed,
|
|
|
|
"mem_limit": node.MemLimit,
|
|
|
|
"mem_used": node.MemUsed,
|
|
|
|
"proc_total": node.ProcTotal,
|
|
|
|
"proc_used": node.ProcUsed,
|
|
|
|
"run_queue": node.RunQueue,
|
|
|
|
"sockets_total": node.SocketsTotal,
|
|
|
|
"sockets_used": node.SocketsUsed,
|
|
|
|
}
|
|
|
|
acc.AddFields("rabbitmq_node", fields, tags, now)
|
2015-07-21 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 08:22:54 +00:00
|
|
|
errChan <- nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:39:43 +00:00
|
|
|
func gatherQueues(r *RabbitMQ, acc inputs.Accumulator, errChan chan error) {
|
2015-11-01 08:22:54 +00:00
|
|
|
// Gather information about queues
|
|
|
|
queues := make([]Queue, 0)
|
2015-12-20 03:26:18 +00:00
|
|
|
err := r.requestJSON("/api/queues", &queues)
|
2015-11-01 08:22:54 +00:00
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, queue := range queues {
|
2015-12-20 03:26:18 +00:00
|
|
|
if !r.shouldGatherQueue(queue) {
|
2015-11-01 08:22:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
tags := map[string]string{
|
2015-12-20 03:26:18 +00:00
|
|
|
"url": r.URL,
|
2015-11-01 08:22:54 +00:00
|
|
|
"queue": queue.Name,
|
|
|
|
"vhost": queue.Vhost,
|
|
|
|
"node": queue.Node,
|
|
|
|
"durable": strconv.FormatBool(queue.Durable),
|
|
|
|
"auto_delete": strconv.FormatBool(queue.AutoDelete),
|
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields(
|
2015-12-15 17:08:13 +00:00
|
|
|
"rabbitmq_queue",
|
2015-11-01 08:22:54 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
// common information
|
|
|
|
"consumers": queue.Consumers,
|
|
|
|
"consumer_utilisation": queue.ConsumerUtilisation,
|
|
|
|
"memory": queue.Memory,
|
|
|
|
// messages information
|
|
|
|
"messages": queue.Messages,
|
|
|
|
"messages_ready": queue.MessagesReady,
|
|
|
|
"messages_unack": queue.MessagesUnacknowledged,
|
|
|
|
"messages_ack": queue.MessageStats.Ack,
|
|
|
|
"messages_ack_rate": queue.MessageStats.AckDetails.Rate,
|
|
|
|
"messages_deliver": queue.MessageStats.Deliver,
|
|
|
|
"messages_deliver_rate": queue.MessageStats.DeliverDetails.Rate,
|
|
|
|
"messages_deliver_get": queue.MessageStats.DeliverGet,
|
|
|
|
"messages_deliver_get_rate": queue.MessageStats.DeliverGetDetails.Rate,
|
|
|
|
"messages_publish": queue.MessageStats.Publish,
|
|
|
|
"messages_publish_rate": queue.MessageStats.PublishDetails.Rate,
|
|
|
|
"messages_redeliver": queue.MessageStats.Redeliver,
|
|
|
|
"messages_redeliver_rate": queue.MessageStats.RedeliverDetails.Rate,
|
|
|
|
},
|
|
|
|
tags,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
errChan <- nil
|
2015-07-21 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
func (r *RabbitMQ) shouldGatherNode(node Node) bool {
|
|
|
|
if len(r.Nodes) == 0 {
|
2015-07-21 17:29:25 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
for _, name := range r.Nodes {
|
2015-07-21 17:29:25 +00:00
|
|
|
if name == node.Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
func (r *RabbitMQ) shouldGatherQueue(queue Queue) bool {
|
|
|
|
if len(r.Queues) == 0 {
|
2015-11-01 08:22:54 +00:00
|
|
|
return true
|
2015-07-21 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 03:26:18 +00:00
|
|
|
for _, name := range r.Queues {
|
2015-11-01 08:22:54 +00:00
|
|
|
if name == queue.Name {
|
|
|
|
return true
|
|
|
|
}
|
2015-07-21 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 08:22:54 +00:00
|
|
|
return false
|
2015-07-21 17:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-07 20:39:43 +00:00
|
|
|
inputs.Add("rabbitmq", func() inputs.Input {
|
2015-07-21 17:29:25 +00:00
|
|
|
return &RabbitMQ{}
|
|
|
|
})
|
|
|
|
}
|