# Logstash initialize variables before input

**URL:** <https://discuss.elastic.co/t/logstash-initialize-variables-before-input/227684>\
**Category:** Logstash\
**Created:** [April 12, 2020, 3:37pm UTC](https://discuss.elastic.co/t/logstash-initialize-variables-before-input/227684 "2020-04-12T15:37:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_Morelli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mario_morelli/32/45072_2.png) [@Mario\_Morelli](https://discuss.elastic.co/u/Mario_Morelli)\
**Post date:** [April 12, 2020, 3:37pm UTC](https://discuss.elastic.co/t/logstash-initialize-variables-before-input/227684/1 "2020-04-12T15:37:40Z")

</div>

Hi Guys  
I use Logstash for the random projects at work and I just cant seem to figure out the following, seems like it should be easy enough

My input uses the http poller plugin, that posts to a REST API  
There are 2 variables that this URL requires

1. Start time
2. End Time

I would just like to understand how I can do the following to input into the http poller  
End Time = Need a method to get todays short date 2019-04-12  
StartTime = Need a method to get yesterdays short date 2019-04-11

What would be the easiest way to get these variables done, to be used as input variables for the http poller?

Thanks in advance:)

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [April 12, 2020, 3:41pm UTC](https://discuss.elastic.co/t/logstash-initialize-variables-before-input/227684/2 "2020-04-12T15:41:51Z")

</div>

Generate them externally to logstash and use environment variables, or alternatively, see [this](https://discuss.elastic.co/t/rest-api-http-poller-and-ruby-code/176447/2) post.

---

<div class="post-metadata">

**Author:** ![alxsss](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alxsss/32/69929_2.png) [@alxsss](https://discuss.elastic.co/u/alxsss)\
**Post date:** [May 1, 2020, 11:53pm UTC](https://discuss.elastic.co/t/logstash-initialize-variables-before-input/227684/3 "2020-05-01T23:53:15Z")

</div>

How to generate date type variable externally? I see environment variables plugin, but it is a filter plugin not an input plugin.

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [May 2, 2020, 12:19am UTC](https://discuss.elastic.co/t/logstash-initialize-variables-before-input/227684/4 "2020-05-02T00:19:48Z")

</div>

Hello @Mario_Morelli

Unfortunately `http_poller` input doesn't support dynamic variables.

I think you can use one of the following approaches.

# Use the exec input with an external script

You can use the [`exec`](https://www.elastic.co/guide/en/logstash/current/plugins-inputs-exec.html) input (which can be scheduled periodically) and use an external script which runs a `curl` request and prints the output of the call to `stdout`.

You might need to adjust the `codec`

```auto
input {
  exec {
    # this can be an external script if necessary
    command => 'curl -s -X GET ...'
    schedule => "* * * * *"
  }
}

```

# User the exec input and http filter

You can use the `exec` input just to trigger the execution periodically.  
The command can be a dummy command or a command which returns the date parameters.

Afterwards, use the [`http`](https://www.elastic.co/guide/en/logstash/current/plugins-filters-http.html) filter to perform the call.

```auto
input {
  exec {
    command => 'echo `date --date="1 day ago" +"%Y-%m-%d"`,`date +"%Y-%m-%d"`'
    schedule => "* * * * *"
  }
}
filter {
  dissect {
    mapping => {
      "message" => "%{StartTime},%{EndTime}"
    }
    remove_field => "message"
  }
  http {
    ... # use %{StartTime} and %{EndTime}
  }
}

```

# Use environment variables and restart Logstash

If you have to change this daily, you could restart Logstash periodically, changing the value of [2 environment variables](https://www.elastic.co/guide/en/logstash/current/environment-variables.html) prior starting Logstash again.  
This is applicable only if you have to change it daily...

---

<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:** [June 3, 2020, 6:08pm UTC](https://discuss.elastic.co/t/logstash-initialize-variables-before-input/227684/6 "2020-06-03T18:08:40Z")

</div>

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