mgmt/scripts/install-monitoring-agents.sh

100 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# 智能安装监控代理软件脚本
# 检查软件是否已安装,如果已安装则跳过
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 节点列表
NODES=(
"ch2.tailnet-68f9.ts.net"
"ch3.tailnet-68f9.ts.net"
"ash1d.tailnet-68f9.ts.net"
"ash2e.tailnet-68f9.ts.net"
"de.tailnet-68f9.ts.net"
"onecloud1.tailnet-68f9.ts.net"
"semaphore.tailnet-68f9.ts.net"
"ch4.tailnet-68f9.ts.net"
"ash3c.tailnet-68f9.ts.net"
"warden.tailnet-68f9.ts.net"
"hcp1.tailnet-68f9.ts.net"
"influxdb.tailnet-68f9.ts.net"
"browser.tailnet-68f9.ts.net"
)
# 检查软件是否已安装
check_software() {
local node=$1
local software=$2
echo -e "${BLUE}[$(date +%H:%M:%S)]${NC} 检查 ${node} 上的 ${software}..."
if ssh ben@${node} "which ${software} >/dev/null 2>&1"; then
echo -e "${GREEN}[SKIP]${NC} ${node} 上已安装 ${software}"
return 0
else
echo -e "${YELLOW}[INSTALL]${NC} ${node} 上需要安装 ${software}"
return 1
fi
}
# 安装软件
install_software() {
local node=$1
local software=$2
echo -e "${BLUE}[$(date +%H:%M:%S)]${NC}${node} 上安装 ${software}..."
case $software in
"prometheus-node-exporter")
echo "3131" | ssh ben@${node} "sudo -S apt update && sudo -S apt install -y prometheus-node-exporter"
;;
"promtail")
echo "3131" | ssh ben@${node} "sudo -S apt update && sudo -S apt install -y promtail"
;;
*)
echo -e "${RED}[ERROR]${NC} 未知软件: ${software}"
return 1
;;
esac
if [ $? -eq 0 ]; then
echo -e "${GREEN}[SUCCESS]${NC} ${node}${software} 安装成功"
else
echo -e "${RED}[ERROR]${NC} ${node}${software} 安装失败"
return 1
fi
}
# 主函数
main() {
echo -e "${BLUE}=== 智能安装监控代理软件 ===${NC}"
# 安装 node-exporter
echo -e "\n${YELLOW}=== 安装 Node Exporter ===${NC}"
for node in "${NODES[@]}"; do
if ! check_software "${node}" "prometheus-node-exporter"; then
install_software "${node}" "prometheus-node-exporter"
fi
done
# 安装 promtail
echo -e "\n${YELLOW}=== 安装 Promtail ===${NC}"
for node in "${NODES[@]}"; do
if ! check_software "${node}" "promtail"; then
install_software "${node}" "promtail"
fi
done
echo -e "\n${GREEN}=== 所有监控代理软件安装完成 ===${NC}"
}
# 运行主函数
main "$@"