r5419 - in trunk/varnish-cache: bin/varnishncsa doc/sphinx/reference

martin at varnish-cache.org martin at varnish-cache.org
Fri Oct 8 13:48:35 CEST 2010


Author: martin
Date: 2010-10-08 13:48:35 +0200 (Fri, 08 Oct 2010)
New Revision: 5419

Modified:
   trunk/varnish-cache/bin/varnishncsa/varnishncsa.c
   trunk/varnish-cache/doc/sphinx/reference/varnishncsa.rst
Log:
Add a -o option to varnishncsa to allow filtering of output to only request groups which match a regular expression on a given tag.

Add errors on -i and -I options in varnishncsa, as these options does not make sense for that utility.

Add documentation to varnishncsa for -o option, and remove documentation for -i and -I.



Modified: trunk/varnish-cache/bin/varnishncsa/varnishncsa.c
===================================================================
--- trunk/varnish-cache/bin/varnishncsa/varnishncsa.c	2010-10-07 15:44:31 UTC (rev 5418)
+++ trunk/varnish-cache/bin/varnishncsa/varnishncsa.c	2010-10-08 11:48:35 UTC (rev 5419)
@@ -79,6 +79,7 @@
 
 #include "libvarnish.h"
 #include "vsl.h"
+#include "vre.h"
 #include "varnishapi.h"
 
 static volatile sig_atomic_t reopen;
@@ -98,12 +99,31 @@
 	char *df_u;			/* %u, Remote user */
 	int active;			/* Is log line in an active trans */
 	int complete;			/* Is log line complete */
+	int matched;			/* Did log line match */
 } **ll;
 
 static size_t nll;
 static int prefer_x_forwarded_for = 0;
 
+static int o_flag = 0;
+static int match_tag;
+static vre_t *match_tag_re;
+
 static int
+name2tag(const char *n)
+{
+	int i;
+
+	for (i = 0; i < 256; i++) {
+		if (VSL_tags[i] == NULL)
+			continue;
+		if (!strcasecmp(n, VSL_tags[i]))
+			return (i);
+	}
+	return (-1);
+}
+
+static int
 isprefix(const char *str, const char *prefix, const char *end,
     const char **next)
 {
@@ -313,6 +333,11 @@
 	assert(spec & VSL_S_CLIENT);
 	end = ptr + len;
 
+	/* Do -o matching if specified */
+	if (o_flag && match_tag == tag && lp->active && 
+	   VRE_exec(match_tag_re, ptr, len, 0, 0, NULL, 0) > 0)
+		lp->matched = 1;
+
 	switch (tag) {
 	case SLT_ReqStart:
 		if(lp->active || lp->df_h != NULL) {
@@ -457,6 +482,10 @@
 	if(!lp->complete)
 		return (reopen);
 
+	if (o_flag && !lp->matched)
+		/* -o is in effect matching rule failed. Don't display */
+		return (reopen);
+
 #if 0
 	/* non-optional fields */
 	if (!lp->df_m || !lp->df_Uq || !lp->df_H || !lp->df_s) {
@@ -587,7 +616,7 @@
 	vd = VSM_New();
 	VSL_Setup(vd);
 
-	while ((c = getopt(argc, argv, VSL_ARGS "aDP:Vw:f")) != -1) {
+	while ((c = getopt(argc, argv, VSL_ARGS "aDP:Vw:fo")) != -1) {
 		switch (c) {
 		case 'a':
 			a_flag = 1;
@@ -611,9 +640,20 @@
 			fprintf(stderr, "-b is not valid for varnishncsa\n");
 			exit(1);
 			break;
+		case 'i':
+			fprintf(stderr, "-i is not valid for varnishncsa\n");
+			exit(1);
+			break;
+		case 'I':
+			fprintf(stderr, "-I is not valid for varnishncsa\n");
+			exit(1);
+			break;
 		case 'c':
 			/* XXX: Silently ignored: it's required anyway */
 			break;
+		case 'o':
+			o_flag = 1;
+			break;
 		default:
 			if (VSL_Arg(vd, c, optarg) > 0)
 				break;
@@ -623,6 +663,26 @@
 
 	VSL_Arg(vd, 'c', optarg);
 
+	if (o_flag) {
+		const char *error;
+		int erroroffset;
+
+		if (argc-optind != 2) {
+			fprintf(stderr, "Wrong number of arguments when using -o\n");
+			exit(2);
+		}
+		match_tag = name2tag(argv[optind]);
+		if (match_tag < 0) {
+			fprintf(stderr, "Tag \"%s\" unknown\n", argv[optind]);
+			exit(2);
+		}
+		match_tag_re = VRE_compile(argv[optind + 1], 0, &error, &erroroffset);
+		if (match_tag_re==NULL) {
+			fprintf(stderr, "Invalid regex: %s\n", error);
+			exit(2);
+		}
+	}
+
 	if (VSL_Open(vd, 1))
 		exit(1);
 

Modified: trunk/varnish-cache/doc/sphinx/reference/varnishncsa.rst
===================================================================
--- trunk/varnish-cache/doc/sphinx/reference/varnishncsa.rst	2010-10-07 15:44:31 UTC (rev 5418)
+++ trunk/varnish-cache/doc/sphinx/reference/varnishncsa.rst	2010-10-08 11:48:35 UTC (rev 5419)
@@ -48,13 +48,9 @@
 -f          Prefer the X-Forwarded-For HTTP header over client.ip in 
 	    the log output.
 
--I regex    Include log entries which match the specified regular 
-   	    expression.  If neither -I nor -i is specified, 
-	    all log entries are included.
+-o	    Filter log output to request groups that match a regular
+	    expression on a specified tag.
 
--i tag      Include log entries with the specified tag.  If neither -I nor 
-   	    -i is specified, all log entries are included.
-
 -n          Specifies the name of the varnishd instance to get logs 
 	    from.  If -n is not specified, the host name is used.
 
@@ -77,6 +73,10 @@
 
 -x tag      Exclude log entries with the specified tag.
 
+If the -o option was specified, a tag and a regex argument must be given.
+varnishncsa will then only log for request groups which include that tag
+and the regular expression matches on that tag.
+
 SEE ALSO
 ========
 




More information about the varnish-commit mailing list