https://blog.csdn.net/weixin_56892849/article/details/118859996
server1 作为saltstack服务器,server2用来做zabbix-server(数据库mariadb), server3用来做zabbix-agent
[kiosk@foundation12 4.0]$ cd /var/www/html/
[kiosk@foundation12 html]$ ls
3000 4.0 apache-tomcat-7.0.90.tar.gz nginx-1.20.1.tar.gz rhel7.6
[kiosk@foundation12 html]$ cd 4.0/
[kiosk@foundation12 4.0]$ ls
fping-3.10-1.el7.x86_64.rpm
iksemel-1.4-2.el7.centos.x86_64.rpm
php-5.4.16-46.el7.x86_64.rpm
php-bcmath-5.4.16-42.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-42.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.0.5-1.el7.x86_64.rpm
zabbix-get-4.0.5-1.el7.x86_64.rpm
zabbix-java-gateway-4.0.5-1.el7.x86_64.rpm
zabbix-proxy-mysql-4.0.5-1.el7.x86_64.rpm
zabbix-sender-4.0.5-1.el7.x86_64.rpm
zabbix-server-mysql-4.0.5-1.el7.x86_64.rpm
zabbix-web-4.0.5-1.el7.noarch.rpm
zabbix-web-mysql-4.0.5-1.el7.noarch.rpm
[kiosk@foundation12 4.0]$
[[root@server1 yum.repos.d]# ls
dvd.repo redhat.repo salt.repo zabbix.repo
[root@server1 yum.repos.d]# pwd
/etc/yum.repos.d
[root@server1 yum.repos.d]# cat zabbix.repo
[zabbix]
name=zabbix
baseurl=http://172.25.12.250/4.0
gpgcheck=0
[root@server1 yum.repos.d]#
[root@server1 ~]# yum install -y zabbix-*
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# ls
apache _grains keepalived mariadb _modules nginx top.sls
[root@server1 salt]# mkdir zabbix/files -p
[root@server1 salt]# cd zabbix/files/
[root@server1 salt]# mkdir zabbix/files -p
[root@server1 salt]# cd zabbix/files/
[root@server1 files]# vim zabbix_agentd.conf
98 Server=172.25.12.2
139 ServerActive=172.25.12.2
150 Hostname=server3
[root@server1 zabbix]# cat install-zabbix-agent.sls
install-zabbix-agent:
pkg.installed:
- pkgs:
- zabbix-agent
file.managed:
- name: /etc/zabbix/zabbix_agentd.conf
- source: salt://zabbix/files/zabbix_agentd.conf
- template: jinja
- context:
ZABBIX_SERVER: 172.25.12.2
ZABBIX_ACTIVE_SERVER: 172.25.12.2
AGENT_HOSTNAME: server3
service.running:
- name: zabbix-agent
- watch:
- file: install-zabbix-agent
[root@server1 zabbix]#
[root@server1 salt]# vim top.sls
[root@server1 salt]# cat top.sls
base:
'server2':
- zabbix.install-zabbix-server
'server3':
- zabbix.install-zabbix-agent
[root@server1 salt]#
[root@server1 salt]# salt server3 state.sls zabbix.install-zabbix-agent
server3:
----------
ID: install-zabbix-agent
Function: pkg.installed
Result: True
Comment: The following packages were installed/updated: zabbix-agent
Started: 03:40:34.665403
Duration: 4266.707 ms
Changes:
----------
zabbix-agent:
----------
new:
4.0.5-1.el7
old:
----------
ID: install-zabbix-agent
Function: file.managed
Name: /etc/zabbix/zabbix_agentd.conf
Result: True
Comment: File /etc/zabbix/zabbix_agentd.conf updated
Started: 03:40:38.962356
Duration: 139.613 ms
Changes:
----------
diff:
---
+++
@@ -95,7 +95,7 @@
# Default:
# Server=
-Server=127.0.0.1
+Server=172.25.12.2
### Option: ListenPort
# Agent will listen on this port for connections from the server.
@@ -136,7 +136,7 @@
# Default:
# ServerActive=
-ServerActive=127.0.0.1
+ServerActive=172.25.12.2
### Option: Hostname
# Unique, case sensitive hostname.
@@ -147,7 +147,7 @@
# Default:
# Hostname=
-Hostname=Zabbix server
+Hostname=server3
### Option: HostnameItem
# Item used for generating Hostname if it is undefined. Ignored if Hostname is defined.
----------
ID: install-zabbix-agent
Function: service.running
Name: zabbix-agent
Result: True
Comment: Started Service zabbix-agent
Started: 03:40:39.111574
Duration: 110.851 ms
Changes:
----------
zabbix-agent:
True
Summary for server3
------------
Succeeded: 3 (changed=3)
Failed: 0
------------
Total states run: 3
Total run time: 4.517 s
[root@server1 salt]#
[root@server3 ~]# systemctl status zabbix-agent.service
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2021-07-19 03:40:39 EDT; 2min 42s ago
Process: 3862 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
Main PID: 3864 (zabbix_agentd)
CGroup: /system.slice/zabbix-agent.service
├─3864 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.co...
├─3865 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
├─3866 /usr/sbin/zabbix_agentd: listener #1 [waiting for conne...
├─3867 /usr/sbin/zabbix_agentd: listener #2 [waiting for conne...
├─3868 /usr/sbin/zabbix_agentd: listener #3 [waiting for conne...
└─3869 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Jul 19 03:40:39 server3 systemd[1]: Starting Zabbix Agent...
Jul 19 03:40:39 server3 systemd[1]: PID file /run/zabbix/zabbix_agentd.p....
Jul 19 03:40:39 server3 systemd[1]: Started Zabbix Agent.
Hint: Some lines were ellipsized, use -l to show in full.
[root@server3 ~]#
[root@server1 zabbix]# ls
files install-zabbix-agent.sls
[root@server1 zabbix]# mkdir mariadb
[root@server1 zabbix]# cd mariadb/
[root@server1 mariadb]# vim install.sls
[root@server1 mariadb]# cat install.sls
install-mariadb:
pkg.installed:
- pkgs:
- mariadb-server
service.running:
- name: mariadb
[root@server1 mariadb]#
[root@server1 salt]# cat top.sls
base:
'server2':
- zabbix.install-zabbix-server
- mariadb.install
'server3':
- zabbix.install-zabbix-agent
[root@server1 mariadb]# salt server2 state.sls mariadb.install
server2:
----------
ID: install-mariadb
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 04:19:16.874259
Duration: 614.906 ms
Changes:
----------
ID: install-mariadb
Function: service.running
Name: mariadb
Result: True
Comment: The service mariadb is already running
Started: 04:19:17.490194
Duration: 47.647 ms
Changes:
Summary for server2
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 662.553 ms
[root@server1 mariadb]# salt server2 state.sls mariadb.install
server2:
----------
ID: install-mariadb
Function: pkg.installed
Result: True
Comment: The following packages were installed/updated: mariadb-server
Started: 04:22:48.881134
Duration: 7625.014 ms
Changes:
----------
mariadb:
----------
new:
1:5.5.60-1.el7_5
old:
mariadb-libs:
----------
new:
1:5.5.60-1.el7_5
old:
mariadb-server:
----------
new:
1:5.5.60-1.el7_5
old:
perl-DBD-MySQL:
----------
new:
4.023-6.el7
old:
----------
ID: install-mariadb
Function: service.running
Name: mariadb
Result: True
Comment: Started Service mariadb
Started: 04:22:56.525569
Duration: 3503.565 ms
Changes:
----------
mariadb:
True
Summary for server2
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 11.129 s
[root@server1 mariadb]#
[root@server2 ~]# systemctl status mariadb.service
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2021-07-19 04:22:59 EDT; 3min 37s ago
Process: 5057 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 4978 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 5056 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─5056 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─5218 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/m...
Jul 19 04:22:57 server2 mariadb-prepare-db-dir[4978]: MySQL manual for mo...
Jul 19 04:22:57 server2 mariadb-prepare-db-dir[4978]: Please report any p...
Jul 19 04:22:57 server2 mariadb-prepare-db-dir[4978]: The latest informat...
Jul 19 04:22:57 server2 mariadb-prepare-db-dir[4978]: You can find additi...
Jul 19 04:22:57 server2 mariadb-prepare-db-dir[4978]: http://dev.mysql.com
Jul 19 04:22:57 server2 mariadb-prepare-db-dir[4978]: Consider joining Ma...
Jul 19 04:22:57 server2 mariadb-prepare-db-dir[4978]: https://mariadb.org...
Jul 19 04:22:58 server2 mysqld_safe[5056]: 210719 04:22:58 mysqld_safe L....
Jul 19 04:22:58 server2 mysqld_safe[5056]: 210719 04:22:58 mysqld_safe S...l
Jul 19 04:22:59 server2 systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@server2 ~]#
[root@server2 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n]
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@server2 ~]#
[root@server2 ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'westos';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
[root@server1 files]# ls
zabbix_agentd.conf
[root@server1 files]# cp /etc/zabbix/zabbix_server.conf .
[root@server1 files]# cp /etc/httpd/conf.d/zabbix.conf .
[root@server1 files]# ls
zabbix_agentd.conf zabbix.conf zabbix_server.conf
[root@server1 files]# vim zabbix_server.conf
124 DBPassword=westos #加入数据库密码
[root@server1 files]# vim zabbix.conf
20 php_value date.timezone Asia/Shanghai
[root@server1 files]#
[root@server1 zabbix]# ls
files install-zabbix-agent.sls
[root@server1 zabbix]# vim install-zabbix-server.sls
[root@server1 zabbix]# cat install-zabbix-server.sls
install-zabbix-server:
pkg.installed:
- pkgs:
- zabbix-server-mysql
- zabbix-web-mysql
file.managed:
- name: /etc/zabbix/zabbix_server.conf
- source: salt://zabbix/files/zabbix_server.conf
service.running:
- name: zabbix
[root@server1 zabbix]# vim install-apache.sls
[root@server1 zabbix]# cat install-apache.sls
install-apache:
pkg.installed:
- pkgs:
- httpd
file.managed:
- name: /etc/httpd/conf.d/zabbix.conf
- source: salt://zabbix/files/zabbix.conf
service.running:
- name: httpd
修改top.sls文件
[root@server1 salt]# vim top.sls
[root@server1 salt]# cat top.sls
base:
'server2':
- zabbix.install-zabbix-server
- mariadb.install
- zabbix.install-apache
'server3':
- zabbix.install-zabbix-agent
[root@server1 salt]# salt server2 state.sls zabbix.install-zabbix-server
server2:
----------
ID: install-zabbix-server
Function: pkg.installed
Result: True
Comment: 2 targeted packages were installed/updated.
Started: 04:43:58.466851
Duration: 6353.066 ms
Changes:
----------
dejavu-fonts-common:
----------
new:
2.33-6.el7
old:
dejavu-sans-fonts:
----------
new:
2.33-6.el7
old:
fontpackages-filesystem:
----------
new:
1.44-8.el7
old:
libX11:
----------
new:
1.6.5-2.el7
old:
libX11-common:
----------
new:
1.6.5-2.el7
old:
libXau:
----------
new:
1.0.8-2.1.el7
old:
libXpm:
----------
new:
3.5.12-1.el7
old:
libjpeg-turbo:
----------
new:
1.2.90-6.el7
old:
libxcb:
----------
new:
1.13-1.el7
old:
php-bcmath:
----------
new:
5.4.16-46.el7
old:
php-gd:
----------
new:
5.4.16-46.el7
old:
php-ldap:
----------
new:
5.4.16-46.el7
old:
php-mbstring:
----------
new:
5.4.16-46.el7
old:
php-mysql:
----------
new:
5.4.16-46.el7
old:
php-pdo:
----------
new:
5.4.16-46.el7
old:
php-xml:
----------
new:
5.4.16-46.el7
old:
t1lib:
----------
new:
5.1.2-14.el7
old:
zabbix-server-mysql:
----------
new:
4.0.5-1.el7
old:
zabbix-web:
----------
new:
4.0.5-1.el7
old:
zabbix-web-mysql:
----------
new:
4.0.5-1.el7
old:
----------
ID: install-zabbix-server
Function: file.managed
Name: /etc/zabbix/zabbix_server.conf
Result: True
Comment: File /etc/zabbix/zabbix_server.conf updated
Started: 04:44:04.855763
Duration: 128.196 ms
Changes:
----------
diff:
---
+++
@@ -121,7 +121,7 @@
#
# Mandatory: no
# Default:
-# DBPassword=
+DBPassword=westos
### Option: DBSocket
# Path to MySQL socket.
----------
ID: install-zabbix-server
Function: service.running
Name: zabbix-server
Result: True
Comment: Started Service zabbix-server
Started: 04:45:45.090012
Duration: 162.921 ms
Changes:
----------
zabbix-server:
True
Summary for server2
------------
Succeeded: 3 (changed=3)
Failed: 0
------------
Total states run: 3
Total run time: 6.500 s
[root@server1 zabbix]# salt server2 state.sls zabbix.install-apache
server2:
----------
ID: install-apache
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 04:49:48.305268
Duration: 631.753 ms
Changes:
----------
ID: install-apache
Function: file.managed
Name: /etc/httpd/conf.d/zabbix.conf
Result: True
Comment: File /etc/httpd/conf.d/zabbix.conf updated
Started: 04:49:48.940245
Duration: 36.577 ms
Changes:
----------
diff:
---
+++
@@ -17,7 +17,7 @@
php_value max_input_time 300
php_value max_input_vars 10000
php_value always_populate_raw_post_data -1
- # php_value date.timezone Europe/Riga
+ # php_value date.timezone Asia/Shanghai
</IfModule>
</Directory>
----------
ID: install-apache
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 04:49:48.977676
Duration: 52.088 ms
Changes:
Summary for server2
------------
Succeeded: 3 (changed=1)
Failed: 0
------------
Total states run: 3
Total run time: 720.418 ms
[root@server2 ~]# cd /usr/share/doc/zabbix-server-mysql-4.0.5/
[root@server2 zabbix-server-mysql-4.0.5]# zcat create.sql.gz | mysql -p zabbix
Enter password:
[root@server2 zabbix-server-mysql-4.0.5]#
访问http://172.25.12.2/zabbix/setup.php