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

Zabbix 概述php

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

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

Zabbix 特色ios

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

案例环境,如表所示。sql

主机                      操做系统                  IP地址                         主要操做数据库

Zabbix服务器       Centos 7                     192.168.66.146           搭建LAMP 架构,部署zabbix-severvim

Linux客户机          Centos 7                     192.168.66.145           部署zabbix-agentwindows


解决中文乱码的软件包连接:浏览器

连接: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 <IfModule dir_module>
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首页

<?php
   phpinfo();
?>

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

0

将 php 首页改成测试链接数据库

[root@localhost html]# vim index.php

<?php
$link=mysql_connect('192.168.66.146','zabbix','admin123');      
if($link) echo "<h1>Success!!!</h1>";
else echo "Fail!!";
mysql_close();
?>

使用 zabbix 用户链接数据库,链接成功输出:Success!!!  ,链接失败则输出:Fail!! 信息

再次使用浏览器访问测试

1

到此 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 后登陆。

2

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

3

4

添加主机信息

5

6

7

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

8

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

9

10

三 . 部署 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 。可从“配置” –> “主机”-> “建立主机”。

11

12

13

14

15

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

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邮箱,选择“设置” ,而后“帐户”

17

发送邮件测试

[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 自带多种监控模板能够直接使用

相关文章
相关标签/搜索