Parse JSON message in Logstash - json

I am sending my jenkins logs to logstash with following config:
redis {
host => "localhost"
key => "logstash"
data_type => "list"
codec => json
}
This works as smooth as expected, now i see follwoing message in KIBANA:
{
"_index": "logstash-2015.12.18",
"_type": "logs",
"_id": "AVG1BN5LXZBIbp7HE4xN",
"_score": null,
"_source": {
"data": {
"id": "965",
"projectName": "NicePJ",
"displayName": "#965",
"fullDisplayName": "NicePJ",
"url": "job/NIcePJ/965/",
"buildHost": "Jenkins",
"buildLabel": "master",
"buildNum": 965,
"buildDuration": 1,
"rootProjectName": "NicePJ",
"rootProjectDisplayName": "#965",
"rootBuildNum": 965,
"buildVariables": {
"target_SUT": "0201",
"report_warnings": "false",
"product": "Ours",
"testsuite": "Exciting_stuff5",
"qft_version": "current",
"target_task": "t324",
"branch": "test",
"testcase": "",
"revision": "HEAD",
"node": "hsqs960",
"client": "Desktop",
"run_specific_test": "false",
"user": "xxxxx"
}
},
"message": [
"A This is a message XYZ"
],
"source": "jenkins",
"source_host": "http://serverXL:8080/",
"#timestamp": "2015-12-18T12:16:02.000Z",
"#version": 1
},
"fields": {
"#timestamp": [
1450440962000
]
},
"sort": [
1450440962000
]
}
Now i want to filter the message field for certain messages, but i cant get it work. How can i filter the message field and how can i access the buildHost field to use it in an if statement in the pipeline?
Following i tried after many examples:
if[data][buildHost]== "jenkins"
{
grok
{
match => { "message[0]" => "\[exec\]\s*\<%{GREEDYDATA:test}\s*\[%{GREEDYDATA:result}\]" }
}
}
But this is just not working at all, please help me out.

Conditional
The == compares simple string and case sensitive, so "jenkins" will not match as your data shows ("buildHost": "Jenkins",):
if[data][buildHost]== "jenkins"
But following does:
if[data][buildHost]== "Jenkins"
If you need match both, you can either use || or regex =~.
Grok
The grok is a filter to parse message with regex pattern. You can test your regex pattern with
online grok debugger
Kibana dev tools's grok debugger

Related

Json Extractor in JMeter

I am using JSON extractor in JMeter. Below is my Response Body. I am using the Json path expression to capture the value, which is working fine.
Apart from the above condition, I need to add one more condition.
If the "travelID" length is equal to 33, then only I need to get the BoundID.
Example : AAA-AB1234-AAABBB-2022-11-10-1111
Total length or count of the above travelID is 33, but sometime I used to get 31,32 also but I need to capture the Bound ID only when the length is 33. Is that feasible ? Please help on the same
PFB sample response body.
{
"data": {
"RenewalDetails": [
{
"ExpiryDetails": {
"duration": "xxxxx",
"destination": "XXX",
"from": "XXX",
"value": 2,
"segments": [
{
"valudeid": "xxx-xx6262-xxxyyy-1111-11-11-1111"
}
]
},
"Itemdetails": [
{
"BoundId": "xxx-1-xxx1-111111111111-1",
"isexpired": true,
"FamilyCode": "PREMIUM",
"availabilityDetails": [
{
"travelID": "AAA-AB1234-AAABBB-2022-11-10-1111",
"quota": "X",
"scale": "XXX",
"class": "X"
}
]
}
]
}
]
},
"warnings": [
{
"code": "xxxx",
"detail": "xxxxxxxx",
"title": "xxxxxxxx"
}
]
}
I don't think it's possible with JSON Extractor, I would rather suggest going for JSR223 PostProcessor and the following Groovy code:
def BoundId = new groovy.json.JsonSlurper().parse(prev.getResponseData())
.data.RenewalDetails[0].Itemdetails.find { itemDetail ->
itemDetail.availabilityDetails[0].travelID.length() == 33
}?.BoundId
vars.put('BoundId', BoundId ?: 'Not Found')
You will be able to refer extracted value as ${BoundId} later on where required.

Not able to retrieve value in JSON response

I am working on API response validation. I am trying to get the value of the particular field in the API response for validation.
This is my API response.
{
"currentdatetime": "11",
"syncinterval_seconds": 4,
"public_keys": [
{
"system": "aa",
"pubkey": "aa"
}
],
"users": [
{
"username": "user",
"user_id": "user",
"realname": "user_10",
"emailaddress": "test#gmail.com",
"accountenabled": false,
"mustchangepassword": false,
"passwordhash": "$E6gi",
"accesslevel": [
"WORKER"
],
"expiration": "2022-11-02T16:21:52",
"site_specific": false,
"symkey": "aaaa",
"privatekey": "aaa"
},
]
}
I am trying to get username /user id from users through robot code.Like this
${response}= GET ${Base_URL}/v1/sync/users ${data} headers=${header}
${origin}= Get Json Value From String ${response.content} users
${lines} = Set Variable ${origin.content['users']['username']}
It is throwing a "type" error.
Any help will be much appreciated.

How to get the index of the object from json file in python

I have a json file with this structure
"user_data": {
"0": {
"user_name": "LinkedIn",
"type": "textfile"
"id": "543211",
"all_tickets": [
"56788",
"43224",
"56788"
]
};
"1": {
"user_name": "stackoverflow",
"type": "textfile"
"id": "123456",
"all_tickets": [
"98134",
"76372",
"44522"
]
}
}
}
Whenever user inputs the same data, I want to check against the existing one in the json file and if it matches, I want to return the index. How can I get it?
For example, if a user inputs the same keys and values of :
"user_name": "LinkedIn",
"type": "textfile"
"id": "543211",
"all_tickets": [
"56788",
"43224",
"56788"
]
The output should be 0(since that is the index)

How to save data in Json format in React-admin v3?

I created JsonInput, which sends a json object to the input SimpleForm, however, a modified version of it gets into the Data Provider.
that's what is sent to the input:
json: {
"FunctionalGroup": [
{
"uaIDref": [
"2104"
],
"_Name": "Текущие параметры",
"_ID": "33"
},
{
"uaIDref": [
"2100"
],
"_Name": "Текущие параметры пониженной точности",
"_ID": "34"
},
],
"_Name": "Прибор 1",
"_ID": "32"
}
that's what came to the server:
json: {
"FunctionalGroup": [
{
"uaIDref": [
"2104"
],
"_Name": "Текущие параметры",
"_ID": "33"
},
{
"uaIDref": [
"2100"
],
"_Name": "Текущие параметры пониженной точности",
"_ID": "34"
}
],
"FunctionalGroupIds": "",
"_Name": "Прибор 1",
"_ID": "32"
},
after a response from the server with this object, this is what came in the field:
json: {
FunctionalGroup: [
{
uaIDref: [
'2104'
]
},
{
uaIDref: [
'2100'
]
}
],
FunctionalGroupIds: ''
}
what kind of magic is this?
I did some tests. When I converted the object to a string, the correct json was sent to the server, however, in the field after the server responded, the modified object was displayed again.
I found out that the conversion is related to redux.js, but what exactly happens is not understood.
Please give any comments on this.
How can I make save json?

Elasticsearch - completion suggester payload transforming, returns invalid JSON

I am trying to use the elasticsearch completion suggester. I have app_user objects, which come into my elasticsearch instance via a couchdb river.
This is the mapping I use:
{
"app_user" : {
"_all" : {"enabled" : true},
"_source" : {
"includes" : [
"_id",
"_rev",
"type",
"profile.callname",
"profile.fullname",
"email"
]
},
"properties" : {
"suggest" : { "type" : "completion",
"index_analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
}
},
"transform" : [
{"script": "ctx._source.suggest = ['input':[ctx._source.email, ctx._source.profile.fullname, ctx._source.profile.callname]]"},
{"script": "ctx._source.suggest.payload = ['_id': ctx._source['_id'], 'type': ctx._source['type'], '_rev': ctx._source['_rev']]"}
,
{"script": "ctx._source.suggest.payload << ['label': ctx._source.profile.fullname, 'text': ctx._source.email]"}
]
}
}
So I am trying to include the object ID and a display text in the payload.
When I view the generated document via http://localhost:9200/myindex/app_user/<someid>?pretty&_source_transform, everything seems OK:
{
"_index": "myindex",
"_type": "app_user",
"_id": "<someid>",
"found": true,
"_source": {
"_rev": "2-dcd7b9d456e205d3e9d859fdc2c6a688",
"_id": "<someid>",
"email": "joni#example.org",
"suggest": {
"input": [
"joni#example.org",
...
],
"output": "joni surname - joni#example.org",
"payload": {
"_id": "<someid>",
"type": "app_user",
"_rev": "2-dcd7b9d456e205d3e9d859fdc2c6a688",
"label": "joni surname",
"text": "joni#example.org"
}
},
"type": "app_user",
"profile": {
"callname": "",
"fullname": "joni surname"
}
}
}
However, when I try to get the document via _suggest, elasticsearch API somehow breaks the JSON object payload:
curl -XGET "http://localhost:9200/myindex/_suggest" -d '{
"all-suggest": {
"text": "joni",
"completion": {
"field": "suggest"
}
}
}'
results in
{"_shards":{"total":5,"successful":5,"failed":0},"all-suggest":[{"text":"joni","offset":0,"length":5,"options":[{"text":"joni surname - joni#example.org","score":1.0,"payload"::)
�_id`<someid>�typeIapp_user�_reva2-dcd7b9d456e205d3e9d859fdc2c6a688�label�joni surname�textSjoni#example.org�}]}]}
which is definitely no valid JSON.. Any ideas?
This is actually a bug in elasticsearch. It was reported and acknowledged here and should be fixed shortly.