Wednesday, September 2, 2020

RAID STATS / Temperature to MRTG

MRTG wants 4 attributes.
In / Out / Uptime / Hostname.

So I put those 4 things into a file…. n4.$DRIVE
for each drive in the array. Then I’m going to use cron to run this
script (Below) which puts it on a samba share that has the server
that’s running MRTG. Which will also have a cron job that will be
polling that file every 5 minutes. Then running MRTG to grab that
data and store it in it’s log file.

Then It’ll give me out 5 sets of graphs with the temperature displayed
over the last 24 / 48 hours.

I used Drive Temp from SYSCTL
Raspberian Temperature to grab the CPU Temp (Which is an independent device from the drives)
as well as grabbing uptime and hostname…

root@nas4:~/bin# cat raidtemp.mrtg
#!/bin/bash
##########################################################
#
#    Use Smartctl to get drive temperature
#
##########################################################
GREP=/usr/bin/grep
CAT=/usr/bin/cat
ECHO=/usr/bin/echo
SCTL="/usr/sbin/smartctl --all"
FIND="ature_Cels"
DATE=`/usr/bin/date +"%m.%d.%y.%H.%M"`
AWK=/usr/bin/awk
CPUTEMP="/root/bin/temp"
FINDCPU="temp"
UPTIME=`/usr/bin/uptime`
HOSTNAME=`$CAT /etc/HOSTNAME`
FILELOC=/home/digitemp/n4

#
#  It'll ignore unfound drives, so list em all
#
DRIVES=(/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf)
#
#
#
for i in "${DRIVES[@]}"
 do
   DRIVE=`$ECHO $i | $AWK -F/ '{print $3}'`
   TEMP=`$SCTL $i | $GREP $FIND | $AWK -F- '{print $2}' | $AWK '{print $1}'`
   CPUTMP=`$CPUTEMP | $GREP $FINDCPU | $AWK -F: '{print $2}' | $AWK -F. '{print $1}'`

   if [ ! -z "$TEMP" ]
   then
    $ECHO $TEMP > $FILELOC.$DRIVE
    $ECHO $CPUTMP >> $FILELOC.$DRIVE
    $ECHO $UPTIME >> $FILELOC.$DRIVE
    $ECHO $HOSTNAME >> $FILELOC.$DRIVE
   fi
 done

root@nas4:~/bin#

Getting temperature from the CPU is fairly simple / straightforward as well

root@nas4:~/bin# cat temp
#!/bin/bash

# SARPi Project : http://sarpi.fatdog.nl - cpu_status.sh
#
# Raspberry Pi 2, 3, 4 - CPU clock frequency and thermal status.
# This script outputs the current status of the CPU clock speed (MHz)
# and core temperature (Celsius) for monitoring or testing purposes
# while under load or idle.
#
# Usage -
# Default command:   watch ./cpu_status.sh     # 2 seconds refresh
# Timed refresh:     watch -n<number of seconds or 0> ./cpu_status.sh
# With highlights:   watch -d -n0 ./cpu_status.sh
# Perm highlights:   watch -d=cumulative -n0 ./cpu_status.sh
#
# Exaga : 15 Jan 2018 - progenitor
#         29 Jun 2019 - updated with rpi model & hw revision
#         03 Jul 2019 - updated with system uptime
#

# Get RPi model and hardware revision
RPiModel=$(dmesg | grep "Machine model:" | cut -d' ' -f10-16)
RPiHWRev=$(cat /proc/cpuinfo | grep Revision | cut -d' ' -f2)

# Get cpu_status function
cpu_status () {
# Get current CPU frequency (All 4 cores)
CPU0freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq)
cpuFreq0=$(($CPU0freq/1000))
if [ -f /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq ]; then
CPU1freq=$(cat /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq)
cpuFreq1=$(($CPU1freq/1000))
CPU2freq=$(cat /sys/devices/system/cpu/cpu2/cpufreq/cpuinfo_cur_freq)
cpuFreq2=$(($CPU2freq/1000))
CPU3freq=$(cat /sys/devices/system/cpu/cpu3/cpufreq/cpuinfo_cur_freq)
cpuFreq3=$(($CPU3freq/1000))
fi

# Ouput RPi model/version and system uptime to terminal
echo Device": "$RPiModel
echo HW Rev": "$RPiHWRev
echo
echo Uptime":"$(uptime)
echo

# Output CPU clock status to terminal
echo CPU Clock Speed
echo CPU 0 freq": "$cpuFreq0"MHz"
if [ -f /sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_cur_freq ]; then
echo CPU 1 freq": "$cpuFreq1"MHz"
echo CPU 2 freq": "$cpuFreq2"MHz"
echo CPU 3 freq": "$cpuFreq3"MHz"
fi
echo

# Get CPU thermal status and output to terminal
echo CPU Thermal Status
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % $cpuTemp1))
echo CPU temp": "$cpuTemp1"."$cpuTempM"°C"

# Output exit method to terminal
echo && echo && echo && echo
echo "[Press CTRL+C to exit.]"
}

# Roll cpu_status function
cpu_status

exit 0

#eofroot@nas4:~/bin#

And the Finished Product: