Difference between revisions of "Linux - Stats over MQTT"
Jump to navigation
Jump to search
(Created page with "=== /usr/local/bin/MQTTstats.sh === <syntaxhighlight lang="sh" line="1"> # 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 %...") |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=== /usr/local/bin/MQTTstats.sh | {{{!}} 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> | |||
{{!}}- <!-- Ubuntu --> | |||
{{!}} 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> | |||
<syntaxhighlight lang="sh" line="1"> | <syntaxhighlight lang="sh" line="1"> | ||
# Get uptime | # Get uptime | ||
Line 37: | 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 = | |||
Add this line by editing roots crontab | Add this line by editing roots crontab | ||
Line 45: | 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 | ||
Latest revision as of 18:05, 25 November 2023
11 (bullseye) |
| |
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