12.8、收集日志:

因为logstash安装在从节点上,所以这里收集的主要是从节点上的服务日志;

1、收集系统日志:

(1)配置文件:

vim /etc/logstash/conf.d/system-log.conf

input {

file {

path => "/var/log/messages"

type => "system_log"

start_position => "beginning"

stat_interval => "2"

}

file {

path => "/var/log/lastlog"

type => "system_last_log"

start_position => "beginning"

stat_interval => "2"

}

}

output {

if [type] == "system_log" {

elasticsearch {

hosts => ["172.16.1.90:9200"]

index => "logstash-system_log-%{+yyyy.MM.dd}"

}

}

if [type] == "system_last_log" {

elasticsearch {

hosts => ["172.16.1.90:9200"]

index => "logstash-system_last_log-%{+yyyy.MM.dd}"

}

}

}

1)参数说明:

A、input file插件:

a、path:

代表日志文件路径;

b、type:

表示自定义的日志类型;

c、start_position:

beginning、end

选择Logstash最初开始读取文件的位置,开头或结尾。默认行为将文件视为实时流,因此默认从最后开始。

如果您要导入旧数据,请将其设置为开头,但是如果记录过文件的读取信息,这个配置也就失去作用了,会从最后端读取;

d、stat_interval:

表示收集日志的间隔时间,默认是1秒,增加此间隔将减少我们进行的系统调用次数,但会增加检测新日志行的时间;

B、output elasticsearch插件:

a、host:

表示elasticsearch集群的地址,集群中任意一节点的地址都可,因为集群的数据是共享的;

b、index:

表示索引的名称;

C、具体参数可以查看官方帮助文档:https://www.elastic.co/guide/en/logstash/current/index.html

(2)验证配置文件:

/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/system-log.conf -t

Configuration OK

(3)修改日志文件的权限:

chmod 755 /var/log/

chmod 644 /var/log/messages

chmod 644 /var/log/lastlog

(4)重启logstash服务:

systemctl restart logstash

tailf /var/log/logstash/logstash-plain.log

[2019-05-10T20:12:37,900][INFO ][logstash.agent] Successfully started Logstash API endpoint {:port=>9600}

(5)查看索引是否已经添加到集群:

1)在elasticsearch插件中查看:

2)在kibana中查看:

3)提示:如果没有看到内容,可能是没有日志文件中没有内容,手动在相应的日志文件中添加几条即可;

(6)在kibana中对elasticsearch集群中的当前索引进行过滤:

1)

2)

3)

4)同上理可以将logstash-system_last_log-2019.05.11索引在kibana中进行过滤;说明:elasticsearch对文本的处理较好,如果是二进制文件等特殊文件会报错;

2、收集nginx日志:

(1)nginx日志转化为json:

1)安装nginx:

yum instal nginx

2)配置文件:

vim /etc/nginx/nginx.conf #在nginx配置文件的http模块进行如下操作;

…………………………

http {

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '

# '$status $body_bytes_sent "$http_referer" '

# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log /var/log/nginx/access.log main;

log_format access_json '{"@timestamp":"$time_iso8601",'

'"server_addr":"$server_addr",'

'"remote_addr":"$remote_addr",'

'"body_bytes_sent":"$body_bytes_sent",'

'"request_time":"$request_time",'

'"upstream_response_time":"$upstream_response_time",'

'"upstream_addr":"$upstream_addr",'

'"uri":"$uri",'

'"http_referer":"$http_referer",'

'"http_user_agent":"$http_user_agent",'

'"http_x_forwarded_for":"$http_x_forwarded_for",'

'"remote_user":"$remote_user",'

'"request":"$request",'

'"status":"$status"}';

access_log /var/log/nginx/access.log access_json;

…………………………

}

3)检查配置文件:

nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

4)启动nginx:

systemctl start nginx.service

5)访问nginx:

yum install httpd-tools -y

ab -n10000 -c100 http://172.16.1.91/index.html

6)验证日志:

tail -1 /var/log/nginx/access.log

{"@timestamp":"2019-05-11T01:21:06+08:00","server_addr":"172.16.1.91","remote_addr":"172.16.1.254",
"body_bytes_sent":0,"request_time":0.000,"upstream_response_time":"-","upstream_addr":"-","uri":"/index.html",
"http_referer":"-","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
"http_x_forwarded_for": "-","remote_user": "-","request": "GET / HTTP/1.1","status":"304"}

