You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

759 lines
24 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #!/bin/bash
  2. #########################
  3. # 广东堡塔安全技术有限公司
  4. # author: 赤井秀一
  5. # mail: 1021266737@qq.com
  6. #########################
  7. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  8. export PATH
  9. LANG=en_US.UTF-8
  10. Btapi_Url='http://www.example.com'
  11. Command_Exists() {
  12. command -v "$@" >/dev/null 2>&1
  13. }
  14. monitor_path="/www/server/bt-monitor"
  15. run_bin="/www/server/bt-monitor/BT-MONITOR"
  16. if [ ! -d "/www/server" ];then
  17. mkdir -p /www/server
  18. fi
  19. old_dir="/www/server/old_btmonitor"
  20. cd ~
  21. setup_path="/www"
  22. python_bin=$setup_path/server/bt-monitor/pyenv/bin/python
  23. cpu_cpunt=$(cat /proc/cpuinfo|grep processor|wc -l)
  24. GetSysInfo(){
  25. if [ -s "/etc/redhat-release" ];then
  26. SYS_VERSION=$(cat /etc/redhat-release)
  27. elif [ -s "/etc/issue" ]; then
  28. SYS_VERSION=$(cat /etc/issue)
  29. fi
  30. SYS_INFO=$(uname -a)
  31. SYS_BIT=$(getconf LONG_BIT)
  32. MEM_TOTAL=$(free -m|grep Mem|awk '{print $2}')
  33. CPU_INFO=$(getconf _NPROCESSORS_ONLN)
  34. echo -e ${SYS_VERSION}
  35. echo -e Bit:${SYS_BIT} Mem:${MEM_TOTAL}M Core:${CPU_INFO}
  36. echo -e ${SYS_INFO}
  37. echo -e "请截图以上报错信息发帖至论坛www.bt.cn/bbs求助"
  38. }
  39. Red_Error(){
  40. echo '=================================================';
  41. printf '\033[1;31;40m%b\033[0m\n' "$@";
  42. GetSysInfo
  43. exit 1;
  44. }
  45. get_node_url(){
  46. if [ ! -f /bin/curl ];then
  47. if [ "${PM}" = "yum" ]; then
  48. yum install curl -y
  49. elif [ "${PM}" = "apt-get" ]; then
  50. apt-get install curl -y
  51. fi
  52. fi
  53. if [ -f "/www/node.pl" ];then
  54. download_Url=$(cat /www/node.pl)
  55. echo "Download node: $download_Url";
  56. echo '---------------------------------------------';
  57. return
  58. fi
  59. echo '---------------------------------------------';
  60. echo "Selected download node...";
  61. # nodes=(http://dg2.bt.cn http://dg1.bt.cn http://125.90.93.52:5880 http://36.133.1.8:5880 http://123.129.198.197 http://38.34.185.130 http://116.213.43.206:5880 http://128.1.164.196);
  62. nodes=(http://dg2.bt.cn http://dg1.bt.cn http://125.90.93.52:5880 http://36.133.1.8:5880 http://123.129.198.197 http://116.213.43.206:5880 http://128.1.164.196);
  63. tmp_file1=/dev/shm/net_test1.pl
  64. tmp_file2=/dev/shm/net_test2.pl
  65. [ -f "${tmp_file1}" ] && rm -f ${tmp_file1}
  66. [ -f "${tmp_file2}" ] && rm -f ${tmp_file2}
  67. touch $tmp_file1
  68. touch $tmp_file2
  69. for node in ${nodes[@]};
  70. do
  71. NODE_CHECK=$(curl --connect-timeout 3 -m 3 2>/dev/null -w "%{http_code} %{time_total}" ${node}/net_test|xargs)
  72. RES=$(echo ${NODE_CHECK}|awk '{print $1}')
  73. NODE_STATUS=$(echo ${NODE_CHECK}|awk '{print $2}')
  74. TIME_TOTAL=$(echo ${NODE_CHECK}|awk '{print $3 * 1000 - 500 }'|cut -d '.' -f 1)
  75. if [ "${NODE_STATUS}" == "200" ];then
  76. if [ $TIME_TOTAL -lt 100 ];then
  77. if [ $RES -ge 1500 ];then
  78. echo "$RES $node" >> $tmp_file1
  79. fi
  80. else
  81. if [ $RES -ge 1500 ];then
  82. echo "$TIME_TOTAL $node" >> $tmp_file2
  83. fi
  84. fi
  85. i=$(($i+1))
  86. if [ $TIME_TOTAL -lt 100 ];then
  87. if [ $RES -ge 3000 ];then
  88. break;
  89. fi
  90. fi
  91. fi
  92. done
  93. NODE_URL=$(cat $tmp_file1|sort -r -g -t " " -k 1|head -n 1|awk '{print $2}')
  94. if [ -z "$NODE_URL" ];then
  95. NODE_URL=$(cat $tmp_file2|sort -g -t " " -k 1|head -n 1|awk '{print $2}')
  96. if [ -z "$NODE_URL" ];then
  97. NODE_URL='http://download.bt.cn';
  98. fi
  99. fi
  100. rm -f $tmp_file1
  101. rm -f $tmp_file2
  102. download_Url=$NODE_URL
  103. echo "Download node: $download_Url";
  104. echo '---------------------------------------------';
  105. }
  106. Get_Versions(){
  107. redhat_version_file="/etc/redhat-release"
  108. deb_version_file="/etc/issue"
  109. if [ -f $redhat_version_file ];then
  110. os_type='el'
  111. is_aliyunos=$(cat $redhat_version_file|grep Aliyun)
  112. if [ "$is_aliyunos" != "" ];then
  113. return
  114. fi
  115. os_version=$(cat $redhat_version_file|grep CentOS|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '^[0-9]')
  116. if [ "${os_version}" = "5" ];then
  117. os_version=""
  118. fi
  119. if [ -z "${os_version}" ];then
  120. os_version=$(cat /etc/redhat-release |grep Stream|grep -oE 8)
  121. fi
  122. else
  123. os_type='ubuntu'
  124. os_version=$(cat $deb_version_file|grep Ubuntu|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '^[0-9]+')
  125. if [ "${os_version}" = "" ];then
  126. os_type='debian'
  127. os_version=$(cat $deb_version_file|grep Debian|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '[0-9]+')
  128. if [ "${os_version}" = "" ];then
  129. os_version=$(cat $deb_version_file|grep Debian|grep -Eo '[0-9]+')
  130. fi
  131. if [ "${os_version}" = "8" ];then
  132. os_version=""
  133. fi
  134. if [ "${is64bit}" = '32' ];then
  135. os_version=""
  136. fi
  137. else
  138. if [ "$os_version" = "14" ];then
  139. os_version=""
  140. fi
  141. if [ "$os_version" = "12" ];then
  142. os_version=""
  143. fi
  144. if [ "$os_version" = "19" ];then
  145. os_version=""
  146. fi
  147. if [ "$os_version" = "21" ];then
  148. os_version=""
  149. fi
  150. if [ "$os_version" = "20" ];then
  151. os_version2004=$(cat /etc/issue|grep 20.04)
  152. if [ -z "${os_version2004}" ];then
  153. os_version=""
  154. fi
  155. fi
  156. fi
  157. fi
  158. }
  159. Install_Python_Lib(){
  160. curl -Ss --connect-timeout 3 -m 60 $download_Url/install/pip_select.sh|bash
  161. pyenv_path="/www/server/bt-monitor"
  162. if [ -f $pyenv_path/pyenv/bin/python ];then
  163. is_ssl=$($python_bin -c "import ssl" 2>&1|grep cannot)
  164. $pyenv_path/pyenv/bin/python3.7 -V
  165. if [ $? -eq 0 ] && [ -z "${is_ssl}" ];then
  166. chmod -R 700 $pyenv_path/pyenv/bin
  167. is_package=$($python_bin -m psutil 2>&1|grep package)
  168. if [ "$is_package" = "" ];then
  169. wget -O $pyenv_path/pyenv/pip.txt $download_Url/install/pyenv/pip.txt -t 5 -T 10
  170. $pyenv_path/pyenv/bin/pip install -U pip
  171. $pyenv_path/pyenv/bin/pip install -U setuptools
  172. $pyenv_path/pyenv/bin/pip install -r $pyenv_path/pyenv/pip.txt
  173. $pyenv_path/pyenv/bin/pip install -U flask==2.2.0
  174. $pyenv_path/pyenv/bin/pip install flask_sock
  175. $pyenv_path/pyenv/bin/pip install cachelib
  176. $pyenv_path/pyenv/bin/pip install py7zr
  177. $pyenv_path/pyenv/bin/pip install backports.lzma
  178. fi
  179. source $pyenv_path/pyenv/bin/activate
  180. chmod -R 700 $pyenv_path/pyenv/bin
  181. return
  182. else
  183. rm -rf $pyenv_path/pyenv
  184. fi
  185. fi
  186. py_version="3.7.9"
  187. if [ ! -d "$pyenv_path" ]; then
  188. mkdir -p $pyenv_path
  189. fi
  190. echo "True" > /www/disk.pl
  191. if [ ! -w /www/disk.pl ];then
  192. Red_Error "ERROR: Install python env fielded." "ERROR: /www目录无法写入,请检查目录/用户/磁盘权限!"
  193. fi
  194. os_type='el'
  195. os_version='7'
  196. is_export_openssl=0
  197. Get_Versions
  198. echo "OS: $os_type - $os_version"
  199. is_aarch64=$(uname -a|grep aarch64)
  200. if [ "$is_aarch64" != "" ];then
  201. is64bit="aarch64"
  202. fi
  203. if [ -f "/www/server/bt-monitor/pymake.pl" ];then
  204. os_version=""
  205. rm -f /www/server/bt-monitor/pymake.pl
  206. fi
  207. if [[ $os_type =~ "debian" ]] || [[ $os_type =~ "ubuntu" ]]; then
  208. isbtm="-btm"
  209. fi
  210. if [ "${os_version}" != "" ];then
  211. pyenv_file="/www/pyenv.tar.gz"
  212. wget -O $pyenv_file $download_Url/install/pyenv/pyenv-${os_type}${os_version}-x${is64bit}${isbtm}.tar.gz -t 5 -T 10
  213. tmp_size=$(du -b $pyenv_file|awk '{print $1}')
  214. if [ $tmp_size -lt 703460 ];then
  215. rm -f $pyenv_file
  216. echo "ERROR: Download python env fielded."
  217. else
  218. echo "Install python env..."
  219. tar zxvf $pyenv_file -C $pyenv_path/ > /dev/null
  220. chmod -R 700 $pyenv_path/pyenv/bin
  221. rm -rf $pyenv_path/pyenv/bin/python
  222. ln -sf $pyenv_path/pyenv/bin/python3.7 $pyenv_path/pyenv/bin/python
  223. $pyenv_path/pyenv/bin/python -m pip install --upgrade --force-reinstall pip
  224. $pyenv_path/pyenv/bin/pip install -U flask==2.2.0
  225. $pyenv_path/pyenv/bin/pip install flask_sock
  226. $pyenv_path/pyenv/bin/pip install cachelib
  227. $pyenv_path/pyenv/bin/pip install py7zr
  228. $pyenv_path/pyenv/bin/pip install backports.lzma
  229. if [ ! -f $pyenv_path/pyenv/bin/python ];then
  230. rm -f $pyenv_file
  231. Red_Error "ERROR: Install python env fielded." "ERROR: 下载堡塔云监控运行环境失败,请尝试重新安装!"
  232. fi
  233. $pyenv_path/pyenv/bin/python3.7 -V
  234. if [ $? -eq 0 ];then
  235. rm -f $pyenv_file
  236. ln -sf $pyenv_path/pyenv/bin/pip3.7 /usr/bin/btmpip
  237. ln -sf $pyenv_path/pyenv/bin/python3.7 /usr/bin/btmpython
  238. source $pyenv_path/pyenv/bin/activate
  239. return
  240. else
  241. rm -f $pyenv_file
  242. rm -rf $pyenv_path/pyenv
  243. fi
  244. fi
  245. fi
  246. cd /www
  247. python_src='/www/python_src.tar.xz'
  248. python_src_path="/www/Python-${py_version}"
  249. wget -O $python_src $download_Url/src/Python-${py_version}.tar.xz -t 5 -T 10
  250. tmp_size=$(du -b $python_src|awk '{print $1}')
  251. if [ $tmp_size -lt 10703460 ];then
  252. rm -f $python_src
  253. Red_Error "ERROR: Download python source code fielded." "ERROR: 下载堡塔云监控运行环境失败,请尝试重新安装!"
  254. fi
  255. tar xvf $python_src
  256. rm -f $python_src
  257. cd $python_src_path
  258. ./configure --prefix=$pyenv_path/pyenv
  259. make -j$cpu_cpunt
  260. make install
  261. if [ ! -f $pyenv_path/pyenv/bin/python3.7 ];then
  262. rm -rf $python_src_path
  263. Red_Error "ERROR: Make python env fielded." "ERROR: 编译堡塔云监控运行环境失败!"
  264. fi
  265. cd ~
  266. rm -rf $python_src_path
  267. wget -O $pyenv_path/pyenv/bin/activate $download_Url/install/pyenv/activate.panel -t 5 -T 10
  268. wget -O $pyenv_path/pyenv/pip.txt $download_Url/install/pyenv/pip-3.7.8.txt -t 5 -T 10
  269. ln -sf $pyenv_path/pyenv/bin/pip3.7 $pyenv_path/pyenv/bin/pip
  270. ln -sf $pyenv_path/pyenv/bin/python3.7 $pyenv_path/pyenv/bin/python
  271. ln -sf $pyenv_path/pyenv/bin/pip3.7 /usr/bin/btmpip
  272. ln -sf $pyenv_path/pyenv/bin/python3.7 /usr/bin/btmpython
  273. chmod -R 700 $pyenv_path/pyenv/bin
  274. $pyenv_path/pyenv/bin/pip install -U pip
  275. $pyenv_path/pyenv/bin/pip install -U setuptools
  276. $pyenv_path/pyenv/bin/pip install -U wheel==0.34.2
  277. $pyenv_path/pyenv/bin/pip install -r $pyenv_path/pyenv/pip.txt
  278. $pyenv_path/pyenv/bin/pip install -U flask==2.2.0
  279. $pyenv_path/pyenv/bin/pip install flask_sock
  280. $pyenv_path/pyenv/bin/pip install cachelib
  281. $pyenv_path/pyenv/bin/pip install py7zr
  282. $pyenv_path/pyenv/bin/pip install backports.lzma
  283. source $pyenv_path/pyenv/bin/activate
  284. is_gevent=$($python_bin -m gevent 2>&1|grep -oE package)
  285. is_psutil=$($python_bin -m psutil 2>&1|grep -oE package)
  286. if [ "${is_gevent}" != "${is_psutil}" ];then
  287. Red_Error "ERROR: psutil/gevent install failed!"
  288. fi
  289. }
  290. Install_Monitor(){
  291. ulimit -n 1000001
  292. tee -a /etc/security/limits.conf << EOF
  293. * hard nofile 1000001
  294. * soft nofile 1000001
  295. root hard nofile 1000001
  296. root soft nofile 1000001
  297. EOF
  298. sysctl -p
  299. panelPort="806"
  300. if [ ! -d "/etc/init.d" ];then
  301. mkdir -p /etc/init.d
  302. fi
  303. if [ -f "/etc/init.d/btm" ]; then
  304. /etc/init.d/btm stop
  305. sleep 1
  306. fi
  307. if [ -f "/www/server/bt-monitor/sqlite-server.sh" ]; then
  308. chmod +x /www/server/bt-monitor/sqlite-server.sh
  309. /www/server/bt-monitor/sqlite-server.sh stop
  310. sleep 1
  311. fi
  312. version="1.0.2"
  313. file_name="bt-monitor"
  314. agent_src="bt-monitor.zip"
  315. cd ~
  316. version=`curl -sf ${Btapi_Url}/bt_monitor/latest_version |awk -F '\"version\"' '{print $2}'|awk -F ':' '{print $2}'|awk -F '"' '{print $2}'`
  317. if [ -z $version ]; then
  318. version="1.0.2"
  319. fi
  320. if [ "$re_install" == "1" ]; then
  321. new_dir="/www/server/new_btmonitor"
  322. if [ ! -d "$new_dir" ];then
  323. mkdir -p $new_dir
  324. fi
  325. wget -O $agent_src ${Btapi_Url}/install/src/$file_name-$version.zip -t 5 -T 10
  326. unzip -o $agent_src -d $new_dir/ > /dev/null
  327. if [ ! -f $new_dir/BT-MONITOR ];then
  328. ls -lh $agent_src
  329. Red_Error "ERROR: Failed to download, please try install again!" "ERROR: 下载堡塔云监控失败,请尝试重新安装!"
  330. fi
  331. rm -rf $new_dir/config
  332. rm -rf $new_dir/data
  333. rm -rf $new_dir/ssl
  334. \cp -r $new_dir/* $monitor_path/
  335. rm -rf $new_dir
  336. else
  337. wget -O $agent_src ${Btapi_Url}/install/src/$file_name-$version.zip -t 5 -T 10
  338. if [ ! -d "$monitor_path" ]; then
  339. mkdir -p $monitor_path
  340. fi
  341. unzip -o $agent_src -d $monitor_path/ > /dev/null
  342. if [ ! -f $run_bin ];then
  343. ls -lh $agent_src
  344. Red_Error "ERROR: Failed to download, please try install again!" "ERROR: 下载堡塔云监控失败,请尝试重新安装!"
  345. fi
  346. fi
  347. rm -rf $agent_src
  348. chmod +x $monitor_path/BT-MONITOR
  349. chmod +x $monitor_path/tools.py
  350. wget -O /etc/init.d/btm ${download_Url}/init/btmonitor.init -t 5 -T 10
  351. # \cp -r $monitor_path/init.sh /etc/init.d/btm
  352. chmod +x /etc/init.d/btm
  353. ln -sf /etc/init.d/btm /usr/bin/btm
  354. if [ ! -f /www/server/bt-monitor/data/user.json ]; then
  355. echo "{\"uid\":1,\"username\":\"Administrator\",\"ip\":\"127.0.0.1\",\"server_id\":\"1\",\"access_key\":\"test\",\"secret_key\":\"123456\"}" > /www/server/bt-monitor/data/user.json
  356. fi
  357. }
  358. Start_Monitor(){
  359. /etc/init.d/btm start
  360. if [ "$?" != "0" ]; then
  361. echo "堡塔云监控启动失败!"
  362. tail $monitor_path/logs/error.log
  363. exit 1
  364. fi
  365. echo "正在初始化云监控..."
  366. if [ "$re_install" == "1" ] || [ "$re_install" == "2" ]; then
  367. user_pass=`$setup_path/server/bt-monitor/tools.py reset_pwd`
  368. password=`echo $user_pass |awk '{print $3}'`
  369. else
  370. user_pass=`$monitor_path/tools.py create_admin`
  371. password=`echo $user_pass |awk -F " " '{print $5}'`
  372. for ((i=1; i<=5; i++));do
  373. if [ -z "$password" ]; then
  374. sleep 7
  375. user_pass=`$monitor_path/tools.py create_admin`
  376. password=`echo $user_pass |awk -F " " '{print $5}'`
  377. else
  378. i=5
  379. fi
  380. done
  381. fi
  382. c_path=$(cat /www/server/bt-monitor/config/config.json |awk -F '\"admin_path\"' '{print $2}'|awk -F ":" '{print $2}'|awk -F '"' '{print $2}')
  383. adminpath=$(echo $c_path|awk -F ',' '{print $1}')
  384. if [ -d "/usr/bin/btmonitoragent" ];then
  385. rm -rf /usr/bin/btmonitoragent
  386. fi
  387. date_f=`date '+%Y%m%d_%H%M%S'`
  388. md5_pl=`echo $date_f | md5sum | head -c 32`
  389. token_pl=`cat $monitor_path/config/token.pl 2>&1`
  390. if [ "$token_pl" == ' ' ] || [ ! -f $monitor_path/config/token.pl ]; then
  391. echo "$md5_pl" > $monitor_path/config/token.pl
  392. fi
  393. echo "正在给本机安装云监控被控端,请等待..."
  394. sleep 20
  395. curl -sSO ${download_Url}/install/btmonitoragent.sh && sh btmonitoragent.sh https://127.0.0.1:806 $md5_pl
  396. }
  397. Set_Firewall(){
  398. sshPort=$(cat /etc/ssh/sshd_config | grep 'Port '|awk '{print $2}')
  399. if [ "${PM}" = "apt-get" ]; then
  400. apt-get install -y ufw
  401. if [ -f "/usr/sbin/ufw" ];then
  402. ufw allow 22/tcp
  403. ufw allow ${panelPort}/tcp
  404. ufw allow ${sshPort}/tcp
  405. ufw_status=`ufw status`
  406. echo y|ufw enable
  407. ufw default deny
  408. ufw reload
  409. fi
  410. else
  411. if [ -f "/etc/init.d/iptables" ];then
  412. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  413. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ${panelPort} -j ACCEPT
  414. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ${sshPort} -j ACCEPT
  415. iptables -A INPUT -p icmp --icmp-type any -j ACCEPT
  416. iptables -A INPUT -s localhost -d localhost -j ACCEPT
  417. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  418. iptables -P INPUT DROP
  419. service iptables save
  420. sed -i "s#IPTABLES_MODULES=\"\"#IPTABLES_MODULES=\"ip_conntrack_netbios_ns ip_conntrack_ftp ip_nat_ftp\"#" /etc/sysconfig/iptables-config
  421. iptables_status=$(service iptables status | grep 'not running')
  422. if [ "${iptables_status}" == '' ];then
  423. service iptables restart
  424. fi
  425. else
  426. AliyunCheck=$(cat /etc/redhat-release|grep "Aliyun Linux")
  427. [ "${AliyunCheck}" ] && return
  428. yum install firewalld -y
  429. [ "${Centos8Check}" ] && yum reinstall python3-six -y
  430. systemctl enable firewalld
  431. systemctl start firewalld
  432. firewall-cmd --set-default-zone=public > /dev/null 2>&1
  433. firewall-cmd --permanent --zone=public --add-port=22/tcp > /dev/null 2>&1
  434. firewall-cmd --permanent --zone=public --add-port=${panelPort}/tcp > /dev/null 2>&1
  435. firewall-cmd --permanent --zone=public --add-port=${sshPort}/tcp > /dev/null 2>&1
  436. firewall-cmd --reload
  437. fi
  438. fi
  439. }
  440. Service_Add(){
  441. if [ $Command_Exists systemctl ]; then
  442. wget -O /usr/lib/systemd/system/btm.service ${download_Url}/init/systemd/btmonitor.service -t 5 -T 10
  443. systemctl daemon-reload
  444. systemctl enable btm
  445. else
  446. if [ "${PM}" == "yum" ] || [ "${PM}" == "dnf" ]; then
  447. chkconfig --add btm
  448. chkconfig --level 2345 btm on
  449. elif [ "${PM}" == "apt-get" ]; then
  450. update-rc.d btm defaults
  451. fi
  452. fi
  453. }
  454. Service_Del(){
  455. if [ $Command_Exists systemctl ]; then
  456. rm -rf /usr/lib/systemd/system/btm.service
  457. systemctl disable btm
  458. else
  459. if [ "${PM}" == "yum" ] || [ "${PM}" == "dnf" ]; then
  460. chkconfig --del btm
  461. chkconfig --level 2345 btm off
  462. elif [ "${PM}" == "apt-get" ]; then
  463. update-rc.d btm remove
  464. fi
  465. fi
  466. }
  467. Get_Ip_Address(){
  468. getIpAddress=""
  469. getIpAddress=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
  470. if [ -z "${getIpAddress}" ] || [ "${getIpAddress}" = "0.0.0.0" ]; then
  471. isHosts=$(cat /etc/hosts|grep 'www.bt.cn')
  472. if [ -z "${isHosts}" ];then
  473. echo "" >> /etc/hosts
  474. echo "116.213.43.206 www.bt.cn" >> /etc/hosts
  475. getIpAddress=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
  476. if [ -z "${getIpAddress}" ];then
  477. sed -i "/bt.cn/d" /etc/hosts
  478. fi
  479. fi
  480. fi
  481. ipv4Check=$($python_bin -c "import re; print(re.match('^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$','${getIpAddress}'))")
  482. if [ "${ipv4Check}" == "None" ];then
  483. ipv6Address=$(echo ${getIpAddress}|tr -d "[]")
  484. ipv6Check=$($python_bin -c "import re; print(re.match('^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$','${ipv6Address}'))")
  485. if [ "${ipv6Check}" == "None" ]; then
  486. getIpAddress="SERVER_IP"
  487. else
  488. echo "True" > ${setup_path}/server/bt-monitor/data/ipv6.pl
  489. sleep 1
  490. /etc/init.d/btm restart
  491. fi
  492. fi
  493. if [ "${getIpAddress}" != "SERVER_IP" ];then
  494. echo "${getIpAddress}" > ${setup_path}/server/bt-monitor/data/iplist.txt
  495. fi
  496. LOCAL_IP=$(ip addr | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -E -v "^127\.|^255\.|^0\." | head -n 1)
  497. }
  498. System_Check(){
  499. if [ -f "$monitor_path/BT-MONITOR" ] || [ -f "$monitor_path/tools.py" ] || [ -f "/etc/init.d/btm" ];then
  500. Install_Check
  501. elif [ -d "$old_dir" ];then
  502. Rev_Install_Check
  503. fi
  504. }
  505. Install_Check(){
  506. echo -e "----------------------------------------------------"
  507. echo -e "检测到已存在堡塔云监控系统,请按照选项选择安装方式!"
  508. echo -e "1) 覆盖安装:保存原有监控配置及数据并安装堡塔云监控"
  509. echo -e "\033[33m2) 全新安装:清空原有监控配置及数据并安装堡塔云监控\033[0m"
  510. echo -e "----------------------------------------------------"
  511. read -p "请输入对应选项[1|2]进行安装或输入任意内容退出安装: " yes;
  512. if [ "$yes" == "1" ]; then
  513. re_install="1"
  514. echo "即将卸载并重装本机的堡塔云监控agent端..."
  515. Uninstall_agent
  516. elif [ "$yes" == "2" ]; then
  517. Backup_Monitor
  518. echo "即将卸载并重装本机的堡塔云监控agent端..."
  519. Uninstall_agent
  520. else
  521. echo -e "------------"
  522. echo "取消安装"
  523. exit;
  524. fi
  525. }
  526. Rev_Install_Check(){
  527. echo -e "----------------------------------------------------"
  528. echo -e "\033[33m检测到上一次卸载云监控时保留的旧数据,请按照选项选择安装方式!\033[0m"
  529. echo -e "1) 还原以前的备份并安装堡塔云监控系统!"
  530. echo -e "2) 不使用原有备份,全新安装堡塔云监控系统!"
  531. echo -e "----------------------------------------------------"
  532. read -p "请输入对应选项[1|2]进行安装或输入任意内容退出安装: " yes;
  533. if [ "$yes" == "1" ]; then
  534. re_install="2"
  535. echo "开始安装堡塔云监控并还原数据..."
  536. elif [ "$yes" == "2" ]; then
  537. echo "开始全新安装堡塔云监控..."
  538. else
  539. echo -e "------------"
  540. echo "取消安装"
  541. exit;
  542. fi
  543. }
  544. Backup_Monitor(){
  545. if [ -f "/etc/init.d/btm" ]; then
  546. /etc/init.d/btm stop
  547. sleep 1
  548. fi
  549. if [ ! -d "$old_dir" ];then
  550. mkdir -p $old_dir
  551. fi
  552. mv $monitor_path/data $old_dir/data
  553. mv $monitor_path/config $old_dir/config
  554. mv $monitor_path/ssl $old_dir/ssl
  555. }
  556. Reinstall_Monitor(){
  557. rm -rf $monitor_path/data
  558. rm -rf $monitor_path/config
  559. rm -rf $monitor_path/ssl
  560. mv $old_dir/data $monitor_path/data
  561. mv $old_dir/config $monitor_path/config
  562. mv $old_dir/ssl $monitor_path/ssl
  563. rm -rf $old_dir
  564. }
  565. is64bit=$(getconf LONG_BIT)
  566. if [ "${is64bit}" != '64' ];then
  567. Red_Error "抱歉, 堡塔云监控不支持32位系统, 请使用64位系统!";
  568. fi
  569. Get_Pack_Manager(){
  570. if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
  571. PM="yum"
  572. elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
  573. PM="apt-get"
  574. fi
  575. }
  576. Install_RPM_Pack(){
  577. yumPacks="wget curl unzip gcc gcc-c++ make libcurl-devel openssl-devel xz-devel python-backports-lzma xz"
  578. yum install -y ${yumPacks}
  579. for yumPack in ${yumPacks}
  580. do
  581. rpmPack=$(rpm -q ${yumPack})
  582. packCheck=$(echo ${rpmPack}|grep not)
  583. if [ "${packCheck}" ]; then
  584. yum install ${yumPack} -y
  585. fi
  586. done
  587. }
  588. Install_Deb_Pack(){
  589. debPacks="wget curl unzip gcc g++ make libcurl4-openssl-dev libssl-dev liblzma-dev xz-utils libffi-dev libbz2-dev libsqlite3-dev libreadline-dev libgdbm-dev python3-bsddb3 tk-dev ncurses-dev uuid-dev";
  590. apt-get install -y $debPacks --force-yes
  591. for debPack in ${debPacks}
  592. do
  593. packCheck=$(dpkg -l ${debPack})
  594. if [ "$?" -ne "0" ] ;then
  595. apt-get install -y $debPack
  596. fi
  597. done
  598. }
  599. Install_Main(){
  600. startTime=`date +%s`
  601. System_Check
  602. Get_Pack_Manager
  603. get_node_url
  604. if [ $PM = "yum" ]; then
  605. Install_RPM_Pack
  606. else
  607. Install_Deb_Pack
  608. fi
  609. Install_Python_Lib
  610. Install_Monitor
  611. Set_Firewall
  612. Get_Ip_Address
  613. Service_Add
  614. if [ "$re_install" == "2" ]; then
  615. Reinstall_Monitor
  616. fi
  617. Start_Monitor
  618. }
  619. Uninstall_Monitor(){
  620. pkill BT-MONITOR
  621. /etc/init.d/btm stop
  622. if [ -f "/www/server/bt-monitor/sqlite-server.sh" ]; then
  623. chmod +x /www/server/bt-monitor/sqlite-server.sh
  624. /www/server/bt-monitor/sqlite-server.sh stop
  625. sleep 1
  626. fi
  627. Service_Del
  628. rm -rf $monitor_path
  629. rm -rf /usr/bin/btm
  630. rm -rf /etc/init.d/btm
  631. echo -e "堡塔云监控Server端卸载成功!"
  632. }
  633. Uninstall_agent(){
  634. get_node_url
  635. if [ -f "/tmp/btmonitoragent.sh" ];then
  636. rm -rf /tmp/btmonitoragent.sh
  637. fi
  638. curl -o /tmp/btmonitoragent.sh -sSO ${download_Url}/install/btmonitoragent.sh && bash /tmp/btmonitoragent.sh uninstall
  639. }
  640. action="${1}"
  641. if [ "$action" == "uninstall" ];then
  642. echo -e "----------------------------------------------------"
  643. echo -e "\033[33m检测到您正在卸载堡塔云监控系统,请按照选项选择卸载方式!\033[0m"
  644. echo -e "1) 备份数据后卸载:保存原有监控配置及数据并卸载堡塔云监控"
  645. echo -e "2) 完全卸载:清空原有监控配置及数据并卸载堡塔云监控"
  646. echo -e "----------------------------------------------------"
  647. read -p "请输入对应选项[1|2]进行卸载或输入任意内容退出卸载: " yes;
  648. if [ "$yes" == "1" ]; then
  649. Backup_Monitor
  650. echo -e "----------------------------------------------------"
  651. echo -e "\033[33m已备份原有监控数据至: ${old_dir}\033[0m"
  652. elif [ "$yes" == "2" ]; then
  653. echo "正在清空堡塔云监控数据..."
  654. else
  655. echo -e "------------"
  656. echo "取消卸载"
  657. exit;
  658. fi
  659. Uninstall_agent
  660. Uninstall_Monitor
  661. exit 0
  662. else
  663. echo "
  664. +----------------------------------------------------------------------
  665. | Bt-Monitor FOR CentOS/Ubuntu/Debian
  666. +----------------------------------------------------------------------
  667. | Copyright © 2015-2099 BT-SOFT(http://www.bt.cn) All rights reserved.
  668. +----------------------------------------------------------------------
  669. | The Monitor URL will be http://SERVER_IP:806 when installed.
  670. +----------------------------------------------------------------------
  671. "
  672. while [ "$go" != 'y' ] && [ "$go" != 'n' ]
  673. do
  674. read -p "Do you want to install Bt-Monitor to the $setup_path directory now?(y/n): " go;
  675. done
  676. if [ "$go" == 'n' ];then
  677. exit;
  678. fi
  679. Install_Main
  680. #curl -o /dev/null -fsSL --connect-time 10 "https://www.bt.cn/api/wpanel/SetupCountCloud?cloud_type=1&token=$md5_pl"
  681. fi
  682. echo -e "=================================================================="
  683. echo -e "\033[32m堡塔云监控安装完成! Installed successfully!\033[0m"
  684. echo -e "=================================================================="
  685. echo "外网访问地址: https://${getIpAddress}:${panelPort}${adminpath}"
  686. echo "内网访问地址: https://${LOCAL_IP}:${panelPort}${adminpath}"
  687. echo -e "username: admin"
  688. echo -e "password: $password"
  689. echo -e "\033[33mIf you cannot access the Monitor,\033[0m"
  690. echo -e "\033[33mrelease the following Monitor port [${panelPort}] in the security group\033[0m"
  691. echo -e "\033[33m若无法访问堡塔云监控,请检查防火墙/安全组是否有放行[${panelPort}]端口\033[0m"
  692. echo -e "=================================================================="
  693. endTime=`date +%s`
  694. ((outTime=($endTime-$startTime)/60))
  695. echo -e "Time consumed:\033[32m $outTime \033[0mMinute!"