Ansible+Zabbix-agent2快速实现对多主机监控

发布于:2025-06-10 ⋅ 阅读:(16) ⋅ 点赞:(0)

ansible

Ansible 是一款开源的自动化工具,用于配置管理(Configuration Management)、应用部署(Application Deployment)、任务自动化(Task Automation)和编排(Orchestration)。它通过声明式的方式(使用 YAML 格式的 Playbook)定义任务,帮助用户高效地管理大规模 IT 基础设施。

zabbix

Zabbix 是一款功能强大的开源企业级监控解决方案,主要用于实时监控IT基础设施(如服务器、网络设备、应用程序、云服务等)的性能和可用性,帮助管理员及时发现并解决问题,确保系统稳定运行。

实现功能

免密配置

ansible的使用,基于ssh的免密配置,因此使用循环的方式将所有节点配置免密登录

然后再ansible默认资源清单中配置主机

各个节点安装zabbix-agent

首先在所有配置zabbix的源仓库文件

然后再所有节点安装zabbix-agent2和zabbix-agent2的插件

发送配置文件,便于zabbix-agent自动注册

将修改完毕的配置文件进行发送,然后启动zabbix-agent2,

server端配置自动注册规则

在server端配置自动注册规则,保证注册节点可以自动被监控

依据不同服务配置自定义监控项

由于使用有MySQL节点,reids节点,nginx节点,lvs节点,java节点

因此根据不同的节点,发送不同脚本文件,实现不同应用的自定义监控

所用剧本

#生成ssh秘钥对
ssh-keygen
#配置域名解析
cat << EOF >> /etc/hosts
192.168.26.101 mysql-master 
192.168.26.102 mysql-replica
192.168.26.103 redis-cluster
192.168.26.104 java-host1
192.168.26.105 java-host2
192.168.26.106 lvs-iptables
192.168.26.107 nginx-host1
192.168.26.108 nginx-host2
192.168.26.109 zabbix-server
EOF
#对其他主机进行配置免密认证
for i in `cat /etc/hosts |grep -vw "localhost" | awk '{print $2}'`; do echo $i ;done
#安装ansible应用
dnf -y install epel-release && dnf -y install ansible
#配置资源清单
cat << EOF >> /etc/ansible/hosts
[mysql]
mysql-master
mysql-replica
[redis]
redis-cluster
[application]
java-host1
java-host2
[web]
nginx-host1
nginx-host2
[iptables]
lvs-iptables
[zabbix]
zabbix-server
EOF
#配置ansible的清单文件,在所有节点安装zabbix-agent2服务并开启
cat << EOF >> zabbix-setting.yaml
---
- hosts: all
  name: zabbix-agent-set
  user: root
  gather_facts: no
  vars:
    packages:
    - zabbix-agent2
    - zabbix-agent2-plugin-mongodb
    - zabbix-agent2-plugin-mssql
    - zabbix-agent2-plugin-postgresql
  tasks:
  - name: judge zabbix-agent yum repositry exists
    shell: cat /etc/yum.repos.d/zabbix.repo
    ignore_errors: yes
    register: repo_result

  - name: set zabbix-agent yum repository
    shell: rpm -Uvh https://repo.zabbix.com/zabbix/7.0/alma/9/x86_64/zabbix-release-latest-7.0.el9.noarch.rpm \           && dnf clean all
    when: repo_result.rc != 0

  - name: install zabbix-agent package and plugin
    dnf:
      name: "{{ item }}"
      state: installed
    loop: "{{ packages }}"

  - name: copy zabbix-agent configure
    copy:
      src: /root/zabbix_agent2.conf
      dest: /etc/zabbix/zabbix_agent2.conf

  - name: copy diy system minitor script
    copy:
      src: /root/system_os.sh
      dest: /etc/zabbix/zabbix_agent2.d/system_os.sh

  - name: copy diy minitor configure file
    copy:
      src: /root/system_os.conf
      dest: /etc/zabbix/zabbix_agent2.d/system_os.conf

  - name: copy mysql minitor script
    copy:
      src: /root/mysql_minitor.sh
      dest: /etc/zabbix/zabbix_agent2.d/mysql_minitor.sh
    when: "'mysql' in  group_names "

  - name: copy mysql minitor configure file
    copy:
      src: /root/mysql_minitor.conf
      dest: /etc/zabbix/zabbix_agent2.d/mysql_minitor.conf
    when: "'mysql' in group_names "

  - name: start zabbix-agent2 service
    service:
      name: zabbix-agent2
      state: restarted
      enabled: yes
#清单中所使用的zabbix-agent2配置文件,修改以下配置
#注释原有文件主机名
#Hostname=Zabbix server
#获取当前主机名
HostnameItem=system.hostname
# 获取当前主机元数据信息
HostMetadataItem=system.uname
#执行清单文件
ansible-playbook zabbix-setting.yaml