content tracking

we found an irule here by Kirk (thanks Kirk) that breaks content down by count. any genius out there that can take this and add total bytes for each category? our ecomm group wants to see the breakdown in the classification of traffic.

thanks!

Content_ Type_ Tracking

Enter a topic name to show or a new topic name to create; then press Enter

.

Contributed by: kirkbauer - kirk@f5.com

Description

Have you ever wondered what types of content are served by your web servers? Have you wondered how many requests could be offloaded to the BIG-IP if you added the RAM Cache module? Or maybe how much acceleration you may get from WebAccelerator ? Try out this iRule. Most of the time everything other than the HTML pages should be cacheable.

iRule Source

First, create statistics profile named “ContentType” with following entries:

HTML

Images

Scripts

Documents

Stylesheets

Other

Now associate this Statistics Profile to the virtual server. Then apply the following iRule.

To view the results, go to Statistics → Profiles - Statistics

when HTTP_RESPONSE {

switch -glob [HTTP::header “Content-type”] {

“image/*” { STATS::incr “ContentType” “Images” }

“text/html” { STATS::incr “ContentType” “HTML” }

“text/css” { STATS::incr “ContentType” “Stylesheets” }

“*javascript” { STATS::incr “ContentType” “Scripts” }

“text/vbscript” { STATS::incr “ContentType” “Scripts” }

“application/pdf” { STATS::incr “ContentType” “Documents” }

“application/msword” { STATS::incr “ContentType” “Documents” }

“application/*powerpoint” { STATS::incr “ContentType” “Documents” }

“application/*excel” { STATS::incr “ContentType” “Documents” }

“default” { STATS::incr “ContentType” “Other” }

}

}

Ah, been a while for stats profile and I don’t have the means to test right now, but how about under each switch match:

STATS::incr “ContentType” “Images_bytes” [HTTP::payload length]

or another profile:

STATS::incr “ContentLength” “Images” [HTTP::payload length]

So the rule would expand to:

when HTTP_RESPONSE { 
   switch -glob [HTTP::header "Content-type"] { 
     "image/*" { 
                      STATS::incr "ContentType" "Images" 
                      STATS::incr "ContentLength" "Images" [HTTP::payload length] 
                    }

…and so on. I assumed another profile for the content length, but you could keep in the same profile with different fields as long as you don’t exceed the max. Untested, but it just might work! HTH

thanks. i loaded that but not knowing what it is exactly doing i cannot seem to correlate the values. guess i better read up on that.

have you ever seen this break a website? i have loaded it on two vips and both give broken images or no images and sometimes resets the browser connection. any ideas? did i mess something up here?

when HTTP_RESPONSE {

switch -glob [HTTP::header “Content-type”] {

“image/*” { STATS::incr “ContentType” “Images”

STATS::incr “ContentLength” “Images” [HTTP::payload length]

}

“text/html” {

STATS::incr “ContentType” “HTML”

STATS::incr “ContentLength” “HTML” [HTTP::payload length]

}

“text/css” { STATS::incr “ContentType” “Stylesheets” }

“*javascript” { STATS::incr “ContentType” “Scripts” }

“text/vbscript” { STATS::incr “ContentType” “Scripts” }

“application/x-shockwave-flash” { STATS::incr “ContentType” “Flash” }

“video/x-flv” { STATS::incr “ContentType” “FlashVideo” }

“default” { STATS::incr “ContentType” “Other” }

}

}

Does the contentLength profile exist? Check /var/log/ltm to see if you are seeing any errors

thanks, i created that profile and just added images and html. in my log file i get this

Feb 24 13:13:13 tmm tmm[955]: 01220001:3: TCL error: Rule http_stat_tracker - Error: ContentLength/HTML: bad field name or profile not attached (line 1) invoked from within "STATS::incr “ContentLength” “HTML” [HTTP::payload length] " (“text/html” arm line 2) invoked from within “switch -glob [HTTP::header “Content-type”] { “image/*” { STATS::incr “ContentType” “Images” STATS::incr “ContentLength” “Images” [HTTP::paylo…”

not sure which profile i associate to vip. i used the origional, created the second stat profile. it works some but other times breaks my website.

Hmm, maybe it is necessary to only have a single stats profile per virtual, my bad. So in that case, I believe you will just need additional fields in the original profile, maybe call them HTML_len and Images_len:

when HTTP_RESPONSE { 
     switch -glob [HTTP::header "Content-type"] { 
       "image/*" { 
                        STATS::incr "ContentType" "Images"  
                        STATS::incr "ContentType" "Images_len" [HTTP::payload length] 
                      } 
   } 
 }

32 fields should be enough to meet your needs.

yes that worked. i just did the image as a test and it worked. i need to figure out the length calculation. i have a page that had a 64k image as a background. i register a hit and it shows 1.4k in length.

thanks!

so i got back to this again and decided to sniff my http connection and realized my mtu on this stream is set to 1460 bytes - 1.4k. it appears that this counts as it reads each frame. so no matter what i will always increment by 1.4k? i think i am closer but it is erratic on what it is reporting and it is far less than i would expect.

Hmm, that doesn’t sound right, most likely becuase you’re not doing an HTTP::collect prior (not sure you really want to collect all the HTTP payload just to count it, though). You could try using the content length header instead:

STATS::incr “ContentType” “Images_len” [HTTP::header value “Content-Length”]

Forgot to mention that you’ll want to make sure that header exists before calling it

if { [HTTP::header exists "Content-Length"] } { 
   STATS::incr "ContentType" "Images_len" [HTTP::header value "Content-Length"]  
 }

Of course, this will lead to potentially incomplete byte counts.

If the Content-Length header doesn’t exist, [HTTP::header value “Content-Length”] will return a null string and STATS::incr will probably throw a TCL error. You might either wrap it in a catch, or check that the HTTP::header result is an integer.

Out of curiosity, scottsams, do you see the content length of 1.4k on every response? Can you post the response headers from a response that shows 1.4k for the HTTP::payload length value?

Aaron

thanks i will try that. as for the 1.4k when i sniffed the transaction my MTU was set to 1460. it has since changed and went to 1.2k. i did a tcpdump and saw the frame sizes negotiated to 1260 also. it seemed to correlate. oddly, when i hit one page with a 116k jpg, the stat incremented to 1 with a size of 1.2k.

i am attaching a jpg screen shot with various info. irule, log, website i hit.

here is the log

Feb 26 10:19:30 tmm tmm[955]: 01220002:6: Rule http_stat_tracker : images_len is 1212

Feb 26 10:20:52 tmm tmm[955]: 01220002:6: Rule http_stat_tracker : Header content is image/JPEG; Charset=utf-8 payload length is 1212

Feb 26 10:20:52 tmm tmm[955]: 01220002:6: Rule http_stat_tracker : images_len is 1212

Feb 26 10:21:06 tmm tmm[955]: 01220002:6: Rule http_stat_tracker : Header content is image/JPEG; Charset=utf-8 payload length is 1212

attached is irule, jpg page size

Looking at this post I guess I can do the below (simple basic example)

Question 1) but what would I have to (or should I) modify it work with a STREAM profile ?

Question 2) I assume that this does NOT impact the basic built in statistics ?

Question 3) When I make a new stats profile does it basically just build a “rrd” infrastructure ?

Question 4) there was alot of discussion in this thread BUT no single best practice was identified “hoolio” could you comment on this ?

As Scott said (above) I assume I have to first define a profile (Profile->Other-Statistics) and create a new named profile “content_and_len”, then check "custum) with following entries:

Html

Html_len

Images

Images_len

Css

Css_len

Other

Other_len

Then associate this Statistics Profile to the virtual server.

Next I would have to the following iRule and then also associate that with the same virtual server, this iRULE will also tests for unset or missing length.

when HTTP_RESPONSE { 
  switch -glob [HTTP::header "Content-type"] { 
    "text/html" { 
      STATS::incr "ContentType" "Html"  
      if { [HTTP::header exists "Content-Length"] } { 
        STATS::incr "ContentType" "Html_len" [HTTP::payload length] 
      }
    } 
    "image/*" { 
      STATS::incr "ContentType" "Images"  
      if { [HTTP::header exists "Content-Length"] } { 
        STATS::incr "ContentType" "Images_len" [HTTP::payload length] 
      }
    }

    "text/css" { 
      STATS::incr "ContentType" "Css"  
      if { [HTTP::header exists "Content-Length"] } { 
        STATS::incr "ContentType" "Css_len" [HTTP::payload length] 
      }
    } 
    "default" { 
      STATS::incr "ContentType" "Other" 
      if { [HTTP::header exists "Content-Length"] } { 
        STATS::incr "ContentType" "Other_len" [HTTP::payload length] 
      }
    } 
  }
}

To view the results, I would then go to Statistics → Local Traffic → Profiles/Statistics

I have not yet pumped any traffic yet but I do see the zero counts.

What if I don’t care to have the above show up in the GUI F5 interface but want to merely keep a count for every five minutes and write it out to a file and zero the stats

(before anyone goes crazy on me - In my use case I will actually track the content_and_len by some key like USER-AGENT)

I imagine I use could use the version 10.1.* “TABLE” syntax for my hashes and counters along with “AFTER” and dump my results to file (preferably named ua_use_yyyymmdd.out). If I can’t use iRULEs to write to a file I guess I could use a rolling log.

rolling log: http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1084377/Writing-to-and-rotating-custom-log-files.aspx

Here is some simple code using AFTER (I did not use the “table” syntax) to show what I am having an issue getting my head around

when RULE_INIT {
      set reqs_last_600_sec 0
      set new_req_count 0
      after 600000 –periodic 

         set reqs_last_600_sec $new_user_count
         set new_req_count 0

          HOW DO I SAVE the reqs_last_600_sec value to a file ?
          Is it better to send it to a rollign log file or is it
          better to make an OOB request to some other server like
          a memcached server.
}
   }
   when HTTP_REQUEST { incr new_req_count }

Any help or directions would be appreciated.

when HTTP_RESPONSE {

if { [HTTP::header exists “Content-Length”] } {

if { [catch {STATS::incr “ContentLength” [HTTP::header “Content-Type”] [HTTP::header “Content-Length”]} } {

log local0.info “Counting content-type \” [HTTP::header “Content-Type”]\" failed with an error"

}

}

}

Seems simpler.

Question 1) but what would I have to (or should I) modify it work with a STREAM profile ?

What do you want to use the stream profile for with this iRule? Do you mean that you’re rechunking the response content? If so, I don’t think that would affect the Content-Length header. I think HTTP_RESPONSE would be triggered before the HTTP payload is modified anyhow.

Question 2) I assume that this does NOT impact the basic built in statistics ?

Correct.

Question 3) When I make a new stats profile does it basically just build a “rrd” infrastructure ?

No, I think the stats data is stored in an internal memory structure. I don’t think it’s ever written to disk.

Question 4) there was alot of discussion in this thread BUT no single best practice was identified “hoolio” could you comment on this ?

Best practice for what part? As far as avoiding a runtime error if the Content-Length header isn’t there? I’d just add a check to see if [HTTP::header Content-Length] ne “” rather than using catch.

Aaron