Logstash 解析Json字符串,删除json嵌套字段
一、场景:此文以一个简单的json字符串文件为例,描述如何用logstash解析嵌套的json,并删除其中的某些字段
我们在linux中test.json的内容如下:
{"timestamp":"2018-08-02T14:42:50.084467+0800","flow_id":364959073190719,"in_iface":"eth1","event_type":"alert","src_ip":"10.0.0.4","src_port":80,"dest_ip":"10.0.0.5","dest_port":16781,"proto":"TCP","tx_id":0,"alert":{"action":"allowed","gid":1,"signature_id":2101201,"rev":10,"signature":"GPL WEB_SERVER 403 Forbidden","category":"Attempted Information Leak","severity":2},"http":{"hostname":"bapi.yahoo.com","url":"\/v1tns\/searchorderlist?_time=1533192163978","http_user_agent":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/67.0.3396.99 Safari\/537.36","xff":"39.106.108.38","http_content_type":"text\/html","http_method":"POST","protocol":"HTTP\/1.0","status":403,"length":568},"app_proto":"http","flow":{"pkts_toserver":5,"pkts_toclient":5,"bytes_toserver":1547,"bytes_toclient":1076,"start":"2018-08-02T14:42:50.082751+0800"}}
为了方便查看,formate后,为如下格式
{
"timestamp":"2018-08-02T14:42:50.084467+0800",
"flow_id":364959073190719,
"in_iface":"eth1",
"event_type":"alert",
"src_ip":"10.0.0.4",
"src_port":80,
"dest_ip":"10.0.0.5",
"dest_port":16781,
"proto":"TCP",
"tx_id":0,
"alert":{
"action":"allowed",
"gid":1,
"signature_id":2101201,
"rev":10,
"signature":"GPL WEB_SERVER 403 Forbidden",
"category":"Attempted Information Leak",
"severity":2
},
"http":{
"hostname":"bapi.yahoo.com",
"url":"\/v1tns\/searchorderlist?_time=1533192163978",
"http_user_agent":"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/67.0.3396.99 Safari\/537.36",
"xff":"39.106.108.38",
"http_content_type":"text\/html",
"http_method":"POST",
"protocol":"HTTP\/1.0",
"status":403,
"length":568
},
"app_proto":"http",
"flow":{
"pkts_toserver":5,
"pkts_toclient":5,
"bytes_toserver":1547,
"bytes_toclient":1076,
"start":"2018-08-02T14:42:50.082751+0800"
}
}
二、目的: 我们需要解析这个json,并且删除json中"src_ip"字段和"http下的hostname"这个字段
我的配置文件如下:
input {
file {
path => "/usr/share/logstash/private.cond/nestjson.json"
codec => "json"
start_position => "beginning"
sincedb_path => "/dev/null"
}
}
filter {
json {
source => "message"
}
mutate {
remove_field => ["src_ip","[http][hostname]"]
}
}
output {
stdout {
codec => rubydebug
}
}
注意第14行删除
注意第14行删除字段和嵌套字段的写法
运行logstash我们得到如下输出:
{
"alert" => {
"gid" => 1,
"rev" => 10,
"severity" => 2,
"signature" => "GPL WEB_SERVER 403 Forbidden",
"action" => "allowed",
"signature_id" => 2101201,
"category" => "Attempted Information Leak"
},
"http" => {
"protocol" => "HTTP/1.0",
"http_content_type" => "text/html",
"http_user_agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"http_method" => "POST",
"length" => 568,
"url" => "/v1tns/searchorderlist?_time=1533192163978",
"xff" => "39.106.108.38",
"status" => 403
},
"path" => "/usr/share/logstash/private.cond/test.json",
"event_type" => "alert",
"src_port" => 80,
"dest_port" => 16781,
"dest_ip" => "10.0.0.5",
"proto" => "TCP",
"flow_id" => 364959073190719,
"tx_id" => 0,
"@version" => "1",
"in_iface" => "eth1",
"timestamp" => "2018-08-02T14:42:50.084467+0800",
"flow" => {
"pkts_toserver" => 5,
"pkts_toclient" => 5,
"bytes_toserver" => 1547,
"start" => "2018-08-02T14:42:50.082751+0800",
"bytes_toclient" => 1076
},
"host" => "elk",
"app_proto" => "http",
"@timestamp" => 2018-08-02T10:14:14.372Z
}
我们可以看到src_ip和http下的hostname已经被成功删除
Logstash 解析Json字符串,删除json嵌套字段的更多相关文章
- Gson解析json字符串、json数组转换成对象
实体类: public class Product { private int id; private String name; private String date; public int get ...
- Java解析json字符串和json数组
Java解析json字符串和json数组 public static Map<String, String> getUploadTransactions(String json){ Map ...
- JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串;JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串:JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
- js 将json字符串转换为json对象的方法解析-转
例如: JSON字符串:var str1 = '{ "name": "cxh", "sex": "man" }'; JS ...
- 使用python解析Json字符串-获取Json字符串关键字
import json data = { "statusCode": 0, "data": { ", "height": &quo ...
- js 将json字符串转换为json对象的方法解析
推荐: var obj = eval('(' + str + ')'); var last=JSON.stringify(obj); //将JSON对象转化为string字符 例如: JSON字符串: ...
- javaScript 将json字符串转换为json对象的方法解析
JSON字符串: var str1 = '{ "name": "cxh", "sex": "man" }'; JSON对 ...
- Json字符串和Json对象的简单总结
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON成为理想的数据交换语言. 易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率). ...
- 【荐】使用eval()、new Function()将JSON字符串转换为JSON对象
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...
- JSON字符串与JSON对象的区别
Q:什么是"JSON字符串",什么是"JSON对象",两者的区别? a.JSON对象是直接可以使用JQuery操作的格式,如C#中可以用对象(类名)点出属性(方 ...
随机推荐
- ActiveMQ 填坑记
前言 MQ是现在大型系统架构中必不可少的一个重要中间件,之前有偏文章<MQ(消息队列)常见的应用场景解析>介绍过MQ的应用场景,现在流行的几个MQ是rabbitmq,rocketma,ka ...
- Ubuntu apt-get提示被锁住
执行 apt-get 时提示资源被锁住 E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavai ...
- Cobbler自动化批量安装Linux操作系统 - 运维总结
一.Cobbler简述 Cobbler是一个自动化和简化系统安装的工具,通过使用网络引导来控制和启动安装.Cobbler的特性包括存储库镜像.Kickstart模板和连接电源管理系统.Cobbler通 ...
- 修改docker的地址为阿里云源
https://blog.csdn.net/jacabe/article/details/78575316
- 12.16 Daily Scrum
Today's Task Tomorrow's Task 丁辛 实现和菜谱相关的餐厅列表. 实现和菜谱相关的餐厅列表. 邓亚梅 美化搜索框UI. 美 ...
- 数组与字符串三(Cocos2d-x 3.x _Array容器)
"程序=数据结构+算法" 在面向对象的语言中,诸如数组.堆栈.队列等的结构都被封装成了特定的类,按照特定数据结构的算法设计起来,这就是容器类. Cocos2d-x中,能使用的容器类 ...
- HDU 2052 Picture
http://acm.hdu.edu.cn/showproblem.php?pid=2052 Problem Description Give you the width and height of ...
- linux中tomcat修改错误日志路径
涉及文件 log4j.properties (一般开发将该文件放在项目的缺省目录即源包下,在文件系统里,就是在项目的/src/java目录下,缺省的文件名是log4j.properties,这样项 ...
- codeforces515B
Drazil and His Happy Friends CodeForces - 515B Drazil有很多朋友,他们中有些人是快乐的,有些人是不快乐的. Drazil想让他的朋友变得快乐.于是, ...
- python----特殊闭包
1.闭包的定义: 当子函数有调用父级函数变量(非全局变量)并返回子函数名时父级函数不会随着函数运行而结束释放,而是会保存状态等待子函数的调用. 我们常见的闭包类型: def fun1(): x = 1 ...