Difference between revisions of "MQTT monitoring Daemon"
Jump to navigation
Jump to search
(Created page with "== Requirements: == * <code>apt install mosquitto-clients</code> * <code>apt install jq</code> == The systemd Unit == <code>/etc/systemd/system/MQTTmon.service</code> <syntaxhighlight lang="bash"> [Unit] Description=MQTT Monitoring Daemon After=network.target Requires=network.target [Service] Type=simple User=root ExecStart=/usr/bin/MQTTmon Restart=always [Install] WantedBy=multi-user.target </syntaxhighlight> * <code>systemctl daemon-reload</code> * <code>systemc...") |
|||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== | {{{!}} class="wikitable" style="float: right; width: 322px;" border="2" | ||
{{!}}+ Proven on: | |||
{{!}}- <!-- Debian --> | |||
{{!}} style="text-align: center; width: 60px;" {{!}} [[File:Logo Debian.png{{!}}60px{{!}}link=https://www.debian.org/{{!}}center{{!}}middle{{!}}frameless]] | |||
{{!}} style="text-align: center; width: 40px;" {{!}} 11 (bullseye) | |||
{{!}} | |||
<br> | |||
{{!}}} | |||
<code>/ | <p class="mwt-heading" >An example of what's possible...</p> | ||
= Requirements: = | |||
* <code>sudo apt install mosquitto-clients</code> | |||
* <code>sudo apt install jq</code> | |||
<syntaxhighlight lang="bash"> | = The systemd Service Unit = | ||
<code>sudo vi /etc/systemd/system/MQTTmon.service</code> | |||
<syntaxhighlight lang="bash" line="1"> | |||
[Unit] | [Unit] | ||
Line 24: | Line 35: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* <code>systemctl daemon-reload</code> | * Any time you create or edit a service unit: | ||
* <code>systemctl restart MQTTmon.service</code> | ** <code>sudo systemctl daemon-reload</code> | ||
* <code>systemctl status MQTTmon.service</code> | ** <code>sudo systemctl restart MQTTmon.service</code> | ||
* Check on how it's going... | |||
** <code>sudo systemctl status MQTTmon.service</code> | |||
= The Script = | = The Script = | ||
<code>/usr/bin/MQTTmon</code> | <code>sudo vi /usr/bin/MQTTmon</code> | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash" line="1"> | ||
#!/bin/bash | #!/bin/bash | ||
Line 64: | Line 77: | ||
= Sending it messages = | = Sending it messages = | ||
In these examples, the machine (LXC) used for testng is named '''poop2''' | In these examples, the machine (an LXC) used for testng is named '''poop2''' | ||
* Telling it to shut down... | * Telling it to shut down... | ||
** <code>mosquitto_pub -h LXC-Mosquitto -t turd -m '{"Target":"poop2","Message": "It is time to DIE","Command":"DIE"}'</code> | ** <code>mosquitto_pub -h LXC-Mosquitto -t turd -m '{"Target":"poop2","Message": "It is time to DIE","Command":"DIE"}'</code> |
Latest revision as of 18:53, 14 April 2023
11 (bullseye) |
|
An example of what's possible...
Requirements:
sudo apt install mosquitto-clients
sudo apt install jq
The systemd Service Unit
sudo vi /etc/systemd/system/MQTTmon.service
[Unit]
Description=MQTT Monitoring Daemon
After=network.target
Requires=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/bin/MQTTmon
Restart=always
[Install]
WantedBy=multi-user.target
- Any time you create or edit a service unit:
sudo systemctl daemon-reload
sudo systemctl restart MQTTmon.service
- Check on how it's going...
sudo systemctl status MQTTmon.service
The Script
sudo vi /usr/bin/MQTTmon
#!/bin/bash
BROKER="LXC-Mosquitto"
TOPIC="turd"
MQTT=`/usr/bin/mosquitto_sub -C 1 -h $BROKER -t $TOPIC`
TARGET=`echo $MQTT {{!}} jq '.Target'`
MESSAGE=`echo $MQTT {{!}} jq '.Message'`
COMMAND=`echo ${MQTT} {{!}} jq '.Command'`
HOST=\"`hostname`\"
if [[ $TARGET == $HOST ]]
then
/root/Logit $TARGET , $COMMAND , $MESSAGE
if [[ $COMMAND == "\"MSG\"" ]] ; then
/root/Logit "Message is: " $MESSAGE
fi
if [[ $COMMAND == "\"WALL\"" ]] ; then
wall -n $MESSAGE
fi
if [[ $COMMAND == "\"DIE\"" ]] ; then
shutdown
fi
else
/root/Logit $TARGET " got a message too, but this is " $HOST " so we don't GAF."
fi
Sending it messages
In these examples, the machine (an LXC) used for testng is named poop2
- Telling it to shut down...
mosquitto_pub -h LXC-Mosquitto -t turd -m '{"Target":"poop2","Message": "It is time to DIE","Command":"DIE"}'
- Send a message to all logged-in users terminals
mosquitto_pub -h LXC-Mosquitto -t turd -m '{"Target":"poop2","Message": "This is a TURD","Command":"WALL"}'
- Just log a message (using random logger for testing)
mosquitto_pub -h LXC-Mosquitto -t turd -m '{"Target":"poop2","Message": "This is another TURD","Command":"MSG"}'
- Send a message to a different machine
mosquitto_pub -h LXC-Mosquitto -t turd -m '{"Target":"poop1","Message": "This is a TURD too","Command":"DoIt"}'