# Want to check connection duration

**URL:** <https://discuss.elastic.co/t/want-to-check-connection-duration/293235>\
**Category:** Logstash\
**Created:** [December 31, 2021, 1:27am UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235 "2021-12-31T01:27:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![root\_linux](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@root\_linux](https://discuss.elastic.co/u/root_linux)\
**Post date:** [December 31, 2021, 1:27am UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235/1 "2021-12-31T01:27:57Z")

</div>

hi all,  
I have configured Elasticsearch, kibana and logstash on a server. Sending logs from different filebeat clients to logstash. I have logs in which connection esatablish and connection reset time present. My requirement is get to know connection duration. sample log is given below:-

```auto
Tue Oct 5 14:05:53 2021 TCP connection established with [AF_INET6]::ffff:192.168.10.1:55630
Tue Oct 5 23:40:51 2021 192.168.10.1:55630 Connection reset, restarting [0]

```

I have written grok filter like that,

```auto
%{DAY:day} %{SYSLOGTIMESTAMP:conn_est_time} %{YEAR:year} TCP connection established with \[AF_INET6\]::ffff:%{IP:server_IP}:%{NUMBER:port}
%{DAY:day} %{SYSLOGTIMESTAMP:conn_reset_time} %{YEAR:year} %{IP:server_IP}:%{NUMBER:port} Connection reset, restarting \[0\]

```

Now i want to get connection duration and visualize it on kibana. How to do that? Please guide me.

---

<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:** [December 31, 2021, 2:05am UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235/2 "2021-12-31T02:05:28Z")

</div>

Use an aggregate filter. Your use case is similar to [Example 1](https://www.elastic.co/guide/en/logstash/current/plugins-filters-aggregate.html#plugins-filters-aggregate-example1). You could try

```
    if [conn_est_time] {
        mutate { add_field => { "[@metadata][ts]" => "%{year} %{conn_est_time}" } }
        date { match => ["[@metadata][ts]", "YYYY MMM d HH:mm:ss", "YYYY MMM dd HH:mm:ss" ] }
        aggregate {
            task_id => "%{server_IP}:%{port}"
            code => 'map["start"] = event.get("@timestamp")'
            map_action => "create"
        }
    }
    if [conn_reset_time] {
        mutate { add_field => { "[@metadata][ts]" => "%{year} %{conn_reset_time}" } }
        date { match => ["[@metadata][ts]", "YYYY MMM d HH:mm:ss", "YYYY MMM dd HH:mm:ss" ] }
        aggregate {
            task_id => "%{server_IP}:%{port}"
            code => 'event.set("duration", event.get("@timestamp") - map["start"])'
            map_action => "update"
            end_of_task => true
        }
    }

```

which gets me

```
       "duration" => 34498.0,
```

---

<div class="post-metadata">

**Author:** ![root\_linux](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@root\_linux](https://discuss.elastic.co/u/root_linux)\
**Post date:** [January 1, 2022, 1:52pm UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235/3 "2022-01-01T13:52:42Z")

</div>

hi @Badger , thanks for your reply.

Will it create "duration" index pattern?

---

<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:** [January 1, 2022, 4:53pm UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235/4 "2022-01-01T16:53:45Z")

</div>

An index pattern? No. When an event that contains a [duration] field is indexed an index mapping will automatically be created.

---

<div class="post-metadata">

**Author:** ![root\_linux](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@root\_linux](https://discuss.elastic.co/u/root_linux)\
**Post date:** [January 2, 2022, 4:44am UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235/5 "2022-01-02T04:44:40Z")

</div>

Yes, sorry. Its my fault. I have written index pattern in place of field.

---

<div class="post-metadata">

**Author:** ![root\_linux](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@root\_linux](https://discuss.elastic.co/u/root_linux)\
**Post date:** [January 2, 2022, 10:13am UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235/6 "2022-01-02T10:13:08Z")

</div>

hi @Badger  
I have another query. Suppose after reset same port is assigned to another connection etablishement, then will I get separately duration for second connection on same port?

---

<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:** [January 2, 2022, 5:17pm UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235/7 "2022-01-02T17:17:11Z")

</div>

> [@root\_linux](#):
>
> will I get separately duration for second connection on same port?

Yes, you will.

---

<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:** [January 30, 2022, 5:17pm UTC](https://discuss.elastic.co/t/want-to-check-connection-duration/293235/8 "2022-01-30T17:17:26Z")

</div>

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