# Log Tcp And Http Request Response Info

**URL:** <https://community.f5.com/t/log-tcp-and-http-request-response-info/67056>\
**Category:** Community CodeShare\
**Tags:** irules, application-delivery, reporting\
**Created:** [March 18, 2015, 7:29pm UTC](https://community.f5.com/t/log-tcp-and-http-request-response-info/67056 "2015-03-18T19:29:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [March 18, 2015, 7:29pm UTC](https://community.f5.com/t/log-tcp-and-http-request-response-info/67056/1 "2015-03-18T19:29:18Z")

</div>

**Problem this snippet solves:**

This iRule logs a line for the following events:

- when a new TCP connection is established with a client
- when the HTTP headers of an HTTP request are received from the client
- when the HTTP headers of an HTTP response are received from the pool member
- when the TCP connection with a client is closed

**Code :**

```auto
# Here is a sample of the log output for a single TCP connection with three HTTP requests:

<client_accepted>: New TCP connection from 192.168.99.210:2675 to 192.168.101.41:80

<http_request>: Client 192.168.99.210:2675 -> test_http_vip/test0.html?parameter=val (request)
<http_response>: Client 192.168.99.210:2675 -> test_http_vip/test0.html?parameter=val (response) - pool info http_pool 192.168.101.45 80 - status: 200 (request/response delta: 0ms)

<http_request>: Client 192.168.99.210:2675 -> test_http_vip/test1.html?parameter=val (request)
<http_response>: Client 192.168.99.210:2675 -> test_http_vip/test1.html?parameter=val (response) - pool info http_pool 192.168.101.45 80 - status: 200 (request/response delta: 0ms)

<http_request>: Client 192.168.99.210:2675 -> test_http_vip/test2.html?parameter=val (request)
<http_response>: Client 192.168.99.210:2675 -> test_http_vip/test2.html?parameter=val (response) - pool info http_pool 192.168.101.45 80 - status: 200 (request/response delta: 1ms)

<client_closed>: Closed TCP connection from 192.168.99.210:2675 to 192.168.101.41:80 (open for: 1078ms)

when CLIENT_ACCEPTED {
# Get time for start of TCP connection in milleseconds
set tcp_start_time [clock clicks -milliseconds]

# Log the start of a new TCP connection
log local0. "New TCP connection from [IP::client_addr]:[TCP::client_port] to [IP::local_addr]:[TCP::local_port]"
}
when HTTP_REQUEST {
# Get time for start of HTTP request
set http_request_time [clock clicks -milliseconds]

# Log the start of a new HTTP request
set LogString "Client [IP::client_addr]:[TCP::client_port] -> [HTTP::host][HTTP::uri]"
log local0. "$LogString (request)"
}

when LB_SELECTED {
log local0. "Client [IP::client_addr]:[TCP::client_port]: Selected [LB::server]"
}
when LB_FAILED {
log local0. "Client [IP::client_addr]:[TCP::client_port]: Failed to [LB::server]"
}
when SERVER_CONNECTED {
log local0. "Client [IP::client_addr]:[TCP::client_port]: Connected to [IP::server_addr]:[TCP::server_port]"
}
when HTTP_RESPONSE {
# Received the response headers from the server. Log the pool name, IP and port, status and time delta
log local0. "$LogString (response) - pool info: [LB::server] - status: [HTTP::status] (request/response delta: [expr {[clock clicks -milliseconds] - $http_request_time}] ms)"
}
when CLIENT_CLOSED {
# Log the end time of the TCP connection
log local0. "Closed TCP connection from [IP::client_addr]:[TCP::client_port] to [IP::local_addr]:[TCP::local_port] (open for: [expr {[clock clicks -milliseconds] - $tcp_start_time}] ms)"
}</client_closed></http_response></http_request></http_response></http_request></http_response></http_request></client_accepted>

```

---

<div class="post-metadata">

**Author:** ![Karthik\_Krishn1](https://avatars.discourse-cdn.com/v4/letter/k/0ea827/32.png) [@Karthik\_Krishn1](https://community.f5.com/u/Karthik_Krishn1)\
**Post date:** [September 12, 2017, 7:04pm UTC](https://community.f5.com/t/log-tcp-and-http-request-response-info/67056/2 "2017-09-12T19:04:30Z")

</div>

> **[Log Tcp And Http Request Response Info](https://community.f5.com/t/log-tcp-and-http-request-response-info)**
>
> Problem this snippet solves: This iRule logs a line for the following events: when a new TCP connection is established with a client when the HTTP headers of an HTTP request are received from the client when the HTTP headers of an HTTP...

¬†

---

<div class="post-metadata">

**Author:** ![JotaCePena\_1783](https://avatars.discourse-cdn.com/v4/letter/j/85e7bf/32.png) [@JotaCePena\_1783](https://community.f5.com/u/JotaCePena_1783)\
**Post date:** [January 4, 2019, 4:05pm UTC](https://community.f5.com/t/log-tcp-and-http-request-response-info/67056/3 "2019-01-04T16:05:21Z")

</div>

Very usefull iRules. Thanks a lot.

---

<div class="post-metadata">

**Author:** ![afotedar](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@afotedar](https://community.f5.com/u/afotedar)\
**Post date:** [October 3, 2023, 9:57pm UTC](https://community.f5.com/t/log-tcp-and-http-request-response-info/67056/4 "2023-10-03T21:57:55Z")

</div>

We have a similar iRule with CLIENT\_ACCEPTED, LB\_SELECTED, SERVER\_CONNECTED, and SERVER\_CLOSED events. We perform SNAT in the LB\_SELECTED event and logging in the SERVER\_\* events. When I try to apply this rule to a UDP VS, I get the following error:

```bash
01070394:3: TCP::client_port in rule (/Common/my_rule) requires an associated BIGPROTO or TCP or FASTHTTP profile on the virtual server (/Common/udp_vs).

```

I understand the error and am wondering if the rule can be applied to a UDP VS. If not, how can I change the logging in SERVER\_\* events so as to resolve this error?

```tcl
when SERVER_CONNECTED {
    log local0. "inner_ip=[IP::client_addr] inner_port=[TCP::client_port] outer_ip=[IP::local_addr] outer_port=[TCP::local_port] dest_ip=[IP::remote_addr] dest_port=[TCP::remote_port]"
}

```

Additionally, would&nbsp;CLIENT\_ACCEPTED and LB\_SELECTED events work for UDP? I would really appreciate an pointers. Thank you.
