-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathagent.sh
162 lines (141 loc) · 5.51 KB
/
agent.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
export VERSION=${VERSION:-'17.5'}
red="\033[1;91m"
green="\e[1;32m"
yellow="\e[1;33m"
plain="\033[0m"
cur_dir=$(pwd)
# check root
[[ $EUID -ne 0 ]] && echo -e "${red}错误:${plain} 请root用户运行此脚本!\n" && exit 1
# check os
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif cat /etc/issue | grep -Eqi "debian"; then
release="debian"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
release="ubuntu"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
release="centos"
elif cat /etc/issue | grep -Eqi "Fedora|almalinux|rocky"; then
release="red hat"
elif cat /proc/version | grep -Eqi "debian"; then
release="debian"
elif cat /proc/version | grep -Eqi "ubuntu"; then
release="ubuntu"
elif cat /proc/version | grep -Eqi "alpine"; then
release="alpine"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
release="centos"
fi
arch=$(arch)
if [[ $arch == "x86_64" || $arch == "x64" || $arch == "amd64" ]]; then
arch="amd64"
elif [[ $arch == "aarch64" || $arch == "arm" || $arch == "arm64" ]]; then
arch="arm64"
elif [[ $arch == "s390x" ]]; then
arch="s390x"
else
arch="amd64"
echo -e "${red}未知的系统架构: ${arch}${plain}"
fi
if [ "$(getconf WORD_BIT)" != '32' ] && [ "$(getconf LONG_BIT)" != '64' ] ; then
echo "本软件不支持 32 位系统(x86),请使用 64 位系统(x86_64),如果检测有误,请联系作者"
exit 2
fi
os_version=""
# os version
if [[ -f /etc/os-release ]]; then
os_version=$(awk -F'[= ."]' '/VERSION_ID/{print $3}' /etc/os-release)
fi
if [[ -z "$os_version" && -f /etc/lsb-release ]]; then
os_version=$(awk -F'[= ."]+' '/DISTRIB_RELEASE/{print $2}' /etc/lsb-release)
fi
if [[ x"${release}" == x"centos" ]]; then
if [[ ${os_version} -le 6 ]]; then
echo -e "${red}请使用 CentOS 7 或更高版本的系统!${plain}\n" && return 0
fi
elif [[ x"${release}" == x"ubuntu" ]]; then
if [[ ${os_version} -lt 16 ]]; then
echo -e "${red}请使用 Ubuntu 16 或更高版本的系统!${plain}\n" && exit 1
fi
elif [[ x"${release}" == x"debian" ]]; then
if [[ ${os_version} -lt 8 ]]; then
echo -e "${red}请使用 Debian 8 或更高版本的系统!${plain}\n" && exit 1
fi
fi
install_base() {
if [[ "${release}" == "centos" ]]; then
yum install epel-release -y
yum install wget unzip -y
elif [[ "${release}" == "alpine" ]]; then
apk update
apk add wget unzip
else
apt update
apt install wget unzip -y
fi
}
Disable_automatic_updates() {
echo -e "${green}开始修改nezha-agent服务"
if [[ "${release}" == "alpine" ]]; then
if [ -f /etc/init.d/nezha-agent ]; then
echo -e "${green}/etc/init.d/nezha-agent服务存在,开始尝试禁用自动更新 ${plain}"
sed -i '/command_args=/ s/"$/ --disable-auto-update --disable-force-update"/' /etc/init.d/nezha-agent
echo -e "${green}=========================================== ${plain}"
cat /etc/init.d/nezha-agent
else
echo -e "${yellow}/etc/init.d/nezha-agent服务不存在,请尝试手动修改${plain}"
fi
else
if [ -f /etc/systemd/system/nezha-agent.service ]; then
echo -e "${green}/etc/systemd/system/nezha-agent.service服务存在,开始尝试禁用自动更新${plain}"
sudo sed -i '/^ExecStart=/ s/$/ --disable-auto-update --disable-force-update/' /etc/systemd/system/nezha-agent.service
echo -e "${green}=========================================== ${plain}"
cat /etc/systemd/system/nezha-agent.service
else
echo -e "${yellow}/etc/systemd/system/nezha-agent.service服务不存在,请尝试手动修改${plain}"
fi
fi
}
Downlond_agent(){
echo -e "${green}开始尝试降级Agent"
if [ -f /opt/nezha/agent/nezha-agent ]; then
echo -e "${green}=========================================== ${plain}"
echo -e "${green}/opt/nezha/agent/nezha-agent存在,开始尝试降级到${VERSION}${plain}"
echo -e "${green}检测到系统为: ${release} 架构: ${arch} ${plain}"
if [[ "${arch}" == "amd64" ]]; then
wget https://github.com/nezhahq/agent/releases/download/v0.${VERSION}/nezha-agent_linux_amd64.zip && unzip nezha-agent_linux_amd64.zip && rm nezha-agent_linux_amd64.zip && mv nezha-agent /opt/nezha/agent/nezha-agent
elif [[ "${arch}" == "arm64" ]]; then
wget https://github.com/nezhahq/agent/releases/download/v0.${VERSION}/nezha-agent_linux_arm64.zip && unzip nezha-agent_linux_arm64.zip && rm nezha-agent_linux_arm64.zip && mv nezha-agent /opt/nezha/agent/nezha-agent
fi
else
echo -e "${yellow}/opt/nezha/agent/nezha-agent不存在,请尝试手动降级${plain}"
fi
}
restart_agent(){
echo -e "${green}开始尝试重启agent服务${plain}"
if [[ "${release}" == "centos" ]]; then
sudo systemctl daemon-reload
systemctl restart nezha-agent
elif [[ "${release}" == "alpine" ]]; then
chmod +x /etc/init.d/nezha-agent
rc-update add nezha-agent default
rc-service nezha-agent restart
else
sudo systemctl daemon-reload
systemctl restart nezha-agent
fi
if [ $? -eq 0 ]; then
echo -e "${green}nezha-agent服务已成功重启\n${plain}"
else
echo -e "${red}nezha-agent服务重启失败\n${plain}"
fi
}
echo -e "${green}当前系统为: ${release} 架构: ${arch} ${plain}"
install_base
Disable_automatic_updates
sleep 1
echo -e "${green}=========================================== ${plain}"
echo -e "${green}请自行判断配置是否正确 ${plain}"
Downlond_agent
restart_agent