IP to DNS reverse lookup + Geo location? Delimitted format?

LTM 1500 9.4.8

Hi - we’d like to see where our website traffic is coming from, and by which companies. I’d prefer to log that into a format that i can read into a database table, so that i can create some friendly reports for management, if doable.

I’m sure there are irules like this already setup in the forum, and many different ways to go about this, but i’m not having any luck finding it.

Can someone point me in the right direction to an irule or forum topic? My irule skills are still beginner → medium, so please be gentle…

I have a winscp connection to the bigip, so i’m able to pull logs to my sql server.

Hi Matt,

I think it would be more efficient (and cheaper) to do this on an application server.

In v10.1, LTM adds an inbuilt Quova GeoIP database. See the whereis wiki page for details (http://devcentral.f5.com/wiki/iRules.whereis.ashx). But with the default license you get with LTM, you can only use the database to make load balancing decisions. You can’t pass the GeoIP data onto any other non-LTM host. You could try getting a custom license for more than this from Quova, but I don’t think it’s cheap. Even if you could use the default Quova license for this, I don’t think it makes sense to try to log every HTTP request or TCP connection via LTM and do IP lookups just for reporting. That would eat into the resources LTM should be using for app delivery.

On the other hand, you could pull your www server logs and perform GeoIP and reverse DNS lookups on them for reporting. MaxMind offers a free GeoIP database with a lot of options for querying.

Aaron

Actually, I need to correct myself. The whereis wiki page shows that this usage is allowed:

Logging data can contain location info and collected into a central logging solution for analysis of F5 logs.

So theoretically, you could upgrade to 10.1+ and then use High Speed Logging and send a message to a pool of syslog servers for each HTTP request with the GeoIP info. This should have relatively low overhead in terms of CPU/memory usage. I’d still avoid doing reverse DNS lookups on every client IP if you can do that easily off LTM.

http://devcentral.f5.com/wiki/iRules.whereis.ashx

http://devcentral.f5.com/wiki/iRules.hsl.ashx

Aaron

Arggg, 10.x. We’ve tried to find a memory module to upgrade our LTM 1500, as I really want to upgrade to 10.x, but simply cannot find one anywhere! Currently, we don’t have the amount of recommended RAM to upgrade to 10.x, hence we are stuck at 9.4.8 :frowning:

I guess I’m stuck with the MaxMind method and parsing through the logs? Thanks for the idea.

You won’t be able to add RAM to a 1500 unless you buy it from F5 and F5 stopped stocking that a bit ago :frowning:

Aaron

If i want to simply log traffic during it’s initial connection, and not log the same IP over and over again while it’s visting the site, what is the optimal way to capture that via an irule?

You’d need to track which IP’s you’ve already logged. This could eat up considerable memory on LTM just for logging.

If you’re on 9.4.8 you can use the remote logging option on the log command to log all connections to the VS. This should have a much lower impact than trying to write to LTM disk.

http://devcentral.f5.com/wiki/iRules.log.ashx

log [-noname] < remote_ip >[:< remote_port>] < facility>.[< level>] < message>

* (LTM only) Logs the specified message directly to the specified IP address (and optional alternate port when specified) via UDP. Facility and/or level are required. The iRule name prefixing the message text may optionally suppressed by including the -noname option. < remote_ip> must be a TMM-routed address. If you must route specific messages to a remote address via the management interface, you must log locally. syslog-ng is able to route messages via both TMM and management interfaces using the standard syntax. You can define an appropriate filter and remote log destination in LTM’s syslog-ng service.

Aaron

Ok, thanks Aaron. I also just found a post you wrote back in 08, i might try that first as i’m not familiar with logging to a syslog server

to jondyke in nov of 08

Thanks for your help!

when CLIENT_ACCEPTED {

Add some logic for determining which clients to log for

if {[matchclass [IP::client_addr] equals $::filteredAddresses}{

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 “New TCP connection from [IP::client_addr]:[TCP::client_port] to [IP::local_addr]:[TCP::local_port]”

} else {

Disable all events for this rule and any other rule for this connection

event disable all

}

}

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 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 "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)"

}

oh, and PS, that would be the only irule i have against the virtual server, per your comment about disabling events for any rule for that connection

that was a very creative idea by the way :slight_smile:

Hi everyone, i’ve strayed away from this post for a while, but am trying to resurrect the above irule. However, i get a “no data received” error when i apply the irule to my existing site, for which i have a default pool selected. Any ideas on what i’d need to modify to make this work, or what i’m doing wrong? I just want to log one ip per user, per session, and then am planning on trying Hoolio’s idea for Maxmind reverse dns and geo.

There’s probably a simple answer here, i’m just not sure what that is…

Hi Matt,

Can you post the exact iRule you’re testing in [ code ] [/ code ] blocks? Are you logging locally to /var/log/ltm? Or remotely to a syslog server? Where are you seeing the “no data received” error?

Aaron

Hi Aaron,

Here is the exact rule, copied from my LTM. I haven’t even checked logging yet - i’m just testing to see if my webpage comes up after i apply the irule, and it does not. I get the “no data received” message in my chrome browser - error 324 ERR_EMPTY_RESPONSE. As i said, i have this irule, and a default pool - i wasn’t sure if i maybe need to add the pool into the irule itself somewhere for this to all process correctly? I’m content with logging this locally, since traffic load will be really light, and this will just process one log entry per session, so there shouldn’t be a whole lot of data being logged.

when CLIENT_ACCEPTED {  
  Add some logic for determining which clients to log for  
 if {[matchclass [IP::client_addr] equals $::filteredAddresses]}{  
  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 "New TCP connection from [IP::client_addr]:[TCP::client_port] to [IP::local_addr]:[TCP::local_port]"  
 } else {  
  Disable all events for this rule and any other rule for this connection  
 event disable all  
 }  
 }  
 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 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 "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)"  
 }

That iRule shouldn’t affect requests or response handling as it’s just logging. Do you see an error in /var/log/ltm when you see the 324 error? If you take the iRule off the virtual server do you still see that error?

Aaron

Aaron - if i enable the rule, the site does not come up, when i remove the irule, the site comes back up.

I’ll check the log

I’m finally getting back around to this - here is the error in the logs when i try and apply this irule:

Nov 22 16:56:37 tmm tmm[1727]: 01220001:3: TCL error: MyIrule - can’t read “::filteredAddresses”: no such variable while executing “matchclass [IP::client_addr] equals $::filteredAddresses”

Nov 22 16:56:37 tmm tmm[1727]: 01220001:3: TCL error: MyIrule - can’t read “tcp_start_time”: no such variable while executing “expr [clock clicks -milliseconds] - $tcp_start_time”

this is on 9.4.6.

Do you have a datagroup named exactly filteredAddresses? If not, the runtime error is expected as the datagroup referenced from the iRule doesn’t exist.

It won’t effect the error, but you should remove the $:: prefix from the iRule reference to the datagroup name if you’re on 9.4.4 or higher:

http://devcentral.f5.com/wiki/iRules.cmpcompatibility.ashx

Aaron

Hi Aaron - no, i don’t have a datagroup named filteredAddresses - do i need to make one, or modify the irule? Sorry, i’m a little clueless.

You can create an address type data group in the GUI under Local Traffic >> iRules >> Data Group List tab >> Create. Select a type of ‘Address’ and then add the hosts and/or subnets you want to log for.

Or if you want to log for all clients, you can skip using a datagroup and use an iRule like this:

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 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 "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)"
}

Aaron

Aaron - that works, however i get an entry in the log for each page for the connection, and even just images on the page - so i get way too much log writing. I was trying to go with the filteredAddress irule because it seemed to figure out a way to only log one entry per client connection.

Hi Aaron - i know you’re just helping people out of the goodness of your heart, and you have a ton of posts that you reply to - but do you have any other comments per the first irule i was trying to get working? Is there anyway to setup the filteredAddresses to be equal to the dynamic IP during the connection, so i can simply log one IP per “session”, or is there another better way to do this?

I’m really just trying to get one IP per day, or the closest i can come to that, so that i don’t have to do unique lookups in a database table after importing this data on a daily basis (as well as keeping log writing on the LTM to a minimum), and setting up some reports for upper management to look at.

Thanks for getting back to me on the previous posts.