Print this page
OS-1840 fmdump shall emit JSON
Reviewed by: Robert Mustacchi <rm@joyent.com>

@@ -20,10 +20,11 @@
  */
 
 /*
  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
+ * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  */
 
 #include <alloca.h>
 #include <unistd.h>
 #include <limits.h>

@@ -191,11 +192,12 @@
         "\t-f  follow growth of log file by waiting for additional data\n    "
     "Output options:\n"
         "\t-m  display human-readable messages (only for fault logs)\n"
         "\t-v  set verbose mode: display additional event detail\n"
         "\t-V  set very verbose mode: display complete event contents\n"
-        "\t-p  Used with -V: apply some output prettification\n    "
+        "\t-p  Used with -V: apply some output prettification\n"
+        "\t-j  Used with -V: emit JSON-formatted output\n    "
     "Selection filters:\n"
         "\t-c  select events that match the specified class\n"
         "\t-t  select events that occurred after the specified time\n"
         "\t-T  select events that occurred before the specified time\n"
         "\t-u  select events that match the specified diagnosis uuid\n"

@@ -1025,11 +1027,11 @@
 
 
 static int
 aggregate(char **ifiles, int n_ifiles, int opt_f,
     fmd_log_filter_t *fv, uint_t fc,
-    int opt_v, int opt_V, int opt_p)
+    int opt_v, int opt_V, int opt_p, int opt_j)
 {
         struct fmdump_pipeline *pipeline, *pl;
         struct fmdump_srlzer srlzer;
         uint32_t npipe;
         int fmt;

@@ -1072,11 +1074,12 @@
                         pipeline[npipe++].pl_logpath = logpath;
                 }
         }
 
         if (opt_V)
-                fmt = opt_p ? FMDUMP_PRETTY : FMDUMP_VERB2;
+                fmt = opt_p ? FMDUMP_PRETTY : opt_j ? FMDUMP_JSON :
+                    FMDUMP_VERB2;
         else if (opt_v)
                 fmt = FMDUMP_VERB1;
         else
                 fmt = FMDUMP_SHORT;
 

@@ -1150,11 +1153,11 @@
 
 int
 main(int argc, char *argv[])
 {
         int opt_a = 0, opt_e = 0, opt_f = 0, opt_H = 0, opt_m = 0, opt_p = 0;
-        int opt_u = 0, opt_v = 0, opt_V = 0;
+        int opt_u = 0, opt_v = 0, opt_V = 0, opt_j = 0;
         int opt_i = 0, opt_I = 0;
         int opt_A = 0;
         char **ifiles = NULL;
         char *ifile = NULL;
         int n_ifiles;

@@ -1185,11 +1188,11 @@
         fltfv = alloca(sizeof (fmd_log_filter_t) * argc);
         allfv = alloca(sizeof (fmd_log_filter_t) * argc);
 
         while (optind < argc) {
                 while ((c =
-                    getopt(argc, argv, "Aac:efHiImn:O:pR:t:T:u:vV")) != EOF) {
+                    getopt(argc, argv, "Aac:efHiIjmn:O:pR:t:T:u:vV")) != EOF) {
                         switch (c) {
                         case 'A':
                                 opt_A++;
                                 break;
                         case 'a':

@@ -1219,18 +1222,25 @@
                         case 'I':
                                 if (opt_e || opt_i)
                                         return (usage(stderr));
                                 opt_I++;
                                 break;
+                        case 'j':
+                                if (opt_p)
+                                        return (usage(stderr));
+                                opt_j++;
+                                break;
                         case 'm':
                                 opt_m++;
                                 break;
                         case 'O':
                                 off = strtoull(optarg, NULL, 16);
                                 iflags |= FMD_LOG_XITER_OFFS;
                                 break;
                         case 'p':
+                                if (opt_j)
+                                        return (usage(stderr));
                                 opt_p++;
                                 break;
                         case 'R':
                                 g_root = optarg;
                                 break;

@@ -1307,11 +1317,11 @@
                         allfv[allfc++] = fltfv[fltfc++];
                 }
 
                 rc = aggregate(ifiles, n_ifiles, opt_f,
                     allfv, allfc,
-                    opt_v, opt_V, opt_p);
+                    opt_v, opt_V, opt_p, opt_j);
 
                 cleanup(ifiles, n_ifiles);
                 return (rc);
         } else {
                 if (ifiles == NULL) {

@@ -1398,11 +1408,12 @@
                 allfv[allfc++] = fltfv[fltfc++];
         }
 
         if (opt_V) {
                 arg.da_fmt =
-                    &ops->do_formats[opt_p ? FMDUMP_PRETTY : FMDUMP_VERB2];
+                    &ops->do_formats[opt_p ? FMDUMP_PRETTY :
+                    opt_j ? FMDUMP_JSON : FMDUMP_VERB2];
                 iflags |= FMD_LOG_XITER_REFS;
         } else if (opt_v) {
                 arg.da_fmt = &ops->do_formats[FMDUMP_VERB1];
         } else if (opt_m) {
                 arg.da_fmt = &ops->do_formats[FMDUMP_MSG];