#!/bin/bash

#echo -e "32768\t61000" > /proc/sys/net/ipv4/ip_local_port_range
#$ sysctl -w net.ipv4.tcp_wmem="253952 253952 16777216"
#$ sysctl fs.file-max
#$ sysctl fs.file-nr 

echo "----------------------------------"
check_value=`getenforce`
echo "GetEnforce : ${check_value}"
echo "----------------------------------"
echo -e "Parameter\tNow\tRecommand"
echo "----------------------------------"

# Recommand value = 0
recom_value="0"
for check_param in net.ipv4.tcp_timestamps net.ipv4.conf.all.accept_source_route net.ipv4.ip_forward net.ipv4.conf.all.accept_redirects net.ipv4.tcp_tw_reuse
do
        check_param2=${check_param%*.*}
        check_param3=`echo ${check_param} | sed -e "s/${check_param2}//" | cut -c 2-`
        check_value=`sysctl -n ${check_param}`

        if [ "$check_value" -eq "${recom_value}" ]
        then 
                echo -e "${check_param3}\t${check_value}\t${recom_value}\t OK"
        else
                echo -e "${check_param3}\t${check_value}\t${recom_value}"
                read -s -n1 -p "    ${check_param3} ${check_value}->${recom_value} : Submit Now? (y/n) : " submit_yn
                if [ "$submit_yn" == "y" ]
                then
                        echo -e "\tsysctl -w ${check_param}=${recom_value}"
                        echo "$check_param = \"${recom_value}\"" >> /etc/sysctl.conf 
                fi
                echo ${submit_yn}
        fi
done

# Recommand value = 1
recom_value="1"
for check_param in net.ipv4.icmp_echo_ignore_broadcasts net.ipv4.conf.all.log_martians net.ipv4.tcp_syncookies net.ipv4.tcp_window_scaling 
do
        check_param2=${check_param%*.*}
        check_param3=`echo ${check_param} | sed -e "s/${check_param2}//" | cut -c 2-`
        check_value=`sysctl -n ${check_param}`

        if [ "$check_value" -eq "${recom_value}" ]
        then 
                echo -e "${check_param3}\t${check_value}\t${recom_value}\t OK"
        else
                echo -e "${check_param3}\t${check_value}\t${recom_value}"
                read -s -n1 -p "    ${check_param3} ${check_value}->${recom_value} : Submit Now? (y/n) : " submit_yn
                if [ "$submit_yn" == "y" ]
                then
                        echo -e "\tsysctl -w ${check_param}=${recom_value}"
                        echo "$check_param = \"${recom_value}\"" >> /etc/sysctl.conf 
                fi
                echo ${submit_yn}
        fi
done


check_value=`sysctl -n .net.ipv4.ip_local_port_range`
echo -e "Port.Range\t${check_value}\t1024\t65535"
check_value=`sysctl -n .net.ipv4.tcp_max_syn_backlog`
echo -e "MAX.BackLog\t${check_value}\t1024"

check_value=`sysctl -n net.ipv4.tcp_max_tw_buckets`
echo -e "TcpTW.Buckets\t${check_value}\t65536"

# check : ulimit -a / sysctl -n fs.file-nr
check_value=`sysctl -n fs.file-max`
echo -e "FS.FileMax\t${check_value}\t77052"
check_value=`sysctl -n net.core.netdev_max_backlog`
echo -e "NetdevMxBackLog\t${check_value}\t30000"
check_value=`sysctl -n net.core.somaxconn`
echo -e "MaxConnections\t${check_value}\t1024"

echo "----------------------------------------------------------------------------"
free -m
echo "----------------------------------------------------------------------------"
swap_size=`free -m | grep -i swap | awk '{print $2}'`
if [ "$swap_size" -eq "0" ]
then
        echo -e "----------------------------------------------------------------------------"
        echo -e "\tdd if=/dev/zero of=/data/swapfile bs=1M count=[1024|2048|8192]"
        echo -e "\tmkswap /data/swapfile"
        echo -e "\tswapon /data/swapfile"
        echo -e "\tchmod 600 swapfile"
        echo -e "\t/data/swapfile swap swap defaults 0 0 >> /etc/fstab"
        echo -e "----------------------------------------------------------------------------"
fi

ulimit -a
echo "----------------------------------------------------------------------------"

echo "sysctl -p"
sysctl -p
