#! /bin/bash # function library for setting/changing (configuration/settings) tasks # v. 1.0.4 # Function list: # k_set_sysctl() - function sets new|updates old variables into /etc/sysctl.conf. Apply changes with sysctl -p > /dev/null # k_confline_set() - function sets confline value. Creates confline if not exists. $1 - confline name, $2 - confline value # Function naming rules # k__ where either object or action are not mandatory (only object or only action is necessary, could be both also) # function sets new|updates old variables into /etc/sysctl.conf. Apply changes with sysctl -p > /dev/null k_set_sysctl() { PROC_NAME=/proc/sys/${1//./\/} report " Set $1 $2" 0 if [ -e $PROC_NAME ]; then echo $2 > $PROC_NAME fi if [ "`grep "$1" /etc/sysctl.conf 2>/dev/null`" == "" ]; then echo "$1 = $2" >> /etc/sysctl.conf else sed -i -e "/$1/c $1 = $2" /etc/sysctl.conf fi } # function sets confline value. Creates confline if not exists. $1 - confline name, $2 - confline value k_confline_set() { if [[ $1 == "" ]]; then report "k_confline_change() No confline name" 1 return 1 fi if [[ $2 == "" ]]; then report "k_confline_change() No confline value" 1 return 1 fi k_config_details table_exists=$(MYSQL_PWD="$DB_PASSWORD" mysql -h $DB_HOST -u"$DB_USERNAME" -D"$DB_NAME" -e "SHOW TABLES LIKE 'conflines';" 2>/dev/null) if [ -z "$table_exists" ]; then report "Table 'conflines' not found in DB - k_confline_set $1" 2 return fi conf_id=`MYSQL_PWD=$DB_PASSWORD /usr/bin/mysql -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME --silent -e "SELECT id FROM conflines WHERE name='$1';" | grep -v value` if [[ $conf_id != "" ]]; then #report "$1 confline found in DB '$conf_id'" 3 `MYSQL_PWD=$DB_PASSWORD /usr/bin/mysql -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME --silent -e "UPDATE conflines SET value = '$2' WHERE name='$1';" | grep -v value` else #report "$1 confline not found in DB" 3 `MYSQL_PWD=$DB_PASSWORD /usr/bin/mysql -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME --silent -e "INSERT INTO conflines (name, value) VALUES ('$1', '$2');" | grep -v value` fi conf_value=`MYSQL_PWD=$DB_PASSWORD /usr/bin/mysql -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME --silent -e "SELECT value FROM conflines WHERE name='$1';" | grep -v value` if [[ "$conf_value" == "$2" ]]; then report "Changed confline $1 value to $2" 0 else report "Failed to change confline $1 value to $2. Ping MK" 1 EXIT_CODE=1 return 1 fi }