自动化运维——利用Saltstack一键部署zabbix监控平台

1. 案例简介

使用Saltstack部署zabbix,主要包括zabbix-server、zabbix-agent、zabbix-web以及mysql。其中,zabbix的版本使用4.4,使用本地自己搭建的zabbix仓库,加快部署效率。

两台虚拟机作为示例,分别是server2和server3。

部署前提:已经完成saltstack的部署(包括salt-master和salt-minion)。

2.部署过程

(1)物理机搭建zabbix软件仓库

[root@foundation8 zabbix]# ls
4.4
[root@foundation8 zabbix]# cd 4.4/
[root@foundation8 4.4]# ls
apache-tomcat-8.5.24.tar.gz
fping-3.10-1.el7.x86_64.rpm
jdk-8u121-linux-x64.rpm
oneitsm_zabbix_release-1.2.0.tar.gz
percona-zabbix-templates-1.1.8-1.noarch.rpm
php-5.4.16-46.el7.x86_64.rpm
php-bcmath-5.4.16-46.el7.x86_64.rpm
php-cli-5.4.16-46.el7.x86_64.rpm
php-common-5.4.16-46.el7.x86_64.rpm
php-gd-5.4.16-46.el7.x86_64.rpm
php-ldap-5.4.16-46.el7.x86_64.rpm
php-mbstring-5.4.16-46.el7.x86_64.rpm
php-mysql-5.4.16-46.el7.x86_64.rpm
php-pdo-5.4.16-46.el7.x86_64.rpm
php-xml-5.4.16-46.el7.x86_64.rpm
repodata
zabbix-agent-4.4.1-1.el7.x86_64.rpm
zabbix-get-4.4.1-1.el7.x86_64.rpm
zabbix-java-gateway-4.4.1-1.el7.x86_64.rpm
zabbix-proxy-mysql-4.4.1-1.el7.x86_64.rpm
zabbix-sender-4.4.1-1.el7.x86_64.rpm
zabbix-server-mysql-4.4.1-1.el7.x86_64.rpm
zabbix-web-4.4.1-1.el7.noarch.rpm
zabbix-web-mysql-4.4.1-1.el7.noarch.rpm
[root@foundation8 4.4]# pwd
/mnt/zabbix/4.4
[root@foundation8 4.4]# 

(2)整体目录结构

[root@server1 srv]# 
[root@server1 srv]# ls
pillar  salt
[root@server1 srv]# tree 
.
├── pillar
│   ├── top.sls
│   └── zabbix.sls
└── salt
    ├── mariadb
    │   ├── files
    │   │   └── create.sql.gz
    │   └── install.sls
    ├── repos
    │   └── zabbix.sls
    ├── top.sls
    ├── zabbix-agent
    │   ├── files
    │   │   └── zabbix_agentd.conf
    │   └── install.sls
    ├── zabbix-server
    │   ├── files
    │   │   └── zabbix_server.conf
    │   └── install.sls
    └── zabbix-web
        ├── files
        │   └── zabbix.conf
        └── install.sls

11 directories, 12 files

(3)仓库 repos目录

[root@server1 salt]# cd repos/
[root@server1 repos]# ls
zabbix.sls
[root@server1 repos]# vim zabbix.sls 
[root@server1 repos]# cat zabbix.sls 
zabbix:
  pkgrepo.managed:
    - baseurl: http://172.25.254.8/zabbix/4.4
    - gpgcheck: 0
[root@server1 repos]# 

(2)数据库mariadb 目录

[root@server1 salt]# cd mariadb/
[root@server1 mariadb]# ls
files  install.sls
[root@server1 mariadb]# cat install.sls 
db-install:
  pkg.installed:
    - pkgs:
      - mariadb-server
      - mariadb
      - MySQL-python


  service.running:
    - name: mariadb
 
  cmd.run:
    - name: mysql -e "DELETE FROM mysql.user WHERE User='';" && mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" && mysql -e "DROP DATABASE IF EXISTS test;" && mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" && mysql -e "FLUSH PRIVILEGES;"
    - onlyif: mysql -e "show databases;"

  mysql_user.present:
    - name: root
    - password: westos

db-configure:
  file.managed:
    - name: /mnt/create.sql.gz
    - source: salt://mariadb/files/create.sql.gz

  mysql_database.present:
    - name: zabbix
    - character_set: utf8
    - collate: utf8_bin
    - connection_user: root
    - connection_pass: westos

  mysql_user.present:
    - name: zabbix
    - host: localhost
    - password: westos
    - connection_user: root
    - connection_pass: westos

  mysql_grants.present:
    - grant: all privileges
    - database: zabbix.*
    - user: zabbix
    - connection_user: root
    - connection_pass: westos

  cmd.wait:
    - name: zcat /mnt/create.sql.gz | mysql -pwestos zabbix
    - watch:
      - mysql_database: db-configure

[root@server1 mariadb]#

files目录下为要导入的数据库
将zabbix数据导入到数据库中的文件 create.sql.gz

[root@server1 mariadb]# ls
files  install.sls
[root@server1 mariadb]# cd files/
[root@server1 files]# ls
create.sql.gz
[root@server1 files]# 

(3)top.sls文件内容,目录srv/salt/top.sls可以清晰的看出各主机作用以及安装和部署的包

[root@server1 salt]# vim top.sls 
[root@server1 salt]# cat top.sls 
base:
  'server2':
    - mariadb.install
    - zabbix-server.install
    - zabbix-web.install

  'server3':
    - zabbix-agent.install
[root@server1 salt]#

(4) zabbix-server的安装文件

[root@server1 zabbix-server]# ls
files  install.sls
[root@server1 zabbix-server]# cat install.sls 
include:
  - repos.zabbix

server-install:
  pkg.installed:
    - pkgs:
      - zabbix-server-mysql
      - zabbix-agent

  file.managed:
    - name: /etc/zabbix/zabbix_server.conf
    - source: salt://zabbix-server/files/zabbix_server.conf
    - template: jinja
    - context:
      dbpasswd: {{ pillar['DBPASSWD'] }}

  service.running:
    - name: zabbix-server
    - watch:
      - file: server-install

zabbix-agent:
  service.running
[root@server1 zabbix-server]#

jinja模版文件中修改

[root@server1 files]# vim zabbix_agentd.conf
 
 124 DBPassword={{ dbpasswd }}

(5)zabbix-agent的安装文件

[root@server1 zabbix-agent]# cat install.sls 
include:
  - repos.zabbix

agent-install:
  pkg.installed:
    - name: zabbix-agent

  file.managed:
    - name: /etc/zabbix/zabbix_agentd.conf
    - source: salt://zabbix-agent/files/zabbix_agentd.conf
    - template: jinja
    - context:
      zabbixserver: {{ pillar['ZABBIX-SERVER'] }}
      hostname: {{ grains['fqdn'] }}

  service.running:
    - name: zabbix-agent
    - watch:
      - file: agent-install
[root@server1 zabbix-agent]# 

模版文件中修改

 98 Server={{ zabbixserver }}
139 ServerActive={{ zabbixserver }}
150 Hostname={{ hostname }}

(6)web前端界面zabbix-web文件内容

[root@server1 zabbix-web]# cat install.sls 
include:
  - repos.zabbix

web-install:
  pkg.installed:
    - pkgs:
      - zabbix-web-mysql
      - zabbix-web
      - httpd
      - php
      - php-mysql

  file.managed:
    - name: /etc/httpd/conf.d/zabbix.conf
    - source: salt://zabbix-web/files/zabbix.conf

  service.running:
    - name: httpd
    - watch:
      - file: web-install
[root@server1 zabbix-web]# 

模版文件修改时区

20         php_value date.timezone Asia/Shanghai

(7)pillar目录下

[root@server1 pillar]# ls
top.sls  zabbix.sls
[root@server1 pillar]# cat top.sls 
base:
  '*':
    - zabbix
[root@server1 pillar]# cat zabbix.sls 
{% if grains['fqdn'] == 'server2' %}
DBPASSWD: westos
{% else %}
ZABBIX-SERVER: 172.25.0.2
{% endif %}
[root@server1 pillar]# 

至此,部署文件完成,这里以两台虚拟机作为示例,如果需要批量的添加,可以修改srv/salt/top.sls的内容即可。

3. 推送测试

[root@server1 pillar]# salt '*' state.highstate

浏览器输入访问:

自动化运维——利用Saltstack一键部署zabbix监控平台_第1张图片

安装登陆后,进监控页面

自动化运维——利用Saltstack一键部署zabbix监控平台_第2张图片
添加server3主机(zabbix-agent)

自动化运维——利用Saltstack一键部署zabbix监控平台_第3张图片

创建监控模板

自动化运维——利用Saltstack一键部署zabbix监控平台_第4张图片

完成监控主机添加

自动化运维——利用Saltstack一键部署zabbix监控平台_第5张图片

你可能感兴趣的:(saltstack)