Heatmaps Part1

Problem this snippet solves:

I’ve been tinkering with a way to allow iRules to make that easier, and to allow those interested parties to see some usage statistics in a visually interesting, real-time manner, without adding much heavy lifting for you or your application. The idea is simple, create a heat map view of your HTTP requests, mapped to the locations around the United States (to start with). This will give you an idea which areas are most highly utilizing your application in a very easy on the eyes fashion. Best of all, of course, is that we’re going to generate this 100% with iRules. For the full write-up check out the article. (Note: There is an update to this iRule using the newer GeoCharts.)

Code: (as gist)

 

 

 

when HTTP_REQUEST {
  if {[HTTP::uri] starts_with "/heatmap"} {
    set chld "" 
    set chd ""
    foreach state [table keys -subtable states] {
      append chld $state
      append chd "[table lookup -subtable states $state],"
    }
    set chd [string trimright $chd ","]
    HTTP::respond 200 content "<HTML><center><font size=5>Here is your site's usage by state:</font><img src='http://chart.apis.google.com/chart?cht=t&chd=&chs=440x220&chtm=usa&chd=t:$chd&chld=$chld&chco=f5f5f5,edf0d4,6c9642,365e24,13390a' border='0'><a href='/resetmap'>Reset Map</a></center></HTML>"
  } elseif {[HTTP::uri] starts_with "/resetmap"} {
    foreach state [table keys -subtable states] {
      table delete -subtable states $state
    }
    HTTP::respond 200 Content "<HTML><center>Table Cleared. <a href='/heatmap'>Return to Map</a></HTML>"
  } else {
    set loc [whereis [IP::client_addr] abbrev]
    if {$loc eq ""} {
      set ip [expr { int(rand()*255) }].[expr { int(rand()*255) }].[expr { int(rand()*255) }].[expr { int(rand()*255) }]
      set loc [whereis $ip abbrev]
    }
    if {[table incr -subtable states -mustexist $loc] eq ""} {
      table set -subtable states $loc 1 indefinite indefinite
    } 
  } 
}

 

 

 

Updated Nov 18, 2022
Version 4.0

Was this article helpful?

No CommentsBe the first to comment