logstash安装及使用

#解压logstash包

tar -zxf logstash-6.2.2.tar.gz

#移到soft文件下创建logstash622文件夹

mv logstash-6.2.2 soft/logstash622

#进去至bin目录

cd soft/logstash622/bin/

#空白输入输出

./logstash -e 'input { stdin {} } output { stdout {} }'

#环境变量

./logstash -e 'input { stdin {} } output { stdout { codec => rubydebug}  }'

#创建config文件夹

mkdir /opt/config

#进去config

cd /opt/config/

编辑文本

vi mylog.conf      
↓
input {
    stdin {}
}
output {
    stdout {
        codec => rubydebug
    }
}

#加载环境变量文件

./logstash -f /opt/config/mylog.conf 

#创建一个文本

vi aa.txt

(随便输入字)

#进去config

cd /opt/config/

编辑文本 (全量读取:没有更改的情况下只可以读取一次)

vi mylog.conf 
↓
input {
    file {
        path => "/opt/aa.txt"
        start_position => "beginning"
    }
}
output {
    stdout {
        codec => rubydebug
    }
}

#加载环境变量文件

./logstash -f /opt/config/mylog.conf

#全量读取:随时读取

input {
    file {
        path => "/opt/aa.txt"
        start_position => "beginning"
        sincedb_path => "/dev/null"
    }
}
output {
    stdout {
        codec => rubydebug
    }
}

#读取json文件

input {
    file {
        path => "/opt/aa.txt"
        start_position => "beginning"
        sincedb_path => "/dev/null"
        codec => json
    }
}
output {
    stdout {
        codec => rubydebug
    }
}

同级读取json日志文件

input {
    file {
        path => "/opt/aa.txt"
        start_position => "beginning"
        sincedb_path => "/dev/null"
        codec => json
    } 
}
filter {
        mutate {
            add_field => { "@adv" => "%{cm}" }
        }
        json {
            source => "@adv"
            remove_field => [ "@adv","cm" ]
        }
}
output {
    stdout {
        codec => rubydebug
    }
}
(
78461|event_login|958975260460|192.168.56.233
40265|event_login|960670860460|192.168.56.247
37170|event_login|962614860460|192.168.56.133
)

正则表达式的匹配

input {
    file {
        path => "/opt/aa.txt"
        start_position => "beginning"
        sincedb_path => "/dev/null"
    }
}
filter {
    grok {
        match => { "message" => "%{NUMBER:user_id}\|%{WORD:event_name}\|%{NUMBER:times}\|%{IP:client_address}" }
        remove_field => ["message"]
    }
}
output {
    stdout {
        codec => rubydebug
    }
}
input {
    file {
        path => "/opt/aa.txt"
        start_position => "beginning"
        sincedb_path => "/dev/null"
    }
}
filter {
    grok {
        match => { "message" => "(?<userid>[0-9]+)\|(?<event_name>[a-zA-Z_]+)\|(?<times>[0-9]+)\|(?<clientip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" }
        remove_field => ["message"]
    }
}
output {
    stdout {
        codec => rubydebug
    }
}

多类型文件解析

input {
    file {
        path => "/opt/aa.txt"
        start_position => "beginning"
        sincedb_path => "/dev/null"
        type => "system"
    }
    file {
        path => "/opt/bb.txt"
        start_position => "beginning"
        sincedb_path => "/dev/null"
        codec => json
        type => "action"
    }
}
filter {
    if [type] == "system" {

        grok {
            match => { "message" => "(?<userid>[0-9]+)\|(?<event_name>[a-zA-Z_]+)\|(?<times>[0-9]+)\|(?<clientip>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" }
            remove_field => ["message"]
        }
    }else {
        mutate {
                add_field => { "@adv" => "%{cm}" }
        }
        json {
                source => "@adv"
                remove_field => [ "@adv","cm" ] 
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值