#!/bin/bash
panel_path=/www/server/panel
log_file=$panel_path/logs/error.log
pidfile=$panel_path/logs/panel.pid

#check_bt_status() {
#pids=`ps -ef | grep -E 'BT-Panel|BT-Task' | grep -v grep | awk '{print $2}'`
#if [ "$pids" == '' ];then
#	echo "not runing...."
#fi
#}
# check_telnet(){
# 		if [ -f "/usr/bin/telnet" ];then
# 			echo -n ""
# 		else
# 			echo  "检测telnet命令不存在,安装telnet命令...."
# 		fi
# }


Install_telnet(){

	if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
                PM="yum"
        elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
                PM="apt-get"
        fi

	if [ "${PM}" = "yum" ];then
		Pack="telnet"
	elif [ "${PM}" = "apt-get" ];then
		Pack="telnet"
	fi

	${PM} install ${Pack} -y
	
}

check_bt_status(){
while true
do
	pids=$(ps -ef | grep -E 'BT-Panel|BT-Task' | grep -v grep | awk '{print $2}')
	if [ "$pids" == "" ];then
		sleep 1;
		echo -e "Starting Bt-Panel..."
		echo -e "------------------------------------------------------------------"
		bt restart;
		break;
	else
		bt status;
		echo -e "------------------------------------------------------------------"
		break;
	fi
done
}

check_ssl() {
ssl_file=/www/server/panel/data/ssl.pl
if [ -f "$ssl_file" ];then
	echo -e "\033[31m已开启面板SSL,请使用HTTPS访问\033[0m"
	echo -e "------------------------------------------------------------------"
else
	echo -e "\033[31m未开启面板SSL\033[0m"
	echo -e "------------------------------------------------------------------"
fi
}

check_port(){ 
port=/www/server/panel/data/port.pl
for line in `cat $port`
do
	echo -e "\033[31m面板端口:$line\033[0m"
done
}

check_url(){
port=$(cat $panel_path/data/port.pl)
	pool=http
	if [ -f $ssl_file ];then
		pool=https
	fi

	url=$(curl --insecure "$pool://127.0.0.1:$port/login" 2>&1 |grep -i html)
	if [ $? -eq 0 ];then
		echo -e "\033[31m服务器内部测试访问正常\033[0m"
	else
		echo -e "\033[31m服务器内部测试访问失败\033[0m"
		echo -e "------------------------------------------------------------------"
	fi
}

check_port_status(){
	red=$(cat /www/server/panel/data/port.pl)
	pid=$(lsof -i:${red} | sed -n '2p')
	if [ "$pid" != "" ];then
		echo -e "$pid"
		echo -e "------------------------------------------------------------------"
	fi

	ip=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
	port_status=$(echo "" | telnet $ip $red 2> /dev/null)
	if [[ $port_status =~ "Escape" ]];then
		echo -e "\033[31m面板端口外网测试访问正常\033[0m"
		echo -e "------------------------------------------------------------------"
	else
		echo -e "\033[31m面板端口外网测试访问不通\033[0m"
		echo -e "------------------------------------------------------------------"
	fi
		
}

check_disk(){
	disk=$(df -H)
	inode=$(df -i)
	if [ "$disk" ];then
		echo "--------------------------磁盘空间使用--------------------------------"
		echo "$disk"
	fi
	if [ "$inode" ];then
		echo "---------------------------Inode空间----------------------------------"
		echo "$inode"
		echo -e "-------------------------------------------------------------------"
	fi
}

error_logs(){
	red=$(cat /www/server/panel/data/port.pl)
    pid=$(lsof -i:$red)
	if [ $? -eq 0 ];then
		echo $? &>/dev/null
	else
		echo -e "-------------------------------------------------------------------"
	 	tail -n 20 $log_file
	fi
}

check_dir(){
	pyenv="/www/server/panel/pyenv"
	pyenv_files="/www/server/panel/pyenv/bin/python"
	bt_files="/etc/init.d/bt"
	if [[ ! -d "$pyenv" ]];then
		echo -e "\033[31m$pyenv\033[0m目录不存在"
	else
		echo -e "\033[31m$pyenv\033[0m目录存在"
	fi	
	if [[ ! -x "$pyenv_files" ]];then
		echo -e "\033[31m$pyenv_files\033[0m没有执行权限"
	fi
	if [[ ! -f "$pyenv_files" ]];then
		echo -e "\033[31m$pyenv_files\033[0m文件不存在"
	else
		echo -e "\033[31m$pyenv_files\033[0m文件存在"
	fi
	if [[ ! -s "$bt_files" ]];then
		echo -e "\033[31m$bt_files\033[0m文件内容为0kb"
	fi
}


Install_telnet
check_bt_status
check_port_status
check_ssl
check_url
check_disk
check_dir
error_logs
