I needed to get this information quickly without new deployments. This can be done by modifying the JSP and adding the following snippet. (Only sessions with activity will receive a value):
<% // Set user agent and ip in session session.setAttribute("agent", request.getHeader("user-agent") + "@" + request.getRemoteAddr()); %>
Then create a groovy script to request jmx:
import javax.management.remote.* import javax.management.* import groovy.jmx.builder.* // Setup JMX connection. def connection = new JmxBuilder().client(port: 4934, host: '192.168.10.6') connection.connect() // Get the MBeanServer. def mbeans = connection.MBeanServerConnection def mbean = new GroovyMBean(mbeans, 'Catalina:type=Manager,host=localhost,context=/') println "Active sessions: " + mbean['activeSessions'] def sessions = mbean.listSessionIds().tokenize(' '); def ips = [:]; def agents = [:]; sessions.each { def agentString = mbean.getSessionAttribute(it, "agent"); if(agentString != null) { agent = agentString.tokenize('@'); } else { agent = ['unknown', 'unknown']; } if(agents[agent[0]] == null) { agents[agent[0]] = []; } agents[agent[0]] += [it]; if(ips[agent[1]] == null) { ips[agent[1]] = []; } ips[agent[1]] += it; }; println "Ips" ips = ips.sort { -it.value.size } ips.each { ip, list -> println "${ip}\t${list.value.size}"; //println list; //println ""; } println "" println "Agents" agents = agents.sort { -it.value.size } agents.each { agent, list -> println "${agent}\t${agents[agent].size}"; //println list; //println ""; }
Result
Active sessions: 729 Ips unknown 102 68.180.230.118 11 80.213.88.107 11 157.55.39.127 9 81.191.247.166 2 ... Agents Mozilla/5.0 (compatible; MJ12bot/v1.4.5; http://www.majestic12.co.uk/bot.php?+) 117 unknown 102 Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) 55 Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp) 29 ...
Brimstedt Jul 22 '15 at 11:13 2015-07-22 11:13
source share