JSON (and color) support for iproute
Subsystems (3)
cover JSON (and color) support for iproute
From: Stephen Hemminger <[email protected]>
This set of patches adds JSON output to route printing. Tested for the simple cases, but there are many variations and there such as lw tunnels which have not be tested.
The color formatting may need some additional tweaks. It looks like for some tags the tag is also showing up in color. This should be fixed in print_color_string rather than having to do special case handling in so many places.
This patchset also changes the default JSON output to be compressed (since the purpose of JSON is to make output machine readable); but do optional pretty print formatting with -p flag.
Stephen Hemminger (4):
json: make pretty printing optional
man: add documentation for json and pretty flags
json: fix newline at end of array
iproute: implement JSON and color output
include/json_print.h | 2 + include/utils.h | 5 + ip/ip.c | 7 +- ip/iproute.c | 376 +++++++++++++++++++++++++++++++++++--------------- ip/iproute_lwtunnel.c | 129 ++++++++++------- lib/json_print.c | 5 +- lib/json_writer.c | 5 + man/man8/ip.8 | 18 ++- man/man8/tc.8 | 3 +- tc/tc.c | 3 + 10 files changed, 381 insertions(+), 172 deletions(-)
Signature · 1 line
2.15.1
PATCH 1/4 json: make pretty printing optional
From: Stephen Hemminger <[email protected]>
Since JSON is intended for programmatic consumption, it makes sense for the default output format to be concise as possible.
For programmer and other uses, it is helpful to keep the pretty whitespace format; therefore enable it with -p flag.
--- include/json_print.h | 2 ++ ip/ip.c | 3 +++ lib/json_print.c | 3 ++- tc/tc.c | 3 +++ 4 files changed, 10 insertions(+), 1 deletion(-)
include/json_print.h +2 -0
@@ -15,6 +15,8 @@#include "json_writer.h" #include "color.h" +extern int show_pretty; +json_writer_t *get_json_writer (void );/*
ip/ip.c +3 -0
@@ -31,6 +31,7 @@ int show_stats;int show_details;int oneline;int brief; +int show_pretty;int json;int timestamp;const char *_SL_;
@@ -259,6 +260,8 @@ int main(int argc, char **argv) ++brief; }else if (matches (opt,"-json" ) ==0 ) { ++json; + }else if (matches (opt,"-pretty" ) ==0 ) { + ++show_pretty; }else if (matches (opt,"-rcvbuf" ) ==0 ) {unsigned int size;
lib/json_print.c +2 -1
@@ -28,7 +28,8 @@ void new_json_obj(int json)perror ("json object" );exit (1 ); } -jsonw_pretty (_jw, true); +if (show_pretty) +jsonw_pretty (_jw, true);jsonw_start_array (_jw); } }
tc/tc.c +3 -0
@@ -42,6 +42,7 @@ int use_iec;int force;bool use_names;int json; +int pretty;static char *conf_file;
@@ -484,6 +485,8 @@ int main(int argc, char **argv) ++timestamp_short; }else if (matches (argv[1 ],"-json" ) ==0 ) { ++json; + }else if (matches (argv[1 ],"-pretty" ) ==0 ) { + ++pretty; }else {fprintf (stderr,"Option \"%s\" is unknown, try \"tc -help\".\n" , argv[1 ]);return -1 ;
Signature · 1 line
2.15.1
PATCH 2/4 man: add documentation for json and pretty flags
In reply to Stephen HemmingerFrom: Stephen Hemminger <[email protected]>
Add description for -json and -pretty options.
--- ip/ip.c | 4 ++-- man/man8/ip.8 | 18 ++++++++++++++---- man/man8/tc.8 | 3 ++- 3 files changed, 18 insertions(+), 7 deletions(-)
ip/ip.c +2 -2
@@ -54,12 +54,12 @@ static void usage(void)" netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila |\n" " vrf | sr }\n" " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n" -" -h[uman-readable] | -iec |\n" +" -h[uman-readable] | -iec | -j[son] | -p[retty] |\n" " -f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | link } |\n" " -4 | -6 | -I | -D | -B | -0 |\n" " -l[oops] { maximum-addr-flush-attempts } | -br[ief] |\n" " -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n" -" -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n"); +" -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n");exit (-1 ); }
man/man8/ip.8 +14 -4
@@ -48,9 +48,10 @@ ip \- show / manipulate routing, network devices, interfaces and tunnels \fB\-ts\fR[\fIhort\fR] | \fB\-n\fR[\fIetns\fR] name | \fB\-a\fR[\fIll\fR] | -\fB\-c\fR[\fIolor\fR] -\fB\-br\fR[\fIief\fR] } - +\fB\-c\fR[\fIolor\fR] | +\fB\-br\fR[\fIief\fR] | +\fB\-j\fR[son\fR] | +\fB\-p\fR[retty\fR] } .SH OPTIONS
@@ -208,10 +209,19 @@ Set the netlink socket receive buffer size, defaults to 1MB. print human readable rates in IEC units (e.g. 1Ki = 1024). .TP -.BR "\-br" , "\-brief" +.BR "\-br" , " \-brief" Print only basic information in a tabular format for better readability. This option is currently only supported by .BR "ip addr show " and " ip link show " commands. +.TP +.BR "\-j", " \-json" +Output results in JavaScript Object Notation (JSON). + +.TP +.BR "\-p", " \-pretty" +The default JSON format is compact and more efficient to parse but hard for most users to read. +This flag adds indentation for readability. + .SH IP - COMMAND SYNTAX .SS
man/man8/tc.8 +2 -1
@@ -670,7 +670,8 @@ output raw hex values for handles. .TP .BR "\-p", " \-pretty" -decode filter offset and mask values to equivalent filter commands based on TCP/IP. +for u32 filter, decode offset and mask values to equivalent filter commands based on TCP/IP. +In JSON output, add whitespace to improve readability. .TP .BR "\-iec"
Signature · 1 line
2.15.1
PATCH 3/4 json: fix newline at end of array
In reply to Stephen HemmingerFrom: Stephen Hemminger <[email protected]>
The json print library was toggling pretty print at the end of an array to workaround a bug in underlying json_writer. Instead, just fix json_writer to pretty print array correctly.
--- lib/json_print.c | 2 -- lib/json_writer.c | 5 +++++ 2 files changed, 5 insertions(+), 2 deletions(-)
lib/json_print.c +0 -2
@@ -89,9 +89,7 @@ void open_json_array(enum output_type type, const char *str)void close_json_array (enum output_type type,const char *str) {if (_IS_JSON_CONTEXT (type)) { -jsonw_pretty (_jw, false);jsonw_end_array (_jw); -jsonw_pretty (_jw, true); }else if (_IS_FP_CONTEXT (type)) {printf ("%s" , str); }
lib/json_writer.c +5 -0
@@ -180,10 +180,15 @@ void jsonw_end_object(json_writer_t *self)void jsonw_start_array (json_writer_t *self) {jsonw_begin (self,'[' ); +if (self->pretty) +putc (' ' , self->out); }void jsonw_end_array (json_writer_t *self) { +if (self->pretty && self->sep) +putc (' ' , self->out); + self->sep ='\0' ;jsonw_end (self,']' ); }
Signature · 1 line
2.15.1
PATCH 4/4 iproute: implement JSON and color output
In reply to Stephen HemmingerFrom: Stephen Hemminger <[email protected]>
Add JSON and color output formatting to ip route command. Similar to existing address and link output.
--- include/utils.h | 5 + ip/iproute.c | 376 +++++++++++++++++++++++++++++++++++--------------- ip/iproute_lwtunnel.c | 129 ++++++++++------- 3 files changed, 348 insertions(+), 162 deletions(-)
include/utils.h +5 -0
@@ -23,6 +23,7 @@ extern int resolve_hosts;extern int oneline;extern int brief;extern int json; +extern int pretty;extern int timestamp;extern int timestamp_short;extern const char * _SL_;
@@ -155,6 +156,10 @@ int af_byte_len(int af);const char *format_host_r (int af,int len,const void *addr,char *buf,int buflen); +#define format_host_rta_r (af, rta, buf, buflen) \ + format_host_r(af, RTA_PAYLOAD(rta), RTA_DATA(rta), \ + buf, buflen) +const char *format_host (int af,int lne,const void *addr);#define format_host_rta (af, rta) \ format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
ip/iproute.c +262 -114
593 lines not on this page · raw message
ip/iproute_lwtunnel.c +81 -48
248 lines not on this page · raw message
Signature · 1 line
2.15.1
Quoted 6 lines · Stephen Hemminger
From: Stephen Hemminger <[email protected]>
This set of patches adds JSON output to route printing. Tested for the simple cases, but there are many variations and there such as lw tunnels which have not be tested.
Quoted 8 lines
The color formatting may need some additional tweaks. It looks like for some tags the tag is also showing up in color. This should be fixed in
print_color_stringrather than having to do special case handling in so many places.This patchset also changes the default JSON output to be compressed (since the purpose of JSON is to make output machine readable); but do optional pretty print formatting with -p flag.
Looks good. Series applied to iproute2-next.