#!/bin/bash

declare -a system_paramaters=("vm.dirty_writeback_centisecs=200" "vm.dirty_background_ratio=5" "vm.dirty_expire_centisecs=300" "vm.dirty_ratio=10" "vm.vfs_cache_pressure=10000" "fs.inotify.max_user_watches=262144" "fs.file-max=65536" "vm.max_map_count=262144" "vm.min_free_kbytes=1024000" "net.core.somaxconn=2048" "vm.swappiness=0")

function config_selinux() {
    SELINUX_STATUS=$(getenforce)
    if [ "${SELINUX_STATUS}" == "Enforcing" ]; then
        setenforce 0
    fi

    SELINUX_CONFIG_STATUS=$(grep "^SELINUX=" /etc/selinux/config)
    if [ "${SELINUX_CONFIG_STATUS}" = "SELINUX=enforcing" ]; then
        sed -i '/^SELINUX/s/enforcing/disabled/g' /etc/selinux/config
    fi
}

# function user_add() {
#     groupadd docker &>/dev/null
# }

# function hostname_set() {
#     for ip in ${IP_LIST[@]}; do
#         HOSTNAME=ssh root@${ip} "hostname"
#         hostnamectl set-hostname
#     done
# }

# function es_config() {
#     if ! grep file-max /etc/sysctl.conf; then
#         echo "fs.file-max=65536" >>/etc/sysctl.conf
#     fi

#     if ! grep max_map_count /etc/sysctl.conf; then
#         echo "vm.max_map_count=262144" >>/etc/sysctl.conf
#     fi

#     if ! grep vm.min_free_kbytes /etc/sysctl.conf; then
#         echo "vm.min_free_kbytes=1024000" >>/etc/sysctl.conf
#     fi

#     if ! grep net.core.somaxconn /etc/sysctl.conf; then
#         echo "net.core.somaxconn=2048" >>/etc/sysctl.conf
#     fi

#     if ! grep vm.swappiness /etc/sysctl.conf; then
#         echo "vm.swappiness=0" >>/etc/sysctl.conf
#     fi

#     if ! grep ip_local_port_range /etc/sysctl.conf; then
#         echo "net.ipv4.ip_local_port_range=1024 65300" >>/etc/sysctl.conf
#     fi

#     sysctl -p
# }

function paramaters_config() {
    for item in ${system_paramaters[@]}; do
        local key=$(echo $item | awk 'BEGIN{FS="="} {print $1}')
        if ! grep $key /etc/sysctl.conf; then
            eval echo "$item" >>/etc/sysctl.conf
        fi
    done
    if ! grep ip_local_port_range /etc/sysctl.conf; then
        echo "net.ipv4.ip_local_port_range=1024 65300" >>/etc/sysctl.conf
    fi
    sysctl -p
}

function limit_config() {
    if ! grep '* - nofile 655300' /etc/security/limits.conf; then
        echo '* - nofile 655300' >>/etc/security/limits.conf
    fi
}

function open_firewalld() {
    systemctl disable firewalld
    systemctl status firewalld | grep dead && systemctl start firewalld
}

function close_firewalld() {
    systemctl disable firewalld
    systemctl stop firewalld
}

function network_config() {
    system_release_version=$(cat /etc/redhat-release | awk -F "[ |.]" '{print $4}')
    if [ "$system_release_version"x == "8"x ]; then
        return
    elif [ -z $system_release_version ]; then
        return
    fi
    systemctl stop NetworkManager
    systemctl disable NetworkManager
}

function set_timezone() {
    timedatectl set-timezone Asia/Shanghai
}

config_selinux
# user_add
# es_config
paramaters_config
limit_config
close_firewalld
network_config
set_timezone
