监控服务Zabbix部署

监控服务Zabbix部署


zabbix介绍

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

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

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

zabbix server可以通过SNMPzabbix agentping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,它可以运行在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)等配合使用。


zabbix特点

zabbix的主要特点:

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

Zabbix主要功能:

  • CPU负荷

  • 内存使用

  • 磁盘使用

  • 网络状况

  • 端口监视

  • 日志监视


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 | 0}
UserParameter 指定自定义监控脚本参数
LogFile 设置客户端日志文件存放路径

部署zabbix

环境说明:

环境 IP 要安装的应用
服务器 192.168.100.1 lamp架构、zabbix server、zabbix agent

因为zabbix是用php语言开发的,所以必须先部署lamp架构,使其能够支持运行php网页

准备工作:

安装lamp,详情见 "lamp部署"

zabbix服务端安装

//关闭防火线和SELINUX
[root@zabbix ~]# systemctl disable --now firewalld
[root@zabbix ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@zabbix ~]# setenforce 0

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

//下载zabbix
[root@zabbix ~]# cd /usr/src/
[root@zabbix src]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.2/zabbix-5.2.6.tar.gz

//解压
[root@zabbix src]# tar xf zabbix-5.2.6.tar.gz

//创建zabbix用户
[root@zabbix src]# useradd -r -M -s /sbin/nologin zabbix

//配置zabbix数据库
[root@zabbix src]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.00 sec)

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

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

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

mysql> quit
Bye

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

//编译安装zabbix
[root@zabbix mysql]# cd /usr/src/zabbix-5.2.6/
[root@zabbix zabbix-5.2.6]# ./configure --enable-server 
--enable-agent 
--with-mysql 
--with-net-snmp 
--with-libcurl 
--with-libxml2

***********************************************************
*            Now run 'make install'                       *
*                                                         *
*            Thank you for using Zabbix!                  *
*              <http://www.zabbix.com>                    *
***********************************************************

[root@zabbix zabbix-5.2.6]# make install

zabbix服务端配置

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

//修改服务端配置文件
//设置数据库信息
[root@zabbix ~]# vim /usr/local/etc/zabbix_server.conf
....
DBPassword=zabbix123!       //设置zabbix数据库连接密码

//启动zabbix_server和zabbix_agentd
[root@zabbix zabbix-5.2.6]# cd
[root@zabbix ~]# zabbix_server
[root@zabbix ~]# zabbix_agentd
[root@zabbix ~]# ss -antl
State     Recv-Q    Send-Q         Local Address:Port          Peer Address:Port    
LISTEN    0         128                  0.0.0.0:22                 0.0.0.0:*       
LISTEN    0         128                  0.0.0.0:10050              0.0.0.0:*       
LISTEN    0         128                  0.0.0.0:10051              0.0.0.0:*       
LISTEN    0         128                  0.0.0.0:9000               0.0.0.0:*       
LISTEN    0         128                        *:80                       *:*       
LISTEN    0         128                     [::]:22                    [::]:*       
LISTEN    0         80                         *:3306                     *:*  

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

zabbix web界面安装前配置

//修改/etc/php.ini的配置并重启php-fpm
[root@zabbix ~]# sed -ri 's/(post_max_size =).*/1 16M/g' /etc/php.ini
[root@zabbix ~]# sed -ri 's/(max_execution_time =).*/1 300/g' /etc/php.ini
[root@zabbix ~]# sed -ri 's/(max_input_time =).*/1 300/g' /etc/php.ini
[root@zabbix ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@zabbix ~]# systemctl restart php-fpm

//复制ui里的文件
[root@zabbix ~]# mkdir /usr/local/apache/htdocs/zabbix
[root@zabbix ~]# cd /usr/src/zabbix-5.2.6/ui
[root@zabbix ~]# cp -r /usr/src/zabbix-5.2.6/ui/* /usr/local/apache/htdocs/zabbix/
[root@zabbix ~]# chown -R apache.apache /usr/local/apache/htdocs/zabbix/

