@@ -52,6 +52,7 @@ struct config {
5252 int data_area ;
5353 char * cfg_file ;
5454 char * binary_file ;
55+ char * jq_filter ;
5556};
5657
5758static void cleanup_json_object (struct json_object * * jobj_ptr )
@@ -68,6 +69,7 @@ int solidigm_get_telemetry_log(int argc, char **argv, struct command *acmd, stru
6869 const char * dgen = "Pick which telemetry data area to report. Default is 3 to fetch areas 1-3. Valid options are 1, 2, 3, 4." ;
6970 const char * cfile = "JSON configuration file" ;
7071 const char * sfile = "binary file containing log dump" ;
72+ const char * jqfilt = "JSON config entry name containing jq filter" ;
7173 bool has_binary_file = false;
7274 _cleanup_nvme_global_ctx_ struct nvme_global_ctx * ctx = NULL ;
7375 _cleanup_nvme_transport_handle_ struct nvme_transport_handle * hdl = NULL ;
@@ -93,6 +95,7 @@ int solidigm_get_telemetry_log(int argc, char **argv, struct command *acmd, stru
9395 OPT_UINT ("data-area" , 'd' , & cfg .data_area , dgen ),
9496 OPT_FILE ("config-file" , 'j' , & cfg .cfg_file , cfile ),
9597 OPT_FILE ("source-file" , 's' , & cfg .binary_file , sfile ),
98+ OPT_STR ("jq-filter" , 'q' , & cfg .jq_filter , jqfilt ),
9699 OPT_INCR ("verbose" , 'v' , & nvme_cfg .verbose , verbose ),
97100 OPT_END ()
98101 };
@@ -144,19 +147,14 @@ int solidigm_get_telemetry_log(int argc, char **argv, struct command *acmd, stru
144147 nvme_show_status (err );
145148 return err ;
146149 }
147- struct json_tokener * jstok = json_tokener_new ();
148-
149- configuration = json_tokener_parse_ex (jstok , conf_str , length );
150- if (jstok -> err != json_tokener_success ) {
151- SOLIDIGM_LOG_WARNING ("Parsing error on JSON configuration file %s: %s (at offset %d)" ,
152- cfg .cfg_file ,
153- json_tokener_error_desc (jstok -> err ),
154- jstok -> char_offset );
155- json_tokener_free (jstok );
150+ configuration = json_tokener_parse (conf_str );
151+ if (!configuration ) {
152+ SOLIDIGM_LOG_WARNING (
153+ "Failed to parse JSON configuration file %s" ,
154+ cfg .cfg_file );
156155 err = EINVAL ;
157156 return err ;
158157 }
159- json_tokener_free (jstok );
160158 tl .configuration = configuration ;
161159 }
162160
@@ -196,7 +194,56 @@ int solidigm_get_telemetry_log(int argc, char **argv, struct command *acmd, stru
196194 tl .log = tlog ;
197195 solidigm_telemetry_log_data_areas_parse (& tl , cfg .data_area );
198196
199- json_print_object (tl .root , NULL );
197+ /* Check if jq filter is requested and available */
198+ if (cfg .jq_filter && configuration ) {
199+ struct json_object * jq_filter_obj = NULL ;
200+
201+ if (json_object_object_get_ex (configuration , cfg .jq_filter ,
202+ & jq_filter_obj )) {
203+ const char * jq_filter_str ;
204+
205+ jq_filter_str = json_object_get_string (jq_filter_obj );
206+ if (jq_filter_str ) {
207+ /* Get JSON string representation */
208+ const char * json_str ;
209+ char cmd [1024 ];
210+ FILE * jq_pipe ;
211+
212+ json_str = json_object_to_json_string (tl .root );
213+
214+ /* Create jq command and pipe JSON through it */
215+ snprintf (cmd , sizeof (cmd ), "jq -r '%s'" ,
216+ jq_filter_str );
217+ jq_pipe = popen (cmd , "w" );
218+ if (jq_pipe ) {
219+ fprintf (jq_pipe , "%s" , json_str );
220+ err = pclose (jq_pipe );
221+ if (err != 0 )
222+ err = - EINVAL ;
223+ } else {
224+ SOLIDIGM_LOG_WARNING (
225+ "Failed to execute jq command" );
226+ err = - ENOENT ;
227+ }
228+ } else {
229+ SOLIDIGM_LOG_WARNING (
230+ "jq filter entry '%s' is not a valid string" ,
231+ cfg .jq_filter );
232+ err = - EINVAL ;
233+ }
234+ } else {
235+ SOLIDIGM_LOG_WARNING (
236+ "jq filter entry '%s' not found in configuration file" ,
237+ cfg .jq_filter );
238+ err = - ENOENT ;
239+ }
240+ } else {
241+ /*
242+ * No jq filter requested or no config file,
243+ * use normal JSON output
244+ */
245+ json_print_object (tl .root , NULL );
246+ }
200247 printf ("\n" );
201248
202249 return err ;
0 commit comments