[experimental-ims] ac387cf varnishstat: Add json output and continous mode
Geoff Simmons
geoff at varnish-cache.org
Mon Jan 9 21:52:29 CET 2012
commit ac387cf066aead9e0c0aae63dc0802417d1ca306
Author: Lasse Karstensen <lasse at varnish-software.com>
Date: Mon Nov 7 13:14:35 2011 +0100
varnishstat: Add json output and continous mode
diff --git a/bin/varnishstat/varnishstat.c b/bin/varnishstat/varnishstat.c
index 3c6c4b5..c20b87f 100644
--- a/bin/varnishstat/varnishstat.c
+++ b/bin/varnishstat/varnishstat.c
@@ -82,6 +82,69 @@ do_xml(struct VSM_data *vd)
printf("</varnishstat>\n");
}
+
+/*--------------------------------------------------------------------*/
+
+static int
+do_json_cb(void *priv, const struct VSC_point * const pt)
+{
+ uint64_t val;
+ int *jp;
+ char jsonkey[255];
+ char jsontmp[255];
+
+ jp = priv;
+
+ assert(!strcmp(pt->fmt, "uint64_t"));
+ val = *(const volatile uint64_t*)pt->ptr;
+
+ if (*jp) *jp = 0; else printf(",\n");
+ jsonkey[0] = '\0';
+
+ /* build the JSON key name. */
+ if (strcmp(pt->ident, "") && strcmp(pt->class, "")) sprintf(jsonkey, "%s.%s", pt->class, pt->ident);
+ if (strcmp(pt->ident, "") && !strcmp(pt->class, "")) sprintf(jsonkey, "%s", pt->ident);
+ if (!strcmp(pt->ident, "") && strcmp(pt->class, "")) sprintf(jsonkey, "%s", pt->class);
+
+ strcpy(jsontmp, jsonkey);
+ if (strcmp(jsonkey, "")) sprintf(jsonkey, "%s.%s", jsontmp, pt->name);
+ else strcpy(jsonkey, pt->name);
+
+ printf("\t\"%s\": {", jsonkey);
+
+ if (strcmp(pt->class, "")) printf("\"type\": \"%s\", ", pt->class);
+ if (strcmp(pt->ident, "")) printf("\"ident\": \"%s\", ", pt->ident);
+
+ printf("\"value\": %ju, ", val);
+
+ printf("\"flag\": \"%c\", ", pt->flag);
+ printf("\"description\": \"%s\"", pt->desc);
+ printf("}");
+
+ if (*jp) printf("\n");
+ return (0);
+}
+
+static void
+do_json(struct VSM_data *vd)
+{
+ char time_stamp[20];
+ time_t now;
+ int jp;
+
+ jp = 1;
+
+ printf("{\n");
+ now = time(NULL);
+
+ (void)strftime(time_stamp, 20, "%Y-%m-%dT%H:%M:%S", localtime(&now));
+ printf("\t\"timestamp\": \"%s\",\n", time_stamp);
+ (void)VSC_Iter(vd, do_json_cb, &jp);
+ printf("\n}\n");
+ fflush(stdout);
+}
+
+
/*--------------------------------------------------------------------*/
struct once_priv {
@@ -167,7 +230,7 @@ usage(void)
"[-1lV] [-f field_list] "
VSC_n_USAGE " "
"[-w delay]\n");
- fprintf(stderr, FMT, "-1", "Print the statistics once and exit");
+ fprintf(stderr, FMT, "-1", "Print the statistics to stdout.");
fprintf(stderr, FMT, "-f field_list",
"Comma separated list of fields to display. ");
fprintf(stderr, FMT, "",
@@ -178,9 +241,11 @@ usage(void)
"The varnishd instance to get logs from");
fprintf(stderr, FMT, "-V", "Display the version number and exit");
fprintf(stderr, FMT, "-w delay",
- "Wait delay seconds between updates. The default is 1.");
+ "Wait delay seconds between updates. Default is 1 second. Can also be be used with -1, -x or -j for repeated output.");
fprintf(stderr, FMT, "-x",
- "Print statistics once as XML and exit.");
+ "Print statistics to stdout as XML.");
+ fprintf(stderr, FMT, "-j",
+ "Print statistics to stdout as JSON.");
#undef FMT
exit(1);
}
@@ -191,12 +256,12 @@ main(int argc, char * const *argv)
int c;
struct VSM_data *vd;
const struct VSC_C_main *VSC_C_main;
- int delay = 1, once = 0, xml = 0;
+ int delay = 1, once = 0, xml = 0, json = 0, do_repeat = 0;
vd = VSM_New();
VSC_Setup(vd);
- while ((c = getopt(argc, argv, VSC_ARGS "1f:lVw:x")) != -1) {
+ while ((c = getopt(argc, argv, VSC_ARGS "1f:lVw:xjt:")) != -1) {
switch (c) {
case '1':
once = 1;
@@ -210,11 +275,15 @@ main(int argc, char * const *argv)
VCS_Message("varnishstat");
exit(0);
case 'w':
+ do_repeat = 1;
delay = atoi(optarg);
break;
case 'x':
xml = 1;
break;
+ case 'j':
+ json = 1;
+ break;
default:
if (VSC_Arg(vd, c, optarg) > 0)
break;
@@ -227,12 +296,27 @@ main(int argc, char * const *argv)
VSC_C_main = VSC_Main(vd);
- if (xml)
- do_xml(vd);
- else if (once)
- do_once(vd, VSC_C_main);
- else
+ if (!(xml || json || once)) {
do_curses(vd, VSC_C_main, delay);
+ exit(0);
+ }
+
+ while (1) {
+ if (xml)
+ do_xml(vd);
+ else if (json)
+ do_json(vd);
+ else if (once)
+ do_once(vd, VSC_C_main);
+ else {
+ assert(0);
+ }
+ if (!do_repeat) break;
+
+ // end of output block marker.
+ printf("\n");
+ sleep(delay);
+ }
exit(0);
}
diff --git a/doc/sphinx/reference/varnishstat.rst b/doc/sphinx/reference/varnishstat.rst
index 09c4989..3c5d541 100644
--- a/doc/sphinx/reference/varnishstat.rst
+++ b/doc/sphinx/reference/varnishstat.rst
@@ -10,15 +10,16 @@ Varnish Cache statistics
:Author: Dag-Erling Smørgrav
:Author: Per Buer
-:Date: 2010-06-1
-:Version: 1.0
+:Author: Lasse Karstensen
+:Date: 2011-11-07
+:Version: 1.1
:Manual section: 1
SYNOPSIS
========
-varnishstat [-1] [-x] [-f field_list] [-l] [-n varnish_name] [-V] [-w delay]
+varnishstat [-1] [-x] [-j] [-f field_list] [-l] [-n varnish_name] [-V] [-w delay]
DESCRIPTION
===========
@@ -27,7 +28,7 @@ The varnishstat utility displays statistics from a running varnishd(1) instance.
The following options are available:
--1 Instead of presenting of a continuously updated display, print the statistics once and exit.
+-1 Instead of presenting of a continuously updated display, print the statistics to stdout.
-f A comma separated list of the fields to display. If it starts with '^' it is used as an exclusion
list.
@@ -39,9 +40,11 @@ The following options are available:
-V Display the version number and exit.
--w delay Wait delay seconds between updates. The default is 1.
+-w delay Wait delay seconds between updates. The default is 1. Can also be used with -1, -x or -j for repeated output.
--x Displays the result as XML once.
+-x Displays the result as XML.
+
+-j Displays the result as JSON.
The columns in the main display are, from left to right:
@@ -65,6 +68,29 @@ When using the -x option, the output is::
<description>FIELD DESCRIPTION</description>
</stat>
+With -j the output format is::
+
+ {
+ "timestamp": "YYYY-MM-DDTHH:mm:SS",
+ "client_conn": {
+ "value": 0, "flag": "a",
+ "description": "Client connections accepted"
+ },
+ "client_drop": {
+ "value": 0, "flag": "a",
+ "description": "Connection dropped, no sess/wrk"
+ },
+ "LCK.backend.creat": {
+ "type": "LCK", "ident": "backend", "value": 1,
+ "flag": "a", "description": "Created locks"
+ },
+ [..]
+ }
+
+Timestamp is the time when the report was generated by varnishstat.
+
+Repeated output with -1, -x or -j will have a single empty line (\\n) between each block of output.
+
SEE ALSO
========
@@ -91,4 +117,4 @@ This document is licensed under the same licence as Varnish
itself. See LICENCE for details.
* Copyright (c) 2006 Verdens Gang AS
-* Copyright (c) 2006-2008 Varnish Software AS
+* Copyright (c) 2006-2011 Varnish Software AS
More information about the varnish-commit
mailing list