]> granicus.if.org Git - sysstat/commitdiff
sar: Add new option -h
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 12 Jan 2018 09:15:40 +0000 (10:15 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 12 Jan 2018 09:15:40 +0000 (10:15 +0100)
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 <sysstat@users.noreply.github.com>
pr_stats.c
sa.h
sar.c

index e1d668af2e50d15fc93fbc01adfe56202d4e477a..59aac5a81393fde7e8163b630e16e186ab21a1fe 100644 (file)
@@ -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 6b4c2add1ae21cc41647d7226dc3c013f46b9b73..945f79f1d9fc616cc01c7f09739d046123359fcb 100644 (file)
--- a/sa.h
+++ b/sa.h
 #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)
 #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 4154d6e8d9062f58b96762a8344eac2b5a7b944c..1987e99970119aa4d8d813d7b549f6f8f0754e61 100644 (file)
--- 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 { <int_list> | SUM | ALL } ] [ -P { <cpu_list> | 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)) {