Chef ERB template with Logstash Grok pattern issues

185 Views Asked by At

I have a logstash config file that I need to convert to a chef erb template (mainly the filters section). However, I keep running into issues due to the format of the grok patterns. Below is an example of the grok pattern.

  grok {
    match => ["message", "<%{POSINT:seqnum1}>%{POSINT:seqnum2}: (\d*: |\.|\*)*%{SYSLOGTIMESTAMP:timestamp} %{WORD:tz}: \%%{WORD:facility_label}-(%{WORD:switch_id}-)*%{INT:severity}-%{WORD:program}: %{GREEDYDATA:message}"]

Here is the issue. Shortly after this I need to have some interpolation fill in an IP address etc. But it wont because the <% starts an interpolation of its own.

mutate {
  add_field => {"[@metadata][sentry][msg]" => "%{host}"
    "[@metadata][sentry][severity]" => "%{severity}"
    "[@metadata][sentry][host]" => "<%= @sentry_host[:ipaddress] %>"
    "[@metadata][sentry][pid]" => "<%= @sentry_pid %>"
    "[@metadata][sentry][key]" => "<%= @sentry_key %>"
    "[@metadata][sentry][secret]" => "<%= @sentry_secret %>"
  }
}

So all of the values above are processed as the string of <%= @sentry_... %>.

Is there a way to get around this? I have tried the escape method of <%%{POSINT:seqnum1}>%{POSINT:seqnum2}:%> seen here. But it still puts the closing %> in. Any other ways to escape characters/strings in ERB?

Thanks! Josh

1

There are 1 best solutions below

2
On

You don't want to put the %> in your text. <%% becomes a literal <% and it doesn't need a paired ending tag.

EDIT example of erb -x:

$ echo '<%% foo <%= asdf %> bar <%= baz %>' | erb -x
#coding:ASCII-8BIT
_erbout = ''; _erbout.concat "<% foo "; _erbout.concat(( asdf ).to_s); _erbout.concat " bar "; _erbout.concat(( baz ).to_s); _erbout.concat "\n"
; _erbout.force_encoding(__ENCODING__)