温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

logstash的更多实用功能

发布时间:2020-03-31 23:09:24 来源:网络 阅读:43189 作者:storysky 栏目:编程语言

1、合并日志
php的错误日志中常常会出现这样的日志

[03-Jun-2013 13:15:29] PHP Fatal error:  Uncaught exception 'Leb_Exception' in /data1/www/bbs.xman.com/htdocs/framework/xbox/ufo.php:68
Stack trace:
#0 /data/www/bbs.xman.com/htdocs/framework/dao/abstract.php(299): Leb_Dao_Pdo->connect(Array, 'read')
#1 /data/www/bbs.xman.com/htdocs/framework/dao/pdo.php(108): Leb_Dao_Abstract->initConnect(false)
#2 /data/www/bbs.xman.com/htdocs/framework/dao/abstract.php(1123): Leb_Dao_Pdo->query('SELECT * FROM `...')
#3 /data/www/bbs.xman.com/htdocs/framework/dao/abstract.php(1217): Leb_Dao_Abstract->select(Array)
#4 /data/www/bbs.xman.com/htdocs/framework/model.php(735): Leb_Dao_Abstract->daoSelect(Array, false)
#5 /data/www/bbs.xman.com/htdocs/app/configure/model/configure.php(40): Leb_Model->find()
#6 /data/www/bbs.xman.com/htdocs/app/search/default.php(131): Configure->get_configure_by_type('news')
#7 /data/www/bbs.xman.com/htdocs/framework/dispatcher.php(291): defaultController->indexAction()
#8 /data/www/bbs.xman.com/htdocs/framework/dispatcher.php(222): Leb_Di in /data1/www/bbs.xman.com/htdocs/framework/dao/pdo.php on line 68

 这个时候 logstash一般会只记录上面一行,所以这类的日志就看不全了。怎么办呢?logstash提供了一个功能解决了这个问题就是"multiline"
这个filter的功能顾名思义就是对多行的日志进行处理 这个是官网上的说明
multiline filter
This filter will collapse multiline messages into a single event.
The multiline filter is for combining multiple events from a single source into the same event.

下面看下格式

filter {
  multiline {
    type => "type"   #类型,不多说
    pattern => "pattern, a regexp" #参数,也可以认为是字符,有点像grep ,如果符合什么字符就交给下面的 what 去处理
    negate => boolean
    what => "previous" or "next" #这个是符合上面 pattern 的要求后具体怎么处理,处理方法有两种,合并到上面一条日志或者下面的日志
  }
}

The 'negate' can be "true" or "false" (defaults false). If true, a message not matching the pattern will constitute a match of the multiline filter and the what will be applied. (vice-versa is also true)
这个 negate 有两种 true 或者 false,默认是 true,如果选了false 的话估计就是取反的意思。
看看例子

filter {
  multiline {
    pattern => "^[^\[]"
    what => "previous"
  }
  }

这个例子是针对我上面的php日志写的,意思就是 如果不是以 "["开头的日志 都跟上一个日志合并在一起。以此类推遇到其他的多行日志也可以按照这个方法来做合并。


2、logstash 根据@message内容来触发命令"exec"
 我当初的想法是这样的,如果php日志文件中出现 "PHP Fatal error"的时候将相关的错误日志发给相关开发的负责人。一开始想到了 output 的 email 功能,但我尝试
了很多次这个email功能不太稳定,有时候能发出来邮件有的时候却发不出来。不知道是邮件服务器的问题还是 logstash本身的问题,具体配置如下


output {
email {
   match => [ "@message", "aaaaa" ]
   to => "storyskya@gmail.com"
   from => "monitor@mib.com.cn"
   options => [ "smtpIporHost", "smtp.mibnet.com",
                "port", "25",
                "userName", "monitor@mib.com.cn",
                "starttls", "true",
                "password", "opmonitor",
                "authenticationType", "login"
              ]
   subject => "123"
   body => '123'
   via => smtp
}
}

后来换了方法,改用 grep+exec来做,具体思路就是 grep 过滤到了 PHP Fatal error 之后根据域名将邮件发给具体人员,格式如下

filter {
  grep {
    match => [ "@message", "PHP Fatal error" ]
    drop  => false
    add_tag => [fatal_error]
       }
                                                  
       grep {
       tags => [fatal_error]
       match => [ "@message", ".*(xbox\.com|xbox\.mib\.com\.cn|supports\.game\.mib\.com\.cn)" ]
       drop  => false
       add_tag => [xboxerror]
            }
         }
output {
  exec {
    tags => [xboxerror]
    command => "echo '%{@timestamp} %{@source}: %{@message}' | mail -s xbox_phplog_error_message xboxdev@xman.com"
        }
}

如此这般 先定义一个grep tag 为fatal_error 先把带有 PHP Fatal error 的日志过滤出来,后面的 grep承接上面的 tag fatal_error 过滤出具体的域名之后再新建一个
tag 叫 xboxerror,最后的output 调用 exec(执行) 去调用一个命令,将时间,源和错误信息发到xboxdev@xman.com 这个邮箱里。
OK 现在就能做到快速的邮件报警了。

3、替换
上面做到了邮件实时提醒,但有的时候我发现并没有发出去邮件,查找原因后发现如果邮件内容中出现很多 单引号 " ' " 的话,mail命令就会报错没法发送。
于是就找到了mutate 这个功能,下面是介绍
The mutate filter allows you to do general mutations to fields. You can rename, remove, replace, and modify fields in your events.
好吧,我现在需要把@message里面的 ' 都给替换成" 这样就能正常发邮件了,什么你说开发换了个符号会看不懂?我#¥%……&*((&……%¥%
格式如下:

mutate {
    type => "phplog"
    gsub => [ "@message","'", "\"" ]
}

好了,有了这些功能模块logstash可以工作的比较开心了~~~ 希望你们也开心

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI