ansible批量管理zabbix

ansible批量管理zabbix

第1章 目录结构

1.功能说明
1.批量安装zabbix客户端
2.批量更新客户端配置文件
3.批量创建/更新/删除主机
4.批量创建/更新/删除组
5.批量创建监控项
6.自定义模版文件并导入

2.目录结构

角色目录:

[root@m01 ~]# tree /etc/ansible/roles/zabbix/
/etc/ansible/roles/zabbix/
├── create_group
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── create_host
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── del_host
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── get_groups
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── get_host
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── import_template
│ ├── files
│ │ └── zabbix_template.xml
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── init
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── zabbix.repo.j2
│ └── vars
├── update_conf
│ ├── files
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── zabbix_agentd.conf.j2
│ └── vars
└── update_item
├── files
│ ├── db
│ │ └── tcp_status.conf
│ └── web
│ └── tcp_status.conf
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
└── vars

执行脚本目录:

[root@m01 ~]# tree /etc/ansible/zabbix/
/etc/ansible/zabbix/
├── 01_init.yaml
├── 02_update_conf.yaml
├── 03_get_host.yaml
├── 04_create_host.yaml
├── 05_del_host.yaml
├── 06_create_groups.yaml
├── 07_get_groups
├── 08_import_template
├── 09_update_item.yaml
└── auto_template
├── auto_template.sh
├── item_list.txt
└── trigger_list.txt

3.主机清单

[root@m01 ~]# cat /etc/ansible/hosts
[zabbix_web]
172.16.1.12
172.16.1.13

[zabbix_db]
172.16.1.51
172.16.1.52

[zabbix_all:children]
zabbix_web
zabbix_db

[zabbix_web:vars]
groups_name=“web”
template_1=“Template OS Linux”
template_2=“TCP”

[zabbix_db:vars]
groups_name=“db”
template_1=“Template OS Linux”
template_2=“TCP”

[zabbix_server]
172.16.1.11

第2章 角色内容

1.初始化清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/init/tasks/main.yaml

  • name: 01_copy_repo
    template:
    src: zabbix.repo.j2
    dest: /etc/yum.repos.d/zabbix.repo

  • name: 02_Install_Zabbix-agent
    yum:
    name: zabbix-agent
    state: latest
    update_cache: yes

  • name: 03_Start_Zabbix-agent
    systemd:
    name: zabbix-agent
    state: started
    enabled: yes

2.创建组清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/create_group/tasks/main.yaml

  • name: Create host groups
    local_action:
    module: zabbix_group
    server_url: http://10.0.0.11/zabbix
    login_user: Admin
    login_password: zabbix
    state: present
    host_groups:
    - “{{ groups_name }}”

3.创建主机清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/create_host/tasks/main.yaml

  • name: 01_Create_host
    local_action:
    module: zabbix_host
    server_url: http://10.0.0.11/zabbix
    login_user: Admin
    login_password: zabbix
    host_name: “{{ ansible_nodename }}”
    visible_name: “{{ ansible_nodename }}”
    host_groups:
    - “{{ groups_name }}”
    link_templates:
    - “{{ template_1 }}”
    - “{{ template_2 }}”
    status: enabled
    state: present
    inventory_mode: automatic
    interfaces:
    - type: 1
    main: 1
    useip: 1
    ip: “{{ ansible_facts.eth1.ipv4.address }}”
    dns: “”
    port: 10050

4.删除主机清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/del_host/tasks/main.yaml

  • name: 01_del_host
    local_action:
    module: zabbix_host
    server_url: http://10.0.0.11/zabbix
    login_user: Admin
    host_name: “{{ ansible_nodename }}”
    login_password: zabbix
    state: absent

5.获取组列表清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/get_groups/tasks/main.yaml

  • name: get_groups
    local_action:
    module: zabbix_group_info
    server_url: http://10.0.0.11/zabbix
    login_user: Admin
    login_password: zabbix
    hostgroup_name:
    - “{{ groups_name }}”
    timeout: 10
    register: group_status

  • debug:
    msg: “{{ group_status.host_groups }}”

6.获取主机列表清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/get_host/tasks/main.yaml

  • name: Get host info
    local_action:
    module: zabbix_host_info
    server_url: http://10.0.0.11/zabbix
    login_user: Admin
    login_password: ‘zabbix’
    #host_name: Zabbix server
    host_ip: “{{ ansible_facts.eth1.ipv4.address }}”
    timeout: 10
    exact_match: no
    remove_duplicate: yes

7.导入模版清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/import_template/tasks/main.yaml

  • name: Import Zabbix templates from JSON
    local_action:
    module: zabbix_template
    server_url: http://10.0.0.11/zabbix
    login_user: Admin
    login_password: zabbix
    template_xml: “{{ lookup(‘file’, ‘zabbix_template.xml’)}}”
    state: present

