2
Some BASH scripts I've written & use regularly
Hey everyone! I've made this page to share the scripts I've created to be run on my home server (Raspberry Pi). Some are useful; some merely fun. Please, feel free to copy and use these in full or in part. If you have any recommendations to them - I plan on adding a comments section below each script.
# ------------------------------------------------------------ #
Script Name: getStats.sh
Status:In use
Description: The purpose of this script is multi-faceted, but the end-result is to generate a whole bunch of graphs that are displayed on an access-controlled site.
Pre-requisites:
  1. RRDTool
  2. moreultils (chronic) - To run the script silently
					
#! /bin/bash

monthly_reset () {
# CHECK FOR THE FILE
if [ ! -e /usr/local/bin/system/old_month ]; then
# CREATE IT IF IT DOESN'T EXIST WITH THIS MONTHS DATE
echo `date` | awk '{print $2,$6}' > /usr/local/bin/system/old_month
fi

# CREATE A NEW FILE EVERY TIME THIS IS RUN WITH THIS MONTHS DATE
echo `date` | awk '{print $2,$6}' > /usr/local/bin/system/this_month

# CHECK IF OLD_MONTH AND THIS_MONTH ARE THE SAME
if ! cmp /usr/local/bin/system/this_month /usr/local/bin/system/old_month > /dev/null 2>&1 ; then
echo 0 > /usr/local/bin/system/rx_bytes_old # ZERO OUT ALL THE VALUES
echo 0 > /usr/local/bin/system/rx_bytes
echo 0 > /usr/local/bin/system/rx_running
echo 0 > /usr/local/bin/system/tx_bytes_old
echo 0 > /usr/local/bin/system/tx_bytes
echo 0 > /usr/local/bin/system/tx_running
echo 0 > /usr/local/bin/system/wlan_rx_bytes_old
echo 0 > /usr/local/bin/system/wlan_rx_bytes
echo 0 > /usr/local/bin/system/wlan_rx_running
echo 0 > /usr/local/bin/system/wlan_tx_bytes_old
echo 0 > /usr/local/bin/system/wlan_tx_bytes
echo 0 > /usr/local/bin/system/wlan_tx_running
echo `date` | awk '{print $2,$6}' > /usr/local/bin/system/old_month # UPDATE THE VALUE IN OLD_MONTH SO THAT THEY WILL MATCH NEXT TIME
echo 1 > /usr/local/bin/system/rebooting # 1 IF REBOOTING, 0 IF NOT, USED FOR ROLL-OVER CALCULATIONS
reboot now # REBOOT
exit 0 # AND EXIT THIS SCRIPT
else
echo 0 > /usr/local/bin/system/rebooting
fi # AFTER REBOOTING, THE TX_BYTES AND RX_BYTES WILL ALL BE 0
}

# COPY IN NEW VALUES IN ONE SHOT TO MAINTAIN DATA CONSISTENCY
# ACTUAL FILES
cp /sys/class/net/eth0/statistics/rx_bytes /usr/local/bin/system/rx_actual
cp /sys/class/net/eth0/statistics/tx_bytes /usr/local/bin/system/tx_actual
cp /sys/class/net/wlan0/statistics/rx_bytes /usr/local/bin/system/wlan_rx_actual
cp /sys/class/net/wlan0/statistics/tx_bytes /usr/local/bin/system/wlan_tx_actual

# CALL THE MONTHLY SCRIPT _ COMMENT THIS OUT IF YOU DON'T WANT TO RESET THE COUNTER EACH MONTH
monthly_reset

# CHECK FOR FILE IN /USR/LOCAL/BIN/SYSTEM
# BYTES FILES
if [ ! -e /usr/local/bin/system/rx_bytes ]; then # IF RX_BYTES DOESN'T EXIST
cp /usr/local/bin/system/rx_actual /usr/local/bin/system/rx_bytes # MAKE A NEW COPY FROM THE ACTUALFILE
else
mv /usr/local/bin/system/rx_bytes /usr/local/bin/system/rx_bytes_old # IF IT DOES EXIST, RENAME IT
cp /usr/local/bin/system/rx_actual /usr/local/bin/system/rx_bytes # AND COPY IN A NEW ONE
fi
if [ ! -e /usr/local/bin/system/tx_bytes ]; then # REPEAT THIS FOR TX_BYTES
cp /usr/local/bin/system/tx_actual /usr/local/bin/system/tx_bytes
else
mv /usr/local/bin/system/tx_bytes /usr/local/bin/system/tx_bytes_old
cp /usr/local/bin/system/tx_actual /usr/local/bin/system/tx_bytes
fi

# BYTES FILES (WLAN0)
if [ ! -e /usr/local/bin/system/wlan_rx_bytes ]; then # IF RX_BYTES DOESN'T EXIST
cp /usr/local/bin/system/wlan_rx_actual /usr/local/bin/system/wlan_rx_bytes # MAKE A NEW COPY FROM THE ACTUALFILE
else
mv /usr/local/bin/system/wlan_rx_bytes /usr/local/bin/system/wlan_rx_bytes_old # IF IT DOES EXIST, RENAME IT
cp /usr/local/bin/system/wlan_rx_actual /usr/local/bin/system/wlan_rx_bytes # AND COPY IN A NEW ONE
fi
if [ ! -e /usr/local/bin/system/wlan_tx_bytes ]; then # REPEAT THIS FOR TX_BYTES
cp /usr/local/bin/system/wlan_tx_actual /usr/local/bin/system/wlan_tx_bytes
else
mv /usr/local/bin/system/wlan_tx_bytes /usr/local/bin/system/wlan_tx_bytes_old
cp /usr/local/bin/system/wlan_tx_actual /usr/local/bin/system/wlan_tx_bytes
fi

# RUNNING TOTAL FILES
if [ ! -e /usr/local/bin/system/rx_running ]; then
cp /usr/local/bin/system/rx_bytes /usr/local/bin/system/rx_running
fi
if [ ! -e /usr/local/bin/system/tx_running ]; then
cp /usr/local/bin/system/tx_bytes /usr/local/bin/system/tx_running
fi

# RUNNING TOTAL FILES (WLAN0)
if [ ! -e /usr/local/bin/system/wlan_rx_running ]; then
cp /usr/local/bin/system/wlan_rx_bytes /usr/local/bin/system/wlan_rx_running
fi
if [ ! -e /usr/local/bin/system/wlan_tx_running ]; then
cp /usr/local/bin/system/wlan_tx_bytes /usr/local/bin/system/wlan_tx_running
fi

# OLD FILES
if [ ! -e /usr/local/bin/system/rx_bytes_old ]; then
cp /usr/local/bin/system/rx_bytes /usr/local/bin/system/rx_bytes_old
fi
if [ ! -e /usr/local/bin/system/tx_bytes_old ]; then
cp /usr/local/bin/system/tx_bytes /usr/local/bin/system/tx_bytes_old
fi

# OLD FILES (WLAN0)
if [ ! -e /usr/local/bin/system/wlan_rx_bytes_old ]; then
cp /usr/local/bin/system/wlan_rx_bytes /usr/local/bin/system/wlan_rx_bytes_old
fi
if [ ! -e /usr/local/bin/system/wlan_tx_bytes_old ]; then
cp /usr/local/bin/system/wlan_tx_bytes /usr/local/bin/system/wlan_tx_bytes_old
fi


# SET VARIABLES FOR CALCULATION
OLDRX=`cat /usr/local/bin/system/rx_bytes_old`
NEWRX=`cat /usr/local/bin/system/rx_bytes`
RUNRX=`cat /usr/local/bin/system/rx_running`
OLDTX=`cat /usr/local/bin/system/tx_bytes_old`
NEWTX=`cat /usr/local/bin/system/tx_bytes`
RUNTX=`cat /usr/local/bin/system/tx_running`
OLDWRX=`cat /usr/local/bin/system/wlan_rx_bytes_old`
NEWWRX=`cat /usr/local/bin/system/wlan_rx_bytes`
RUNWRX=`cat /usr/local/bin/system/wlan_rx_running`
OLDWTX=`cat /usr/local/bin/system/wlan_tx_bytes_old`
NEWWTX=`cat /usr/local/bin/system/wlan_tx_bytes`
RUNWTX=`cat /usr/local/bin/system/wlan_tx_running`
REBOOTING=`cat /usr/local/bin/system/rebooting`
MAX=4294967296

# COMPARE AND DO MATH
if [ $NEWRX -lt $OLDRX ]; then # IF NEW VALUE IS LESS THAN OLD VALUE (max reached and LOOPED AROUND)
if [ $REBOOTING -eq 0 ]; then # AND IF THE PI HAS NOT JUST BEEN REBOOTED
TOPRX=`expr $MAX - $OLDRX` # SUBTRACT THE OLD VALUE FROM THE MAX VALUE
USERX=`expr $TOPRX + $RUNRX` # ADD IT TO RUNNING TOTAL
$USERX=`expr $USERX + $NEWRX` # ADD THAT TO THE NEW VALUE
echo $USERX > /usr/local/bin/system/rx_running # OUTPUT THAT TO THE NEW RUNNING TOTAL
fi
else # OTHERWISE (it hasn't looped around)
TOPRX=`expr $NEWRX - $OLDRX` # SUBTRACT THE OLD VALUE FROM THE NEW
USERX=`expr $RUNRX + $TOPRX` # ADD IT TO THE RUNNING VALUE
echo $USERX > /usr/local/bin/system/rx_running # OUTPUT THAT TO THE NEW RUNNING TOTAL
fi
if [ $NEWTX -lt $OLDTX ]; then # REPEAT ABOVE FOR UPLOADS
if [ $REBOOTING -eq 0 ]; then
TOPTX=`expr $MAX - $OLDTX`
USETX=`expr $TOPTX + $RUNTX`
$USETX=`expr $USETX + $NEWTX`
echo $USETX > /usr/local/bin/system/tx_running
fi
else
TOPTX=`expr $NEWTX - $OLDTX`
USETX=`expr $RUNTX + $TOPTX`
echo $USETX > /usr/local/bin/system/tx_running
fi
#COMPARE AND DO MATH (WLAN0)
if [ $NEWWRX -lt $OLDWRX ]; then # IF NEW VALUE IS LESS THAN OLD VALUE (max reached and LOOPED AROUND)
if [ $REBOOTING -eq 0 ]; then # AND IF THE PI HAS NOT JUST BEEN REBOOTED
TOPWRX=`expr $MAX - $OLDWRX` # SUBTRACT THE OLD VALUE FROM THE MAX VALUE
USEWRX=`expr $TOPWRX + $RUNWRX` # ADD IT TO RUNNING TOTAL
$USEWRX=`expr $USEWRX + $NEWWRX` # ADD THAT TO THE NEW VALUE
echo $USEWRX > /usr/local/bin/system/wlan_rx_running # OUTPUT THAT TO THE NEW RUNNING TOTAL
fi
else # OTHERWISE (it hasn't looped around)
TOPWRX=`expr $NEWWRX - $OLDWRX` # SUBTRACT THE OLD VALUE FROM THE NEW
USEWRX=`expr $RUNWRX + $TOPWRX` # ADD IT TO THE RUNNING VALUE
echo $USEWRX > /usr/local/bin/system/wlan_rx_running # OUTPUT THAT TO THE NEW RUNNING TOTAL
fi
if [ $NEWWTX -lt $OLDWTX ]; then # REPEAT ABOVE FOR UPLOADS
if [ $REBOOTING -eq 0 ]; then
TOPWTX=`expr $MAX - $OLDWTX`
USEWTX=`expr $TOPWTX + $RUNWTX`
$USEWTX=`expr $USEWTX + $NEWWTX`
echo $USEWTX > /usr/local/bin/system/wlan_tx_running
fi
else
TOPWTX=`expr $NEWWTX - $OLDWTX`
USEWTX=`expr $RUNWTX + $TOPWTX`
echo $USEWTX > /usr/local/bin/system/wlan_tx_running
fi

# GET VALUES AND ASSIGN THEM TO VARIABLES
LOAD=`cat /proc/loadavg | awk '{print $1}'` # LOAD AVERAGES
RXBYTES=`cat /usr/local/bin/system/rx_running` # DOWNLOAD DATA
TXBYTES=`cat /usr/local/bin/system/tx_running` # UPLOAD DATA
WRXBYTES=`cat /usr/local/bin/system/wlan_rx_running` # DOWNLOAD DATA (WLAN0)
WTXBYTES=`cat /usr/local/bin/system/wlan_tx_running` # UPLOAD DATA (WLAN0)
TEMP=`/opt/vc/bin/vcgencmd measure_temp|cut -c6-9` # SoC TEMPERATURE
MEM=`free -b | grep Mem | awk '{print $4/$2 * 100.0}'` # MEMORY USAGE (%)
RRDTOOL=/usr/bin/rrdtool

# WRITE TEMPREATURE TO FILE TO BE INCLUDED IN NON-RRD FASHION
/opt/vc/bin/vcgencmd measure_temp|cut -c6-9 > /var/www/html/include/temp.php

