#!/bin/bash
# Date:2022-09-05
# 系统优化脚本

base(){
    rpm -ivh "${event_path}"/Install_Packages/ntp-4.2.8p14-5.p01.ky10.aarch64.rpm
    rpm -ivh "${event_path}"/Install_Packages/ntp-help-4.2.8p14-5.p01.ky10.noarch.rpm
    systemctl start ntpd
    systemctl enable ntpd
    date

    # 离线安装ntp
    if [ "${TYPE}" == "allinone" ];then
        hostnamectl set-hostname ${ONE_IP}
    fi
    if [ "${TYPE}" == "cluster" ];then
        #hostnamectl set-hostname localhost
        hostname
    fi
    ntpdate ${TIMESERVER} && hwclock -w >>/dev/null 2>&1 &

    sed -i.backup 's/defaults/defaults,noatime/g' /etc/fstab
    
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    echo 'Asia/Shanghai' >/etc/timezone

    cat << EOF > /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
10 0 * * * root ntpdate ${TIMESERVER} && hwclock -w >>/dev/null 2>&1 &
EOF

    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    echo 'Asia/Shanghai' >/etc/timezone

    cat << EOF > /etc/sysctl.conf
net.ipv4.tcp_retrans_collapse = 0
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.tcp_window_scaling = 1024      65000
net.ipv4.ip_local_port_range = 1
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_wmem = 4096 65536 8388608
net.ipv4.tcp_rmem = 4096 87380 8388608
kernel.panic = 5
net.ipv4.tcp_max_syn_backlog = 262144
net.core.somaxconn = 16384
net.core.rmem_max = 8388608
net.core.wmem_max = 8388608
net.netfilter.nf_conntrack_max = 409600
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_tw_buckets = 256000
net.ipv4.tcp_window_scaling = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
kernel.msgmnb = 655350
kernel.msgmax = 655350
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_keepalive_probes = 5
fs.inotify.max_user_watches = 2097152
vm.max_map_count = 262144
fs.inotify.max_user_instances = 512
fs.file-max = 1000000
EOF
    sleep 2
    sysctl -p
    cat /etc/profile | grep ulimit
    if [ $? -ne 0 ];then
        echo "ulimit not exist!!!"
        cat << EOF >> /etc/profile
ulimit -HSn 655350
EOF
    else
        echo "ulimit have been exist!!!"
    fi
    source /etc/profile

    check_dns=`cat /etc/resolv.conf | grep nameserver | awk -F " " '{print $1}' | uniq`
    if [ "$check_dns" == "nameserver" ];then
        echo "nameserver have been exist."
    else
        echo "注意事项"
        echo "nameserver must be exist!"
        echo "nameserver must be exist: 测试主机DNS 解析 失败 不存在！"
        echo "请检查配置DNS地址: /etc/resolv.conf"
        echo "nameserver 114.114.114.114" >>  /etc/resolv.conf
#        cat << EOF > /etc/resolv.conf
# Generated by NetworkManager
#nameserver 114.114.114.114
#EOF
        #exit
    fi

    #history log
    cat << EOF > /etc/profile.d/base.sh
# History format 
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
shopt -s histappend
export HISTSIZE=100000
export HISTFILESIZE=100000
export PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; }); logger -p local5.info "\$HOSTNAME [HIST] : \$SSH_CLIENT : \$PWD : \$msg"; }'
export PS1="\[\e[1;32m\]\u\[\e[m\]\[\e[1;33m\]@\[\e[m\]\[\e[1;31m\]\h\[\e[m\]:\[\033[36m\]\w\[\033[00m\]#"
EOF

    cat << EOF > /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 65535
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
EOF

    #restart rsyslog
    echo "##### restart rsyslog #####"
    systemctl restart rsyslog.service
    systemctl enable rsyslog.service

    ##KyCentos 79
    echo "##### disable firewalld #####"
    systemctl disable --now firewalld
    echo "##### disable dnsmasq #####"
    systemctl disable --now dnsmasq
    echo "##### disable NetworkManager #####"
    #systemctl disable --now NetworkManager
    systemctl stop firewalld.service
    systemctl enable rsyslog.service
    systemctl restart rsyslog.service

    setenforce 0
    sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
    sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

    swapoff -a && sysctl -w vm.swappiness=0
    sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
}

echo "======== start base system ========"
if [ "${TYPE}" == "allinone" ];then 
    event_path=$1
    cat << EOF > /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
${ONE_IP}   ${K8S_HOSTNAME} local localhost localhost.localdomain
${ONE_IP}   registry.edoc2.com
EOF
chmod -R 755 "$event_path"/soft/k8s"$KVERSION"/*
fi

if [ "${TYPE}" == "cluster" ];then
    event_path="/tmp" 
fi
base
