zabbix自动添加主机

zabbix分布式自动添加主机和模板

自动添加主机有两种方式

  1. 网络发现 ( Network discovery) server-agent server非常繁忙;
  2. 自动注册 ( Active agent auto-registration ) agent向sever发出添加主机请求

实验思路

zabbix自动添加主机

  1. 配置server端(为了让sever减轻负载配置proxy)
  2. 配置proxy端
  3. 配置agent端指向proxy端,然后proxy端向server发送数据
  4. 配置自动注册(网络发现对sever端有很大负载)添加模板

配置过程

实验环境

主机名公网ip地址内网ip
zabbix-server10.0.0.71
zabbix-proxy10.0.0.72172.16.1.72
zabbix-agent-01172.16.1.31
zabbix-agent-02172.16.1.32

配置proxy
安装zabbix-proxy mariadb 启动服务

[root@zabbix-proxy ~]# yum -y install mariadb-server mariadb https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-proxy-mysql-5.0.15-1.el7.x86_64.rpm
[root@zabbix-proxy ~]# systemctl enable --now mariadb
[root@zabbix-proxy ~]# systemctl enable --now zabbix-proxy.service

进入到数据库创建zabbix-proxy数据库

[root@zabbix-proxy ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 28
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database zabbix_proxy default charset utf8;
MariaDB [(none)]> grant all on zabbix_proxy.* to zabbix_proxy@'localhost' identified by 'zabbix_proxy';
[root@zabbix-proxy ~]# zcat /usr/share/doc/zabbix-proxy-mysql-5.0.15/schema.sql.gz | mysql -uzabbix_proxy -pzabbix_proxy zabbix_proxy

修改配置文件 server指向10.0.0.71

[root@zabbix-proxy ~]# vim /etc/zabbix/zabbix_proxy.conf
[root@zabbix-proxy ~]# grep '^[a-Z]' /etc/zabbix/zabbix_proxy.conf
Server=172.16.1.71
Hostname=Zabbix_proxy
LogFile=/var/log/zabbix/zabbix_proxy.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_proxy.pid
SocketDir=/var/run/zabbix
DBName=zabbix_proxy
DBUser=zabbix_proxy
DBPassword=zabbix_proxy
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=30
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
[root@zabbix-proxy ~]# 

配置agent客户端
用ansible部署agent
创建角色

[root@zabbix-server zabbix-agent2]# ansible-galaxy init zabbix-agent2
[root@zabbix-server ansible]# ls
ansible.cfg  zabbix-agent2   hosts        

添加主机清单

[root@zabbix-server ansible]# vim hosts
[zabbix-agent]
172.16.1.31
172.16.1.32

测试ansible能否访问客户端

[root@zabbix-server ansible]# ansible all -m ping
[WARNING]: Invalid characters were found in group names but not replaced, use
-vvvv to see details
172.16.1.31 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.1.32 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@zabbix-server ansible]# 

配置角色

[root@zabbix-server zabbix-agent2]# grep '^[a-Z]' templates/zabbix_agent2.conf.j2 
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server={{ zabbix_proxy }}
ServerActive={{ zabbix_proxy  }}
Hostname={{ ansible_hostname }}
Include=/etc/zabbix/zabbix_agent2.d/*.conf
ControlSocket=/tmp/agent.sock
[root@zabbix-server zabbix-agent2]# ls files/zabbix_agent2.d/ 
default.conf      nginx-status.sh  ssl-web.sh
LNMP_status.conf  php-status.sh
[root@zabbix-server zabbix-agent2]# 
[root@zabbix-server zabbix-agent2]# vim tasks/main.yml 
---
# tasks file for zabbix-agent2

- name: yum zabbix.agent2
  yum:
    name: https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent2-5.0.15-1.el7.x86_64.rpm
    state: present

- name: set zabbix-agent2.conf
  template:
    src: zabbix_agent2.conf.j2
    dest: /etc/zabbix/
    mode: '0644'

- name: cp userparameter
  copy:
    src: zabbix_agent2.d/
    dest: /etc/zabbix/zabbix.agent2.d/

- name: status zabbix
  service:
    name: zabbix-agent2
    state: started
    enabled: yes

设置变量

[root@localhost ansible]# vim zabbix-agent2/vars/main.yml
---
# vars file for zabbix-agent2
zabbix_proxy: 172.16.1.72

调用角色

[root@zabbix-server ansible]# cat zabbix.yml 
- hosts: zabbix-agent
  roles: 
    - zabbix-agent2

执行脚本

[root@localhost ansible]# ansible-playbook zabbix.yml 
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [zabbix-agent] *********************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.32]

TASK [zabbix-agent2 : yum zabbix.agent2] ************************************************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.32]

TASK [set zabbix-agent2.conf] ***********************************************************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.32]

TASK [zabbix-agent2 : cp userparameter] *************************************************************************************************************
ok: [172.16.1.31]
ok: [172.16.1.32]

TASK [zabbix-agent2 : status zabbix] ****************************************************************************************************************
changed: [172.16.1.31]
changed: [172.16.1.32]

PLAY RECAP ******************************************************************************************************************************************
172.16.1.31                : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.32                : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ansible]# 

添加zabbix-proxy代理
在这里插入图片描述
添加自动发现 配置–动作–自动注册动作–创建动作
在这里插入图片描述
在这里插入图片描述
效果测试
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值