//配置apache虚拟主机
[root@zabbix ~]# vim /usr/local/apache/conf/extra/vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/zabbix"
    ServerName www.lamp.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@zabbix ~]# chmod 777 /usr/local/apache/htdocs/zabbix/conf
[root@zabbix ~]# ll -d /usr/local/apache/htdocs/zabbix/conf
drwxrwxrwx. 3 apache apache 94 Apr  7 15:38 /usr/local/apache/htdocs/zabbix/conf

//重启apache
[root@zabbix ~]# apachectl -t
Syntax OK
[root@zabbix ~]# apachectl restart
[root@zabbix ~]# ss -antl
State     Recv-Q    Send-Q        Local Address:Port          Peer Address:Port    
LISTEN    0         128                 0.0.0.0:22                 0.0.0.0:*       
LISTEN    0         128                 0.0.0.0:10050              0.0.0.0:*       
LISTEN    0         128                 0.0.0.0:10051              0.0.0.0:*       
LISTEN    0         128                 0.0.0.0:9000               0.0.0.0:*       
LISTEN    0         128                       *:80                       *:*       
LISTEN    0         128                    [::]:22                    [::]:*       
LISTEN    0         80                        *:3306                     *:*   

安装zabbix web界面

  • 修改/etc/hosts文件,添加域名与IP的映射
  • 在浏览器上访问域名,本文设置的域名为zabbix.example.com,你需要修改成你自己的域名
  • 恢复zabbix/conf目录的权限为755

在浏览器上访问域名进行安装:

进入安装界面

1

检测所有都是OK

2

输入授权的IP,输入数据库zabbix密码zabbix123!

3

下一步

4

设置时区和样式,下一步

5

确定信息,完成安装

6

安装完成

7

确认后自动跳转到用户登录界面

8

恢复zabbix/conf目录的权限为755:

[root@zabbix ~]# chmod 755 /usr/local/apache/htdocs/zabbix/conf
[root@zabbix ~]# ll -d /usr/local/apache/htdocs/zabbix/conf
drwxr-xr-x. 3 apache apache 117 Apr  7 15:58 /usr/local/apache/htdocs/zabbix/conf

登录zabbix

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

用户名 密码
Admin zabbix

9

10

zabbix的开机自启

zabbix的源码提供了系统服务脚本,在/usr/src/zabbix-5.2.6/misc/init.d/目录下,我的系统是RedHat的,所以选择fedora下的脚本复制到/etc/init.d下

[root@zabbix ~]# cd /usr/src/zabbix-5.2.6/misc/init.d/fedora/core5
[root@zabbix core5]# ls
zabbix_agentd  zabbix_server
[root@zabbix ~]# cp -a zabbix_server /etc/init.d/
[root@zabbix ~]# cp -a zabbix_agentd /etc/init.d/

//添加到开机自启动
[root@zabbix ~]# chkconfig --add zabbix_server
[root@zabbix ~]# chkconfig zabbix_server on
[root@zabbix ~]# chkconfig --list|grep zabbix_server

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

zabbix_server  	0:off	1:off	2:on	3:on	4:on	5:on	6:off

[root@zabbix ~]# chkconfig --add zabbix_agentd
[root@zabbix ~]# chkconfig zabbix_agentd on
[root@zabbix ~]# chkconfig --list|grep zabbix_agentd

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

zabbix_agentd  	0:off	1:off	2:on	3:on	4:on	5:on	6:off

//重启测试
[root@zabbix ~]# reboot

[root@zabbix ~]# ss -antl
State     Recv-Q     Send-Q         Local Address:Port          Peer Address:Port    
LISTEN    0          128                  0.0.0.0:22                 0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:10050              0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:10051              0.0.0.0:*       
LISTEN    0          128                  0.0.0.0:9000               0.0.0.0:*       
LISTEN    0          128                        *:80                       *:*       
LISTEN    0          128                     [::]:22                    [::]:*       
LISTEN    0          80                         *:3306                     *:*   
原文地址:https://www.cnblogs.com/yuqinghao/p/14629134.html