一、RabbitMQ Exporter部署

1、rabbitmq创建用户

1
2
3
4
5
6
7
8
9
10
11
# 创建ops_monitor用户,并设置密码
rabbitmqctl add_user ops_monitor ops123
# 给ops_monitor用户打上monitoring角色
rabbitmqctl set_user_tags ops_monitor monitoring
# 创建名为ops的virtual host
rabbitmqctl add_vhost ops
# 为ops_monitor用户配置名称"ops"的virtual host的所有权限
rabbitmqctl set_permissions -p ops ops_monitor ".*" ".*" ".*"
# 为ops_monitor用户配置名称"/"的virtual host的只读权限
# rabbitmqctl [-n <node>] [-l] [-q] set_permissions [-p <vhost>] <username> <conf> <write> <read>
rabbitmqctl set_permissions -p / ops_monitor "" "" ".*"

2、部署rabbitmq_exporter

将 rabbitmq_exporter-1.0.0-RC19_linux_amd64.tar.gz 拷贝到每个 RabbitMQ 节点上的 /opt 目录下。

1
2
3
cd /opt
tar zxvf rabbitmq_exporter-1.0.0-RC19_linux_amd64.tar.gz
# 如果第一步创建ops_monitor用户的密码是自定义的,则需要修改./rabbitmq_exporter-1.0.0-RC19_linux_amd64/config.json的rabbit_pass配置。

3、创建exporter的service启动文件

每个 RabbitMQ 节点上都需要执行以下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cd /etc/systemd/system
cat << EOF > rabbitmq_exporter.service
[Unit]
Description=rabbitmq_exporter
After=local-fs.target network-online.target network.target
Wants=local-fs.target network-online.target network.target

[Service]
ExecStart=/opt/rabbitmq_exporter-1.0.0-RC19_linux_amd64/rabbitmq_exporter -config-file /opt/rabbitmq_exporter-1.0.0-RC19_linux_amd64/config.json
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
EOF

config.json内容:

注意:rabbit_user、rabbit_pass、skip_vhost、aliveness_vhost配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
"rabbit_url": "http://localhost:15672",
"rabbit_user": "ops_monitor",
"rabbit_pass": "ops123",
"publish_port": "9419",
"publish_addr": "",
"output_format": "TTY",
"ca_file": "ca.pem",
"cert_file": "client-cert.pem",
"key_file": "client-key.pem",
"insecure_skip_verify": false,
"exlude_metrics": [],
"include_exchanges": ".*",
"skip_exchanges": "^$",
"include_queues": ".*",
"skip_queues": "^$",
"skip_vhost": "^ops$",
"include_vhost": ".*",
"rabbit_capabilities": "no_sort,bert",
"aliveness_vhost": "ops",
"enabled_exporters": [
"exchange",
"node",
"overview",
"queue",
"aliveness"
],
"timeout": 30,
"max_queues": 0
}

启动rabbitmq_exporter:

1
systemctl start rabbitmq_exporter

查看运行状态:

1
systemctl status rabbitmq_exporter

设置开机自启动:

1
systemctl enable rabbitmq_exporter

4、测试

执行以下命令:

1
curl -XGET http://localhost:9419/metrics | grep rabbitmq_up

如果 rabbitmq_up 的值为1,则证明指标采集成功。

可以登录 RabbitMQ 控制台,选择某个队列,点击进入详情,然后就可以发送消息到该队列了。

二、补充RabbitMQ相关命令

1
2
3
4
5
6
7
8
9
10
11
12
# 查看用户列表,及对应的tag(administrator、monitoring、policymaker、management、none)
rabbitmqctl list_users
# 查看vhost列表
rabbitmqctl list_vhosts
# 查看某用户的权限
rabbitmqctl list_user_permissions {用户}
# 给用户admin设置vhost的操作权限;"/"为vhost的名字
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
# 查看某vhost下的队列;"/"为vhost的名字
rabbitmqctl list_queues -p "/"
# 集群状态
rabbitmqctl cluster_status

RabbitMQ是多租户系统,不同的virtual host是相互独立的。

rabbitmq的权限控制通过两层来实现,一是vhost的权限,二是确认有权限访问vhost后,对vhost内资源的权限控制(配置,读,写)。

通俗的可以理解为:指用户对exchange,queue的操作权限,包括配置权限,读写权限。配置权限会影响到exchange,queue的声明和删除。读写权限影响到从queue里取消息,向exchange发送消息以及queue和exchange的绑定(bind)操作等等

授权分三个操作:

  • 读:有关消费消息的任何操作,包括”清除”整个队列
  • 写:发布消息
  • 配置:队列和交换机的创建和删除

参考资料: