1 19 20 package com.sslexplorer.server; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.PrintWriter ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.mortbay.jetty.Server; 29 30 import com.sslexplorer.boot.ContextHolder; 31 import com.sslexplorer.boot.Util; 32 33 public class StatsLogger extends Thread { 34 final static Log log = LogFactory.getLog(StatsLogger.class); 35 36 private Server server; 37 private int update; 38 39 public StatsLogger(Server server, int update) { 40 super("StatsLogger"); 41 server.setStatsOn(true); 42 setDaemon(true); 43 this.server = server; 44 this.update = update; 45 start(); 46 } 47 48 public void run() { 49 FileOutputStream out = null; 50 try { 51 out = new FileOutputStream (new File (ContextHolder.getContext().getLogDirectory(), "stats.csv")); 52 PrintWriter pw = new PrintWriter (out, true); 53 pw.println("connections,connectionsOpen,connectionsOpenMax,connectionsDurationAve," + "connectionsDurationMax,connectionsRequestsAve,connectionsRequestsMax," 54 + "errors,requests,requestsActive,requestsActiveMax,requestsDurectionAve,requestsDurationMax"); 55 StringBuffer sb = new StringBuffer (); 56 while (true) { 57 sb.append(server.getConnections()); 58 sb.append(","); 59 sb.append(server.getConnectionsOpen()); 60 sb.append(","); 61 sb.append(server.getConnectionsOpenMax()); 62 sb.append(","); 63 sb.append(server.getConnectionsDurationAve()); 64 sb.append(","); 65 sb.append(server.getConnectionsDurationMax()); 66 sb.append(","); 67 sb.append(server.getConnectionsRequestsAve()); 68 sb.append(","); 69 sb.append(server.getConnectionsRequestsMax()); 70 sb.append(","); 71 sb.append(server.getErrors()); 72 sb.append(","); 73 sb.append(server.getRequests()); 74 sb.append(","); 75 sb.append(server.getRequestsActive()); 76 sb.append(","); 77 sb.append(server.getRequestsActiveMax()); 78 sb.append(","); 79 sb.append(server.getRequestsDurationAve()); 80 sb.append(","); 81 sb.append(server.getRequestsDurationMax()); 82 pw.println(sb.toString()); 83 sb.setLength(0); 84 Thread.sleep(update); 85 } 86 } catch (Exception e) { 87 log.error("Failed to create stats files.", e); 88 } finally { 89 Util.closeStream(out); 90 } 91 } 92 93 } 94 | Popular Tags |