云岚

Centos7安装Zabbix监测
前言 Zabbix是一个服务器监控软件,可以用于监控服务器的网络、CPU、内存、硬盘等的情况,相比Smokepin...
扫描右侧二维码阅读全文
08
2018/12

Centos7安装Zabbix监测

前言

Zabbix是一个服务器监控软件,可以用于监控服务器的网络、CPU、内存、硬盘等的情况,相比Smokeping来说界面更友好,有后台可以更容易配置。但是由于需要数据库的关系,内存需求会比Smokeping更高,不过CPU占用相比Smokeping来说更加低。

安装环境

腾讯云上海区,1核2G,1M网络,作为网络监控并不需要太高的带宽,系统使用的是Centos7.5 64位

开始安装

Zabbix的官网有比较详细的安装方法,选择自己的系统和数据库安装方法就会出现在下面
Zabbix官网:https://www.zabbix.com/download)

选择安装环境

安装方法

先添加Zabbix的官方源

输入rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

运行后出现

[[email protected]_0_12_centos ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
warning: /var/tmp/rpm-tmp.IO3mf9: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY

安装zabbix所需的环境以及依赖

输入yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent

中间会提示是否导入Zabbix源的密钥,输入y允许即可

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
Importing GPG key 0xA14FE591:
 Userid     : "Zabbix LLC <[email protected]>"
 Fingerprint: a184 8f53 52d0 22b9 471d 83d0 082a b56b a14f e591
 Package    : zabbix-release-4.0-1.el7.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
Is this ok [y/N]: y
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
Importing GPG key 0x79EA5ED4:
 Userid     : "Zabbix SIA <[email protected]>"
 Fingerprint: fbab d5fb 2025 5eca b22e e194 d13d 58e4 79ea 5ed4
 Package    : zabbix-release-4.0-1.el7.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
Is this ok [y/N]: y

运行过程太长,这里就只贴完成提示了,由于Zabbix源在国外,国内的服务器可能会比较慢,耐心等待即可

......
  trousers.x86_64 0:0.3.14-2.el7
  unixODBC.x86_64 0:2.3.1-11.el7
  zabbix-web.noarch 0:4.0.2-1.el7

Complete!

安装并设置MySQL数据库

首先添加MySQL官方源
输入yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm,这里添加的是最新的MySQL8.0的源

[[email protected]_0_12_centos ~]# yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
Loaded plugins: fastestmirror, langpacks
mysql80-community-release-el7-1.noarch.rpm               |  25 kB     00:00
Examining /var/tmp/yum-root-9KHMQX/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch
Marking /var/tmp/yum-root-9KHMQX/mysql80-community-release-el7-1.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package mysql80-community-release.noarch 0:el7-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch   Version
                                  Repository                               Size
================================================================================
Installing:
 mysql80-community-release
                     noarch el7-1 /mysql80-community-release-el7-1.noarch  31 k

Transaction Summary
================================================================================
Install  1 Package

Total size: 31 k
Installed size: 31 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql80-community-release-el7-1.noarch                       1/1
  Verifying  : mysql80-community-release-el7-1.noarch                       1/1

Installed:
  mysql80-community-release.noarch 0:el7-1

Complete!

接下来输入yum install mysql-community-server来安装MySQL8.0,由于源在国外,所以对国内速度可能不是很快
会提示导入MySQL的密钥,输入y允许

Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <[email protected]>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql80-community-release-el7-1.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y

完成提示

Installed:
  mysql-community-libs.x86_64 0:8.0.13-1.el7
  mysql-community-libs-compat.x86_64 0:8.0.13-1.el7
  mysql-community-server.x86_64 0:8.0.13-1.el7

Dependency Installed:
  mysql-community-client.x86_64 0:8.0.13-1.el7
  mysql-community-common.x86_64 0:8.0.13-1.el7
  numactl-libs.x86_64 0:2.0.9-7.el7

Replaced:
  mariadb-libs.x86_64 1:5.5.60-1.el7_5

Complete!

输入以下命令启动和开启启动

systemctl enable mysqld
systemctl start mysqld
[[email protected]_0_12_centos ~]# sudo systemctl enable mysqld
[[email protected]_0_12_centos ~]# sudo systemctl start mysqld

MySQL在第一次启动时会生成一个临时密码,输入grep 'temporary password' /var/log/mysqld.log查看临时密码

2018-12-08T07:31:06.895276Z 5 [Note] [MY-010454] [Server] A temporary password is generated for [email protected]: ukktrcvL)3&e

uukktrcvL)3&e就是我的临时密码,在接下来的设置中会用到

接下来输入mysql_secure_installation来提高MySQL的安全性,会要求输入密码,把刚刚的临时密码输入进去即可

[[email protected]_0_12_centos ~]# mysql_secure_installation

Securing the MySQL server deployment.
#输入刚刚的临时密码
Enter password for user root:

The existing password for the user account root has expired. Please set a new password.
#设置新密码,需要大小写+数字+符号
New password:
#再次输入新密码
Re-enter new password:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100
#询问是否修改root密码,按照刚刚的再做一遍即可
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password:

Re-enter new password:

Estimated strength of the password: 100
#是否要使用刚刚的密码,输入y确认
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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.
#是否移除匿名账户,输入y确认
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
#是否禁止root远程登陆,输入y禁止
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL 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.

#是否移除test表,输入y确认
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - 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.
#是否重载权限表,输入y确认
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

至此,MySQL的安全设置就完毕了,接下来要设置Zabbix的数据库以及导入

输入mysql -uroot -p并输入刚刚设置的密码来登陆数据库

[[email protected]_0_12_centos ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

输入create database zabbix character set utf8 collate utf8_bin;创建数据库

mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected, 2 warnings (0.10 sec)

接下来创建用户以及赋予权限,这里需要注意的是由于Zabbix官网提供的命令是针对5.x的MySQL的,而我们安装的是8.0的MySQL,所以官网的命令不能用

输入create user ' zabbix'@'localhost' identified by 'password';创建zabbix用户,替换password为你要设置的密码

mysql> create user ' zabbix'@'localhost' identified by 'password';
Query OK, 0 rows affected (0.12 sec)

输入grant all privileges on zabbix.* to [email protected];赋予权限给刚刚创建的用户

mysql> grant all privileges on zabbix.* to [email protected];
Query OK, 0 rows affected (0.10 sec)

最后输入flush privileges;刷新权限表,输入quit退出MySQL命令行工具

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye

输入zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix将数据导入数据库中,会要求输入刚刚给zabbix用户输入的密码,需要等待一会,不要着急

[[email protected]_0_12_centos ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
Enter password:
[[email protected]_0_12_centos ~]#

修改Zabbix配置文件中的数据库密码,输入vim /etc/zabbix/zabbix_server.conf编辑zabbix配置文件,在大约124行,去掉前面的#注释符,=后输入刚刚设置的密码

DBPassword=password

至此,数据库就设置完毕了

设置时区

输入vim /etc/httpd/conf.d/zabbix.conf编辑apache配置文件,找到第20行# php_value date.timezone Europe/Riga,去掉前面的#号,Europe/Riga修改为Asia/Shanghai,也就是中国时区,根据自己需要调整

        php_value date.timezone Asia/Shanghai

启动Zabbix并设置开机启动

输入

systemctl restart zabbix-server zabbix-agent httpd
systemctl enable zabbix-server zabbix-agent httpd

出现

[[email protected]_0_12_centos ~]# systemctl enable zabbix-server zabbix-agent httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[[email protected]_0_12_centos ~]# systemctl restart zabbix-server zabbix-agent httpd

配置Zabbix设置向导

访问http://ip/zabbix,点击next step

设置向导1

由于我们是按照文档来安装的,所以正常情况下这里是全部显示OK,直接按next step就行

设置向导2

这里输入我们前面设置的zabbix密码

设置向导3

这里出现了一个错误,因为MySQL8.0使用了新的加密插件,而zabbix不支持

设置向导4

解决方法:

输入mysql -uroot -p登陆数据库,输入root密码

[[email protected]_0_12_centos ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 8.0.13 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

输入ALTER USER 'zabbix'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';修改密码插件,password修改为自己zabbix的密码

mysql> ALTER USER 'zabbix'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Query OK, 0 rows affected (0.06 sec)

输入flush privileges;刷新权限表,并输入quit退出MySQL命令行工具

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye

这时候再按next step就能正常进入下一步了

然后会让你为设置本机的监控,Name处填一个自己喜欢的名字就行,当然留空也可以

设置向导5

最后出现设置概览,直接下一步就行

设置向导6

至此,设置结束,按finish完成

设置向导7

登陆Zabbix并修改密码以及语言

默认用户名Admin密码zabbix,使用这个进行登陆

登陆

点击右上角的头像标志,点击change password按照提示即可修改密码
再点击Language,选择Chinese(zh_CN),再点击update就会变成中文了

语言密码

至此,Zabbix安装完毕

常见问题

1、出现Lack of free swap space on Zabbix server

这是因为一些云主机没有配备swap,所以zabbix认为swap不足
进入配置-模板-Template OS Linux-触发器
找到Lack of free swap space on {HOST.NAME}
将原本的表达式{Template OS Linux:system.swap.size[,pfree].last(0)}<50替换为{Template OS Linux:system.swap.size[,pfree].last(0)}<50 and {Template OS Linux:system.swap.size[,free].last(0)}<>0

稍微等一会儿警告就消失了

2、如何去除地址栏的/zabbix

我们直接访问自己服务器的ip的话会进入apache的测试页面,后面加上/zabbix才会进入zabbix的页面,去除方法也很简单
1、输入vim /etc/httpd/conf.d/zabbix.conf编辑zabbix的站点配置文件
注释掉第5行的的

Alias /zabbix /usr/share/zabbix

再修改apache的默认配置文件,输入vim /etc/httpd/conf/httpd.conf,找到119行的

DocumentRoot "/var/www/html"

修改为

DocumentRoot "/usr/share/zabbix/"

注释124、125、127、128、131、144、151、156、157行的

<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

164行的DirectoryIndex index.html后面再添加index.php

2、删除/etc/httpd/conf.d/welcome.conf,执行rm -rf /etc/httpd/conf.d/welcome.conf
最后输入systemctl restart httpd重启apache

Last modification:January 17th, 2019 at 04:55 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment