zabbix全自动化监控

发布时间 2023-05-21 21:55:22作者: Thespace

zabbix全自动化监控



配置过程

思路

1、新的机器;
2、执行Ansible;
2.1、安装zabbixagent
2.2、配置好conf文件和脚本;
2.3、自动的被加入到监控项中,然后根据主机的角色关联不同的模板(Server事先配置好模板、和自动注册规则)

实验环境

主机名 公网ip地址 内网ip
zabbix 10.0.0.71
web01 10.0.0.7 172.16.1.7
web02 10.0.0.8 172.16.1.8
db01 10.0.0.51 172.16.1.51
redis 10.0.0.41 172.16.1.41

配置客户端

创建ansible脚本

[root@ansible ansible]# cat zabbix.yml 
- hosts: all 
  vars: 
    - Zabbix_Server_IP: 172.16.1.71
  tasks: 
  - name: install zabbixagent2 
    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: configure zabbixagent2.conf 
    template: 
      src: ./files/zabbix_agent2.conf.j2
      dest: /etc/zabbix/zabbix_agent2.conf  
    notify: restart zabbix agent2

  - name: configure zabbixagent2.conf userparameter 
    copy: 
      src: ./files/zabbix_agent2.d/
      dest: /etc/zabbix/zabbix_agent2.d/
    notify: restart zabbix agent2

  - name: systemd zabbixagent2
    systemd: 
      name: zabbix-agent2
      state: started
      enabled: yes

  handlers: 
  - name: restart zabbix agent2
    systemd: 
      name: zabbix-agent2 
      state: restarted

添加主机清单

[root@ansible ansible]# cat hosts 
[webserver]
172.16.1.7
172.16.1.8


[dbserver]
172.16.1.51
172.16.1.41

测试连通性

注意ansible与hosts端需要进行免密登录

[root@ansible ansible]# ansible all -m ping
172.16.1.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.1.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.1.41 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.16.1.51 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

执行脚本

[root@ansible ansible]# ansible-playbook zabbix.yml -i hosts

PLAY [all] ****************************************************************************************

TASK [Gathering Facts] ****************************************************************************
ok: [172.16.1.51]
ok: [172.16.1.41]
ok: [172.16.1.7]
ok: [172.16.1.8]

TASK [install zabbixagent2] ***********************************************************************
ok: [172.16.1.41]
ok: [172.16.1.51]
ok: [172.16.1.8]
ok: [172.16.1.7]

TASK [configure zabbixagent2.conf] ****************************************************************
ok: [172.16.1.51]
ok: [172.16.1.8]
ok: [172.16.1.41]
ok: [172.16.1.7]

TASK [configure zabbixagent2.conf userparameter] **************************************************
ok: [172.16.1.7]
ok: [172.16.1.8]
ok: [172.16.1.51]
ok: [172.16.1.41]

TASK [systemd zabbixagent2] ***********************************************************************
ok: [172.16.1.7]
ok: [172.16.1.41]
ok: [172.16.1.51]
ok: [172.16.1.8]

PLAY RECAP ****************************************************************************************
172.16.1.41                : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.51                : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.7                 : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.8                 : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

添加自动发现

配置–动作–自动注册动作–创建动作




效果测试