-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzbx_active_mode.sh
179 lines (151 loc) · 5.55 KB
/
zbx_active_mode.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
#!/bin/bash
# Date: 2019.08.28
UBUNTU_CHRONY_CONFIG="/etc/chrony/chrony.conf"
CENTOS_CHRONY_CONFIG="/etc/chrony.conf"
ZBX_CONFIG="/etc/zabbix/zabbix_agentd.conf"
ZBX_SERVER="X.Y.Z.W:20051"
AGENT_NAME=`ip addr | grep inet | egrep -v '(127.0.0.1|inet6|docker)' | awk '{print $2}' | tr -d "addr:" | head -n 1 | cut -d / -f1`
TIMEOUT=30
START_AGENT=0
DISTROS=$(awk -F "\"| " 'NR==1 {print $2}' /etc/os-release)
DISTROS_VERSION=$(awk -F "\"| " 'NR==2 {print $2}' /etc/os-release)
IP_LIST=$(ip addr | grep inet | egrep -v '(127.0.0.1|inet6|docker)' | awk '{print $2}' | tr -d "addr:" | cut -d / -f1)
IP_COUNT=$(ip addr | grep inet | egrep -v '(127.0.0.1|inet6|docker)' | awk '{print $2}' | tr -d "addr:" | cut -d / -f1 | wc -l)
let COUNT=1
if [ ${IP_COUNT} -gt 1 ]; then
for i in ${IP_LIST}; do
printf "$COUNT: $i\n"
let COUNT+=1
done
read -p "Please choose a number: " NUMBER
AGENT_NAME=`ip addr | grep inet | egrep -v '(127.0.0.1|inet6|docker)' | awk '{print $2}' | tr -d "addr:" | cut -d / -f1 | awk "NR==$NUMBER {print $1}"`
else
AGENT_NAME=`ip addr | grep inet | egrep -v '(127.0.0.1|inet6|docker)' | awk '{print $2}' | tr -d "addr:" | head -n 1 | cut -d / -f1`
fi
command_exists() {
command -v "$@" > /dev/null 2>&1
}
yum_install_pkgs() {
local PKG_NAME=$1
local BIN_NAME=$2
if ! command_exists ${BIN_NAME} &> /dev/null; then
yum -y install ${PKG_NAME}
if ! command_exists ${BIN_NAME} &> /dev/null; then
echo "${PKG_NAME} service install failed, please install it manually."
exit 1
fi
else
echo "${PKG_NAME} service already exist."
fi
}
apt_install_pkgs() {
local PKG_NAME=$1
local BIN_NAME=$2
if ! command_exists ${BIN_NAME} &> /dev/null; then
apt-get -y install ${PKG_NAME} &> /dev/null
if ! command_exists ${BIN_NAME} &> /dev/null; then
echo "${PKG_NAME} service install failed, please install it manually."
exit 1
fi
else
echo "${PKG_NAME} service already exist."
fi
}
update_ubuntu_chrony_config() {
sed -i 's/^\(pool .*\)/#\1/g' ${UBUNTU_CHRONY_CONFIG}
echo "pool ntp1.aliyun.com online iburst" >> ${UBUNTU_CHRONY_CONFIG}
echo "pool ntp2.aliyun.com online iburst" >> ${UBUNTU_CHRONY_CONFIG}
echo "pool ntp3.aliyun.com online iburst" >> ${UBUNTU_CHRONY_CONFIG}
}
update_centos_chrony_config() {
sed -i 's/^\(server .*\)/#\1/g' ${CENTOS_CHRONY_CONFIG}
echo "server ntp1.aliyun.com iburst" >> ${CENTOS_CHRONY_CONFIG}
echo "server ntp2.aliyun.com iburst" >> ${CENTOS_CHRONY_CONFIG}
echo "server ntp3.aliyun.com iburst" >> ${CENTOS_CHRONY_CONFIG}
}
modify_zbx_config() {
sed -i 's/^\(Server=.*\)/#\1/g' ${ZBX_CONFIG}
sed -i "s/^\(ServerActive=\).*/\1${ZBX_SERVER}/g" ${ZBX_CONFIG}
sed -i "s/^\(Hostname=\).*/\1${AGENT_NAME}/g" ${ZBX_CONFIG}
sed -i 's/^# \(HostMetadataItem=\).*/\1system.uname/g' ${ZBX_CONFIG}
sed -i "s/^# \(Timeout=\).*/\1${TIMEOUT}/g" ${ZBX_CONFIG}
if ! egrep "^StartAgents" ${ZBX_CONFIG} &> /dev/null; then
echo "StartAgents=0" >> ${ZBX_CONFIG}
else
sed -i "s/^\(StartAgents=\).*/\1${START_AGENT}/g" ${ZBX_CONFIG}
fi
}
start_service() {
local SRV_NMAE=$1
systemctl restart ${SRV_NMAE} &> /dev/null
systemctl enable ${SRV_NMAE} &> /dev/null
if systemctl status ${SRV_NMAE} &> /dev/null; then
echo "${SRV_NMAE} started successfully."
else
echo "${SRV_NMAE} start failed."
fi
}
#start_zbx_agent() {
# systemctl restart zabbix-agent &> /dev/null
# systemctl enable zabbix-agent &> /dev/null
#
# if systemctl status zabbix-agent &> /dev/null; then
# echo "zabbix agent started successfully."
# else
# echo "zabbix agent start failed."
# fi
#}
add_centos_zbx_repo() {
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
yum makecache fast
}
add_ubuntu16_zbx_repo() {
wget -q -t 0 -c https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+xenial_all.deb && dpkg -i zabbix-release_4.0-3+xenial_all.deb && apt update
}
add_ubuntu18_zbx_repo() {
wget -q -t 0 -c https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb && dpkg -i zabbix-release_4.0-3+bionic_all.deb && apt update
}
install_centos_zbx_agent () {
yum -y install wget
add_centos_zbx_repo
yum_install_pkgs zabbix-agent zabbix_agentd
modify_zbx_config
#start_zbx_agent
start_service zabbix-agent
}
install_ubuntu16_zbx_agent() {
apt -y install wget
add_ubuntu16_zbx_repo
apt_install_pkgs zabbix-agent zabbix_agentd
modify_zbx_config
#start_zbx_agent
start_service zabbix-agent
}
install_ubuntu18_zbx_agent() {
apt -y install wget
add_ubuntu18_zbx_repo
apt_install_pkgs zabbix-agent zabbix_agentd
modify_zbx_config
#start_zbx_agent
start_service zabbix-agent
}
# ########## main ##########
if [ ${DISTROS} == "CentOS" -a ${DISTROS_VERSION} == "7" ]; then
install_centos_zbx_agent
yum_install_pkgs chrony chronyd
update_centos_chrony_config
start_service chronyd
elif [[ ${DISTROS} == "Ubuntu" ]] && [[ ${DISTROS_VERSION} =~ 16* ]]; then
install_ubuntu16_zbx_agent
apt_install_pkgs chrony chronyd
update_ubuntu16_chrony_config
start_service chrony
elif [[ ${DISTROS} == "Ubuntu" ]] && [[ ${DISTROS_VERSION} =~ 18* ]]; then
install_ubuntu18_zbx_agent
apt_install_pkgs chrony chronyd
update_ubuntu18_chrony_config
start_service chrony
else
echo "Unknown distros"
exit 1
fi