| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nats |
| 4 | |
| 5 | import ( |
| 6 | "time" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 9 | ) |
| 10 | |
| 11 | // https://docs.nats.io/running-a-nats-service/nats_admin/monitoring |
| 12 | |
| 13 | const ( |
| 14 | // https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#general-information |
| 15 | urlPathVarz = "/varz" |
| 16 | // https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#health |
| 17 | urlPathHealthz = "/healthz" |
| 18 | // https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#account-statistics |
| 19 | urlPathAccstatz = "/accstatz" |
| 20 | // https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#route-information |
| 21 | urlPathRoutez = "/routez" |
| 22 | // https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#gateway-information |
| 23 | urlPathGatewayz = "/gatewayz" |
| 24 | // https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#leaf-node-information |
| 25 | urlPathLeafz = "/leafz" |
| 26 | // https://docs.nats.io/running-a-nats-service/nats_admin/monitoring#jetstream-information |
| 27 | urlPathJsz = "/jsz" |
| 28 | ) |
| 29 | |
| 30 | var ( |
| 31 | urlQueryHealthzJsEnabledOnly = web.URLQuery("js-enabled-only", "true") |
| 32 | urlQueryHealthzJsServerOnly = web.URLQuery("js-server-only", "true") |
| 33 | urlQueryAccstatz = web.URLQuery("unused", "1") |
| 34 | ) |
| 35 | |
| 36 | // //https://github.com/nats-io/nats-server/blob/v2.10.24/server/server.go#L2851 |
| 37 | var httpEndpoints = []string{ |
| 38 | "/", |
| 39 | "/varz", |
| 40 | "/connz", |
| 41 | "/routez", |
| 42 | "/gatewayz", |
| 43 | "/leafz", |
| 44 | "/subsz", |
| 45 | "/stacksz", |
| 46 | "/accountz", |
| 47 | "/accstatz", |
| 48 | "/jsz", |
| 49 | "/healthz", |
| 50 | "/ipqueuesz", |
| 51 | "/raftz", |
| 52 | } |
| 53 | |
| 54 | // https://github.com/nats-io/nats-server/blob/v2.10.24/server/monitor.go#L3125 |
| 55 | type healthzResponse struct { |
| 56 | Status *string `json:"status"` |
| 57 | } |
| 58 | |
| 59 | // https://github.com/nats-io/nats-server/blob/v2.10.24/server/monitor.go#L1164 |
| 60 | type varzResponse struct { |
| 61 | ID string `json:"server_id"` |
| 62 | Name string `json:"server_name"` |
| 63 | Version string `json:"version"` |
| 64 | Proto int `json:"proto"` |
| 65 | Host string `json:"host"` |
| 66 | Port int `json:"port"` |
| 67 | IP string `json:"ip,omitempty"` |
| 68 | MaxConn int `json:"max_connections"` |
| 69 | MaxSubs int `json:"max_subscriptions,omitempty"` |
| 70 | Uptime string `json:"uptime"` |
| 71 | Mem int64 `json:"mem"` |
| 72 | CPU float64 `json:"cpu"` |
| 73 | Connections int `json:"connections"` |
| 74 | TotalConnections uint64 `json:"total_connections"` |
| 75 | Routes int `json:"routes"` |
| 76 | Remotes int `json:"remotes"` |
| 77 | Leafs int `json:"leafnodes"` |
| 78 | InMsgs int64 `json:"in_msgs"` |
| 79 | OutMsgs int64 `json:"out_msgs"` |
| 80 | InBytes int64 `json:"in_bytes"` |
| 81 | OutBytes int64 `json:"out_bytes"` |
| 82 | SlowConsumers int64 `json:"slow_consumers"` |
| 83 | Subscriptions uint32 `json:"subscriptions"` |
| 84 | HTTPReqStats map[string]uint64 `json:"http_req_stats"` |
| 85 | } |
| 86 | |
| 87 | // https://github.com/nats-io/nats-server/blob/v2.10.24/server/monitor.go#L2279 |
| 88 | type ( |
| 89 | accstatzResponse struct { |
| 90 | AccStats []accountInfo `json:"account_statz"` |
| 91 | } |
| 92 | accountInfo struct { |
| 93 | Account string `json:"acc"` |
| 94 | Conns int `json:"conns"` |
| 95 | TotalConns int `json:"total_conns"` |
| 96 | LeafNodes int `json:"leafnodes"` |
| 97 | NumSubs uint32 `json:"num_subscriptions"` |
| 98 | Sent struct { |
| 99 | Msgs int64 `json:"msgs"` |
| 100 | Bytes int64 `json:"bytes"` |
| 101 | } `json:"sent"` |
| 102 | Received struct { |
| 103 | Msgs int64 `json:"msgs"` |
| 104 | Bytes int64 `json:"bytes"` |
| 105 | } `json:"received"` |
| 106 | SlowConsumers int64 `json:"slow_consumers"` |
| 107 | } |
| 108 | ) |
| 109 | |
| 110 | // https://github.com/nats-io/nats-server/blob/v2.10.24/server/monitor.go#L752 |
| 111 | type ( |
| 112 | routezResponse struct { |
| 113 | Routes []routeInfo `json:"routes"` |
| 114 | } |
| 115 | routeInfo struct { |
| 116 | Rid uint64 `json:"rid"` |
| 117 | RemoteID string `json:"remote_id"` |
| 118 | InMsgs int64 `json:"in_msgs"` |
| 119 | OutMsgs int64 `json:"out_msgs"` |
| 120 | InBytes int64 `json:"in_bytes"` |
| 121 | OutBytes int64 `json:"out_bytes"` |
| 122 | NumSubs uint32 `json:"subscriptions"` |
| 123 | } |
| 124 | ) |
| 125 | |
| 126 | type ( |
| 127 | // https://github.com/nats-io/nats-server/blob/v2.10.24/server/monitor.go#L1875 |
| 128 | gatewayzResponse struct { |
| 129 | Name string `json:"name"` |
| 130 | OutboundGateways map[string]*remoteGatewayInfo `json:"outbound_gateways"` |
| 131 | InboundGateways map[string][]*remoteGatewayInfo `json:"inbound_gateways"` |
| 132 | } |
| 133 | remoteGatewayInfo struct { |
| 134 | IsConfigured bool `json:"configured"` |
| 135 | Connection connectionInfo `json:"connection"` |
| 136 | } |
| 137 | connectionInfo struct { |
| 138 | Cid uint64 `json:"cid"` |
| 139 | Uptime string `json:"uptime"` |
| 140 | InMsgs int64 `json:"in_msgs"` |
| 141 | OutMsgs int64 `json:"out_msgs"` |
| 142 | InBytes int64 `json:"in_bytes"` |
| 143 | OutBytes int64 `json:"out_bytes"` |
| 144 | NumSubs uint32 `json:"subscriptions"` |
| 145 | } |
| 146 | ) |
| 147 | |
| 148 | type ( |
| 149 | // https://github.com/nats-io/nats-server/blob/v2.10.24/server/monitor.go#L2163 |
| 150 | leafzResponse struct { |
| 151 | Leafs []leafInfo `json:"leafs"` |
| 152 | } |
| 153 | leafInfo struct { |
| 154 | Name string `json:"name"` // remote server name or id |
| 155 | Account string `json:"account"` |
| 156 | IP string `json:"ip"` |
| 157 | Port int `json:"port"` |
| 158 | RTT string `json:"rtt,omitempty"` |
| 159 | InMsgs int64 `json:"in_msgs"` |
| 160 | OutMsgs int64 `json:"out_msgs"` |
| 161 | InBytes int64 `json:"in_bytes"` |
| 162 | OutBytes int64 `json:"out_bytes"` |
| 163 | NumSubs uint32 `json:"subscriptions"` |
| 164 | } |
| 165 | ) |
| 166 | |
| 167 | // https://github.com/nats-io/nats-server/blob/v2.10.24/server/monitor.go#L2801 |
| 168 | type ( |
| 169 | jszResponse struct { |
| 170 | Disabled bool `json:"disabled"` |
| 171 | Streams int `json:"streams"` |
| 172 | Consumers int `json:"consumers"` |
| 173 | Messages uint64 `json:"messages"` |
| 174 | Bytes uint64 `json:"bytes"` |
| 175 | Memory uint64 `json:"memory"` |
| 176 | Store uint64 `json:"storage"` |
| 177 | ReservedMemory uint64 `json:"reserved_memory"` |
| 178 | ReservedStore uint64 `json:"reserved_storage"` |
| 179 | Accounts int `json:"accounts"` |
| 180 | HAAssets int `json:"ha_assets"` |
| 181 | Api struct { |
| 182 | Total uint64 `json:"total"` |
| 183 | Errors uint64 `json:"errors"` |
| 184 | Inflight uint64 `json:"inflight"` |
| 185 | } `json:"api"` |
| 186 | Meta *jszMetaClusterInfo `json:"meta_cluster"` |
| 187 | } |
| 188 | jszMetaClusterInfo struct { |
| 189 | Name string `json:"name"` |
| 190 | Leader string `json:"leader"` |
| 191 | Peer string `json:"peer"` |
| 192 | Replicas []*jszPeerInfo `json:"replicas"` |
| 193 | Size int `json:"cluster_size"` |
| 194 | Pending int `json:"pending"` |
| 195 | } |
| 196 | jszPeerInfo struct { |
| 197 | Name string `json:"name"` |
| 198 | Current bool `json:"current"` |
| 199 | Offline bool `json:"offline"` |
| 200 | Active time.Duration `json:"active"` |
| 201 | Lag uint64 `json:"lag"` |
| 202 | Peer string `json:"peer"` |
| 203 | } |
| 204 | ) |