一、简单介绍zabbix
Zabbix 是一个高度集成的网络监控解决方案,由一个国外的团队持续维护更新,软件可以自由下载使用。具备常见的商业监控软件所具备的功能(主机的性能监控、网络设备性能监控、数据库性能监控、FTP 等通用协议监控、多种告警方式、详细的报表图表绘制).Zabbix 通过 C/S 模式采集数据,通过 B/S 模式在 web 端展示和配置:
- Client(被监控)端: 主机通过安装 agent 方式采集数据,网络设备通过 SNMP 方式采集数据
- Server (服务)端: 通过收集 SNMP 和 agent 发送的数据,写入数据库再通过 php+apache 在 web 前端展示。
Zabbix功能
- 支持自动发现网络设备和服务器(可以通过配置自动发现服务器规则来实现)
- 支持自动发现(low discovery)key 实现动态监控项的批量监控(需写脚本)
- 支持分布式,能集中展示、管理分布式的监控点
- 扩展性强,server 提供通用接口(api 功能),可以自己开发完善各类监控(根据相关接口编写程序实现)
二、编写Playbook——Zabbix自动安装部署
主机名(ip) | 服务 |
---|---|
server1(172.25.2.1) | ansible、mariadb |
server2(172.25.2.2) | 被管控主机hosts、zabbix-server、zabbix-agent |
server3(172.25.2.3) | 被管控主机hosts、web前端 |
1、定义Inventory文件:vim /home/devops/ansible/hosts
2、创建zabbix目录、存放需要的配置文件: mkdir zabbix
3、编写YAML文件: vim deploy.yml
---
- hosts: mariadb
##对mariadb主机进行操作
tasks:
- name: install mariadb
yum:
name: mariadb-server,MySQL-python
state: present
- name: config mariadb
copy:
src: my.cnf #数据库安全初始化文件
dest: /etc/my.cnf
notify: restart mariadb
- name: start mariadb #开启数据库
service:
name: '{{ item }}' #引用变量
state: started
enabled: yes
loop:
- mariadb
- firewalld
- name: create database zabbix #创建数据库zabbix
mysql_db:
login_user: root
login_password: westos
name: zabbix
state: present
notify: import create.sql #使用触发器
- name: create user
mysql_user:
login_user: root
login_password: westos
name: zabbix
password: zabbix
host: '%' #%是匹配所有host的主机,即接收所有主机访问
priv: 'zabbix.*:ALL' #授权
state: present
- name: copy create.sql
copy:
src: create.sql.gz
dest: /tmp/create.sql.gz
- name: config firewalld #配置防火墙
firewalld:
service: mysql
permanent: yes
immediate: yes
state: enabled
handlers: #触发器
- name: restart mariadb #名称必须与notify一致
service:
name: mariadb
state: restarted
- name: import create.sql #将zabbix数据库初始化包导入数据库
mysql_db:
login_user: root
login_password: westos
name: zabbix
state: import
target: /tmp/create.sql.gz
- hosts: zabbix-server
tasks:
- name: add zabbix repo
yum_repository:
name: zabbix
description: zabbix 4.0
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: add update repo #添加版本较新的yum源,满足zabbix版本安装需求
yum_repository:
name: update
description: non-supported
baseurl: https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
gpgcheck: no
- name: install zabbix-server
yum:
name: zabbix-server-mysql,zabbix-agent
state: present
- name: config zabbix-server
copy:
src: zabbix_server.conf
dest: /etc/zabbix/zabbix_server.conf
owner: root
group: zabbix
mode: 640
notify: restart zabbix-server
- name: start zabbix-server
service:
name: "{{ item }}"
state: started
loop:
- zabbix-server
- zabbix-agent
- firewalld
- name: config firewalld
firewalld:
port: 10051/tcp #添加zabbix服务端口
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart zabbix-server
service:
name: zabbix-server
state: restarted
- hosts: zabbix-web
tasks:
- name: add zabbix repo
yum_repository:
name: zabbix
description: zabbix 4.0
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: add update repo
yum_repository:
name: update
description: non-supported
baseurl: https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
gpgcheck: no
- name: add centos repo
yum_repository:
name: centos
description: centos 7
baseurl: https://mirrors.aliyun.com/centos/7/os/x86_64/
gpgcheck: no
- name: install zabbix-web
yum:
name: zabbix-web-mysql
state: present
- name: config zabbix-web
copy:
src: zabbix.conf
dest: /etc/httpd/conf.d/zabbix.conf
notify: restart httpd
- name: start httpd
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: config firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: zabbix-agent
tasks:
- name: add zabbix repo
yum_repository:
name: zabbix
description: zabbix 4.0
baseurl: https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck: no
- name: install zabbix-agent
yum:
name: zabbix-agent
state: present
- name: config zabbix-agent
template:
src: zabbix_agentd.conf.j2
dest: /etc/zabbix/zabbix_agentd.conf
owner: root
group: root
mode: 644
notify: restart zabbix-agent
- name: start zabbix-agent
service:
name: "{{ item }}"
state: started
loop:
- zabbix-agent
- firewalld
- name: config firewalld
firewalld:
port: 10050/tcp
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart zabbix-agent
service:
name: zabbix-agent
state: restarted
上述配置文件/home/devops/ansible/zabbix/deploy.yml写到的文件来源:
-
数据库安全初始化文件 my.cnf
在server1上安装数据库mariadb-server,进行安全初始化,然后将生成的文件/eth/my.cnf拷贝到zabbix目录下。
-
数据库zabbix的初始化数据包:create.sql.gz
在/usr/share/doc/zabbix-server-mysql-4.0.5/下,就可以得到 create.sql.gz,再将它发送到devops下的server1 /home/devops/ansible/zabbix/。
- zabbix_server.conf配置文件
在server2上,将/etc/zabbix/zabbix_server.conf发到devops下的server1上,并修改i文件内容
91 BHost=172.25.2.1
116 DBUser=zabbix
124 DBPassword= zabbix
137 DBPort=3306
- zabbix.conf文件,将其复制到zabbix目录下,并修改时区
php_value date.timezone Asia/Shanghai
- zabbix_agentd.conf 文件,将其复制到zabbix目录下,并更改文件后缀,改为zabbix_agentd.conf.j2,并修改文件
Server=172.25.2.1
ServerActive=172.25.2.1
Hostname= {{ ansible_hostname }}
4、运行脚本
5、打开web界面搜索web主机: http://172.25.2.3/zabbix