ELK基础篇3:logstash的安装及使用

1、安装logstash

logstash的安装并没像elasticsearch和kibana那样要求在linux系统下非root用户运行,我们这里在root用户下安装logstash6.x版本。

但是安装logstash之前必须确定已经按照jdk,如果我们使用的logstash6.x版本,则jdk必须是1.8版本及以上版本。

【安装logstash】

[root@wzy-yunwei soft]# tar -zxf logstash-6.1.1.tar.gz
[root@wzy-yunwei soft]# mv logstash-6.1.1 /usr/local/
[root@wzy-yunwei soft]# ll -h /usr/local/logstash-6.1.1/

【修改logstash的jvm】

根据自己服务器的情况进行修改,默认是1g

[root@wzy-yunwei soft]# vim /usr/local/logstash-6.1.1/config/jvm.options
-Xms512M
-Xmx512M

【logstash标准输入输出】

[root@wzy_woyun ~]# cd /usr/local/logstash-6.5.1/config/
[root@wzy_woyun config]# cat std.conf 
#输入
input{
    stdin{
      }
}

#输出
output{
    stdout{
       codec=>rubydebug
      
       }
}

【启动】

我们这里先编写一个启动脚本start.sh

#编写启动脚本
[root@wzy-yunwei soft]# vim /usr/local/logstash-6.1.1/bin/start.sh
nohup /usr/local/logstash-6.1.1/bin/logstash -f /usr/local/logstash-6.1.1/config/logstash.conf >>/tmp/logstash.log 2>>/tmp/logstash.log &
#脚本权限修改
[root@wzy-yunwei soft]# chmod a+x /usr/local/logstash-6.1.1/bin/start.sh
#启动logstash
[root@wzy-yunwei soft]# /usr/local/logstash-6.1.1/bin/start.sh

如果我们想要对logstash的conf文件进行检查,那么我可以安装下面写法:

[root@wzy-yunwei soft]#/usr/local/logstash-6.5.1/bin/logstash -f  /usr/local/logstash-6.5.1/config/logstash.conf -t
#注意上面的-f参数表示指定配置文件;-t表示检查配置文件是否正确

【检查启动是否正常】

[root@wzy_woyun config]# jps
10885 Logstash
5144 Elasticsearch
25832 Jps
#通过jps命令我们就能发现有Logstash
[root@wzy_woyun config]# ps -ef |grep logstash

2、logstash中使用GeoIP

GeoIP根据ip地址就能显示位置信息,而GeoIP是离线的库;我们这里采用的是GeoIP的免费版本。

注意:这里需要强调的是,如果使用GeoIP显示地图,那么logstash中定义的索引index必须是【logstash-】开头

【GeoIP安装】

#下载免费GeoIP
[root@wzy_woyun soft]# wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
#解压缩
[root@wzy_woyun soft]# tar -zxvf GeoLite2-City.tar.gz 
GeoLite2-City_20181218/
GeoLite2-City_20181218/COPYRIGHT.txt
GeoLite2-City_20181218/README.txt
GeoLite2-City_20181218/LICENSE.txt
GeoLite2-City_20181218/GeoLite2-City.mmdb
[root@wzy_woyun soft]# cd GeoLite2-City_20181218
#把离线的GeoIP库复制到logstash的安装目录下
[root@wzy_woyun GeoLite2-City_20181218]# cp GeoLite2-City.mmdb /usr/local/logstash-6.5.1/
#添加GeoIP之后检查logstash配置文件
[root@wzy_woyun conf.d]# /usr/local/logstash-6.5.1/bin/logstash -f /usr/local/logstash-6.5.1/conf.d/aliyun_nginx.conf -t
Sending Logstash logs to /usr/local/logstash-6.5.1/logs which is now configured via log4j2.properties
[2018-12-27T13:44:55,340][WARN ][logstash.config.source.multilocal] Ignoring the 'pipelines.yml' file because modules or command line options are specified
Configuration OK
[2018-12-27T13:44:58,009][INFO ][logstash.runner          ] Using config.test_and_exit mode. Config Validation Result: OK. Exiting Logstash
#启动logstash服务
[root@wzy_woyun conf.d]# /usr/local/logstash-6.5.1/bin/logstash -f /usr/local/logstash-6.5.1/conf.d/aliyun_nginx.conf >> /tmp/logstash.log &
[1] 16944

【logstash配置文件】

我们需要在logstash配置文件的输入中添加过滤器。

filter {  
  
 if [fields][type] == "wzy"  { 
       geoip {
            source => "client" 
            target => "geoip"
            database => "/usr/local/logstash-6.5.1/GeoLite2-City.mmdb" 
            add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
            add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
        }
      
      mutate {
         convert => [ "[geoip][coordinates]", "float"]
        }
  }

 说明:

(1)geoip: IP查询插件;
(2)source: 需要通过geoip插件处理的field,一般为ip,这里因为通过控制台手动输入的是ip所以直接填message,生成环境中如果查询nginx访问用户,需先将客户端ip过滤出来,然后这里填clientip即可;
(3)target: 解析后的Geoip地址数据,应该存放在哪一个字段中,默认是geoip这个字段;
(4)database: 指定下载的数据库文件;
(5)add_field: 这里两行是添加经纬度,地图中地区显示是根据经纬度来识别。.

【KIbana中展示地图】

选择“Visualize”---》“Maps”---》“Coordinate Map”选择索引,具体操作如下:

3、logstash配置文件详解

【输入参数说明】

参数名称

含义

path =>

表示监听的文件路径,多个文件用[“file1.log”,”file2.log”]实现

type =>

给类型打上标记,用来区分不同的文件类型

start_postion => “beginning”

从哪里开始记录文件,默认是从文件结尾开始标记,要是想从开始导入一个文件值设为”beginning”

sincedb_path => “/dev/null”

监控库存放的位置,默认在/data/plugins/inputs/file

discover_interval => “15s”

多久去监听path下是否有文件,默认是15s;

exclude =>

排除什么文件

close_older =>

监听文件,如果操作这个事件就关闭文件句柄,默认是3600s;

sincedb_write_interval => logstash

每隔多久写一次sincedb文件,默认是15s

stat_interval=>logstash

每隔多久检查一次被监听文件状态是否有更新,默认是1秒

 

 

【输出参数说明】 

参数

说明

if[type] == “command”

是我们之前在input时候添加的,这个参数非常有作用,当我们添加多个匹配conf文件的时候,就可以生成不同的索引。

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

index比较关键和重要。可以使用别的,但是建议使用logstash*这样的格式,因为es支持预定义mapping.logstash打头的logstash自带一套优化好的模板,所以创建索引是logstash-*这样的格式

flush_size => 20000

logstash的包数量达到2000个才批量提交到es,默认是500个。

idle_flush_time => 10

多长时间发送数据

 

 

猜你喜欢

转载自blog.csdn.net/u013089490/article/details/85338892