文章目录
saltstack部署zabbix监控
一 实验环境准备
实验环境:
server2 172.25.70.2 salt-master
server3 172.25.70.3 salt-minion zabbix-server
server4 172.25.70.4 salt-minion zabbix-agent
上述主机关闭火墙,selinux,可以连网
1.在真机中,将所需的zabbix的软件包放在/var/www/html/zabbix目录中,创建软件仓库
createrepo -v zabbix/
二 salt-master推送repo文件
cd /srv/salt
mkdir repos
cd repos/
vim init.sls
salt '*' state.sls repos #推送后minion端会生成repo文件
三 zabbix-server创建数据库
mkdir /srv/salt/zabbix-db
vim init.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://zabbix-db/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
进行推送
salt server3 state.sls zabbix-db
在server3中发现推过去的目录
三 为server3部署zabbix-server
mkdir /srv/salt/zabbix-server
vim /srv/salt/zabbix-server/init.sls
server-install:
pkg.installed:
- pkgs:
- zabbix-server-mysql
- zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_server.conf
- source: salt://zabbix-server/zabbix_server.conf
- template: jinja
- context:
dbpasswd: westos
service.running:
- name: zabbix-server
- watch:
- file: server-install
zabbix-agent:
service.running
修改zabbix-server配置文件:
vim /srv/salt/zabbix-server/zabbix_server.conf
修改DBPasswd:
DBPassword={{ dbpasswd }}
进行推送:
四 为server3和server4部署zabbix-agent
mkdir /srv/salt/zabbix-agent
cd /srv/salt/zabbix-agent
vim init.sls
agent-install:
pkg.installed:
- name: zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_agentd.conf
- source: salt://zabbix-agent/zabbix_agentd.conf
- template: jinja
- context:
{% if grains['fqdn'] == 'server3' %}
zabbixserver: 172.25.70.3
{% elif grains['fqdn'] == 'server4' %}
zabbixserver: 172.25.70.4
{% endif %}
hostname: {{ grains['fqdn'] }}
service.running:
- name: zabbix-agent
- watch:
- file: agent-install
修改配置文件
vim /srv/salt/zabbix-agent/zabbix_agentd.conf
尝试推送:
五 为server3部署zabbix-web界面
mkdir /srv/salt/zabbix-web
cd /srv/salt/zabbix-web
vim init.sls
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/zabbix.conf
service.running:
- name: httpd
- watch:
- file: web-install
编写配置文件,更改时区就可以
vim vim zabbix.conf
php_value date.timezone Asia/Shanghai
尝试推送
六 编写top文件
可以将上述的推送整合到top文件中,一键推送:
vim /srv/salt/top.sls
base:
'server3':
- zabbix-db
- zabbix-server
- zabbix-web
- zabbix-agent
'server4':
- zabbix-agent
尝试高级推
:
出现的问题:
这是因为之前操作安装了mariadb,设置了密码,可以将mariadb卸载删除后再尝试
安装了mariadb的server3上
systemctl stop mariadb
yum remove mariadb
rm -fr /var/lib/mysql
rm -fr /etc/my.cnf
再尝试:
七 测试
浏览器访问:(之前更改了httpd的端口)
进行操作: