# Can I reference a variable set in my GROK statement in an if statement later on in the filter?

**URL:** <https://discuss.elastic.co/t/can-i-reference-a-variable-set-in-my-grok-statement-in-an-if-statement-later-on-in-the-filter/167786>\
**Category:** Logstash\
**Created:** [February 11, 2019, 3:23am UTC](https://discuss.elastic.co/t/can-i-reference-a-variable-set-in-my-grok-statement-in-an-if-statement-later-on-in-the-filter/167786 "2019-02-11T03:23:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdvincent](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@sdvincent](https://discuss.elastic.co/u/sdvincent)\
**Post date:** [February 11, 2019, 3:23am UTC](https://discuss.elastic.co/t/can-i-reference-a-variable-set-in-my-grok-statement-in-an-if-statement-later-on-in-the-filter/167786/1 "2019-02-11T03:23:12Z")

</div>

The goal is to parse a line from syslog and if the program is sshd, parse the extracted message to grab the source IP. My grok line looks like this:

```auto
filter {
  if [fields][type] == "syslog" {
    grok {
      match => {
        "message" => ["%{SYSLOGTIMESTAMP:[system][syslog][timestamp]} %{SYSLOGHOST:system.syslog.hostname} %{DATA:[system][syslog][program]}(?:\[%{POSINT:[system][syslog][pid]}\])?: %{GREEDYMULTILINE:[system][syslog][message]}"]
      }
      pattern_definitions => {
        "GREEDYMULTILINE"=> "(.|\n)*"
      }
      remove_field => ["message"]
    }
...
}

```

What I would like to have is something like:

```auto
filter {
  if [fields][type] == "syslog" {
    grok {
      match => {
        "message" => ["%{SYSLOGTIMESTAMP:[system][syslog][timestamp]} %{SYSLOGHOST:system.syslog.hostname} %{DATA:[system][syslog][program]}(?:\[%{POSINT:[system][syslog][pid]}\])?: %{GREEDYMULTILINE:[system][syslog][message]}"]
      }
      pattern_definitions => {
        "GREEDYMULTILINE"=> "(.|\n)*"
      }
      remove_field => ["message"]
    }
    if "system.syslog.program" == "sshd" {
      grok {
        match => { "[system][syslog][message]" => ["Disconnected from user root %{IP:[system][auth][ssh][ip]} ", "Accepted keyboard-interactive/pam for root from %{IP:[system][auth][ssh][ip]} " ] }
      }
      geoip {
        source => "system.auth.ssh.ip"
        target => "system.auth.ssh.geoip"
      }
      mutate {
        add_tag => ["auth"]
      }
    }
 }
}

```

Is this possible? I've tried it the way described above as well as trying:

```auto
if "[system][syslog][program]" == "sshd" {

```

But I've had no luck with either. Is this even possible to do?

---

<div class="post-metadata">

**Author:** ![sdvincent](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@sdvincent](https://discuss.elastic.co/u/sdvincent)\
**Post date:** [February 11, 2019, 12:54pm UTC](https://discuss.elastic.co/t/can-i-reference-a-variable-set-in-my-grok-statement-in-an-if-statement-later-on-in-the-filter/167786/2 "2019-02-11T12:54:56Z")

</div>

Turns out I was pretty close. I just had to drop the quotes (ugh!). Now my filter looks like this:

```auto
filter {
  if [fields][type] == "syslog" {
    grok {
      match => {
        "message" => ["%{SYSLOGTIMESTAMP:[system][syslog][timestamp]} %{SYSLOGHOST:system.syslog.hostname} %{DATA:[system][syslog][program]}(?:\[%{POSINT:[system][syslog][pid]}\])?: %{GREEDYMULTILINE:[system][syslog][message]}"]
      }
      pattern_definitions => {
        "GREEDYMULTILINE"=> "(.|\n)*"
      }
      remove_field => ["message"]
    }
    if [system][syslog][program] == "sshd" {
      grok {
        match => { "[system][syslog][message]" => ["Disconnected from user root %{IP:[system][auth][ssh][ip]} ", "Accepted keyboard-interactive/pam for root from %{IP:[system][auth][ssh][ip]} " ] }
      }
      geoip {
        source => "[system][auth][ssh][ip]"
        target => "[system][auth][ssh][geoip]"
      }
      mutate {
        add_tag => ["auth"]
      }
    }
    mutate {
      add_tag => ["system", "syslog"]
    }
    date {
      match => ["[system][syslog][timestamp]", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
    }
  }
}

```

I guess I was confused because under a match like in date or add\_field, you wrap it in quotes but in if statements you don't.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 11, 2019, 12:54pm UTC](https://discuss.elastic.co/t/can-i-reference-a-variable-set-in-my-grok-statement-in-an-if-statement-later-on-in-the-filter/167786/3 "2019-03-11T12:54:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
