1. 脚本介绍
1.1 使用场景
- Zabbix-Server为5.0版本(可通过修改脚本自定义)
- 操作系统版本为centos6、centos7、ubuntu14、ubuntu18、ubuntu20的Linux服务器(可自行修改脚本添加)
- 未配置zabbix-agent2的Linux主机自动化部署zabbix-agent2
- 集群节点批量自动部署zabbix-agent2
1.2 脚本功能
- 脚本自动识别Linux操作系统发行版,安装对应的Zabbix软件源并安装zabbix-agent2
- 支持操作系统发行版:centos6、centos7、ubuntu14、ubuntu18、ubuntu20(ubuntu14不支持agent2因此部署agent)
- 自动修改zabbix-agent2配置文件里的Server、ServerActive配置部分,即自动修改agent2被动模式和主动模式的Zabbix-Server服务器的IP地址
1.3 配置文件实例:
2. 使用方法
脚本名称:auto_deployment_zabbix_agent2.sh
2.1 直接运行脚本
./auto_deployment_zabbix_agent2.sh
脚本会使用默认配置的IP来修改zabbix-agent的配置文件,如需自定义,修改ZABBIX_SERVER_DEFAULT_HOST变量的值即可
2.2 带IP地址运行脚本
./auto_deployment_zabbix_agent2.sh 192.168.222.200
3. 运行案例
3.1 直接运行脚本
./auto_deployment_zabbix_agent2.sh
自动部署完zabbix-agent程序并修改配置Server的IP地址为脚本设置的ZABBIX_SERVER_DEFAULT_HOST变量值(192.168.222.137)
3.2 带IP地址运行脚本
./auto_deployment_zabbix_agent2.sh 192.168.222.200
自动部署完zabbix-agent程序并修改配置Server的IP地址为脚本运行时传入的IP地址
4. 脚本源代码
#!/bin/bash
OS_NAME=
OS_VERSION=
ZAABIX_CONFINA_NAME=()
ZABBIX_CONFIGNAME=
ZABBIX_COMPLETE_CONFIGNAME=
ZABBIX_SERVER_DEFAULT_HOST="192.168.222.137"
ZABBIX_SERVER_HOST=${1-$ZABBIX_SERVER_DEFAULT_HOST}
AGENT_NAME=
PARAMETER_SUM=$#
SCRIPTNAME=$0
parameter_judge(){
if [ $PARAMETER_SUM -ge 2 ];then echo "Usage:$0 zabbix_server_host_ip(option)" && exit ;fi
}
os_release_judge(){
local osname
local osname_result
osname=`sudo cat /proc/version`
if [[ $osname =~ "Red" ]];then osname_result="Centos";elif [[ $osname =~ "Ubuntu" ]];then osname_result="Ubuntu";fi
echo $osname_result
}
os_version_judge(){
local osversion_digits
osversion_digits=()
osversion_digits[0]=`sudo cat /etc/os-release | sed -nr 's/^VERSION_ID="?([0-9]+).?.*/\1/p'`
osversion_digits[1]=`sudo uname -a | awk '{print $14}'`
if [[ ${osversion_digits[1]} =~ "64" ]];then osversion_digits[1]="64";elif [[ ${osversion_digits[1]} =~ "32" ]];then osversion_digits[1]="32";fi
echo "${osversion_digits[@]}"
}
zabbix_agent_installed_judge(){
local os
os=$1
if [ -z $os ];then echo "OS_NAME is error" && exit; fi
case "$os" in
"Centos" ) centos::installed_judge ;;
"Ubuntu" ) ubuntu::installed_judge ;;
* ) echo "!!! No operating system distribution information was matched" ;;
esac
}
centos::installed_judge(){
local agent_installed_result
local zabbix_agent_ver
sudo rpm -qa | grep zabbix-agent &> /dev/null
agent_installed_result=`echo $?`
zabbix_agent_ver=`sudo rpm -qa | grep zabbix-agent | sed -r 's/\w+-(\w+2?)-.*/\1/'`
if [ $agent_installed_result -eq 0 ];then
echo ">> zabbix-$zabbix_agent_ver installed"
else
echo -e "\033[31m>> zabbix-agent未安装"'!!!!'"\033[0m"
centos::zabbix_agent_install ${OS_VERSION[0]}
fi
}
ubuntu::installed_judge(){
local agent_installed_result
local zabbix_agent_ver
sudo dpkg -s zabbix-agent &> /dev/null
agent_installed_result=`echo $?`
zabbix_agent_ver=`sudo apt-cache show zabbix-agent &> /dev/null | awk 'NR==1{print $2}'`
if [ $agent_installed_result -eq 0 ];then
echo ">> $zabbix_agent_ver installed"
else
echo -e "\033[0m>> zabbix-agent未安装"'!!!!'"\033[0m"
ubuntu::zabbix_agent_install ${OS_VERSION[0]}
fi
}
centos::zabbix_agent_install(){
local os_version
os_version=$1
echo -e "\n====== Starting to Install the Zabbix-agent ======"
if [ -z $os_version ];then echo ">> An error occurred during the installation of ZABBIx-Agent, not knowing the operating system distribution information" && exit 1;fi
case "$os_version" in
"6" )
if [ ! -d "/tmp" ];then sudo mkdir /tmp ;fi
cd /tmp
if [ ! -e "/etc/yum.repos.d/zabbix.repo" ];then
echo "------Start downloading the Zabbix software source-----"
sudo rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/6/x86_64/zabbix-release-5.0-1.el6.noarch.rpm &> /dev/null
if [ $? -ne 0 ];then echo "zabbix软件源下载失败,请检查!!!" && exit 1 ;fi
fi
sudo yum clean all &> /dev/null
echo "------The system starts to download the Zabbix-Agent2 software------"
echo ">> The Zabbix-agent2 is being installed. Please wait..."
sudo yum install -y zabbix-agent2 &> /dev/null
if [ $? -ne 0 ];then echo "yum安装zabbix-agent2下载失败,请检查!!!" && exit 1 ;else echo -e "\n>> zabbix-agent2 installed";fi
cd -
;;
"7" )
if [ ! -d "/tmp" ];then sudo mkdir /tmp ;fi
cd /tmp
if [ ! -e "/etc/yum.repos.d/zabbix.repo" ];then
echo "------Start downloading the Zabbix software source-----"
sudo rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm &> /dev/null
if [ $? -ne 0 ];then echo "zabbix软件源下载失败,请检查!!!" && exit 1 ;fi
fi
sudo yum clean all &> /dev/null
echo "------The system starts to download the Zabbix-Agent2 software------"
echo ">> The Zabbix-agent2 is being installed. Please wait..."
sudo yum install -y zabbix-agent2 &> /dev/null
if [ $? -ne 0 ];then echo "yum安装zabbix-agent2下载失败,请检查!!!" && exit 1 ;else echo -e "\n>> zabbix-agent2 installed";fi
cd -
;;
"*" ) echo "os_version=$os_version,无法检测对应操作系统信息!" ;;
esac
}
ubuntu::zabbix_agent_install(){
local os_version
os_version=$1
echo -e "\n====== Starting to Install the Zabbix-agent ======"
if [ -z $os_version ];then echo ">> An error occurred during the installation of ZABBIx-Agent, not knowing the operating system distribution information" && exit 1;fi
case "$os_version" in
"14" )
if [ ! -d "/tmp" ];then sudo mkdir /tmp ;fi
cd /tmp
echo "------Start downloading the Zabbix software source-----"
sudo wget --no-check-certificate https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1%2Btrusty_all.deb &> /dev/null
if [ $? -ne 0 ];then echo "zabbix软件源下载失败,请检查!!!" && exit 1;fi
echo "------The system starts to download the Zabbix-Agent2 software------"
echo ">> The Zabbix-agent is being installed. Please wait..."
sudo dpkg -i zabbix-release_5.0-1+trusty_all.deb &> /dev/null
sudo apt update &> /dev/null
sudo apt install -y zabbix-agent &> /dev/null
if [ $? -ne 0 ];then echo "apt安装zabbix-agent下载失败,请检查!!!" && exit 1 ;else echo -e "\n>> Zabbix-agent installed";fi
cd -
;;
"18" )
if [ ! -d "/tmp" ];then sudo mkdir /tmp ;fi
cd /tmp
echo "------Start downloading the Zabbix software source-----"
sudo wget --no-check-certificate https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1%2Bbionic_all.deb &> /dev/null
if [ $? -ne 0 ];then echo "zabbix软件源下载失败,请检查!!!" && exit 1;fi
echo "------The system starts to download the Zabbix-Agent2 software------"
echo ">> The Zabbix-agent2 is being installed. Please wait..."
sudo dpkg -i zabbix-release_5.0-1+bionic_all.deb &> /dev/null
sudo apt update &> /dev/null
sudo apt install -y zabbix-agent &> /dev/null
if [ $? -ne 0 ];then echo "apt安装zabbix-agent下载失败,请检查!!!" && exit 1 ;else echo -e "\n>> Zabbix-agent installed";fi
cd -
;;
"20" )
if [ ! -d "/tmp" ];then sudo mkdir /tmp ;fi
cd /tmp
echo "------Start downloading the Zabbix software source-----"
sudo wget --no-check-certificate https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1%2Bfocal_all.deb &> /dev/null
if [ $? -ne 0 ];then echo "zabbix软件源下载失败,请检查!!!" && exit 1;fi
echo "------The system starts to download the Zabbix-Agent2 software------"
echo ">> The Zabbix-agent2 is being installed. Please wait..."
sudo dpkg -i zabbix-release_5.0-1+focal_all.deb &> /dev/null
sudo apt update &> /dev/null
sudo apt install -y zabbix-agent2 &> /dev/null
if [ $? -ne 0 ];then echo "apt安装zabbix-agent下载失败,请检查!!!" && exit 1 ;else echo -e "\n>> Zabbix-agent installed";fi
cd -
;;
"*" ) echo "os_version=$os_version,无法检测对应操作系统信息!" ;;
esac
}
zabbix_agent_config_name_get(){
local congfigname_arr
local config_dir
local config_file_name
local config_file_num
congfigname_arr=( )
config_dir="/etc/zabbix"
if [ ! -d "$config_dir" ];then return 100 ;fi
config_file_num=$(sudo find /etc/zabbix/ -name "zabbix_agent*.conf"| wc -l)
if [ ! "$config_file_num" -eq 1 ];then return 200 ;fi
config_file_name=$(sudo find /etc/zabbix/ -name "zabbix_agent*.conf")
config_file_name=${config_file_name##/*/}
if [ ! -s `sudo find /etc/zabbix/ -name "zabbix_agent*.conf"` ];then return 44 ;fi
congfigname_arr[0]=`sudo find /etc/zabbix/ -name "zabbix_agent*.conf" | xargs basename `
congfigname_arr[1]=`sudo find /etc/zabbix/ -name "zabbix_agent*.conf"`
echo ${congfigname_arr[@]}
}
zabbix_config_mod(){
local config_name
local config_ipaddress_1
local config_ipaddress_2
config_name=(`echo $1`)
if [ -z ${config_name[0]} -o -z ${config_name[1]} ];then echo -e "\033[31m>> config_name参数在函数调用过程中出错,请检查zabbix_agent_config_name_get函数"'!!!'"\033[0m" && exit 1
else
config_ipaddress_1=`sudo egrep "^Server=[0-9]+.[0-9]+.[0-9]+.[0-9]+" ${config_name[1]} | awk -F"=" '{print $2}'`
if [ $? -ne 0 ];then echo -e "\033[31m>> 配置文件出错,请检查"'!!!'"\033[0m" && exit 1; fi
config_ipaddress_2=`sudo egrep "^ServerActive=[0-9]+.[0-9]+.[0-9]+.[0-9]+" ${config_name[1]} | awk -F"=" '{print $2}'`
if [ $? -ne 0 ];then echo -e "\033[31m>> 配置文件出错,请检查"'!!!'"\033[0m" && exit 1; fi
sudo sed -i '/^Server=/s/'"$config_ipaddress_1"'/'"$ZABBIX_SERVER_HOST"'/' ${config_name[1]}
if [ $? -ne 0 ];then echo -e "\033[31m>> 配置文件出错,请检查"'!!!'"\033[0m" && exit 1; fi
sudo sed -i '/^ServerActive=/s/'"$config_ipaddress_2"'/'"$ZABBIX_SERVER_HOST"'/' ${config_name[1]}
if [ $? -ne 0 ];then echo -e "\033[31m>> 配置文件出错,请检查"'!!!'"\033[0m" && exit 1; fi
fi
}
#centos-zabbix-agent服务控制
centos::zabbix_agent_run(){
sudo systemctl restart zabbix-agent2 &> /dev/null;if [ $? -ne 0 ];then echo -e "\033[31m>> 服务启动失败,请检查"'!!!'"\033[0m";fi
sudo systemctl enable zabbix-agent2 &> /dev/null;if [ $? -ne 0 ];then echo -e "\033[31m>> 服务启动失败,请检查"'!!!'"\033[0m";fi
}
#ubuntu-zabbix-agent服务控制
ubuntu::zabbix_agent_run(){
sudo service zabbix-agent restart &> /dev/null;if [ $? -ne 0 ];then echo -e "\033[31m>> 服务启动失败,请检查"'!!!'"\033[0m";fi
sudo update-rc.d zabbix-agent enable &> /dev/null;if [ $? -ne 0 ];then echo -e "\033[31m>> 服务启动失败,请检查"'!!!'"\033[0m";fi
}
#控制函数
main(){
clear
#1.调用传参函数进行位置参数判断
echo -e "====== start ${SCRIPTNAME##.*/} script ======"
parameter_judge
#2.调用Linux发行版判断函数将返回的结果赋值给全局变量OS_NAME
OS_NAME=`os_release_judge`
#3.调用Linux操作系统版本判断函数将返回的数组结果赋值给全局变量数组OS_VERSION
OS_VERSION=(`os_version_judge`)
echo ">> 当前的操作系统版本:$OS_NAME-${OS_VERSION[0]}-${OS_VERSION[1]}"
#4.输出当前操作系统环境Selinux和Firewall状态信息
echo -e "\n====== The system starts to check the operating system environment ======"
if [ $OS_NAME == "Centos" ];then
echo -e ">> Current system Time: $( date "+%Y-%m-%d %T")"
echo -e ">> Selinux State: $(sudo getenforce)"
echo -n ">> Firewall State:"
echo -e " $(sudo firewall-cmd --state)"
elif [ $OS_NAME == "Ubuntu" ];then
echo -e ">> Current system Time: $( date "+%Y-%m-%d %T")"
fi
#5.安装zabbix_agent
echo -e "\n====== The system starts checking the Zabbix-agent ======"
#将OS发行版信息传参给安装函数进行软件测试是否安装
zabbix_agent_installed_judge $OS_NAME
#if [ $? -ne 0 ];then exit 1;fi
#6.zabbix_agent_config_name_get函数检测配置文件是否正确存在
echo -e "\n====== The system starts to check the zabbix-agent configuration file ======"
zabbix_agent_config_name_get &> /dev/null
case "$?" in
100 ) echo -e "\033[31m>> /etc/zabbix配置文件目录不存在,请确认"'!!!'"\033[0m" && exit 1 ;;
200 ) echo -e "\033[31m>> zabbix-agent配置文件错误,请检查配置文件是否存在或存在多个配置文件"'!!!'"\033[0m" && exit 1 ;;
44 ) echo -e "\033[31m>> zabbix-agent配置文件异常,请检查配置文件是否正确"'!!!'"\033[0m" && exit 1 ;;
* ) ;;
esac
# if [ $? -eq 100 ];then
# echo -e "\033[31m>> /etc/zabbix配置文件目录不存在,请确认"'!!!'"\033[0m" && exit 1
# elif [ $? -eq 200 ];then
# echo -e "\033[31m>> zabbix-agent配置文件错误,请检查配置文件是否存在或存在多个配置文件"'!!!'"\033[0m" && exit 1
# elif [ $? -eq 300 ];then
# echo -e "\033[31m>> zabbix-agent配置文件异常,请检查配置文件是否正确"'!!!'"\033[0m" && exit 1
ZAABIX_CONFINA_NAME=(`zabbix_agent_config_name_get`)
ZABBIX_CONFIGNAME=${ZAABIX_CONFINA_NAME[0]}
ZABBIX_COMPLETE_CONFIGNAME=${ZAABIX_CONFINA_NAME[1]}
echo -e "\n>> config_file:\n${ZAABIX_CONFINA_NAME[0]}\nfile_path:\n${ZAABIX_CONFINA_NAME[1]}"
echo -e "\n====== The system starts to modify the Zabbix-agent configuration file ======"
zabbix_config_mod "${ZAABIX_CONFINA_NAME[*]}"
echo -e "\n>> ${ZAABIX_CONFINA_NAME[0]} Zabbix-Server HOST:"
egrep "^Server.*=[0-9]+.[0-9]+.[0-9]+.[0-9]+" ${ZAABIX_CONFINA_NAME[1]}
echo -e "\n====== Start Start the Zabbix-agent process and set it to start upon startup ======\n"
if [ $OS_NAME == "Centos" ];then centos::zabbix_agent_run && echo -e ">> The zabbix-agent process has been rstarted!";fi
if [ $OS_NAME == "Ubuntu" ];then ubuntu::zabbix_agent_run && echo -e ">> The zabbix-agent process has been rstarted!";fi
}
main
exit
欢迎交流学习~~~
|