From patchwork Mon Dec 5 18:35:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Austin S. Hemmelgarn" X-Patchwork-Id: 9461325 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 880FD6071F for ; Mon, 5 Dec 2016 18:35:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 74F7227F3E for ; Mon, 5 Dec 2016 18:35:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 67CC627F7F; Mon, 5 Dec 2016 18:35:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5D60F27E63 for ; Mon, 5 Dec 2016 18:35:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752102AbcLESfk (ORCPT ); Mon, 5 Dec 2016 13:35:40 -0500 Received: from mail-oi0-f68.google.com ([209.85.218.68]:35443 "EHLO mail-oi0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751646AbcLESfj (ORCPT ); Mon, 5 Dec 2016 13:35:39 -0500 Received: by mail-oi0-f68.google.com with SMTP id v84so37699992oie.2 for ; Mon, 05 Dec 2016 10:35:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=QQoPhXzRPcYhqXPBPt8QQKvfvL9LhZUV4U5wUPCTrCY=; b=qngv8uC+V74gmbcTY6NrMk+7Sar5ndn/cOTd8UPNC1+U4gvkc3zztE0YgrsRcs0p/f /aEBhnOl6+u5utqU6HFSSqDd8Q1fh+HAPnF5vRt/wfYqZc4gqb4oiEXY09rGQpsjSogp actoRbnEG2ck7ZvPzTMoqhr6e2ljV3m+GYQY5dUMt/RmKvGvj+m1z0nyABGR9JEZ9+in WDYQpcIX2oEZMpeBWb6vK5tZVHnlvoBGqaeBpbgQdG3mXyZkHlaVvsW1/IpGrGoJfqar irsYIQEr/s7WNkgfV48KxMapKesqQcddS0dUzezyFiO/uz7WtzKG/3n+srFO+Chmgkw4 8SYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=QQoPhXzRPcYhqXPBPt8QQKvfvL9LhZUV4U5wUPCTrCY=; b=PNQtMXgiiD5/XOowv86VTncz4GcCXo/jE7T6Ego6MX6X8uJ42mIxTqFPtbU6CUt8gN uJz8rzySqIY8ggJQdFbjpPh/PZxj1Dv7KNDNfoXmlbVtBK6o9QwiFZQYZsFex+wGCgSs COYWOGPncGX3AUQHvYpeb6VUo6FZRyGeLfzx2XVKplV8Zuf6+ZYQCVEQ0P5w7aOPR8WV rT70IXbukM2ozcNLWSzmklFvMRmSKmddAlcdZUjIW8wxHml+GpEkHUphLJ+cgeiXeSb/ DCNPWJWQZnLeTWnNMOhJZ4loTIvGoAj4AkhZUyTKjj7jO2bPuHlvs8y52gwvTqh5j0tt jDGQ== X-Gm-Message-State: AKaTC0157nZTjjMEArK9vA/u01DMj4MucrHakcOEGydt6bjeT6puoSmbjePi3x4vfLNlyg== X-Received: by 10.36.34.196 with SMTP id o187mr9672662ito.3.1480962938807; Mon, 05 Dec 2016 10:35:38 -0800 (PST) Received: from wild-karde.localdomain (rrcs-70-62-41-24.central.biz.rr.com. [70.62.41.24]) by smtp.googlemail.com with ESMTPSA id e35sm7581125iod.32.2016.12.05.10.35.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Dec 2016 10:35:38 -0800 (PST) Received: by wild-karde.localdomain (Postfix, from userid 1000) id E6FC41E1D6CB; Mon, 5 Dec 2016 13:35:35 -0500 (EST) From: "Austin S. Hemmelgarn" To: dsterba@suse.cz, linux-btrfs@vger.kernel.org Cc: "Austin S. Hemmelgarn" Subject: [RESEND][PATCH v2] btrfs-progs: add dev stats returncode option Date: Mon, 5 Dec 2016 13:35:20 -0500 Message-Id: <20161205183520.67039-1-ahferroin7@gmail.com> X-Mailer: git-send-email 2.11.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently, `btrfs device stats` returns non-zero only when there was an error getting the counter values. This is fine for when it gets run by a user directly, but is a serious pain when trying to use it in a script or for monitoring since you need to parse the (not at all machine friendly) output to check the counter values. This patch adds an option ('-s') which causes `btrfs device stats` to set bit 6 in the return code if any of the counters are non-zero. This greatly simplifies checking from a script or monitoring software if any errors have been recorded. In the event that this switch is passed and an error occurs reading the stats, the return code will have bit 0 set (so if there are errors reading counters, and the counters which were read were non-zero, the return value will be 65). Signed-off-by: Austin S. Hemmelgarn --- Changes since v1: * Switched to using bit 6 instead of bit 7 so we don't stomp on Bash's manipulation of return codes. Thanks to Mike Fleetwood for reminding me about this. Apparently this didn't make it to the ML last time, so trying again. Sorry if you got this twice David. Documentation/btrfs-device.asciidoc | 8 +++++++- cmds-device.c | 39 ++++++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Documentation/btrfs-device.asciidoc b/Documentation/btrfs-device.asciidoc index 239c99b..d398b6d 100644 --- a/Documentation/btrfs-device.asciidoc +++ b/Documentation/btrfs-device.asciidoc @@ -98,7 +98,7 @@ remain as such. Reloading the kernel module will drop this information. There's an alternative way of mounting multiple-device filesystem without the need for prior scanning. See the mount option 'device'. -*stats* [-z] |:: +*stats* [-zs] |:: Read and print the device IO error statistics for all devices of the given filesystem identified by or for a single . See section *DEVICE STATS* for more information. @@ -108,6 +108,9 @@ STATS* for more information. -z:::: Print the stats and reset the values to zero afterwards. +-s:::: +Set bit 6 of the return-code if any error statistics are non-zero. + *usage* [options] [...]:: Show detailed information about internal allocations in devices. + @@ -231,6 +234,9 @@ EXIT STATUS *btrfs device* returns a zero exit status if it succeeds. Non zero is returned in case of failure. +If the '-s' option is used, *btrfs device stats* will add 64 to the +exit status if any of the error counters is non-zero. + AVAILABILITY ------------ *btrfs* is part of btrfs-progs. diff --git a/cmds-device.c b/cmds-device.c index fa0830f..392e37c 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -376,6 +376,7 @@ static const char * const cmd_device_stats_usage[] = { "Show current device IO stats.", "", "-z show current stats and reset values to zero", + "-s return non-zero if any stat counter is not zero", NULL }; @@ -389,14 +390,18 @@ static int cmd_device_stats(int argc, char **argv) int i; int c; int err = 0; + int status = 0; __u64 flags = 0; DIR *dirstream = NULL; - while ((c = getopt(argc, argv, "z")) != -1) { + while ((c = getopt(argc, argv, "zs")) != -1) { switch (c) { case 'z': flags = BTRFS_DEV_STATS_RESET; break; + case 's': + status = 1; + break; case '?': default: usage(cmd_device_stats_usage); @@ -440,7 +445,7 @@ static int cmd_device_stats(int argc, char **argv) if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) { error("DEV_STATS ioctl failed on %s: %s", path, strerror(errno)); - err = 1; + err |= 1; } else { char *canonical_path; @@ -457,31 +462,51 @@ static int cmd_device_stats(int argc, char **argv) "devid:%llu", args.devid); } - if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1) + if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1) { printf("[%s].write_io_errs %llu\n", canonical_path, (unsigned long long) args.values[ BTRFS_DEV_STAT_WRITE_ERRS]); - if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1) + if ((status == 1) && (args.values[BTRFS_DEV_STAT_WRITE_ERRS] > 0)) { + err |= 64; + } + } + if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1) { printf("[%s].read_io_errs %llu\n", canonical_path, (unsigned long long) args.values[ BTRFS_DEV_STAT_READ_ERRS]); - if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1) + if ((status == 1) && (args.values[BTRFS_DEV_STAT_READ_ERRS] > 0)) { + err |= 64; + } + } + if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1) { printf("[%s].flush_io_errs %llu\n", canonical_path, (unsigned long long) args.values[ BTRFS_DEV_STAT_FLUSH_ERRS]); - if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1) + if ((status == 1) && (args.values[BTRFS_DEV_STAT_FLUSH_ERRS] > 0)) { + err |= 64; + } + } + if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1) { printf("[%s].corruption_errs %llu\n", canonical_path, (unsigned long long) args.values[ BTRFS_DEV_STAT_CORRUPTION_ERRS]); - if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1) + if ((status == 1) && (args.values[BTRFS_DEV_STAT_CORRUPTION_ERRS] > 0)) { + err |= 64; + } + } + if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1) { printf("[%s].generation_errs %llu\n", canonical_path, (unsigned long long) args.values[ BTRFS_DEV_STAT_GENERATION_ERRS]); + if ((status == 1) && (args.values[BTRFS_DEV_STAT_GENERATION_ERRS] > 0)) { + err |= 64; + } + } free(canonical_path); }