管理命令(CLI)

emqttd消息服务器提供了’./bin/emqttd_ctl’的管理命令行。

Warning

限制: Windows平台无法使用。

status命令

查询emqttd消息服务器运行状态:

  1. $ ./bin/emqttd_ctl status
  2. Node 'emqttd@127.0.0.1' is started
  3. emqttd 0.16.0 is running

broker命令

broker命令查询服务器基本信息,启动时间,统计数据与性能数据。

broker

查询emqttd消息服务器描述、版本、启动时间

broker pubsub

查询核心的Erlang PubSub进程状态(调试)

broker stats

查询连接(Client)、会话(Session)、主题(Topic)、 订阅(Subscription)、路由(Route)统计信息

broker metrics

查询MQTT报文(Packet)、消息(Message)收发统计

查询emqttd消息服务器基本信息包括版本、启动时间等:

  1. $ ./bin/emqttd_ctl broker
  2. sysdescr : Erlang MQTT Broker
  3. version : 0.15.0
  4. uptime : 1 hours, 25 minutes, 24 seconds
  5. datetime : 2016-01-16 13:17:32

broker stats

查询服务器客户端连接(Client)、会话(Session)、主题(Topic)、订阅(Subscription)、路由(Route)统计:

  1. $ ./bin/emqttd_ctl broker stats
  2. clients/count : 1
  3. clients/max : 1
  4. queues/count : 0
  5. queues/max : 0
  6. retained/count : 2
  7. retained/max : 2
  8. routes/count : 2
  9. routes/reverse : 2
  10. sessions/count : 0
  11. sessions/max : 0
  12. subscriptions/count : 1
  13. subscriptions/max : 1
  14. topics/count : 54
  15. topics/max : 54

broker metrics

查询服务器流量(Bytes)、MQTT报文(Packets)、消息(Messages)收发统计:

  1. $ ./bin/emqttd_ctl broker metrics
  2. bytes/received : 297
  3. bytes/sent : 40
  4. messages/dropped : 348
  5. messages/qos0/received : 0
  6. messages/qos0/sent : 0
  7. messages/qos1/received : 0
  8. messages/qos1/sent : 0
  9. messages/qos2/received : 0
  10. messages/qos2/sent : 0
  11. messages/received : 0
  12. messages/retained : 2
  13. messages/sent : 0
  14. packets/connack : 5
  15. packets/connect : 5
  16. packets/disconnect : 0
  17. packets/pingreq : 0
  18. packets/pingresp : 0
  19. packets/puback/received : 0
  20. packets/puback/sent : 0
  21. packets/pubcomp/received: 0
  22. packets/pubcomp/sent : 0
  23. packets/publish/received: 0
  24. packets/publish/sent : 0
  25. packets/pubrec/received : 0
  26. packets/pubrec/sent : 0
  27. packets/pubrel/received : 0
  28. packets/pubrel/sent : 0
  29. packets/received : 9
  30. packets/sent : 9
  31. packets/suback : 4
  32. packets/subscribe : 4
  33. packets/unsuback : 0
  34. packets/unsubscribe : 0

cluster命令

cluster命令集群多个emqttd消息服务器节点(进程):

cluster join <Node>

加入集群

cluster leave

离开集群

cluster remove <Node>

从集群删除节点

cluster status

查询集群状态

cluster命令集群本机两个emqttd节点示例:

目录

节点名

MQTT端口

emqttd1

emqttd1@127.0.0.1

1883

emqttd2

emqttd2@127.0.0.1

2883

启动emqttd1:

  1. cd emqttd1 && ./bin/emqttd start

启动emqttd2:

  1. cd emqttd2 && ./bin/emqttd start

emqttd2节点与emqttd1集群,emqttd2目录下:

  1. $ ./bin/emqttd_ctl cluster join emqttd1@127.0.0.1
  2. Join the cluster successfully.
  3. Cluster status: [{running_nodes,['emqttd1@127.0.0.1','emqttd2@127.0.0.1']}]

任意节点目录下查询集群状态:

  1. $ ./bin/emqttd_ctl cluster status
  2. Cluster status: [{running_nodes,['emqttd2@127.0.0.1','emqttd1@127.0.0.1']}]

集群消息路由测试:

  1. # emqttd1节点上订阅x
  2. mosquitto_sub -t x -q 1 -p 1883
  3. # emqttd2节点上向x发布消息
  4. mosquitto_pub -t x -q 1 -p 2883 -m hello

emqttd2节点离开集群:

  1. cd emqttd2 && ./bin/emqttd_ctl cluster leave

