部署 Zabbix 集中监控系统 (基于 LAMP 架构)

网友投稿 281 2022-11-12

部署 Zabbix 集中监控系统 (基于 LAMP 架构)

Zabbix 概述

Zabbix 是一个高度集成的企业级开源网络监控解决方案,与 Cacti、Nagios 类似,提供分布式监控以及集中的 Web 管理界面。由 zabbix serve r端与 zabbix agent 组成。被监控对象只有支持 SNMP 协议或者运行 Zabbix_agents 代理程序即可。Zabbix 的官方网址为 ,软件可以自由下载使用。

Zabbix 具备常见商业监控软件所具备的功能:主机性能检测网络设备性能监控、数据库性能监控、ftp等同于协议的监控,能够利用灵活的可定制警告机制,运行用户对事件发送基于 E-mail 的警告,可以保证相关维护人员对问题作出快速响应,还可以利用存储数据提供杰出的报表及实时的图形化数据处理。

Zabbix 特点

支持自动发现网络设备和服务器    支持分布式监控    可设置报警阀值    可通过多种方式进行数据收集    可定制的报警方式    实时的绘图功能    通过web监控系统与设置

案例环境,如表所示。

主机                      操作系统                  IP地址                         主要操作Zabbix服务器       Centos 7                     192.168.66.146           搭建LAMP 架构,部署zabbix-severLinux客户机          Centos 7                     192.168.66.145           部署zabbix-agent解决中文乱码的软件包链接:链接:提取码:nxy3

Zabbix 通过 C/S 模式采集数据,通过 B/S 模式在 Web 端展示和配置。其中 Zabbix_server 可以运行在 CentOS 、RHEL 、SUSE 、Ubuntu 等 Linux 系统上,还需要使用 LAMP 平台来承载数据库和 Web 界面。

一. 在监控端部署 LAMP 架构

1.关闭防火墙

[root@localhost ~]# systemctl stop firewalld.service[root@localhost ~]# systemctl disable firewalld.service                   Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.[root@localhost ~]# setenforce 0

2.安装 LAMP 所需的软件包

[root@localhost ~]# yum install -y mariadb-server mariadb php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mhash

3.修改配置文件

[root@localhost ~]# vim /etc///的主配置文件95 ServerName yun.com:80                                                 //设置servername163 164     DirectoryIndex index.html index.php                                 //页面首页类型支持 index.php

编辑 /etc/php.ini 配置文件,设置时区

[root@localhost ~]# vim /etc/php.ini878 date.timezone = PRC                               //设置中国时区

4.启动 服务和 maruadb 服务,并查看端口。

[root@localhost ~]# systemctl start [root@localhost ~]# systemctl start mariadb.service [root@localhost ~]# netstat -ntap | egrep '(3306|80)'                   //查看3306和80端口tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      4630/mysqld         tcp6       0      0 :::80                   :::*                    LISTEN      4351/~]# mysql_secure_installationNOTE: 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 currentpassword for the root user.  If you've just installed MariaDB, andyou 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):                   //回车进入OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDBroot 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 anyoneto log into MariaDB without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] n              //是否删除匿名用户 n  ... skipping.Normally, root should only be allowed to connect from 'localhost'.  Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n         //运行远程登录  ... skipping.By default, MariaDB comes with a database named 'test' that anyone canaccess.  This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] n              //删除测试数据库 n  ... skipping.Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] y                         //重新加载  ... Success!Cleaning up...All done!  If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB!

6.数据库初始化完成后,即可登录数据库

[root@localhost ~]# mysql -uroot –p                //使用root 用户登录数据库Enter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 6Server version: 5.5.60-MariaDB MariaDB ServerCopyright (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;         //创建zabbix 数据库,被设置字符集为简体中文Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;                //查看数据库+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               || zabbix             |+--------------------+5 rows in set (0.00 sec)MariaDB [(none)]> grant all privileges on *.* to 'zabbix'@'%' identified by 'admin123';  //创建zabbix用户,所有库、表都给zabbix 管理,密码为:admin123Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> flush privileges;                //刷新数据库Query OK, 0 rows affected (0.00 sec)

使用创建用户zabbix登录数据库

[root@localhost html]# mysql -uzabbix -pEnter password: ERROR 1045 (28000): Access denied for user 'zabbix'@'localhost' (using password: YES)使用 zabbix 用户登录数据库失败,其原因是有空用户名称占用导致本地无法连接,解决方法为删除空用户名

以root 用户登录数据库,删除空用户名

[root@localhost html]# mysql -uroot -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 8Server 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)]> select user,host from mysql.user;                //查看当前用户+--------+-----------------------+| user   | host                  |+--------+-----------------------+| zabbix | %                     || root   | 127.0.0.1             || root   | ::1                   ||        | localhost             |                                    //存在两个空用户名| root   | localhost             ||        | localhost.localdomain || root   | localhost.localdomain |+--------+-----------------------+7 rows in set (0.00 sec)MariaDB [(none)]> drop user ''@localhost;                      //删除空用户Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> drop user ''@localhost.localdomain;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> quitBye

此时退出,再以 zabbix 用户登录数据库

[root@localhost html]# mysql -uzabbix -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 9Server 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)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               || zabbix             |+--------------------+5 rows in set (0.00 sec)

7. 测试 php ,能否使用 zabbix 用户连接数据库

[root@localhost ~]# cd /var/html]# ls[root@localhost html]# vim index.php                  编辑php首页

使用浏览器访问监控端的 IP 地址,即192.168.66.146. 查看 php 首页

将 php 首页改为测试连接数据库

[root@localhost html]# vim index.phpSuccess!!!";else echo "Fail!!";mysql_close();?>使用 zabbix 用户连接数据库,连接成功输出:Success!!!  ,连接失败则输出:Fail!! 信息

再次使用浏览器访问测试

到此 LAMP 平台部署已经完成,接下来在监控端部署 Zabbix _server 的服务。

二 . 部署 Zabbix_server 服务

1 .安装与 php 相关的软件包

[root@localhost html]# yum install php-bcmath php-mbstring –y

安装 zabbix 的yum 源,自动产生 repo 文件

[root@localhost html]# rpm -ivh 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY准备中...                          ################################# [100%]正在升级/安装...    1:zabbix-release-3.5-1.el7         ################################# [100%]

安装 zabbix-server-mysql 和 zabbix-web-mysql

[root@localhost html]# yum install zabbix-server-mysql zabbix-web-mysql –y

2. 初始化数据库模块,生成数据库文件,注意密码不要输成 root

[root@localhost html]# zcat /usr/share/doc/zabbix-server-mysql-4.0.0/create.sql.gz | mysql -uzabbix -p zabbixEnter password:         //密码为 admin123

3.(1)更改配置文件(这些过滤的内容是配置文件中自动配置好的,不需要更改,只有数据库登录密码需要更改)

[root@localhost html]# 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                             //pid 文件路径      82:SocketDir=/var/run/zabbix101:DBName=zabbix                                                                         //数据库名称117:DBUser=zabbix                                                                           //数据库用户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                                                               //慢日志文件

更改数据库登录密码

[root@localhost html]# vim /etc/zabbix/zabbix_server.conf125 DBPassword=admin123

(2)修改时区

[root@localhost html]# vim /etc/php_value date.timezone Asia/Shanghai               //修改为上海的时区

(3)修正图表中文乱码

[root@localhost html]# vim /usr/share/zabbix/include/defines.inc.php:%s /graphfont/kaiti/g          //将 graphfont 全文替换为 kaiti

复制相应的字体文件到 /usr/share/zabbix/fonts/

[root@localhost html]# yum install lrzsz –y       //lrzsz 工具可以在windows 和Linux 系统之间传输文件,[root@localhost html]# mkdir /aaa[root@localhost html]# cd /aaa[root@localhost aaa]# ls[root@localhost aaa]# rz[root@localhost aaa]# lsphp-bcmath-5.4.16-42.el7.x86_64.rpm    STKAITI.TTFphp-mbstring-5.4.16-42.el7.x86_64.rpm[root@localhost aaa]# cp STKAITI.TTF /usr/share/zabbix/fonts/

4启动 zabbix-server 服务,并查看端口

[root@localhost aaa]# systemctl start zabbix-server.service [root@localhost aaa]# systemctl enable zabbix-server.service                  //开机自启动Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.[root@localhost aaa]# netstat -an | grep 10051                                             //zabbix 的端口为10051tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN     tcp6       0      0 :::10051                :::*                    LISTEN

由于修改过配置文件,重启 服务

[root@localhost aaa]# systemctl restart ,安装 zabbix 后登录。

测试数据库的连通性,测试连通成功会显示 OK 字样。如下图所示

添加主机信息

使用默认用户 Admin ,密码 zabbix  登录

启动中文界面,Zabbix Web 管理界面自带多种语言包,默认使用的语言为英语。将 Zabbix 语言切换到中文版本

三 . 部署 zabbix_agent 服务(在被监控端搭建)

Agent 的作用就是获得 host 数据,然后把收集的数据发送给 Server(主动模式)或者是 Server 主动来拿取数据(被动模式)。

1.关闭防火墙

[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# systemctl disable firewalld.serviceRemoved symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.[root@localhost ~]# setenforce 0

2. 安装 yum 源,安装 zabbix-agent 服务

[root@localhost ~]# rpm -ivh 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY准备中...                          ################################# [100%]正在升级/安装...    1:zabbix-release-3.5-1.el7         ################################# [100%][root@localhost ~]# yum install zabbix-agent –y

3.更改 zabbix_agent 配置文件

[root@localhost ~]# grep -n '^'[a-Z] /etc/zabbix/zabbix_agentd.conf           13:PidFile=/var/run/zabbix/zabbix_agentd.pid32:LogFile=/var/log/zabbix/zabbix_agentd.log43:LogFileSize=098:Server=127.0.0.1139:ServerActive=127.0.0.1150:Hostname=Zabbix server268:Include=/etc/zabbix/zabbix_agentd.d/*.conf

指定服务器的 IP地址

[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf98 Server=192.168.66.146139 ServerActive=192.168.66.146150 Hostname=test

4.启动 zabbix-agent 服务并查看端口

[root@localhost ~]# systemctl start zabbix-agent.service [root@localhost ~]# systemctl enable zabbix-agent.service Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.[root@localhost ~]# netstat -natp | grep 10050tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      54896/zabbix_agentd tcp6       0      0 :::10050                :::*                    LISTEN      54896/zabbix_agentdserver 端的端口为:10051,agent 端口为:10050

此时 zabbix-server 和 zabbix-agent 都配置完成,并启动服务,到zabbix 的 web 页面,添加被监控主机。

创建主机。Host  是 Zabbix 监控的基本载体,所有监控项目都是基于 Host 。可从“配置” –> “主机”-> “创建主机”。

四 . 部署邮件发送服务(在监控端配置邮件报警)

1.安装 mailx 邮件软件包

[root@localhost aaa]# yum install mailx –y

更改配置文件

[root@localhost aaa]# vim /etc/mail.rc               //注意如使用网易或qq邮箱需要开启客户端授权码进行第三方登录set from=1947…@qq.com                             //qq邮箱地址set smtp=smtp.qq.comset smtp-auth-user=1947….@qq.comset smtp-auth-password=ahixbfxiuztjcfjb        //第三方授权码set smtp-auth=login

qq邮箱的第三方授权码获得方法如下,进入qq邮箱,选择“设置” ,然后“账户”

发送邮件测试

[root@localhost aaa]# echo "this is zabbix" | mail -s "testmail" 1947….@qq.com         //测试发送邮件,查看是否能收到

2.编写发邮件脚本

[root@localhost aaa]# cd /usr/lib/zabbix/[root@localhost zabbix]# lsalertscripts  externalscripts[root@localhost zabbix]# cd alertscripts/[root@localhost alertscripts]# ls[root@localhost alertscripts]# vim mail.sh#!/bin/bash#send mailmessages=`echo $3 | tr '\r\n' '\n'`subject=`echo $2 | tr '\r\n' '\n'`echo "${messages}" | mail -s "${subject}" $1 >>/tmp/mailx.log 2>&1

给脚本执行权限

[root@localhost alertscripts]# mv mail.sh mailx.sh[root@localhost alertscripts]# touch /tmp/mailx.log[root@localhost alertscripts]# chown -R zabbix.zabbix /tmp/mailx.log[root@localhost alertscripts]# chmod +x /usr/lib/zabbix/alertscripts/mail.sh[root@localhost alertscripts]# chown -R zabbix.zabbix /usr/lib/zabbix/

测试发邮件脚本

[root@localhost alertscripts]# ./mailx.sh 1947…..@qq.com "yum" "heihei"

总结:

Zabbix 是一个企业级的、开源的、分布式的监控套件,可以监控网络和服务的状态。Zabbix 可以利用数据提供图形化的报告,还具有灵活的告警机制。Zabbix 可以使用 Zabbix Web 管理页面进行管理配置。Zabbix 自带多种监控模板可以直接使用。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:log4j控制日志输出文件名称的两种方式小结
下一篇:pcie接口定义及知识解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~