Filebeat+logstash+zabbix监控服务构建
前言
日志监控结构为filebeat+logstash+zabbix
filebeat监控应用程序日志的写入,并连接输出到logstash。Logstash采集、处理,输出zabbix显示。
软件安装
操作系统:CentOS7.6
MariaDB数据库安装
Zabbix服务需要数据库的支持,首先安装数据库,这里选择MariaDB
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。在存储引擎方面,使用XtraDB(英语:XtraDB)来代替MySQL的InnoDB。
yum install mariaDB
shell> yum -y install mariadb mariadb-server mariadb-devel
启动mariaDB
shell> systemctl start mariadb
设置开机启动
shell> systemctl enable mariadb
数据库初始化
shell> mysql_secure_installation
配置mariaDB字符集
- shell> vi /etc/my.cnf
#[mysqld]标签下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
- shell> vi /etc/my.cnf.d/client.cnf
#[client]标签下添加
default-character-set=utf8
- shell> vi /etc/my.cnf.d/mysql-clients.cnf
#[mysql]标签下添加
default-character-set=utf8
- 配置完成以后重启数据库服务
shell> systemctl restart mariadb
- 登录mariaDB,查看MariaDB字符集
#登陆
shell> mysql -u root –p
#查看字符集
shell> show variables like "%character%";
shell> show variables like "%collation%";
Zabbix 数据库初始化
Zabbix源码安装包已经包含数据库相关脚本
首先上传源码包至目标服务器,解压:
tar zxvf zabbix-4.2.5.tar.gz
数据库初始化
#登陆
shell> mysql -uroot -p<password>
#创建zabbix数据库
mysql> create database zabbix character set utf8 collate utf8_bin;
#添加zabbix用户,并授权。’<password>’表示设置的zabbix用户密码
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by '<password>';
#刷新权限
mysql> FLUSH PRIVILEGES;
mysql> quit;
#If you use Zabbix packages continue with instructions for Debian/Ubuntu or RHEL/CentOS to import the data into the database.
#数据库脚本在zabbix解压目录, /解压目录/zabbix-4.2.5/database/mysql
shell> cd database/mysql
#执行创表脚本
shell> mysql -uzabbix -p zabbix < schema.sql
# stop here if you are creating database for Zabbix proxy
#导出初始化数据
shell> mysql -uzabbix -p zabbix < images.sql
shell> mysql -uzabbix -p zabbix < data.sql
zabbix安装
zabbix安装包含zabbix_server、zabbix frontend两部分。
zabbix_server提供核心服务
zabbix frontend提供友好操作界面给用户
zabbix_server
创建系统用户
shell> groupadd --system zabbix
shell> useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix
安装zabbix依赖
#依赖1
shell> yum install -y \
httpd \
php \
php-mysql \
php-gd \
libjpeg* \
php-ldap \
php-odbc \
php-pear \
php-xml \
php-xmlrpc \
php-mhash \
libxml2* \
net-snmp-devel \
libevent-devel \
curl-devel
#依赖2,安装PHP bcmath,mbstring插件
shell> yum install php-bcmath
shell> yum install php-mbstring
编译与安装
#编译,注意是否有Error信息
shell> ./configure --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2
#安装
shell> make install
参数配置
- zabbix_server配置文件路径 /usr/local/etc/zabbix_server.conf
- zabbix_agentd agent服务配置文件路径 /usr/local/etc/zabbix_agentd.conf
#配置zabbix_server.conf文件,主要修改数据库相关属性
#按照实际情况修改
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBPort=3306
#如果DBHost为localhost,mysql使用socket方式连接。故DBHost设置为localhost时,需指定mysql.socket路径
#默认路径是:/var/lib/mysql/mysql.sock
DBSocket=/var/lib/mysql/mysql.sock
启动
#启动zabbix服务
shell> zabbix_server
#zabbix_server启动日志查看
shell> tail -f /tmp/zabbix_server.log
zabbix frontend
Zabbix frontend is written in PHP, so to run it a PHP supported webserver is needed. Installation is done by simply copying the PHP files from frontends/php to the webserver HTML documents directory.
Common locations of HTML documents directories for Apache web servers include
Zabbix frontend端使用PHP开发,运行于Apache服务;
需将zabbix frontend端文件部署至Apache
部署
Apache部署目录:/var/www/html (Debian, Ubuntu, Fedora, RHEL, CentOS)
#1、创建zabbix部署目录
shell> mkdir /var/www/html/zabbix
#2、部署。/usr/local/monitor/zabbix-4.2.5为zabbix源码解压路径
shell> cp -a /usr/local/monitor/zabbix-4.2.5/frontends/php/* /var/www/html/zabbix
启动Apache服务
shell> systemctl start httpd
配置
浏览器访问:http://ip:端口/zabbix,打开zabbix frontend网页端进行初始化
Welcome
Check of pre-requisites
设置zabbix 前端运行必要的PHP参数项;
按照提示,修改PHP配置文件中,post_max_size、max_execution_time、max_input_time、date.timezone (date.timezone设置为RPC) 4项;
注:PHP 插件bcmath、mbstring已在安装依赖步骤进行安装
#修改PHP配置文件
shell> vim /etc/php.ini
post_max_size=16M
max_execution_time=300
max_input_time=300
date.timezone=RPC
修改完成,重启Apache服务生效配置。操作网页,重新检测参数项。等待参数检查全部通过
Configure DB connection
设置zabbix数据库的连接配置
Zabbix server details
设置zabbix server系统信息
Pre-installation summary
Install
点击”Download the configuration file”,下载zabbix.conf.php配置文件
传文件至 /var/www/html/zabbix/conf/zabbix.conf.php
Finish
登陆
地址:http://ip:port/zabbix
默认username:Admin
默认Password:zabbix
首页
安装全部完成
Logstash安装
Logstash License:Apache 2.0
当前版本:7.3.1
JDK版本:1.8
解压安装包
上传安装包至目标服务器,进行解压
shell> tar zxvf logstash-oss-7.3.1.tar.gz
安装插件
安装logstash-output-zabbix插件
在线安装
shell> bin/logstash-plugin install logstash-output-zabbix
离线安装
首先下载插件包logstash-output-zabbix.zip,上传至目标服务器
离线插件包下载:
shell> ./bin/logstash-plugin prrepare-offline-pack --overwrite --output /root/logstash-output-zabbix.zip logstash-output-zabbix
离线插件包安装:
#/usr/local/monitor/表示离线插件包的上传目录
shell> bin/logstash-plugin install file:////usr/local/monitor/logstash-output-zabbix.zip
启动
#启动命令
shell> nohup bin/logstash -f config/logstash-sample.conf --config.reload.automatic &
#日志查看
shell> tail –f nohup.out
Filebeat安装
Filebeat License:Apache 2.0
当前版本:7.3.1
安装方式:RPM
官方网址:https://www.elastic.co/cn/downloads/beats/filebeat-oss
RPM安装
#安装
shell> rpm –ivh filebeat-7.3.1-x86_64.rpm
启动
shell> systemctl start filebeat
开机启动
shell> systemctl enable filebeat
日志查看
#默认日志输出
shell> tail -f /var/log/messages
软件配置
Logstash配置
logstash-sample.conf,定义日志输入、过滤、输出模块
#定义日志输入插件filebeat
shell> vim config/logstash-sample.conf
input {
beats {
port => 5044
}
}
#定义过滤模块
filter {
#定义zabbix_key ,需与zabbix中监控项的键值一致
#定义zabbix_host,zabbix server name
mutate {
add_field => ["[@metadata][zabbix_key]","log.error"]
add_field => ["[@metadata][zabbix_host]","Zabbix server"]
}
}
#定义输出模块
output {
#输出插件为zabbix
# zabbix_host 引用filter模块定义的zabbix_host值
# zabbix_server_host zabbix_server服务的host
# zabbix_server_port zabbix_server服务的端口,默认10051
# zabbix_key 引用filter模块定义的zabbix_key值
# zabbix_value 输出zabbix数据字段的名称,默认message
zabbix {
zabbix_host => "[@metadata][zabbix_host]"
zabbix_server_host => "192.168.56.102"
zabbix_server_port => "10051"
zabbix_key => "[@metadata][zabbix_key]"
zabbix_value => "message"
}
}
Filebeat配置
官方文档
filebeat.yml,主要修改一下节点:
- filebeat.inputs:enabled、paths、multiline.pattern、multiline.match
- filebeat.config.modules: reload.enabled
- output.logstash
- output.elasticsearch
shell> vim /etc/filebeat/filebeat.yml
#参考配置
#inputs:enabled、paths、multiline.pattern、multiline.match
filebeat.inputs:
- type: log
enabled: true
paths:
#日志所在目录
- /usr/local/monitor/*.log
#多行日志正则的处理,如Java异常
multiline.pattern: '^[[:space:]]+(at|\.{3})\b|^Caused by:|[ERROR]|[\n]'
multiline.match: after
#filebeat.config.modules: reload.enabled
reload.enabled: true
#设置为logstash
#output.logstash: hosts
hosts: ["localhost:5044"]
#注释elasticsearch服务的配置
#output.elasticsearch: hosts
重启
保存配置,重启filebeat
shell> systemctl restart filebeat
Zabbix配置
监控配置
创建应用集
- 配置-主机-应用集
- 新建应用集
创建监控项
监控项,键值要与logstash-simple.conf中的zabbix_key保持一致
创建触发器
- 触发器
- 触发器表达式
查看监控日志
- 创建日志,写入测试数据
shell> touch a.log
shell> echo Hello>>/usr/local/monitor/a.log
- 查看监控日志
Filebeat、logstash、zabbix监控服务完成
语言切换
登陆首页,右上角人像图标:
中文乱码
zabbix配置图形,X轴数据栏目可能出现乱码。需要设置字体
上传字体包
下载windows字体包目录: C:\Windows\Fonts\simkai.ttf(楷体) 上传zabbix服务web端font目录: /var/www/html/zabbix/assets/fonts/
指定字体
#修改ZBX_GRAPH_FONT_NAME的值为simkai;
shell> vim /var/www/html/zabbix/include/defines.inc.php
重启:systemctl restart httpd
验证中文是否正常显示
以上,Filebeat+logstash+zabbix监控服务构建完成。