All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sidong Yang <realwakka@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Sidong Yang <realwakka@gmail.com>
Subject: [PATCH] btrfs-progs: Support json format in device stats
Date: Thu, 17 Sep 2020 07:29:47 +0000	[thread overview]
Message-ID: <20200917072948.3354-1-realwakka@gmail.com> (raw)

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


             reply	other threads:[~2020-09-17  7:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-17  7:29 Sidong Yang [this message]
2020-10-02 16:21 ` [PATCH] btrfs-progs: Support json format in device stats David Sterba
2020-10-02 17:53   ` Sidong Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200917072948.3354-1-realwakka@gmail.com \
    --to=realwakka@gmail.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.