forked from eheh-org/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
293 lines (244 loc) · 8.68 KB
/
install.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
#
# EHEH Agent Installation Script
#
# @version 1.0.0
# @date 2020-09-21
# @copyright: The script baseon nodequery.com/nq-agent project under mit license
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Set environment
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_yellow() {
printf '\033[0;31;33m%b\033[0m' "$1"
}
_blue() {
printf '\033[0;31;36m%b\033[0m' "$1"
}
clear
# Prepare output
echo -e "|\n| EHEH 一键安装\n| ===================\n|"
# Root required
if [ $(id -u) != "0" ];
then
echo -e "| Error: 你需要用root账号执行该脚本\n|"
echo -e "| 安装脚本将创建一个EHEH账号用来定时执行脚本\n|"
exit 1
fi
# Parameters required
if [ $# -lt 1 ]
then
echo -e "| 用法: bash $0 'token'\n|"
exit 1
fi
#delete exits data
echo "|" && read -p "| 安装将删除本地的以前记录 [Y/n] " input_del
if [ -z $input_del ] || [ $input_del == "Y" ] || [ $input_del == "y" ]
then
#uninstall
rm -Rf /etc/EHEH
crontab -u EHEH -r 1>/dev/null 2>/dev/null
userdel EHEH 1>/dev/null 2>/dev/null
else
exit 1
fi
# Check if crontab is installed
if [ ! -n "$(command -v crontab)" ]
then
# Confirm crontab installation
echo "|" && read -p "| 当前系统没安装Crontab,无法定时推送数据,安装吗?? [Y/n] " input_variable_install
# Attempt to install crontab
if [ -z $input_variable_install ] || [ $input_variable_install == "Y" ] || [ $input_variable_install == "y" ]
then
if [ -n "$(command -v apt-get)" ]
then
# echo -e "|\n| Notice: Installing required package 'cron' via 'apt-get'"
apt-get -y update
apt-get -y install cron
elif [ -n "$(command -v yum)" ]
then
# echo -e "|\n| Notice: Installing required package 'cronie' via 'yum'"
yum -y install cronie
if [ ! -n "$(command -v crontab)" ]
then
# echo -e "|\n| Notice: Installing required package 'vixie-cron' via 'yum'"
yum -y install vixie-cron
fi
elif [ -n "$(command -v pacman)" ]
then
# echo -e "|\n| Notice: Installing required package 'cronie' via 'pacman'"
pacman -S --noconfirm cronie
fi
fi
if [ ! -n "$(command -v crontab)" ]
then
# Show error
echo -e "|\n| Error: Crontab is required and could not be installed\n|"
exit 1
fi
fi
# Check if netstat is installed
if [ ! -n "$(command -v netstat)" ]
then
echo "|" && read -p "| 当前系统没安装netstat,安装吗?? [Y/n] " input_variable_install
# Attempt to install crontab
if [ -z $input_variable_install ] || [ $input_variable_install == "Y" ] || [ $input_variable_install == "y" ]
then
if [ -n "$(command -v apt-get)" ]
then
apt-get -y update
apt-get -y install net-tools
elif [ -n "$(command -v yum)" ]
then
yum -y install net-tools
elif [ -n "$(command -v pacman)" ]
then
pacman -S --noconfirm netstat-nat
fi
fi
if [ ! -n "$(command -v netstat)" ]
then
# Show error
echo -e "|\n| 错误:自动安装netstat过程中出现错误,一般是你取消了安装或源或者依赖问题,请处理后再进行\n|"
echo -e "|\n| 如果你系统是Ubuntu 20.04, 19.10 或者 19.04,且提示错误是libpng12-0缺失,可参考以下解决\n|"
echo -e "| add-apt-repository ppa:linuxuprising/libpng12|"
echo -e "| apt update|"
echo -e "| apt install libpng12-0\n|"
echo -e "|\n| 如果你系统是其他版本的ubuntu,或其他操作系统,请自行搜索解决\n|"
printf "$(_blue "| netstat命令没安装好,将无法统计MYSQL基本指标、Web端口信息,如您不需要统计这2项,可不做处理。") \n\n"
# exit 1
fi
fi
# Check if netstat is installed
if [ ! -n "$(command -v ping)" ]
then
echo "|" && read -p "| 当前系统没安装ping,安装吗?? [Y/n] " input_variable_install
# Attempt to install crontab
if [ -z $input_variable_install ] || [ $input_variable_install == "Y" ] || [ $input_variable_install == "y" ]
then
if [ -n "$(command -v apt-get)" ]
then
apt-get -y update
apt-get -y install iputils-ping
elif [ -n "$(command -v yum)" ]
then
yum -y install iputils
elif [ -n "$(command -v pacman)" ]
then
pacman -S --noconfirm iputils
fi
fi
if [ ! -n "$(command -v ping)" ]
then
# Show error
echo -e "|\n| 错误:自动安装ping过程中出现错误,一般是源或者依赖问题,请根据提示修复后再进行\n|"
exit 1
fi
fi
# Check if cron is running
if [ -z "$(ps -Al | grep cron | grep -v grep)" ]
then
# Confirm cron service
echo "|" && read -p "| 系统定时任务Crontab没启动,要启动吗? [Y/n] " input_variable_service
# Attempt to start cron
if [ -z $input_variable_service ] || [ $input_variable_service == "Y" ] || [ $input_variable_service == "y" ]
then
if [ -n "$(command -v apt-get)" ]
then
# echo -e "|\n| Notice: Starting 'cron' via 'service'"
service cron start
elif [ -n "$(command -v yum)" ]
then
# echo -e "|\n| Notice: Starting 'crond' via 'service'"
chkconfig crond on
service crond start
elif [ -n "$(command -v pacman)" ]
then
# echo -e "|\n| Notice: Starting 'cronie' via 'systemctl'"
systemctl start cronie
systemctl enable cronie
fi
fi
# Check if cron was started
if [ -z "$(ps -Al | grep cron | grep -v grep)" ]
then
# Show error
echo -e "|\n| Error: Crontab定时任务没启动\n|"
exit 1
fi
fi
# Attempt to delete previous agent
if [ -f /etc/EHEH/eheh-agent.sh ]
then
# Remove agent dir
rm -Rf /etc/EHEH
# Remove cron entry and user
if id -u EHEH >/dev/null 2>&1
then
(crontab -u EHEH -l | grep -v "/etc/EHEH/eheh-agent.sh") | crontab -u EHEH - && userdel EHEH
else
(crontab -u root -l | grep -v "/etc/EHEH/eheh-agent.sh") | crontab -u root -
fi
fi
# Create agent dir
mkdir -p /etc/EHEH
# Download agent -offical website
#echo -e "| 下载脚本到 /etc/EHEH\n|\n| + $(wget -nv -o /dev/stdout -O /etc/EHEH/eheh-agent.sh --no-check-certificate https://eheh.org/shell/eheh-agent.sh)"
#github link
echo -e "| 下载脚本到 /etc/EHEH\n|\n| + $(wget -nv -o /dev/stdout -O /etc/EHEH/eheh-agent.sh --no-check-certificate https://raw.github.com/eheh-org/agent/master/eheh-agent.sh)"
if [ -f /etc/EHEH/eheh-agent.sh ]
then
# Create auth file
echo "$1" > /etc/EHEH/eheh-auth.log
# Create user
useradd EHEH -r -d /etc/EHEH -s /bin/false
# Modify user permissions
chown -R EHEH:EHEH /etc/EHEH && chmod -R 700 /etc/EHEH
# Modify ping permissions
chmod +s `type -p ping`
# Configure cron
crontab -u EHEH -l 2>/dev/null | { cat; echo "*/3 * * * * bash /etc/EHEH/eheh-agent.sh > /etc/EHEH/eheh-cron.log 2>&1"; } | crontab -u EHEH -
# Show success
echo -e "|\n| 小提示:如果你的服务器安装有宝塔,crontab可能被停用了,请参考:https://eheh.org/article/10 进行设置下"
echo -e "|\n| 小提示:如果你的服务使用了Cpanel,ping可能缺少权限,请参考https://eheh.org/article/6 进行设置下"
echo -e "|\n|"
printf "$(_blue "| 安装完成,开始设置") \n\n"
echo "|" && read -p "| 你想监控mysql状态吗,如果要请输入Y,我们将帮你设置 [Y/n] " input_mysql
if [ -z $input_mysql ] || [ $input_mysql == "Y" ] || [ $input_mysql == "y" ]
then
echo "|" && read -p "| 请输入mysql用户名 " input_mu
$(echo "$input_mu" > /etc/EHEH/mysql-user)
echo "|" && read -p "| 请输入mysql密码 " input_mp
$(echo "$input_mp" > /etc/EHEH/mysql-pass)
echo -e "|\n| Done,完成设置"
fi
if [ -n "$(command -v apt-get)" ]
then
printf "$(_yellow "| Debian/ubuntu的默认shell已经切换成了dash。如需马上推送一次数据,请手动执行以下2条命令")\n\n"
printf "$(_blue "| /etc/EHEH/eheh-agent.sh ") \n\n"
printf "$(_blue "| chown -Rf EHEH.EHEH /etc/EHEH/") \n\n"
else
#push data at once ?
echo "|" && read -p "| 需要马上推送数据吗?或等5/6分钟 [Y/n] " push
if [ $push == "Y" ] || [ $push == "y" ]
then
sh /etc/EHEH/eheh-agent.sh
chown -Rf EHEH.EHEH /etc/EHEH/
fi
fi
echo -e "|\n| 完成安装!EHEH.ORG 上见\n|"
# Attempt to delete installation script
if [ -f $0 ]
then
rm -f $0
fi
else
# Show error
echo -e "|\n| 错误,无法安装\n|"
fi