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.

445 lines
13 KiB

1 year ago
  1. #!/bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  3. export PATH
  4. LANG=en_US.UTF-8
  5. Btapi_Url='http://www.example.com'
  6. monitor_path="/www/server/bt-monitor"
  7. run_bin="/www/server/bt-monitor/BT-MONITOR"
  8. is64bit=$(getconf LONG_BIT)
  9. if [ ! -d $monitor_path ]; then
  10. echo "没有安装云监控,请执行下面的命令安装堡塔云监控!"
  11. echo "curl -sSO ${Btapi_Url}/install/install_btmonitor.sh && bash install_btmonitor.sh"
  12. exit 1
  13. fi
  14. cd ~
  15. setup_path="/www"
  16. if [ -f "/etc/init.d/btm" ]; then
  17. /etc/init.d/btm stop
  18. sleep 1
  19. fi
  20. if [ -f "/www/server/bt-monitor/sqlite-server.sh" ]; then
  21. chmod +x /www/server/bt-monitor/sqlite-server.sh
  22. /www/server/bt-monitor/sqlite-server.sh stop
  23. sleep 1
  24. fi
  25. get_node_url(){
  26. if [ ! -f /bin/curl ];then
  27. if [ "${PM}" = "yum" ]; then
  28. yum install curl -y
  29. elif [ "${PM}" = "apt-get" ]; then
  30. apt-get install curl -y
  31. fi
  32. fi
  33. if [ -f "/www/node.pl" ];then
  34. download_Url=$(cat /www/node.pl)
  35. echo "Download node: $download_Url";
  36. echo '---------------------------------------------';
  37. return
  38. fi
  39. echo '---------------------------------------------';
  40. echo "Selected download node...";
  41. # 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);
  42. #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);
  43. nodes=(https://dg2.bt.cn https://dg1.bt.cn https://download.bt.cn);
  44. tmp_file1=/dev/shm/net_test1.pl
  45. tmp_file2=/dev/shm/net_test2.pl
  46. [ -f "${tmp_file1}" ] && rm -f ${tmp_file1}
  47. [ -f "${tmp_file2}" ] && rm -f ${tmp_file2}
  48. touch $tmp_file1
  49. touch $tmp_file2
  50. for node in ${nodes[@]};
  51. do
  52. NODE_CHECK=$(curl --connect-timeout 3 -m 3 2>/dev/null -w "%{http_code} %{time_total}" ${node}/net_test|xargs)
  53. RES=$(echo ${NODE_CHECK}|awk '{print $1}')
  54. NODE_STATUS=$(echo ${NODE_CHECK}|awk '{print $2}')
  55. TIME_TOTAL=$(echo ${NODE_CHECK}|awk '{print $3 * 1000 - 500 }'|cut -d '.' -f 1)
  56. if [ "${NODE_STATUS}" == "200" ];then
  57. if [ $TIME_TOTAL -lt 100 ];then
  58. if [ $RES -ge 1500 ];then
  59. echo "$RES $node" >> $tmp_file1
  60. fi
  61. else
  62. if [ $RES -ge 1500 ];then
  63. echo "$TIME_TOTAL $node" >> $tmp_file2
  64. fi
  65. fi
  66. i=$(($i+1))
  67. if [ $TIME_TOTAL -lt 100 ];then
  68. if [ $RES -ge 3000 ];then
  69. break;
  70. fi
  71. fi
  72. fi
  73. done
  74. NODE_URL=$(cat $tmp_file1|sort -r -g -t " " -k 1|head -n 1|awk '{print $2}')
  75. if [ -z "$NODE_URL" ];then
  76. NODE_URL=$(cat $tmp_file2|sort -g -t " " -k 1|head -n 1|awk '{print $2}')
  77. if [ -z "$NODE_URL" ];then
  78. NODE_URL='https://download.bt.cn';
  79. fi
  80. fi
  81. rm -f $tmp_file1
  82. rm -f $tmp_file2
  83. download_Url=$NODE_URL
  84. echo "Download node: $download_Url";
  85. echo '---------------------------------------------';
  86. }
  87. Get_Versions(){
  88. redhat_version_file="/etc/redhat-release"
  89. deb_version_file="/etc/issue"
  90. if [ -f $redhat_version_file ];then
  91. os_type='el'
  92. is_aliyunos=$(cat $redhat_version_file|grep Aliyun)
  93. if [ "$is_aliyunos" != "" ];then
  94. return
  95. fi
  96. os_version=$(cat $redhat_version_file|grep CentOS|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '^[0-9]')
  97. if [ "${os_version}" = "5" ];then
  98. os_version=""
  99. fi
  100. if [ -z "${os_version}" ];then
  101. os_version=$(cat /etc/redhat-release |grep Stream|grep -oE 8)
  102. fi
  103. else
  104. os_type='ubuntu'
  105. os_version=$(cat $deb_version_file|grep Ubuntu|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '^[0-9]+')
  106. if [ "${os_version}" = "" ];then
  107. os_type='debian'
  108. os_version=$(cat $deb_version_file|grep Debian|grep -Eo '([0-9]+\.)+[0-9]+'|grep -Eo '[0-9]+')
  109. if [ "${os_version}" = "" ];then
  110. os_version=$(cat $deb_version_file|grep Debian|grep -Eo '[0-9]+')
  111. fi
  112. if [ "${os_version}" = "8" ];then
  113. os_version=""
  114. fi
  115. if [ "${is64bit}" = '32' ];then
  116. os_version=""
  117. fi
  118. else
  119. if [ "$os_version" = "14" ];then
  120. os_version=""
  121. fi
  122. if [ "$os_version" = "12" ];then
  123. os_version=""
  124. fi
  125. if [ "$os_version" = "19" ];then
  126. os_version=""
  127. fi
  128. if [ "$os_version" = "21" ];then
  129. os_version=""
  130. fi
  131. if [ "$os_version" = "20" ];then
  132. os_version2004=$(cat /etc/issue|grep 20.04)
  133. if [ -z "${os_version2004}" ];then
  134. os_version=""
  135. fi
  136. fi
  137. fi
  138. fi
  139. }
  140. Install_Python_Lib(){
  141. curl -Ss --connect-timeout 3 -m 60 $download_Url/install/pip_select.sh|bash
  142. pyenv_path="/www/server/bt-monitor"
  143. if [ -f $pyenv_path/pyenv/bin/python ];then
  144. is_ssl=$($python_bin -c "import ssl" 2>&1|grep cannot)
  145. $pyenv_path/pyenv/bin/python3.7 -V
  146. if [ $? -eq 0 ] && [ -z "${is_ssl}" ];then
  147. chmod -R 700 $pyenv_path/pyenv/bin
  148. is_package=$($python_bin -m psutil 2>&1|grep package)
  149. if [ "$is_package" = "" ];then
  150. wget -O $pyenv_path/pyenv/pip.txt $download_Url/install/pyenv/pip.txt -t 5 -T 10
  151. $pyenv_path/pyenv/bin/pip install -U pip
  152. $pyenv_path/pyenv/bin/pip install -U setuptools
  153. $pyenv_path/pyenv/bin/pip install -r $pyenv_path/pyenv/pip.txt
  154. $pyenv_path/pyenv/bin/pip install -U flask==2.2.0
  155. $pyenv_path/pyenv/bin/pip install flask_sock
  156. $pyenv_path/pyenv/bin/pip install cachelib
  157. $pyenv_path/pyenv/bin/pip install py7zr
  158. $pyenv_path/pyenv/bin/pip install backports.lzma
  159. fi
  160. source $pyenv_path/pyenv/bin/activate
  161. chmod -R 700 $pyenv_path/pyenv/bin
  162. return
  163. else
  164. rm -rf $pyenv_path/pyenv
  165. fi
  166. fi
  167. py_version="3.7.9"
  168. if [ ! -d "$pyenv_path" ]; then
  169. mkdir -p $pyenv_path
  170. fi
  171. echo "True" > /www/disk.pl
  172. if [ ! -w /www/disk.pl ];then
  173. Red_Error "ERROR: Install python env fielded." "ERROR: /www目录无法写入,请检查目录/用户/磁盘权限!"
  174. fi
  175. os_type='el'
  176. os_version='7'
  177. is_export_openssl=0
  178. Get_Versions
  179. echo "OS: $os_type - $os_version"
  180. is_aarch64=$(uname -a|grep aarch64)
  181. if [ "$is_aarch64" != "" ];then
  182. is64bit="aarch64"
  183. fi
  184. if [ -f "/www/server/bt-monitor/pymake.pl" ];then
  185. os_version=""
  186. rm -f /www/server/bt-monitor/pymake.pl
  187. fi
  188. if [[ $os_type =~ "debian" ]] || [[ $os_type =~ "ubuntu" ]]; then
  189. isbtm="-btm"
  190. fi
  191. if [ "${os_version}" != "" ];then
  192. pyenv_file="/www/pyenv.tar.gz"
  193. wget -O $pyenv_file $download_Url/install/pyenv/pyenv-${os_type}${os_version}-x${is64bit}${isbtm}.tar.gz -t 5 -T 10
  194. tmp_size=$(du -b $pyenv_file|awk '{print $1}')
  195. if [ $tmp_size -lt 703460 ];then
  196. rm -f $pyenv_file
  197. echo "ERROR: Download python env fielded."
  198. else
  199. echo "Install python env..."
  200. tar zxvf $pyenv_file -C $pyenv_path/ > /dev/null
  201. chmod -R 700 $pyenv_path/pyenv/bin
  202. rm -rf $pyenv_path/pyenv/bin/python
  203. ln -sf $pyenv_path/pyenv/bin/python3.7 $pyenv_path/pyenv/bin/python
  204. $pyenv_path/pyenv/bin/python -m pip install --upgrade --force-reinstall pip
  205. $pyenv_path/pyenv/bin/pip install -U flask==2.2.0
  206. $pyenv_path/pyenv/bin/pip install flask_sock
  207. $pyenv_path/pyenv/bin/pip install cachelib
  208. $pyenv_path/pyenv/bin/pip install py7zr
  209. $pyenv_path/pyenv/bin/pip install backports.lzma
  210. if [ ! -f $pyenv_path/pyenv/bin/python ];then
  211. rm -f $pyenv_file
  212. Red_Error "ERROR: Install python env fielded." "ERROR: 下载堡塔云监控运行环境失败,请尝试重新安装!"
  213. fi
  214. $pyenv_path/pyenv/bin/python3.7 -V
  215. if [ $? -eq 0 ];then
  216. rm -f $pyenv_file
  217. ln -sf $pyenv_path/pyenv/bin/pip3.7 /usr/bin/btmpip
  218. ln -sf $pyenv_path/pyenv/bin/python3.7 /usr/bin/btmpython
  219. source $pyenv_path/pyenv/bin/activate
  220. return
  221. else
  222. rm -f $pyenv_file
  223. rm -rf $pyenv_path/pyenv
  224. fi
  225. fi
  226. fi
  227. cd /www
  228. python_src='/www/python_src.tar.xz'
  229. python_src_path="/www/Python-${py_version}"
  230. wget -O $python_src $download_Url/src/Python-${py_version}.tar.xz -t 5 -T 10
  231. tmp_size=$(du -b $python_src|awk '{print $1}')
  232. if [ $tmp_size -lt 10703460 ];then
  233. rm -f $python_src
  234. Red_Error "ERROR: Download python source code fielded." "ERROR: 下载堡塔云监控运行环境失败,请尝试重新安装!"
  235. fi
  236. tar xvf $python_src
  237. rm -f $python_src
  238. cd $python_src_path
  239. ./configure --prefix=$pyenv_path/pyenv
  240. make -j$cpu_cpunt
  241. make install
  242. if [ ! -f $pyenv_path/pyenv/bin/python3.7 ];then
  243. rm -rf $python_src_path
  244. Red_Error "ERROR: Make python env fielded." "ERROR: 编译堡塔云监控运行环境失败!"
  245. fi
  246. cd ~
  247. rm -rf $python_src_path
  248. wget -O $pyenv_path/pyenv/bin/activate $download_Url/install/pyenv/activate.panel -t 5 -T 10
  249. wget -O $pyenv_path/pyenv/pip.txt $download_Url/install/pyenv/pip-3.7.8.txt -t 5 -T 10
  250. ln -sf $pyenv_path/pyenv/bin/pip3.7 $pyenv_path/pyenv/bin/pip
  251. ln -sf $pyenv_path/pyenv/bin/python3.7 $pyenv_path/pyenv/bin/python
  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. chmod -R 700 $pyenv_path/pyenv/bin
  255. $pyenv_path/pyenv/bin/pip install -U pip
  256. $pyenv_path/pyenv/bin/pip install -U setuptools
  257. $pyenv_path/pyenv/bin/pip install -U wheel==0.34.2
  258. $pyenv_path/pyenv/bin/pip install -r $pyenv_path/pyenv/pip.txt
  259. $pyenv_path/pyenv/bin/pip install -U flask==2.2.0
  260. $pyenv_path/pyenv/bin/pip install flask_sock
  261. $pyenv_path/pyenv/bin/pip install cachelib
  262. $pyenv_path/pyenv/bin/pip install py7zr
  263. $pyenv_path/pyenv/bin/pip install backports.lzma
  264. source $pyenv_path/pyenv/bin/activate
  265. is_gevent=$($python_bin -m gevent 2>&1|grep -oE package)
  266. is_psutil=$($python_bin -m psutil 2>&1|grep -oE package)
  267. if [ "${is_gevent}" != "${is_psutil}" ];then
  268. Red_Error "ERROR: psutil/gevent install failed!"
  269. fi
  270. }
  271. Install_Monitor(){
  272. version="1.0.2"
  273. file_name="bt-monitor"
  274. agent_src="bt-monitor.zip"
  275. cd ~
  276. version=`curl -sf ${Btapi_Url}/bt_monitor/latest_version |awk -F '\"version\"' '{print $2}'|awk -F ':' '{print $2}'|awk -F '"' '{print $2}'`
  277. if [ -z $version ]; then
  278. version="1.0.2"
  279. fi
  280. new_dir="/www/server/new_btmonitor"
  281. if [ ! -d "$new_dir" ];then
  282. mkdir -p $new_dir
  283. fi
  284. if [ ! -z "$action" ]; then
  285. # 例如:sh update_btmonitor.sh /root/demo.zip
  286. if [[ "$action" =~ "zip" ]]; then
  287. version="指定版本"
  288. unzip -o $action -d $new_dir/
  289. else
  290. wget -O $agent_src ${Btapi_Url}/install/src/$file_name-$version.zip -t 5 -T 10
  291. unzip -o $agent_src -d $new_dir/ > /dev/null
  292. fi
  293. else
  294. wget -O $agent_src ${Btapi_Url}/install/src/$file_name-$version.zip -t 5 -T 10
  295. unzip -o $agent_src -d $new_dir/ > /dev/null
  296. fi
  297. if [ ! -f $new_dir/BT-MONITOR ];then
  298. ls -lh $agent_src
  299. Red_Error "ERROR: Failed to download, please try install again!" "ERROR: 下载堡塔云监控失败,请尝试重新安装!"
  300. fi
  301. rm -rf $new_dir/config
  302. rm -rf $new_dir/data
  303. rm -rf $new_dir/ssl
  304. \cp -r $new_dir/* $monitor_path/
  305. rm -rf $agent_src
  306. rm -rf $new_dir
  307. chmod +x $monitor_path/BT-MONITOR
  308. chmod +x $monitor_path/tools.py
  309. wget -O /etc/init.d/btm ${download_Url}/init/btmonitor.init -t 5 -T 10
  310. chmod +x /etc/init.d/btm
  311. ln -sf /etc/init.d/btm /usr/bin/btm
  312. if [ ! -f $monitor_path/data/user.json ]; then
  313. echo "{\"uid\":1,\"username\":\"Administrator\",\"ip\":\"127.0.0.1\",\"server_id\":\"1\",\"access_key\":\"test\",\"secret_key\":\"123456\"}" > $monitor_path/data/user.json
  314. fi
  315. if [ -f $monitor_path/core/include/c_loader/PluginLoader.so ]; then
  316. rm -f $monitor_path/core/include/c_loader/PluginLoader.so
  317. fi
  318. }
  319. Service_Add(){
  320. if [ $Command_Exists systemctl ]; then
  321. wget -O /usr/lib/systemd/system/btm.service ${download_Url}/init/systemd/btmonitor.service -t 5 -T 10
  322. systemctl daemon-reload
  323. systemctl enable btm
  324. else
  325. if [ "${PM}" == "yum" ] || [ "${PM}" == "dnf" ]; then
  326. chkconfig --add btm
  327. chkconfig --level 2345 btm on
  328. elif [ "${PM}" == "apt-get" ]; then
  329. update-rc.d btm defaults
  330. fi
  331. fi
  332. }
  333. Start_Monitor(){
  334. /etc/init.d/btm start
  335. if [ "$?" != "0" ]; then
  336. echo "堡塔云监控启动失败!"
  337. tail $monitor_path/logs/error.log
  338. exit 1
  339. fi
  340. echo "已成功升级到[$version]${Ver}";
  341. }
  342. GetSysInfo(){
  343. if [ -s "/etc/redhat-release" ];then
  344. SYS_VERSION=$(cat /etc/redhat-release)
  345. elif [ -s "/etc/issue" ]; then
  346. SYS_VERSION=$(cat /etc/issue)
  347. fi
  348. SYS_INFO=$(uname -a)
  349. SYS_BIT=$(getconf LONG_BIT)
  350. MEM_TOTAL=$(free -m|grep Mem|awk '{print $2}')
  351. CPU_INFO=$(getconf _NPROCESSORS_ONLN)
  352. echo -e ${SYS_VERSION}
  353. echo -e Bit:${SYS_BIT} Mem:${MEM_TOTAL}M Core:${CPU_INFO}
  354. echo -e ${SYS_INFO}
  355. echo -e "请截图以上报错信息发帖至论坛www.bt.cn/bbs求助"
  356. }
  357. Red_Error(){
  358. echo '=================================================';
  359. printf '\033[1;31;40m%b\033[0m\n' "$@";
  360. GetSysInfo
  361. exit 1;
  362. }
  363. Install_RPM_Pack(){
  364. yumPacks="wget curl unzip gcc gcc-c++ make libcurl-devel openssl-devel xz-devel python-backports-lzma xz"
  365. yum install -y ${yumPacks}
  366. for yumPack in ${yumPacks}
  367. do
  368. rpmPack=$(rpm -q ${yumPack})
  369. packCheck=$(echo ${rpmPack}|grep not)
  370. if [ "${packCheck}" ]; then
  371. yum install ${yumPack} -y
  372. fi
  373. done
  374. }
  375. Install_Deb_Pack(){
  376. apt-get update -y
  377. 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";
  378. apt-get install -y $debPacks --force-yes
  379. for debPack in ${debPacks}
  380. do
  381. packCheck=$(dpkg -l ${debPack})
  382. if [ "$?" -ne "0" ] ;then
  383. apt-get install -y $debPack
  384. fi
  385. done
  386. }
  387. Get_Pack_Manager(){
  388. if [ -f "/usr/bin/yum" ] && [ -d "/etc/yum.repos.d" ]; then
  389. PM="yum"
  390. elif [ -f "/usr/bin/apt-get" ] && [ -f "/usr/bin/dpkg" ]; then
  391. PM="apt-get"
  392. fi
  393. }
  394. Update_Monitor(){
  395. Get_Pack_Manager
  396. get_node_url
  397. if [ $PM = "yum" ]; then
  398. Install_RPM_Pack
  399. else
  400. Install_Deb_Pack
  401. fi
  402. if [ "$action" == "update_py" ]; then
  403. Install_Python_Lib
  404. fi
  405. Install_Monitor
  406. Service_Add
  407. Start_Monitor
  408. }
  409. action=${1}
  410. Update_Monitor