From c7487300ab40f4923d432a8022a5c088df53efd7 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Fri, 12 Jan 2018 10:15:40 +0100 Subject: [PATCH] sar: Add new option -h Add option -h to make sar's output easier to read by a human. This option tells sar to display {devices, network interfaces} names at the end of the line to make sure the output won't shift. This option also enables implicitly --human and -p (pretty-print). Signed-off-by: Sebastien GODARD --- pr_stats.c | 30 ++++++++++++++++++++++++------ sa.h | 2 ++ sar.c | 11 ++++++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/pr_stats.c b/pr_stats.c index e1d668a..59aac5a 100644 --- a/pr_stats.c +++ b/pr_stats.c @@ -1041,7 +1041,7 @@ __print_funct_t print_disk_stats(struct activity *a, int prev, int curr, } if (dis) { - print_hdr_line(timestamp[!curr], a, FIRST, -1, 9); + print_hdr_line(timestamp[!curr], a, FIRST, DISPLAY_HUMAN_READ(flags) ? -1 : 0, 9); } for (i = 0; i < a->nr[curr]; i++) { @@ -1090,6 +1090,9 @@ __print_funct_t print_disk_stats(struct activity *a, int prev, int curr, printf("%-11s", timestamp[curr]); + if (!DISPLAY_HUMAN_READ(flags)) { + cprintf_in(IS_STR, " %9s", dev_name, 0); + } cprintf_f(NO_UNIT, 1, 9, 2, S_VALUE(sdp->nr_ios, sdc->nr_ios, itv)); cprintf_f(unit, 2, 9, 2, @@ -1104,7 +1107,10 @@ __print_funct_t print_disk_stats(struct activity *a, int prev, int curr, xds.svctm); cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, xds.util / 10.0); - cprintf_in(IS_STR, " %s\n", dev_name, 0); + if (DISPLAY_HUMAN_READ(flags)) { + cprintf_in(IS_STR, " %s", dev_name, 0); + } + printf("\n"); } } @@ -1135,7 +1141,7 @@ __print_funct_t print_net_dev_stats(struct activity *a, int prev, int curr, } if (dis) { - print_hdr_line(timestamp[!curr], a, FIRST, -1, 9); + print_hdr_line(timestamp[!curr], a, FIRST, DISPLAY_HUMAN_READ(flags) ? -1 : 0, 9); } for (i = 0; i < a->nr[curr]; i++) { @@ -1160,6 +1166,9 @@ __print_funct_t print_net_dev_stats(struct activity *a, int prev, int curr, printf("%-11s", timestamp[curr]); + if (!DISPLAY_HUMAN_READ(flags)) { + cprintf_in(IS_STR, " %9s", sndc->interface, 0); + } rxkb = S_VALUE(sndp->rx_bytes, sndc->rx_bytes, itv); txkb = S_VALUE(sndp->tx_bytes, sndc->tx_bytes, itv); @@ -1175,7 +1184,10 @@ __print_funct_t print_net_dev_stats(struct activity *a, int prev, int curr, S_VALUE(sndp->multicast, sndc->multicast, itv)); ifutil = compute_ifutil(sndc, rxkb, txkb); cprintf_pc(DISPLAY_UNIT(flags), 1, 9, 2, ifutil); - cprintf_in(IS_STR, " %s\n", sndc->interface, 0); + if (DISPLAY_HUMAN_READ(flags)) { + cprintf_in(IS_STR, " %s", sndc->interface, 0); + } + printf("\n"); } } @@ -1199,7 +1211,7 @@ __print_funct_t print_net_edev_stats(struct activity *a, int prev, int curr, memset(&snedzero, 0, STATS_NET_EDEV_SIZE); if (dis) { - print_hdr_line(timestamp[!curr], a, FIRST, -1, 9); + print_hdr_line(timestamp[!curr], a, FIRST, DISPLAY_HUMAN_READ(flags) ? -1 : 0, 9); } for (i = 0; i < a->nr[curr]; i++) { @@ -1224,6 +1236,9 @@ __print_funct_t print_net_edev_stats(struct activity *a, int prev, int curr, printf("%-11s", timestamp[curr]); + if (!DISPLAY_HUMAN_READ(flags)) { + cprintf_in(IS_STR, " %9s", snedc->interface, 0); + } cprintf_f(NO_UNIT, 9, 9, 2, S_VALUE(snedp->rx_errors, snedc->rx_errors, itv), S_VALUE(snedp->tx_errors, snedc->tx_errors, itv), @@ -1234,7 +1249,10 @@ __print_funct_t print_net_edev_stats(struct activity *a, int prev, int curr, S_VALUE(snedp->rx_frame_errors, snedc->rx_frame_errors, itv), S_VALUE(snedp->rx_fifo_errors, snedc->rx_fifo_errors, itv), S_VALUE(snedp->tx_fifo_errors, snedc->tx_fifo_errors, itv)); - cprintf_in(IS_STR, " %s\n", snedc->interface, 0); + if (DISPLAY_HUMAN_READ(flags)) { + cprintf_in(IS_STR, " %s", snedc->interface, 0); + } + printf("\n"); } } diff --git a/sa.h b/sa.h index 6b4c2ad..945f79f 100644 --- a/sa.h +++ b/sa.h @@ -107,6 +107,7 @@ #define S_F_SVG_HEIGHT 0x00200000 #define S_F_SVG_PACKED 0x00400000 #define S_F_SVG_SHOW_INFO 0x00800000 +#define S_F_HUMAN_READ 0x01000000 #define WANT_SINCE_BOOT(m) (((m) & S_F_SINCE_BOOT) == S_F_SINCE_BOOT) #define WANT_SA_ROTAT(m) (((m) & S_F_SA_ROTAT) == S_F_SA_ROTAT) @@ -133,6 +134,7 @@ #define DISPLAY_UNIT(m) (((m) & S_F_UNIT) == S_F_UNIT) #define SET_CANVAS_HEIGHT(m) (((m) & S_F_SVG_HEIGHT) == S_F_SVG_HEIGHT) #define PACK_VIEWS(m) (((m) & S_F_SVG_PACKED) == S_F_SVG_PACKED) +#define DISPLAY_HUMAN_READ(m) (((m) & S_F_HUMAN_READ) == S_F_HUMAN_READ) #define AO_F_NULL 0x00000000 diff --git a/sar.c b/sar.c index 4154d6e..1987e99 100644 --- a/sar.c +++ b/sar.c @@ -113,7 +113,7 @@ void usage(char *progname) { print_usage_title(stderr, progname); fprintf(stderr, _("Options are:\n" - "[ -A ] [ -B ] [ -b ] [ -C ] [ -D ] [ -d ] [ -F [ MOUNT ] ] [ -H ]\n" + "[ -A ] [ -B ] [ -b ] [ -C ] [ -D ] [ -d ] [ -F [ MOUNT ] ] [ -H ] [ -h ]\n" "[ -p ] [ -q ] [ -r [ ALL ] ] [ -S ] [ -t ] [ -u [ ALL ] ] [ -V ]\n" "[ -v ] [ -W ] [ -w ] [ -y ] [ --help ] [ --human ] [ --sadc ]\n" "[ -I { | SUM | ALL } ] [ -P { | ALL } ]\n" @@ -1236,6 +1236,15 @@ int main(int argc, char **argv) opt++; } + else if (!strcmp(argv[opt], "-h")) { + /* + * Make output easier to read by a human. + * Option -h implies --human and -p (pretty-print). + */ + flags |= S_F_HUMAN_READ + S_F_UNIT + S_F_DEV_PRETTY; + opt++; + } + else if (!strcmp(argv[opt], "-I")) { /* Parse -I option */ if (parse_sar_I_opt(argv, &opt, act)) { -- 2.40.0