#!/bin/sh
# memory usage stats

# DEFine global parms
# rrd path
rrdtool=/usr/bin/rrdtool

# name of dataset, will be uset to generate db and graphs
ds_name=ssd

#directory to store graphs - DO NOT REMOVE $ds_name
img=/mnt/zfs_www/rrd/$ds_name

# set db parameters
# db directory - DO NOT REMOVE $ds_name
db=/mnt/zfs_pool/rrd/db/$ds_name.rrd
# heartbeat time(s) wihout data
hbeat=120
# minimum value to be stored in db
min_value=30
# maximum value to be stored in db
max_value=50
# amount of time(s) we expect data to be updated into the database
step=60
# how many "steps" we will store in the db,
# for 1 month with 60s step 60*24*31
steps=44640

# ssd_disks=`find /dev/disk/by-id/ -name "temp-*" -not -name "*part*" -exec basename {} \;`
# ssd_disks=`ls -1 /dev/disk/by-id/ata* | grep -v part`

# for disk in ${ssd_disks[@]} ; do
#  #echo "$(basename $disk): $(hddtemp -n $disk)"
#  printf "%s:\t\t%s\n" $(basename $disk) $(hddtemp -n $disk)
# done


SSDPR_S25A=$(hddtemp -n /dev/disk/by-id/ata-IR-SSDPR-S25A-120_GUL036145)
SSDPR_CX400=$(hddtemp -n /dev/disk/by-id/ata-SSDPR-CX400-128_GUX007579)
SPCC_30010555175=$(hddtemp -n /dev/disk/by-id/ata-SPCC_Solid_State_Disk_30010555175)
SPCC_30010554596=$(hddtemp -n /dev/disk/by-id/ata-SPCC_Solid_State_Disk_30010554596)
QVO_S4CZNF0MA70255L=$(hddtemp -n /dev/disk/by-id/ata-Samsung_SSD_860_QVO_1TB_S4CZNF0MA70255L)
QVO_S4CZNF0MA70526E=$(hddtemp -n /dev/disk/by-id/ata-Samsung_SSD_860_QVO_1TB_S4CZNF0MA70526E)
CT1000MX500SSD1=$(hddtemp -n /dev/disk/by-id/ata-CT1000MX500SSD1_1950E22EA42D)
WDC_WD20EFRX=$(hddtemp -n /dev/disk/by-id/ata-WDC_WD20EFRX-68EUZN0_WD-WCC4M0856843)

if [ ! -e $db ]; then
 rrdtool create $db \
  --step $step \
  DS:wdc-wd20efrx:GAUGE:$hbeat:$min_value:$max_value \
  DS:sppc-30010555175:GAUGE:$hbeat:$min_value:$max_value \
  DS:sppc-30010554596:GAUGE:$hbeat:$min_value:$max_value \
  DS:qvo-s4cznf0ma70255l:GAUGE:$hbeat:$min_value:$max_value \
  DS:qvo-s4cznf0ma70526e:GAUGE:$hbeat:$min_value:$max_value \
  DS:ssdpr-s25a:GAUGE:$hbeat:$min_value:$max_value \
  DS:ssdpr-cx400:GAUGE:$hbeat:$min_value:$max_value \
  DS:ct1000mx500ssd1:GAUGE:$hbeat:$min_value:$max_value \
  RRA:AVERAGE:0.5:1:$steps
fi

echo "Updating RRD $ds_name with values: $WDC_WD20EFRX, $SPCC_30010555175, $SPCC_30010554596, $QVO_S4CZNF0MA70255L, $QVO_S4CZNF0MA70526E, $SSDPR_S25A, $SSDPR_CX400, $CT1000MX500SSD1"
$rrdtool update $db N:$WDC_WD20EFRX:$SPCC_30010555175:$SPCC_30010554596:$QVO_S4CZNF0MA70255L:$QVO_S4CZNF0MA70526E:$SSDPR_S25A:$SSDPR_CX400:$CT1000MX500SSD1

# generate graph from db
for period in day week month ; do

case $period in
  "day")   x_axis="--x-grid MINUTE:10:HOUR:1:MINUTE:120:0:%R" ;;
  "week")  x_axis="--x-grid HOUR:12:DAY:1:DAY:1:0:%A" ;;
  "month") x_axis="--x-grid WEEK:1:WEEK:1:DAY:1:0:%d"  ;;
  *) x_axis=" "
esac

  $rrdtool graph "$img"-"$period".png \
   -w 785 -h 120 -a PNG \
   --slope-mode \
   --disable-rrdtool-tag \
   --start end-1"$period" --end now \
   --font DEFAULT:7: \
   --color CANVAS#35373C \
   --title "System SSD temps (°C)" \
   --watermark "`date`" \
   $x_axis \
   --upper-limit $max_value \
   --lower-limit "$min_value" \
   --rigid \
   --base=1000 \
   DEF:wdc-wd20efrx=$db:wdc-wd20efrx:AVERAGE \
   DEF:sppc-30010555175=$db:sppc-30010555175:AVERAGE \
   DEF:sppc-30010554596=$db:sppc-30010554596:AVERAGE \
   DEF:qvo-s4cznf0ma70255l=$db:qvo-s4cznf0ma70255l:AVERAGE \
   DEF:qvo-s4cznf0ma70526e=$db:qvo-s4cznf0ma70526e:AVERAGE \
   DEF:ssdpr-s25a=$db:ssdpr-s25a:AVERAGE \
   DEF:ssdpr-cx400=$db:ssdpr-cx400:AVERAGE \
   DEF:ct1000mx500ssd1=$db:ct1000mx500ssd1:AVERAGE \
   LINE:ssdpr-s25a#ecd748:"ssdpr-s25a": \
   LINE:ssdpr-cx400#ec9d48:"ssdpr-cx400": \
   LINE:sppc-30010555175#7648ec:"sppc-30010555175": \
   LINE:sppc-30010554596#de48ec:"sppc-30010554596": \
   LINE:qvo-s4cznf0ma70255l#48c4ec:"qvo-s4cznf0ma70255l": \
   LINE:qvo-s4cznf0ma70526e#54ec48:"qvo-s4cznf0ma70526e":\
   LINE:ct1000mx500ssd1#ea644a:"ct1000mx500ssd1" \
   LINE:wdc-wd20efrx#F9F871:"wdc-wd20efrx":  
done
