Difference between revisions of "Linux - Stats over MQTT"

From Da Nerd Mage Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Requirements: ==
{{{!}} class="wikitable" style="float: right; width: 322px;" border="2"
* <code>apt install mosquitto-clients</code>
{{!}}+ Proven on:
* <code>apt install lm-sensors</code>
{{!}}- &lt;!-- Debian --&gt;
{{!}} 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>
{{!}}- &lt;!-- Ubuntu --&gt;
{{!}} style="text-align: center; width: 65.7px;" {{!}} [[File:Logo Ubuntu.png{{!}}60px{{!}}link=https://ubuntu.com/{{!}}center{{!}}middle{{!}}frameless]]
{{!}} 20.04.3
{{!}}
 
<br>
{{!}}}
 
= Requirements: =
* <code>sudo apt install mosquitto-clients</code>
* <code>sudo apt install lm-sensors</code>
 
= The Script =
 
* <code>vi /usr/local/bin/MQTTstats.sh</code>


=== /usr/local/bin/MQTTstats.sh ===
<syntaxhighlight lang="sh" line="1">
<syntaxhighlight lang="sh" line="1">
# Get uptime
# Get uptime
Line 41: Line 60:
* <code>sudo chmod +x /usr/local/bin/MQTTstats.sh</code>
* <code>sudo chmod +x /usr/local/bin/MQTTstats.sh</code>


=== The cron job ===
= The cron job =
Add this line by editing roots crontab
Add this line by editing roots crontab


Line 49: Line 68:
* <code>* * * * * /usr/bin/mosquitto_pub -h broker.domain.tld -t `hostname` -m "`/usr/local/bin/MQTTstats.sh`"</code>
* <code>* * * * * /usr/bin/mosquitto_pub -h broker.domain.tld -t `hostname` -m "`/usr/local/bin/MQTTstats.sh`"</code>
replace '''broker.domain.tld''' with the address of your broker
replace '''broker.domain.tld''' with the address of your broker
(Yes... It's true... If you haven't yet installed the [[AutomationServer_-_MQTT#Install_client_tools_for_testing_etc{{!}}Mosquitto Clients]], it's not gonna work... :P )

Latest revision as of 18:05, 25 November 2023

Proven on:
Logo Debian.png
11 (bullseye)


Logo Ubuntu.png
20.04.3


Requirements:

  • sudo apt install mosquitto-clients
  • sudo apt install lm-sensors

The Script

  • vi /usr/local/bin/MQTTstats.sh
# Get uptime
if [ -f "/proc/uptime" ]; then
uptime=`cat /proc/uptime`
uptime=${uptime%%.*}
seconds=$(( uptime%60 ))
minutes=$(( uptime/60%60 ))
hours=$(( uptime/60/60%24 ))
days=$(( uptime/60/60/24 ))
fi

# Get loadavg
if [ -f "/proc/loadavg" ]; then
OneMin=`cat /proc/loadavg {{!}} cut -b 1-4`
FiveMin=`cat /proc/loadavg {{!}} cut -b 6-9`
FifteenMin=`cat /proc/loadavg {{!}} cut -b 11-14`
fi

# Get % Memory usage
MemUsed=`/usr/bin/free -t {{!}} grep "Mem:" {{!}} awk '{print (1-($7/$2))*100}'`

printf '{\t"uptime":"%s:%s:%s:%s",\n' "$days" "$hours" "$minutes" "$seconds"
printf '\t"loadavg1min":%.2f,\n' "$OneMin"
printf '\t"loadavg5min":%.2f,\n' "$FiveMin"
printf '\t"loadavg15min":%.2f,\n' "$FifteenMin"
printf '\t"loadavg":"%s %s %s",\n' "$OneMin" "$FiveMin" "$FifteenMin"
printf '\t"memused":%.2f,\n' "$MemUsed"
printf '"SENSORS":'
printf '%s' `/usr/bin/sensors -j`
printf '}\n'

Don't forget to make it executable...

  • sudo chmod +x /usr/local/bin/MQTTstats.sh

The cron job

Add this line by editing roots crontab

  • sudo crontab -e

& add this line at the end:

  • * * * * * /usr/bin/mosquitto_pub -h broker.domain.tld -t `hostname` -m "`/usr/local/bin/MQTTstats.sh`"

replace broker.domain.tld with the address of your broker