#!/bin/bash
# BEGIN_ICS_COPYRIGHT8 ****************************************
# 
# Copyright (c) 2015-2017, Intel Corporation
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Intel Corporation nor the names of its contributors
#       may be used to endorse or promote products derived from this software
#       without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# END_ICS_COPYRIGHT8   ****************************************

# The script helps to configure system settings
#
# Each configuration option has three functions
# 1) Config_Enable
# 2) Config_Disable
# 3) Config_Status
# The naming convention for any system setting should be
# Utility_SysConfig_

ENABLE=0
DISABLE=1
NOT_AVAIL=2

SysConfig_Comp[0]="Irq_Balance"
SysConfig_Comp[1]="Memory_Limit"
SysConfig_Comp[2]="Udev_Access"

# Find start system in use
function get_start_system() {
		if [ -e /proc/1/cmdline ]; then
			echo $(ps -o comm= 1)
		elif [ -d /usr/lib/systemd ]; then
			echo 'systemd'
		elif [ -d /etc/init.d ]; then
			echo 'init'
		else
			echo ''
		fi
}

IRQFILE=/etc/sysconfig/irqbalance
IRQBAKFILE=/etc/sysconfig/irqbalance.bak
IRQ_ARGS_VALUE="policyscript=/etc/sysconfig/opa/hintpolicy_exact_hfi1.sh"

