0

I have this to kind of logs for dhcpack:

Jun 30 06:34:18 HOSTNAME dhcpd: DHCPACK to IP (MAC) via eth2

Jun 30 06:34:28 HOSTNAME dhcpd: DHCPACK on IP to MAC via eth2

How can I use grok, to use two different matches? I have these two matches for dhcpack, but just use the first:

((%{SYSLOGTIMESTAMP:timestamp})\s*(%{HOSTNAME:hostname})\sdhcpd\S+\s(%{WORD:dhcp_action})?.[for|on] (%{IPV4:dhcp_client_ip})?.[from|to] (%{COMMONMAC:dhcp_client_mac})?.*via (%{USERNAME:interface}))

((%{SYSLOGTIMESTAMP:timestamp})\s*(%{HOSTNAME:hostname})\sdhcpd\S+\s(%{WORD:dhcp_action})?.*[to] (%{IPV4:dhcp_client_ip})?.*via (%{USERNAME:interface}))

Someone can help?

Community
  • 1
  • 1
Miguel Bessa
  • 325
  • 2
  • 5
  • 21

3 Answers3

0

I would suggest pulling the common stuff (up to the colon) off first and then processing the more specific stuff with more specific patterns. Some details here.

As shown in the doc, grok{} can take multiple patterns:

filter {
  grok { match => { "message" => [
     "Duration: %{NUMBER:duration}",
     "Speed: %{NUMBER:speed}"
  ] } }
}

By default, it will stop processing after the first match, but that's configurable.

EDIT:

Based on your other comments, you can also branch based on conditionals:

if [myField] == "someValue" {
    grok {
        ...
    }
}
else {
    grok {
        ...
    }
}

In this case, you're running a comparison ("==") or regexp ("=~") to see if you should run a regexp (grok{}). Depending on the full business logic, this seems like a waste.

Alain Collins
  • 16,268
  • 2
  • 32
  • 55
  • I want do something like: if (dhcp_action == "DHCPINFORM") { ((%{SYSLOGTIMESTAMP:timestamp})\s*(%{HOSTNAME:hostname})\sdhcpd\S+\s(%{WORD:dhcp_action})?.[for|on] (%{IPV4:dhcp_client_ip})?.[from|to] (%{COMMONMAC:dhcp_client_mac})?.*via (%{USERNAME:interface})) } else { ((%{SYSLOGTIMESTAMP:timestamp})\s*(%{HOSTNAME:hostname})\sdhcpd\S+\s(%{WORD:dhcp_action})?.*[to] (%{IPV4:dhcp_client_ip})?.*via (%{USERNAME:interface})) } It's possible? – Miguel Bessa Jul 08 '15 at 00:38
0

I want do something like:

In ((%{SYSLOGTIMESTAMP:timestamp})\s*(%{HOSTNAME:hostname})\sdhcpd\S+\s(%{WORD:dhcp_action})?.[for|on] (%{IPV4:dhcp_client_ip})?.[from|to] (%{COMMONMAC:dhcp_client_mac})?.*via (%{USERNAME:interface})) 

get just dhcp_action and use if statement, like:

 if (mCursor != null && mCursor.moveToFirst()) {
         ......
 } else {
         ......
 }

It's possible?

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
Miguel Bessa
  • 325
  • 2
  • 5
  • 21
0

I solve the problem with this:

filter { grok { match => ["message", "(dhcpd\S+\s*(%{WORD:dhcp_action_test}))"] } if "DHCPINFORM" in [message] { grok { match => ["message","((%{SYSLOGTIMESTAMP:timestamp})\s* (%{HOSTNAME:hostname})\sdhcpd\S+\s(%{WORD:dhcp_action})?.[from] (%{IPV4:dhcp_client_ip}))"] } } else if "DHCPDISCOVER" in [message] { grok { match => ["message","((%{SYSLOGTIMESTAMP:timestamp})\s(%{HOSTNAME:hostname})\sdhcpd\S+\s(%{WORD:dhcp_action})?.*[from] (%{COMMONMAC:dhcp_client_mac})"] } } else { drop {} }

}

Miguel Bessa
  • 325
  • 2
  • 5
  • 21