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:


Friday, July 31, 2020

starting my own ddns.

have my own vm with westhost… barilin.com

using cron.

# Updated 2/9/19
2,7,12,17,22,27,32,37,42,47,52,57 * * * * /root/bin/reportipaddress &> /dev/null

I have it running the foloowing every 5 minutes…
pushing the IP Address up to my VM

root@cp1:~/bin# cat reportipaddress
DATE=/usr/bin/date
QUERY=""
QUERY="/usr/bin/curl http://barilin.com/whatipaddressami"
$QUERY
if [ "$QUERY" != "" ]; then
  $ECHO $DATE >> /var/log/reportipaddress.log
fi;
root@cp1:~/bin#

I have it report into my vm and have it query a non exsistant url..
Then I parse the logs every 5 mintues with cron

5,10,15,20,25,30,35,40,45,50,55,59 * * * * ~/bin/5minutes.sh

cron calls a script

[uscopley@sl-508-21 bin]$ cat 5minutes.sh
CAT=/usr/bin/cat
FILE="/home/uscopley/access-logs/barilin.com"
HEADHTML="/home/uscopley/bin/5minutes.head"
TAILHTML="/home/uscopley/bin/5minutes.tail"
GREP=/usr/bin/grep
TAIL="/usr/bin/tail -10"
STRING=whatipaddressami
REPORT="/home/uscopley/www/reportipaddress.html"
#
#
#
$CAT $HEADHTML > $REPORT
$CAT $FILE | $GREP $STRING | $TAIL >> $REPORT
$CAT $TAILHTML >> $REPORT
#
#
#

#cat ~/access-logs/barilin.com | grep whatipaddressami | tail -3 > ~/www/reportipaddress.html

The next part hasn’t been written yet. I’m going to grab that ip address that’s reported
and shove it into dns… With my home A name.

Thursday, July 30, 2020

whole lotta pies / Download config on reboot

So, I like to have all my pies download a set of configs at boot. Allows me to make a change
a centeralized location and push it out to everyone (When they reboot)

All pies get this in their /etc/rc.d/rc.local

/home/zero/updates/update.sh
#
/etc/sethostalias.sh > /etc/hostalias
#
/usr/sbin/netdate ntp1 >> /home/netdate/n3.netdate.log
#
sleep 20;
/etc/rc.d/rc.ntpd restart
#

and then of course /etc/fstab has a samba mounted share with the following

//cp1/zero      /home/zero      cifs      user=timc,pass=[password]       0   0

and the /home/zero/updates/update.sh has the following

#
#  Setup some variables
#
CP="/usr/bin/cp"
DIR="/home/zero/updates"
MKDIR="/usr/bin/mkdir"
#
#  Updatre aliases and hosts and profiles from CP1
#
$CP $DIR/sethostalias.sh /etc
$CP $DIR/hosts /etc
$CP $DIR/profile /etc
$CP $DIR/alias /etc
#
#  Make a bin directory for root on host
#
EDIR="/root/bin"
if [ ! -d $EDIR ]; then
  $MKDIR $EDIR
  $CP $DIR/bin/ $EDIR
fi
#
#  Couple files for Serial stuff on PI's
#
$CP $DIR/config.txt /root
$CP $DIR/cmdline.txt /root
$CP $DIR/inittab /root
#
# Couple fixes for snmp / ntp
$CP $DIR/rc.M /etc/rc.d/
$CP $DIR/snmpd.conf /etc/snmp/
$CP $DIR/ntp.conf /etc/

with all the appropiate file goodies. This seems to fluctuate based upon what I’m doing…