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.

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