emqttd1节点下删除emqttd2:

  1. cd emqttd1 && ./bin/emqttd_ctl cluster remove emqttd2@127.0.0.1

clients命令

clients命令查询连接的MQTT客户端。

clients list

查询全部客户端连接

clients show <ClientId>

根据ClientId查询客户端

clients kick <ClientId>

根据ClientId踢出客户端

clients list

查询全部客户端连接:

  1. $ ./bin/emqttd_ctl clients list
  2. Client(mosqsub/43832-airlee.lo, clean_sess=true, username=test, peername=127.0.0.1:64896, connected_at=1452929113)
  3. Client(mosqsub/44011-airlee.lo, clean_sess=true, username=test, peername=127.0.0.1:64961, connected_at=1452929275)
  4. ...

返回Client对象的属性:

clean_sess

清除会话标记

username

用户名

peername

对端TCP地址

connected_at

客户端连接时间

clients show <ClientId>

根据ClientId查询客户端:

  1. ./bin/emqttd_ctl clients show "mosqsub/43832-airlee.lo"
  2. Client(mosqsub/43832-airlee.lo, clean_sess=true, username=test, peername=127.0.0.1:64896, connected_at=1452929113)

clients kick <ClientId>

根据ClientId踢出客户端:

  1. ./bin/emqttd_ctl clients kick "clientid"

sessions命令

sessions命令查询MQTT连接会话。emqttd消息服务器会为每个连接创建会话,clean_session标记true,创建临时(transient)会话;clean_session标记为false,创建持久会话(persistent)。

sessions list

查询全部会话

sessions list persistent

查询全部持久会话

sessions list transient

查询全部临时会话

sessions show <ClientId>

根据ClientID查询会话

sessions list

查询全部会话:

  1. $ ./bin/emqttd_ctl sessions list
  2. Session(clientid, clean_sess=false, max_inflight=100, inflight_queue=0, message_queue=0, message_dropped=0, awaiting_rel=0, awaiting_ack=0, awaiting_comp=0, created_at=1452935508)
  3. Session(mosqsub/44101-airlee.lo, clean_sess=true, max_inflight=100, inflight_queue=0, message_queue=0, message_dropped=0, awaiting_rel=0, awaiting_ack=0, awaiting_comp=0, created_at=1452935401)

返回Session对象属性:

clean_sess

false: 持久会话,true: 临时会话

max_inflight

飞行窗口(最大允许同时下发消息数)

inflight_queue

当前正在下发的消息数

message_queue

当前缓存消息数

message_dropped

会话丢掉的消息数

awaiting_rel

等待客户端发送PUBREL的QoS2消息数

awaiting_ack

等待客户端响应PUBACK的QoS1/2消息数

awaiting_comp

等待客户端响应PUBCOMP的QoS2消息数

created_at

会话创建时间戳

sessions list persistent

查询全部持久会话:

  1. $ ./bin/emqttd_ctl sessions list persistent
  2. Session(clientid, clean_sess=false, max_inflight=100, inflight_queue=0, message_queue=0, message_dropped=0, awaiting_rel=0, awaiting_ack=0, awaiting_comp=0, created_at=1452935508)

sessions list transient

查询全部临时会话:

  1. $ ./bin/emqttd_ctl sessions list transient
  2. Session(mosqsub/44101-airlee.lo, clean_sess=true, max_inflight=100, inflight_queue=0, message_queue=0, message_dropped=0, awaiting_rel=0, awaiting_ack=0, awaiting_comp=0, created_at=1452935401)

sessions show <ClientId>

根据ClientId查询会话:

  1. $ ./bin/emqttd_ctl sessions show clientid
  2. Session(clientid, clean_sess=false, max_inflight=100, inflight_queue=0, message_queue=0, message_dropped=0, awaiting_rel=0, awaiting_ack=0, awaiting_comp=0, created_at=1452935508)

routes命令

routes命令查询路由表。

routes list

查询全部路由:

  1. $ ./bin/emqttd_ctl routes list
  2. t2/# -> emqttd2@127.0.0.1
  3. t/+/x -> emqttd2@127.0.0.1,emqttd@127.0.0.1

routes show <Topic>

根据Topic查询一条路由:

  1. $ ./bin/emqttd_ctl routes show t/+/x
  2. t/+/x -> emqttd2@127.0.0.1,emqttd@127.0.0.1

topics命令

topics命令查询当前的主题(Topic)表。

topics list

查询全部主题(Topic):

  1. $ ./bin/emqttd_ctl topics list
  2. $SYS/brokers/emqttd@127.0.0.1/metrics/packets/subscribe: static
  3. $SYS/brokers/emqttd@127.0.0.1/stats/subscriptions/max: static
  4. $SYS/brokers/emqttd2@127.0.0.1/stats/subscriptions/count: static
  5. ...

topics show <Topic>

查询某个主题(Topic):

  1. $ ./bin/emqttd_ctl topics show '$SYS/brokers'
  2. $SYS/brokers: static

subscriptions命令

subscriptions命令查询消息服务器的订阅(Subscription)表。

subscriptions list

查询全部订阅

subscriptions show <ClientId>

查询某个ClientId的订阅

subscriptions add <ClientId> <Topic> <Qos>

手工添加一条订阅

subscriptions del <ClientId> <Topic>

手工删除一条订阅

subscriptions list

查询全部订阅:

  1. $ ./bin/emqttd_ctl subscriptions list
  2. mosqsub/91042-airlee.lo -> t/y:1
  3. mosqsub/90475-airlee.lo -> t/+/x:2

subscriptions list static

查询全部静态订阅:

  1. $ ./bin/emqttd_ctl subscriptions list static
  2. clientid -> new_topic:1

subscriptions show <ClientId>

查询某个Client的订阅:

  1. $ ./bin/emqttd_ctl subscriptions show 'mosqsub/90475-airlee.lo'
  2. mosqsub/90475-airlee.lo -> t/+/x:2

subscriptions add <ClientId> <Topic> <QoS>

手工添加一条静态(Static)订阅:

  1. $ ./bin/emqttd_ctl subscriptions add clientid new_topic 1
  2. ok

subscriptions del <ClientId> <Topic>

手工删除一条静态(Static)订阅:

  1. $ ./bin/emqttd_ctl subscriptions del clientid new_topic
  2. ok

plugins命令

plugins命令用于加载、卸载、查询插件应用。emqttd消息服务器通过插件扩展认证、定制功能,插件置于plugins/目录下。

plugins list

列出全部插件(Plugin)

plugins load <Plugin>

加载插件(Plugin)

plugins unload <Plugin>

卸载插件(Plugin)

plugins list

列出全部插件:

  1. $ ./bin/emqttd_ctl plugins list
  2. Plugin(emqttd_dashboard, version=0.16.0, description=emqttd web dashboard, active=true)
  3. Plugin(emqttd_plugin_mysql, version=0.16.0, description=emqttd Authentication/ACL with MySQL, active=false)
  4. Plugin(emqttd_plugin_pgsql, version=0.16.0, description=emqttd PostgreSQL Plugin, active=false)
  5. Plugin(emqttd_plugin_redis, version=0.16.0, description=emqttd Redis Plugin, active=false)
  6. Plugin(emqttd_plugin_template, version=0.16.0, description=emqttd plugin template, active=false)
  7. Plugin(emqttd_recon, version=0.16.0, description=emqttd recon plugin, active=false)
  8. Plugin(emqttd_stomp, version=0.16.0, description=Stomp Protocol Plugin for emqttd broker, active=false)

插件属性:

version

插件版本

description

插件描述

active

是否已加载

load <Plugin>

加载插件:

  1. $ ./bin/emqttd_ctl plugins load emqttd_recon
  2. Start apps: [recon,emqttd_recon]
  3. Plugin emqttd_recon loaded successfully.

unload <Plugin>

卸载插件:

  1. $ ./bin/emqttd_ctl plugins unload emqttd_recon
  2. Plugin emqttd_recon unloaded successfully.

bridges命令

bridges命令用于在多台emqttd服务器节点间创建桥接:

  1. --------- ---------

Publisher –> | node1 | –Bridge Forward–> | node2 | –> Subscriber

——— ———

bridges list

查询全部桥接

bridges options

查询创建桥接选项

bridges start <Node> <Topic>

创建桥接

bridges start <Node> <Topic> <Options>

创建桥接并带选项设置

bridges stop <Node> <Topic>

删除桥接

创建一条emqttd1 -> emqttd2节点的桥接,转发传感器主题(Topic)消息到emqttd2:

  1. $ ./bin/emqttd_ctl bridges start emqttd2@127.0.0.1 sensor/#
  2. bridge is started.
  3. $ ./bin/emqttd_ctl bridges list
  4. bridge: emqttd1@127.0.0.1--sensor/#-->emqttd2@127.0.0.1

测试emqttd1–sensor/#–>emqttd2的桥接:

  1. #emqttd2节点上
  2. mosquitto_sub -t sensor/# -p 2883 -d
  3. #emqttd1节点上
  4. mosquitto_pub -t sensor/1/temperature -m "37.5" -d

bridge options

查询bridge创建选项设置:

  1. $ ./bin/emqttd_ctl bridges options
  2. Options:
  3. qos = 0 | 1 | 2
  4. prefix = string
  5. suffix = string
  6. queue = integer
  7. Example:
  8. qos=2,prefix=abc/,suffix=/yxz,queue=1000

bridges stop <Node> <Topic>

删除emqttd1–sensor/#–>emqttd2的桥接:

  1. $ ./bin/emqttd_ctl bridges stop emqttd2@127.0.0.1 sensor/#
  2. bridge is stopped.

vm命令

vm命令用于查询Erlang虚拟机负载、内存、进程、IO信息。

vm all

查询VM全部信息

vm load

查询VM负载

vm memory

查询VM内存

vm process

查询VM Erlang进程数量

vm io

查询VM io最大文件句柄

vm load

查询VM负载:

  1. $ ./bin/emqttd_ctl vm load
  2. cpu/load1 : 2.21
  3. cpu/load5 : 2.60
  4. cpu/load15 : 2.36

vm memory

查询VM内存:

  1. $ ./bin/emqttd_ctl vm memory
  2. memory/total : 23967736
  3. memory/processes : 3594216
  4. memory/processes_used : 3593112
  5. memory/system : 20373520
  6. memory/atom : 512601
  7. memory/atom_used : 491955
  8. memory/binary : 51432
  9. memory/code : 13401565
  10. memory/ets : 1082848

vm process

查询Erlang进程数量:

  1. $ ./bin/emqttd_ctl vm process
  2. process/limit : 8192
  3. process/count : 221

vm io

查询IO最大句柄数:

  1. $ ./bin/emqttd_ctl vm io
  2. io/max_fds : 2560
  3. io/active_fds : 1

trace命令

trace命令用于追踪某个客户端或Topic,打印日志信息到文件。

trace list

查询全部开启的追踪

trace client <ClientId> <LogFile>

开启Client追踪,日志到文件

trace client <ClientId> off

关闭Client追踪

trace topic <Topic> <LogFile>

开启Topic追踪,日志到文件

trace topic <Topic> off

关闭Topic追踪

trace client <ClientId> <LogFile>

开启Client追踪:

  1. $ ./bin/emqttd_ctl trace client clientid log/clientid_trace.log
  2. trace client clientid successfully.

trace client <ClientId> off

关闭Client追踪:

  1. $ ./bin/emqttd_ctl trace client clientid off
  2. stop to trace client clientid successfully.

trace topic <Topic> <LogFile>

开启Topic追踪:

  1. $ ./bin/emqttd_ctl trace topic topic log/topic_trace.log
  2. trace topic topic successfully.

trace topic <Topic> off

关闭Topic追踪:

  1. $ ./bin/emqttd_ctl trace topic topic off
  2. stop to trace topic topic successfully.

trace list

查询全部开启的追踪:

  1. $ ./bin/emqttd_ctl trace list
  2. trace client clientid -> log/clientid_trace.log
  3. trace topic topic -> log/topic_trace.log

listeners

listeners命令用于查询开启的TCP服务监听器:

  1. $ ./bin/emqttd_ctl listeners
  2. listener on http:8083
  3. acceptors : 4
  4. max_clients : 64
  5. current_clients : 0
  6. shutdown_count : []
  7. listener on mqtts:8883
  8. acceptors : 4
  9. max_clients : 512
  10. current_clients : 0
  11. shutdown_count : []
  12. listener on mqtt:1883
  13. acceptors : 16
  14. max_clients : 8192
  15. current_clients : 1
  16. shutdown_count : [{closed,1}]
  17. listener on http:18083
  18. acceptors : 4
  19. max_clients : 512
  20. current_clients : 0
  21. shutdown_count : []

listener参数说明:

acceptors

TCP Acceptor池

max_clients

最大允许连接数

current_clients

当前连接数

shutdown_count

Socket关闭原因统计

mnesia命令

查询mnesia数据库系统状态。

admins命令

Dashboard插件会自动注册admins命令,用于创建、删除管理员账号,重置管理员密码。

admins add <Username> <Password>

创建admin账号

admins passwd <Username> <Password>

重置admin密码

admins del <Username>

删除admin账号

admins add

创建admin账户:

  1. $ ./bin/emqttd_ctl admins add root public
  2. ok

admins passwd

重置admin账户密码:

  1. $ ./bin/emqttd_ctl admins passwd root private
  2. ok

admins del

删除admin账户:

  1. $ ./bin/emqttd_ctl admins del root
  2. ok