######################################################################
#### function: Initialize system parameters
####author:danial_chen
####create date:2019-11-07
####modify date:2019-11-07
######################################################################
#/bin/bash
#useage: sh init_sys_parm.sh
set -e
pd=$PWD

## stop && disable firewalld ##
systemctl stop firewalld
systemctl disable firewalld

systemd_var='
#####cosmicval 
DefaultLimitCORE=infinity
DefaultLimitNOFILE=655350
DefaultLimitNPROC=655350
'
if [ `grep cosmicval /etc/systemd/system.conf | wc -l` = 0  ];then
    echo  "$systemd_var" >>  /etc/systemd/system.conf
    systemctl daemon-reexec
fi

## edit /etc/sysctl.conf ##
declare -a sysctl_conf=("vm.overcommit_memory = 1" "kernel.shmall = 2043878" "kernel.shmmax = 8175512" "kernel.shmmni = 4096" "kernel.sem = 250 32000 100 128" "fs.file-max = 655350" "net.ipv4.ip_local_port_range = 33000 65000" "net.core.rmem_default = 262144" "net.core.rmem_max = 262144" "net.core.somaxconn = 2048" "net.core.wmem_default = 262144" "net.core.wmem_max = 262144" "net.ipv4.tcp_rmem = 262144" "net.ipv4.tcp_wmem = 262144")
for value in "${sysctl_conf[@]}"
do
  if [ `grep "$value" /etc/sysctl.conf | wc -l` = 0 ]; then
    echo "$value" >> /etc/sysctl.conf
  fi
done

## ulimit settings ##
declare -a limits_conf=("* soft nofile 655350" "* hard nofile 655350" "* soft nproc 655350" "* hard nproc 655350")
for value in "${limits_conf[@]}"
do
  if [ `grep "$value" /etc/security/limits.conf | wc -l` = 0 ]; then
    echo "$value" >> /etc/security/limits.conf
  fi
done

#echo "kernel.shmall = 2043878" >> /etc/sysctl.conf
#echo "kernel.shmmax = 8175512" >> /etc/sysctl.conf
#echo "kernel.shmmni = 4096"  >> /etc/sysctl.conf
#echo "kernel.sem = 250 32000 100 128"  >> /etc/sysctl.conf
#echo "fs.file-max = 65536"  >> /etc/sysctl.conf
#echo "net.ipv4.ip_local_port_range = 33000 65000"  >> /etc/sysctl.conf
#echo "net.core.rmem_default = 262144"  >> /etc/sysctl.conf
#echo "net.core.rmem_max = 262144"  >> /etc/sysctl.conf
#echo "net.core.somaxconn = 2048"  >> /etc/sysctl.conf
#echo "net.core.wmem_default = 262144"  >> /etc/sysctl.conf
#echo "net.core.wmem_max = 262144"  >> /etc/sysctl.conf
#echo "net.ipv4.tcp_rmem = 262144"  >> /etc/sysctl.conf
#echo "net.ipv4.tcp_wmem = 262144"  >> /etc/sysctl.conf
#
### ulimit settings ##
#echo "* soft nofile 65536" >> /etc/security/limits.conf
#echo "* hard nofile 65536" >> /etc/security/limits.conf
#echo "* soft nproc 65536"  >> /etc/security/limits.conf
#echo "* hard nproc 65536"  >> /etc/security/limits.conf

sysctl -p || echo

## install net-tools ##
## dependencies里面有，不在这安装了
#rpm -Uvh $pd/*.rpm --nodeps --force

## echo install resault ##
echo "System parameters init success!"
