zabbix
作者:互联网
zabbix部署
清华大学下载
1.下载zabbix-agent (服务端)
[root@zabbix ~]# wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm
2.下载zabbix-server-mysql
[root@zabbix ~]# wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-server-mysql-4.0.0-2.el7.x86_64.rpm
3.下载zabbix-web
[root@zabbix ~]# wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-web-4.0.0-2.el7.noarch.rpm
下载zabbix-mysql
[root@zabbix ~]# wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-web-mysql-4.0.0-2.el7.noarch.rpm
官网下载
1.安装仓库
# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
# yum clean all
2.安装软件包
# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent
#一次不行就多试几次
mysql数据库jinjia模板
### mysql配置文件jinjia模板
vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
{% if ansible_fqdn == 'db01' %}
server_id = 1
{% else %}
server_id = 2
{% endif %}
log-bin=mysql-bin
character_set_server=utf8
skip_name_resolve
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
zabbix服务端连接数据库配置文件jinjia
[root@zabbix ~]# cat /etc/zabbix/zabbix_server.conf |egrep -v '^$|^#'
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost={{ ansible_default_ipv4 }}
DBName=zabbix
DBUser=zabbix
DBPassword=1
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
#DBHost只能是一个
zabbix客户端配置文件jinjia
[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.conf |egrep -v '^$|^#'
#可以被多个ip监控,中间用,隔开
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=172.16.1.71,10.0.0.71
ServerActive=172.16.1.71,10.0.0.71
Hostname={{ ansible_fqdn }}
Include=/etc/zabbix/zabbix_agentd.d/*.conf
http服务(appche)zabbix.conf server
[root@zabbix yum.repos.d]# vim /etc/httpd/conf.d/zabbix.conf
#
# Zabbix monitoring system php web frontend
#
Alias /zabbix /usr/share/zabbix
<Directory "/usr/share/zabbix">
Options FollowSymLinks
AllowOverride None
Require all granted
<IfModule mod_php5.c>
php_value max_execution_time 300
php_value memory_limit 128M
php_value post_max_size 16M
php_value upload_max_filesize 2M
php_value max_input_time 300
php_value max_input_vars 10000
php_value always_populate_raw_post_data -1
php_value date.timezone Asia/Shanghai
</IfModule>
</Directory>
<Directory "/usr/share/zabbix/conf">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/app">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/include">
Require all denied
</Directory>
<Directory "/usr/share/zabbix/local">
Require all denied
</Directory>
#####################################服务端
创建角色
[root@m01 roles]# ansible-galaxy init zabbix_server
编辑tasks目录
1.安装
[root@m01 zabbix_server]# vim tasks/install.yml
- name: copy zabbix_packages
copy:
src: "{{ item }}"
dest: "/tmp"
loop: "{{ zabbix }}"
- name: install zabbix_server zabbix_agent
yum:
name: "{{ zabbix_packages}}"
state: present
ignore_errors: yes
- name: check httpd
shell: "rpm -q httpd"
ignore_errors: yes
register: check_httpd
- name: install httpd
yum:
name: httpd
when: check_httpd.rc != 0
- name: check mariadb-server
shell: "rpm -q mariadb-server"
ignore_errors: yes
register: check_mariadb_server
- name: install mariadb-server
yum:
name: mariadb-server
when: check_mariadb_server.rc != 0
- name: check_mariadb_status
shell: "systemctl status mariadb"
ignore_errors: yes
register: check_mariadb_status
- name: start mariadb
systemd:
name: mariadb
state: started
enabled: yes
when: check_mariadb_status.rc != 0
2.拷贝
[root@m01 zabbix_server]# vim tasks/copy.yml
- name: copy my.cnf
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- { src: "my.cnf.j2",dest: "/etc/my.cnf" }
notify:
- "restart mariadb "
- name: copy httpd_conf.d_zabbix.conf.j2
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- { src: "httpd_conf.d_zabbix.conf.j2",dest: "/etc/httpd/conf.d/zabbix.conf" }
notify:
- "restart httpd"
- name: copy zabbix_server.conf
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
ignore_errors: yes
with_items:
- { src: "zabbix_server.conf.j2",dest: "/etc/zabbix/zabbix_server.conf" }
notify:
- "restart zabbix_server "
- name: copy zabbix_agentd.conf
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
ignore_errors: yes
with_items:
- { src: "zabbix_agent.conf.j2",dest: "/etc/zabbix/zabbix_agent.conf" }
notify:
- "restart zabbix_agent"
3.解压
[root@m01 zabbix_server]# vim tasks/jieya.yml
- name: jiaya create.sql.gz
unarchive:
src: "create.sql.gz"
dest: "/usr/share/doc/zabbix-server-mysql-4.0.1/"
copy: no
4.导入
[root@m01 zabbix_server]# vim tasks/target.yml
- name: import a database create.sql
mysql_db:
login_host: "localhost"
login_user: "root"
login_password: "1"
login_port: "3306"
name: "zabbix"
target: "/usr/share/doc/zabbix-server-mysql-4.0.1/create.sql"
state: "import"
5.启动
[root@m01 zabbix_server]# vim tasks/start.yml
- name: start zabbix_server zabbix_agent
systemd:
name: "{{item}}"
state: started
enabled: yes
with_items:
- "zabbix-server.service"
- "zabbix-agent.service"
6.include
[root@m01 zabbix_server]# vim tasks/main.yml
- include: install.yml
- include: copy.yml
- include: jieya.yml
- include: target.yml
- include: start.yml
7.编辑files目录
[root@m01 zabbix_server]# ll files/
httpd_conf.d_zabbix.conf.j2 zabbix-web-4.0.0-2.el7.noarch.rpm
zabbix-agent-4.0.0-2.el7.x86_64.rpm zabbix-web-mysql-4.0.0-2.el7.noarch.rpm
zabbix-server-mysql-4.0.0-2.el7.x86_64.rpm
8.编辑template目录
[root@m01 zabbix_server]# vim templates/
my.cnf.j2 zabbix_agent.conf.j2 zabbix_server.conf.j2
9.编辑触发器
[root@m01 zabbix_server]# vim handlers/main.yml
- name: restart mariadb
systemd:
name: "{{item}}"
state: restarted
with_items:
- "mariadb"
- name: restart httpd
systemd:
name: "{{item}}"
state: restarted
with_items:
- "httpd"
- name: restart zabbix_server
systemd:
name: "{{item}}"
state: restarted
with_items:
- "zabbix_server"
- name: restart zabbix_agent
systemd:
name: "{{item}}"
state: restarted
with_items:
- "zabbix_agent"
编辑入口文件
[root@m01 roles]# vim site.yml
- hosts: all
roles:
#- { role: base }
#- { role: rsync_client,when: ansible_fqdn is match 'web*' }
#- { role: rsync_client,when: ansible_fqdn is match 'nfs*' }
#- { role: rsync_server,when: ansible_fqdn is match 'backup*' }
#- { role: nfs_server,when: ansible_fqdn is match 'nfs*' }
#- { role: nfs_client,when: ansible_fqdn is match 'web*' }
#- { role: mount_server,when: ansible_fqdn is match 'nfs*' }
#- { role: mount_client,when: ansible_fqdn is match 'web*' }
#- { role: sersync,when: ansible_fqdn is match 'nfs' }
#- { role: nginx_web,when: ansible_fqdn is match 'web*' }
#- { role: nginx_lb,when: ansible_fqdn is match 'lb*' }
#- { role: keepalived_lb,when: ansible_fqdn is match 'lb*' }
#- { role: lb_ssl,when: ansible_fqdn is match 'lb*' }
#- { role: php,when: ansible_fqdn is match 'web*' }
#- { role: mariadb,when: ansible_fqdn is match 'db*' }
#- { role: wordpress,when: ansible_fqdn is match 'web*' }
#- { role: wecenter,when: ansible_fqdn is match 'web*' }
#- { role: mysql_master,when: ansible_fqdn is match 'db01' }
#- { role: mysql_slave,when: ansible_fqdn is match 'db02' }
- { role: zabbix_server,when: ansible_fqdn is match 'zabbix' }
##########################客户端
zabbix客户端配置文件jinjia
[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.conf |egrep -v '^$|^#'
#可以被多个ip监控,中间用,隔开
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=172.16.1.71,10.0.0.71
ServerActive=172.16.1.71,10.0.0.71
Hostname={{ ansible_fqdn }}
Include=/etc/zabbix/zabbix_agentd.d/*.conf
创建角色
[root@m01 roles]# ansible-galaxy init zabbix_client
编辑tasks目录
1.拷贝和安装
[root@m01 tasks]# vim copy.yml
- name: copy zabbix_agent packages
copy:
src: "zabbix-agent-4.0.0-2.el7.x86_64.rpm"
dest: "/tmp"
- include: install.yml
- name: copy zabbix_agent.conf.j2
template:
src: "zabbix_agent.conf.j2"
dest: "/etc/zabbix/zabbix_agent.conf"
notify:
- "restart zabbix_agent"
2.启动
[root@m01 zabbix_client]# vim tasks/start.yml
- name: start zabbix_agent
systemd:
name: "zabbix_agent"
state: started
enabled: yes
3.包含
[root@m01 zabbix_client]# vim tasks/main.yml
- include: copy.yml
- include: start.yml
5.触发器
[root@m01 zabbix_client]# vim handlers/main.yml
- name: restart zabbix_agent
systemd:
name: "zabbix_agent"
state: restarted
编辑入口文件
[root@m01 roles]# vim site.yml
- hosts: all
roles:
#- { role: base }
#- { role: rsync_client,when: ansible_fqdn is match 'web*' }
#- { role: rsync_client,when: ansible_fqdn is match 'nfs*' }
#- { role: rsync_server,when: ansible_fqdn is match 'backup*' }
#- { role: nfs_server,when: ansible_fqdn is match 'nfs*' }
#- { role: nfs_client,when: ansible_fqdn is match 'web*' }
#- { role: mount_server,when: ansible_fqdn is match 'nfs*' }
#- { role: mount_client,when: ansible_fqdn is match 'web*' }
#- { role: sersync,when: ansible_fqdn is match 'nfs' }
#- { role: nginx_web,when: ansible_fqdn is match 'web*' }
#- { role: nginx_lb,when: ansible_fqdn is match 'lb*' }
#- { role: keepalived_lb,when: ansible_fqdn is match 'lb*' }
#- { role: lb_ssl,when: ansible_fqdn is match 'lb*' }
#- { role: php,when: ansible_fqdn is match 'web*' }
#- { role: mariadb,when: ansible_fqdn is match 'db*' }
#- { role: wordpress,when: ansible_fqdn is match 'web*' }
#- { role: wecenter,when: ansible_fqdn is match 'web*' }
#- { role: mysql_master,when: ansible_fqdn is match 'db01' }
#- { role: mysql_slave,when: ansible_fqdn is match 'db02' }
- { role: zabbix_server,when: ansible_fqdn is match 'zabbix' }
- { role: zabbix_client,when: ansible_fqdn is match 'zabbix' }
执行
标签:when,fqdn,server,zabbix,ansible,role 来源: https://www.cnblogs.com/syy1757528181/p/13184838.html