LinkTracking2

Problem this snippet solves:

This iRule application builds on the LinkTracking iRule that allows you to track URI's that come in through your virtual server and store them in a table with the number of times they have been referenced. The data stored can then be viewed by passing in the "/linkadmin" url which will generate a table of the URI's and their view counts. In this iRule, I've also added the ability to filter the results by a wildcard match on the URI or by the reference count.

Code :

when HTTP_REQUEST {
  
  set TABLE_LINK    "LINK_TRACKING_[virtual name]";
  
  switch -glob [string tolower [HTTP::uri]] {
    "/linkadmin*" {
      set count_filter "";
      set data_filter "";
      set msg [URI::decode [getfield [HTTP::uri] "/" 3]]
      if { ($msg starts_with "f(") && ($msg ends_with ")") } {
        set count_filter "";
        set data_filter [string range $msg 2 end-1];
        set msg "";
      } elseif { ($msg starts_with "c(") && ($msg ends_with ")") } {
        set count_filter [string range $msg 2 end-1];
        set data_filter "";
        set msg "";
      }
      set count [table keys -subtable $TABLE_LINK -count]
      set content {Link Tracking
        
        
        
}; append content "" append content ""; append content ""; append content {} append content "}; foreach key [table keys -subtable $TABLE_LINK] { if { "" != $data_filter } { if { [string match $data_filter $key] } { set v [table lookup -subtable $TABLE_LINK $key]; append content ""; } } elseif { "" != $count_filter } { set v [table lookup -subtable $TABLE_LINK $key]; if { $v >= $count_filter } { append content ""; } } else { set v [table lookup -subtable $TABLE_LINK $key]; append content ""; } } append content "
Link MetricsClear Data
URICount
Filter Results } append content " [x] [x]
\[x\] $key$v
\[x\] $key$v
\[x\] $key$v
"; HTTP::respond 200 Content $content; } "/linkcleardata" { table delete -subtable $TABLE_LINK -all; HTTP::redirect "http://[HTTP::host]/linkadmin/Link+Tracking+Cleared" } "/linkremovedata/*" { set val [string range [HTTP::uri] [string length "/linkremovedata/"] end] if { "" != $val } { table delete -subtable $TABLE_LINK $val; } HTTP::redirect "http://[HTTP::host]/linkadmin/Link+Deleted"; } default { set match 1; set c [table keys -subtable $TABLE_FILTERS -count] if { $c != 0 } { set match 0; foreach key [table keys -subtable $TABLE_FILTERS] { set m [string match $key [HTTP::uri]]; if { 1 == $m } { set match 1; break; } } } if { $match == 1} { if { [table incr -subtable $TABLE_LINK -mustexist [HTTP::uri]] eq ""} { table set -subtable $TABLE_LINK [HTTP::uri] 1 indefinite indefinite; } } } } }
Published Mar 18, 2015
Version 1.0

Was this article helpful?

1 Comment

  • B_Earp's avatar
    B_Earp
    Icon for Nimbostratus rankNimbostratus

    TCL ERROR: can't read "TABLE_FILTERS": no such variable while executing "table keys -subtable $TABLE_FILTERS -count" ("default" arm line 3) invoked from within "switch -glob [string tolower [HTTP::uri]] { "/linkadmin*" { set count_filter ""; set data_filter ""; set msg [URI::decode [getfi..."