Difference between revisions of "IoT - IT Hardware Monitoring with Node-Red"

From The TinkerNet Wiki
Jump to navigation Jump to search
Line 51: Line 51:
 
printf '{\n\t"loadavg":"%s %s %s"\n}\n' "$OneMin" "$FiveMin" "$FifteenMin"
 
printf '{\n\t"loadavg":"%s %s %s"\n}\n' "$OneMin" "$FiveMin" "$FifteenMin"
  
 +
</syntaxhighlight>
 +
 +
==== Combining These ====
 +
 +
<syntaxhighlight lang="bash" 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 5-9`
 +
FifteenMin=`cat /proc/loadavg | cut -b 11-14`
 +
fi
 +
 +
printf '{\n\t"uptime":"%s:%s:%s:%s",\n' "$days" "$hours" "$minutes" "$seconds"
 +
printf '\t"loadavg":"%s %s %s"\n}\n' "$OneMin" "$FiveMin" "$FifteenMin"
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Dealing with the information in Node-Red ==
 
== Dealing with the information in Node-Red ==
 
<br />
 
<br />

Revision as of 14:07, 11 October 2020

I run Linux here...

This means that most of my instructions will be Linux-based. But, If I work out how to do things under Windows or Mac, I'll try to write those up as well.

Getting the information from the hardware

Sensor data

lm-sensors 3.6.0 does JSON output (w00t w00t)

Mosquitto can be given the JSON output from lm-sensors

(This may be just a little TOO easy... :P )

/usr/bin/mosquitto_pub -h automation.DOMAIN.TLD -t MachineName -m "`/usr/local/bin/sensors -j`"

This, of course, can be run by cron. On my machines, I've told cron to run it once per minute.

Other data

Pretty much anything you can persuade your machine to spit out as plain text can be turned into MQTT messages using your scripting language of choice. (or even get hard-core & write some full-on code.)

The /proc filesystem is an excellent source of system information. (One heck of a rabbithole to dive down can be found by typing man proc into a terminal.)

I have a little script on my machines to spit out stats. It formats the output as JSON to be transferred via MQTT. The following scripts spit out individual parts. My script (WIP) is a combination of them all.

uptime

 1 # Get uptime
 2 if [ -f "/proc/uptime" ]; then
 3 uptime=`cat /proc/uptime`
 4 uptime=${uptime%%.*}
 5 seconds=$(( uptime%60 ))
 6 minutes=$(( uptime/60%60 ))
 7 hours=$(( uptime/60/60%24 ))
 8 days=$(( uptime/60/60/24 ))
 9 fi
10 
11 printf '{\n\t"uptime":"%s:%s:%s:%s",\n}\n' "$days" "$hours" "$minutes" "$seconds"

Load Averages

1 # Get loadavg
2 if [ -f "/proc/loadavg" ]; then
3 OneMin=`cat /proc/loadavg | cut -b 1-4`
4 FiveMin=`cat /proc/loadavg | cut -b 5-9`
5 FifteenMin=`cat /proc/loadavg | cut -b 11-14`
6 fi
7 
8 printf '{\n\t"loadavg":"%s %s %s"\n}\n' "$OneMin" "$FiveMin" "$FifteenMin"

Combining These

 1 # Get uptime
 2 if [ -f "/proc/uptime" ]; then
 3 uptime=`cat /proc/uptime`
 4 uptime=${uptime%%.*}
 5 seconds=$(( uptime%60 ))
 6 minutes=$(( uptime/60%60 ))
 7 hours=$(( uptime/60/60%24 ))
 8 days=$(( uptime/60/60/24 ))
 9 fi
10 
11 # Get loadavg
12 if [ -f "/proc/loadavg" ]; then
13 OneMin=`cat /proc/loadavg | cut -b 1-4`
14 FiveMin=`cat /proc/loadavg | cut -b 5-9`
15 FifteenMin=`cat /proc/loadavg | cut -b 11-14`
16 fi
17 
18 printf '{\n\t"uptime":"%s:%s:%s:%s",\n' "$days" "$hours" "$minutes" "$seconds"
19 printf '\t"loadavg":"%s %s %s"\n}\n' "$OneMin" "$FiveMin" "$FifteenMin"

Dealing with the information in Node-Red