[master] 467f10c Implement backend.list -j

Poul-Henning Kamp phk at FreeBSD.org
Sun May 13 19:01:27 UTC 2018


commit 467f10c5d572add23ff2f7e6ed5746cf89152b76
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sun May 13 18:45:34 2018 +0000

    Implement backend.list -j

diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 512886a..cc3eef8 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -418,11 +418,12 @@ vbe_list(const struct director *d, struct vsb *vsb, int vflag, int pflag,
 
 	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
 	CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC);
-	AZ(jflag);
 
 	if (bp->probe != NULL)
-		VBP_Status(vsb, bp, vflag | pflag);
-	else if ((vflag | pflag) == 0)
+		VBP_Status(vsb, bp, vflag | pflag, jflag);
+	else if ((vflag | pflag) == 0 && jflag)
+		VSB_printf(vsb, "\"%s\",\n", d->sick ? "sick" : "healthy");
+	else
 		VSB_printf(vsb, "%-10s", d->sick ? "sick" : "healthy");
 }
 
diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index 1872c70..940e242 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -81,4 +81,4 @@ void VBP_Insert(struct backend *b, struct vrt_backend_probe const *p,
     struct tcp_pool *);
 void VBP_Remove(struct backend *b);
 void VBP_Control(const struct backend *b, int stop);
-void VBP_Status(struct vsb *, const struct backend *, int details);
+void VBP_Status(struct vsb *, const struct backend *, int details, int json);
diff --git a/bin/varnishd/cache/cache_backend_probe.c b/bin/varnishd/cache/cache_backend_probe.c
index 37b6016..2299d1b 100644
--- a/bin/varnishd/cache/cache_backend_probe.c
+++ b/bin/varnishd/cache/cache_backend_probe.c
@@ -473,7 +473,7 @@ vbp_bitmap(struct vsb *vsb, char c, uint64_t map, const char *lbl)
 /*lint -e{506} constant value boolean */
 /*lint -e{774} constant value boolean */
 void
-VBP_Status(struct vsb *vsb, const struct backend *be, int details)
+VBP_Status(struct vsb *vsb, const struct backend *be, int details, int json)
 {
 	struct vbp_target *vt;
 	char buf[12];
@@ -483,9 +483,27 @@ VBP_Status(struct vsb *vsb, const struct backend *be, int details)
 	CHECK_OBJ_NOTNULL(vt, VBP_TARGET_MAGIC);
 
 	if (!details) {
-		bprintf(buf, "%d/%d %s", vt->good, vt->window,
-		    vt->backend->director->sick ? "bad" : "good");
-		VSB_printf(vsb, "%-10s", buf);
+		if (json) {
+			VSB_printf(vsb, "[%u, %u, \"%s\"]",
+			    vt->good, vt->window, 
+			    vt->backend->director->sick ? "bad" : "good");
+		} else {
+			bprintf(buf, "%u/%u %s", vt->good, vt->window,
+			    vt->backend->director->sick ? "bad" : "good");
+			VSB_printf(vsb, "%-10s", buf);
+		}
+		return;
+	}
+
+	if (json) {
+		VSB_printf(vsb, "{\n");
+#define BITMAP(nn, cc, tt, bb)					\
+		VSB_printf(vsb, "\t    \"bits_%c\": %ju,\n", cc, vt->nn);
+#include "tbl/backend_poll.h"
+		VSB_printf(vsb, "\t    \"good\": %u,\n", vt->good);
+		VSB_printf(vsb, "\t    \"threshold\": %u,\n", vt->threshold);
+		VSB_printf(vsb, "\t    \"window\": %u\n", vt->window);
+		VSB_printf(vsb, "\t    },\n");
 		return;
 	}
 
diff --git a/bin/varnishd/cache/cache_director.c b/bin/varnishd/cache/cache_director.c
index 671e305..51723a0 100644
--- a/bin/varnishd/cache/cache_director.c
+++ b/bin/varnishd/cache/cache_director.c
@@ -273,6 +273,8 @@ struct list_args {
 #define LIST_ARGS_MAGIC	0x7e7cefeb
 	int		p;
 	int		v;
+	int		j;
+	const char	*jsep;
 };
 
 static int v_matchproto_(vcl_be_func)
@@ -302,6 +304,39 @@ do_list(struct cli *cli, struct director *d, void *priv)
 	return (0);
 }
 
