编译安装zabbix-agent脚本

编译安装zabbix-agent脚本

#!/bin/bash

#********************************************************************
# File Name: zabbix-agent.sh
# Version: V1.0
# Author: dahuangji
# Email: 
# Created Time : 2022-02-20 00:55:11
# Description:
#********************************************************************

RED='\E[31;2m'
GREEN='\E[32;1m'
END='\E[0m'

. /etc/init.d/functions &>/dev/null
OS=$(awk -F'=|"| +|\\.' 'NR==1{print $3}' /etc/os-release 2>/dev/null)
VS=$(awk -F'=|"| +|\\.' 'NR==2{print $3}' /etc/os-release 2>/dev/null)
PFX_DIR='/opt/zabbix_agent'
HNAME=$(ifconfig|awk 'NR==2{print $2}')
SER=2.2.2.42
SER_ACT=2.2.2.42,2.2.2.32
ZBX_VS=zabbix-4.0.36
SCP=$(ls /opt/*.{sh,py} 2>/dev/null)
set -eo pipefail

conf_init() {
	cat > ${PFX_DIR}/etc/zabbix_agentd.conf <<-eof
PidFile=${PFX_DIR}/run/zabbix_agentd.pid
LogFile=${PFX_DIR}/log/zabbix_agentd.log
LogFileSize=1
EnableRemoteCommands=1
LogRemoteCommands=1
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=5
Timeout=30
Server=$SER
ServerActive=$SER_ACT
Hostname=$HNAME
Include=${PFX_DIR}/etc/zabbix_agentd.conf.d/*.conf
UnsafeUserParameters=1
eof
	cat > ${PFX_DIR}/etc/zabbix_agentd.conf.d/monitor.conf <<-eof
meter=tcp_status[*],bash ${PFX_DIR}/etc/zabbix_agentd.conf.d/linux_status.sh "\$1"
meter=nginx.status[*],bash ${PFX_DIR}/etc/zabbix_agentd.conf.d/nginx.sh "\$1" "\$2"
meter=redis_status[*],bash ${PFX_DIR}/etc/zabbix_agentd.conf.d/redis.sh "\$1" "\$2"
eof
	cat > /etc/systemd/system/zabbix-agent.service <<-eof
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=${PFX_DIR}/etc/zabbix_agentd.conf"
Type=simple
Restart=on-failure
PIDFile=${PFX_DIR}/run/zabbix_agent.pid
KillMode=control-group
ExecStart=${PFX_DIR}/sbin/zabbix_agentd -c \$CONFFILE -f
ExecStop=/bin/sh -c '[ -n "\$1" ] && kill -s TERM "\$1"' -- "\$MAINPID"
RestartSec=10s
User=zabbix
Group=zabbix

[Install]
WantedBy=multi-user.target
eof
}

copy_conf(){
	for i in $SCP ;do
		if [[ $i != $0 ]]&&[[ -n $i ]] ;then
			\cp ${i} ${PFX_DIR}/etc/zabbix_agentd.conf.d/ &&echo -e "\e[32;1m 监控脚本准备成功:$i\e[0m"
		else
			echo '当前没有脚本文件'
			break
		fi
	done
}

ctos8() {
	yum install -y vim iotop bc gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel zip unzip zlib-devel net-tools lrzsz tree telnet lsof tcpdump wget libevent libevent-devel bc systemd-devel bash-completion traceroute make &> /dev/null
	tar xf ${ZBX_VS}.tar.gz	-C /opt/
	cd /opt/$ZBX_VS
	./configure --prefix=/opt/zabbix_agent --enable-agent > /dev/null
	make >/dev/null&& make install >/dev/null &&action "zabbix-agent安装成功"
	mkdir /opt/zabbix_agent/{log,run}
	chown -R zabbix.zabbix /opt/zabbix_agent
}

ubuntu18() {
	apt-get  -y install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree  openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute  gcc openssh-server lrzsz tree  openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute iotop unzip zip make &> /dev/null
	tar xf ${ZBX_VS}.tar.gz -C /opt/
	cd $ZBX_VS
	./configure --prefix=/opt/zabbix_agent --enable-agent > /dev/null
	make > /dev/null && make install >/dev/null && echo -e '\e[32;1m zabbix-agent安装成功\e[0m'
}

clean_file() {
	cd /opt
	rm -rf ${ZBX_VS}*
}

case $OS in 
	CentOS)
		case $VS in 
		 	8)
				echo 'agent正在安装,请等待'
				echo '----------------------'
				ctos8
				conf_init
				copy_conf
				clean_file
				;;
			*)
				echo "未适配此系统"
				wait
				;;
		esac
		;;
	Ubuntu)
		case $VS in 
			18)	
				echo 'agent正在安装,请等待'
				echo '----------------------'
				ubuntu18
				conf_init
				copy_conf
				clean_file
				;;
			*)
				echo "未适配此系统"
				;;
		esac
		;;
	*)
		echo "未识别此系统"
esac
				

posted @ 2022-02-20 14:11  suyanhj  阅读(91)  评论(0编辑  收藏  举报