(2)配置logstash:

1)配置日志收集文件:

vim nginx-access-log.conf

input{

file {

path => "/var/log/nginx/access.log"

type => "nginx_access_log"

start_position => "beginning"

stat_interval => "2"

}

}

output{

if [type] == "nginx_access_log" {

elasticsearch {

hosts => ["172.16.1.90:9200"]

index => "logstash-nginx_access_log-%{+YYYY.MM.dd}"

}

}

}

2)验证配置文件:

/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx-access-log.conf -t

Configuration OK

3)更改日志权限:

chmod 755 /var/log/nginx/

chmod 644 /var/log/nginx/access.log

4)重启logstash服务:

systemctl restart logstash

tailf /var/log/logstash/logstash-plain.log

[2019-05-11T09:32:01,033][INFO ][logstash.agent] Successfully started Logstash API endpoint {:port=>9600}

5)访问nginx:

ab -n10000 -c100 http://172.16.1.91/index.html

6)查看日志索引是否添加成功:

(3)在kibana中对elasticsearch集群中的当前索引进行过滤:

1)

2)

3)

3、收集tomcat日志:

(1)tomcat日志转json格式:

1)安装tomcat:

yum installl tomcat

2)配置主配置文件:

vim /etc/tomcat/server.xml #修改日志配置如下;

…………………

<!-- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

prefix="localhost_access_log." suffix=".txt"

pattern="%h %l %u %t &quot;%r&quot; %s %b" />

-->

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

prefix="tomcat_access_log" suffix=".log"

pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}"/>

</Host>

…………………

3)在站点添加index.html测试文件:

mkdir -p /usr/share/tomcat/webapps/test/

echo "tomcat" >/usr/share/tomcat/webapps/test/index.html

4)启动tomcat:

systemctl start tomcat

5)访问tomcat服务:

ab -n1000 -c100 http://172.16.1.91:8080/test/

tail -1 /var/log/tomcat/tomcat_access_log2019-05-11.log

{"clientip":"172.16.1.91","ClientUser":"-","authenticated":"-","AccessTime":"[11/May/2019:21:37:46 +0800]","method":"GET /test/ HTTP/1.0","status":"200","SendBytes":"7","Query?string":"","partner":"-","AgentVersion":"ApacheBench/2.3"}

6)验证json格式:

(2)配置logstash:

1)配置日志收集文件:

vim /etc/logstash/conf.d/tomcat-access-log.conf

input{

file {

path => "/var/log/tomcat/tomcat_access_log*.log"

type => "tomcat_access_log"

start_position => "beginning"

stat_interval => "2"

}

}

output{

if [type] == "tomcat_access_log" {

elasticsearch {

hosts => ["172.16.1.90:9200"]

index => "logstash-tomcat_access_log-%{+YYYY.MM.dd}"

}

}

}

2)验证配置文件语法:

/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tomcat-access-log.conf -t

Configuration OK

3)修改权限:

chmod 755 /var/log/tomcat/

tomcat日志文件默认是644,所以不用进行修改;

4)重启logstash:

systemctl restart logstash.service

tailf -1 /var/log/logstash/logstash-plain.log

[2019-05-11T23:18:24,701][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}

5)访问tomcat站点:

ab -n10000 -c1000 http://172.16.1.91:8080/test/index.html

6)查看索引是否被添加到elasticsearch集群:

(3)在kibana中对elasticsearch集群中的当前索引进行过滤:

1)

2)

3)

4、收集java日志:

(0)日志内容和使用语法:

1)日志示例:

tail -3 /data/logs/elk-cluster.log

[2019-05-11T20:07:17,215][INFO ][o.e.n.Node ] [elk-node2] started

[2019-05-11T20:08:09,702][INFO ][o.e.m.j.JvmGcMonitorService] [elk-node2] [gc][young][62][20] duration [763ms], collections [1]/[1.6s], total [763ms]/[2.9s], memory [225.8mb]->[147.8mb]/[1.

9gb], all_pools {[young] [84.5mb]->[4.1mb]/[133.1mb]}{[survivor] [5.2mb]->[6.8mb]/[16.6mb]}{[old] [136mb]->[136.8mb]/[1.8gb]}[2019-05-11T20:08:09,704][INFO ][o.e.m.j.JvmGcMonitorService] [elk-node2] [gc][62] overhead, spent [763ms] collecting in the last [1.6s]