+static int v_matchproto_(vcl_be_func)
+do_list_json(struct cli *cli, struct director *d, void *priv)
+{
+	struct list_args *la;
+
+	CAST_OBJ_NOTNULL(la, priv, LIST_ARGS_MAGIC);
+	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
+
+	if (d->vdir->admin_health == VDI_AH_DELETED)
+		return (0);
+
+	VCLI_Out(cli, "%s", la->jsep);
+	la->jsep = ",\n    ";
+	// XXX admin health "probe" for the no-probe case is confusing
+	VCLI_Out(cli, "\"%s\": {\n", d->vdir->cli_name);
+	VCLI_Out(cli, "\t\"type\": \"%s\",\n", d->vdir->methods->type);
+	VCLI_Out(cli, "\t\"admin_health\": \"%s\",\n", VDI_Ahealth(d));
+	VCLI_Out(cli, "\t\"probe_health\": ");
+	if (d->vdir->methods->list != NULL)
+		d->vdir->methods->list(d, cli->sb, 0, 0, 1);
+	else
+		VCLI_Out(cli, "\"%s\"", d->sick ? "sick" : "healthy");
+	VCLI_Out(cli, ",\n");
+
+	if ((la->p || la->v) && d->vdir->methods->list != NULL) {
+		VCLI_Out(cli, "\t\"probe_details\": ");
+		d->vdir->methods->list(d, cli->sb, la->p, la->v, 1);
+	}
+	VCLI_Out(cli, "\t\"last_change\": %.3f\n    }",
+	    d->vdir->health_changed);
+	return (0);
+}
+
 static void v_matchproto_(cli_func_t)
 cli_backend_list(struct cli *cli, const char * const *av, void *priv)
 {
@@ -311,9 +346,11 @@ cli_backend_list(struct cli *cli, const char * const *av, void *priv)
 	(void)priv;
 	ASSERT_CLI();
 	INIT_OBJ(la, LIST_ARGS_MAGIC);
+	la->jsep = "\n    ";
 	while (av[2] != NULL && av[2][0] == '-') {
 		for(p = av[2] + 1; *p; p++) {
 			switch(*p) {
+			case 'j': la->j = 1; break;
 			case 'p': la->p = !la->p; break;
 			case 'v': la->p = !la->p; break;
 			default:
@@ -329,9 +366,15 @@ cli_backend_list(struct cli *cli, const char * const *av, void *priv)
 		VCLI_SetResult(cli, CLIS_PARAM);
 		return;
 	}
-	VCLI_Out(cli, "%-30s %-7s %-10s %s",
-	    "Backend name", "Admin", "Probe", "Last change");
-	(void)VCL_IterDirector(cli, av[2], do_list, la);
+	if (la->j) {
+		VCLI_Out(cli, "{");
+		(void)VCL_IterDirector(cli, av[2], do_list_json, la);
+		VCLI_Out(cli, "\n}\n");
+	} else {
+		VCLI_Out(cli, "%-30s %-7s %-10s %s",
+		    "Backend name", "Admin", "Probe", "Last change");
+		(void)VCL_IterDirector(cli, av[2], do_list, la);
+	}
 }
 
 /*---------------------------------------------------------------------*/
@@ -389,7 +432,8 @@ cli_backend_set_health(struct cli *cli, const char * const *av, void *priv)
 /*---------------------------------------------------------------------*/
 
 static struct cli_proto backend_cmds[] = {
-	{ CLICMD_BACKEND_LIST,		"", cli_backend_list },
+	{ CLICMD_BACKEND_LIST,		"",
+	     cli_backend_list, cli_backend_list },
 	{ CLICMD_BACKEND_SET_HEALTH,	"", cli_backend_set_health },
 	{ NULL }
 };
diff --git a/bin/varnishtest/tests/d00005.vtc b/bin/varnishtest/tests/d00005.vtc
index 1706901..202b57b 100644
--- a/bin/varnishtest/tests/d00005.vtc
+++ b/bin/varnishtest/tests/d00005.vtc
@@ -94,3 +94,6 @@ client c1 {
 	rxresp
 	expect resp.body == "4444"
 } -run
+
+varnish v1 -vsl_catchup
+varnish v1 -cliok "backend.list -j"
diff --git a/bin/varnishtest/tests/v00014.vtc b/bin/varnishtest/tests/v00014.vtc
index 01c8251..7b9212a 100644
--- a/bin/varnishtest/tests/v00014.vtc
+++ b/bin/varnishtest/tests/v00014.vtc
@@ -50,6 +50,7 @@ varnish v1 -vcl {
 } -start
 
 varnish v1 -cliok "backend.list -p"
+varnish v1 -cliok "backend.list -j -p"
 
 client c1 {
 	txreq


More information about the varnish-commit mailing list