Commit 64a8c2a2 authored by Lasse Karstensen's avatar Lasse Karstensen Committed by Tollef Fog Heen

varnishstat: Add json output and continous mode

parent e8fb459e
...@@ -84,6 +84,69 @@ do_xml(struct VSM_data *vd) ...@@ -84,6 +84,69 @@ do_xml(struct VSM_data *vd)
printf("</varnishstat>\n"); 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 { struct once_priv {
...@@ -169,7 +232,7 @@ usage(void) ...@@ -169,7 +232,7 @@ usage(void)
"[-1lV] [-f field_list] " "[-1lV] [-f field_list] "
VSC_n_USAGE " " VSC_n_USAGE " "
"[-w delay]\n"); "[-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", fprintf(stderr, FMT, "-f field_list",
"Comma separated list of fields to display. "); "Comma separated list of fields to display. ");
fprintf(stderr, FMT, "", fprintf(stderr, FMT, "",
...@@ -180,9 +243,11 @@ usage(void) ...@@ -180,9 +243,11 @@ usage(void)
"The varnishd instance to get logs from"); "The varnishd instance to get logs from");
fprintf(stderr, FMT, "-V", "Display the version number and exit"); fprintf(stderr, FMT, "-V", "Display the version number and exit");
fprintf(stderr, FMT, "-w delay", 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", 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 #undef FMT
exit(1); exit(1);
} }
...@@ -193,12 +258,12 @@ main(int argc, char * const *argv) ...@@ -193,12 +258,12 @@ main(int argc, char * const *argv)
int c; int c;
struct VSM_data *vd; struct VSM_data *vd;
const struct VSC_C_main *VSC_C_main; 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(); vd = VSM_New();
VSC_Setup(vd); 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) { switch (c) {
case '1': case '1':
once = 1; once = 1;
...@@ -212,11 +277,15 @@ main(int argc, char * const *argv) ...@@ -212,11 +277,15 @@ main(int argc, char * const *argv)
VCS_Message("varnishstat"); VCS_Message("varnishstat");
exit(0); exit(0);
case 'w': case 'w':
do_repeat = 1;
delay = atoi(optarg); delay = atoi(optarg);
break; break;
case 'x': case 'x':
xml = 1; xml = 1;
break; break;
case 'j':
json = 1;
break;
default: default:
if (VSC_Arg(vd, c, optarg) > 0) if (VSC_Arg(vd, c, optarg) > 0)
break; break;
...@@ -229,12 +298,27 @@ main(int argc, char * const *argv) ...@@ -229,12 +298,27 @@ main(int argc, char * const *argv)
VSC_C_main = VSC_Main(vd); VSC_C_main = VSC_Main(vd);
if (xml) if (!(xml || json || once)) {
do_xml(vd);
else if (once)
do_once(vd, VSC_C_main);
else
do_curses(vd, VSC_C_main, delay); 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); exit(0);
} }
...@@ -10,15 +10,16 @@ Varnish Cache statistics ...@@ -10,15 +10,16 @@ Varnish Cache statistics
:Author: Dag-Erling Smørgrav :Author: Dag-Erling Smørgrav
:Author: Per Buer :Author: Per Buer
:Date: 2010-06-1 :Author: Lasse Karstensen
:Version: 1.0 :Date: 2011-11-07
:Version: 1.1
:Manual section: 1 :Manual section: 1
SYNOPSIS 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 DESCRIPTION
=========== ===========
...@@ -27,7 +28,7 @@ The varnishstat utility displays statistics from a running varnishd(1) instance. ...@@ -27,7 +28,7 @@ The varnishstat utility displays statistics from a running varnishd(1) instance.
The following options are available: 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 -f A comma separated list of the fields to display. If it starts with '^' it is used as an exclusion
list. list.
...@@ -39,9 +40,11 @@ The following options are available: ...@@ -39,9 +40,11 @@ The following options are available:
-V Display the version number and exit. -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: The columns in the main display are, from left to right:
...@@ -65,6 +68,29 @@ When using the -x option, the output is:: ...@@ -65,6 +68,29 @@ When using the -x option, the output is::
<description>FIELD DESCRIPTION</description> <description>FIELD DESCRIPTION</description>
</stat> </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 SEE ALSO
======== ========
...@@ -91,4 +117,4 @@ This document is licensed under the same licence as Varnish ...@@ -91,4 +117,4 @@ This document is licensed under the same licence as Varnish
itself. See LICENCE for details. itself. See LICENCE for details.
* Copyright (c) 2006 Verdens Gang AS * Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2008 Varnish Software AS * Copyright (c) 2006-2011 Varnish Software AS
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