#Irq_balance
function Irq_Balance_SysConfig_Enable(){
	# Grep file to see if IRQBALANCE_ARGS is set
	local irq_current_line=$(egrep "^IRQBALANCE_ARGS=" $IRQFILE)

	# If argument is not found, just append it to the file
	if [ -z "${irq_current_line}" ]; then
		cat $IRQFILE > $IRQBAKFILE
		echo "IRQBALANCE_ARGS=\"--$IRQ_ARGS_VALUE\"" >> $IRQFILE

	# If argument is found, we must parse it to see if hintpolicy=exact is already set.
	# We must also preserve the currently set arguments, so that we can prepend the policyscript to them in our 'sed' command
	else
		local irq_current_value=$(echo ${irq_current_line} | cut -d= -f2-)
		if $(echo ${irq_current_value} | grep -q "$IRQ_ARGS_VALUE") || $(echo ${irq_current_value} | grep -q 'h hintpolicy_exact_hfi1.sh' ); then
			echo "$IRQ_ARGS_VALUE already set"
		elif $(echo ${irq_current_value} | grep -q \"); then # SLES Default. The args are set to an empty string: IRQBALANCE_ARGS=""
			local irq_current_value_in_quotes=$(echo ${irq_current_value} | tr -d \")
			# If the value is not empty, we will add a space to separate it from the hintpolicy.
			[ -n "${irq_current_value_in_quotes}" ] && irq_current_value_in_quotes=" ${irq_current_value_in_quotes}"
			local sed_command="s/^IRQBALANCE_ARGS=.*/IRQBALANCE_ARGS=\"--${IRQ_ARGS_VALUE//\//\\/}${irq_current_value_in_quotes//\//\\/}\"/g"
		else                                                            # RHEL Default. The args are commented out: #IRQBALANCE_ARGS=
			[ -n "${irq_current_value}" ] && irq_current_value=" ${irq_current_value}" # If the value is not empty, we will add a space to separate it from the policyscript.
			local sed_command="s/^IRQBALANCE_ARGS=.*/IRQBALANCE_ARGS=\"--${IRQ_ARGS_VALUE//\//\\/}${irq_current_value//\//\\/}\"/g"
		fi

		cat $IRQFILE > $IRQBAKFILE
		[ -n "$sed_command" ] && sed -i "$sed_command" $IRQFILE
	fi

	# Check for systemd vs upstart
	local start_system=$(get_start_system)
	if [ "${start_system}" = "systemd" ]; then
		systemctl enable irqbalance
		systemctl restart irqbalance
	elif [ "${start_system}" = "init" ]; then
		chkconfig irqbalance on
		service irqbalance restart
	else
		echo "irqbalance was not started. Please start it manually."
	fi
}

function Irq_Balance_SysConfig_Disable(){
	cat $IRQBAKFILE > $IRQFILE
	# Check for systemd vs upstart
	local start_system=$(get_start_system)
	if [ "${start_system}" = "systemd" ]; then
		systemctl disable irqbalance
	elif [ "${start_system}" = "init" ]; then
		chkconfig irqbalance off
	else
		echo "irqbalance was not disabled. Please disable it manually."
	fi
}

function Irq_Balance_SysConfig_Status(){
	# Check for systemd vs upstart
	local start_system=$(get_start_system)
	if [ "${start_system}" = "systemd" ]; then
		local status=$(systemctl is-enabled irqbalance)
	elif [ "${start_system}" = "init" ]; then
		local service_output=$(service irqbalance 2>&1)
		if $(echo ${service_output} | grep -q 'unrecognized service'); then
			local status="notfound"
		else
			chkconfig irqbalance
			local status_rc=$?
			if [ $status_rc -eq 0 ];then
				local status="enabled"
			elif [ $status_rc -eq 1 ]; then
				local status="disabled"
			fi
		fi
	else
		echo "start system cannot be queried."
	fi

        # Check for status
        if [ "$status" == "enabled" ]; then
                return $ENABLE
        elif [ "$status" == "disabled" ]; then
                return $DISABLE
        else
                return $NOT_AVAIL
        fi
}

#Memory Limit
APPEND_FILE=/etc/sysconfig/opa/limits.conf
LIMIT_FILE=/etc/security/limits.conf
LIMIT_BAKFILE=/etc/security/limits.conf.bak

function Memory_Limit_SysConfig_Enable(){
	Memory_Limit_SysConfig_Status
        retval=$?
        if [ "$retval" = "1" ]; then
		cat $LIMIT_FILE > $LIMIT_BAKFILE
		cat $APPEND_FILE >> $LIMIT_FILE	
        fi

}

function Memory_Limit_SysConfig_Disable(){
	cat $LIMIT_BAKFILE > $LIMIT_FILE
}

function Memory_Limit_SysConfig_Status(){
	CHECK1=$(sed -n '/hard memlock unlimited/'p $LIMIT_FILE)
	CHECK2=$(sed -n '/soft memlock unlimited/'p $LIMIT_FILE)
	if [[ ("$CHECK1" == "") || ("$CHECK2" == "") ]]; then
		return $DISABLE
	else
		return $ENABLE
	fi
		
}

#Udev Access
SOURCE_FILE=/etc/sysconfig/opa/udev.rules
RULE_FILE=/etc/udev/rules.d/05-opa.rules

function Udev_Access_SysConfig_Enable(){	
	Udev_Access_SysConfig_Status
	retval=$?
	if [ "$retval" = "1" ]; then
		touch $RULE_FILE
		cat $SOURCE_FILE >> $RULE_FILE
		chown root $RULE_FILE
		chgrp root $RULE_FILE	
		chmod 0644 $RULE_FILE
	fi
}

function Udev_Access_SysConfig_Disable(){
	rm -f $RULE_FILE
}

function Udev_Access_SysConfig_Status(){
        if [ -e $RULE_FILE ]; then
                return $ENABLE
        else
                return $DISABLE
        fi
}

function comp_status(){
	comp_name=$1
        comp_num=${#SysConfig_Comp[@]}
        for ((i = 0; i < comp_num; i++))
        do
                if [ "${SysConfig_Comp[i]}" == "$comp_name" ]; then
                        ${SysConfig_Comp[i]}_SysConfig_Status
			retval=$?
                        break
                fi
        done
        if [ "$i" == "$comp_num" ]; then
                echo $comp_name":" No such Component
        fi
	
	return ${retval}
}

function comp_enable(){
	comp_name=$1
        comp_num=${#SysConfig_Comp[@]}
        for ((i = 0; i < comp_num; i++))
        do
                if [ "${SysConfig_Comp[i]}" == "$comp_name" ]; then
                        ${SysConfig_Comp[i]}_SysConfig_Enable
                        break
                fi
        done
        if [ "$i" == "$comp_num" ]; then
                echo $comp_name":" No such Component
        fi
}

function comp_disable(){
	comp_name=$1
        comp_num=${#SysConfig_Comp[@]}
        for ((i = 0; i < comp_num; i++))
        do
                if [ "${SysConfig_Comp[i]}" == "$comp_name" ]; then
                        ${SysConfig_Comp[i]}_SysConfig_Disable
                        break
                fi
        done
        if [ "$i" == "$comp_num" ]; then
                echo $comp_name":" No such Component
        fi
}

function list_settings(){
        echo "Available Settings"
        comp_num=${#SysConfig_Comp[@]}
        for ((i = 0; i < comp_num; i++))
        do
		comp_status ${SysConfig_Comp[i]}
		retval=$?
		if [ "$retval" == "0" ]; then
			echo "-->" ${SysConfig_Comp[i]} "[ENABLED]"
		elif [ "$retval" == "1" ]; then
			echo "-->" ${SysConfig_Comp[i]} "[DISABLED]"
		fi
        done
}



function usage(){
        help
}

function help(){
        echo "Usage:	opasystemconfig --[Action] [Utility]"
        echo "          or"
        echo "	opasystemconfig --help"
        echo "  --help - produce full help text" 
        echo "  --status - shows status of setting"
        echo "  --enable - enables the setting"
        echo "  --disable - disables the setting"
        echo "  --list - lists all available utilities"
}


#########################################################
####   OPA System Settings Script
#########################################################

if [ "$(id -u)" != "0" ]; then
        echo "The script must be run as root"
        exit 4
fi

Arguments=$@
declare -a Arg_Array=(${Arguments})
arg_num=${#Arg_Array[@]}

if [ "${Arg_Array[0]}" == "--status" ]; then
        for ((j = 1; j < arg_num; j++))
        do
                comp_status ${Arg_Array[j]}
                retval=$?
                if [ "$retval" == "0" ]; then
                        echo ${Arg_Array[j]} [ENABLED]
                elif [ "$retval" == "1" ]; then
                        echo ${Arg_Array[j]} [DISABLED]
                else
                        echo ${Arg_Array[j]} [NOT INSTALLED]
                fi
        done

elif [ "${Arg_Array[0]}" == "--enable" ]; then
        for ((j = 1; j < arg_num; j++))
        do
                comp_enable ${Arg_Array[j]}
        done

elif [ "${Arg_Array[0]}" == "--disable" ]; then
        for ((j = 1; j < arg_num; j++))
        do
                comp_disable ${Arg_Array[j]}
        done

elif [ "${Arg_Array[0]}" == "--list" ]; then
        list_settings

elif [ "${Arg_Array[0]}" == "--help" ]; then
        help
else
        usage
	exit 1
fi
