使用SaltStack自动化部署Zabbix监控平台(zabbix-server、zabbix-agent)

本文详细介绍了使用SaltStack进行Zabbix监控系统的自动化部署过程,包括Mariadb数据库的安装配置、Zabbix-server及Zabbix-agent的部署、web前端界面的搭建,以及通过模版文件实现关键配置项的动态修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

待完善…

目录结构如下

[root@server1 srv]# pwd
/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

各个文件的内容以及说明如下

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
      - mariadb-server
      - MySQL-python

  service.running:
    - name: mariadb-server

  cmd.run:
    - name: mysql -e "DELETE FROM mysql.user WHERE User='';" && "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" && "DROP DATABASE IF EXISTS test;" && "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" && "FLUSH PRIVILEGES;"       #根据mariadb的初始化脚本编写
    - onlyif : mysql -e "show databases;"     #只有在执行show databases不报错时才进行安全初始化 -e 表示执行

  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

在这里插入图片描述
在这里插入图片描述

注意:mariadb初始化脚本的查看方法:

[root@foundation11 ~]# which mysql_secure_installation 
/bin/mysql_secure_installation
[root@foundation11 ~]# vim /bin/mysql_secure_installation
  • files目录下为要导入的数据库。
[root@server1 mariadb]# ls
files  install.sls
[root@server1 mariadb]# ls files/
create.sql.gz

在这里插入图片描述
yum源的配置目录以及文件编写为如下:

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

在这里插入图片描述
top.sls文件内容为如下:

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

  'server2':
    - zabbix-agent.install    #客户端

zabbix-agent的安装文件为如下:

[root@server1 salt]# cd zabbix-agent/
[root@server1 zabbix-agent]# ls
files  install.sls
[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

模版文件中修改3处:

[root@server1 files]# vim zabbix_agentd.conf
 98 Server= {{ zabbixserver }}
139 ServerActive={{ zabbixserver }}
150 Hostname={{ hostname }}

zabbix-server的安装文件为如下:

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

server-install:
  pkg.installed:
    - 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

模版文件修改2处:

[root@server1 zabbix-server]# cd files/
[root@server1 files]# ls
zabbix_server.conf
[root@server1 files]# vim zabbix_server.conf 
124 DBPassword={{ dbpasswd }}

web前端界面zabbix-web内容如下:

[root@server1 salt]# cd zabbix-web/
[root@server1 zabbix-web]# ls
files  install.sls
[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 files]# ls
zabbix.conf
[root@server1 files]# vim zabbix.conf
 20         php_value date.timezone Asia/Shanghai

pillar目录下:

[root@server1 srv]# ls
pillar  salt
[root@server1 srv]# cd 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'] == 'server1' %}
DBPASSWD: westos
{% else %}
ZABBIX-SERVER: 172.25.11.1
{% endif %}

高级推:

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

完成后浏览器输入访问。

注意:salt-master与zabbix-server不要在同一台主机。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值