Getting count of sessions created by minute

In the wake of a flood of 24,000 application server sessions created per minute at the start of Black Friday week, we wanted to share how we calculated how many sessions were created per minute. This can be obtained from a jmx-console jboss.web call to “host=localhost,path=/,type=Manager” and the sessionCounter attribute, but it is a live call, so we didn’t have the information historically.

Thankfully, in the access log we log the JSESSIONID created by JBOSS to uniquely identify a session. The python script below simply counts the number of unique JSESSIONID’s by minute. In other words, if JSESSIONID 123ABC first occurs at 08:44, it will be added to the counter for sessions created at 08:44. No future occurrences in the log will be considered for JSESSIONID 123ABC.

The weakness to this approach is that it does not account for sessions created the previous day, such as 11:57PM, that then show up at 12:00AM in the newly rolled over log. As such, it isn’t 100% accurate, but it is very close.

Depending on your access log format, you may have to change some of the index numbers below. For example, we log the JSESSIONID in the tenth column of the access log.

Hopefully its useful.

"""
-----------------------------------------------------------------------------------
Author:         Steve Howard
Date:           November 14, 2013
Purpose:        Provide count of sessions created in the access log.  It only
                  logs the first occurrence for a given JSESSIONID.
-----------------------------------------------------------------------------------
"""

import sys
if len(sys.argv) == 2:
  f = open(sys.argv[1])
else:
  f = sys.stdin
d = dict() #fast to search by key, or JSESSIONID in our case
tim = dict()

#read each line in the provided file...
for l in f:
  #...then split it into an array...
  l2 = l.split()
  #...and if it isn't an akamai performance request or a load balancer request, and it is
  #   in the new log format with JSESSIONID...
  if l.find("sure_route") == -1 and l2[0] != "-" and len(l2) == 13:
    #...if we don't already have this JSESSIONID in our dict, i.e., the first time we have seen it...
    if l2[9] not in d:
      #...then add JSESSIONID without any real value for the value we only need the key
      d[l2[9]] = 0
      t=l2[2].split(":")
      #...and finally print the time of the first request for this JSESSIONID, as that is when the session
      #...was created.
      if t[1] + ":" + t[2] in tim:
        tim[t[1] + ":" + t[2]] = tim[t[1] + ":" + t[2]] + 1
      else:
        tim[t[1] + ":" + t[2]] = 1
for j in sorted(tim.items()):
  print j[0],j[1]

This will generate output similar to what is below…

23:40 354
23:41 329
23:42 356
23:43 369
23:44 338
23:45 384
23:46 358
23:47 320
23:48 334
23:49 365
23:50 344
23:51 254
23:52 313
23:53 346
23:54 321
23:55 329
23:56 349
23:57 369
23:58 326
23:59 381

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.