简体   繁体   English

检查蚊子健康的脚本

[英]Script to check mosquitto is healthy

I'm trying to create a health check script for mosquitto (to be used by docker). 我正在尝试为mosquitto创建一个运行状况检查脚本(由docker使用)。

if [ -z "$USERNAME" ]; then
 mosquitto_sub -t '$SYS/#' -C 1 | grep -v Error || exit 1
else 
 mosquitto_sub -t '$SYS/#' -C 1 -u $USERNAME -P $PASSWRD | grep -v Error || exit 1
fi

The issue I'm having is that if an incorrect password is given mosquitto_sub just keeps outputting Connection Refused: not authorised. 我遇到的问题是,如果给了错误的密码,mosquitto_sub只会继续输出Connection Refused: not authorised. over and over, and the timeout stuff in docker appears flakey, so it just never ends. 一遍又一遍,docker中的超时内容显示为flakey,所以它永远不会结束。

It doesn't look like mosquitto gives any way to fail better. 看起来mosquitto并没有提供更好的失败方法。 I think I might need to execute it as a background process that I can kill, but my bash isn't really that great, so does anyone have any better ideas? 我认为我可能需要将其作为可以杀死的后台进程执行,但是我的bash并不是那么好,所以有人有更好的主意吗?

[EDIT - updated as per BMitch's suggestion] [编辑-根据BMitch的建议进行了更新]

I have modified the script to look like this: 我已将脚本修改为如下所示:

#!/bin/sh

if [ -z "$USERNAME" ]; then
        (sleep 10; kill $$) & exec mosquitto_sub -t '$SYS/#' -C 1 | grep -v Error || exit 1 "$@"
else
        (sleep 10; kill $$) & exec mosquitto_sub -t '$SYS/#' -u $USERNAME -P $PASSWORD -C 1 | grep -v Error || exit 1  "$@"
fi

but running it just gives the following output: 但是运行它只会得到以下输出:

Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Terminated
root@e30e9cadd8fc:/# Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.

There's a bash FAQ on how to timeout your running script. 关于如何超时运行脚本,有一个bash常见问题解答 Try replacing mosquitto_sub with a mosquitto_sub_timeout.sh that contains the following: 尝试将mosquitto_sub替换为包含以下内容的mosquitto_sub_timeout.sh:

#!/bin/bash
(sleep 10; kill $$) & exec mosquitto_sub "$@"

Then your healthcheck script will look like: 然后,您的运行状况检查脚本将如下所示:

if [ -z "$USERNAME" ]; then
 mosquitto_sub_timeout.sh -t '$SYS/#' -C 1 | grep -v Error || exit 1
else 
 mosquitto_sub_timeout.sh -t '$SYS/#' -C 1 -u $USERNAME -P $PASSWRD | grep -v Error || exit 1
fi

And one last update, as mentioned in the bash FAQ, the timeout command may be the best solution of them all as long as it's installed in your container: 正如bash FAQ中提到的那样,最后一次更新,只要安装在您的容器中,timeout命令可能是所有它们的最佳解决方案:

if [ -z "$USERNAME" ]; then
 timeout --foreground 10 mosquitto_sub -t '$SYS/#' -C 1 | grep -v Error || exit 1
else 
 timeout --foreground 10 mosquitto_sub -t '$SYS/#' -C 1 -u $USERNAME -P $PASSWRD | grep -v Error || exit 1
fi

(changed idea of BMitch ) BMitch的想法改变了)

I had to change the timeout argument from --foreground to -t and escape the $ by putting one more $ sign before it. 我不得不超时参数从改变--foreground-t和逃避$通过将多了一个$符号前。

In my docker-compose it look next( keep in mind that username and password are removed, you still can add them - just escape the $ sign again ): version: '3' services: mosquitto: image: 'eclipse-mosquitto:1.6.7' container_name: mosquitto hostname: mosquitto volumes: - ./mosquitto/data:/mosquitto/data - ./mosquitto/log:/mosquitto/log - ./mosquitto/config:/mosquitto/config networks: - cluster restart: on-failure healthcheck: test: ["CMD-SHELL", "timeout -t 5 mosquitto_sub -t '$$SYS/#' -C 1 | grep -v Error || exit 1"] interval: 10s timeout: 10s retries: 6 在我的docker-compose中,查找下一个( 请记住,用户名和密码已删除,您仍然可以添加它们-只需再次转义$符号 ): version: '3' services: mosquitto: image: 'eclipse-mosquitto:1.6.7' container_name: mosquitto hostname: mosquitto volumes: - ./mosquitto/data:/mosquitto/data - ./mosquitto/log:/mosquitto/log - ./mosquitto/config:/mosquitto/config networks: - cluster restart: on-failure healthcheck: test: ["CMD-SHELL", "timeout -t 5 mosquitto_sub -t '$$SYS/#' -C 1 | grep -v Error || exit 1"] interval: 10s timeout: 10s retries: 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM