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

267 lines
9.1 KiB

3 years ago
#!/usr/bin/env bash
#
# Description: Install monitor or agent automatically.
3 years ago
#
# 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'
3 years ago
target_dir="/opt/ysnet"
3 years ago
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}"
3 years ago
echo -e "${YELLOW} Copyright (C) 2021 liveJQ cloud@livejq.fun ${PLAIN}"
echo ""
read -rp "请输入数字选择:" num
case $num in
1)
before
3 years ago
install_SMP
after
3 years ago
;;
2)
before
3 years ago
install_Agent
after
3 years ago
;;
*)
before
3 years ago
install_Agent
install_SMP
after
3 years ago
;;
esac
}
3 years ago
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() {
3 years ago
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
3 years ago
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
3 years ago
else
exit 1
fi
}
after() {
rm -f master.zip
rm -fr pingm-master
>/root/.bash_history
}
3 years ago
install_SMP() {
rpm_packages="smokeping tcptraceroute wqy-zenhei-fonts.noarch"
apt_packages="smokeping echoping traceroute tcptraceroute"
3 years ago
read -rp "输入监控页面端口(默认20000):" port1
ss -tnlp | grep -q ":${port1:-20000} " && echo -e "${RED}端口 ${port1:-20000} 已被占用${PLAIN}" && exit 1
3 years ago
if [[ $ID == "debian" || $ID == "ubuntu" ]]; then
$INS $apt_packages -y || error=1
[[ $error -eq 1 ]] && echo -e "${RED}安装 smokeping 失败${PLAIN}" && exit 1
3 years ago
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
3 years ago
systemctl stop httpd && systemctl stop smokeping
configureCe
fi
}
3 years ago
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
3 years ago
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
3 years ago
configureCeAgent
fi
}
3 years ago
configureUb() {
#get local ip
ip=$(curl -sL https://api64.ipify.org -4) || error=1
[[ $error -eq 1 ]] && echo -e "${RED}获取本机 IP 地址失败${PLAIN}" && exit 1
3 years ago
#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
3 years ago
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
3 years ago
chmod 777 -R /var/lib/smokeping/
#ufw
ufw allow 22/tcp && ufw allow ${port1:-20000}/tcp
ufw enable && systemctl enable ufw
ufw reload
3 years ago
#timezone
timedatectl set-timezone Asia/Shanghai
#test
smokeping --debug || error=1
[[ $error -eq 1 ]] && echo -e "${RED}测试运行失败${PLAIN}" && exit 1
3 years ago
systemctl start apache2 smokeping || error=1
[[ $error -eq 1 ]] && echo -e "${RED}启动失败${PLAIN}" && exit 1
3 years ago
echo ""
3 years ago
echo "Smokeping安装完成,监控页面:http://$ip:${port1:-20000}/smokeping"
3 years ago
echo ""
echo "注意:"
echo "如有必要请在防火墙放行 ${port1:-20000} 端口"
echo "请等待一会,监控数据不会立即更新"
echo ""
}
3 years ago
configureUbAgent() {
mkdir -p /var/monitor_agent
#get Gitee config
3 years ago
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
3 years ago
chmod 755 /root/AgentRestart.sh
/root/AgentRestart.sh
echo ""
echo "Agent安装完成,请到监控页面添加Agent和下发策略:$monitorSvr"
echo ""
echo "注意:"
echo "如有必要请在防火墙放行 9123 端口"
echo ""
}
3 years ago
configureCe() {
#get local ip
ip=$(curl -sL https://api64.ipify.org -4) || error=1
[[ $error -eq 1 ]] && echo -e "${RED}获取本机 IP 地址失败${PLAIN}" && exit 1
3 years ago
#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
3 years ago
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
3 years ago
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
3 years ago
#timezone
timedatectl set-timezone Asia/Shanghai
#test
smokeping --debug || error=1
[[ $error -eq 1 ]] && echo -e "${RED}测试运行失败${PLAIN}" && exit 1
3 years ago
systemctl start httpd smokeping || error=1
[[ $error -eq 1 ]] && echo -e "${RED}启动失败${PLAIN}" && exit 1
3 years ago
echo ""
3 years ago
echo "Smokeping安装完成,监控页面:http://$ip:${port1:-20000}/smokeping/sm.cgi"
3 years ago
echo ""
echo "注意:"
echo "如有必要请在防火墙放行 ${port1:-20000} 端口"
echo "请等待一会,监控数据不会立即更新"
echo ""
}
3 years ago
configureCeAgent() {
mkdir -p /var/monitor_agent
#get Gitee config
3 years ago
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
3 years ago
chmod 755 /root/AgentRestart.sh
/root/AgentRestart.sh
echo ""
echo "Agent安装完成,请到监控页面添加Agent和下发策略:$monitorSvr"
echo ""
echo "注意:"
echo "如有必要请在防火墙放行 9123 端口"
echo ""
}
3 years ago
selectAction