监控
Monitoring NATS
To monitor the NATS messaging system, nats-server
provides a lightweight HTTP server on a dedicated monitoring port. The monitoring server provides several endpoints, providing statistics and other information about the following:
All endpoints return a JSON object.
The NATS monitoring endpoints support JSONP and CORS, making it easy to create single page monitoring web applications. Part of the NATS ecosystem is a tool called nats-top that visualizes data from these endpoints on the command line.
nats-server
does not have authentication/authorization for the monitoring endpoint. When you plan to open your nats-server
to the internet make sure to not expose the monitoring port as well. By default, monitoring binds to every interface 0.0.0.0
so consider setting monitoring to localhost
or have appropriate firewall rules.
Enabling monitoring
Monitoring can be enabled in server configuration or as a server command-line option. The conventional port is 8222
.
As server configuration:
As a command-line option:
Once the server is running using one of the two methods, go to http://localhost:8222 to browse the available endpoints detailed below.
Monitoring Endpoints
General Information
The /varz
endpoint returns general information about the server state and configuration.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
N/A
Example
https://demo.nats.io:8222/varz
Response
Connection Information
The /connz
endpoint reports more detailed information on current and recently closed connections. It uses a paging mechanism which defaults to 1024 connections.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
sort
(see sort options)
Sorts the results. Default is connection ID.
auth
true, 1, false, 0
Include username. Default is false.
subs
true, 1, false, 0 or detail
Include subscriptions. Default is false. When set to detail
a list with more detailed subscription information will be returned.
offset
number > 0
Pagination offset. Default is 0.
limit
number > 0
Number of results to return. Default is 1024.
cid
number, valid id
Return a connection by it's id
state
open, *closed, any
Return connections of particular state. Default is open.
mqtt_client
string
Filter the connection with this MQTT client ID.
The server will default to holding the last 10,000 closed connections.
Sort Options
cid
Connection ID
start
Connection start time, same as CID
subs
Number of subscriptions
pending
Amount of data in bytes waiting to be sent to client
msgs_to
Number of messages sent
msgs_from
Number of messages received
bytes_to
Number of bytes sent
bytes_from
Number of bytes received
last
Last activity
idle
Amount of inactivity
uptime
Lifetime of the connection
stop
Stop time for a closed connection
reason
Reason for a closed connection
Examples
Get up to 1024 connections: https://demo.nats.io:8222/connz
Control limit and offset: https://demo.nats.io:8222/connz?limit=16&offset=128.
Get closed connection information: https://demo.nats.io:8222/connz?state=closed.
You can also report detailed subscription information on a per connection basis using subs=1. For example: https://demo.nats.io:8222/connz?limit=1&offset=1&subs=1.
Response
Route Information
The /routez
endpoint reports information on active routes for a cluster. Routes are expected to be low, so there is no paging mechanism with this endpoint.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
subs
true, 1, false, 0 or detail
Include subscriptions. Default is false. When set to detail
a list with more detailed subscription information will be returned.
As noted above, the routez
endpoint does support the subs
argument from the /connz
endpoint. For example: https://demo.nats.io:8222/routez?subs=1
Example
Get route information: https://demo.nats.io:8222/routez?subs=1
Response
Gateway Information
The /gatewayz
endpoint reports information about gateways used to create a NATS supercluster. Like routes, the number of gateways are expected to be low, so there is no paging mechanism with this endpoint.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
accs
true, 1, false, 0
Include account information. Default is false.
gw_name
string
Return only remote gateways with this name.
acc_name
string
Limit the list of accounts to this account name.
Examples
Retrieve Gateway Information: https://demo.nats.io:8222/gatewayz
Response
Leaf Node Information
The /leafz
endpoint reports detailed information about the leaf node connections.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
subs
true, 1, false, 0
Include internal subscriptions. Default is false.
As noted above, the leafz
endpoint does support the subs
argument from the /connz
endpoint. For example: https://demo.nats.io:8222/leafz?subs=1
Example
Get leaf nodes information: https://demo.nats.io:8222/leafz?subs=1
Response
Subscription Routing Information
The /subsz
endpoint reports detailed information about the current subscriptions and the routing data structure. It is not normally used.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
subs
true, 1, false, 0
Include subscriptions. Default is false.
offset
integer > 0
Pagination offset. Default is 0.
limit
integer > 0
Number of results to return. Default is 1024.
test
subject
Test whether a subsciption exists.
Example
Get subscription routing information: https://demo.nats.io:8222/subsz
Response
Account Information
The /accountz
endpoint reports information on a server's active accounts. The default behavior is to return a list of all accounts known to the server.
Success
200 (OK)
Error
400 (Bad Request)
acc
account name
Include metrics for the specified account. Default is empty. When not set, a list of all accounts is included.
Example
Get list of all accounts: https://demo.nats.io:8222/accountz
Get details for specific account
$G
: https://demo.nats.io:8222/accountz?acc=$G
Response
Default behavior:
Retrieve specific account:
Account Statistics
The /accstatz
endpoint reports per-account statistics such as the number of connections, messages/bytes in/out, etc.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
unused
true, 1, false, 0
If true, include accounts that do not have any current connections. Default is false.
Examples
Accounts with active connections - https://demo.nats.io:8222/accstatz
Include ones without any connections (in this case
$SYS
)- https://demo.nats.io:8222/accstatz?unused=1
Response
JetStream Information
The /jsz
endpoint reports more detailed information on JetStream. For accounts, it uses a paging mechanism that defaults to 1024 connections.
Note: If you're in a clustered environment, it is recommended to retrieve the information from the stream's leader in order to get the most accurate and up-to-date data.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
acc
account name
Include metrics for the specified account. Default is unset.
accounts
true, 1, false, 0
Include account specific JetStream information. Default is false.
streams
true, 1, false, 0
Include streams. When set, implies accounts=true
. Default is false.
consumers
true, 1, false, 0
Include consumer. When set, implies streams=true
. Default is false.
config
true, 1, false, 0
When stream or consumer are requested, include their respective configuration. Default is false.
leader-only
true, 1, false, 0
Only the leader responds. Default is false.
offset
number > 0
Pagination offset. Default is 0.
limit
number > 0
Number of results to return. Default is 1024.
Examples
Get basic JetStream information: https://demo.nats.io:8222/jsz
Request accounts and control limit and offset: https://demo.nats.io:8222/jsz?accounts=true&limit=16&offset=128.
You can also report detailed consumer information on a per connection basis using consumer=true. For example: https://demo.nats.io:8222/jsz?consumers=true.
Response
Health
The /healthz
endpoint returns OK if the server is able to accept connections.
Success
200 (OK)
Error
400 (Bad Request)
Arguments
js-enabled
true, 1
Returns an error if JetStream is disabled.
js-server-only
true, 1
Skip health check of accounts, streams, and consumers.
Example
Default - https://demo.nats.io:8222/healthz
Expect JetStream - https://demo.nats.io:8222/healthz?js-enabled=true
Response
Creating Monitoring Applications
NATS monitoring endpoints support JSONP and CORS. You can easily create single page web applications for monitoring. To do this you simply pass the callback
query parameter to any endpoint.
For example:
Here is a JQuery example implementation:
Monitoring Tools
In addition to writing custom monitoring tools, you can monitor nats-server in Prometheus. The Prometheus NATS Exporter allows you to configure the metrics you want to observe and store in Prometheus, and there are Grafana dashboards available for you to visualize the server metrics.
See the Walkthrough of Monitoring NATS with Prometheus and Grafana for more details.
最后更新于