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.

906 lines
30 KiB

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