vlambda博客
学习文章列表

技巧 | Zabbix_Server 迁移之 Agent 地址批量修改

【作者】广州乐维工程师 许远


背景需求:

某企业在测试环境上搭建了一套zabbix , 并且通过zabbix的自动注册功能已经将几百台机器包括Window和Linux纳入监控,现需要在生产环境上重新搭建多一套新的zabbix,需要把之前测试环境上添加的所有机器迁移到生产环境上,并且针对Linux操作系统需要在原有指标的基础上补充一些监控指标。

需求分析:

需求实现:

Linux操作系统

技巧 | Zabbix_Server 迁移之 Agent 地址批量修改
技巧 | Zabbix_Server 迁移之 Agent 地址批量修改
技巧 | Zabbix_Server 迁移之 Agent 地址批量修改

(2) 脚本执行结果

技巧 | Zabbix_Server 迁移之 Agent 地址批量修改

附l wrp.sh 脚本内容:(因部署环境不同,脚本的细节可能需要修改)

!/bin/bash

desc: used for replace zabbix client ip address

set -e

################################参数区#############################

baseshell=$(cd dirname $0; pwd)

Script="$(cd $baseshell; pwd)/script"

config="$(cd $baseshell; pwd)/etc"

BASEDIR="/usr/local/zabbix"

CONF_FILE="${BASEDIR}/etc/zabbix_agentd.conf"

IncludeDir="${BASEDIR}/etc/zabbix_agentd.conf.d"

LinuxVersion=$(cat /proc/version | awk '{print $3}' | awk -F '[.]' '{print $(NF-1)}')

Include="${BASEDIR}/etc/zabbix_agentd.conf.d/*.conf"

Agent_User="zabbix"

Agent_Server="192.168.1.105"

#############################命令执行#############################

function run() {

_cmd="${1}"

_debug="0"

_red="33[0;31m"

_green="33[0;32m"

_reset="33[0m"

_user="$(whoami)"

如果设置了第二个参数,可以开启调试模式

if [ "${#}" = "2" ];then

if [ "${2}" = "1" ];then

_debug="1"

fi

fi

if [ "${DEBUG_COMMANDS}" = "1" ] || [ "${_debug}" = "1" ];then

printf "${_red}%s $ ${_green}${_cmd}${_reset}n" "${_user}"

fi

sh -c "LANG=C LC_ALL=C ${_cmd}"

}

####################################区域分割线###################################

function parting(){

local string

if [ "$#" = 0 ];then

echo -e "33[34;1m" && printf "%40s" '='|tr ' ' '=' && echo -e "33[31m[null]c" && echo -e "33[34mc" && printf "%40sn" '='|tr ' ' '=' && echo -e "33[0m"

sleep 3

fi

if [ "$#" -ge 1 ];then

string=$*

echo -e "33[34;1m" && printf "%40s" '='|tr ' ' '=' && echo -e "33[31m[$string]c" && echo -e "33[34mc" && printf "%40sn" '='|tr ' ' '=' && echo -e "33[0m"

sleep 3

fi

}

###############################判断是否有监控脚本###################################

function scripts_check(){

parting "脚本检查"

iostat脚本检测

scripts_iostat="${BASEDIR}/scripts/iostat1.py"

if [ -f "${scripts_iostat}" ];then

echo "iostat脚本已存在"

else

run "cp ${Script}/iostat1.py ${BASEDIR}/scripts/"

echo "iostat脚本放置成功"

fi

swap脚本检查

scripts_swap="${BASEDIR}/scripts/swap.py"

if [ -f "${scripts_swap}" ];then

echo "交换分区脚本已存在"

else

run "cp ${Script}/swap.py ${BASEDIR}/scripts/"

echo "交换分区脚本放置成功"

fi

io_information脚本检查

scripts_io_inform="${BASEDIR}/scripts/io_information.sh"

if [ -f "${scripts_io_inform}" ];then

echo "IO信息脚本已存在"

else

run "cp ${Script}/io_information.sh ${BASEDIR}/scripts/"

echo "IO信息脚本已放置成功"

fi

cpu脚本检查

scripts_cpu_iowait="${BASEDIR}/scripts/cpu_iowait.sh"

if [ -f "${scripts_cpu_iowait}" ];then

echo "cpu_iowait脚本已存在"

else

run "cp ${Script}/cpu_iowait.sh ${BASEDIR}/scripts/"

echo "cpu_iowait脚本放置成功"

fi

Agent扩展配置文件检查

Agent_All_Conf="${IncludeDir}/all.conf"

if [ -f "$Agent_All_Conf" ];then

echo "Agent扩展配置文件已经存在"

else

run "cp ${config}/all.conf ${IncludeDir}/"

run "sed -i \"s@/usr/local/zabbix@${BASEDIR}@g\" ${IncludeDir}/all.conf"

echo "Agent扩展配置文件已放置成功"

fi

}

function agent_conf_check(){

判断配置文件

if [ -f "$CONF_FILE" ];then

echo "IO脚本已存在"

run "sed -i 's/^Server=.*$/Server=${Agent_Server}/' ${CONF_FILE}"

run "sed -i 's/^ServerActive=.*$/ServerActive=${Agent_Server}/' ${CONF_FILE}"

Agent_Include=cat ${CONF_FILE} |grep ^Include= | wc -l

if [ $Agent_Include -eq "0" ];then

run "sed -i \"s@# Include=@Include=${Include}@g\" grep Include= -rl ${CONF_FILE}"

echo -e "Agent的Include配置成功."

else

Agent_Include_t=cat ${CONF_FILE} | grep "^Include=${BASEDIR}/etc/zabbix_agentd.conf.d/*\.conf" | wc -l

if [ $Agent_Include_t -ne 1 ];then

echo "Include扩展模块需要重新修改!!!!!!"

exit 1

fi

fi

run "chmod -R 755 ${BASEDIR}"

run "chown -R ${Agent_User}.${Agent_User} ${BASEDIR}"

else

echo "Agent的配置文件不存,请重新修改脚本!!!"

exit 0

fi

}

###############################重启Zabbix_Agent##################################

function reload_agent(){

parting "重启Agent服务"

if [ ${LinuxVersion} = "el7" ];then

run "systemctl restart zabbix_agentd.service"

if [ $? -eq 0 ];then

echo -e "Agent服务重启success."

else

echo -e "Agent服务重启fail,请检查配置!!!"

fi

else

run "/etc/init.d/zabbix_agentd restart"

if [ $? -eq 0 ];then

echo -e "Agent服务重启success."

else

echo -e "Agent服务重启fail,请检查配置!!!"

fi

fi

}

case $1 in

install)

scripts_check;

agent_conf_check;

reload_agent
;;
*)
echo -e "33[31m usage: basename ${0} [install] 33[0m"
;;
esac
exit 0

Window操作系统

技巧 | Zabbix_Server 迁移之 Agent 地址批量修改

(2) 脚本执行的结果

技巧 | Zabbix_Server 迁移之 Agent 地址批量修改

附 window_agent.bat 脚本内容:(因部署环境不同,脚本的细节可能需要修改)

@echo off

cd C:Program FilesZABBIX_AGENT3conf

copy zabbix_agentd.win.conf zabbix_agentd.win.conf.bak

Set strOld=192.168.1.233

Set strNew=192.168.1.105

Set fn=zabbix_agentd.win.conf

setlocal enabledelayedexpansion

for /f "tokens=*" %%i in (%fn%) do (

set a=%%i

set "a=!a:%strOld%=%strNew%!"

echo !a!>>temp1010.txt)

echo 'replace success!!'

move /y temp1010.txt "%fn%"

echo stop zabbix services

net stop "Zabbix Agent"

echo start zabbix services

net start "Zabbix Agent"

pause

如有任何问题,可点击文末 阅读原文 ,到社区原文下评论交流
觉得本文有用,请转发、点赞或点击“在看”,让更多同行看到


 资料/文章推荐:



http://www.talkwithtrend.com/Topic/3937


下载 twt 社区客户端 APP

技巧 | Zabbix_Server 迁移之 Agent 地址批量修改

或到应用商店搜索“twt”