All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: Support json format in device stats
@ 2020-09-17  7:29 Sidong Yang
  2020-10-02 16:21 ` David Sterba
  0 siblings, 1 reply; 3+ messages in thread
From: Sidong Yang @ 2020-09-17  7:29 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Sidong Yang

This patch supports the feature that printing device stats in json
format. So textcollector like prometheus can parse the output easier.
This patch implements option for json format and print in json when
the option enabled.

This patch implements enhancement for this issue.
https://github.com/kdave/btrfs-progs/issues/291

Signed-off-by: Sidong Yang <realwakka@gmail.com>
---
 cmds/device.c | 46 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 12 deletions(-)

diff --git a/cmds/device.c b/cmds/device.c
index d72881f8..3e4bf837 100644
--- a/cmds/device.c
+++ b/cmds/device.c
@@ -467,6 +467,7 @@ static const char * const cmd_device_stats_usage[] = {
 	"",
 	"-c|--check             return non-zero if any stat counter is not zero",
 	"-z|--reset             show current stats and reset values to zero",
+	"-j|--json              show stats in json format",
 	NULL
 };
 
@@ -482,6 +483,7 @@ static int cmd_device_stats(const struct cmd_struct *cmd, int argc, char **argv)
 	int check = 0;
 	__u64 flags = 0;
 	DIR *dirstream = NULL;
+	bool json = 0;
 
 	optind = 0;
 	while (1) {
@@ -489,6 +491,7 @@ static int cmd_device_stats(const struct cmd_struct *cmd, int argc, char **argv)
 		static const struct option long_options[] = {
 			{"check", no_argument, NULL, 'c'},
 			{"reset", no_argument, NULL, 'z'},
+			{"json", no_argument, NULL, 'j'},
 			{NULL, 0, NULL, 0}
 		};
 
@@ -503,6 +506,9 @@ static int cmd_device_stats(const struct cmd_struct *cmd, int argc, char **argv)
 		case 'z':
 			flags = BTRFS_DEV_STATS_RESET;
 			break;
+		case 'j':
+			json = 1;
+			break;
 		default:
 			usage_unknown_option(cmd, argv);
 		}
@@ -574,18 +580,34 @@ static int cmd_device_stats(const struct cmd_struct *cmd, int argc, char **argv)
 				snprintf(canonical_path, 32,
 					 "devid:%llu", args.devid);
 			}
-
-			for (j = 0; j < ARRAY_SIZE(dev_stats); j++) {
-				/* We got fewer items than we know */
-				if (args.nr_items < dev_stats[j].num + 1)
-					continue;
-				printf("[%s].%-16s %llu\n", canonical_path,
-					dev_stats[j].name,
-					(unsigned long long)
-					 args.values[dev_stats[j].num]);
-				if ((check == 1)
-				    && (args.values[dev_stats[j].num] > 0))
-					err |= 64;
+			if (json) {
+				printf("{\n\t\"device\" : \"%s\",\n", canonical_path);
+				for (j = 0; j < ARRAY_SIZE(dev_stats); j++) {
+					/* We got fewer items than we know */
+					if (args.nr_items < dev_stats[j].num + 1)
+						continue;
+					printf("\t\"%s\" : %llu,\n",
+						dev_stats[j].name,
+						(unsigned long long)
+						 args.values[dev_stats[j].num]);
+					if ((check == 1)
+					    && (args.values[dev_stats[j].num] > 0))
+						err |= 64;
+				}
+				printf("}\n");
+			} else {
+				for (j = 0; j < ARRAY_SIZE(dev_stats); j++) {
+					/* We got fewer items than we know */
+					if (args.nr_items < dev_stats[j].num + 1)
+						continue;
+					printf("[%s].%-16s %llu\n", canonical_path,
+						dev_stats[j].name,
+						(unsigned long long)
+						 args.values[dev_stats[j].num]);
+					if ((check == 1)
+					    && (args.values[dev_stats[j].num] > 0))
+						err |= 64;
+				}
 			}
 
 			free(canonical_path);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-10-02 17:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-17  7:29 [PATCH] btrfs-progs: Support json format in device stats Sidong Yang
2020-10-02 16:21 ` David Sterba
2020-10-02 17:53   ` Sidong Yang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.