m-shell-scripts/scripts_shell/install_zabbix_agentd.sh

179 lines
4.3 KiB
Bash
Raw Normal View History

2025-03-19 17:38:46 +08:00
#!/bin/bash
function read_variable() {
readonly install_path=/usr/local/zabbix-7.0.1
readonly url="https://cdn.zabbix.com/zabbix/sources/stable/7.0/zabbix-7.0.1.tar.gz"
readonly service_name="zabbix_agentd"
}
function build_variable() {
read_variable
which wget 2>/dev/null
if [ $? -ne 0 ]; then
yum install wget -y
fi
[ -f /etc/init.d/${service_name} ] && echo "Service /etc/init.d/${service_name} Exist! Exit!" && exit
[ -d $install_path ] && echo "Path ${install_path} Exist! Exit" && exit
readonly package=$(echo $url | awk -F'/' '{print $NF}')
readonly zip_dir=$(echo $package | sed 's/.tar.gz//g')
if [ ! -f $package ]; then
wget $url --no-check-certificate
fi
[ ! -f $package ] && echo "$package not found" && exit
[ -d $zip_dir ] && echo "$zip_dir Exsist! Exit!" && exit
}
function help1() {
echo -e "\033[01m"
echo
echo "$0 install"
echo
echo -e "\033[0m"
}
function rewrite_conf() {
mkdir -p ${install_path}/etc/
[ -f ${install_path}/etc/zabbix_agentd.conf ] && mv ${install_path}/etc/zabbix_agentd.conf ${install_path}/etc/zabbix_agentd.conf.bak
cat <<EOF >${install_path}/etc/zabbix_agentd.conf
PidFile=${install_path}/zabbix_agentd.pid
LogType=file
LogFile=${install_path}/zabbix_agentd.log
# 被动模式的服务器地址
Server=192.168.0.200
ListenPort=10050
# 监听的IP, 可填多个, 第一个IP用于自动注册
ListenIP=0.0.0.0
# 主动模式的服务器地址
ServerActive=192.168.0.200
# 主机名
Hostname=localhost
# 主机元数据
HostMetadata=Linux LiLin
User=zabbix
Include=${install_path}/etc/zabbix_agentd.conf.d/*.conf
##################################################################
## 主动模式的链接方式 # unencrypted psk cert
#TLSConnect=unencrypted
## 被动模式的链接方式 # unencrypted psk cert
#TLSAccept=cert
## CA证书
#TLSCAFile=/root/SSL/CA/ca.pem
## 客户端证书, 必须为对等证书
#TLSCertFile=/root/SSL/zabbix_agentd/zabbix_agent.pem
## 客户端私钥, 必须是对等证书
#TLSKeyFile=/root/SSL/zabbix_agentd/zabbix_agent-key.pem
###################################################################
EOF
}
function create_services() {
cat <<EOF >/etc/init.d/${service_name}
#!/bin/bash
function read_variable() {
readonly home_path="/usr/local/zabbix-7.0.1"
}
function build_variable() {
read_variable
readonly pid_file=\${home_path}/zabbix_agentd.pid
readonly log_file=\${home_path}/zabbix_agentd.log
readonly conf_path=\${home_path}/etc/zabbix_agentd.conf
readonly sbin_path=\${home_path}/sbin/zabbix_agentd
[ ! -d \$home_path ] && echo "path not found" && exit
[ ! -f \$conf_path ] && echo "conf not found" && exit
[ ! -f \$sbin_path ] && echo "sbin not found" && exit
cd \$home_path
}
function edit_server() {
build_variable
vim \$conf_path
}
function start_server() {
build_variable
echo "" >\$log_file
\$sbin_path -c \$conf_path
tail -f \$log_file
}
function stop_server() {
build_variable
echo \$pid_file
[ ! -f \$pid_file ] && echo "pid not found" && exit
cat \$pid_file | xargs kill
}
function restart_server() {
stop_server
start_server 2>/dev/null
}
case \$1 in
"start")
start_server
;;
"stop")
stop_server
;;
"restart")
restart_server
;;
"edit")
edit_server
;;
*)
echo "\$0 start|stop|restart"
;;
esac
EOF
}
function install_zabbix() {
build_variable
openssl version
if [ $? -ne 0 ]; then
yum install openssl-devel -y
fi
tar xf $package
cd $zip_dir
./configure --enable-agent -prefix=$install_path --with-openssl
make && make install
id zabbix
[ $? -ne 0 ] && useradd -s /sbin/nologin zabbix
touch $install_path\/zabbix_agentd.log
chown -R zabbix:zabbix $install_path
rewrite_conf # 重写配置文件
create_services # 创建服务脚本
echo
echo "install_success"
echo
chmod +x /etc/init.d/${service_name}
/etc/init.d/${service_name}
/etc/init.d/${service_name}
echo "use command edit conf: /etc/init.d/${service_name} edit"
}
case $1 in
"install")
install_zabbix
;;
*)
help1
;;
esac