• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Home
  • Contact Us

iHash

News and How to's

  • JBL Flip 6 Portable Bluetooth Speaker (Open Box) for $74

    JBL Flip 6 Portable Bluetooth Speaker (Open Box) for $74
  • Navee V25 300W Foldable e-Scooter for $299

    Navee V25 300W Foldable e-Scooter for $299
  • Smart Tracker Includes Key Ring – Works with Apple Find My App (2-Pack) for $34

    Smart Tracker Includes Key Ring – Works with Apple Find My App (2-Pack) for $34
  • Harmony Premium Plan Lifetime Subscription for $99

    Harmony Premium Plan Lifetime Subscription for $99
  • Lenovo 11.6" 100e Chromebook 2nd Gen (2019) MediaTek MT8173C 4GB RAM 16GB eMMC (Refurbished) for $54

    Lenovo 11.6" 100e Chromebook 2nd Gen (2019) MediaTek MT8173C 4GB RAM 16GB eMMC (Refurbished) for $54
  • News
    • Rumor
    • Design
    • Concept
    • WWDC
    • Security
    • BigData
  • Apps
    • Free Apps
    • OS X
    • iOS
    • iTunes
      • Music
      • Movie
      • Books
  • How to
    • OS X
      • OS X Mavericks
      • OS X Yosemite
      • Where Download OS X 10.9 Mavericks
    • iOS
      • iOS 7
      • iOS 8
      • iPhone Firmware
      • iPad Firmware
      • iPod touch
      • AppleTV Firmware
      • Where Download iOS 7 Beta
      • Jailbreak News
      • iOS 8 Beta/GM Download Links (mega links) and How to Upgrade
      • iPhone Recovery Mode
      • iPhone DFU Mode
      • How to Upgrade iOS 6 to iOS 7
      • How To Downgrade From iOS 7 Beta to iOS 6
    • Other
      • Disable Apple Remote Control
      • Pair Apple Remote Control
      • Unpair Apple Remote Control
  • Special Offers
  • Contact us

Grok Pattern Examples for Log Parsing

Mar 24, 2022 by iHash Leave a Comment


Searching and visualizing logs is next to impossible without log parsing, an underappreciated skill loggers need to read their data. Parsing structures your incoming (unstructured) logs so that there are clear fields and values that the user can search against during investigations, or when setting up dashboards. 

The most popular log parsing language is Grok. You can use Grok plugins to parse log data in all kinds of log management and analysis tools, including the ELK Stack and Logz.io. Check out our Grok tutorial here.

But parsing logs with Grok can be tricky. This blog will examine some Grok pattern examples, which can help you learn how to parse your log data.

Table of Contents

  • Let’s start Grok
  • 1. Extracting an IP
  • 2. Timestamps and Arrays
  • 3. Extracting Verbs
  • 4. Extracting Request 
  • 5. Extracting the Status 
  • 6. Extracting the Bytes 
  • 7. Extracting the referrer
  • 8. Ignoring data and extracting OS
    • How do we use the pattern we just created?

Let’s start Grok

Let’s start with an example unstructured log message, which we will then structure with a Grok pattern:

128.39.24.23 - - [25/Dec/2021:12:16:50 +0000] "GET /category/electronics HTTP/1.1" 200 61 "/category/finance" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"

Imagine searching through millions of log lines that look like that! It seems terrible. And that’s why we have parsing languages like Grok – to make the data easier to read, and easier to search. 

A much easier way to view that data – and to search through it with analysis tools like Kibana – is to break it down into fields with values, like the list below:

  1. ip:128.39.24.23
  2. timestamp:25/Dec/2021:12:16:50 +0000
  3. verb:GET
  4. request:/category/electronics HTTP/1.1
  5. status: 200
  6. bytes: 61
  7. referrer:/category/finance
  8. os: Windows

Let’s use an example Grok pattern to generate these fields. The following sections will show the Grok pattern syntax to generate each one of the fields above. 

With each section, our Grok pattern will expand as it includes more fields to parse. The patterns are regexes that GROK can read – we can use them to phrase our message. 

Below are some helpful links to get started with some grok patterns. But we’ll provide more examples throughout the rest of the blog. 

GROK pattern
GROK Debugger

Let’s get started building a Grok pattern to structure the data.

We describe the beginning of the pattern by using: ^
The syntax goes like this: %{pattern:Name_of_the_field}

PLEASE NOTE: It’s not recommended to use space to describe the field’s name.

1. Extracting an IP

Let’s say we want to extract the IP, we can use the IP method:

^%{IP:ip}

Then we have these:  – – 

To tell Grok to ignore them, we just add them to our pattern.

^%{IP:ip} – –

This pattern gives us this field:

Ip:128.39.24.23 

One field down. Seven to go!

2. Timestamps and Arrays

In the next part of our unstructured log message, we have the timestamp “trapped” inside an array:

[25/Dec/2021:12:16:50 +0000] 

To extract it, we need to use regex and the HTTPDATE method, while adding the brackets on the outside so Grok knows to ignore them:

\[%{HTTPDATE:timestamp}\]

Building on our previous pattern, we now have:

^%{IP:ip} – – \[%{HTTPDATE:timestamp}\] 

That gives us:

ip: 128.39.24.23
timestamp: 25/Dec/2021:12:16:50 +0000

Going back to our original unstructured message, it looks like we have a space where the timestamp ends, and when the “GET starts. We need to tell Grok to ignore spaces as well. To do that, we just hit the spacebar on our keyboard or we can use %{SPACE} -> that catches until 4 spaces.

3. Extracting Verbs

Time to extract the GET field. First, we need to tell Grok to ignore the quotation marks – we will do that by writing“ ->. Then use the WORD method: “%{WORD:verb} ->

So, now our pattern reads:

^%{IP:ip} – – \[%{HTTPDATE:timestamp}\] “%{WORD:verb}

That gives us these fields. 

ip:128.39.24.23
timestamp:25/Dec/2021:12:16:50 +0000
verb: GET

4. Extracting Request 

In order to extract the request -> /category/electronics HTTP/1.1″, we need to use the DATA method, which is essentially the wild card in regex. 

This means we need to add a stopping point to extract this information to tell the DATA method where to stop – otherwise, it won’t capture any of the data. We can use the quotation marks as a stop mark: 

%{DATA:request}”

Now, we have the following grok pattern:

^%{IP:ip} – – \[%{HTTPDATE:timestamp}\] “%{WORD:verb} %{DATA:request}”

That gives us these fields:

ip:128.39.24.23
timestamp:25/Dec/2021:12:16:50 +0000
verb: GET
request:/category/electronics HTTP/1.1

5. Extracting the Status 

Next up is the status, but again we have a space between the end of the request and the status, we can add a space or %{SPACE}. To extract numbers, we use the NUMBER method. 

%{NUMBER:status} -> 

Now our pattern extends to:
^%{IP:ip} – – \[%{HTTPDATE:timestamp}\] “%{WORD:verb} %{DATA:request}” %{NUMBER:status}

That gives us:

ip:128.39.24.23
timestamp:25/Dec/2021:12:16:50 +0000
verb: GET
request:/category/electronics HTTP/1.1
status: 200

6. Extracting the Bytes 

In order to extract bytes -> space or %{SPACE}  -> %{NUMBER:bytes}

^%{IP:ip} – – \[%{HTTPDATE:timestamp}\] “%{WORD:verb} %{DATA:request}” %{NUMBER:status} %{NUMBER:bytes}

That gives us:

ip:128.39.24.23
timestamp:25/Dec/2021:12:16:50 +0000
verb: GET
request:/category/electronics HTTP/1.1
status: 200
bytes: 6

The end is in sight.

7. Extracting the referrer

To extract “referrer” we need to use space or %{SPACE} -> “ so GROK will  ignore it:

 -> %{DATA:referrer}” 

As you can see, we added DATA stops when it sees “

^%{IP:ip} – – \[%{HTTPDATE:timestamp}\] “%{WORD:verb} %{DATA:request}” %{NUMBER:status} %{NUMBER:bytes} “%{DATA:referrer}”

That gives us:

ip:128.39.24.23
timestamp:25/Dec/2021:12:16:50 +0000
verb: GET
request:/category/electronics HTTP/1.1
status: 200
referrer:/category/finance

8. Ignoring data and extracting OS

