zabbix部署
因为zabbix是用php语言开发的,所以必须先部署lamp架构,使其能够支持运行php网页
[root@master pillar]# tree
.
├── base
└── prod
├── apache.sls
├── mysql.sls
├── top.sls
└── zabbix.sls
[root@master prod]# cat top.sls
prod:
'node':
- apache
- zabbix
- mysql
[root@master pillar]# salt '*' pillar.items
node:
----------
data_dir:
/opt/data
extracted_dir:
/usr/src
install_dir:
/usr/local/apache
mysql_install_dir:
/usr/local
- salt://modules.web.apache.install
[root@master apache]# cat install.sls
"Development Tools":
pkg.group_installed
httpd-dep-packages:
pkg.installed:
- pkgs:
- openssl-devel
- pcre-devel
- expat-devel
- libtool
- gcc
- gcc-c++
- make
create-httpd-user:
user.present:
- name: apache
- shell: /sbin/nologin
- createhome: false
- system: true
copy-software:
file.managed:
- names:
- /usr/src/apr-1.7.0.tar.gz:
- source: salt://modules/web/apache/file/apr-1.7.0.tar.gz
- /usr/src/apr-util-1.6.1.tar.gz:
- source: salt://modules/web/apache/file/apr-util-1.6.1.tar.gz
- /usr/src/httpd-2.4.48.tar.gz:
- source: salt://modules/web/apache/file/httpd-2.4.48.tar.gz
httpd-install:
cmd.script:
- name: salt://modules/web/apache/file/install.sh {{ pillar['install_dir'] }}
- unless: test -d /usr/local/apache
{{ pillar['install_dir'] }}/conf/httpd.conf:
file.managed:
- source: salt://modules/web/apache/file/httpd.conf
- user: root
- group: root
- mode: '0644'
- require:
- cmd: httpd-install
/usr/lib/systemd/system/httpd.service:
file.managed:
- source: salt://modules/web/apache/file/httpd.service.j2
- user: root
- group: root
- mode: '0644'
- template: jinja
- salt://modules.web.apache.file
[root@master file]# pwd
/srv/salt/prod/modules/web/apache/file
[root@master file]# ls
apr-1.7.0.tar.gz httpd-2.4.48.tar.gz httpd.service.j2
apr-util-1.6.1.tar.gz httpd.conf install.sh
- salt://modules.database.mysql.install
[root@master mysql]# cat install.sls
dep-mysql-install:
pkg.installed:
- pkgs:
- ncurses-compat-libs
mysql:
user.present:
- system: true
- createhome: false
- shell: /sbin/nologin
{{ pillar['mysql_install_dir'] }}:
archive.extracted:
- source: salt://modules/database/mysql/files/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
file.symlink:
- name: {{ pillar['mysql_install_dir'] }}/mysql
- target: {{ pillar['mysql_install_dir'] }}/mysql-5.7.34-linux-glibc2.12-x86_64
{{ pillar['mysql_install_dir'] }}/mysql:
file.directory:
- user: mysql
- group: mysql
- mode: '0755'
- recurse:
- user
- group
{{ pillar['data_dir'] }}:
file.directory:
- user: mysql
- group: mysql
- mode: '0755'
- makedirs: true
- recurse:
- user
- group
/etc/profile.d/mysqld.sh:
file.managed:
- source: salt://modules/database/mysql/files/mysqld.sh.j2
- template: jinja
{{ pillar['mysql_install_dir'] }}/mysql/support-files/mysql.server:
file.managed:
- source: salt://modules/database/mysql/files/mysql.server
- user: mysql
- group: mysql
- mode: '0755'
/usr/lib/systemd/system/mysqld.service:
file.managed:
- source: salt://modules/database/mysql/files/mysqld.service.j2
- user: root
- group: root
- mode: '0644'
- template: jinja
mysql-initialize:
cmd.run:
- name: '{{ pillar['mysql_install_dir'] }}/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir={{ pillar['data_dir'] }}/'
- require:
- archive: {{ pillar['mysql_install_dir'] }}
- file: {{ pillar['data_dir'] }}
- user: mysql
- unless: test $(ls -l {{ pillar['data_dir'] }} |wc -l) -gt 1
- salt://modules.database.mysql.files
[root@master files]# pwd
/srv/salt/prod/modules/database/mysql/files
[root@master files]# ls
my.cnf.j2 mysqld.sh.j2
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz mysql.server
mysqld.service.j2
- salt://modules.application.php.install
[root@master php]# cat install.sls
/tmp/oniguruma-devel-6.8.2-2.el8.x86_64.rpm:
file.managed:
- source: salt://modules/application/php/files/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- user: root
- group: root
- mode: '0644'
cmd.run:
- name: yum -y install /tmp/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
- unless: rpm -q oniguruma-devel
php-dep-package:
pkg.installed:
- pkgs:
- libxml2
- libxml2-devel
- libsqlite3x-devel
- openssl
- openssl-devel
- bzip2
- bzip2-devel
- libcurl
- libcurl-devel
- libicu-devel
- libjpeg-turbo
- libjpeg-turbo-devel
- libpng
- libpng-devel
- openldap-devel
- pcre-devel
- freetype
- freetype-devel
- gmp
- gmp-devel
- libmcrypt
- libmcrypt-devel
- readline
- readline-devel
- libxslt
- libxslt-devel
- libzip
- libzip-devel
- mhash
- mhash-devel
- php-mysqlnd
- make
- gcc
- gcc-c++
/usr/src:
archive.extracted:
- source: salt://modules/application/php/files/php-7.4.24.tar.xz
php-install:
cmd.script:
- name: salt://modules/application/php/files/install.sh
- unless: test -d /usr/local/ph7
copy-file-php:
file.managed:
- names:
- /usr/local/php7/etc/php-fpm.conf:
- source: salt://modules/application/php/files/php-fpm.conf.default
- /usr/local/php7/etc/php-fpm.d/www.conf:
- source: salt://modules/application/php/files/www.conf.default
- /etc/php.ini:
- source: salt://modules/application/php/files/php.ini-production
- /etc/init.d/php-fpm:
- source: salt://modules/application/php/files/init.d.php-fpm
- user: root
- group: root
- mode: '0755'
- /usr/lib/systemd/system/php-fpm.service:
- source: salt://modules/application/php/files/php-fpm.service
- require:
- cmd: php-install
php-fpm.service:
service.running:
- enable: true
- reload: true
- require:
- cmd: php-install
- file: copy-file-php
- watch:
- file: copy-file-php
- salt://modules.application.php.files
[root@master files]# pwd
/srv/salt/prod/modules/application/php/files
[root@master files]# ls
init.d.php-fpm php-fpm.conf.default
install.sh php-fpm.service
oniguruma-devel-6.8.2-2.el8.x86_64.rpm php.ini-production
php-7.4.24.tar.xz www.conf.default
php-8.0.11.tar.xz
[root@master zabbix]# pwd
/srv/salt/prod/zabbix
[root@master zabbix]# ls
apache.sls files main.sls mysql.sls
[root@master zabbix]# cat apache.sls
include:
- modules.web.apache.install
/usr/include/httpd:
file.symlink:
- target: {{ pillar['install_dir'] }}/include
create-zabbix-dir:
file.directory:
- name: {{ pillar['install_dir'] }}/htdocs/zabbix
- user: root
- group: root
- mode: '0755'
- makedirs: true
{{ pillar['install_dir'] }}/conf/extra/vhosts.conf:
file.managed:
- source: salt://zabbix/files/vhosts.conf.j2
- user: root
- group: root
- mode: '0644'
- template: jinja
- require:
- cmd: httpd-install
{{ pillar['install_dir'] }}/htdocs/zabbix/index.php:
file.managed:
- source: salt://zabbix/files/index.php
- user: root
- group: root
- mode: '0644'
httpd.service:
service.running:
- enable: true
- reload: true
- require:
- cmd: httpd-install
- file: /usr/lib/systemd/system/httpd.service
- watch:
- file: {{ pillar['install_dir'] }}/conf/httpd.conf
- file: {{ pillar['install_dir'] }}/conf/extra/vhosts.conf
[root@master zabbix]# cat mysql.sls
include:
- modules.database.mysql.install
extend:
dep-mysql-install:
pkg.installed:
- pkgs:
- ncurses-devel
- openssl-devel
- openssl
- cmake
- mariadb-devel
{{ pillar['mysql_install_dir'] }}/include/mysql:
file.symlink:
- target: {{ pillar['mysql_install_dir'] }}/mysql/include
/etc/ld.so.conf.d/mysql.conf:
file.managed:
- source: salt://zabbix/files/mysql.conf.j2
- user: root
- group: root
- mode: '0644'
- template: jinja
/etc/my.cnf:
file.managed:
- source: salt://zabbix/files/my.cnf.j2
- user: root
- group: root
- mode: '0644'
- template: jinja
mysqld.service:
service.running:
- enable: true
- reload: true
- require:
- archive: {{ pillar['mysql_install_dir'] }}
- file: /usr/lib/systemd/system/mysqld.service
- file: /etc/my.cnf
- watch:
- file: /etc/my.cnf
set-password-mysql:
cmd.run:
- name: {{ pillar['mysql_install_dir'] }}/mysql/bin/mysql -e "set password = password('123456');"
- require:
- service: mysqld.service
- unless: {{ pillar['mysql_install_dir'] }}/mysql/bin/mysql -uroot -p123456 -e 'exit'
[root@master zabbix]# cat main.sls
include:
- zabbix.apache
- zabbix.mysql
- modules.application.php.install
zabbix-dep-package:
pkg.installed:
- pkgs:
- net-snmp-devel
- libevent-devel
zabbix-extracted:
archive.extracted:
- name: {{ pillar['extracted_dir'] }}
- source: salt://zabbix/files/zabbix-5.4.4.tar.gz
zabbix:
user.present:
- system: true
- createhome: false
- shell: /sbin/nologin
zabbix-install:
cmd.script:
- name: salt://zabbix/files/install.sh {{ pillar['extracted_dir'] }}
- unless: test $(ls -l /usr/local/etc |wc -l) -gt 1
/usr/local/etc/zabbix_server.conf:
file.managed:
- source: salt://zabbix/files/zabbix_server.conf
- user: root
- group: root
- mode: '0644'
- require:
- cmd: zabbix-install
/var/lib/mysql:
file.directory:
- user: root
- group: root
- mode: '0755'
- makedirs: true
/var/lib/mysql/mysql.sock:
file.symlink:
- target: /tmp/mysql.sock
/usr/local/apache/htdocs/zabbix:
file.recurse:
- source: salt://zabbix/files/zabbix
- include_empty: true
- require:
- cmd: httpd-install
/usr/local/apache/htdocs:
file.directory:
- user: apache
- group: apache
- mode: '0755'
- recurse:
- user
- group
- require:
- cmd: httpd-install
- user: create-httpd-user
/etc/php.ini:
file.managed:
- source: salt://zabbix/files/php.ini
- require:
- cmd: php-install
- file: copy-file-php
/usr/local/apache/htdocs/zabbix/conf:
file.directory:
- user: apache
- group: apache
- mode: '0777'
- require:
- cmd: httpd-install
php-fpm.service-watch:
service.running:
- name: php-fpm.service
- enable: true
- reload: true
- watch:
- file: copy-file-php
start-zabbix-server:
cmd.run:
- name: zabbix_server
- unless: ss -antl |grep 10051
start-zabbix-agentd:
cmd.run:
- name: zabbix_agentd
- unless: ss -antl |grep 10050
mysqld.service-watch:
service.running:
- name: mysqld.service
- enable: true
- reload: true
- watch:
- file: /etc/my.cnf
httpd.service-watch:
service.running:
- name: httpd.service
- enable: true
- reload: true
- watch:
- file: {{ pillar['install_dir'] }}/conf/httpd.conf
- file: {{ pillar['install_dir'] }}/conf/extra/vhosts.conf
[root@master zabbix]# salt '*' state.sls zabbix.main saltenv=prod
|