Zabbix 概述
- Zabbix 是一个基于 WEB 界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
- Zabbix 能监视各种网络参数,保证服务器的安全运营;并提供灵活的通知机制以及让管理员快速定位和解决存在个问题。
- Zabbix 它由 2 部分构成,Zabbix server 与可选组件 Zabbix agent。
- Zabbix server 可以通过 SNMP,zabbix agent ,ping,端口监视等方法提供对远程服务器或网络状态的监视数据收集等功能,它可以运行在 Linux,Solaris,HP-UX,AIX,Free BSD,OS X等平台上。
搭建Zabbix 监控环境
要想搭建一个 Zabbix 的工作环境,需要从服务器入手,与服务器通信,管理员需要使用一个Zabbix前端界面,与 Zabbix 服务器和数据库进行通信。三个关键(界面、服务器和数据库)可以安装在同一台服务器上,但是如果你有一个更大更复杂的环境,将它们安装在不同的主机上也是一个选项。Zabbix 服务器能够直接监控到同一网络中的设备,如果其他网络的设备也需要被监控,那还需要一台 Zabbix 代理服务器。
Zabbix 特点:
1. 安装与配置简单,学习成本低;
2. 支持多语言(包括中文);
3. 免费开源(这个很重要);
4. 自动发现服务器与网络设备;
5. 分布式监视以及 WEB 集中管理功能;
6. 可以无视 agent 监视;
7. 用户安全认证和柔软的授权方式;
8. 通过 WEB 界面设置和查看监视结果;
9. email 等通知功能。
部署 Zabbix 环境准备
此次部署都属于 yum 安装,一台服务器,一台被监控机。
1·安装 nginx1.14
2·安装 mysql5.7
3·安装 PHP7.2
4·Zabbix 4.0
因 Zabbix 通过 c/s 模式采集数据,通过 B/S 模式在 web 端展示和配置。所以需要搭建 LAMP 或 LNMP 架构。此文章是采取 LNMP 结构。
| 主机 | 操作系统 | IP地址 | 主要软件 | -------- | -------- | -------- | | Zabbix 服务器 | CentOS 7.4 | 192.168.154.132 | nginx1.14、mysql5.7、PHP7.2、Zabbix 4.0| |Linux 客户机| CentOS 7.4 | 192.168.154.133 | zabbix-agent|
部署步骤
(1)关闭防火墙:
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
(2)搭建 LNMP 之 安装 nginx 。
配置 nginx 官方源,安装、启动 nginx
[root@localhost ~]# wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]# rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx
[root@localhost ~]# netstat -ntap | grep 80
(3)搭建 LNMP 之 安装 mysql。
[root@localhost ~]# yum install mariadb-server mariadb -y
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# systemctl enable mariadb.service
(4)由于是 yum 安装 mysql ,所以需要简单的配置
[root@localhost ~]# mysql_secure_installation
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] y //是否给 root 用户设置登陆密码
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] n // 是否移除匿名用户a
... skipping.
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] n // 远程禁止 root 登陆吗
... skipping.
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] n //删除测试数据库并访问它?
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y //现在重新加载特权表吗?
... Success!
(5)搭建 LNMP 之 安装 安装PHP 7.2
[root@localhost ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@localhost ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@localhost ~]# yum install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql -y
(6)配置 Nginx ,让它支持 PHP。
1)root@localhost ~]# vim /etc/php-fpm.d/www.conf
; Start a new pool named 'www'.
[www]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx //将Apache改为nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx //将Apache改为nginx
2)[root@localhost ~]# vim /etc/php.ini
359行 expose_php = Off #隐藏PHP版本
368行 max_execution_time = 300 #执行时间
378行 max_input_time = 300 #接收数据等待时间
389行 memory_limit = 128M #每个脚本占用内存
656行 post_max_size = 16M #POST数据大小
799行 upload_max_filesize = 2M #下载文件大小
800行 always_populate_raw_post_data = -1 #可以用$HTTP_RAW_POST_DATA接收post raw data
877行 date.timezone = Asia/Shanghai #修改时区为 亚洲/上海
(7)配置 Nginx ,让 PHP 处理动态请求。
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
10行 index index.php index.html index.htm; //加上支持页面格式 index.php
30行 location ~ \.php$ { //以下需要去掉注释
31行 root /usr/share/nginx/html;
32行 fastcgi_pass 127.0.0.1:9000;
33行 fastcgi_index index.php;
34行 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; // 把fastcgi_param中的/scripts改为$document_root
35行 include fastcgi_params;
36行 }
(8)编写 PHP 测试页面,准备测试 PHP 是否搭建成功!
[root@localhost ~]# vim /usr/share/nginx/html/info.php
<?php
phpinfo();
?>
[root@localhost ~]# systemctl start php-fpm.service
[root@localhost ~]# systemctl enable php-fpm.service
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# netstat -ntap | grep 9000
(9)测试 ,访问 web 界面,是否搭建PHP成功
(10)编写 MySQL 测试页面。
[root@localhost ~]# vim /usr/share/nginx/html/info.php
<?php
$link=mysqli_connect('127.0.0.1','root','123123');
if ($link) echo "true"; //成功返回 true
else echo "false"; //失败返回 false
?>
(11)测试,访问 web 界面 ,测试 MySQL 链接是否成功。
(12)创建数据库 zabbix ,并授权。
[root@localhost ~]# mysql -u root -p123123
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
MariaDB [(none)]> grant all privileges on *.* to 'zabbix'@'%' identified by '123123';
MariaDB [(none)]> flush privileges;
(13)删除空用户,否则 zabbix 用户不能登陆 数据库
MariaDB [(none)]> select user,host from mysql.user;
+--------+-----------------------+
| user | host |
+--------+-----------------------+
| zabbix | % |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
+--------+-----------------------+
把空的用户 名称 都删除,这样就能使用 zabbix 用户 登陆mysql
MariaDB [(none)]> drop user ''@'localhost';
MariaDB [(none)]> drop user ''@localhost.localdomain;
(14)编写代码,测试 zabbix 是否能连接php。
[root@localhost ~]# vim /usr/share/nginx/html/info.php
<?php
$link=mysqli_connect('127.0.0.1','zabbix','123123');
if ($link) echo "true";
else echo "false";
?>
(15)到此 LNMP 搭建完毕,现在开始部署 Zabbix 服务器。 [root@localhost ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm [root@localhost ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y
(16)将zabbix脚本文件导入到zabbix数据库中.
[root@localhost ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix //导入到zabbix数据库中
[root@localhost ~]# mysql -uzabbix -p123123
MariaDB [(none)]> use zabbix;
MariaDB [zabbix]> show tables; //查看是否导入成功
(17)修改 zabbix 配置文件
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf
91行:DBHost=localhost #去掉注释
125行:DBPassword=123123 #修改密码
> 对比完整的配置文件:
[root@localhost ~]# grep -n '^'[a-Z] /etc/zabbix/zabbix_server.conf
38:LogFile=/var/log/zabbix/zabbix_server.log
49:LogFileSize=0
72:PidFile=/var/run/zabbix/zabbix_server.pid
82:SocketDir=/var/run/zabbix
91:DBHost=localhost
101:DBName=zabbix
117:DBUser=zabbix
125:DBPassword=123123
357:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
475:Timeout=4
518:AlertScriptsPath=/usr/lib/zabbix/alertscripts
529:ExternalScripts=/usr/lib/zabbix/externalscripts
565:LogSlowQueries=3000
(18)把 zabbix 的文件都移动到 nginx 的主页下。
[root@localhost ~]# cp -r /usr/share/zabbix/ /usr/share/nginx/html
[root@localhost ~]# chown -R zabbix.zabbix /etc/zabbix/
[root@localhost ~]# chown -R zabbix.zabbix /usr/share/nginx/
[root@localhost ~]# chown -R zabbix.zabbix /usr/lib/zabbix/
[root@localhost ~]# chmod -R 755 /etc/zabbix/web/
[root@localhost ~]# chmod -R 777 /var/lib/php/session/
(19)启动 zabbix 服务,因为这里是连自己都监控了,所以顺便开启了被监控服务。
[root@localhost ~]# systemctl start zabbix-server.service
[root@localhost ~]# systemctl enable zabbix-server.service
[root@localhost ~]# systemctl start zabbix-agent.service
[root@localhost ~]# systemctl enable zabbix-agent.service
[root@localhost ~]# netstat -ntap | grep 10051 //监控端默认端口号
** (20)重启服务。**
[root@localhost ~]# systemctl restart php-fpm.service
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# systemctl restart zabbix-server.service
(21)开始 web 界面安装,访问。
**说明:用户名是 Admin 密码是:zabbix **
(22)到此监控服务器完成,现在需要配置被监控端
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[root@localhost ~]# yum install zabbix-agent -y
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
13:PidFile=/var/run/zabbix/zabbix_agentd.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
98:Server=192.168.199.129 //指定监控端
139:ServerActive=192.168.199.129 //指定监控端
150:Hostname=test
268:Include=/etc/zabbix/zabbix_agentd.d/*.conf
(23)启动服务。
[root@localhost ~]# systemctl start zabbix-agent.service
[root@localhost ~]# systemctl enable zabbix-agent.service
[root@localhost ~]# netstat -ntap | grep 10050
(24)被监控端搭建完毕,现在需要把它添加到监控范围内。这个是在 web 界面配置。
总结:到此所有配置完成,下篇文章将会详解 怎么监控,如何邮件报警!