# UPDATE THE RRDs WITH THOSE VALUES
$RRDTOOL update /usr/local/bin/system/load.rrd N:$LOAD
$RRDTOOL update /usr/local/bin/system/data.rrd -t rx:tx:rxc:txc:wrx:wtx:wrxc:wtxc N:$RXBYTES:$TXBYTES:$RXBYTES:$TXBYTES:$WRXBYTES:$WTXBYTES:$WRXBYTES:$WTXBYTES
$RRDTOOL update /usr/local/bin/system/pitemp.rrd N:$TEMP
$RRDTOOL update /usr/local/bin/system/mem.rrd N:$MEM


# DATA
# ETH0
# 36H
$RRDTOOL graph /var/www/html/images/graphs/data36h.png \
--title 'Odin Absolute Traffic (eth0)' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-36h \
'DEF:rx=/usr/local/bin/system/data.rrd:rx:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:tx:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA
# ETH0
# 10D
$RRDTOOL graph /var/www/html/images/graphs/data10d.png \
--title 'Odin Absolute Traffic (eth0) 10 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10d \
'DEF:rx=/usr/local/bin/system/data.rrd:rx:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:tx:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA
# ETH0
# 45D
$RRDTOOL graph /var/www/html/images/graphs/data45d.png \
--title 'Odin Absolute Traffic (eth0) 45 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-45d \
'DEF:rx=/usr/local/bin/system/data.rrd:rx:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:tx:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA
# ETH0
# 18M
$RRDTOOL graph /var/www/html/images/graphs/data18m.png \
--title 'Odin Absolute Traffic (eth0) 18 month' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-1y6m \
'DEF:rx=/usr/local/bin/system/data.rrd:rx:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:tx:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA
# ETH0
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/data10y.png \
--title 'Odin Absolute Traffic (eth0) 10 year' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10y \
'DEF:rx=/usr/local/bin/system/data.rrd:rx:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:tx:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# ETH0
# 36H
$RRDTOOL graph /var/www/html/images/graphs/datac36h.png \
--title 'Odin Traffic (eth0)' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '1' \
--logarithmic \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-36h \
'DEF:rx=/usr/local/bin/system/data.rrd:rxc:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:txc:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# ETH0
# 10D
$RRDTOOL graph /var/www/html/images/graphs/datac10d.png \
--title 'Odin Traffic (eth0) 10 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '1' \
--logarithmic \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10d \
'DEF:rx=/usr/local/bin/system/data.rrd:rxc:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:txc:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# ETH0
# 45D
$RRDTOOL graph /var/www/html/images/graphs/datac45d.png \
--title 'Odin Traffic (eth0) 45 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '1' \
--logarithmic \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-45d \
'DEF:rx=/usr/local/bin/system/data.rrd:rxc:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:txc:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# ETH0
# 18M
$RRDTOOL graph /var/www/html/images/graphs/datac18m.png \
--title 'Odin Traffic (eth0) 18 month' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '1' \
--logarithmic \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-1y6m \
'DEF:rx=/usr/local/bin/system/data.rrd:rxc:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:txc:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# ETH0
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/datac10y.png \
--title 'Odin Traffic (eth0) 10 year' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10y \
'DEF:rx=/usr/local/bin/system/data.rrd:rxc:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:txc:AVERAGE' \
'AREA:tx#00CC00FF:Upload\:' \
'GPRINT:tx:LAST:\:%8.2lf %s]' \
'STACK:rx#0000FFFF:Download\:' \
'GPRINT:rx:LAST:\:%8.2lf %s]\n'

# DATA
# WLAN0
# 36H
$RRDTOOL graph /var/www/html/images/graphs/wdata36h.png \
--title 'Odin Absolute Traffic (wlan0)' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-36h \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrx:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtx:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA
# WLAN0
# 10D
$RRDTOOL graph /var/www/html/images/graphs/wdata10d.png \
--title 'Odin Absolute Traffic (wlan0) 10 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10d \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrx:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtx:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA
# WLAN0
# 45D
$RRDTOOL graph /var/www/html/images/graphs/wdata45d.png \
--title 'Odin Absolute Traffic (wlan0) 45 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-45d \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrx:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtx:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA
# WLAN0
# 18M
$RRDTOOL graph /var/www/html/images/graphs/wdata18m.png \
--title 'Odin Absolute Traffic (wlan0) 18 month' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-1y6m \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrx:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtx:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA
# WLAN0
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/wdata10y.png \
--title 'Odin Absolute Traffic (wlan0) 10 year' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10y \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrx:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtx:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# WLAN0
# 36H
$RRDTOOL graph /var/www/html/images/graphs/wdatac36h.png \
--title 'Odin Traffic (wlan0)' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '1' \
--logarithmic \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-36h \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrxc:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtxc:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# WLAN0
# 10D
$RRDTOOL graph /var/www/html/images/graphs/wdatac10d.png \
--title 'Odin Traffic (wlan0) 10 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '1' \
--logarithmic \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10d \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrxc:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtxc:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# WLAN0
# 45D
$RRDTOOL graph /var/www/html/images/graphs/wdatac45d.png \
--title 'Odin Traffic (wlan0) 45 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '1' \
--logarithmic \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-45d \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrxc:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtxc:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# WLAN0
# 18M
$RRDTOOL graph /var/www/html/images/graphs/wdatac18m.png \
--title 'Odin Traffic (wlan0) 18 month' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '1' \
--logarithmic \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-1y6m \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrxc:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtxc:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# DATA COUNT
# WLAN0
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/wdatac10y.png \
--title 'Odin Traffic (wlan0) 10 year' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Bytes / Second' \
--lower-limit '0' \
--rigid \
--alt-autoscale \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10y \
'DEF:wrx=/usr/local/bin/system/data.rrd:wrxc:AVERAGE' \
'DEF:wtx=/usr/local/bin/system/data.rrd:wtxc:AVERAGE' \
'AREA:wtx#00CC00FF:Upload\:' \
'GPRINT:wtx:LAST:\:%8.2lf %s]' \
'STACK:wrx#0000FFFF:Download\:' \
'GPRINT:wrx:LAST:\:%8.2lf %s]\n'

# LOAD
# 36H
$RRDTOOL graph /var/www/html/images/graphs/load36h.png \
--title 'Odin Load Average' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Load' \
--lower-limit '0' \
--rigid \
--units-exponent '0' \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-36h \
'DEF:load=/usr/local/bin/system/load.rrd:load:AVERAGE' \
'AREA:load#BA00FFFF:Load Average\:' \
'GPRINT:load:LAST:\:%8.2lf %s]'

# LOAD
# 10D
$RRDTOOL graph /var/www/html/images/graphs/load10d.png \
--title 'Odin Load Average 10 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Load' \
--lower-limit '0' \
--rigid \
--units-exponent '0' \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10d \
'DEF:load=/usr/local/bin/system/load.rrd:load:AVERAGE' \
'AREA:load#BA00FFFF:Load Average\:' \
'GPRINT:load:LAST:\:%8.2lf %s]'

# LOAD
# 45D
$RRDTOOL graph /var/www/html/images/graphs/load45d.png \
--title 'Odin Load Average 45 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Load' \
--lower-limit '0' \
--rigid \
--units-exponent '0' \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-45d \
'DEF:load=/usr/local/bin/system/load.rrd:load:AVERAGE' \
'AREA:load#BA00FFFF:Load Average\:' \
'GPRINT:load:LAST:\:%8.2lf %s]'

# LOAD
# 18M
$RRDTOOL graph /var/www/html/images/graphs/load18m.png \
--title 'Odin Load Average 18 month' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Load' \
--lower-limit '0' \
--units-exponent '0' \
--rigid \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-1y6m \
'DEF:load=/usr/local/bin/system/load.rrd:load:AVERAGE' \
'AREA:load#BA00FFFF:Load Average\:' \
'GPRINT:load:LAST:\:%8.2lf %s]'

# LOAD
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/load10y.png \
--title 'Odin Load Average 10 year' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Load' \
--lower-limit '0' \
--units-exponent '0' \
--rigid \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10y \
'DEF:load=/usr/local/bin/system/load.rrd:load:AVERAGE' \
'AREA:load#BA00FFFF:Load Average\:' \
'GPRINT:load:LAST:\:%8.2lf %s]'

# MEMORY
# 36H
$RRDTOOL graph /var/www/html/images/graphs/mem36h.png \
--title 'Odin Memory Usage' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Percent' \
--lower-limit '0' \
--width '640' \
--height '300' \
--full-size-mode \
--start end-36h \
'DEF:mem=/usr/local/bin/system/mem.rrd:mem:AVERAGE' \
'AREA:mem#84A7D5FF:Memory\:' \
'GPRINT:mem:LAST:\:%8.2lf %s]'

# MEMORY
# 10D
$RRDTOOL graph /var/www/html/images/graphs/mem10d.png \
--title 'Odin Memory Usage 10 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Percent' \
--lower-limit '0' \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10d \
'DEF:mem=/usr/local/bin/system/mem.rrd:mem:AVERAGE' \
'AREA:mem#84A7D5FF:Memory\:' \
'GPRINT:mem:LAST:\:%8.2lf %s]'

# MEMORY
# 45D
$RRDTOOL graph /var/www/html/images/graphs/mem45d.png \
--title 'Odin Memory Usage 45 days' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Percent' \
--lower-limit '0' \
--width '640' \
--height '300' \
--full-size-mode \
--start end-45d \
'DEF:mem=/usr/local/bin/system/mem.rrd:mem:AVERAGE' \
'AREA:mem#84A7D5FF:Memory\:' \
'GPRINT:mem:LAST:\:%8.2lf %s]'

# MEMORY
# 18M
$RRDTOOL graph /var/www/html/images/graphs/mem18m.png \
--title 'Odin Memory Usage 18 month' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Percent' \
--lower-limit '0' \
--width '640' \
--height '300' \
--full-size-mode \
--start end-1y6m \
'DEF:mem=/usr/local/bin/system/mem.rrd:mem:AVERAGE' \
'AREA:mem#84A7D5FF:Memory\:' \
'GPRINT:mem:LAST:\:%8.2lf %s]'

# MEMORY
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/mem10y.png \
--title 'Odin Memory Usage 10 year' \
--watermark "Graph Drawn `date`" \
--vertical-label 'Percent' \
--lower-limit '0' \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10y \
'DEF:mem=/usr/local/bin/system/mem.rrd:mem:AVERAGE' \
'AREA:mem#84A7D5FF:Memory\:' \
'GPRINT:mem:LAST:\:%8.2lf %s]'

# SOC TEMP
# 36H
$RRDTOOL graph /var/www/html/images/graphs/pitemp36h.png \
--title 'Odin SoC Temperature' \
--watermark "Graph Drawn `date`" \
--vertical-label '°C' \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-36h \
'DEF:pitemp=/usr/local/bin/system/pitemp.rrd:pitemp:AVERAGE' \
'AREA:pitemp#FF6B10FF:CPU/GPU Temperature\:' \
'GPRINT:pitemp:LAST:\:%8.2lf %s]'

# SOC TEMP
# 10D
$RRDTOOL graph /var/www/html/images/graphs/pitemp10d.png \
--title 'Odin SoC Temperature 10 days' \
--watermark "Graph Drawn `date`" \
--vertical-label '°C' \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10d \
'DEF:pitemp=/usr/local/bin/system/pitemp.rrd:pitemp:AVERAGE' \
'AREA:pitemp#FF6B10FF:CPU/GPU Temperature\:' \
'GPRINT:pitemp:LAST:\:%8.2lf %s]'

# SOC TEMP
# 45D
$RRDTOOL graph /var/www/html/images/graphs/pitemp45d.png \
--title 'Odin SoC Temperature 45 days' \
--watermark "Graph Drawn `date`" \
--vertical-label '°C' \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-45d \
'DEF:pitemp=/usr/local/bin/system/pitemp.rrd:pitemp:AVERAGE' \
'AREA:pitemp#FF6B10FF:CPU/GPU Temperature\:' \
'GPRINT:pitemp:LAST:\:%8.2lf %s]'

# SOC TEMP
# 18M
$RRDTOOL graph /var/www/html/images/graphs/pitemp18m.png \
--title 'Odin SoC Temperature 18 month' \
--watermark "Graph Drawn `date`" \
--vertical-label '°C' \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-1y6m \
'DEF:pitemp=/usr/local/bin/system/pitemp.rrd:pitemp:AVERAGE' \
'AREA:pitemp#FF6B10FF:CPU/GPU Temperature\:' \
'GPRINT:pitemp:LAST:\:%8.2lf %s]'

# SOC TEMP
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/pitemp10y.png \
--title 'Odin SoC Temperature 10 year' \
--watermark "Graph Drawn `date`" \
--alt-autoscale \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10y \
'DEF:pitemp=/usr/local/bin/system/pitemp.rrd:pitemp:AVERAGE' \
'AREA:pitemp#FF6B10FF:CPU/GPU Temperature\:' \
'GPRINT:pitemp:LAST:\:%8.2lf %s]'

#FOR THE MENU SCREEN
$RRDTOOL graph /var/www/html/images/graphs.png \
--lower-limit '0' \
--rigid \
--units=si \
--width '288' \
--height '164' \
--x-grid 'none' \
--y-grid 'none' \
--full-size-mode \
--start end-45d \
'DEF:rx=/usr/local/bin/system/data.rrd:rx:AVERAGE' \
'DEF:tx=/usr/local/bin/system/data.rrd:tx:AVERAGE' \
'AREA:rx#00CC00FF' \
'STACK:tx#0000FFFF'

exit 0

Although not part of the above script, the below script is required to create the round-robin database used by RRDTool
					
#! /bin/bash

rrdtool create data.rrd \
--start N --step '60' \
'DS:rx:GAUGE:120:0:U' \
'DS:tx:GAUGE:120:0:U' \
'DS:rxc:COUNTER:120:0:U' \
'DS:txc:COUNTER:120:0:U' \
'DS:wrx:GAUGE:120:0:U' \
'DS:wtx:GAUGE:120:0:U' \
'DS:wrxc:COUNTER:120:0:U' \
'DS:wtxc:COUNTER:120:0:U' \
'RRA:AVERAGE:0.5:1:129600' \
'RRA:AVERAGE:0.5:2:64800' \
'RRA:AVERAGE:0.5:60:14400' \
'RRA:AVERAGE:0.5:300:12960' \
'RRA:AVERAGE:0.5:3600:13140' \
'RRA:AVERAGE:0.5:3600:3650'

rrdtool create load.rrd \
--start N --step '60' \
'DS:load:GAUGE:120:0:4' \
'RRA:AVERAGE:0.5:1:129600' \
'RRA:AVERAGE:0.5:2:64800' \
'RRA:AVERAGE:0.5:60:14400' \
'RRA:AVERAGE:0.5:300:12960' \
'RRA:AVERAGE:0.5:3600:13140' \
'RRA:AVERAGE:0.5:3600:3650'

rrdtool create mem.rrd
--start N --step '60' \
'DS:mem:GAUGE:120:0:100' \
'RRA:AVERAGE:0.5:1:129600' \
'RRA:AVERAGE:0.5:2:64800' \
'RRA:AVERAGE:0.5:60:14400' \
'RRA:AVERAGE:0.5:300:12960' \
'RRA:AVERAGE:0.5:3600:13140' \
'RRA:AVERAGE:0.5:3600:3650'

rrdtool create pitemp.rrd \
--start N --step '60' \
'DS:pitemp:GAUGE:120:U:U' \
'RRA:AVERAGE:0.5:1:129600' \
'RRA:AVERAGE:0.5:2:64800' \
'RRA:AVERAGE:0.5:60:14400' \
'RRA:AVERAGE:0.5:300:12960' \
'RRA:AVERAGE:0.5:3600:13140' \
'RRA:AVERAGE:0.5:3600:3650'
Notes:
# ------------------------------------------------------------ #
Script Name: whoip.sh
Status:In use
Description: Part of my day job is to build up a reliable database linking IP address ranges to the countries where they are based. I do this mainly using the main Internet Number Registries (ARIN/APNIC/LACNIC/etc), but also rely on this script. The base command is a whois lookup.
Pre-requisites:
  1. none
					
#! /bin/bash

# THE COLOUR OF LIGHT CYAN
LCYAN='\033[1;36m'
# THE COLOUR OF LIGHT RED
LRED='\033[1;31m'
# THE REGEX FOR DETERMINING A VALID IPv4 ADDRESS WITH SPACES BEFORE AND/OR AFTER
REGEX='^\s*((25[0-5]|2[0-4][0-9]|1?([0-9]){1,2})\.){3}((25[0-5]|2[0-4][0-9]|1?([0-9]){1,2}))\s*$'
# INITIATING A COUNTER TO DETERMINE HOW MANY TIMES THE USER HAS NOT ENTERED A VALID IPv4 ADDRESS
COUNT=0

# FUNCTION TO DETERMINE WHETHER THE ENTERED STRING WAS A VALID IPv4 ADDRESS
check(){
[[ $IP =~ $REGEX ]] && lookup || reask # IF IT IS, CALL THE LOOKUP CODE, OTHERWISE ASK AGAIN
}

# FUNCTTION TO DISPLAY THE QUERY TO THE USER AND GET THE IP ADDRESS THEN CALL THE CHECKING FUNCTION
ask(){
echo -e "${LCYAN} Enter IPv4 Address and press [ENTER]:\e[0m"
read IP
check
}

# FUNCTION TO REASK FOR THE IP ADDRESS IF IT WAS NOT A VALID IPv4 ADDRESS
reask(){
unset IP
if [ $COUNT -eq 2 ]; then
echo -e "${LRED} IPv4 Address format is xxx.xxx.xxx.xxx where \"xxx\" is a number from 0 to 255."
COUNT=`expr $COUNT + 1`
ask
elif [ $COUNT -eq 3 ]; then
echo -e "${LRED} Please take this seriously."
exit 1
else
echo -e "${LRED} NOT a valid IPv4 address."
COUNT=`expr $COUNT + 1`
ask
fi
}

# THE FUNCTION WITH THE ACTUAL LOOKUP CODE
lookup(){
echo $IP >> /usr/local/bin/whois/ip.log
whois $IP | grep -Ei --color 'netname|inetnum|country|CIDR|inetnumup|descr|^route|^organization'
}

# START EVERYTHING BY DISPLAYING THE REQUEST FOR THE IP ADDRESS
ask

exit 0
Notes:
# ------------------------------------------------------------ #
Script Name: newlease.sh
Status:On hold
Description: The point of this script is to notify me whenever someone new accesses my home WiFi. I have set up all the usual devices on my home network to have static IP addresses on a /23 IP range. The first /24 is for "servers" (routers/web-cameras/etc) and the 2nd for "users" (PCs/Laptops/phones/kindles/etc). I'm also using dnsmasq's DHCP functionality. I've limited the DHCP to a range of 128 IP addresses in the last /26 of the second /24.
Pre-requisites:
  1. dnsmasq
  2. ssmtp
  3. Gmail account
					
#! /bin/bash

# This script checks the values of the file created by DNSMASQ
# When a new device is assigned a dynamic IP address, an Email will be generated
# containing all the currently connected devices that are using DHCP

DHCPATH=/usr/local/bin/dhcp

# GRAB THE LATEST LEASES FILE AND MAKE A LOCAL WORKING COPY
cp /var/lib/misc/dnsmasq.leases $DHCPATH/
# CHECK IF THE RELEVANT FILES EXIST AND CREATE THEM IF NEED BE
if [ ! -e $DHCPATH/leases.new ]; then
# IF THEY DON'T EXIST, CREATE THEM
cp $DHCPATH/dnsmasq.leases $DHCPATH/leases.new
cp $DHCPATH/dnsmasq.leases $DHCPATH/leases.old
else
# IF THEY DO EXIST, CHANGE THE OLD `NEW` to `OLD` & CREATE A NEW `NEW`
mv $DHCPATH/leases.new $DHCPATH/leases.old
cp $DHCPATH/dnsmasq.leases $DHCPATH/leases.new
fi

# CHECK TO SEE IF THERE'S A DIFFERENCE BETWEEN LEASES.NEW & LEASES.OLD
DIFF=$(diff $DHCPATH/leases.new $DHCPATH/leases.old)
if [ "$DIFF" = "" ]; then
echo 'same'
else
echo 'Subject: Someone has accessed your home network' > $DHCPATH/new-access # Create new "new-access" with the email's subject line
echo 'MIME-Version: 1.0' >> $DHCPATH/new-access # Add the MIME version
echo 'Content-Type: text/html; charset=utf-8' >> $DHCPATH/new-access # Add content-type to allow HTML email
echo '' >> $DHCPATH/new-access # Add a blank line
echo 'It seems as if there is a new connection on the Wi-Fi at home:\n' >> $DHCPATH/new-access # Add some text to make the email slightly more readable
awk 'BEGIN{PROCINFO["strftime"]="%s"} { print $4, "started using", $3, "on", strftime("%Y-%m-%d %T",$1), "with MAC", $2"."} ORS="\n\n"' $DHCPATH/leases.new >> $DHCPATH/new-access
chmod 766 $DHCPATH/new-access # Set permission to make the file readable and writeable

/usr/sbin/ssmtp -F"Odin" xxxxxxxxxx@gmail.com < $DHCPATH/new-access # Send the mail with ssmtp
fi
echo `cat $DHCPATH/new-access`
rm $DHCPATH/new-access # clean up the old file

exit 0
Notes:
# ------------------------------------------------------------ #
Script Name: ip.sh
Status:In use
Description: This is a notification system to let me know when a new IP address has been assigned to my home internet connection. If for whatever reason my DynDNS falls over, I can still SSH into my home server, because I'll know the IP address.
Pre-requisites:
  1. ssmtp
  2. Gmail account
					
#!/bin/bash

# SET SOME VARIABLES
IPPATH=/usr/local/bin/ip
REGEX='^\s*((25[0-5]|2[0-4][0-9]|1?([0-9]){1,2})\.){3}((25[0-5]|2[0-4][0-9]|1?([0-9]){1,2}))\s*$'
ATTEMPT=0

# CHECK IF FILE `IP.NEW` EXISTS
if [ ! -e $IPPATH/ip.new ]; then
# CREATE IT IF IT DOESN'T EXIST WITH 0.0.0.0
echo "0.0.0.0" > $IPPATH/ip.new
fi

# CHECK IF FILE `IP.OLD` EXISTS
if [ ! -e $IPPATH/ip.old ]; then
# CREATE IT IF IT DOESN'T EXIST WITH 0.0.0.0
echo "0.0.0.0" > $IPPATH/ip.old
else
# OTHERWISE RENAME IP.NEW to IP.OLD
mv $IPPATH/ip.new $IPPATH/ip.old
fi

if [ ! -e $IPPATH/ohnoes ]; then
echo "Subject: FAILED getting Odin's IP address" > $IPPATH/ohnoes # Create new `ohnoes` with the email's subject line
echo "MIME-Version: 1.0" >> $IPPATH/ohnoes # Add the MIME version
echo "Content-Type: text/html; charset=utf-8" >> $IPPATH/ohnoes # Add content-type to allow HTML email
echo "" >> $IPPATH/ohnoes # Add a blank line
echo "Odin Failed to get an external IP Address. It may be that there was just a DNS failure. A reboot may be required." >> $IPPATH/ohnoes # Add some text to make the email slightly more readable
fi

# HAVE WE BEEN HERE BEFORE? EACH OF THESE WILL BE TRIED ONCE FOR A VALID IP ADDRESS
GETIP(){
case $ATTEMPT in
0)
IP=$(curl -s http://ip.tyk.nu/)
CHECK
;;
1)
IP=$(curl -s http://whatismyip.akamai.com/)
CHECK
;;
2)
IP=$(curl -s http://ifcfg.me/)
CHECK
;;
3)
IP=$(curl -s http://ip.appspot.com/)
CHECK
;;
4)
IP=$(curl -s http://ident.me/)
CHECK
;;
5)
IP=$(curl -s http://ipof.in/txt)
CHECK
;;
6)
IP=$(curl -s http://wgetip.com/)
CHECK
;;
7)
IP=$(curl -s http://eth0.me/)
CHECK
;;
8)
IP=$(curl -s http://ipecho.net/plain)
CHECK
;;
9)
IP=$(currentlyl -s https://tnx.nl/ip)
CHECK
;;
*)
FAIL
;;
esac
}

# FUNCTION TO DETERMINE WHETHER THE ENTERED STRING WAS A VALID IPv4 ADDRESS
CHECK(){
ATTEMPT=`expr $ATTEMPT + 1`
[[ $IP =~ $REGEX ]] && TRIM || GETIP # IF IT IS, CALL THE VALID CODE, OTHERWISE ASK AGAIN
}

# TRIMS PRECEDING AND FOLLOWING WHITE SPACE
TRIM(){
IP=`echo $IP | xargs`
echo $IP > ip.new
COMPARE
}

# IF THE NEW IP.NEW DOES NOT MATCH THE NEW IP.OLD, THEN SEND IT THROUGH FOR ACTION OTHERWISE EXIT
COMPARE(){
if ! cmp $IPPATH/ip.old $IPPATH/ip.new >/dev/null 2>&1 ; then
CREATE
else
exit 0
fi
}

CREATE(){
URL0='<br><a href="http://' # First part of link
URL1='/">' # Second part of link
URL2='</a>' # Last part of link
URL=$URL0$IP$URL1$IP$URL2 # Build link text by concatenating variables
echo 'Subject: Odin has a new IP address' > $IPPATH/current-ip # Create new `current-ip` with the email's subject line
echo 'MIME-Version: 1.0' >> $IPPATH/current-ip # Add the MIME version
echo 'Content-Type: text/html; charset=utf-8' >> $IPPATH/current-ip # Add content-type to allow HTML email
echo '' >> $IPPATH/current-ip # Add a blank line
echo 'It seems as if a new IP address as been assigned to Odin:' >> $IPPATH/current-ip # Add some text to make the email slightly more readable
echo $URL >> $IPPATH/current-ip # Add the link (using HTML) to the new IP Addres in current-ip`
/usr/sbin/ssmtp -F"Odin" xxxxxxxx@gmail.com < $IPPATH/current-ip# SEND THAT EMAIL
echo $IP >> ip.log
exit 0
}

FAIL(){
/usr/sbin/ssmtp -F"Odin" xxxxxxxx@gmail.com < $IPPATH/ohnoes# IF ALL THE IP LOOKUPS FAIL SEND THE FIAL EMAIL.
exit 1
}

GETIP

exit 2
Notes:
# ------------------------------------------------------------ #
Script Name: viewtime.sh
Status:In use
Description: How much tv do you watch in a day? Month? Year? With this script I intend on answering this timeless question. I recently got a Smart TV that can be pinged. So, I ping it once per minute and use the exit code from the ping to determine whether the TV is on or not.
Pre-requisites:
  1. You need a TV that has an IP address
  2. RRDTool
					
#! /bin/bash

TVPATH=/usr/local/bin/tv

if [ ! -e $TVPATH/month.view ]; then
echo 0 > $TVPATH/month.view
fi

if [ ! -e $TVPATH/year.view ]; then
echo 0 > $TVPATH/year.view
fi

monthView=`cat $TVPATH/month.view`
yearView=`cat $TVPATH/year.view`
live=`cat $TVPATH/live.view`

monthly_reset () {
# CHECK FOR THE FILE
if [ ! -e $TVPATH/old_month ]; then
# CREATE IT IF IT DOESN'T EXIST WITH THIS MONTHS DATE
echo `date` | awk '{print $2,$6}' > $TVPATH/old_month
fi

# CREATE A NEW FILE EVERY TIME THIS IS RUN WITH THIS MONTHS DATE
echo `date` | awk '{print $2,$6}' > $TVPATH/this_month
# CHECK IF OLD_MONTH AND THIS_MONTH ARE THE SAME

if ! cmp $TVPATH/this_month $TVPATH/old_month > /dev/null 2>&1 ; then
echo 0 > $TVPATH/month.view
# UPDATE THE VALUE IN OLD_MONTH SO THAT THEY WILL MATCH NEXT TIME
cp $TVPATH/this_month $TVPATH/old_month fi
}

yearly_reset () {
# CHECK FOR THE FILE
if [ ! -e $TVPATH/old_year ]; then
# CREATE IT IF IT DOESN'T EXIST WITH THIS MONTHS DATE
echo `date` | awk '{print $6}' > $TVPATH/old_year
fi
# CREATE A NEW FILE EVERY TIME THIS IS RUN WITH THIS MONTHS DATE
echo `date` | awk '{print $6}' > $TVPATH/this_year
# CHECK IF OLD_YEAR AND THIS_YEAR ARE THE SAME
if ! cmp $TVPATH/this_year $TVPATH/old_year > /dev/null 2>&1 ; then
echo 0 > $TVPATH/year.view
# UPDATE THE VALUE IN OLD_MONTH SO THAT THEY WILL MATCH NEXT TIME
cp $TVPATH/this_year $TVPATH/old_year fi
}

monthly_reset
yearly_reset

# PING THE TV ONCE
ping -q -c 1 192.168.0.8 &> /dev/null
# CHECK THE EXIT CODE OF THE PING AND WRITE THAT VALUE TO A FILE
echo $? > $TVPATH/exit_code

# IF THE TV IS ON, INCREMENT COUNTERS
if [ `cat $TVPATH/exit_code` -eq 0 ]; then
((monthView++))
((yearView++))
((live++))
echo $monthView > $TVPATH/month.view
echo $yearView > $TVPATH/year.view
echo $live > $TVPATH/live.view
else
echo 0 > $TVPATH/live.view
fi

