Parsing Jenkins build log application log using logstash Grok pattern and load in Elasticsearch - logstash-grok

I am very new to Logstash and ELK in general. I need to write a grok pattern for a Jenkins build console log. My requirement is below-
"Started by user User_SMS" => From this line, I have to extract the username "User_SMS" where the line starts with text "Started by user".
Similarly, from the line "git checkout -f 07999b25163b658686558d9a1d05dd99c30c6059 # timeout=10" I have to extract the hexadecimal checkout id 07999b25163b658686558d9a1d05dd99c30c6059 when line starts with "git checkout -f".
From the line I have to find the build status "Finished: SUCCESS". The line starts with "Finished:" and I have to capture the value "SUCCESS" here, it would be "FAILURE" in some other build as well.
Please help in parsing the log using Grok.
The index in Elasticsearch will have the above fields user_name, checkout_id, build_status etc.
I am unable to create the Grok pattern to parse this Jenkins log. Please guide me with this.
Jenkins Log image

Related

How to get immediate output from a job run within gitlab-runner?

The command gitlab-runner lets you "test" a gitlab job locally. However, the local run of a job seems to have the same problem as a gitlab job run in gitlab CI: The output is not immediate!
What I mean: Even if your code/test/whatever produces printed output, it is not shown immediately in your log or console.
Here is how you can reproduce this behavior (on Linux):
Create a new git repository
mkdir testrepo
cd testrepo
git init
Create file .gitlab-ci.yml with the following content
job_test:
image: python:3.8-buster
script:
- python tester.py
Create a file tester.py with the following content:
import time
for index in range(10):
print(f"{time.time()} test output")
time.sleep(1)
Run this code locally
python tester.py
which produces the output
1648130393.143866 test output
1648130394.1441162 test output
1648130395.14529 test output
1648130396.1466148 test output
1648130397.147796 test output
1648130398.148115 test output
1648130399.148294 test output
1648130400.1494567 test output
1648130401.1506176 test output
1648130402.1508648 test output
with each line appearing on the console every second.
You commit the changes
git add tester.py
git add .gitlab-ci.yml
git commit -m "just a test"
You start the job within a gitlab runner
gitlab-runner exec docker job_test
....
1648130501.9057398 test output
1648130502.9068272 test output
1648130503.9079702 test output
1648130504.9090931 test output
1648130505.910158 test output
1648130506.9112566 test output
1648130507.9120533 test output
1648130508.9131665 test output
1648130509.9142723 test output
1648130510.9154003 test output
Job succeeded
Here you get essentially the same output, but you have to wait for 10 seconds and then you get the complete output at once!
What I want is to see the output as it happens. So like one line every second.
How can I do that for both, the local gitlab-runner and the gitlab CI?
In the source code, this is controlled mostly by the clientJobTrace's updateInterval and forceSendInterval properties.
These properties are not user-configurable. In order to change this functionality, you would have to patch the source code for the GitLab Runner and compile it yourself.
The parameters for the job trace are passed from the newJobTrace function and their defaults (where you would need to alter the source) are defined here.
Also note that the UI for GitLab may not necessarily get the trace in realtime, either. So, even if the runner has sent the trace to GitLab, the javascript responsible for updating the UI only polls for trace data every ~4 or 5 seconds.
You can poll gitlab web for new log lines as fast as you can:
For running job, use url like: https://gitlab.example.sk/grpup/project/-/jobs/42006/trace It will send you a json structure with lines of log file, offset, size and so on. You can have a look at documentation here: https://docs.gitlab.com/ee/api/jobs.html#get-a-log-file
Sidenote: you can use undocumented “state” parameter from response in subsequent request to get only new lines (if any). This is handy.
Through, this does not affect latency of arrival newlines from actual job from runner to gitlab web/backend. See sytech answer for this question.
This answer should help, when there is configured redis cache, incremental logging architecture, and someone wants to get logs from currently running job in "realtime". Polling is still needed through.
Some notes can be found also on forum: https://forum.gitlab.com/t/is-there-an-api-for-getting-live-log-from-running-job/73072

