[master] 0a36e0b Implement -t timeout logic for varnishstat

Martin Blix Grydeland martin at varnish-software.com
Thu Apr 9 15:16:28 CEST 2015


commit 0a36e0b311a0663545ca78ff1f73184bcac789f7
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Thu Apr 9 14:20:01 2015 +0200

    Implement -t timeout logic for varnishstat

diff --git a/bin/varnishstat/varnishstat.c b/bin/varnishstat/varnishstat.c
index 3f8f9d0..bd0beee 100644
--- a/bin/varnishstat/varnishstat.c
+++ b/bin/varnishstat/varnishstat.c
@@ -33,12 +33,16 @@
 #include "config.h"
 
 #include <sys/time.h>
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <math.h>
+#include <stdint.h>
+
+#include "vnum.h"
+#include "vtim.h"
 
 #include "varnishstat.h"
 
@@ -275,10 +279,12 @@ main(int argc, char * const *argv)
 {
 	int c;
 	struct VSM_data *vd;
-	double delay = 1.0;
-	int   once = 0, xml = 0, json = 0, do_repeat = 0, f_list = 0;
+	double delay = 1.0, t_arg = 5.0, t_start = NAN;
+	int once = 0, xml = 0, json = 0, do_repeat = 0, f_list = 0, curses = 0;
+	int i;
 
 	vd = VSM_New();
+	AN(vd);
 
 	while ((c = getopt(argc, argv, VSC_ARGS "1f:lVw:xjt:")) != -1) {
 		switch (c) {
@@ -288,12 +294,31 @@ main(int argc, char * const *argv)
 		case 'l':
 			f_list = 1;
 			break;
+		case 't':
+			if (!strcasecmp(optarg, "off"))
+				t_arg = -1.;
+			else {
+				t_arg = VNUM(optarg);
+				if (isnan(t_arg)) {
+					fprintf(stderr, "-t: Syntax error");
+					exit(1);
+				}
+				if (t_arg < 0.) {
+					fprintf(stderr, "-t: Range error");
+					exit(1);
+				}
+			}
+			break;
 		case 'V':
 			VCS_Message("varnishstat");
 			exit(0);
 		case 'w':
 			do_repeat = 1;
-			delay = atof(optarg);
+			delay = VNUM(optarg);
+			if (isnan(delay)) {
+				fprintf(stderr, "-w: Syntax error");
+				exit(1);
+			}
 			break;
 		case 'x':
 			xml = 1;
@@ -309,15 +334,40 @@ main(int argc, char * const *argv)
 		}
 	}
 
-	if (VSM_Open(vd)) {
-		fprintf(stderr, "%s\n", VSM_Error(vd));
-		exit(1);
+	if (!(xml || json || once || f_list))
+		curses = 1;
+
+	while (1) {
+		i = VSM_Open(vd);
+		if (!i)
+			break;
+		if (isnan(t_start) && t_arg > 0.) {
+			fprintf(stderr, "Can't open log -"
+			    " retrying for %.0f seconds\n", t_arg);
+			t_start = VTIM_real();
+		}
+		if (t_arg <= 0.)
+			break;
+		if (VTIM_real() - t_start > t_arg)
+			break;
+		VSM_ResetError(vd);
+		VTIM_sleep(0.5);
 	}
-	if (!(xml || json || once || f_list)) {
+
+	if (curses) {
+		if (i && t_arg >= 0.) {
+			fprintf(stderr, "%s\n", VSM_Error(vd));
+			exit(1);
+		}
 		do_curses(vd, delay);
 		exit(0);
 	}
 
+	if (i) {
+		fprintf(stderr, "%s\n", VSM_Error(vd));
+		exit(1);
+	}
+
 	while (1) {
 		if (xml)
 			do_xml(vd);



More information about the varnish-commit mailing list