#!/bin/bash


_install() {
    SYSCTL_CONFIG="$BASEDIR/configs/sysctl"

    cp -f $SYSCTL_CONFIG/sysctl.conf.default /etc/sysctl.conf

    # nf_conntrack
    echo "options nf_conntrack hashsize=524288" > /etc/modprobe.d/nf_conntrack.conf
    modprobe nf_nat_ftp
    modprobe nf_conntrack
    echo '4194304' > /proc/sys/net/nf_conntrack_max

    # 临时配置
    if [[ $OS == "CentOS7" ]];then
        sed -i "/DefaultLimitNOFILE/d" /etc/systemd/system.conf
        sed -i "/DefaultLimitNPROC/d" /etc/systemd/system.conf
        sed -i "/DefaultLimitCORE/d" /etc/systemd/system.conf
        echo "DefaultLimitNOFILE=1000000" >> /etc/systemd/system.conf
        echo "DefaultLimitNPROC=150000" >> /etc/systemd/system.conf
        echo "DefaultLimitCORE=infinity" >> /etc/systemd/system.conf
        systemctl daemon-reexec > /dev/null 2>&1
    fi

    # 写入配置文件
    echo "*                soft    core            unlimited" > /etc/security/limits.conf
    echo "*                soft    nofile          1000000" >> /etc/security/limits.conf
    echo "*                hard    core            unlimited" >> /etc/security/limits.conf
    echo "*                hard    nofile          1000000" >> /etc/security/limits.conf

    if [[ $OS == "CentOS6" ]];then
        sed -i "s/1024/150000/" /etc/security/limits.d/90-nproc.conf
    elif [[ $OS == "CentOS7" ]];then
        sed -i "s/4096/150000/" /etc/security/limits.d/20-nproc.conf
    else
        PRINT_ERR "Not support OS '$OS'"
        return 1
    fi

    if [[ $ARCH == "aarch64" ]];then
        sed -i '/tcp_syncookies/d' /etc/sysctl.conf
    fi

    sysctl -p 2>&1 > /dev/null
#没有意义，重新登录即可生效
#        ulimit -SHu 150000
#        ulimit -SHn 1000000
#        ulimit -SHc unlimited
}

_check() {
    _check="PASS"

    if [[ $(lsmod | grep -c ^nf_conntrack) -ne 0 ]];then
        echo '4194304' > /proc/sys/net/nf_conntrack_max
        if [[ $(cat /proc/sys/net/nf_conntrack_max) != "4194304" ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Module nf_conntrack_max Setting Incorrect."
        fi
    fi
    if [[ $OS == "CentOS6" ]];then
        if [[ $(cat /etc/security/limits.conf | grep hard|grep core|awk '{print $NF}') != unlimited ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Core File Size Setting Not Correct."
        fi
        if [[ $(grep "^\*" /etc/security/limits.conf|grep nofile|awk '{print $NF}'|sort -u) != 1000000 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Open Files Setting Not Correct."
        fi
        if [[ $(grep "^\*" /etc/security/limits.d/90-nproc.conf|grep nproc|awk '{print $NF}'|sort -u) != 150000 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Max User Processes Setting Not Correct."
        fi
    elif [[ $OS == "CentOS7" ]];then
        if [[ $(grep -c "DefaultLimitCORE=infinity"  /etc/systemd/system.conf) != 1 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Core File Size Setting Not Correct In Systemd."
        fi
        if [[ $(grep "^\*" /etc/security/limits.conf|grep core|awk '{print $NF}'|sort -u) != "unlimited" ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Core File Size Setting Not Correct In Limits Config."
        fi
        if [[ $(grep -c "DefaultLimitNOFILE=1000000"  /etc/systemd/system.conf) != 1 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Open Files Setting Not Correct."
        fi
        if [[ $(grep "^\*" /etc/security/limits.conf|grep nofile|awk '{print $NF}'|sort -u) != 1000000 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Open Files Setting Not Correct In Limits Config."
        fi
        if [[ $(grep "^\*" /etc/security/limits.d/20-nproc.conf|grep nproc|awk '{print $NF}'|sort -u) != 150000 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Max User Processes Setting Not Correct In Limits Config."
        fi
        if [[ $(grep -c "DefaultLimitNPROC=150000"  /etc/systemd/system.conf) != 1 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] Max User Processes Setting Not Correct."
        fi
    else
        PRINT_ERR "Not support OS '$OS'"
        return 1
    fi
}