Perforce trigger won't run rubyscript

jenkins change-submit //... "ruby %quote%//HVS/Main/BuildScripts/notify_jenkins.rb%quote%"
So I have made the above p4 trigger in my triggers file, and I'm trying to run a build script file that I wrote in ruby but when I try to submit a file, I'm getting this error:
'jenkins' validation failed: ruby: No such file or directory -- //HVS/Main/BuildScripts/notify_jenkins.rb (LoadError)
Is there no way to make a p4 trigger run a file that's inside of a stream? The documentation says you can do this, but when I try to run it, it's saying it can't find the file.
Per the doc:
https://www.perforce.com/perforce/r14.2/manuals/p4sag/chapter.scripting.html#basics.scripts.depot
the format you want is:
jenkins change-submit //... "ruby %//HVS/Main/BuildScripts/notify_jenkins.rb%"
Surrounding it in %quote% characters means you're expecting the OS to be able to interpret that path as a local filesystem path.

Log4j - specify a script to be executed post Log File Rollover

Can log4j be configured to run a script after RollingFile has finished to enable me to send out email that roll over has occurred or grep through log to see if a text pattern occurred, etc?
thank you

Issue while sending file content from filebeat to logstash

I am new to ELK and i am trying to do some handson using the ELK stack. I am performing the following on WINDOWS,
1. Installed Elastic search,confirmed with http://localhost:9200/
2. Installed logstash,confirmed using http://localhost:9600/
logstash -f logstash.config
logstash.config file looks like this,
input {
beats {
port => "5043"
}
}
# The filter part of this file is commented out to indicate that it is
# optional.
# filter {
#
# }
output {
elasticsearch { hosts => ["localhost:9200"] }
}
3. Installed Kibana, confirmed using http://localhost:5601
Now, i want to use filebeat to pass a log file to logstash which parses and forwards it to Elastic search for indexing. and finally kibana displays it.
In order to do that,
"
i did the following changes in filebeat.yml.
change 1 :
In Filebeat prospectors, i added
paths:
# - /var/log/*.log
- D:\KibanaInput\vinod.log
Contents of vinod.log: Hello World from FileBeat.
Change 2:
In Outputs,
#output.logstash:
# The Logstash hosts
hosts: ["localhost:9600"]
when i run the below command,
filebeat -c filebeat.yml -e
i get the below error,
ERR Connecting error publishing events (retrying): Failed to parse JSON response: json: cannot unmarshal string into Go value of type struct { Number string }
Please let me know what mistake i am doing.
You are in a good path.
Please confirm the following:
in your filebeat.yml make sure that you don't have comment in the output.logstash: line, that correspond to your change number 2.
Make sure your messages are been grok correctly. Add the following output in your logstash pipeline config file.
output {
stdout { codec => json }
}
3.Start your logstash in debug mode.
4.If you are reading the same file with the same content make sure you remove the registry file in filebeat. ($filebeatHome/data/registry)
5.Read the log files.

how can i find my generated files logstash?

I'm beginner with ELK stack , so I configured logstash , and when I want to search with ElasticSearch I have no results , so I'm supposed to get a result , because I do my parse on grokdebug and it works very well .
I do my research as follows:
"http://localhost:9200/logstash-2016.03.14/_search?q=*"
I wanted to know if I can see my logstash files generated and if it generated the results or not?
knowing that I tried searching elastic search on a JSON file and it works.
The problem is at logstash .
thanks
Logstash does not generate any file (except for its configuration).
To debug your logstash instance, you can :
Use the --verbose flag and/or --debug
Use the -l "file.log" to output logs in file.log (default to stdout)
Use the stdout output plugin et see the results
Also, did you use the elasticsearch output plugin?

Resources