设为首页 收藏本站
查看: 1238|回复: 0

[经验分享] 部署 Zabbix 集中监控系统 (基于 LAMP 架构)

[复制链接]

尚未签到

发表于 2019-1-23 14:07:49 | 显示全部楼层 |阅读模式
  Zabbix 概述
  Zabbix 是一个高度集成的企业级开源网络监控解决方案,与 Cacti、Nagios 类似,提供分布式监控以及集中的 Web 管理界面。由 zabbix serve r端与 zabbix agent 组成。被监控对象只有支持 SNMP 协议或者运行 Zabbix_agents 代理程序即可。Zabbix 的官方网址为 http://www.zabbix.com/ ,软件可以自由下载使用。
  Zabbix 具备常见商业监控软件所具备的功能:主机性能检测网络设备性能监控、数据库性能监控、ftp等同于协议的监控,能够利用灵活的可定制警告机制,运行用户对事件发送基于 E-mail 的警告,可以保证相关维护人员对问题作出快速响应,还可以利用存储数据提供杰出的报表及实时的图形化数据处理。
  Zabbix 特点
  支持自动发现网络设备和服务器
    支持分布式监控
    可设置报警阀值
    可通过多种方式进行数据收集
    可定制的报警方式
    实时的绘图功能
    通过web监控系统与设置
  案例环境,如表所示。
  主机                      操作系统                  IP地址                         主要操作
  Zabbix服务器       Centos 7                     192.168.66.146           搭建LAMP 架构,部署zabbix-sever
  Linux客户机          Centos 7                     192.168.66.145           部署zabbix-agent
  解决中文乱码的软件包链接:
  链接:https://pan.baidu.com/s/1ObFt9zepWO9l05k-f79ZfQ
提取码: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 httpd 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/httpd/conf/httpd.conf                 //httpd 的主配置文件
  95 ServerName www.yun.com:80                                                 //设置servername
  163
164     DirectoryIndex index.html index.php                                 //页面首页类型支持 index.php
  编辑 /etc/php.ini 配置文件,设置时区
  [root@localhost ~]# vim /etc/php.ini
  878 date.timezone = PRC                               //设置中国时区
  4.启动 httpd 服务和 maruadb 服务,并查看端口。
  [root@localhost ~]# systemctl start httpd.service
[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/httpd
  5.初始化数据库的配置
  [root@localhost ~]# 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):                   //回车进入
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              //是否删除匿名用户 n
  ... 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         //运行远程登录
  ... 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              //删除测试数据库 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!
  Cleaning up...
  All done!  If you've completed all of the above steps, your MariaDB
installation 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 6
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;         //创建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 管理,密码为:admin123
Query 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 -p
Enter password:
ERROR 1045 (28000): Access denied for user 'zabbix'@'localhost' (using password: YES)
  使用 zabbix 用户登录数据库失败,其原因是有空用户名称占用导致本地无法连接,解决方法为删除空用户名
  以root 用户登录数据库,删除空用户名
  [root@localhost html]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
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)]> 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)]> quit
Bye
  此时退出,再以 zabbix 用户登录数据库
  [root@localhost html]# mysql -uzabbix -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)
  7. 测试 php ,能否使用 zabbix 用户连接数据库
  [root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.php                  编辑php首页

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

  将 php 首页改为测试连接数据库
  [root@localhost html]# vim index.php

  
  使用 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 http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
获取http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.85LcDm: 头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 zabbix
Enter 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/zabbix
101: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.conf
  125 DBPassword=admin123
  (2)修改时区
  [root@localhost html]# vim /etc/httpd/conf.d/zabbix.conf
  20          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]# ls
php-bcmath-5.4.16-42.el7.x86_64.rpm    STKAITI.TTF
php-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 的端口为10051
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN     
tcp6       0      0 :::10051                :::*                    LISTEN
  由于修改过配置文件,重启 httpd 服务
  [root@localhost aaa]# systemctl restart httpd.service
  5.服务重启后,访问 http://192.168.66.146/zabbix,安装 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.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. 安装 yum 源,安装 zabbix-agent 服务
  [root@localhost ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
获取http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.hITfvK: 头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.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
98:Server=127.0.0.1
139:ServerActive=127.0.0.1
150:Hostname=Zabbix server
268:Include=/etc/zabbix/zabbix_agentd.d/*.conf
  指定服务器的 IP地址
  [root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
  98 Server=192.168.66.146
  139 ServerActive=192.168.66.146
  150 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 10050
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      54896/zabbix_agentd
tcp6       0      0 :::10050                :::*                    LISTEN      54896/zabbix_agentd
  server 端的端口为: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.com
set smtp-auth-user=1947….@qq.com
set 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]# ls
alertscripts  externalscripts
[root@localhost zabbix]# cd alertscripts/
[root@localhost alertscripts]# ls
[root@localhost alertscripts]# vim mail.sh

  #!/bin/bash
#send mail
messages=`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 自带多种监控模板可以直接使用
  




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-666716-1-1.html 上篇帖子: Zabbix宏使用及用户自定义监控(三) 下篇帖子: 分布式监控系统Zabbix3.2对数据库的连接数预警
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表