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.

747 lines
24 KiB

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. version="1.0.2"
  308. file_name="bt-monitor"
  309. agent_src="bt-monitor.zip"
  310. cd ~
  311. version=`curl -sf ${Btapi_Url}/bt_monitor/latest_version |awk -F '\"version\"' '{print $2}'|awk -F ':' '{print $2}'|awk -F '"' '{print $2}'`
  312. if [ -z $version ]; then
  313. version="1.0.2"
  314. fi
  315. if [ "$re_install" == "1" ]; then
  316. new_dir="/www/server/new_btmonitor"
  317. if [ ! -d "$new_dir" ];then
  318. mkdir -p $new_dir
  319. fi
  320. wget -O $agent_src ${Btapi_Url}/install/src/$file_name-$version.zip -t 5 -T 10
  321. unzip -o $agent_src -d $new_dir/ > /dev/null
  322. if [ ! -f $new_dir/BT-MONITOR ];then
  323. ls -lh $agent_src
  324. Red_Error "ERROR: Failed to download, please try install again!" "ERROR: 下载堡塔云监控失败,请尝试重新安装!"
  325. fi
  326. rm -rf $new_dir/config
  327. rm -rf $new_dir/data
  328. rm -rf $new_dir/ssl
  329. \cp -r $new_dir/* $monitor_path/
  330. rm -rf $new_dir
  331. else
  332. wget -O $agent_src ${Btapi_Url}/install/src/$file_name-$version.zip -t 5 -T 10
  333. if [ ! -d "$monitor_path" ]; then
  334. mkdir -p $monitor_path
  335. fi
  336. unzip -o $agent_src -d $monitor_path/ > /dev/null
  337. if [ ! -f $run_bin ];then
  338. ls -lh $agent_src
  339. Red_Error "ERROR: Failed to download, please try install again!" "ERROR: 下载堡塔云监控失败,请尝试重新安装!"
  340. fi
  341. fi
  342. rm -rf $agent_src
  343. chmod +x $monitor_path/BT-MONITOR
  344. chmod +x $monitor_path/tools.py
  345. wget -O /etc/init.d/btm ${download_Url}/init/btmonitor.init -t 5 -T 10
  346. # \cp -r $monitor_path/init.sh /etc/init.d/btm
  347. chmod +x /etc/init.d/btm
  348. ln -sf /etc/init.d/btm /usr/bin/btm
  349. if [ ! -f /www/server/bt-monitor/data/user.json ]; then
  350. 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
  351. fi
  352. }
  353. Start_Monitor(){
  354. /etc/init.d/btm start
  355. if [ "$?" != "0" ]; then
  356. echo "堡塔云监控启动失败!"
  357. tail $monitor_path/logs/error.log
  358. exit 1
  359. fi
  360. echo "正在初始化云监控..."
  361. if [ "$re_install" == "1" ] || [ "$re_install" == "2" ]; then
  362. user_pass=`$setup_path/server/bt-monitor/tools.py reset_pwd`
  363. password=`echo $user_pass |awk '{print $3}'`
  364. else
  365. user_pass=`$monitor_path/tools.py create_admin`
  366. password=`echo $user_pass |awk -F " " '{print $5}'`
  367. for ((i=1; i<=5; i++));do
  368. if [ -z "$password" ]; then
  369. sleep 7
  370. user_pass=`$monitor_path/tools.py create_admin`
  371. password=`echo $user_pass |awk -F " " '{print $5}'`
  372. else
  373. i=5
  374. fi
  375. done
  376. fi
  377. c_path=$(cat /www/server/bt-monitor/config/config.json |awk -F '\"admin_path\"' '{print $2}'|awk -F ":" '{print $2}'|awk -F '"' '{print $2}')
  378. adminpath=$(echo $c_path|awk -F ',' '{print $1}')
  379. if [ -d "/usr/bin/btmonitoragent" ];then
  380. rm -rf /usr/bin/btmonitoragent
  381. fi
  382. date_f=`date '+%Y%m%d_%H%M%S'`
  383. md5_pl=`echo $date_f | md5sum | head -c 32`
  384. token_pl=`cat $monitor_path/config/token.pl 2>&1`
  385. if [ "$token_pl" == ' ' ] || [ ! -f $monitor_path/config/token.pl ]; then
  386. echo "$md5_pl" > $monitor_path/config/token.pl
  387. fi
  388. echo "正在给本机安装云监控被控端,请等待..."
  389. sleep 20
  390. curl -sSO ${download_Url}/install/btmonitoragent.sh && sh btmonitoragent.sh https://127.0.0.1:806 $md5_pl
  391. }
  392. Set_Firewall(){
  393. sshPort=$(cat /etc/ssh/sshd_config | grep 'Port '|awk '{print $2}')
  394. if [ "${PM}" = "apt-get" ]; then
  395. apt-get install -y ufw
  396. if [ -f "/usr/sbin/ufw" ];then
  397. ufw allow 22/tcp
  398. ufw allow ${panelPort}/tcp
  399. ufw allow ${sshPort}/tcp
  400. ufw_status=`ufw status`
  401. echo y|ufw enable
  402. ufw default deny
  403. ufw reload
  404. fi
  405. else
  406. if [ -f "/etc/init.d/iptables" ];then
  407. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  408. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ${panelPort} -j ACCEPT
  409. iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport ${sshPort} -j ACCEPT
  410. iptables -A INPUT -p icmp --icmp-type any -j ACCEPT
  411. iptables -A INPUT -s localhost -d localhost -j ACCEPT
  412. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  413. iptables -P INPUT DROP
  414. service iptables save
  415. sed -i "s#IPTABLES_MODULES=\"\"#IPTABLES_MODULES=\"ip_conntrack_netbios_ns ip_conntrack_ftp ip_nat_ftp\"#" /etc/sysconfig/iptables-config
  416. iptables_status=$(service iptables status | grep 'not running')
  417. if [ "${iptables_status}" == '' ];then
  418. service iptables restart
  419. fi
  420. else
  421. AliyunCheck=$(cat /etc/redhat-release|grep "Aliyun Linux")
  422. [ "${AliyunCheck}" ] && return
  423. yum install firewalld -y
  424. [ "${Centos8Check}" ] && yum reinstall python3-six -y
  425. systemctl enable firewalld
  426. systemctl start firewalld
  427. firewall-cmd --set-default-zone=public > /dev/null 2>&1
  428. firewall-cmd --permanent --zone=public --add-port=22/tcp > /dev/null 2>&1
  429. firewall-cmd --permanent --zone=public --add-port=${panelPort}/tcp > /dev/null 2>&1
  430. firewall-cmd --permanent --zone=public --add-port=${sshPort}/tcp > /dev/null 2>&1
  431. firewall-cmd --reload
  432. fi
  433. fi
  434. }
  435. Service_Add(){
  436. if [ $Command_Exists systemctl ]; then
  437. wget -O /usr/lib/systemd/system/btm.service ${download_Url}/init/systemd/btmonitor.service -t 5 -T 10
  438. systemctl daemon-reload
  439. systemctl enable btm
  440. else
  441. if [ "${PM}" == "yum" ] || [ "${PM}" == "dnf" ]; then
  442. chkconfig --add btm
  443. chkconfig --level 2345 btm on
  444. elif [ "${PM}" == "apt-get" ]; then
  445. update-rc.d btm defaults
  446. fi
  447. fi
  448. }
  449. Service_Del(){
  450. if [ $Command_Exists systemctl ]; then
  451. rm -rf /usr/lib/systemd/system/btm.service
  452. systemctl disable btm
  453. else
  454. if [ "${PM}" == "yum" ] || [ "${PM}" == "dnf" ]; then
  455. chkconfig --del btm
  456. chkconfig --level 2345 btm off
  457. elif [ "${PM}" == "apt-get" ]; then
  458. update-rc.d btm remove
  459. fi
  460. fi
  461. }
  462. Get_Ip_Address(){
  463. getIpAddress=""
  464. getIpAddress=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
  465. if [ -z "${getIpAddress}" ] || [ "${getIpAddress}" = "0.0.0.0" ]; then
  466. isHosts=$(cat /etc/hosts|grep 'www.bt.cn')
  467. if [ -z "${isHosts}" ];then
  468. echo "" >> /etc/hosts
  469. echo "116.213.43.206 www.bt.cn" >> /etc/hosts
  470. getIpAddress=$(curl -sS --connect-timeout 10 -m 60 https://www.bt.cn/Api/getIpAddress)
  471. if [ -z "${getIpAddress}" ];then
  472. sed -i "/bt.cn/d" /etc/hosts
  473. fi
  474. fi
  475. fi
  476. ipv4Check=$($python_bin -c "import re; print(re.match('^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$','${getIpAddress}'))")
  477. if [ "${ipv4Check}" == "None" ];then
  478. ipv6Address=$(echo ${getIpAddress}|tr -d "[]")
  479. ipv6Check=$($python_bin -c "import re; print(re.match('^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$','${ipv6Address}'))")
  480. if [ "${ipv6Check}" == "None" ]; then
  481. getIpAddress="SERVER_IP"
  482. else
  483. echo "True" > ${setup_path}/server/bt-monitor/data/ipv6.pl
  484. sleep 1
  485. /etc/init.d/btm restart
  486. fi
  487. fi
  488. if [ "${getIpAddress}" != "SERVER_IP" ];then
  489. echo "${getIpAddress}" > ${setup_path}/server/bt-monitor/data/iplist.txt
  490. fi
  491. 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)
  492. }
  493. System_Check(){
  494. if [ -f "$monitor_path/BT-MONITOR" ] || [ -f "$monitor_path/tools.py" ] || [ -f "/etc/init.d/btm" ];then
  495. Install_Check
  496. elif [ -d "$old_dir" ];then
  497. Rev_Install_Check
  498. fi
  499. }
  500. Install_Check(){
  501. echo -e "----------------------------------------------------"
  502. echo -e "检测到已存在堡塔云监控系统,请按照选项选择安装方式!"
  503. echo -e "1) 覆盖安装:保存原有监控配置及数据并安装堡塔云监控"
  504. echo -e "\033[33m2) 全新安装:清空原有监控配置及数据并安装堡塔云监控\033[0m"
  505. echo -e "----------------------------------------------------"
  506. read -p "请输入对应选项[1|2]进行安装或输入任意内容退出安装: " yes;
  507. if [ "$yes" == "1" ]; then
  508. re_install="1"
  509. echo "即将卸载并重装本机的堡塔云监控agent端..."
  510. Uninstall_agent
  511. elif [ "$yes" == "2" ]; then
  512. Backup_Monitor
  513. echo "即将卸载并重装本机的堡塔云监控agent端..."
  514. Uninstall_agent
  515. else
  516. echo -e "------------"
  517. echo "取消安装"
  518. exit;
  519. fi
  520. }
  521. Rev_Install_Check(){
  522. echo -e "----------------------------------------------------"
  523. echo -e "\033[33m检测到上一次卸载云监控时保留的旧数据,请按照选项选择安装方式!\033[0m"
  524. echo -e "1) 还原以前的备份并安装堡塔云监控系统!"
  525. echo -e "2) 不使用原有备份,全新安装堡塔云监控系统!"
  526. echo -e "----------------------------------------------------"
  527. read -p "请输入对应选项[1|2]进行安装或输入任意内容退出安装: " yes;
  528. if [ "$yes" == "1" ]; then
  529. re_install="2"
  530. echo "开始安装堡塔云监控并还原数据..."
  531. elif [ "$yes" == "2" ]; then
  532. echo "开始全新安装堡塔云监控..."
  533. else
  534. echo -e "------------"
  535. echo "取消安装"
  536. exit;
  537. fi
  538. }
  539. Backup_Monitor(){
  540. if [ -f "/etc/init.d/btm" ]; then
  541. /etc/init.d/btm stop
  542. sleep 1
  543. fi
  544. if [ ! -d "$old_dir" ];then
  545. mkdir -p $old_dir
  546. fi
  547. mv $monitor_path/data $old_dir/data
  548. mv $monitor_path/config $old_dir/config
  549. mv $monitor_path/ssl $old_dir/ssl
  550. }
  551. Reinstall_Monitor(){
  552. rm -rf $monitor_path/data
  553. rm -rf $monitor_path/config
  554. rm -rf $monitor_path/ssl
  555. mv $old_dir/data $monitor_path/data
  556. mv $old_dir/config $monitor_path/config
  557. mv $old_dir/ssl $monitor_path/ssl
  558. rm -rf $old_dir
  559. }
  560. is64bit=$(getconf LONG_BIT)
  561. if [ "${is64bit}" != '64' ];then
  562. Red_Error "抱歉, 堡塔云监控不支持32位系统, 请使用64位系统!";
  563. fi
  564. Get_Pack_Manager(){
  565. if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
  566. PM="yum"
  567. elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
  568. PM="apt-get"
  569. fi
  570. }
  571. Install_RPM_Pack(){
  572. yumPacks="wget curl unzip gcc gcc-c++ make libcurl-devel openssl-devel xz-devel python-backports-lzma xz"
  573. yum install -y ${yumPacks}
  574. for yumPack in ${yumPacks}
  575. do
  576. rpmPack=$(rpm -q ${yumPack})
  577. packCheck=$(echo ${rpmPack}|grep not)
  578. if [ "${packCheck}" ]; then
  579. yum install ${yumPack} -y
  580. fi
  581. done
  582. }
  583. Install_Deb_Pack(){
  584. 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";
  585. apt-get install -y $debPacks --force-yes
  586. for debPack in ${debPacks}
  587. do
  588. packCheck=$(dpkg -l ${debPack})
  589. if [ "$?" -ne "0" ] ;then
  590. apt-get install -y $debPack
  591. fi
  592. done
  593. }
  594. Install_Main(){
  595. startTime=`date +%s`
  596. System_Check
  597. Get_Pack_Manager
  598. get_node_url
  599. if [ $PM = "yum" ]; then
  600. Install_RPM_Pack
  601. else
  602. Install_Deb_Pack
  603. fi
  604. Install_Python_Lib
  605. Install_Monitor
  606. Set_Firewall
  607. Get_Ip_Address
  608. Service_Add
  609. if [ "$re_install" == "2" ]; then
  610. Reinstall_Monitor
  611. fi
  612. Start_Monitor
  613. }
  614. Uninstall_Monitor(){
  615. pkill BT-MONITOR
  616. /etc/init.d/btm stop
  617. Service_Del
  618. rm -rf $monitor_path
  619. rm -rf /usr/bin/btm
  620. rm -rf /etc/init.d/btm
  621. echo -e "堡塔云监控Server端卸载成功!"
  622. }
  623. Uninstall_agent(){
  624. get_node_url
  625. if [ -f "/tmp/btmonitoragent.sh" ];then
  626. rm -rf /tmp/btmonitoragent.sh
  627. fi
  628. curl -o /tmp/btmonitoragent.sh -sSO ${download_Url}/install/btmonitoragent.sh && bash /tmp/btmonitoragent.sh uninstall
  629. }
  630. action="${1}"
  631. if [ "$action" == "uninstall" ];then
  632. echo -e "----------------------------------------------------"
  633. echo -e "\033[33m检测到您正在卸载堡塔云监控系统,请按照选项选择卸载方式!\033[0m"
  634. echo -e "1) 备份数据后卸载:保存原有监控配置及数据并卸载堡塔云监控"
  635. echo -e "2) 完全卸载:清空原有监控配置及数据并卸载堡塔云监控"
  636. echo -e "----------------------------------------------------"
  637. read -p "请输入对应选项[1|2]进行卸载或输入任意内容退出卸载: " yes;
  638. if [ "$yes" == "1" ]; then
  639. Backup_Monitor
  640. echo -e "----------------------------------------------------"
  641. echo -e "\033[33m已备份原有监控数据至: ${old_dir}\033[0m"
  642. elif [ "$yes" == "2" ]; then
  643. echo "正在清空堡塔云监控数据..."
  644. else
  645. echo -e "------------"
  646. echo "取消卸载"
  647. exit;
  648. fi
  649. Uninstall_agent
  650. Uninstall_Monitor
  651. exit 0
  652. else
  653. echo "
  654. +----------------------------------------------------------------------
  655. | Bt-Monitor FOR CentOS/Ubuntu/Debian
  656. +----------------------------------------------------------------------
  657. | Copyright © 2015-2099 BT-SOFT(http://www.bt.cn) All rights reserved.
  658. +----------------------------------------------------------------------
  659. | The Monitor URL will be http://SERVER_IP:806 when installed.
  660. +----------------------------------------------------------------------
  661. "
  662. while [ "$go" != 'y' ] && [ "$go" != 'n' ]
  663. do
  664. read -p "Do you want to install Bt-Monitor to the $setup_path directory now?(y/n): " go;
  665. done
  666. if [ "$go" == 'n' ];then
  667. exit;
  668. fi
  669. Install_Main
  670. #curl -o /dev/null -fsSL --connect-time 10 "https://www.bt.cn/api/wpanel/SetupCountCloud?cloud_type=1&token=$md5_pl"
  671. fi
  672. echo -e "=================================================================="
  673. echo -e "\033[32m堡塔云监控安装完成! Installed successfully!\033[0m"
  674. echo -e "=================================================================="
  675. echo "外网访问地址: https://${getIpAddress}:${panelPort}${adminpath}"
  676. echo "内网访问地址: https://${LOCAL_IP}:${panelPort}${adminpath}"
  677. echo -e "username: admin"
  678. echo -e "password: $password"
  679. echo -e "\033[33mIf you cannot access the Monitor,\033[0m"
  680. echo -e "\033[33mrelease the following Monitor port [${panelPort}] in the security group\033[0m"
  681. echo -e "\033[33m若无法访问堡塔云监控,请检查防火墙/安全组是否有放行[${panelPort}]端口\033[0m"
  682. echo -e "=================================================================="
  683. endTime=`date +%s`
  684. ((outTime=($endTime-$startTime)/60))
  685. echo -e "Time consumed:\033[32m $outTime \033[0mMinute!"