centos yum logstash5.x安装及简单运用

centos环境推荐用yum安装的方式,至于其他方式,请关注官方文档(https://www.elastic.co/guide/en/logstash/current/installing-logstash.html

1、Download and install the public signing key:

rpm -- import  https: //artifacts.elastic.co/GPG-KEY-elasticsearch

2、添加repo

添加如下内容,到vi /etc/yum.repos.d/logstash.repo 目录,以.repo结尾,推荐为:

[logstash- 5 .x]
name=Elastic repository  for  5 .x packages
baseurl=https: //artifacts.elastic.co/packages/5.x/yum
gpgcheck= 1
gpgkey=https: //artifacts.elastic.co/GPG-KEY-elasticsearch
enabled= 1
autorefresh= 1
type=rpm-md

3、最后执行命令安装;

sudo yum install logstash


vi /etc/logstash/conf.d/syslog.conf




input {  # 定义日志源
  syslog {
    type => "system-syslog"  # 定义类型
    port => 10514    # 定义监听端口
  }
  file {
        path => ["/data/logs/logstash-001.log"]
        start_position => "beginning"
   }
}
filter{
  ruby {
        code=>"
        msg=event.get('message')
        #event.set('msg',msg)
        if msg.length>209
          msg=msg[70,11]
          #event.set('aa',msg)
          if msg=='{\"app_name'
            event.set('type','api_business')
          elsif msg=='Resolving e'
            event.set('type','empty')
          end
        else
          event.set('type','empty')
        end
        "
   }
   json {
       source => "message"
    }
   if [type] == "api_business" {
        json{
          source => "message"
          target => "response"
         }
    }
}
output {  # 定义日志输出
   if [type] != "empty" {
       elasticsearch {
        hosts => ["127.0.0.1:9200"]
        index => "logstash-log-%{+YYYY.MM}"
        codec => rubydebug
      }
    } 


   if [type] == "api_business" {
       elasticsearch {
         hosts => ["127.0.0.1:9200"]
         index => "api-bus-log-%{+YYYY.MM}"
         codec => rubydebug
       }
    }
}

elasticsearch 

查看index
curl 'localhost:9200/_cat/indices?v'
删除index
curl -X DELETE 'localhost:9200/sdf-hdfs-audit-2017.04*' 


启动项目

systemctl start logstash.service

配置参考文件

Filter

filter {
  grok {
     match => { "path" => "/usr/local/test/logs/%{DATA:fileName}.log"}  #获取log文件名
  }
  if [message] =~ ".*Exception.*" {  # 格式化日志信息
    grok {
        match => { "message" => ".*%{LOGLEVEL:priority}.*uid=%{NUMBER:uid},traceId=%{DATA:traceId} .*\(%{DATA:method}@%{DATA:class}\.java:%{NUMBER:line}\)" }
    }
  }
  else {
    grok {
        mutate { replace => { "type" => "apache_access" } }
        match => {" message " => "\s*\[impl\]\[in\]requestId=%{NUMBER:reqId},reqTime=%{NUMBER:reqTime} req=%{GREEDYDATA:req}" }  # 会自动生成相应的JSON的_source的key-value
    }
  }
}

正则匹配测试

http://grokdebug.herokuapp.com 预先检验grok的正则匹配是否正确

1. 预警及发送http提醒

input {
    # 略
}
filter {
  grok {
     match => { "message" => "\s*\[impl\]\[in\] traceId=%{NUMBER:traceId},reqTime=%{NUMBER:reqTime} req=%{GREEDYDATA:req}" } 
    # remove_field => [ "message" ]
  }
  if [priority] == "ERROR" { #是错误 添加标记
     grok {
        tag_on_failure => "error"
     }
  }
  # 计数
  metrics {
      # 每60秒清空计数器  #should be a multiple of 5s
      clear_interval =>60
      # 每隔60秒统计一次  #Must be a multiple of 5s
      flush_interval =>60
      # 计数器数据保存的字段名 priority的值默认就有 是日志的级别
      meter => "events_%{priority}"
      # 增加"metric",作为标记
      add_tag => "metric"
      # 3秒内的message数据才统计,避免延迟
      # ignore_older_than => 3
  }
  # 如果event中标记为“metric”的
  if "metric" in [tags] {
      # 执行ruby代码
      ruby {
          # 如果level为warn的数量小于3条,就忽略此事件(即不发送任何消息)。
          code => "event.cancel if event['events_WARN']['count'] < 3"
      }
  }
}
output {
  # 如果event中标记为“metric”,表示只发送计数的消息。
  if "metric" in [tags]{
   # 通过http请求公众号发送微信消息。(此处简略描述)
      http {
          http_method => "post"  
          url => "http://116.1.1.112:33333/xxx?count=%{[events_WARN][count]}"
      }
  }
  stdout { codec => rubydebug } # 标准输出

#  elasticsearch {
#    action => "index"          #The operation on ES
#    hosts  => "127.0.0.1:9344"   #ElasticSearch host, can be array.
#    index  => "logstash-%{+YYYY.MM.dd}"         #The index to write data to.
#  }
}

2. 预警 发送email提醒

# input 与 filter 参考上面的
output {
  # 如果event中标记为“error”,表示出错
  if "error" in [tags] {            
     email { 
        from => "shyn_xxxxx@163.com" 
        to => "123456@qq.com" 
        via => "smtp" 
        port => 25
        subject => "[%{@timestamp} xxx服务器 日志发现异常!]" 
        address => "smtp.163.com" 
        domain => "smtp.163.com" 
        body => "u have new bug ! %{message}" 
        username => "shyn_xxxxx@163.com" 
        password => "password" 
     }
  }
  stdout { codec => rubydebug }
}

3. 指定Exeception异常提醒

filter {
  if [message] =~ "\s*(Arithmetic|Runtime)Exception\s*" {
    ruby {
         code => “”
        add_tag => "specific_exeception"
    }
  }
  grok {
     match => { "message" => "\s*\[impl\]\[in\] traceId=%{NUMBER:traceId},reqTime=%{NUMBER:reqTime} req=%{GREEDYDATA:req}" }
  }
  if [priority] == "ERROR" {
     grok {
        tag_on_failure => "error"
     }
  }
}

Logstash的一些配置
file {  
  path => "/data/web/logstash/logFile/*/*"
  start_position => "beginning" #从文件开始处读写
}


* 有一些比较有用的配置项,可以用来指定 FileWatch 库的行为:

discover_interval

logstash 每隔多久去检查一次被监听的 path 下是否有新文件。默认值是 15 秒。

exclude

不想被监听的文件可以排除出去,这里跟 path 一样支持 glob 展开。

sincedb_path

如果你不想用默认的 $HOME/.sincedb(Windows 平台上在C:\Windows\System32\config\systemprofile\.sincedb),可以通过这个配置定义 sincedb 文件到其他位置。

sincedb_write_interval

logstash 每隔多久写一次 sincedb 文件,默认是 15 秒。

stat_interval

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

start_position

logstash 从什么位置开始读取文件数据,默认是结束位置,也就是说 logstash 进程会以类似 tail -F 的形式运行。如果你是要导入原有数据,把这个设定改成 "beginning",logstash 进程就从头开始读取,有点类似cat,但是读到最后一行不会终止,而是继续变成 tail -F。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值