#!/usr/bin/env bash # # Description: Choose a faster mirror for Linux script # # Copyright (C) 2021 liveJQ # RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' SKYBLUE='\033[0;36m' PLAIN='\033[0m' target_dir="/opt/ysnet" origin="https://gitee.com/livejq/pingm/repository/archive/master.zip" monitorSvr="http://116.31.96.74:28080/monitorSvr/a?login" selectAction() { echo "" echo -e "${GREEN} ========================================================= ${PLAIN}" echo -e "${GREEN} \ 1.安装SmokePing / ${PLAIN}" echo -e "${GREEN} \ 2.安装Agent / ${PLAIN}" echo -e "${GREEN} \ 直接回车一起安装 / ${PLAIN}" echo -e "${GREEN} ========================================================= ${PLAIN}" echo -e "${YELLOW} System requirements: ${PLAIN}${RED}CentOS 7${PLAIN}${YELLOW} or ${PLAIN}${RED}Ubuntu 20.04${PLAIN}${YELLOW}. ${PLAIN}" echo -e "${YELLOW} Version: ${PLAIN}${GREEN}1.1.0${PLAIN}${YELLOW} (13 Jan. 2022) ${PLAIN}" echo -e "${YELLOW} Copyright (C) 2021 liveJQ cloud@livejq.fun ${PLAIN}" echo "" read -rp "请输入数字选择:" num case $num in 1) before install_SMP after ;; 2) before install_Agent after ;; *) before install_SMP install_Agent after ;; esac } updateUb() { cp /etc/apt/sources.list /etc/apt/sources.list.old cat << EOF > /etc/apt/sources.list deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse EOF } updateCe() { mv /etc/yum.repos.d/* /tmp/ curl -o /etc/yum.repos.d/centos7.repo http://mirrors.aliyun.com/repo/Centos-7.repo yum -y install epel-release yum clean all && yum repolist } before() { source /etc/os-release || source /usr/lib/os-release || exit 1 if [[ $ID == "centos" ]]; then PM="yum" INS="yum install" updateCe $PM update $INS wget curl unzip -y wget $origin unzip master.zip elif [[ $ID == "debian" || $ID == "ubuntu" ]]; then PM="apt" INS="apt install" updateUb $PM update $INS wget curl unzip -y wget $origin unzip master.zip else exit 1 fi } after() { rm -f master.zip rm -fr pingm-master >/root/.bash_history } install_SMP() { rpm_packages="smokeping wqy-zenhei-fonts.noarch" apt_packages="smokeping echoping traceroute" read -rp "输入监控页面端口(默认20000):" port1 ss -tnlp | grep -q ":${port1:-20000} " && echo "端口 ${port1:-20000} 已被占用" && exit 1 if [[ $ID == "debian" || $ID == "ubuntu" ]]; then $INS $apt_packages -y || error=1 [[ $error -eq 1 ]] && echo "安装 smokeping 失败" && exit 1 systemctl stop apache2 && systemctl stop smokeping configureUb elif [[ $ID == "centos" ]]; then sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config setenforce 0 $INS $rpm_packages -y || error=1 [[ $error -eq 1 ]] && echo "安装 smokeping 失败" && exit 1 systemctl stop httpd && systemctl stop smokeping configureCe fi } install_Agent() { rpm_packages="mtr java-1.8.0-openjdk*" apt_packages="mtr openjdk-8-jdk" if [[ $ID == "debian" || $ID == "ubuntu" ]]; then $INS $apt_packages -y || error=1 [[ $error -eq 1 ]] && echo "安装 Agent 客户端依赖包失败" && exit 1 configureUbAgent elif [[ $ID == "centos" ]]; then sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config setenforce 0 $INS $rpm_packages -y || error=1 [[ $error -eq 1 ]] && echo "安装 Agent 客户端依赖包失败" && exit 1 configureCeAgent fi } configureUb() { #get local ip ip=$(curl -sL https://api64.ipify.org -4) || error=1 [[ $error -eq 1 ]] && echo "获取本机 IP 地址失败" && exit 1 #clean local config mkdir -p $target_dir mkdir -p /var/run/smokeping rm -fr /var/lib/smokeping/*/*.rrd rm -fr /etc/smokeping/config.d #get Gitee config mv pingm-master/SMP/Ubuntu20 pingm-master/SMP/config.d mv pingm-master/SMP/config.d /etc/smokeping mv /etc/smokeping/config.d/HW-HK /opt/ysnet sed -i 's/some.url/'$ip':'${port1:-20000}'/g;' /etc/smokeping/config.d/General sed -i 's/smokeping.cgi/smokeping/g;' /etc/smokeping/config.d/General #apache2 sed -i 's/80/'${port1:-20000}'/g' /etc/apache2/ports.conf sed -i 's/DirectoryIndex/DirectoryIndex smokeping.cgi/g' /etc/apache2/mods-available/dir.conf systemctl enable apache2 smokeping #TCPPing #wget -O /usr/bin/tcpping https://raw.githubusercontent.com/deajan/tcpping/master/tcpping #chmod 755 /usr/bin/tcpping chmod 777 -R /var/lib/smokeping/ #ufw ufw allow 22/tcp && ufw allow ${port1:-20000}/tcp ufw enable && systemctl enable ufw ufw reload #timezone timedatectl set-timezone Asia/Shanghai #test smokeping --debug || error=1 [[ $error -eq 1 ]] && echo "测试运行失败!" && exit 1 systemctl start apache2 smokeping || error=1 [[ $error -eq 1 ]] && echo "启动失败" && exit 1 echo "" echo "Smokeping安装完成,监控页面:http://$ip:${port1:-20000}/smokeping/sm.cgi" echo "" echo "注意:" echo "如有必要请在防火墙放行 ${port1:-20000} 端口" echo "请等待一会,监控数据不会立即更新" echo "" } configureUbAgent() { mkdir -p /var/monitor_agent #get Gitee config mv pingm-master/Agent/agentMain.jar /var/monitor_agent mv pingm-master/Agent/conf /var/monitor_agent mv pingm-master/Agent/AgentRestart.sh /root chmod 755 -R /var/monitor_agent #ufw ufw allow 22/tcp && ufw allow 9123/tcp ufw enable && systemctl enable ufw ufw reload #Running chmod 755 /root/AgentRestart.sh /root/AgentRestart.sh echo "" echo "Agent安装完成,请到监控页面添加Agent和下发策略:$monitorSvr" echo "" echo "注意:" echo "如有必要请在防火墙放行 9123 端口" echo "" } configureCe() { #get local ip ip=$(curl -sL https://api64.ipify.org -4) || error=1 [[ $error -eq 1 ]] && echo "获取本机 IP 地址失败" && exit 1 #clean local config mkdir -p $target_dir rm -fr /var/lib/smokeping/*/*.rrd rm -fr /etc/smokeping/config.d rm -fr /etc/smokeping/config #get Gitee config mv pingm-master/SMP/CentOS7 pingm-master/SMP/config.d mv pingm-master/SMP/config.d /etc/smokeping mv /etc/smokeping/config.d/HW-HK /opt/ysnet mv /etc/smokeping/config.d/config /etc/smokeping sed -i 's/localhost/'$ip':'${port1:-20000}'/g;' /etc/smokeping/config.d/General #httpd sed -i 's/Require local/Require all granted/g;' /etc/httpd/conf.d/smokeping.conf sed -i 's/Listen 80/Listen '${port1:-20000}'/g' /etc/httpd/conf/httpd.conf systemctl enable httpd smokeping #TCPPing #wget -O /usr/bin/tcpping https://raw.githubusercontent.com/deajan/tcpping/master/tcpping #chmod 755 /usr/bin/tcpping chmod 777 -R /var/lib/smokeping/ #firewalld systemctl restart firewalld && systemctl enable firewalld firewall-cmd --zone=public --add-port=${port1:-20000}/tcp --permanent && firewall-cmd --reload #timezone timedatectl set-timezone Asia/Shanghai #test smokeping --debug || error=1 [[ $error -eq 1 ]] && echo "测试运行失败!" && exit 1 systemctl start httpd smokeping || error=1 [[ $error -eq 1 ]] && echo "启动失败" && exit 1 echo "" echo "Smokeping安装完成,监控页面:http://$ip:${port1:-20000}/smokeping/sm.cgi" echo "" echo "注意:" echo "如有必要请在防火墙放行 ${port1:-20000} 端口" echo "请等待一会,监控数据不会立即更新" echo "" } configureCeAgent() { mkdir -p /var/monitor_agent #get Gitee config mv pingm-master/Agent/agentMain.jar /var/monitor_agent mv pingm-master/Agent/conf /var/monitor_agent mv pingm-master/Agent/AgentRestart.sh /root chmod 755 -R /var/monitor_agent #firewall systemctl restart firewalld firewall-cmd --zone=public --add-port=9123/tcp --permanent && firewall-cmd --reload #Running chmod 755 /root/AgentRestart.sh /root/AgentRestart.sh echo "" echo "Agent安装完成,请到监控页面添加Agent和下发策略:$monitorSvr" echo "" echo "注意:" echo "如有必要请在防火墙放行 9123 端口" echo "" } selectAction