Commit bf1d9979 authored by Geoff Simmons's avatar Geoff Simmons Committed by Dridi Boukelmoune

Add JSON support for the CLI "vcl.list" command.

parent 0a89d2c9
......@@ -664,6 +664,52 @@ vcl_cli_list(struct cli *cli, const char * const *av, void *priv)
}
}
static void v_matchproto_(cli_func_t)
vcl_cli_list_json(struct cli *cli, const char * const *av, void *priv)
{
struct vcl *vcl;
(void)priv;
ASSERT_CLI();
VCLI_JSON_begin(cli, 2, av);
VCLI_Out(cli, ",\n");
VTAILQ_FOREACH(vcl, &vcl_head, list) {
VCLI_Out(cli, "{\n");
VSB_indent(cli->sb, 2);
VCLI_Out(cli, "\"status\": ");
if (vcl == vcl_active) {
VCLI_Out(cli, "\"active\",\n");
} else if (vcl->discard) {
VCLI_Out(cli, "\"discarded\",\n");
} else
VCLI_Out(cli, "\"available\",\n");
VCLI_Out(cli, "\"state\": \"%s\",\n", vcl->state);
VCLI_Out(cli, "\"temperature\": \"%s\",\n", vcl->temp);
VCLI_Out(cli, "\"busy\": %u,\n", vcl->busy);
VCLI_Out(cli, "\"name\": \"%s\"", vcl->loaded_name);
if (vcl->label != NULL) {
VCLI_Out(cli, ",\n");
VCLI_Out(cli, "\"label\": {\n");
VSB_indent(cli->sb, 2);
VCLI_Out(cli, "\"name\": \"%s\"",
vcl->label->loaded_name);
if (vcl->nrefs)
VCLI_Out(cli, ",\n\"refs\": %d", vcl->nrefs);
VCLI_Out(cli, "\n");
VCLI_Out(cli, "}");
VSB_indent(cli->sb, -2);
} else if (vcl->nlabels > 0) {
VCLI_Out(cli, ",\n");
VCLI_Out(cli, "\"labels\": %d", vcl->nlabels);
}
VSB_indent(cli->sb, -2);
VCLI_Out(cli, "\n}");
if (VTAILQ_NEXT(vcl, list) != NULL)
VCLI_Out(cli, ",\n");
}
VCLI_JSON_end(cli);
}
static void v_matchproto_(cli_func_t)
vcl_cli_load(struct cli *cli, const char * const *av, void *priv)
{
......@@ -823,7 +869,7 @@ vcl_cli_show(struct cli *cli, const char * const *av, void *priv)
static struct cli_proto vcl_cmds[] = {
{ CLICMD_VCL_LOAD, "", vcl_cli_load },
{ CLICMD_VCL_LIST, "", vcl_cli_list },
{ CLICMD_VCL_LIST, "", vcl_cli_list, vcl_cli_list_json },
{ CLICMD_VCL_STATE, "", vcl_cli_state },
{ CLICMD_VCL_DISCARD, "", vcl_cli_discard },
{ CLICMD_VCL_USE, "", vcl_cli_use },
......
......@@ -795,6 +795,61 @@ mcf_vcl_list(struct cli *cli, const char * const *av, void *priv)
}
}
static void v_matchproto_(cli_func_t)
mcf_vcl_list_json(struct cli *cli, const char * const *av, void *priv)
{
unsigned status;
char *p;
struct vclprog *vp;
struct vcldep *vd;
/* NB: Shall generate same output as vcl_cli_list() */
(void)priv;
if (MCH_Running()) {
if (!mgt_cli_askchild(&status, &p, "vcl.list -j\n")) {
VCLI_SetResult(cli, status);
VCLI_Out(cli, "%s", p);
}
free(p);
} else {
VCLI_JSON_begin(cli, 2, av);
VCLI_Out(cli, ",\n");
VTAILQ_FOREACH(vp, &vclhead, list) {
VCLI_Out(cli, "{\n");
VSB_indent(cli->sb, 2);
VCLI_Out(cli, "\"status\": \"%s\",\n",
vp == active_vcl ? "active" : "available");
VCLI_Out(cli, "\"state\": \"%s\",\n", vp->state);
VCLI_Out(cli, "\"temperature\": \"%s\",\n",
vp->warm ? VCL_STATE_WARM : VCL_STATE_COLD);
VCLI_Out(cli, "\"name\": \"%s\"", vp->name);
if (mcf_is_label(vp)) {
vd = VTAILQ_FIRST(&vp->dfrom);
AN(vd);
VCLI_Out(cli, ",\n");
VCLI_Out(cli, "\"label\": {\n");
VSB_indent(cli->sb, 2);
VCLI_Out(cli, "\"name\": \"%s\"", vd->to->name);
if (vp->nto > 0)
VCLI_Out(cli, ",\n\"refs\": %d",
vp->nto);
VSB_indent(cli->sb, -2);
VCLI_Out(cli, "\n");
VCLI_Out(cli, "}");
} else if (vp->nto > 0) {
VCLI_Out(cli, ",\n");
VCLI_Out(cli, "\"labels\": %d", vp->nto);
}
VSB_indent(cli->sb, -2);
VCLI_Out(cli, "\n}");
if (VTAILQ_NEXT(vp, list) != NULL)
VCLI_Out(cli, ",\n");
}
VCLI_JSON_end(cli);
}
}
static void v_matchproto_(cli_func_t)
mcf_vcl_label(struct cli *cli, const char * const *av, void *priv)
{
......@@ -888,7 +943,7 @@ static struct cli_proto cli_vcl[] = {
{ CLICMD_VCL_USE, "", mcf_vcl_use },
{ CLICMD_VCL_STATE, "", mcf_vcl_state },
{ CLICMD_VCL_DISCARD, "", mcf_vcl_discard },
{ CLICMD_VCL_LIST, "", mcf_vcl_list },
{ CLICMD_VCL_LIST, "", mcf_vcl_list, mcf_vcl_list_json },
{ CLICMD_VCL_LABEL, "", mcf_vcl_label },
{ NULL }
};
......
......@@ -11,9 +11,13 @@ varnish v1 -vcl+backend {}
varnish v1 -vcl+backend {}
varnish v1 -cliok vcl.list
varnish v1 -clijson "vcl.list -j"
varnish v1 -cliok start
varnish v1 -cliok vcl.list
varnish v1 -clijson "vcl.list -j"
varnish v1 -cliok "vcl.use vcl1"
varnish v1 -clierr 300 "vcl.discard vcl1"
......
......@@ -39,6 +39,7 @@ varnish v1 -clierr 106 "vcl.label foo vcl0"
varnish v1 -cliok "vcl.label foo vcl2"
varnish v1 -cliok "vcl.label bar vcl2"
varnish v1 -cliok "vcl.list"
varnish v1 -clijson "vcl.list -j"
varnish v1 -clierr 300 "vcl.discard vcl2"
varnish v1 -cliok "vcl.discard bar"
varnish v1 -cliok "vcl.label foo vcl1"
......@@ -100,6 +101,7 @@ varnish v1 -cliok "vcl.label label1 vcl1"
varnish v1 -cliok "vcl.label label2 vcl1"
varnish v1 -cliok "vcl.label label3 vcl1"
varnish v1 -cliok "vcl.list"
varnish v1 -clijson "vcl.list -j"
varnish v1 -start
varnish v1 -cliok "vcl.list"
......@@ -111,6 +113,7 @@ client c1 -run
#######################################################################
varnish v1 -cliok vcl.list
varnish v1 -clijson "vcl.list -j"
varnish v1 -vcl+backend { }
......
......@@ -18,6 +18,7 @@ varnish v1 -cliok "vcl.state vcl1 cold"
delay 1
varnish v1 -cliexpect "cold/cooling.*vcl1" vcl.list
varnish v1 -clijson "vcl.list -j"
# It can't be warmed up yet
delay 1
......@@ -26,6 +27,7 @@ varnish v1 -cliexpect "vmod-debug ref on vcl1" "vcl.state vcl1 warm"
# It will eventually cool down
delay 2
varnish v1 -cliexpect "cold/cold.*vcl1" vcl.list
varnish v1 -clijson "vcl.list -j"
# At this point it becomes possible to warm up again
varnish v1 -cliok "vcl.state vcl1 warm"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment