监控服务zabbix部署

1. zabbix介绍

zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。

zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。

zabbix由2部分构成,zabbix server与可选组件zabbix agent。

zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,它可以运行在Linux,Ubuntu,Solaris,HP-UX,AIX,Free BSD,Open BSD,OS X等平台上。

zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。

zabbix server可以单独监视远程服务器的服务状态;同时也可以与zabbix agent配合,可以轮询zabbix agent主动接收监视数据(agent方式),同时还可被动接收zabbix agent发送的数据(trapping方式)。
另外zabbix server还支持SNMP (v1,v2),可以与SNMP软件(例如:net-snmp)等配合使用。

2. zabbix特点

zabbix的主要特点:

  • 安装与配置简单,学习成本低
  • 支持多语言(包括中文)
  • 免费开源
  • 自动发现服务器与网络设备
  • 分布式监视以及WEB集中管理功能
  • 可以无agent监视
  • 用户安全认证和柔软的授权方式
  • 通过WEB界面设置或查看监视结果
  • email等通知功能

Zabbix主要功能:

  • CPU负荷
  • 内存使用
  • 磁盘使用
  • 网络状况
  • 端口监视
  • 日志监视

3. zabbix配置文件

zabbix配置文件有两种:

  • 服务器端配置文件(/usr/local/etc/zabbix_server.conf)
  • 客户端配置文件(/usr/local/etc/zabbix_agentd.conf)
  • zabbix代理配置文件(/usr/local/etc/zabbix_proxy.conf)

服务器端配置文件zabbix_server.conf常用配置参数:

参数作用
LogFile设置服务端日志文件存放路径
ListenIP设置服务端监听IP
ListenPort设置服务端监听的端口号
PidFile设置服务端进程号文件存放路径
DBHost指定zabbix的数据库服务器IP
DBName指定zabbix使用的数据库库名
DBUser指定zabbix数据库登录用户
DBPassword指定zabbix数据库登录密码
DBPort指定zabbix数据库端口号
User设置zabbix以什么用户的身份运行
AlertScriptsPath设置告警脚本存放路径
ExternalScripts外部脚本存放路径

客户端配置文件zabbix_agentd.conf常用配置参数:

