From c9685cd10e9f2745b55b03516a0594fe616e3e29 Mon Sep 17 00:00:00 2001 From: andersb Date: Mon, 7 Aug 2006 00:21:34 +0000 Subject: [PATCH] Fresh start after study of output from varnishlog -o. First off is IP adress logging and clearing. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@703 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishncsa/varnishncsa.c | 247 ++++---------------- 1 file changed, 43 insertions(+), 204 deletions(-) diff --git a/varnish-cache/bin/varnishncsa/varnishncsa.c b/varnish-cache/bin/varnishncsa/varnishncsa.c index 366cba11..c205a70d 100644 --- a/varnish-cache/bin/varnishncsa/varnishncsa.c +++ b/varnish-cache/bin/varnishncsa/varnishncsa.c @@ -35,20 +35,8 @@ struct logline { char df_h[4 * (3 + 1)]; // Datafield for %h (IP adress) - // XXX Set to 1 if we have a IP adress. Not sure when to unset. - // Know for sure when we have a real SessionClose. Probably - // When we clean also. When we have timeout. Are there any more? - int v; - // Set to 1 if we wanna print the loglinestring because we are done - int w; - // unsigned char *df_l; // Datafield for %l - // unsigned char *df_u; // Datafield for %u - struct tm *logline_time; // Datafield for %t - unsigned char *df_r; // Datafield for %r - unsigned char *df_s; // Datafield for %s - unsigned char *df_b; // Datafield for %b - unsigned char *df_R; // Datafield for %{Referer}i - unsigned char *df_U; // Datafield for %{User-agent}i + unsigned char *df_r; // Datafield for %r (Request) + }; /* We make a array of pointers to sbuf's. Sbuf is a string buffer. @@ -85,219 +73,96 @@ static void extended_log_format(unsigned char *p, char *w_opt) { unsigned u,v,w; - int i,j; - unsigned char *tmpPtr; - char *tmpPtra; - char *tmpPtrb; - char *tmpPtrc; - int timesec = 0; // Where we store the utime for request as int. - char temp_time[27]; // Where we store the string we take from the log - time_t req_time; // Timeobject used for making the requesttime. - - // Variables used to clean memory. - int cm_h = 0; - int cm_r = 0; - int cm_s = 0; - int cm_b = 0; - int cm_R = 0; - int cm_U = 0; - - - - if (w_opt != NULL){ - // printf(" Has w_opt\n"); - } else { - //printf(" Does not have w_opt:\n"); - } + // Used for getting IP. + unsigned char *tmpPtr; + int j; u = (p[2] << 8) | p[3]; if (ob[u] == NULL) { ob[u] = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); assert(ob[u] != NULL); } - //printf("Hele [%d]: %s %s\n",u, p+4); - i = 0; v = 0; w = 0; - ll[u].w = 0; switch (p[0]) { - // XXX remember to check for NULL when strdup, if no allocate - // XXX also remember to free() after strdup? - case SLT_SessionOpen: - // Finding the IP adress when data is: "XXX.XXX.XXX.XXX somenumber" + + // We catch the IP adress of the session. + // We also catch IP in SessionReuse because we can not always + // be sure we see a SessionOpen when we start logging. tmpPtr = strchr(p + 4, ' '); - j = strlen(p + 4) - strlen(tmpPtr); // length of IP - strncpy(ll[u].df_h, p + 4, j); - ll[u].df_h[j] = '\0'; // put on a NULL at end of buffer. - //printf("New session [%d]: %s \n",u, ll[u].df_h); - ll[u].v = 1; // We have IP - - // We have a new session. This is a good place to initialize and - // clean data from previous sessions with same filedescriptor. - - //free(ll[u].df_U); + j = strlen(p + 4) - strlen(tmpPtr); // length of IP + strncpy(ll[u].df_h, p + 4, j); + ll[u].df_h[j] = '\0'; // put on a NULL at end of buffer. + //printf("New session [%d]: %s \n",u, ll[u].df_h); break; case SLT_RxRequest: - // XXX: Remember to support more than GET, HEAD and POST. - // http://rfc.net/rfc2616.html#p51 - // - // Have to gather together data in SLT_RxRequest, SLT_RxURL, SLT_RxProtocol - // to build the request, so I use a sbuf. - - sbuf_clear(ob[u]); - - if (p[1] >= 4 && !strncasecmp((void *)&p[4], "HEAD",4)){ - sbuf_bcat(ob[u], p + 4, strlen(p + 4)); - //printf("Got a HEAD\n"); - } - - else if (p[1] >= 4 && !strncasecmp((void *)&p[4], "POST",4)){ - sbuf_bcat(ob[u], p + 4, strlen(p + 4)); - //printf("Got a POST\n"); - } - - else if (p[1] >= 3 && !strncasecmp((void *)&p[4], "GET",3)){ - sbuf_bcat(ob[u], p + 4, strlen(p + 4)); - //printf("Got a GET\n"); - } - - else { - sbuf_bcat(ob[u], p + 4, strlen(p + 4)); - //printf("Got something other than HEAD, POST, GET\n"); - } break; case SLT_RxURL: - sbuf_cat(ob[u], " "); - sbuf_bcat(ob[u], p + 4, strlen(p + 4)); - break; case SLT_RxProtocol: - sbuf_cat(ob[u], " "); - sbuf_bcat(ob[u], p + 4, strlen(p + 4)); - break; case SLT_RxHeader: - if (p[1] >= 11 && !strncasecmp((void *)&p[4], "user-agent:",11)){ - ll[u].df_U = strdup(p + 4); - cm_U = 1; - } - if (p[1] >= 8 && !strncasecmp((void *)&p[4], "referer:",8)){ - ll[u].df_R = strdup(p + 4); - cm_R = 1; - } - else if (ll[u].df_R == NULL){ - ll[u].df_R = strdup(p + 4); - ll[u].df_R[0] = '-'; - ll[u].df_R[1] = '\0'; - cm_R = 1; - } - break; case SLT_ReqServTime: - // First clear temp_time - temp_time[0] = '\0'; - - tmpPtrb = strdup(p + 4); - - for ( tmpPtra = strtok(tmpPtrb," "); tmpPtra != NULL; tmpPtra = strtok(NULL, " ")){ - //if (i = 1){ - tmpPtrc = tmpPtra; - //} - //printf("ReqServTime number %d: %s\n", i, tmpPtra); - - i++; - } - - - //printf("Br: %s\n",tmpPtrc); - - /* - tmpPtr = strchr(tmpPtrc, '.'); - j = strlen(tmpPtrc) - strlen(tmpPtr); // length of timestamp - strncpy(temp_time, tmpPtrc, j); - temp_time[j] = '\0'; - printf("j: %s",temp_time); - timesec = atoi(temp_time); - */ - timesec = 1; - req_time = timesec; - ll[u].logline_time = localtime(&req_time); - strftime (temp_time, 50, "[%d/%b/%Y:%X %z] ", ll[u].logline_time); - cm_r = 1; - break; case SLT_TxStatus: - ll[u].df_s = strdup(p + 4); - break; case SLT_Length: - ll[u].df_b = strdup(p + 4); - if (!atoi(ll[u].df_b)){ - ll[u].df_b[0] = '-'; - ll[u].df_b[1] = '\0'; - } - break; case SLT_SessionClose: - if (p[1] >= 7 && !strncasecmp((void *)&p[4], "timeout",7)){ - // XXX what to do with the timeout? - // Right now I am gonna just let it pass, and not even clean memory. - //printf("Timeout...\n"); - //ll[u].w = 1; - } - else{ - - ll[u].w = 1; // We are done, clean memory + // Session is closed, we clean up things. But do not write. - } + //printf("Session close [%d]\n", u); + + v = 1; - free(ll[u].df_U); break; case SLT_SessionReuse: - // XXX have to catch the IP in the SessionReuse in case - // We never got the SessionOpen and the client keeps open + // It's in SessionReuse we wrap things up. + // SessionClose is not suited to use for a write, but to clean up. + // Catch IP if not already done. + if (ll[u].df_h[0] == '\0'){ - // If we are here, there is a session going on, and we haven't - // catched the IP in SessionOpen, we "steal" it from SessionReuse. - // + // We don't have IP, fetch it. + tmpPtr = strchr(p + 4, ' '); - j = strlen(p + 4) - strlen(tmpPtr); // length of IP - strncpy(ll[u].df_h, p + 4, j); - ll[u].df_h[j] = '\0'; // put on a NULL at end of buffer. - ll[u].v = 1; // We have a IP - + j = strlen(p + 4) - strlen(tmpPtr); // length of IP + strncpy(ll[u].df_h, p + 4, j); + ll[u].df_h[j] = '\0'; // put on a NULL at end of buffer. + printf("Got IP from Reuse [%d] : %s\n", u, ll[u].df_h); } - - ll[u].w = 1; // We are done, clean memory - + //printf("Session reuse [%d]\n", u); + + w = 1; + break; default: @@ -305,46 +170,20 @@ extended_log_format(unsigned char *p, char *w_opt) break; } - // Memorycleaner and stringwriter. w is 1 after SLT_SessionClose OR SLT_SessionReuse that - // do something useful. v is set when we have a real IP adress, somewhere we are getting - // requests without. - // - // XXX Find out why we don't have IP and get rid of v. - // - if (ll[u].w && ll[u].v) { - - - - printf("%s - - %s", ll[u].df_h, temp_time); - sbuf_finish(ob[u]); - printf("\"%s\"", sbuf_data(ob[u])); - printf(" %s %s \"%s\" \"%s\"\n", ll[u].df_s, ll[u].df_b, ll[u].df_R, ll[u].df_U); - sbuf_clear(ob[u]); - ll[u].df_U == NULL; - - - - if (cm_R){ - // Clean the memory for Referer - free(ll[u].df_R); - } - if (cm_U){ - // Clean User-Agent. - - // Clean memory for User-Agent - free(ll[u].df_U); + if (v) { + // We have a SessionClose. Lets clean. + // + // Clean IP adress + ll[u].df_h[0] = '\0'; + + } - // Initialize User-Agent. - ll[u].df_U == NULL; + if (w) { + // We have a SessionReuse. Lets print the logline + // - } - if (cm_r){ - // Clean memory for Date variables - free(tmpPtrb); - } - // XXX We reinitialize the struct logline - // free(ll[u]); - + printf("%s ", ll[u].df_h); + printf("\n"); } -- 2.39.5