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

From The TinkerNet Wiki
Jump to navigation Jump to search
Line 40: Line 40:
  
 
==== Load Averages ====
 
==== Load Averages ====
Still working on this one...
 
  
 
<syntaxhighlight lang="bash" line="1">
 
<syntaxhighlight lang="bash" line="1">
 
# Get loadavg
 
# Get loadavg
 
if [ -f "/proc/loadavg" ]; then
 
if [ -f "/proc/loadavg" ]; then
loadavg=`cat /proc/loadavg`
+
OneMin=`cat /proc/loadavg | cut -b 1-4`
#loadavg=${loadavg%%.*}
+
FiveMin=`cat /proc/loadavg | cut -b 5-9`
 +
FifteenMin=`cat /proc/loadavg | cut -b 11-14`
 
fi
 
fi
 
# Place holder data
 
OneMin="0.00"
 
FiveMin="0.00"
 
FifteenMin="0.00"
 
  
 
printf '{\n\t"loadavg":"%s %s %s"\n}\n' "$OneMin" "$FiveMin" "$FifteenMin"
 
printf '{\n\t"loadavg":"%s %s %s"\n}\n' "$OneMin" "$FiveMin" "$FifteenMin"

Revision as of 14:05, 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"

Dealing with the information in Node-Red