Now we’d like to IGNORE this data ->” “Mozilla/5.0 (compatible; MSIE 9.0;

To do this, we will use the DATA method for “Mozilla/5.0 but without writing the field it will ignore it. 

Then we will use the WORD method without the colon or field name to ignore (compatible;

Then finally, we will use the DATA to ignore MSIE 9.0; 

This leaves us with the following pattern to ignore that data

%{DATA}\(%{WORD};%{DATA};

To explain this pattern a bit more…

\( -> stops until it reaches (
; ->  stops until it reaches ;

And now we can use the WORD method to extract the os -> %{WORD:os}

That’s it! Now we’re left with the following grok pattern to structure our data.

^%{IP:ip} – – \[%{HTTPDATE:timestamp}\] “%{WORD:verb} %{DATA:request}” %{NUMBER:status} %{NUMBER:bytes}
“%{DATA:referrer}”%{DATA}\(%{WORD};%{DATA}; %{WORD:os}

And that gives us these tidy fields we can use to more easily search and visualize our log data:

ip:128.39.24.23
timestamp:25/Dec/2021:12:16:50 +0000
verb: GET
request:/category/electronics HTTP/1.1
status: 200
referrer:/category/finance

os:Windows

This is what our pattern looks like in Grok.

How do we use the pattern we just created?

Now that we have a Grok pattern, we can implement it in a variety of grok processors. These can be found in plugins with log shippers like Fluentd, or with services like Logz.io’s Sawmill. 

Logz.io is a centralized logging and observability platform that uses a service called Sawmill to parse the incoming data. We can use Logz.io’s self-service parser to implement this pattern in Sawmill, which will parse the log data coming into your Logz.io account.

Find the self-service parser here. And learn more about the service here.
Here is our final grok pattern example, which we can implement in any tool that parses logs with Grok.

{
  "steps": [
    {
      "grok": {
        "config": {
          "field": "message",
          "patterns": [
            "^%{IP:ip} - - \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request}\" %{NUMBER:status} %{NUMBER:bytes} \"%{DATA:referrer}\"%{DATA}\\(%{WORD};%{DATA}; %{WORD:os}"
          ]
        }
      }
    }
  ]}

And here is what it looks like to implement our grok pattern into Logz.io’s self-service parser.

Now, all of our incoming logs will be structured into fields, making them easy to search and visualize. For example, if we are using Logz.io as a centralized logging tool, we could pull up our unsuccessful request logs by simply searching “status:400 OR status:500” in the search bar. 

Logz.io also offers parsing-as-a-service. Within your Logz.io account, simply reach out to a Customer Support Engineer through the chat in the bottom right corner, and ask them to parse your logs. They’d be happy to help.



Source link

Share this:

  • Facebook
  • Twitter
  • Pinterest
  • LinkedIn

Filed Under: News Tagged With: Examples, Grok, Log, Parsing, Pattern

Special Offers

  • JBL Flip 6 Portable Bluetooth Speaker (Open Box) for $74

    JBL Flip 6 Portable Bluetooth Speaker (Open Box) for $74
  • Navee V25 300W Foldable e-Scooter for $299

    Navee V25 300W Foldable e-Scooter for $299
  • Smart Tracker Includes Key Ring – Works with Apple Find My App (2-Pack) for $34

    Smart Tracker Includes Key Ring – Works with Apple Find My App (2-Pack) for $34
  • Harmony Premium Plan Lifetime Subscription for $99

    Harmony Premium Plan Lifetime Subscription for $99
  • Lenovo 11.6" 100e Chromebook 2nd Gen (2019) MediaTek MT8173C 4GB RAM 16GB eMMC (Refurbished) for $54

    Lenovo 11.6" 100e Chromebook 2nd Gen (2019) MediaTek MT8173C 4GB RAM 16GB eMMC (Refurbished) for $54

Reader Interactions

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

  • Facebook
  • GitHub
  • Instagram
  • Pinterest
  • Twitter
  • YouTube

More to See

Microsoft Sues Hacking Group Exploiting Azure AI for Harmful Content Creation

Jan 11, 2025 By iHash

Apple Arcade launches into 2025 with 10 new games, including PGA TOUR Pro Golf

Jan 11, 2025 By iHash

Tags

* Apple attack attacks Cisco cloud computer security Critical cyber attacks cyber news cybersecurity Cyber Security cyber security news cyber security news today cyber security updates cyber updates data data breach Elastic google hacker hacker news Hackers hacking hacking news how to hack incident response information security iOS 7 iOS 8 iPhone Malware microsoft network security ransomware ransomware malware Secure security security vulnerabilities software vulnerability the hacker news Threat update video vulnerability

Latest

Protecting California's workforce: EDD’s cybersecurity evolution after COVID-19

Protecting California’s workforce: EDD’s cybersecurity evolution after COVID-19

Enhancing cybersecurity resilience and operational efficiency with Elastic The Employment Development Department (EDD) of California plays a vital role in administering essential services, including unemployment insurance, disability insurance, paid family leave, tax collection, and job matching. The onset of COVID-19 and subsequent surge in cyber threats prompted a significant evolution in EDD’s cybersecurity measures, leading […]

Google Project Zero Researcher Uncovers Zero-Click Exploit Targeting Samsung Devices

Jan 10, 2025Ravie LakshmananCybersecurity / Android Cybersecurity researchers have detailed a now-patched security flaw impacting Monkey’s Audio (APE) decoder on Samsung smartphones that could lead to code execution. The high-severity vulnerability, tracked as CVE-2024-49415 (CVSS score: 8.1), affects Samsung devices running Android versions 12, 13, and 14. “Out-of-bounds write in libsaped.so prior to SMR Dec-2024 […]

Latest Product Updates

Latest Product Updates and Features in Logz.io

Introducing Our New Support Help Center We’re thrilled to launch our brand-new and improved Support Help Center, designed to streamline how you interact with our support team and access the resources you need. This enhanced platform empowers users to: Submit and track support tickets, ensuring full visibility into your requests and their progress. Find answers […]

Our longstanding privacy commitment with Siri

At Apple, we are committed to protecting user data, and our products and features are built from the ground up with innovative privacy technologies and techniques. Privacy is a foundational part of the design process, driven by principles that include data minimization, on-device intelligence, transparency and control, and strong security protections that work together to […]

Neglected Domains Used in Malspam to Evade SPF and DMARC Security Protections

Cybersecurity researchers have found that bad actors are continuing to have success by spoofing sender email addresses as part of various malspam campaigns. Faking the sender address of an email is widely seen as an attempt to make the digital missive more legitimate and get past security mechanisms that could otherwise flag it as malicious. […]

5 insights from public sector leaders: Solving organizational challenges with data and AI

5 insights from public sector leaders: Solving organizational challenges with data and AI

Despite the best intentions of many public sector leaders to build data-driven organizations, the reality is that 65% of public sector leaders still struggle to use data continuously in real time and at scale. The upside? Many leaders are taking advantage of AI and generative AI to tackle this critical need. But to reach that […]

Jailbreak

Pangu Releases Updated Jailbreak of iOS 9 Pangu9 v1.2.0

Pangu has updated its jailbreak utility for iOS 9.0 to 9.0.2 with a fix for the manage storage bug and the latest version of Cydia. Change log V1.2.0 (2015-10-27) 1. Bundle latest Cydia with new Patcyh which fixed failure to open url scheme in MobileSafari 2. Fixed the bug that “preferences -> Storage&iCloud Usage -> […]

Apple Blocks Pangu Jailbreak Exploits With Release of iOS 9.1

Apple has blocked exploits used by the Pangu Jailbreak with the release of iOS 9.1. Pangu was able to jailbreak iOS 9.0 to 9.0.2; however, in Apple’s document on the security content of iOS 9.1, PanguTeam is credited with discovering two vulnerabilities that have been patched.

Pangu Releases Updated Jailbreak of iOS 9 Pangu9 v1.1.0

  Pangu has released an update to its jailbreak utility for iOS 9 that improves its reliability and success rate.   Change log V1.1.0 (2015-10-21) 1. Improve the success rate and reliability of jailbreak program for 64bit devices 2. Optimize backup process and improve jailbreak speed, and fix an issue that leads to fail to […]

Activator 1.9.6 Released With Support for iOS 9, 3D Touch

  Ryan Petrich has released Activator 1.9.6, an update to the centralized gesture, button, and shortcut manager, that brings support for iOS 9 and 3D Touch.

Copyright iHash.eu © 2025
We use cookies on this website. By using this site, you agree that we may store and access cookies on your device. Accept Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT