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

需求分析: 这里有几种方式实现,一是通过一键卸载脚本,结合自动化运维工具,将之前安装的agent批量删除,然后批量安装新的Agent部署包;二是直接修改zabbix_agentd.conf配置文件,将原来的Server地址替换为生产环境上的地址,然后添加进行自定义监控指标的添加,最后重启Agent。本文使用是第二种。

需求实现: Linux操作系统

(1)制作替换的脚本;操作步骤:一、把对应的新增指标相关脚本放置在对应的脚本目录,这里需要注意zabbix_agent配置文件是否开启Include参数并且指定对应的目录,二是修改zabbix_agentd.conf的配置文件,将Server参数和ActiveServer修改为生产环境的地址,三是重启zabbix_agentd。

(2)脚本执行结果 附lwrp.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="\033[0;31m"
_green="\033[0;32m"
_reset="\033[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 "\033[34;1m" && printf "%40s" '='|tr ' ' '=' && echo -e "\033[31m[null]\c" && echo -e "\033[34m\c" && printf "%40s\n" '='|tr ' ' '=' && echo -e "\033[0m" sleep 3 fi if [ "$#" -ge 1 ];then string=$* echo -e "\033[34;1m" && printf "%40s" '='|tr ' ' '=' && echo -e "\033[31m[$string]\c" && echo -e "\033[34m\c" && printf "%40s\n" '='|tr ' ' '=' && echo -e "\033[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

}

###############################Agent配置文件Server地址修改################################### function agent_conf_check(){ parting "Agent配置文件Server地址修改" #判断配置文件 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}" echo "Agent_Server的ip地址替换成功." 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 "\033[31m usage: basename ${0} [install] \033[0m" ;; esac exit 0

Window操作系统 (1)编写替换Server ip地址的BAT批处理文件;实现步骤:将原来的地址修改为生产环境的地址,然后重启zabbix_agentd (2)脚本执行的结果 附window_agent.bat脚本内容:(因部署环境不同,脚本的细节可能需要修改) @echo off

cd C:\Program Files\ZABBIX_AGENT3\conf 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