Need to exclude few words from logs using grok - logstash-grok

Consider the below string
date 00:00 1.1.1.1 POST test.com hello-world
How could I print only the date totaltime and URL(test.com) using grok?

Given the sample above
^%{DATA:date} %{DATA:time} %{IP:ip} %{DATA:method} %{DATA:url} %{GREEDYDATA:path}$
would generate:
{
"date": [
[
"date"
]
],
"time": [
[
"00:00"
]
],
"ip": [
[
"1.1.1.1"
]
],
"method": [
[
"POST"
]
],
"url": [
[
"test.com"
]
],
"path": [
[
"hello-world"
]
]
}
Afterwards you can mutate it whichever form you want

Related

GROK pattern to match URIPATH

Here is my sample URL
http://localhost:8080/abc2/query/errorLogs
was trying to extract only query/errorLogs. For this i have tried below GROK patten
(%{URIPROTO}://%{URIHOST}(?<path>/[^/]+/[^/]+/[^/]+))
Below output i am getting
{
"URIPROTO": [
[
"http"
]
],
"URIHOST": [
[
"localhost:8080"
]
],
"IPORHOST": [
[
"localhost"
]
],
"HOSTNAME": [
[
"localhost"
]
],
"IP": [
[
null
]
],
"IPV6": [
[
null
]
],
"IPV4": [
[
null
]
],
"port": [
[
"8080"
]
],
"path": [
[
"/abc2/query/errorLogs"
]
]
}
but i was expecting path should be "/query/errorLogs".
try this :
(%{URIPROTO}://%{URIHOST}(?<first_path>/[^/]+)%{GREEDYDATA:path})
result:
port 8080
first_path /abc2
path /query/errorLogs

Parse result of XML -> XML2JS ->JSON.stringify using JQ

I have a file created by doing an XHR fetch of XML and parsing it through the node module xlm2js and then JSON.stringify. It has about 700 segments of two basic types. This is an edited version of the file with one segment of each type:
{
"NewDataSet": {
"Table": [
{
"SegmentID": [
"2342"
],
"StationID": [
"005es00045:_MN_Stn"
],
"SegmentName": [
"I-5 NB MP0.45 # SR-14"
],
"SegmentType": [
"2"
],
"SegmentLength": [
"1135"
],
"MinimumLanesReporting": [
"0.5"
],
"CalculationThreshold": [
"30"
],
"CalculationPeriod": [
"2"
],
"MinimumSamples": [
"3"
],
"SegmentMaximumFilter": [
"774"
],
"SegmentMinimumFilter": [
"12"
],
"StandardDeviationSamples": [
"15"
],
"StandardDeviationMultiplier": [
"1.96"
],
"UseStandardDeviationFilter": [
"false"
],
"IsActive": [
"true"
]
},
{
"SegmentID": [
"3051"
],
"BeginningDcuID": [
"584"
],
"EndDcuID": [
"589"
],
"SourceSystem": [
"TravelTime"
],
"SegmentName": [
"OR212 at SE 242nd Ave to OR212 at SE Foster Rd"
],
"SegmentType": [
"1"
],
"SegmentLength": [
"100"
],
"CalculationThreshold": [
"60"
],
"CalculationPeriod": [
"10"
],
"MinimumSamples": [
"3"
],
"SegmentMaximumFilter": [
"3600"
],
"SegmentMinimumFilter": [
"50"
],
"StandardDeviationSamples": [
"20"
],
"StandardDeviationMultiplier": [
"1.96"
],
"UseStandardDeviationFilter": [
"true"
],
"IsActive": [
"true"
]
}
]
}
}
I need to ignore the "SegmentType":["2"] segments and extract SegmentID, SegmentName, BeginningDcuID, EndingDcuID, and SegmentLength from the type 1 segments where IsActive is true.
I can list the file with jq "." but any attempt at other operations with jq fail, usually with the message:
'jq: error: syntax error, unexpected '[' (Unix shell quoting issues?) at , line 1:'
Any suggestions for jq syntax changes or xml2js parameter changes to make this work would be outstandingly helpful.
Never use double quotes for quoting an argument if there is nothing in it that you want the shell to expand.
$ jq '.NewDataSet.Table[]
| select(.SegmentType[0] != "2" and .IsActive[0] == "true")
| (.SegmentID, .SegmentName, .BeginningDcuID, .EndingDcuID, .SegmentLength)[0]' file
"3051"
"OR212 at SE 242nd Ave to OR212 at SE Foster Rd"
"584"
null
"100"

Filter logs with grok in logstash

I am trying to filter out logs received with the help of grok. Below is the sample log
INFO | jvm 1 | main | 2013/04/05 01:08:47.048 | [m[32mINFO [TaskExecutor-master-2443-ProcessTask [31111111112]] [b2cConfirmationAction] CRON JOB ID : 101AA1C, ACTION : ConfirmationAction , CUSTOMER ID : 000001111111 , EMAIL ADDRESS : abc#gmail.com , SCHEDULE : Every 1 week , MESSAGE : Execution started for action ConfirmationAction
I am using grok debugger (https://grokdebug.herokuapp.com/) to test before updating logstash conf file.
Below is my filter code :
%{LOGLEVEL:level}%{GREEDYDATA:greedydata}%{SPACE}%{YEAR}[/-]%{MONTHNUM}[/-]%{MONTHDAY}%{SPACE}%{HOUR}:%{MINUTE}:%{SECOND}%{GREEDYDATA:gd} \[(?:%{WORD:action})\]%{GREEDYDATA:cronjobresult}
Here I am getting outpout as
"level": [ [ "INFO" ] ], "greedydata": [ [ " | jvm 1 | main | 20" ] ], "SPACE": [ [ "", " " ] ], "YEAR": [ [ "13" ] ], "MONTHNUM": [ [ "04" ] ], "MONTHDAY": [ [ "05" ] ], "HOUR": [ [ "01" ] ], "MINUTE": [ [ "08" ] ], "SECOND": [ [ "47.048" ] ], "gd": [ [ " | \u001b[m\u001b[32mINFO [TaskExecutor-master-2443-ProcessTask [31111111112]]" ] ], "action": [ [ "b2cConfirmationAction" ] ], "cronjobresult": [ [ " CRON JOB ID : 101AA4A , ACTION : ConfirmationAction , CUSTOMER ID : 000001111111 , EMAIL ADDRESS : abc#gmail.com , SCHEDULE : Every 1 week , MESSAGE : Execution started for action ConfirmationAction" ] ] }
My requirement is to get values under cronjobresult like cron job iD customer id with different and independent field so that I can use these values in kibana. Right now I am not able to get it. Also I have used greedyData twice, better approach for this log would be appreciable.
You can simply extend your filter further and match it explicitly. For instance, to match cron job id, you can write CRON JOB ID : %{BASE16NUM:Cron_job_id} in your filter.
If you do not need any data from log then you can simply write .* instead of GREEDYDATA and it will be skipped.
Here is the complete filter for your log,
%{LOGLEVEL:level}%{GREEDYDATA:greedydata}%{SPACE}%{YEAR}[/-]%{MONTHNUM}[/-]%{MONTHDAY}%{SPACE}%{HOUR}:%{MINUTE}:%{SECOND}%{GREEDYDATA:gd} \[(?:%{WORD:action})\] CRON JOB ID : %{BASE16NUM:Cron_job_id},.*CUSTOMER ID : %{NUMBER:Customer_id}.*EMAIL ADDRESS : %{EMAILADDRESS}.*SCHEDULE : %{GREEDYDATA:schedule}.*, MESSAGE : %{GREEDYDATA:Message}
Output:
{
"level": [
[
"INFO"
]
],
"greedydata": [
[
" | jvm 1 | main | 20"
]
],
"SPACE": [
[
"",
" "
]
],
"YEAR": [
[
"13"
]
],
"MONTHNUM": [
[
"04"
]
],
"MONTHDAY": [
[
"05"
]
],
"HOUR": [
[
"01"
]
],
"MINUTE": [
[
"08"
]
],
"SECOND": [
[
"47.048"
]
],
"gd": [
[
" | [m[32mINFO [TaskExecutor-master-2443-ProcessTask [31111111112]]"
]
],
"action": [
[
"b2cConfirmationAction"
]
],
"Cron_job_id": [
[
"101AA1C"
]
],
"Customer_id": [
[
"000001111111"
]
],
"BASE10NUM": [
[
"000001111111"
]
],
"EMAILADDRESS": [
[
"abc#gmail.com"
]
],
"local": [
[
"abc"
]
],
"remote": [
[
"gmail.com"
]
],
"schedule": [
[
"Every 1 week "
]
],
"Message": [
[
"Execution started for action"
]
]
}
Please note that I have used EMAILADDRESS pattern from, https://github.com/rgevaert/grok-patterns/blob/master/grok.d/postfix_patterns
If you want to test it on https://grokdebug.herokuapp.com, you need to add,
EMAILADDRESSPART [a-zA-Z0-9_.+-=:]+
EMAILADDRESS %{EMAILADDRESSPART:local}#%{EMAILADDRESSPART:remote}
as cusomtom patterns by checking add custom patterns

grok pattern to parse the logs using logstash

I have been trying to parse a sample log file using logstash grok filter but was unable to output the distinguish fields.
my sample logs look like following-
INFO [2016-05-26 11:54:57,741] [main]: org.eclipse.jetty.util.log:?:?- Logging initialized #5776ms`enter code here`
what i want to separate out is INFO, timestamp ,[main] and the message in two parts from from ?:?.
what pattern i have tried in grok filter is ->
match => { "message" => "%{WORD:severity}
%{CISCOTIMESTAMP:timestamp} %{NOTSPACE} %{GREEDYDATA:logmsg}" }
but its not correctly output the pattern.
can please someone provide me the correct grok pattern match!!
Any related help would be useful!!
As it is not clear what exact format do you want to get, I provide you with following filter:
match => { "message" => "%{LOGLEVEL:severity} *\[%{TIMESTAMP_ISO8601:timestamp}\] *\[%{WORD:tread}\]\: *%{NOTSPACE:file} *%{GREEDYDATA:msg}" }
This will effectively split your example to:
{
"severity": [
[
"INFO"
]
],
"timestamp": [
[
"2016-05-26 11:54:57,741"
]
],
"YEAR": [
[
"2016"
]
],
"MONTHNUM": [
[
"05"
]
],
"MONTHDAY": [
[
"26"
]
],
"HOUR": [
[
"11",
null
]
],
"MINUTE": [
[
"54",
null
]
],
"SECOND": [
[
"57,741"
]
],
"ISO8601_TIMEZONE": [
[
null
]
],
"tread": [
[
"main"
]
],
"file": [
[
"org.eclipse.jetty.util.log:?:?-"
]
],
"msg": [
[
"Logging initialized #5776ms`enter code here`"
]
]
}
This doesn't gracefully parse :?:?- part, so adjust it if needed.
Take a look at Grokdebug which is great for on-the-fly filter testing.

grok help for logstash

My logs look as such
00009139 2015-03-03 00:00:20.142 5254 11607 "HTTP First Line: GET /?main&legacy HTTP/1.1"
I tried using grok debugger to get this information formatted with no success. Is there any way to get this format using grok? The quoted string would be the message
So I used the following formatting simply by using the grok patterns page.
%{NUMBER:Sequence} %{YEAR}-%{MONTHNUM}-%{MONTHDAY}[T ]%{HOUR}:?%{MINUTE}(?::?%{SECOND})?%{ISO8601_TIMEZONE}? %{NUMBER:Process}%{NUMBER:Process2}%{WORD:Message}
This is the closest I could get with the current info.
%{INT}%{SPACE}%{TIMESTAMP_ISO8601}%{SPACE}%{INT:pid1}%{SPACE}%{INT:pid2}%{SPACE}%{GREEDYDATA:message}
With the above grok pattern, this is what the grokdebugger "catches":
{
"INT": [
[
"00009139"
]
],
"SPACE": [
[
" ",
" ",
" ",
" "
]
],
"TIMESTAMP_ISO8601": [
[
"2015-03-03 00:00:20.142"
]
],
"YEAR": [
[
"2015"
]
],
"MONTHNUM": [
[
"03"
]
],
"MONTHDAY": [
[
"03"
]
],
"HOUR": [
[
"00",
null
]
],
"MINUTE": [
[
"00",
null
]
],
"SECOND": [
[
"20.142"
]
],
"ISO8601_TIMEZONE": [
[
null
]
],
"pid1": [
[
"5254"
]
],
"pid2": [
[
"11607"
]
],
"message": [
[
""HTTP First Line: GET /?main&legacy HTTP/1.1""
]
]
}
Hope I was of some help.
Try to replace %{WORD:Message} at the end of your grok with %{QS:message}.
hope this helps :)

Resources