關於iptables的調整

背景說明:
bash

iptables的contrack模塊,由於業務量大,而致使drop packet的情況,現針對線上機器進行灰度,灰度的原則是:沒有使用iptable,則將其禁用並修改hash表,若是有使用iptables,則直接修改hash表便可。curl


腳本內容tcp

#!/bin/bash
iptables_init="/etc/init.d/iptables"
date=`date +%F`
#將5.9和6.3系統的iptables相關的模塊禁用,包括conntrack,filter表,nat表,mangle表

function disable_modules_5.9() {
        cat >/etc/modprobe.d/kugou.conf <<EOF
        install nfnetlink       /bin/true 
        install ip_conntrack    /bin/true   
        install xt_conntrack    /bin/true
        install ip6_tables      /bin/true
        install ip6table_filter /bin/true
        install iptable_filter  /bin/true
        install ebtables        /bin/true
        install ebtable_nat     /bin/true
        install ip_nat          /bin/true
        install iptable_nat     /bin/true
        install iptable_mangle  /bin/true
        install ip6table_mangle /bin/true
EOF
        sed -i 's/^[[:space:]]*//g' /etc/modprobe.d/kugou.conf      
}

function disable_modules_6.3() {
cat >/etc/modprobe.d/kugou.conf <<EOF
        install nfnetlink         /bin/true
        install nf_conntrack      /bin/true
        install nf_defrag_ipv4    /bin/true
        install nf_conntrack_ipv4 /bin/true
        install ip6_tables        /bin/true
        install ip6table_filter   /bin/true
        install iptable_filter    /bin/true
        install ebtable_nat       /bin/true
        install ebtables          /bin/true
        install nf_nat            /bin/true
        install iptable_nat       /bin/true
        install iptable_mangle    /bin/true
        install ip6table_mangle   /bin/true
EOF
        sed -i 's/^[[:space:]]*//g' /etc/modprobe.d/kugou.conf      
}

#調整5.9和6.3系統的內核bucket參數,包括conntrack支持最大的數目和會話超時時間
function setup_bucket_5.9() {
        cp ${iptables_init} /root/iptables-${date}
        sed -i '/\<ip_conntrack_max\>/ d' ${iptables_init} 
        sed -i '/\<ip_conntrack_tcp_timeout_syn_recv\>/ d' ${iptables_init }
        sed -i '/\<ip_conntrack_tcp_timeout_established\>/ d' ${iptables_init}
        sed -i '/touch $VAR_SUBSYS_IPTABLES/ i\    echo 1048576 > /proc/sys/net/ipv4/netfilter/ip_conntrack_max' ${iptables_init}
        sed -i '/touch $VAR_SUBSYS_IPTABLES/ i\    echo 30 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_syn_recv' ${iptables_init}
        sed -i '/touch $VAR_SUBSYS_IPTABLES/ i\    echo 60 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established' ${iptables_init}
        echo 60 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established
        echo 30 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_syn_recv
        echo 1048576 > /proc/sys/net/ipv4/netfilter/ip_conntrack_max
}

function setup_bucket_6.3() {
        cp ${iptables_init} /root/iptables-${date}
        sed -i '/\<nf_conntrack_max\>/ d' ${iptables_init}
        sed -i '/\<nf_conntrack_tcp_timeout_established\>/ d' ${iptables_init}
        sed -i "/touch $VAR_SUBSYS_IPTABLES/ i\    echo 655350 > /proc/sys/net/nf_conntrack_max" ${iptables_init}
        sed -i "/touch $VAR_SUBSYS_IPTABLES/ i\    echo 655350 > /proc/sys/net/netfilter/nf_conntrack_max" ${iptables_init}
        sed -i "/touch $VAR_SUBSYS_IPTABLES/ i\    echo 60 > /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established" ${iptables_init}
        echo 1048576  > /proc/sys/net/nf_conntrack_max
        echo 1048576  > /proc/sys/net/netfilter/nf_conntrack_max
        echo 60 > /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established
}

#禁用iptables客戶端
function disable_iptables_client() {
        if [ -e /sbin/iptables ];then
            mv /sbin/iptables /sbin/selbatpi
            else
            echo "iptables客戶端已經設置好"
        fi
}

function warn_logs() {
        if [ -e /usr/bin/curl ];then
            curl http://10.1.2.128/iptables_on >/dev/null 
        fi
}

#功能調用,對於已經開啓iptables的機器,調整bucket參數,告警上報;對於未開啓的iptables的機器,則調整bucket參數,禁用模塊,禁用iptables客戶端
function main() {
        cp ${iptables_init} ${date}-iptables 
        osversion=`awk '{print $3}' /etc/redhat-release`
        case ${osversion} in
            5.[0-9])
                if [ `lsmod |grep iptables | wc -l` -eq 0 ];then
                disable_modules_5.9
                disable_iptables_client
                else
                warn_logs
                fi
                setup_bucket_5.9
                ;;
            6.[0-6])
                if [ `lsmod |grep iptables | wc -l` -eq 0 ];then
                disable_modules_6.3
                disable_iptables_client
                else
                warn_logs
                fi
                setup_bucket_6.3
                ;;
            *)
                echo "當前操做系統版本不支持,對應的版本爲:${osversion}"
                exit 1
            ;;
    esac
}

main
相關文章
相關標籤/搜索