集所有优点于一身的 Zabbix 监控基于 LNMP 环境
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集所有优点于一身的 Zabbix 监控基于 LNMP 环境相关的知识,希望对你有一定的参考价值。
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)关闭防火墙:
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
(2)搭建 LNMP 之 安装 nginx 。
配置 nginx 官方源,安装、启动 nginx
[[email protected] ~]# wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[[email protected] ~]# rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
[[email protected] ~]# yum install nginx -y
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# systemctl enable nginx
[[email protected] ~]# netstat -ntap | grep 80
(3)搭建 LNMP 之 安装 mysql。
[[email protected] ~]# yum install mariadb-server mariadb -y
[[email protected] ~]# systemctl start mariadb.service
[[email protected] ~]# systemctl enable mariadb.service
(4)由于是 yum 安装 mysql ,所以需要简单的配置
[[email protected] ~]# 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
[[email protected] ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[[email protected] ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[[email protected] ~]# yum install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql -y
(6)配置 Nginx ,让它支持 PHP。
1)[email protected] ~]# 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)[[email protected] ~]# 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 处理动态请求。
[[email protected] ~]# 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 是否搭建成功!
[[email protected] ~]# vim /usr/share/nginx/html/info.php
<?php
phpinfo();
?>
[[email protected] ~]# systemctl start php-fpm.service
[[email protected] ~]# systemctl enable php-fpm.service
[[email protected] ~]# systemctl restart nginx
[[email protected] ~]# netstat -ntap | grep 9000
(9)测试 ,访问 web 界面,是否搭建PHP成功
(10)编写 MySQL 测试页面。
[[email protected] ~]# 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 ,并授权。
[[email protected] ~]# 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。
[[email protected] ~]# 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 服务器。
[[email protected] ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[[email protected] ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y
(16)将zabbix脚本文件导入到zabbix数据库中.
[[email protected] ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix //导入到zabbix数据库中
[[email protected] ~]# mysql -uzabbix -p123123
MariaDB [(none)]> use zabbix;
MariaDB [zabbix]> show tables; //查看是否导入成功
(17)修改 zabbix 配置文件
[[email protected] ~]# vim /etc/zabbix/zabbix_server.conf
91行:DBHost=localhost #去掉注释
125行:DBPassword=123123 #修改密码
> 对比完整的配置文件:
[[email protected] ~]# 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 的主页下。
[[email protected] ~]# cp -r /usr/share/zabbix/ /usr/share/nginx/html
[[email protected] ~]# chown -R zabbix.zabbix /etc/zabbix/
[[email protected] ~]# chown -R zabbix.zabbix /usr/share/nginx/
[[email protected] ~]# chown -R zabbix.zabbix /usr/lib/zabbix/
[[email protected] ~]# chmod -R 755 /etc/zabbix/web/
[[email protected] ~]# chmod -R 777 /var/lib/php/session/
(19)启动 zabbix 服务,因为这里是连自己都监控了,所以顺便开启了被监控服务。
[[email protected] ~]# systemctl start zabbix-server.service
[[email protected] ~]# systemctl enable zabbix-server.service
[[email protected] ~]# systemctl start zabbix-agent.service
[[email protected] ~]# systemctl enable zabbix-agent.service
[[email protected] ~]# netstat -ntap | grep 10051 //监控端默认端口号
(20)重启服务。
[[email protected] ~]# systemctl restart php-fpm.service
[[email protected] ~]# systemctl restart nginx
[[email protected] ~]# systemctl restart zabbix-server.service
(21)开始 web 界面安装,访问。
说明:用户名是 Admin 密码是:zabbix
(22)到此监控服务器完成,现在需要配置被监控端
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# setenforce 0
[[email protected] ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[[email protected] ~]# yum install zabbix-agent -y
[[email protected] ~]# 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)启动服务。
[[email protected] ~]# systemctl start zabbix-agent.service
[[email protected] ~]# systemctl enable zabbix-agent.service
[[email protected] ~]# netstat -ntap | grep 10050
(24)被监控端搭建完毕,现在需要把它添加到监控范围内。这个是在 web 界面配置。
总结:到此所有配置完成,下篇文章将会详解 怎么监控,如何邮件报警!
以上是关于集所有优点于一身的 Zabbix 监控基于 LNMP 环境的主要内容,如果未能解决你的问题,请参考以下文章