#!/usr/local/bin/bash

# Config file for spinpid2.sh, beginning version 2019-11-01, for dual fan zones.

IPMITOOL="/usr/local/bin/ipmitool -H hostname -U username -P password"

#################  OUTPUT SETTINGS ################

# Change to your desired log location/name:
# LOG=/mnt/MyPool/MyDataSet/MyDirectory/spinpid2.log
LOG=$(dirname "${BASH_SOURCE[0]}")/spinpid2.log

# Where do you want output to go?  Comment/uncomment (#) to select.  
# First sends output to the log file AND to screen/console, good for testing.
# Second goes only to log file, no feedback if running manually, but doesn't take over console.  
# In the first, if you want to append to existing log, add '-a' to the tee command.
# exec > >(tee -i $LOG) 2>&1		# Log + console, good for testing
exec &> $LOG						# Log only

# CPU output sent to a separate log for interim cycles
# It can get big so turn off after testing. 1 = log cpu; anything else = don't log cpu
CPU_LOG_YES=0

# Path/name of cpu log
# CPU_LOG=/mnt/MyPool/MyDataSet/MyDirectory/cpu.log
CPU_LOG=$(dirname "${BASH_SOURCE[0]}")/cpu.log

#################  FAN SETTINGS ################

# Supermicro says:
# Zone 0 - CPU/System fans, headers with number (e.g., FAN1, FAN2, etc.)
# Zone 1 - Peripheral fans, headers with letter (e.g., FANA, FANB, etc.)
# Some want the reverse (i.e, drive cooling fans on headers FAN1-4 and 
# CPU fan on FANA), so that's the default.  But you can switch to SM way.
ZONE_CPU=0
ZONE_PER=1

# Set min and max duty cycle to avoid stalling or zombie apocalypse
DUTY_PER_MIN=20
DUTY_PER_MAX=100
DUTY_CPU_MIN=20
DUTY_CPU_MAX=100

# Your measured fan RPMs at 30% duty cycle and 100% duty cycle
# RPM_CPU is for FANA if ZONE_CPU=1 or FAN1 if ZONE_CPU=0
# RPM_PER is for the other fan.
# RPM_CPU_30=400   # Your system
# RPM_CPU_MAX=1500
# RPM_PER_30=600
# RPM_PER_MAX=1800
RPM_CPU_30=600   # My system
RPM_CPU_MAX=1900
RPM_PER_30=500
RPM_PER_MAX=1300

# How should we determine what the fan duty % is?  Some dual-zone 
# boards apparently report incorrect fan duty.  You can: 
# (1) let the script read it, or
# (any other value) assume it's where it was set.
# Reading it from the board is recommended unless you know
# your board gets it wrong.
HOW_DUTY=1

#################  DRIVE SETTINGS ################

SP=31.00   #  Setpoint mean drive temperature (C)

#  Time interval for checking drives (minutes).  Drives change
#  temperature slowly; 5 minutes is probably frequent enough.
DRIVE_T=5
# Tunable constants for drive control (see comments at end of script)
Kp=4    #  Proportional tunable constant
Kd=40   #  Derivative tunable constant

#################  CPU SETTINGS ################

#  Time interval for checking CPU (seconds).  1 to 12 may be appropriate
CPU_T=5

#  Reference temperature (C) for scaling CPU_DUTY (NOT a setpoint).
#  At and below this temperature, CPU will demand minimum
#  duty cycle (DUTY_CPU_MIN).
CPU_REF=45  # Integer only!
#  Scalar for scaling CPU_DUTY.
#  CPU will demand this number of percentage points in additional
#  duty cycle for each degree of temperature above CPU_REF.
CPU_SCALE=6  # Integer only!

function Post_CPU_check_adjust() {

    svr="grafana"
    db="spinpid"

    date="$(date +"%s")000000000"              #nano seconds

    cpu_fan=$(${IPMITOOL} sensor get "FAN1" | grep "Sensor Reading" | awk '{print $4}')

    curl -s -i -o /dev/null -XPOST "http://${svr}:8086/write?db=${db}" --data-binary   "health_data,host=${db},sensor=CPU_Act_Temp value=$CPU_TEMP $date
                		                    					health_data,host=${db},sensor=CPU_Ref_Temp value=$CPU_REF $date
                		                    					health_data,host=${db},sensor=CPU_Scale value=$CPU_SCALE $date
    		                                					health_data,host=${db},sensor=CPU_Fan_Duty value=$DUTY_CPU $date
							                                health_data,host=${db},sensor=CPU_Fan_Rpm value=$cpu_fan $date"
}

function Post_DRIVES_check_adjust() {

    svr="grafana"
    db="spinpid"

    date="$(date +"%s")000000000"              #nano seconds

    per_fan=$(${IPMITOOL} sensor get "FANA" | grep "Sensor Reading" | awk '{print $4}')

    curl -s -i -o /dev/null -XPOST "http://${svr}:8086/write?db=${db}" --data-binary   "health_data,host=${db},sensor=HDD_Avg_Temp value=$Tmean $date
                                		    					health_data,host=${db},sensor=HDD_Max_Temp value=$Tmax $date
                                		    					health_data,host=${db},sensor=HDD_Ref_Temp value=$SP $date
    							                                health_data,host=${db},sensor=PER_Fan_Duty value=$DUTY_PER $date
    							                                health_data,host=${db},sensor=PER_Fan_Rpm value=$per_fan $date"
}
