#!/bin/bash

function selinux_close(){
setenforce 0
sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/sysconfig/selinux
sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/selinux/config
getenforce && echo "selinux run ..." || `echo "selinux is stop";exit 1`
}

function install_tools(){
rm -rf /run/yum.pid
yum clean all
yum install -y yum-axelget net-snmp.x86_64 epel-release lrzsz gcc gcc-c++ make man vim unzip wget curl lua-devel lua-static patch libxml2-devel libxslt libxslt-devel gd gd-devel ntp ntpdate screen sysstat tree rsync lsof openssh-clients iftop net-snmp libuuid-devel.x86_64 uuidd.x86_64 device-mapper-devel.x86_64 popt-devel.x86_64 popt.x86_64 popt-static.x86_64 iptables-services rdate ntp smartmontools iptraf psmisc traceroute net-tools.x86_64 tmux bash-completion
if [ $? -eq 0 ]; then
	echo "yum install is ok!!!"
else
	echo "yum install is fall !!!"
fi
}

function service_close(){
systemctl disable firewalld postfix acpid ip6tables mcelogd mdmonitor rpcbind rpcgssd &> /dev/null
systemctl disable rpcidmapd auditd haldaemon lldpad atd kdump netfs nfslock openct &> /dev/null
systemctl stop firewalld
systemctl restart iptables
systemctl enable iptables.service sshd crond &> /dev/null
echo "systemctl stop and disable some software is ok!!!"
}


function time_zone(){
echo "y"|cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &> /dev/null
echo "1 1 */1 * * ntpdate asia.pool.ntp.org" >>/var/spool/cron/root&&chmod 600 /var/spool/cron/root
echo "system time is Shanghai and ntpdate is ok!!!"
}

function innter_sys_optimization(){
cat > /etc/sysctl.conf <<EOF
net.nf_conntrack_max  = 65536
net.netfilter.nf_conntrack_max = 65536
net.ipv4.ip_forward = 0  
net.ipv4.conf.default.rp_filter = 1  
net.ipv4.conf.default.accept_source_route = 0  
kernel.sysrq = 0  
kernel.core_uses_pid = 1  
net.ipv4.tcp_syncookies = 1  
kernel.msgmnb = 65536  
kernel.msgmax = 65536  
kernel.shmmax = 68719476736  
kernel.shmall = 4294967296  
net.ipv4.tcp_max_tw_buckets = 15000
net.ipv4.tcp_sack = 1  
net.ipv4.tcp_window_scaling = 1  
net.ipv4.tcp_rmem = 4096 87380 4194304   
net.ipv4.tcp_wmem = 4096 16384 4194304   
net.core.wmem_default = 8388608  
net.core.rmem_default = 8388608  
net.core.rmem_max = 16777216  
net.core.wmem_max = 16777216  
net.core.netdev_max_backlog = 262144  
net.core.somaxconn = 262144  
net.ipv4.tcp_max_orphans = 3276800  
net.ipv4.tcp_max_syn_backlog = 262144  
net.ipv4.tcp_timestamps = 0  
net.ipv4.tcp_synack_retries = 1  
net.ipv4.tcp_syn_retries = 1  
net.ipv4.tcp_tw_recycle = 1  
net.ipv4.tcp_tw_reuse = 1  
net.ipv4.tcp_mem = 94500000 915000000 927000000   
net.ipv4.tcp_fin_timeout = 30 
net.ipv4.tcp_keepalive_time = 30  
net.ipv4.ip_local_port_range = 1024 65000
fs.file-max = 13000000 
net.ipv4.tcp_keepalive_probes=5
net.ipv4.tcp_orphan_retries=3
net.ipv4.tcp_synack_retries=2
kernel.printk_ratelimit = 30
kernel.printk_ratelimit_burst = 200
net.ipv6.conf.all.disable_ipv6=0
net.ipv6.conf.default.disable_ipv6=0
net.ipv6.conf.lo.disable_ipv6=0
EOF
sysctl -p &> /dev/null && echo "innter_sys_optimization is ok!!!" || `echo "innter_sys_optimization is fail ... ";exit 1`
}

function limit_file_num(){
cat >> /etc/security/limits.conf <<EOF
* soft nofile 1024000
* hard nofile 1024000
hive   - nofile 1024000
hive   - nproc  1024000
# End of file
EOF

ulimit -n 1024000
ulimit -HSn 102400
echo "limit_file_num is ok!!!" 
}

function history_set(){
cat >> /etc/profile <<EOF
USER_IP=\`who -u am i 2>/dev/null| awk '{print \$NF}'|sed -e 's/[()]//g'\`   
HISTFILESIZE=4000  
HISTSIZE=4000  
HISTTIMEFORMAT="%F %T \${USER_IP} \`whoami\` "  
export HISTTIMEFORMAT 
EOF
echo "history_set is ok!!!" 
}

selinux_close
install_tools
service_close
time_zone
innter_sys_optimization
limit_file_num
history_set
