master
go 145 lines 5.88 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package rabbitmq
4
5 import (
6 "encoding/json"
7 "fmt"
8 )
9
10 const (
11 urlPathAPIWhoami = "/api/whoami"
12 urlPathAPIDefinitions = "/api/definitions"
13 urlPathAPIOverview = "/api/overview"
14 urlPathAPINodes = "/api/nodes"
15 urlPathAPIVhosts = "/api/vhosts"
16 urlPathAPIQueues = "/api/queues"
17 )
18
19 type apiWhoamiResp struct {
20 Name string `json:"name"`
21 Tags apiWhoamiTags `json:"tags"`
22 }
23
24 type apiWhoamiTags []string
25
26 func (a *apiWhoamiTags) UnmarshalJSON(data []byte) error {
27 var multi []string
28 if err := json.Unmarshal(data, &multi); err == nil {
29 *a = multi
30 return nil
31 }
32
33 var single string
34 if err := json.Unmarshal(data, &single); err == nil {
35 *a = []string{single}
36 return nil
37 }
38
39 return fmt.Errorf("unexpected tags format: %s", string(data))
40 }
41
42 type apiDefinitionsResp struct {
43 GlobalParams []struct {
44 Name string `json:"name"`
45 Value any `json:"value"`
46 } `json:"global_parameters"`
47 }
48
49 // https://www.rabbitmq.com/monitoring.html#cluster-wide-metrics
50 type apiOverviewResp struct {
51 ObjectTotals struct {
52 Consumers int64 `json:"consumers" stm:"consumers"`
53 Queues int64 `json:"queues" stm:"queues"`
54 Exchanges int64 `json:"exchanges" stm:"exchanges"`
55 Connections int64 `json:"connections" stm:"connections"`
56 Channels int64 `json:"channels" stm:"channels"`
57 } `json:"object_totals" stm:"object_totals"`
58 ChurnRates struct {
59 ChannelClosed int64 `json:"channel_closed" stm:"channel_closed"`
60 ChannelCreated int64 `json:"channel_created" stm:"channel_created"`
61 ConnectionClosed int64 `json:"connection_closed" stm:"connection_closed"`
62 ConnectionCreated int64 `json:"connection_created" stm:"connection_created"`
63 QueueCreated int64 `json:"queue_created" stm:"queue_created"`
64 QueueDeclared int64 `json:"queue_declared" stm:"queue_declared"`
65 QueueDeleted int64 `json:"queue_deleted" stm:"queue_deleted"`
66 } `json:"churn_rates" stm:"churn_rates"`
67 QueueTotals struct {
68 Messages int64 `json:"messages" stm:"messages"`
69 MessagesReady int64 `json:"messages_ready" stm:"messages_ready"`
70 MessagesUnacknowledged int64 `json:"messages_unacknowledged" stm:"messages_unacknowledged"`
71 } `json:"queue_totals" stm:"queue_totals"`
72 MessageStats apiMessageStats `json:"message_stats" stm:"message_stats"`
73 }
74
75 // https://www.rabbitmq.com/monitoring.html#node-metrics
76 type (
77 apiNodeResp struct {
78 Name string `json:"name"`
79 OsPid string `json:"os_pid"`
80 Partitions []string `json:"partitions"` // network partitions https://www.rabbitmq.com/docs/partitions#detecting
81 FDTotal int64 `json:"fd_total"`
82 FDUsed int64 `json:"fd_used"`
83 MemLimit int64 `json:"mem_limit"`
84 MemUsed int64 `json:"mem_used"`
85 SocketsTotal int64 `json:"sockets_total"`
86 SocketsUsed int64 `json:"sockets_used"`
87 ProcTotal int64 `json:"proc_total"`
88 ProcUsed int64 `json:"proc_used"`
89 DiskFree int64 `json:"disk_free"`
90 RunQueue int64 `json:"run_queue"`
91 Uptime int64 `json:"uptime"`
92 Running bool `json:"running"`
93 MemAlarm bool `json:"mem_alarm"`
94 DiskFreeAlarm bool `json:"disk_free_alarm"`
95 BeingDrained bool `json:"being_drained"`
96 ClusterLinks []apiClusterPeer `json:"cluster_links"`
97 }
98 apiClusterPeer struct {
99 Name string `json:"name"`
100 RecvBytes int64 `json:"recv_bytes"`
101 SendBytes int64 `json:"send_bytes"`
102 }
103 )
104
105 type apiVhostResp struct {
106 Name string `json:"name"`
107 ClusterState map[string]string `json:"cluster_state"`
108 Messages int64 `json:"messages" stm:"messages"`
109 MessagesReady int64 `json:"messages_ready" stm:"messages_ready"`
110 MessagesUnacknowledged int64 `json:"messages_unacknowledged" stm:"messages_unacknowledged"`
111 MessageStats apiMessageStats `json:"message_stats" stm:"message_stats"`
112 }
113
114 // https://www.rabbitmq.com/monitoring.html#queue-metrics
115 type apiQueueResp struct {
116 Name string `json:"name"`
117 Node string `json:"node"`
118 Vhost string `json:"vhost"`
119 Type string `json:"type"`
120 State string `json:"state"`
121 IdleSince *any `json:"idle_since"`
122 Messages int64 `json:"messages" stm:"messages"`
123 MessagesReady int64 `json:"messages_ready" stm:"messages_ready"`
124 MessagesUnacknowledged int64 `json:"messages_unacknowledged" stm:"messages_unacknowledged"`
125 MessagesPagedOut int64 `json:"messages_paged_out" stm:"messages_paged_out"`
126 MessagesPersistent int64 `json:"messages_persistent" stm:"messages_persistent"`
127 MessageStats apiMessageStats `json:"message_stats" stm:"message_stats"`
128 }
129
130 // https://rawcdn.githack.com/rabbitmq/rabbitmq-server/v3.11.5/deps/rabbitmq_management/priv/www/api/index.html
131 type apiMessageStats struct {
132 Ack int64 `json:"ack" stm:"ack"`
133 Publish int64 `json:"publish" stm:"publish"`
134 PublishIn int64 `json:"publish_in" stm:"publish_in"`
135 PublishOut int64 `json:"publish_out" stm:"publish_out"`
136 Confirm int64 `json:"confirm" stm:"confirm"`
137 Deliver int64 `json:"deliver" stm:"deliver"`
138 DeliverNoAck int64 `json:"deliver_no_ack" stm:"deliver_no_ack"`
139 Get int64 `json:"get" stm:"get"`
140 GetEmpty int64 `json:"get_empty" stm:"get_empty"`
141 GetNoAck int64 `json:"get_no_ack" stm:"get_no_ack"`
142 DeliverGet int64 `json:"deliver_get" stm:"deliver_get"`
143 Redeliver int64 `json:"redeliver" stm:"redeliver"`
144 ReturnUnroutable int64 `json:"return_unroutable" stm:"return_unroutable"`
145 }