提示:默认情况下一条日志就是一行,如果有特殊情况需要合并日志的需要使用该语法;

2)日志合并语法:

input {

stdin {

codec => multiline { #使用multiline插件

pattern => "pattern, a regexp" #正则匹配

negate => "true" or "false" #匹配是否成功

what => "previous" or "next" #和上面的还是和下面的内容合并

}

}

(1)配置logstash日志收集文件:

vim /etc/logstash/conf.d/elasticsearch-access-log.conf

input {

file {

path => "/data/logs/elk-cluster.log"

type => "logstash-elasticsearch_access_log"

start_position => "beginning"

stat_interval => "2"

codec => multiline {

pattern => "^\["

negate => "true"

what => "previous"

}

}

}

output {

if [type] == "logstash-elasticsearch_access_log" {

elasticsearch {

hosts => ["172.16.1.90:9200"]

index => "logstash-elasticsearch_access_log-%{+YYYY.MM.dd}"

}

}

}

(2)检查配置文件:

/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/elasticsearch-access-log.conf -t

Configuration OK

(3)修改日志权限:

chmod 755 /data/logs/

chmod 644 /data/logs/elk-cluster.log

(4)重启logstash服务:

systemctl restart logstash

tailf -1 /var/log/logstash/logstash-plain.log

[2019-05-12T00:25:43,814][INFO ][logstash.agent] Successfully started Logstash API endpoint {:port=>9600}

(5)查看索引是否被添加到elasticsearch集群:

(6)在kibana中对elasticsearch集群中的当前索引进行过滤:

1)

2)

3)

5、收集tcp网络日志:

tcp模块的使用场景如下: 有一台服务器A只需要收集一个日志,那么我们就可以不需要在这服务器上安装logstash,我们

通过在其他logstash上启用tcp模块,监听某个端口,然后我们在这个服务器A把日志通过nc发送到logstash上即可。

(1)配置logstash日志收集文件:

vim /etc/logstash/conf.d/tcp-log.conf

input {

tcp{

port => "5600"

mode => "server"

type => "tcp_5600"

}

}

output {

if [type] == "tcp_5600" {

elasticsearch {

hosts => ["172.16.1.90:9200"]

index => "logstash-tcp_5600-%{+YYYY.MM.dd}"

}

}

}

(2)检查配置文件:

/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/tcp-log.conf -t

Configuration OK

(3)重启logstash服务:

systemctl restart logstash

tail -1 /var/log/logstash/logstash-plain.log

[2019-05-12T12:05:25,367][INFO ][logstash.agent] Successfully started Logstash API endpoint {:port=>9600}

netstat -tunlp | grep 5600

tcp6 0 0 :::5600 :::* LISTEN 4120/java

(4)通过tcp发送测试数据:

[root@controller-node1 ~]# chmod 755 /var/log/

[root@controller-node1 ~]# chmod 644 /var/log/boot.log

[root@controller-node1 ~]# nc 172.16.1.91 5600 </var/log/boot.log

(5)查看索引是否被添加到elasticsearch集群:

1)

2)

(6)在kibana中对elasticsearch集群中的当前索引进行过滤:

1)

2)

3)

12.9、小结:

1、数据在集群中是分片、副本(主从备份共享)的方式进行存储;kibana和elasticsearch-head都是查看、过滤集群数据的网页浏览

软件,可以安装在集群中任何一台服务器上,可以访问集群中任意一台上的数据,但是需要开启elasticsearch的跨域访问功能;

2、在不重启logsta服务的情况下测试日志收集功能:

vim /etc/logstash/conf.d/nginx-access-log.conf

input{

file {

path => "/var/log/nginx/access.log"

type => "nginx_access_log"

start_position => "beginning"

stat_interval => "2"

}

}

output{

if [type] == "nginx_access_log" {

stdout {

codec => rubydebug

}

file {

path => "/tmp/logstash-nginx_access_log-%{+YYYY.MM.dd}.log"

}

}

}

/usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/nginx-access-log.conf

