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

248 lines
8.3 KiB

#!/usr/bin/env bash
#
# Description: Choose a faster mirror for Linux script
#
# 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.0${PLAIN}${YELLOW} (30 Dec 2021) ${PLAIN}"
echo -e "${YELLOW} Copyright (C) 2021 liveJQ cloud@livejq.fun ${PLAIN}"
echo ""
read -rp "请输入数字选择:" num
case $num in
1)
get_info
install_SMP
;;
2)
get_info
install_Agent
;;
*)
get_info
install_SMP
install_Agent
;;
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
}
get_info() {
source /etc/os-release || source /usr/lib/os-release || exit 1
if [[ $ID == "centos" ]]; then
PM="yum"
INS="yum install"
updateCe
elif [[ $ID == "debian" || $ID == "ubuntu" ]]; then
PM="apt"
INS="apt install"
updateUb
else
exit 1
fi
}
install_SMP() {
rpm_packages="smokeping unzip wqy-zenhei-fonts.noarch"
apt_packages="smokeping echoping unzip traceroute"
read -rp "输入监控页面端口(默认20000):" port1
ss -tnlp | grep -q ":${port1:-20000} " && echo "端口 ${port1:-20000} 已被占用" && exit 1
if [[ $ID == "debian" || $ID == "ubuntu" ]]; then
$PM update
$INS wget curl -y
$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 wget curl -y
$INS $rpm_packages -y
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
$PM update
$INS wget curl -y
$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 wget curl -y
$INS $rpm_packages -y
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 Gist config
wget https://gist.github.com/livejq/d60d8db950a42e9ceb7e4c01e9d32921/archive/9275570e264e12fbaf62a0fc40229818834a74d3.zip
unzip -o *.zip -d /etc/smokeping/
mv /etc/smokeping/d60d8db950a42e9ceb7e4c01e9d32921-9275570e264e12fbaf62a0fc40229818834a74d3 /etc/smokeping/config.d
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/
#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() {
#Agent
mkdir -p /var/monitor_agent
wget $origin
unzip master.zip
mv pingm-master/Agent/agentMain.jar /var/monitor_agent
mv pingm-master/Agent/conf /var/monitor_agent
mv pingm-master/Agent/AgentRestart.sh /root
rm -f master.zip
rm -fr pingm-master
chmod 755 -R /var/monitor_agent
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 Gist config
wget https://gist.github.com/livejq/5175c6801ac088cf5177f6a1b12af45c/archive/002038e18ae5617b7aea89ba188c4886cc72eb99.zip
unzip -o *.zip -d /etc/smokeping/
mv /etc/smokeping/5175c6801ac088cf5177f6a1b12af45c-002038e18ae5617b7aea89ba188c4886cc72eb99 /etc/smokeping/config.d
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 stop firewalld && systemctl disable firewalld
#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() {
#Agent
mkdir -p /var/monitor_agent
wget $origin
unzip master.zip
mv pingm-master/Agent/agentMain.jar /var/monitor_agent
mv pingm-master/Agent/conf /var/monitor_agent
mv pingm-master/Agent/AgentRestart.sh /root
rm -f master.zip
rm -fr pingm-master
chmod 755 -R /var/monitor_agent
chmod 755 /root/AgentRestart.sh
/root/AgentRestart.sh
echo ""
echo "Agent安装完成,请到监控页面添加Agent和下发策略:$monitorSvr"
echo ""
echo "注意:"
echo "如有必要请在防火墙放行 9123 端口"
echo ""
}
selectAction