参数作用
Server指定zabbix服务器的IP或域名
ServerActive指定zabbix服务器的IP或域名
Hostname指定本机的主机名,此项必须与web界面配置项一致
UnsafeUserParameters是否启用自定义监控项,可选值为{1
UserParameter指定自定义监控脚本参数
LogFile设置客户端日志文件存放路径

4. 部署zabbix

环境说明:

环境IP要安装的应用
服务器192.168.19.138lamp架构
zabbix server
zabbix agent
客户端192.168.19.128zabbix agent

4.1 zabbix服务端安装

//安装依赖包
[root@yh src]# yum -y install net-snmp-devel libevent-devel libxml2-devel curl-devel pcre*
已加载插件:product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
base                                                                                | 3.6 kB  00:00:00 

//下载zabbix
[root@yh src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz

//解压
[root@yh src]# tar xf zabbix-4.0.3.tar.gz 
[root@yh src]# ls
debug  kernels  nginx-1.12.2  nginx-1.12.2.tar.gz  zabbix-4.0.3  zabbix-4.0.3.tar.gz

//创建zabbix用户和组
[root@yh ~]# groupadd -r zabbix
[root@yh ~]# useradd -r -g zabbix -M -s /sbin/nologin zabbix



//配置zabbix数据库
[root@yh ~]# mysql -uroot -pyh123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.07 sec)

mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.06 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.07 sec)

mysql> exit


[root@yh ~]# cd /usr/src/zabbix-4.0.3/database/mysql/
[root@yh mysql]# ls
data.sql  images.sql  Makefile.am  Makefile.in  schema.sql
[root@yh mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@yh mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@yh mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.




//编译安装zabbix
[root@yh ~]# cd /usr/src/zabbix-4.0.3
[root@yh zabbix-4.0.3]# ./configure --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
...

[root@localhost zabbix-4.0.3]# make install
...
make[2]: 对“install-data-am”无需做任何事。
make[2]: 离开目录“/usr/src/zabbix-4.0.3”
make[1]: 离开目录“/usr/src/zabbix-4.0.3”
[root@yh zabbix-4.0.3]# 

4.2 zabbix服务端配置

[root@yh ~]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d  zabbix_server.conf  zabbix_server.conf.d

//修改服务端配置文件
//设置数据库信息
[root@yh ~]# vim /usr/local/etc/zabbix_server.conf

....
DBPassword=zabbix123!       //设置zabbix数据库连接密码

### Option: DBSocket
#       Path to MySQL socket.
#
# Mandatory: no
# Default:
DBSocket=/tmp/mysql.sock  修改

第一次重启没有10051端口
做下列操作
[root@yh mysql]# mkdir /var/lib/mysql
[root@yh mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
[root@yh mysql]# /etc/rc.d/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 


//启动zabbix_server和zabbix_agentd
[root@yh ~]# zabbix_server
[root@yh ~]# zabbix_agentd
[root@yh ~]# ss -antl
State       Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
LISTEN      0      128                       *:22                                    *:*                  
LISTEN      0      100               127.0.0.1:25                                    *:*                  
LISTEN      0      128                       *:10050                                 *:*                  
LISTEN      0      128                       *:10051                                 *:*                  
LISTEN      0      128               127.0.0.1:9000                                  *:*                  
LISTEN      0      128                      :::80                                   :::*                  
LISTEN      0      128                      :::22                                   :::*                  
LISTEN      0      100                     ::1:25                                   :::*                  
LISTEN      0      80                       :::3306                                 :::*  

4.3 zabbix服务端web界面安装与配置

4.3.1 zabbix web界面安装前配置

[root@yh ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@yh ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@yh ~]#  sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@yh ~]#  sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@yh ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done


[root@yh ~]# cd /usr/src/zabbix-4.0.3
[root@yh zabbix-4.0.3]# ls
aclocal.m4  ChangeLog     config.log     configure.ac  frontends   m4           man      README
AUTHORS     compile       config.status  COPYING       include     Makefile     misc     sass
bin         conf          config.sub     database      INSTALL     Makefile.am  missing  src
build       config.guess  configure      depcomp       install-sh  Makefile.in  NEWS
[root@yh zabbix-4.0.3]# mkdir /usr/local/apache/htdocs/zabbix
[root@yh zabbix-4.0.3]# cp -a frontends/php/* /usr/local/apache/htdocs/zabbix/
[root@yh zabbix-4.0.3]# groupadd -r apache
[root@yh zabbix-4.0.3]# useradd -r -g apache -M -s /sbin/nologin apache
[root@yh zabbix-4.0.3]# chown -R apache.apache /usr/local/apache/htdocs

配置apache虚拟主机
[root@localhost ~]# vim /etc/httpd/httpd.conf
在配置文件的末尾加如下内容
ServerName zabbix.yh.com:80
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/zabbix"
    ServerName zabbix.yh.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/zabbix/$1
    <Directory "/usr/local/apache/htdocs/zabbix">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>



设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php
[root@yh ~]# chmod 777 /usr/local/apache/htdocs/zabbix/conf
[root@yh ~]# ll -d /usr/local/apache/htdocs/zabbix/conf
drwxrwsrwx. 2 apache apache 81 12月 20 19:24 /usr/local/apache/htdocs/zabbix/conf



重启apache
[root@yh ~]# ln -s /usr/local/apache/bin/apachectl /bin/ 
[root@yh ~]# apachectl -t
Syntax OK
[root@yh ~]# apachectl stop
[root@yh ~]# apachectl start
[root@yh ~]# ss -antl
State       Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
LISTEN      0      128                       *:22                                    *:*                  
LISTEN      0      100               127.0.0.1:25                                    *:*                  
LISTEN      0      128                       *:10050                                 *:*                  
LISTEN      0      128                       *:10051                                 *:*                  
LISTEN      0      128               127.0.0.1:9000                                  *:*                  
LISTEN      0      128                      :::80                                   :::*                  
LISTEN      0      128                      :::22                                   :::*                  
LISTEN      0      100                     ::1:25                                   :::*                  
LISTEN      0      80                       :::3306                                 :::*  

4.3.2 安装zabbix web界面

修改/etc/hosts文件,添加域名与IP的映射

[root@yh ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.19.128 zabbix.yh.com
~                                

在浏览器上访问域名,本文设置的域名为zabbix.wenhs.com,你需要修改成你自己的域名
恢复zabbix/conf目录的权限为755

在浏览器上访问域名进行安装:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
恢复zabbix/conf目录的权限为755:

[root@yh ~]# chmod 755 /usr/local/apache/htdocs/zabbix/conf
[root@yh ~]# ll -d /usr/local/apache/htdocs/zabbix/conf
drwxr-sr-x. 2 apache apache 104 5月  16 18:58 /usr/local/apache/htdocs/zabbix/conf

4.3.3登录zabbix

zabbix默认登录用户名和密码:

用户名密码
Adminzabbix

4.3.4汉化zabbix

zabbix 自带多种语言包,也包括中文。登陆zabbix web控制台默认是英文,可切换中文版本

1.默认登陆界面(英文版)
在这里插入图片描述
点击右上角小人图标,切换语言
在这里插入图片描述
汉化
在这里插入图片描述
解决中文乱码

上传本地字体

找到本地C:\Windows\Fonts\SIMKAI.TTF (楷体)上传到/usr/local/apache/htdocs/zabbix/fonts

在这里插入图片描述
重命名字体名称

将zabbix服务器正在使用字体备份

[root@yh fonts]#  mv /usr/local/apache/htdocs/zabbix/fonts/DejaVuSans.ttf{,.bak}
[root@yh fonts]# ll
总用量 12252
-rw-r--r--. 1 apache apache   756072 12月 20 19:24 DejaVuSans.ttf.bak
-rw-r--r--. 1 root   apache 11785184 5月  17 14:59 SIMKAI.TTF
[root@yh fonts]# scp SIMKAI.TTF root@192.168.19.128:/usr/local/apache/htdocs/zabbix/fonts/DejaVuSans.ttf 
The authenticity of host '192.168.19.128 (192.168.19.128)' can't be established.
ECDSA key fingerprint is SHA256:+earxJh51iQYRB6Kge5ZuhFJGN5NSGsxjS3KJpjyzO8.
ECDSA key fingerprint is MD5:2e:7b:1b:ee:ee:91:9c:ef:37:68:e7:60:7b:59:30:88.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.19.128' (ECDSA) to the list of known hosts.
root@192.168.19.128's password: 
SIMKAI.TTF                                                               100%   11MB  62.0MB/s   00:00   

再次查看界面
在这里插入图片描述

5.客户端配置

安装依赖包
[root@yh1~]# yum -y install net-snmp-devel libevent-devel libxml2-devel curl-devel pcre*

下载zabbix
[root@yh ~]# cd /usr/src/
[root@yh src]# scp 192.168.19.140:/usr/srczabbix-4..0.3.tar.gz .
root@192.168.19.140's password: 
zabbix-4.2.1.tar.gz                               100%   17MB  61.7MB/s   00:00    
[root@yh1 src]# tar xf zabbix-4..0.3.tar.gz

创建zabbix用户和组
[root@wenhs5479 ~]# groupadd -r zabbix
[root@wenhs5479 ~]# useradd -r -g zabbix -M -s /sbin/nologin zabbix

编译安装zabbix
[root@yh1 ~]# cd /usr/src/zabbix-4..0.3    
[root@yh1 zabbix-4..0.3]# ./configure --enable-agent
[root@yh1 zabbix-4..0.3]# make install
[root@yh1 zabbix-4..0.3]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d 

修改配置文件
设置数据库信息
[root@yh1 zabbix-4..0.3]# vim /usr/local/etc/zabbix_agentd.conf
Server=192.168.19.138
ServerActive=192.168.19.138
Hostname=138
启动zabbix_agentd
[root@yh1 zabbix-4..0.3]# groupadd -r zabbix
[root@yh1 zabbix-4..0.3]# useradd -r -M -s /sbin/nologin -g zabbix zabbix
[root@yh1 zabbix-4..0.3]# zabbix_agentd
[root@yh1 zabbix-4..0.3]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128            *:111                        *:*                  
LISTEN     0      5      192.168.122.1:53                         *:*                  
LISTEN     0      128            *:22                         *:*                  
LISTEN     0      128    127.0.0.1:631                        *:*                  
LISTEN     0      100    127.0.0.1:25                         *:*                  
LISTEN     0      128    127.0.0.1:6010                       *:*                  
LISTEN     0      128            *:10050                      *:*                  
LISTEN     0      128           :::111                       :::*                  
LISTEN     0      128           :::22                        :::*                  
LISTEN     0      128          ::1:631                       :::*                  
LISTEN     0      100          ::1:25                        :::*                  
LISTEN     0      128          ::1:6010                      :::*

6.监控流程

创建主机群组
在这里插入图片描述
创建主机
在这里插入图片描述
添加模板
在这里插入图片描述
添加监控项
在这里插入图片描述
添加触发器
在这里插入图片描述

测试

[root@yh ~]# echo "hello" >> /etc/passwd

在web界面查看
在这里插入图片描述
电子邮件报警

在这里插入图片描述

  • 先要在服务端安装mailx;
  • 重新启动postfix服务;
[root@zabbix ~]# systemctl restart postfix

在邮箱查看
在这里插入图片描述

添加报警媒介内容(可以选择默认的E-mail方式或者手动创建媒介类型);

  • 默认的E-mail方式;
  • 点击管理——报警媒介类型——选择Email,页面设置如下

在这里插入图片描述

  • 配置用户发送的警告方式
  • 管理——用户——选择Admin(或者其他用户)——报警媒介,页面设置如下
  • 配置触发器的动作
  • 点击配置——动作——创建动作,第一页面添加名称,——点击操作,页面如下
    在这里插入图片描述
  • 修改/etc/passwd文件查看验证
[root@yh ~]# echo "w" >> /etc/passwd

在这里插入图片描述
脚本报警

  • 安装mailx
  • 重启postfix服务
  • 测试手动发送邮件
  • 添加告警媒介
    • 点击配置——报警媒介类型——创建媒介类型,页面配置如下

在这里插入图片描述

  • 配置用户的警告方式
    在这里插入图片描述
    点击更新
  • 配置触发器的动作
  • 点击配置——动作——创建动作,页面如图一——操作,页面如图二
    在这里插入图片描述
  • 在服务端上写发送告警信息脚本
  • 在服务端创建存放脚本的目录并写如下图脚本,将目录以及脚本的属主和属组设置为zabbix,并给脚本执行权限
[root@zabbix ~]# mkdir /usr/local/etc/scripts
[root@zabbix scripts]# vim sendmail.sh
#!/bin/bash
subject=$(echo $2 |tr "\r\n" "\n")
message=$(echo $3 |tr "\r\n" "\n")
echo "$message" | /usr/bin/mail -s "$subject" $1 &>/tmp/sm.log

[root@zabbix etc]# chown -R zabbix.zabbix scripts
[root@zabbix etc]# chmod +x scripts/138.sh
[root@zabbix etc]# 

[root@zabbix scripts]# tail -5 /etc/mail.rc 
#set from=xxxx@163.com
#set smtp=smtp.163.com
#set smtp-auth-user=xxxx@163.com
#set smtp-auth-password=W授权码
#set smtp-auth=login
取消注释使用163邮箱发邮件,不取消使用本机虚拟邮箱

修改/usr/local/etc/zabbix_server.conf,修改如下

AlertScriptsPath=/usr/local/etc/scripts

重启服务

[root@server ~]# pkill zabbix
[root@server ~]# zabbix_server 
[root@server ~]# zabbix_agentd 
[root@server ~]# ss -ntl
State       Recv-Q Send-Q               Local Address:Port                 Peer Address:Port 
LISTEN      0      128                             *:10050                           *:*     
LISTEN      0      128                              *:10051  

测试

[root@yh ~]# echo "w" >> /etc/passwd

在这里插入图片描述
通过zabbix用户发送邮件

  • 在服务端安装mailx、postfix
  • 重新启动postfix
  • 添加邮箱白名单zabbix@zabbix.server.com
  • 修改主机名
[root@yh ~]# hostnamectl set-hostname zabbix.server.com
  • 修改/etc/hosts文件,添加如下
127.0.0.1	zabbix.com
  • 重启postfix服务
[root@zabbix etc]# systemctl restart postfix
  • 添加告警媒介
    在这里插入图片描述
  • 添加告警用户
    在这里插入图片描述
  • 添加动作

在这里插入图片描述
修改配置文件进行验证

[root@yh ~]# echo "w" >> /etc/passwd

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值