rrd() {
RRDTOOL=/usr/bin/rrdtool
$RRDTOOL update $TVPATH/tv.rrd -t live:month:year N:$live:$monthView:$yearView

$RRDTOOL graph /var/www/html/images/graphs/tv36h.png \
--title 'TV Viewing Time' \
--watermark "Graph Drawn `date`" \
--vertical-label 'minutes' \
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-36h \
'DEF:live=/usr/local/bin/tv/tv.rrd:live:AVERAGE' \
'AREA:live#00CC00FF:View Time\:' \
'GPRINT:live:LAST:\:%8.2lf %s]'

# 10D
$RRDTOOL graph /var/www/html/images/graphs/tv10d.png \
--title 'TV Viewing Time' \
--watermark "Graph Drawn `date`" \
--vertical-label 'minutes' \
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si \
--width '640' \
--height '300' \
--full-size-mode \
--start end-10d \
'DEF:live=/usr/local/bin/tv/tv.rrd:live:AVERAGE' \
'AREA:live#00CC00FF:View Time\:' \
'GPRINT:live:LAST:\:%8.2lf %s]' \

# 45D
$RRDTOOL graph /var/www/html/images/graphs/tv45d.png
--title 'TV Viewing Time' \
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-45d \
'DEF:live=/usr/local/bin/tv/tv.rrd:live:AVERAGE'
'AREA:live#00CC00FF:View Time\:'
'GPRINT:live:LAST:\:%8.2lf %s]'

# 18M
$RRDTOOL graph /var/www/html/images/graphs/tv18m.png
--title 'TV Viewing Time' \
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-1y6m
'DEF:live=/usr/local/bin/tv/tv.rrd:live:AVERAGE'
'AREA:live#00CC00FF:View Time\:'
'GPRINT:live:LAST:\:%8.2lf %s]'

# 10Y
$RRDTOOL graph /var/www/html/images/graphs/tv10y.png
--title 'TV Viewing Time' \
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-10y \
'DEF:live=/usr/local/bin/tv/tv.rrd:live:AVERAGE'
'AREA:live#00CC00FF:View Time\:'
'GPRINT:live:LAST:\:%8.2lf %s]'

# MONTHLY VIEW
# 36H
$RRDTOOL graph /var/www/html/images/graphs/mtv36h.png \
--title 'Monthly TV Viewing Time' \
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-36h \
'DEF:month=/usr/local/bin/tv/tv.rrd:month:AVERAGE'
'AREA:month#00CC00FF:Month View Time\:' \
'GPRINT:month:LAST:\:%8.2lf %s]'

# MONTHLY VIEW
# 10D
$RRDTOOL graph /var/www/html/images/graphs/mtv10d.png \
--title 'Monthly TV Viewing Time' \
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-10d \
'DEF:month=/usr/local/bin/tv/tv.rrd:month:AVERAGE'
'AREA:month#00CC00FF:Month View Time\:' \
'GPRINT:month:LAST:\:%8.2lf %s]'

# MONTHLY VIEW
# 45D
$RRDTOOL graph /var/www/html/images/graphs/mtv45d.png \
--title 'Monthly TV Viewing Time' \
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-45d \
'DEF:month=/usr/local/bin/tv/tv.rrd:month:AVERAGE'
'AREA:month#00CC00FF:Month View Time\:' \
'GPRINT:month:LAST:\:%8.2lf %s]'

# MONTHLY VIEW
# 18M
$RRDTOOL graph /var/www/html/images/graphs/mtv18m.png \
--title 'Monthly TV Viewing Time' \
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-1y6m
'DEF:month=/usr/local/bin/tv/tv.rrd:month:AVERAGE'
'AREA:month#00CC00FF:Month View Time\:' \
'GPRINT:month:LAST:\:%8.2lf %s]'

# MONTHLY VIEW
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/mtv10y.png \
--title 'Monthly TV Viewing Time' \
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-10y \
'DEF:month=/usr/local/bin/tv/tv.rrd:month:AVERAGE'
'AREA:month#00CC00FF:Month View Time\:' \
'GPRINT:month:LAST:\:%8.2lf %s]'

# YEARLY VIEW
# 36H
$RRDTOOL graph /var/www/html/images/graphs/ytv36h.png \
--title 'Yearly TV Viewing Time'
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-36h \
'DEF:year=/usr/local/bin/tv/tv.rrd:year:AVERAGE'
'AREA:year#00CC00FF:Year View Time\:' \
'GPRINT:year:LAST:\:%8.2lf %s]'

# YEARLY VIEW
# 10D
$RRDTOOL graph /var/www/html/images/graphs/ytv10d.png \
--title 'Yearly TV Viewing Time'
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-10d \
'DEF:year=/usr/local/bin/tv/tv.rrd:year:AVERAGE'
'AREA:year#00CC00FF:Year View Time\:' \
'GPRINT:year:LAST:\:%8.2lf %s]'

# YEARLY VIEW
# 45D
$RRDTOOL graph /var/www/html/images/graphs/ytv45d.png \
--title 'Yearly TV Viewing Time'
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-45d \
'DEF:year=/usr/local/bin/tv/tv.rrd:year:AVERAGE'
'AREA:year#00CC00FF:Year View Time\:' \
'GPRINT:year:LAST:\:%8.2lf %s]'

# YEARLY VIEW
# 18M
$RRDTOOL graph /var/www/html/images/graphs/ytv18m.png \
--title 'Yearly TV Viewing Time'
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-1y6m
'DEF:year=/usr/local/bin/tv/tv.rrd:year:AVERAGE'
'AREA:year#00CC00FF:Year View Time\:' \
'GPRINT:year:LAST:\:%8.2lf %s]'

# YEARLY VIEW
# 10Y
$RRDTOOL graph /var/www/html/images/graphs/ytv10y.png \
--title 'Yearly TV Viewing Time'
--watermark "Graph Drawn `date`"
--vertical-label 'minutes'
--lower-limit '0' \
--alt-autoscale \
--rigid \
--units=si
--width '640' \
--height '300'
--full-size-mode
--start end-10y \
'DEF:year=/usr/local/bin/tv/tv.rrd:year:AVERAGE'
'AREA:year#00CC00FF:Year View Time\:' \
'GPRINT:year:LAST:\:%8.2lf %s]'
}

rrd

exit 0

Notes:
# ------------------------------------------------------------ #
Script Name: serverstatus.sh
Status:In use
Description: I manage the office dashboard (an auto-scrolling, auto-refreshing web site with a whole bunch of office data on it in HTML format). After one of our local office servers was out, I decided that a server status notificaiton would be kinda nice to have. What this does is pings all of the remote servers and all the local servers 4 times. If there's even one ping response, the server is deemed "up", otherwise it's "OFFLINE".
Pre-requisites:
  1. Apache2
  2. PHP
					
#! /bin/bash

PINGPATH=/usr/local/bin/servers
declare -a SERVER=("put" "all" "your" "remote" "servers" "here")
declare -a LSERVER=("and" "the" "local" "ones" "here")
DOMAIN= #put your domain name and TLD here
LDOMAIN= #put your local net server domains here
SITELOCAL=/var/www/status #or /var/www//html/status if you're running Jessie
STATUS=status.php

for i in "${SERVER[@]}"
do
ping -q -c 4 $i.$DOMAIN &> /dev/null
if [ $? -eq 0 ]; then
OUT=""
OUT="<div class='steady'>"
OUT+="${i^^}"
OUT+=": online</div>"
echo $OUT > $SITELOCAL/$i$STATUS
else
OUT=""
OUT="<div class='blink'>"
OUT+="${i^^}"
OUT+=": OFFLINE!</div>"
echo $OUT > $SITELOCAL/$i$STATUS
fi
done

for i in "${LSERVER[@]}"
do
ping -q -c 4 $i.$LDOMAIN &> /dev/null
if [ $? -eq 0 ]; then
LOUT=""
LOUT="<div class='steady'>"
LOUT+="${i^^}"
LOUT+=": online</div>"
echo $LOUT > $SITELOCAL/$i$STATUS
else
LOUT=""
LOUT="<div class='blink'>"
LOUT+="${i^^}"
LOUT+=": OFFLINE!</div>"
echo $LOUT > $SITELOCAL/$i$STATUS
fi
done
exit 0
Here's the styling I used so that the OFFLINE servers will flash in red when they're offline and not show up if they're online. There are different layout versions (CSS) of the dashboard. this is the one for the TV.
					
#status{
position: fixed;
bottom: 10px;
left: 5px;
/*opacity: 0.5;*/
}
.blink{
position: relative;
-webkit-animation: blink .75s linear infinite;
-moz-animation: blink .75s linear infinite;
animation: blink .75s linear infinite;
background-color:#FF0000;
color:#FFFFFF;
z-index: 4;
font-size: 45px;
}
@-webkit-keyframes blink {
0% { opacity: 1; }
50% { opacity: 1; }
50.01% { opacity: 0; }
100% { opacity: 0; }
}
@-moz-keyframes blink {
0% { opacity: 1; }
50% { opacity: 1; }
50.01% { opacity: 0; }
100% { opacity: 0; }
}
@keyframes blink {
0% { opacity: 1; }
50% { opacity: 1; }
50.01% { opacity: 0; }
100% { opacity: 0; }
}
.steady{
position: relative;
background-color:#00FF00;
color:#FFFFFF;
z-index: 4;
opacity: 0;
display: none;
}
Notes:
# ------------------------------------------------------------ #