#!/bin/bash


function fn_check_ppp_isdown()
{

	for eth in $( cat /opt/pppoe/pppoe_user.txt | awk '{print $4}' | sort -uV);do
		echo ${eth}":" $(ethtool ${eth} | grep -i speed | awk '{print $2}')
	done
	mkdir  -p /opt/pppoe
	:> /opt/pppoe/pppoe_error_user.txt

	unset list_err_num
	list_err_num=()

    config_list=`ls /etc/sysconfig/network-scripts/ifcfg-ppp*|sort -Vk 1`
    for config in $config_list
    do
        str_name=$(echo ${config} |  grep -Eo "ppp[0-9]+" )
        ser_user=$(cat ${config}  | awk -F'=' '/USER=/ {print $2}')
        #echo ${str_name} ${config}
        if [[ $(echo ${config} | grep -c bak) == 1 ]];then
            config_old=${config}
            config=$(echo  ${config} | sed -e 's/.bak//')
			echo ${config}"未拨通"
            mv ${config_old}  ${config}
            ifup ${config}
            if [[ $(/usr/sbin/pppoe-status ${config} | grep -c "Link is up") -eq 1 ]];then
                echo ${str_name}" 重拨成功"
            else
                echo ${str_name}" 重拨失败"
				num_line=$(echo ${str_name} | tr -cd  "[0-9]*" | awk '{printf "%d",$1+1 }'  )
				if [[ -s /opt/pppoe/pppoe_user.txt  ]];then
					cat /opt/pppoe/pppoe_user.txt | sed  -n "${num_line}p" >> /opt/pppoe/pppoe_error_user.txt
				else
					cat ${config} | grep "USER=" | awk -F'=' '{print $2}' >> /opt/pppoe/pppoe_error_user.txt
				fi
            fi
        else
			if [[ $(/usr/sbin/pppoe-status ${config} | grep -c "Link is up") -eq 0 ]];then
				num_line=$(echo ${str_name} | tr -cd  "[0-9]*" | awk '{printf "%d",$1+1 }' )
				echo "第${num_line}行帐号不通"
				list_err_num+=("${num_line}")
				if [[ -s /opt/pppoe/pppoe_user.txt  ]];then
					cat /opt/pppoe/pppoe_user.txt | sed  -n "${num_line}p" >> /opt/pppoe/pppoe_error_user.txt
				else
					cat ${config} | grep "USER=" | awk -F'=' '{print $2}' >> /opt/pppoe/pppoe_error_user.txt
				fi
			fi
        fi
    done
	

	num_errppp=$(cat /opt/pppoe/pppoe_error_user.txt | wc -l)
	echo ""
	if [[ ${num_errppp} -gt 0 ]];then
		echo -e "UUID:$(cat /etc/salt/minion_id) ${num_errppp}/$(cat /opt/pppoe/pppoe_user.txt| wc -l )线路不通" >> /opt/pppoe/pppoe_error_user.txt
	else
		echo -e "UUID:$(cat /etc/salt/minion_id) 拨号 all success" >> /opt/pppoe/pppoe_error_user.txt
	fi
	cat /opt/pppoe/pppoe_error_user.txt
	echo "行数 ${list_err_num[*]}"
	if [[ ${#list_err_num[*]} -gt 0 ]];then
		echo $(date "+%Y-%m-%d %H:%M:%S")  "${#list_err_num[*]}/$(cat /opt/pppoe/pppoe_user.txt| wc -l )断线  行数: ${list_err_num[*]} " >> /opt/pppoe/offline_ppp.log
	fi
}


function fn_ksp2p_nicout_check_old(){
	operation=$1
	uuid=$(cat  /etc/salt/minion_id)
	flag_nicout=0
	str_nic_out=$(ps aux | grep ksp2p | grep -Po "nic_out=([[:alnum:],]+)" | awk -F '=' '{print $2}')
	#if [[ -n ${str_nic_out} ]];then
	
	if [[ $(ps aux | grep -v grep  | grep -c  'nic_out=') -eq 0 ]];then
		exit
	else
		if [[ 1 == 1 ]];then
			array=(${str_nic_out//,/ })
			num_new=0
			unset list_nowork_eth
			list_nowork_eth=()
			eth=$(/usr/sbin/ip route | grep metric |  awk -F'dev'  '{print  $2}'  |  awk  '{print $NF FS $0}'   | sort -n | head -n1 | awk '{print $2}')
			for ppp  in $(/usr/sbin/ip a | grep -E 'global ppp|global macv' | grep -Eo 'ppp[0-9]+|macv[0-9]+' );do
				if [[ $(echo $str_nic_out | grep -Ec "\b${ppp}\b") = 0 ]];then
					#echo $ppp "未映射"
					list_nowork_eth+=("${ppp}")
					((num_new++))
				else
					c=$(echo ${array[*]} | sed 's/\<'$ppp'\>//')
					unset array #清空a数组
					array=(${c[@]}) #将新的数组赋值给a
				fi
			done
			
			#去除管理网卡
			array=(${array[*]/${eth}})
			if [[ -z ${array[*]} ]];then
				#echo "array为空值"
				unset array
			# else
			# 	echo  ${array[*]} "已断连"
			fi
			if [[ -s /opt/pppoe/static_ip.txt  ]];then
				num_all=$(cat /opt/pppoe/static_ip.txt 2> /dev/null | wc -l  ) 
			elif [[ -s /opt/pppoe/pppoe_user.txt  ]];then
				num_all=$(cat /opt/pppoe/pppoe_user.txt 2> /dev/null | wc -l )
			fi
			if [[ ${#array[*]}  -gt 0 ]];then
				#有断的
				num_loss=${#array[*]}
				flag_nicout=3
			fi
			if [[ ${num_new}  -gt 0 ]];then
				#有新拨上的
				flag_nicout=2
			fi
		fi
		
		p_loss=$(echo $num_loss |  awk '{a=$1/'$num_all'*100} {printf("%d",int(a)==a?a:(int(a)+1))}')      
		p_new=$(echo $num_new |  awk '{a=$1/'$num_all'*100} {printf("%d",int(a)==a?a:(int(a)+1))}')

		if [[ ${flag_nicout} -eq 3 ]];then
			echo  ${uuid} "nic_out有断连  ${#array[*]}/$num_all断了 ${array[*]}"
			if [[ ${num_new}  -gt 0 ]];then
				echo  "${#list_nowork_eth[*]}/$num_all未映射  ${list_nowork_eth[*]}"	
			fi
		elif [[ ${flag_nicout} -eq 2 ]];then
			echo  ${uuid} "nic_out未映射  ${num_new}/$num_all  ${list_nowork_eth[*]}"	
		fi


		if [[ ${flag_nicout} -gt 1 ]];then
			if [[ ${operation} == 'restart' ]];then
				docker restart ksp2p_1
				docker restart ksp2plive_1
			fi
		fi
		
		if [[ ${p_loss} -gt 0 ||  ${p_new} -gt 0 ]];then
			#打印日志
			if [[ ${p_loss} -gt 0 ]];then
				echo $(date "+%Y-%m-%d %H:%M:%S") "p_loss:${p_loss}%" "${#array[*]}/$num_all断了 ${array[*]}" >> /opt/pppoe/nic_out.log
			fi
			if [[ ${p_new} -gt 0 ]];then
				echo $(date "+%Y-%m-%d %H:%M:%S") "p_new:${p_new}%" "${#list_nowork_eth[*]}/$num_all未映射  ${list_nowork_eth[*]}"  >> /opt/pppoe/nic_out.log
			fi

			# #定量重启 关闭
			# if [[ ${p_loss} -ge 10 ||  ${p_new} -ge 20 ]];then
			# 	echo $(date "+%Y-%m-%d %H:%M:%S") "docker restart ksp2p_1" >> /opt/pppoe/nic_out.log
			# 	docker restart ksp2p_1
			# fi

			#非高峰期调整
			time_hour=$(date "+%Y-%m-%d %H:%M:%S" | awk '{print $2}' | awk -F':' '{print $1}')
			#if [[ ${time_hour} -le 10 ||  ( ${time_hour} -ge 14 && ${time_hour} -le 17 )  ]];then

			#0-9
			if [[ ${time_hour} -le 9   ]];then
				docker restart ksp2p_1
				docker restart ksp2plive_1
				echo $(date "+%Y-%m-%d %H:%M:%S") "docker restart ksp2p_1(9)" >> /opt/pppoe/nic_out.log	
				
			elif [[ ${time_hour} -ge 14 && ${time_hour} -le 17 ]];then
				#线路大于20% 大于5条 或者大于20条
				docker restart ksp2p_1
				docker restart ksp2plive_1
				if [[ ( ${p_new} -gt 20  && ${num_new} -gt 5 ) ||  ${num_new} -gt 20 ]];then
					echo $(date "+%Y-%m-%d %H:%M:%S") "docker restart ksp2p_1(14-17)" >> /opt/pppoe/nic_out.log
				fi
			fi


			#增加线路比例大于50%
			if [[ ${p_new} -gt 50  ]];then
				echo $(date "+%Y-%m-%d %H:%M:%S") "p_new:${p_new}% docker restart ksp2p_1" >> /opt/pppoe/nic_out.log
				docker restart ksp2p_1
				docker restart ksp2plive_1
			fi

		else
			echo "no problem"
		fi



	fi
	

	
	fn_ksp2p_smartdns_check
	

}


function fn_ksp2p_nicout_check() {
    operation=$1
    uuid=$(cat /etc/salt/minion_id)
    flag_nicout=0
    str_nic_out=$(ps aux | grep ksp2p | grep -Po "nic_out=([[:alnum:],]+)" | awk -F '=' '{print $2}')
    
    # 检查是否存在包含 'nic_out=' 的进程
    if [[ $(ps aux | grep -v grep | grep -c 'nic_out=') -eq 0 ]]; then
        exit
    else
        if [[ 1 == 1 ]]; then
            array=(${str_nic_out//,/ })
            num_new=0
            unset list_nowork_eth
            list_nowork_eth=()
            
            # 获取当前使用的网卡
            eth=$(/usr/sbin/ip route | grep metric | awk -F'dev' '{print $2}' | awk '{print $NF FS $0}' | sort -n | head -n1 | awk '{print $2}')
            
            # 检查未映射的网卡和已断连的网卡
            for ppp in $(/usr/sbin/ip a | grep -E 'global ppp|global macv' | grep -Eo 'ppp[0-9]+|macv[0-9]+'); do
                if [[ $(echo $str_nic_out | grep -Ec "\b${ppp}\b") = 0 ]]; then
                    list_nowork_eth+=("${ppp}")
                    ((num_new++))
                else
                    c=$(echo ${array[*]} | sed 's/\<'$ppp'\>//')
                    unset array
                    array=(${c[@]})
                fi
            done
            
            # 去除管理网卡
            array=(${array[*]/${eth}})
            
            if [[ -z ${array[*]} ]]; then
                unset array
            fi
            
            if [[ -s /opt/pppoe/static_ip.txt ]]; then
                num_all=$(cat /opt/pppoe/static_ip.txt 2> /dev/null | wc -l)
            elif [[ -s /opt/pppoe/pppoe_user.txt ]]; then
                num_all=$(cat /opt/pppoe/pppoe_user.txt 2> /dev/null | wc -l)
			else
				echo "非汇聚"
				exit
            fi
            
            if [[ ${#array[*]} -gt 0 ]]; then
                num_loss=${#array[*]}
                flag_nicout=3
            fi
            
            if [[ ${num_new} -gt 0 ]]; then
                flag_nicout=2
            fi
        fi
        
        p_loss=$(echo $num_loss | awk '{a=$1/'$num_all'*100} {printf("%d", int(a)==a ? a : (int(a)+1))}')
        p_new=$(echo $num_new | awk '{a=$1/'$num_all'*100} {printf("%d", int(a)==a ? a : (int(a)+1))}')
        
        if [[ ${flag_nicout} -eq 3 ]]; then
            echo ${uuid} "nic_out有断连 ${#array[*]}/$num_all断了 ${array[*]}"
            
            if [[ ${num_new} -gt 0 ]]; then
                echo "${#list_nowork_eth[*]}/$num_all未映射 ${list_nowork_eth[*]}"
            fi
        elif [[ ${flag_nicout} -eq 2 ]]; then
            echo ${uuid} "nic_out未映射 ${num_new}/$num_all ${list_nowork_eth[*]}"
        fi


        #指定参数restart 进行重启 用于开启后重新映射
        if [[ ${flag_nicout} -gt 1 ]]; then
            if [[ ${operation} == 'restart' ]]; then
                docker restart ksp2p_1
				docker restart ksp2plive_1
            fi
        fi
        
		#如果nic_out有断连、未映射的比例大于0
        if [[ ${p_loss} -gt 0 || ${p_new} -gt 0 ]]; then
            if [[ ${p_loss} -gt 0 ]]; then
                echo $(date "+%Y-%m-%d %H:%M:%S") "p_loss:${p_loss}%" "${#array[*]}/$num_all断了 ${array[*]}" >> /opt/pppoe/nic_out.log
            fi
            
            if [[ ${p_new} -gt 0 ]]; then
                echo $(date "+%Y-%m-%d %H:%M:%S") "p_new:${p_new}%" "${#list_nowork_eth[*]}/$num_all未映射 ${list_nowork_eth[*]}" >> /opt/pppoe/nic_out.log
            fi
            
			#time_hour 标识小时时间
            time_hour=$(date "+%Y-%m-%d %H:%M:%S" | awk '{print $2}' | awk -F':' '{print $1}')
            
			#如果小于等于9点，重启下
            if [[ ${time_hour} -le 9 ]]; then
                docker restart ksp2p_1
				docker restart ksp2plive_1
                echo $(date "+%Y-%m-%d %H:%M:%S") "docker restart ksp2p_1(le 9)" >> /opt/pppoe/nic_out.log
            elif [[ ${time_hour} -ge 14 && ${time_hour} -le 17 ]]; then
               
                
                if [[ (${p_new} -gt 20 && ${num_new} -gt 5) || ${num_new} -gt 20 ]]; then
					#如果在14点到17点，未映射的比例大于20且新线路大于5,或者新线路大于20，重启下
					docker restart ksp2p_1
					docker restart ksp2plive_1
                    echo $(date "+%Y-%m-%d %H:%M:%S") "docker restart ksp2p_1(14-17)" >> /opt/pppoe/nic_out.log
                fi
            else
            
				if [[ ${p_new} -gt 70 ]]; then
					#任意时间 未映射的比例大于70，重启下
					echo $(date "+%Y-%m-%d %H:%M:%S") "p_new:${p_new}% docker restart ksp2p_1" >> /opt/pppoe/nic_out.log
					docker restart ksp2p_1
					docker restart ksp2plive_1
				fi
			fi

        else
            echo "no problem"
        fi
    fi
    
    fn_ksp2p_smartdns_check
}




function init_dns(){
    chattr -i /etc/resolv.conf;
    :> /etc/resolv.conf
    echo "nameserver 127.0.0.1" >>/etc/resolv.conf;
    echo "nameserver 119.29.29.29" >>/etc/resolv.conf;
    echo "nameserver 114.114.114.114" >>/etc/resolv.conf;
    echo "nameserver 223.6.6.6" >>/etc/resolv.conf;
    echo "options timeout:2 attempts:2 rotate" >> /etc/resolv.conf;
}


function fn_ksp2p_smartdns_check(){
	if [[ $(ps aux |  grep -v grep  | grep -c doh-client)  -gt 0 ]];then
		#echo "dnsmasq 已部署"
		if [[ $(docker ps -a | grep -c smartdns) == 1  ]];then
			docker rm -f smartdns
			sed -i "/smartdns/d" /var/spool/cron/root
			sed -i "/smartdns/d" /etc/rc.d/rc.local
		fi
		exit
	fi

	if [[ $(docker ps -a | grep smartdns | grep -Ec -i "exit|restart|create" ) == 1 ]];then
		init_dns
		docker rm -f smartdns
		docker run --restart=always -itd --net=host --name=smartdns registry.cn-hangzhou.aliyuncs.com/p2pcdn/smartdns:v0.2
	elif [[ $(cat /etc/resolv.conf | grep -c "127.0.0.1") == 1 &&  $(docker ps -a | grep  -c smartdns ) == 0 ]];then
		init_dns
		docker run --restart=always -itd --net=host --name=smartdns registry.cn-hangzhou.aliyuncs.com/p2pcdn/smartdns:v0.2
	fi
}



function ksp2p_pppoe_crontab()
{
	if  [[ $(ip addr | grep -Ec ppp[0-9]+) -gt 0 || ( -s  /opt/pppoe/pppoe_user.txt ) ]];then
		echo "设置拨号线路检测定时任务 (10点前  14-17点)"
		curl -o /opt/pppoe/ksp2p_nicout_check.sh  https://automateplus.oss-cn-hangzhou.aliyuncs.com/ksp2p_nicout_check.sh
		chmod +x /opt/pppoe/ksp2p_nicout_check.sh
		sed -i "/ksp2p_nicout_check.sh nicout/d" /var/spool/cron/root
		sed -i "/ksp2p_nicout_check.sh ppp/d" /var/spool/cron/root
		sed -i "/ksp2p_nicout_check.sh smartdns/d" /var/spool/cron/root
		echo "2-59/30 * * * * su - root -c ' /opt/pppoe/ksp2p_nicout_check.sh nicout'" >> /var/spool/cron/root
		echo "2-59/30 * * * * su - root -c ' /opt/pppoe/ksp2p_nicout_check.sh ppp'" >> /var/spool/cron/root
		echo "*/5 * * * * sh /opt/pppoe/ksp2p_nicout_check.sh smartdns" >> /var/spool/cron/root
		
		#su - root -c '
		
		sed -i "/smartdns/d" /etc/rc.d/rc.local
		echo "docker start smartdns" >> /etc/rc.d/rc.local
		
		sed -i "/ksp2p_nicout_check/d" /etc/rc.d/rc.local
		echo "sh /opt/pppoe/ksp2p_nicout_check.sh reboot"  >> /etc/rc.d/rc.local
	else
		echo "非拨号"
	fi
}


function ksp2p_smartdns_crontab()
{
	echo "设置smartdns检测定时任务"
	mkdir -p /opt/pppoe/
	curl -o /opt/pppoe/ksp2p_nicout_check.sh  https://automateplus.oss-cn-hangzhou.aliyuncs.com/ksp2p_nicout_check.sh
	chmod +x /opt/pppoe/ksp2p_nicout_check.sh
	sed -i "/smartdns/d" /var/spool/cron/root
	echo "*/5 * * * * sh /opt/pppoe/ksp2p_nicout_check.sh smartdns" >> /var/spool/cron/root
}

function fn_ksp2p_multi_line_speed_check()
{
	mkdir -p /opt/pppoe
	multi_line_speed_use=$(ps aux | grep multi_line_speed | grep -v grep | xargs -n1 | grep multi_line_speed | grep -v grep | awk -F '=' '{print  $2}' | awk -F. '{print $1}' | tr -cd [0-9])
	bandwidth_number=$(cat /opt/hyops/bandwidth_number)
	multi_line_speed_new=$(cat /opt/hyops/bandwidth | awk '{printf "%d",$1/'${bandwidth_number}'}'	)
	#绝对值小于1就不重启
	if [[ $(echo "${multi_line_speed_use} ${multi_line_speed_new}" | awk '{diff=$1-$2; abs_diff=diff<0?-diff:diff; print abs_diff}' ) -gt 1  ]];then
		echo $(date "+%Y-%m-%d %H:%M:%S") "单线带宽$multi_line_speed_use 需更新至 ${multi_line_speed_new}" >> /opt/pppoe/nic_out.log
		#非高峰期调整
		time_hour=$(date "+%Y-%m-%d %H:%M:%S" | awk '{print $2}' | awk -F':' '{print $1}')
		if [[ ${time_hour} -le 10 ||  ( ${time_hour} -ge 14 && ${time_hour} -le 17 )  ]];then
			echo $(date "+%Y-%m-%d %H:%M:%S") "带宽更新 docker restart ksp2p_1" >> /opt/pppoe/nic_out.log
			docker restart ksp2p_1
			docker restart ksp2plive_1
		fi
	else
		echo "no problem"
	fi
}

function unset_ksp2p_pppoe_crontab()
{
	echo "关闭拨号线路检测定时任务"
	sed -i "/ksp2p_nicout_check.sh nicout/d" /var/spool/cron/root
	sed -i "/ksp2p_nicout_check.sh ppp/d" /var/spool/cron/root
	sed -i "/ksp2p_nicout_check/d" /etc/rc.d/rc.local
}

function unset_ksp2p_tcpre_crontab()
{
	echo "关闭tcp重传检测定时任务"
	sed -i "/tcp_re_rate/d" /var/spool/cron/root

}

function unset_ksp2p_multi_line_speed_crontab()
{

	echo "关闭带宽定时检测任务"
	sed -i "/multi_line_speed/d" /var/spool/cron/root
}
function ksp2p_tcpre_crontab()
{
	
	echo "设置tcp重传检测定时任务"
	curl -o /root/tcp_re_rate.sh  https://automateplus.oss-cn-hangzhou.aliyuncs.com/tcp_re_rate.sh
	chmod +x /root/tcp_re_rate.sh
	sed -i "/tcp_re_rate/d" /var/spool/cron/root
	echo "*/5 10-23 * * * sh /root/tcp_re_rate.sh" >> /var/spool/cron/root


}

function ksp2p_multi_line_speed_crontab()
{
	echo "设置带宽定时检测任务 (10点前  14-17点)"
	mkdir -p /opt/pppoe
	curl -o /opt/pppoe/ksp2p_nicout_check.sh  http://down.9494.org:9999/pppoe/ksp2p_nicout_check.sh
	chmod +x /opt/pppoe/ksp2p_nicout_check.sh
	sed -i "/multi_line_speed/d" /var/spool/cron/root
	echo "2-59/10 * * * * su - root -c ' /opt/pppoe/ksp2p_nicout_check.sh multi_line_speed'" >> /var/spool/cron/root

	chmod +x /etc/rc.d/rc.local
}

#重启检测
function fn_ksp2p_pppoe_reboot()
{
	sleep 5m
	fn_ksp2p_nicout_check	
}


if [[ $1 == "reboot" ]];then
	fn_ksp2p_pppoe_reboot
elif [[ $1 == "nicout" ]];then
	fn_ksp2p_nicout_check $2
elif [[ $1 == "set_tcpre" ]];then	
	ksp2p_tcpre_crontab
elif [[ $1 == "unset_tcpre" ]];then	
	unset_ksp2p_tcpre_crontab
elif [[ $1 == "set_multi_line" ]];then	
	ksp2p_multi_line_speed_crontab
elif [[ $1 == "unset_multi_line" ]];then	
	unset_ksp2p_multi_line_speed_crontab
elif [[ $1 == "set_nic" ]];then	
	ksp2p_pppoe_crontab
elif [[ $1 == "unset_nic" ]];then	
	unset_ksp2p_pppoe_crontab
elif [[ $1 == "smartdns" ]];then	
	fn_ksp2p_smartdns_check
elif [[ $1 == "set_smartdns" ]];then	
	ksp2p_smartdns_crontab
elif [[ $1 == "ppp" ]];then	
	fn_check_ppp_isdown
elif [[ $1 == "multi_line_speed" ]];then	
	fn_ksp2p_multi_line_speed_check
fi

