「Zabbix」- 使用 Ansible 批量部署 zabbix-agent 服务 @20210226

问题描述

需要为内网 50.x 网络的多台 Linux 主机部署 zabbix 监控,工作内容如下:

安装 zabbix agent 服务

由于资产管理极度混乱,因此需要先辨别出 Linux 主机,排除 Windows/ESXi 主机
在安装 zabbix agent 服务时,需要辨别发行版(CentOS 6/7)

在 zabbix server 中添加主机

将主机添加到 zabbix server 中
并为主机添加监控模板

解决办法

安装 zabbix agent 服务

使用 Ansible 批量部署,避免重复执行安装任务。

在 zabbix server 中添加主机

使用 Zabbix 自动发现,避免重复执行配置工作。

注意事项

本笔记只是简述过程,用于记录操作流程,并非详细操作方法。场景不同需要具体问题需要具体分析。

环境概述

网络:10.10.50.0/25
主机:Linux / Windows / ESXi,但是我们只部署 Linux 监控
系统:CentOS 6.x / CentOS 7.x

第一步、配置自动发现(Active agent autoregistration)

参考 Active agent autoregistration 笔记进行配置

创建自动注册规则(Auto registration)

使用自动注册,需要添加规则:Event source => Auto registration

过滤自动注册主机(Conditions)

在该网络中,同时具有 Windows/Linux/ESXi 主机,我们只部署 Linux 监控,需要防止其他主机自动注册。

因此需要在 zabbix agent 中配置 HostMetadata 参数(添加 Linux 字符串),然后在 Conditions 中过滤(Host metadata contains Linux)。在此步骤中我需要们配置过滤条件,而配置 HostMetadata 参数放在下一步(配置 Zabbix Agent 服务)中完成。

扫描二维码关注公众号,回复: 12600494 查看本文章

添加自动注册操作(Operations)

根据需要在 Operations 中添加 Add host、Add to host groups、Link to templates 等等,以指定在出发自动注册时需要进行的动作。

第二步、部署 zabbix agent 服务

辨别 CentOS 主机,创建清单文件

使用 fping 命令找到全部存活主机(00-fping-scan.sh) => 获取内网所有 Linux 主机,以端口 22 开放为标准(01-port22-detecting.sh) => 通过测试 redhat-release 文件,获取内网所有 CentOS 主机(02-linux-detecting.sh) => 最后创建主机清单文件:

[all:vars]
ansible_user=root
ansible_ssh_private_key_file=config/ssh-private-key/id_rsa
ansible_ssh_common_args="-oStrictHostKeyChecking=no"

[centos]
# 省略主机列表......

执行 ansible centos -i inventory.txt -m ping 命令测试主机是否可以正常访问。

编写 Playbook 脚本

该 Playbook 脚本需要完成以下任务:
1)安装 Zabbix 仓库(注意区分 CentOS 6/7 发行版)
2)安装 zabbix agent 服务
3)部署配置文件:zabbix_agentd.conf(该配置文件只包含关键配置)
4)启动服务、开机启动(注意区分 CentOS 6/7 发行版)

完成服务部署

执行 Playbook 脚本,完成服务部署。

注意事项,建议使用某台主机进行测试,在完成测试之后再对全部目标主机应用 Playbook 脚本。防止由于失误而导致配置错误,而导致其他主机毁坏。

第三步、验证监控状态

访问 Zabbix 监控,查看主机是否已经全部自动注册。

参考文献

How do I make ssh fail rather than prompt for a password if the public-key authentication fails?
How to set host_key_checking=false in ansible inventory file?

猜你喜欢

转载自blog.csdn.net/u013670453/article/details/114147061