8.更新配置文件清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/update_conf/tasks/main.yaml

  • name: 01_update_conf
    template:
    src: zabbix_agentd.conf.j2
    dest: /etc/zabbix/zabbix_agentd.conf
    notify:
    • restart zabbix-agent

9.更新监控项清单

[root@m01 ~]# cat /etc/ansible/roles/zabbix/update_item/tasks/main.yaml

  • name: 01_update_item_conf
    synchronize:
    src: “/etc/ansible/roles/zabbix/update_item/files/{{ groups_name }}/”
    dest: /etc/zabbix/zabbix_agentd.d
    notify:
    • restart zabbix-agent

第3章 自动生成模版文件

1.自动生成脚本

[root@m01 /etc/ansible/zabbix/auto_template]# cat auto_template.sh
#!/bin/bash

#1.定义变量
TIME=$(date +%FT%H:%M:%SZ)
DIR=/etc/ansible/roles/zabbix/import_template/files

#2.清空文件内容

${DIR}/zabbix_template.xml

#3.生成第一段固定内容
cat >${DIR}/zabbix_template.xml<<EOF

<?xml version="1.0" encoding="UTF-8"?>

<zabbix_export>
4.0
${TIME}


db




TCP
TCP



Linux servers




TCP


新的应用集


EOF

#4.循环生成监控项内容
for i in ( c a t i t e m l i s t . t x t ) d o c a t > > (cat item_list.txt) do cat >> (catitemlist.txt)docat>>{DIR}/zabbix_template.xml<<EOF

$(echo ${i}|awk -F"," '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲')</name> …(echo ${i}|awk -F"," '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')</key> …(echo ${i}|awk -F"," ‘{print $3}’)


新的应用集





<jmx_endpoint/>
3s

<query_fields/>

<status_codes>200</status_codes>
<follow_redirects>1</follow_redirects>
<post_type>0</post_type>
<http_proxy/>

<retrieve_mode>0</retrieve_mode>
<request_method>0</request_method>
<output_format>0</output_format>
<allow_traps>0</allow_traps>
<ssl_cert_file/>
<ssl_key_file/>
<ssl_key_password/>
<verify_peer>0</verify_peer>
<verify_host>0</verify_host>
<master_item/>

EOF
done

#5.生成固定格式
cat >>${DIR}/zabbix_template.xml<<EOF

<discovery_rules/>







EOF

#06.循环生成触发器
for i in ( c a t t r i g g e r l i s t . t x t ) d o c a t > > (cat trigger_list.txt) do cat >> (cattriggerlist.txt)docat>>{DIR}/zabbix_template.xml<<EOF

$(echo ${i}|awk -F"," '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲')</expression>…(echo ${i}|awk -F"," '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')</name> …(echo ${i}|awk -F"," ‘{print $3}’)

0
<manual_close>0</manual_close>



EOF
done

#07.生成固定格式
cat >>${DIR}/zabbix_template.xml<<EOF

</zabbix_export>
EOF

2.监控项清单

[root@m01 ~]# cat /etc/ansible/zabbix/auto_template/item_list.txt
TCP_LISTEN,TCP_STATUS[LISTEN],TCP
TCP_ESTABLISHED,TCP_STATUS[ESTABLISHED],TCP

3.触发器清单

[root@m01 ~]# cat /etc/ansible/zabbix/auto_template/trigger_list.txt
{TCP:TCP_STATUS[LISTEN].last()}=100,LISTEN连接数过多,2
{TCP:TCP_STATUS[ESTABLISHED].last()}=100,ESTABLISHED连接数过多,3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Zabbix 是一个企业级分布式开源监控解决方案。 Zabbix 软件能够监控众多网络参数和服务器的健康度、完整性。Zabbix 使用灵活的告警机制,允许用户为几乎任何事件配置基于邮件的告警。这样用户可以快速响应服务器问题。Zabbix 基于存储的数据提供出色的报表和数据可视化功能。 Zabbix 支持主动轮询(polling)和被动捕获(trapping)。Zabbix所有的报表、统计数据和配置参数都可以通过基于 Web 的前端页面进行访问。基于 Web 的前端页面确保您可以在任何地方访问您监控的网络状态和服务器健康状况。适当的配置后,Zabbix 可以在监控 IT 基础设施方面发挥重要作用。无论是对于有少量服务器的小型组织,还是拥有大量服务器的大企业而言,同样适用。 Zabbix 是免费的。Zabbix 是根据 GPL 通用公共许可证的第二版编写和发布的。这意味着产品源代码是免费发布的,可供公共使用。主要讲解8个主题:1.     通过SNMP防火墙设备的监控2.     通过SNMP交换机设备的监控3.     对Windows的性能、服务、用户登陆监控4.     对Linux的性能、用户登陆情况进行监控5.     生产中常用的服务进行监控,如nginx,httpd,mysql,ceph,bind等6.     Zabbix分布式的部署和监控7.     常见的zabbix三种报警方式8.     使用grafana对zabbix中的数据进行展示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值