Auto For Ping
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.
pingm/AutoPing.sh

268 lines
9.2 KiB

#!/usr/bin/env bash
#
# Description: Install monitor or agent automatically.
#
# Copyright (C) 2021 liveJQ <cloud@livejq.fun>
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.0.7${PLAIN}${YELLOW} (26 Mar. 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_Agent
install_SMP
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/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-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
rm -f master.zip && rm -fr pingm-master
wget $origin && unzip master.zip || error=1
[[ $error -eq 1 ]] && echo -e "${RED}下载资源失败,请确保本地正常访问 gitee.com 后重新尝试${PLAIN}" && exit 1
elif [[ $ID == "debian" || $ID == "ubuntu" ]]; then
PM="apt"
INS="apt install"
updateUb
$PM update
$INS wget curl unzip -y
rm -f master.zip && rm -fr pingm-master
wget $origin && unzip master.zip || error=1
[[ $error -eq 1 ]] && echo -e "${RED}下载资源失败,请确保本地正常访问 gitee.com 后重新尝试${PLAIN}" && exit 1
else
exit 1
fi
}
after() {
rm -f master.zip
rm -fr pingm-master
>/root/.bash_history
}
install_SMP() {
rpm_packages="smokeping tcptraceroute wqy-zenhei-fonts.noarch"
apt_packages="smokeping echoping traceroute tcptraceroute"
read -rp "输入监控页面端口(默认20000):" port1
ss -tnlp | grep -q ":${port1:-20000} " && echo -e "${RED}端口 ${port1:-20000} 已被占用${PLAIN}" && exit 1
if [[ $ID == "debian" || $ID == "ubuntu" ]]; then
$INS $apt_packages -y || error=1
[[ $error -eq 1 ]] && echo -e "${RED}安装 smokeping 失败${PLAIN}" && 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 -e "${RED}安装 smokeping 失败${PLAIN}" && 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 -e "${RED}安装 Agent 客户端依赖包失败${PLAIN}" && 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 -e "${RED}安装 Agent 客户端依赖包失败${PLAIN}" && exit 1
configureCeAgent
fi
}
configureUb() {
#get local ip
ip=$(curl -sL https://api64.ipify.org -4) || error=1
[[ $error -eq 1 ]] && echo -e "${RED}获取本机 IP 地址失败${PLAIN}" && 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 http://www.vdberg.org/~richard/tcpping
chmod 755 /usr/bin/tcpping
#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 -e "${RED}测试运行失败${PLAIN}" && exit 1
chmod 777 -R /var/lib/smokeping/
systemctl start apache2 smokeping || error=1
[[ $error -eq 1 ]] && echo -e "${RED}启动失败${PLAIN}" && exit 1
echo ""
echo "Smokeping安装完成,监控页面:http://$ip:${port1:-20000}/smokeping"
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 -e "${RED}获取本机 IP 地址失败${PLAIN}" && 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 http://www.vdberg.org/~richard/tcpping
chmod 755 /usr/bin/tcpping
#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
#httpd
systemctl start httpd smokeping || error=1
[[ $error -eq 1 ]] && echo -e "${RED}httpd 启动失败${PLAIN}" && exit 1
#test
smokeping --debug || error=1
[[ $error -eq 1 ]] && echo -e "${RED}smokeping 测试运行失败${PLAIN}" && exit 1
chmod 777 -R /var/lib/smokeping/
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