12、elk的使用(2)的更多相关文章

  1. ELK实践(一):基础入门

    虽然用了ELK很久了,但一直苦于没有自己尝试搭建过,所以想抽时间尝试尝试.原本打算按照教程 <ELK集中式日志平台之二 - 部署>(作者:樊浩柏科学院) 进行测试的,没想到一路出了很多坑, ...

  2. ELK处理Spring Boot 日志,妙!

    在排查线上异常的过程中,查询日志总是必不可缺的一部分.现今大多采用的微服务架构,日志被分散在不同的机器上,使得日志的查询变得异常困难. 工欲善其事,必先利其器.如果此时有一个统一的实时日志分析平台,那 ...

  3. ELKStack之极速入门(上)

    ELKStack之极速入门(上) 链接:https://pan.baidu.com/s/1V2aYpB86ZzxL21Hf-AF1rA 提取码:7izv 复制这段内容后打开百度网盘手机App,操作更方 ...

  4. ELK6环境搭建

    (一)什么是ELK Stack ELK 到底是什么呢? "ELK"是三个开源项目的首字母缩写,这三个项目分别是:Elasticsearch.Logstash 和 Kibana. E ...

  5. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  6. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

  7. 在mybatis中写sql语句的一些体会

    本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...

  8. 12.10、elk实用案例

    1.架构图: 服务器名称 ip地址 controller-node1(主) 172.16.1.90 slave-node1(从) 172.16.1.91 2.安装filebeat: filebeat不 ...

  9. 12、elk的使用(1)

    12.0.架构图: 服务器名称 ip地址 controller-node1(主) 172.16.1.90 slave-node1(从) 172.16.1.91 12.1.elk介绍: (1)ELK是三 ...

  10. ELK——在 CentOS/Linux 把 Kibana 3.0 部署在 Nginx 1.9.12

    上一篇文章<安装 logstash 2.2.0.elasticsearch 2.2.0 和 Kibana 3.0>,介绍了如何安装 Logstash.Elasticsearch 以及用 P ...

随机推荐

  1. [DB] 大数据集群安装

    学习要点 体系架构.原理 多做练习.试验 装虚拟机 网络模式:仅主机模式 software selection:development tools, GUI network & host na ...

  2. kenel 和shell

    开源应用/商业软件 第三方应用 命令行 交互 shell kernel 设备

  3. zabbix官方源替换为阿里云的zabbix源,一键脚本。(安装zabbix报错curl#18 - "transfer closed with 2988713 bytes remaining to read":15 ETA Trying other mirro)

    最近突然安装zabbix总是报错,比如 (24/27): t1lib-5.1.2-14.el7.x86_64.rpm | 166 kB 00:00:00 zabbix-web-4.4.6-1.el7. ...

  4. 说明位图,矢量图,像素,分辨率,PPI,DPI?

    说明位图,矢量图,像素,分辨率,PPI,DPI? 显示全部 关注者 28 被浏览 7,031 关注问题写回答 ​邀请回答 ​添加评论 ​分享 ​     2 个回答 默认排序 刘凯   21 人赞同了 ...

  5. mysql基础之数据库备份和恢复实操

    一.基于二进制文件的恢复*** 1.算好要恢复数据的时间段,重定向输入到bin.sql文件中 [root@ren7 mysql]# mysqlbinlog --start-datetime=" ...

  6. Lua中的基本函数库--(转自忧郁的加菲猫)

    基本函数库为Lua内置的函数库,不需要额外装载assert (v [, message])功能:相当于C的断言,参数:v:当表达式v为nil或false将触发错误,message:发生错误时返回的信息 ...

  7. 计划任务 at & crond tbc

    一次性任务 at 工具 由包 at 提供 依赖与atd服务,需要启动才能实现at任务 at队列存放在/var/spool/at目录中 执行任务时PATH变量的值和当前定义任务的用户身份一致 作业执行命 ...

  8. 浅析PriorityBlockingQueue优先级队列原理

    介绍 当你看本文时,需要具备以下知识点 二叉树.完全二叉树.二叉堆.二叉树的表示方法 如果上述内容不懂也没关系可以先看概念. PriorityBlockingQueue是一个无界的基于数组的优先级阻塞 ...

  9. [leetcode] 90. 子集 II.md

    90. 子集 II 78. 子集题的扩展,其中的元素可能会出现重复了 我们仍沿用78题的代码,稍作改动即可: 此时需要对nums先排个序,方便我们后面跳过选取相同的子集. 跳过选取相同的子集.当选取完 ...

  10. GO学习-(38) Go语言结构体转map[string]interface{}的若干方法

    结构体转map[string]interface{}的若干方法 本文介绍了Go语言中将结构体转成map[string]interface{}时你需要了解的"坑",也有你需要知道的若 ...