]> granicus.if.org Git - sysstat/commitdiff
Added option -[0-9]* to sar to show data of that days ago.
authorSebastien <seb@kluane.home>
Sat, 30 Jun 2012 20:32:38 +0000 (22:32 +0200)
committerSebastien <seb@kluane.home>
Sun, 1 Jul 2012 13:56:39 +0000 (15:56 +0200)
Mail from Don <do1@yandex.ru> 22/06/2012:

Hello Sysstat author(s),

Please add option `-[0-9]+' like `-1', `-2', etc. to sar tool, to show
data of that days ago. That would be handy and useful for everybody.

Example implementation in bash:

function sar() {
        case "$1" in -[0-9]*) local OPT="-f /var/log/sa/sa`date +%d
--date=${1#-}' day ago'`"; shift;; esac
        command sar $OPT "$@"
}

Limitation of this implementation is that -1 option should be first, but
if you implement this type of options in main code you can avoid it and
also many people will benefit. It will easier than to rememebr proper
date and type `-f /var/log/sa/saXX'.

Best regards,
Don.

14 files changed:
CHANGES
cifsiostat.c
common.c
common.h
iostat.c
man/sar.in
mpstat.c
nfsiostat.c
pidstat.c
sa.h
sa_common.c
sadc.c
sadf.c
sar.c

diff --git a/CHANGES b/CHANGES
index e0352800a72da3530e00ca414d18d08cc928624c..685ed7df6332758a7e6138d81feb3e8a188c0d59 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,7 @@
 Changes:
 
 xxxx/xx/xx: Version 10.1.1 - Sebastien Godard (sysstat <at> orange.fr)
+       * Added option -[0-9]* to sar to show data of that days ago.
 
 2012/05/16: Version 10.0.5 - Sebastien Godard (sysstat <at> orange.fr)
        * [Alain Chereau]: Options -g and -T added to iostat. These
index 3cf8cc015a3fd4b7df49af3a3becd15bd1460724..7e369201ed7305011885b58d5efac251a161cca6 100644 (file)
@@ -541,7 +541,7 @@ void rw_io_stat_loop(long int count, struct tm *rectime)
                read_cifs_stat(curr);
 
                /* Get time */
-               get_localtime(rectime);
+               get_localtime(rectime, 0);
 
                /* Print results */
                write_stats(curr, rectime);
@@ -660,7 +660,7 @@ int main(int argc, char **argv)
        /* Init structures according to machine architecture */
        io_sys_init();
 
-       get_localtime(&rectime);
+       get_localtime(&rectime, 0);
 
        /* Get system name, release number and hostname */
        uname(&header);
index eb633895d8651e80829e87d6b9a89050bd2a03a5..ea8267d34c4bb4d88bfc987b5789e61e6bb622d0 100644 (file)
--- a/common.c
+++ b/common.c
@@ -63,6 +63,9 @@ void print_version(void)
  ***************************************************************************
  * Get local date and time.
  *
+ * IN:
+ * @d_off      Day offset (number of days to go back in the past).
+ * 
  * OUT:
  * @rectime    Current local date and time.
  *
@@ -70,12 +73,13 @@ void print_version(void)
  * Value of time in seconds since the Epoch.
  ***************************************************************************
  */
-time_t get_localtime(struct tm *rectime)
+time_t get_localtime(struct tm *rectime, int d_off)
 {
        time_t timer;
        struct tm *ltm;
 
        time(&timer);
+       timer -= SEC_PER_DAY * d_off;
        ltm = localtime(&timer);
 
        *rectime = *ltm;
@@ -86,6 +90,9 @@ time_t get_localtime(struct tm *rectime)
  ***************************************************************************
  * Get date and time expressed in UTC.
  *
+ * IN:
+ * @d_off      Day offset (number of days to go back in the past).
+ * 
  * OUT:
  * @rectime    Current date and time expressed in UTC.
  *
@@ -93,12 +100,13 @@ time_t get_localtime(struct tm *rectime)
  * Value of time in seconds since the Epoch.
  ***************************************************************************
  */
-time_t get_gmtime(struct tm *rectime)
+time_t get_gmtime(struct tm *rectime, int d_off)
 {
        time_t timer;
        struct tm *ltm;
 
        time(&timer);
+       timer -= SEC_PER_DAY * d_off;
        ltm = gmtime(&timer);
 
        *rectime = *ltm;
@@ -109,6 +117,9 @@ time_t get_gmtime(struct tm *rectime)
  ***************************************************************************
  * Get date and time and take into account <ENV_TIME_DEFTM> variable.
  *
+ * IN:
+ * @d_off      Day offset (number of days to go back in the past).
+ * 
  * OUT:
  * @rectime    Current date and time.
  *
@@ -116,7 +127,7 @@ time_t get_gmtime(struct tm *rectime)
  * Value of time in seconds since the Epoch.
  ***************************************************************************
  */
-time_t get_time(struct tm *rectime)
+time_t get_time(struct tm *rectime, int d_off)
 {
        static int utc = 0;
        char *e;
@@ -130,9 +141,9 @@ time_t get_time(struct tm *rectime)
        }
        
        if (utc == 2)
-               return get_gmtime(rectime);
+               return get_gmtime(rectime, d_off);
        else
-               return get_localtime(rectime);
+               return get_localtime(rectime, d_off);
 }
 
 /*
index 3ad5544fa66f3a5875bee73c7ba196ff0e265607..283cd3fb619028169287a5ea04a15b858aa21b56 100644 (file)
--- a/common.h
+++ b/common.h
@@ -25,6 +25,9 @@
 
 #define DISP_HDR       1
 
+/* Number of seconds per day */
+#define SEC_PER_DAY    3600 * 24
+
 /* Maximum number of CPUs */
 #ifdef __CPU_SETSIZE
 #define NR_CPUS                __CPU_SETSIZE
@@ -183,9 +186,9 @@ extern unsigned long long
 extern void
        get_kb_shift(void);
 extern time_t
-       get_localtime(struct tm *);
+       get_localtime(struct tm *, int);
 extern time_t
-       get_time(struct tm *);
+       get_time(struct tm *, int);
 unsigned long long
        get_per_cpu_interval(struct stats_cpu *, struct stats_cpu *);
 extern int
index 07cc8d61b5acdc72199be3604ff5affb616952b6..61fb2a6e5ed66b61e18e465885da3959a49f0a32 100644 (file)
--- a/iostat.c
+++ b/iostat.c
@@ -1245,7 +1245,7 @@ void rw_io_stat_loop(long int count, struct tm *rectime)
                }
 
                /* Get time */
-               get_localtime(rectime);
+               get_localtime(rectime, 0);
 
                /* Print results */
                write_stats(curr, rectime);
@@ -1511,7 +1511,7 @@ int main(int argc, char **argv)
                presave_device_list();
        }
 
-       get_localtime(&rectime);
+       get_localtime(&rectime, 0);
 
        /* Get system name, release number and hostname */
        uname(&header);
index 6e9bd220cfa48ef47386657d92fca9ed5705fdbc..47a3c15f80af7b5cce2c89bcb6d0f8bbec0ea4d4 100644 (file)
@@ -1,4 +1,4 @@
-.TH SAR 1 "MAY 2011" Linux "Linux User's Manual" -*- nroff -*-
+.TH SAR 1 "JUNE 2012" Linux "Linux User's Manual" -*- nroff -*-
 .SH NAME
 sar \- Collect, report, or save system activity information.
 .SH SYNOPSIS
@@ -20,7 +20,7 @@ sar \- Collect, report, or save system activity information.
 .I filename
 .B ] | -f [
 .I filename
-.B ] ] [ -s [
+.B ] | -[0-9]+ ] [ -s [
 .I hh:mm:ss
 .B ] ] [ -e [
 .I hh:mm:ss
@@ -69,6 +69,10 @@ command extracts and writes to standard output records previously
 saved in a file. This file can be either the one specified by the
 .B -f
 flag or, by default, the standard system activity daily data file.
+It is also possible to enter -1, -2 etc. as an argument to sar to
+display data
+of that days ago. For example, -1 will point at the standard system
+activity file of yesterday.
 
 Without the
 .B -P
index e54cbe5c9c07d8612a0d5be01a527f7dadcad35b..322bb389d9a20e59957bd9a965f393afe609933b 100644 (file)
--- a/mpstat.c
+++ b/mpstat.c
@@ -787,7 +787,7 @@ void rw_mpstat_loop(int dis_hdr, int rows)
                }
 
                /* Get time */
-               get_localtime(&(mp_tstamp[curr]));
+               get_localtime(&(mp_tstamp[curr]), 0);
 
                /* Read stats */
                if (cpu_nr > 1) {
@@ -1022,7 +1022,7 @@ int main(int argc, char **argv)
        }
 
        /* Get time */
-       get_localtime(&(mp_tstamp[0]));
+       get_localtime(&(mp_tstamp[0]), 0);
 
        /* Get system name, release number and hostname */
        uname(&header);
index bf0ba17e5bf7f99c51b5fa3620e224b0483977b3..c6dca8f25124ec177d41a3c6435142bccf9caadb 100644 (file)
@@ -608,7 +608,7 @@ void rw_io_stat_loop(long int count, struct tm *rectime)
                read_nfs_stat(curr);
 
                /* Get time */
-               get_localtime(rectime);
+               get_localtime(rectime, 0);
 
                /* Print results */
                write_stats(curr, rectime);
@@ -729,7 +729,7 @@ int main(int argc, char **argv)
        /* Init structures according to machine architecture */
        io_sys_init();
 
-       get_localtime(&rectime);
+       get_localtime(&rectime, 0);
 
        /* Get system name, release number and hostname */
        uname(&header);
index 1e03c7f9ca6b0e70e78bf6042fec343f844ac191..4fa1550d9d1e9198294a8130f9b70c578081ebc9 100644 (file)
--- a/pidstat.c
+++ b/pidstat.c
@@ -1906,7 +1906,7 @@ void rw_pidstat_loop(int dis_hdr, int rows)
 
        do {
                /* Get time */
-               get_localtime(&ps_tstamp[curr]);
+               get_localtime(&ps_tstamp[curr], 0);
 
                if (cpu_nr > 1) {
                        /*
@@ -2175,7 +2175,7 @@ int main(int argc, char **argv)
        }
 
        /* Get time */
-       get_localtime(&(ps_tstamp[0]));
+       get_localtime(&(ps_tstamp[0]), 0);
 
        /* Get system name, release number and hostname */
        uname(&header);
diff --git a/sa.h b/sa.h
index f4ebe6fda302d75e2dc354a5da19afe0a370d707..3bac346fefabdb676c0b0cb30a9744e330c4f1ac 100644 (file)
--- a/sa.h
+++ b/sa.h
@@ -841,7 +841,7 @@ extern void
 extern void
        set_bitmap(unsigned char [], unsigned char, unsigned int);
 extern void
-       set_default_file(struct tm *, char *);
+       set_default_file(struct tm *, char *, int);
 extern void
        set_hdr_rectime(unsigned int, struct tm *, struct file_header *);
 
index 4297087621dea9c1485dee4c1f30806a61985006..925b93ebd071ab0b93c1eab3ee58bfda83bc969f 100644 (file)
@@ -301,14 +301,17 @@ int parse_timestamp(char *argv[], int *opt, struct tstamp *tse,
  ***************************************************************************
  * Set current daily data file name.
  *
+ * IN:
+ * @d_off      Day offset (number of days to go back in the past).
+ * 
  * OUT:
  * @rectime    Current date and time.
  * @datafile   Name of daily data file.
  ***************************************************************************
  */
-void set_default_file(struct tm *rectime, char *datafile)
+void set_default_file(struct tm *rectime, char *datafile, int d_off)
 {
-       get_time(rectime);
+       get_time(rectime, d_off);
        snprintf(datafile, MAX_FILE_LEN,
                 "%s/sa%02d", SA_DIR, rectime->tm_mday);
        datafile[MAX_FILE_LEN - 1] = '\0';
@@ -369,7 +372,7 @@ void get_file_timestamp_struct(unsigned int flags, struct tm *rectime,
 
        if (PRINT_TRUE_TIME(flags)) {
                /* Get local time. This is just to fill HH:MM:SS fields */
-               get_time(rectime);
+               get_time(rectime, 0);
 
                rectime->tm_mday = file_hdr->sa_day;
                rectime->tm_mon  = file_hdr->sa_month;
diff --git a/sadc.c b/sadc.c
index af42fba9a105c5451380182659afcb8f187ba170..f5fd382a09c027e2abefab4f197910216615590c 100644 (file)
--- a/sadc.c
+++ b/sadc.c
@@ -454,7 +454,7 @@ void setup_file_hdr(int fd)
        memset(&file_hdr, 0, FILE_HEADER_SIZE);
 
        /* Then get current date */
-       file_hdr.sa_ust_time = get_time(&rectime);
+       file_hdr.sa_ust_time = get_time(&rectime, 0);
 
        /* OK, now fill the header */
        file_hdr.sa_nr_act      = get_activity_nr(act, AO_COLLECTED, COUNT_ACTIVITIES);
@@ -542,7 +542,7 @@ void write_special_record(int ofd, int rtype)
        record_hdr.record_type = rtype;
 
        /* Save time */
-       record_hdr.ust_time = get_time(&rectime);
+       record_hdr.ust_time = get_time(&rectime, 0);
 
        record_hdr.hour   = rectime.tm_hour;
        record_hdr.minute = rectime.tm_min;
@@ -726,7 +726,7 @@ void open_ofile(int *ofd, char ofile[])
                         * as "-" on the command line) and it is from a past month,
                         * then overwrite (truncate) it.
                         */
-                       get_time(&rectime);
+                       get_time(&rectime, 0);
                        
                        if (((file_hdr.sa_month != rectime.tm_mon) ||
                            (file_hdr.sa_year != rectime.tm_year)) &&
@@ -882,7 +882,7 @@ void rw_sa_stat_loop(long count, struct tm *rectime, int stdfd, int ofd,
                reset_stats();
 
                /* Save time */
-               record_hdr.ust_time = get_time(rectime);
+               record_hdr.ust_time = get_time(rectime, 0);
                record_hdr.hour     = rectime->tm_hour;
                record_hdr.minute   = rectime->tm_min;
                record_hdr.second   = rectime->tm_sec;
@@ -963,7 +963,7 @@ void rw_sa_stat_loop(long count, struct tm *rectime, int stdfd, int ofd,
                /* Rotate activity file if necessary */
                if (WANT_SA_ROTAT(flags)) {
                        /* The user specified '-' as the filename to use */
-                       set_default_file(rectime, new_ofile);
+                       set_default_file(rectime, new_ofile, 0);
 
                        if (strcmp(ofile, new_ofile)) {
                                do_sa_rotat = TRUE;
@@ -1057,7 +1057,7 @@ int main(int argc, char **argv)
                                stdfd = -1;     /* Don't write to STDOUT */
                                if (!strcmp(argv[opt], "-")) {
                                        /* File name set to '-' */
-                                       set_default_file(&rectime, ofile);
+                                       set_default_file(&rectime, ofile, 0);
                                        flags |= S_F_SA_ROTAT;
                                }
                                else if (!strncmp(argv[opt], "-", 1)) {
diff --git a/sadf.c b/sadf.c
index d6a2b76fc0fcb7d5170e52569f6dbced50982da1..70b284b6c65d5057de9f842ada166adbdde79dbc 100644 (file)
--- a/sadf.c
+++ b/sadf.c
@@ -1500,7 +1500,7 @@ int main(int argc, char **argv)
                        if (!dfile[0]) {
                                if (!strcmp(argv[opt], "-")) {
                                        /* File name set to '-' */
-                                       set_default_file(&rectime, dfile);
+                                       set_default_file(&rectime, dfile, 0);
                                        opt++;
                                }
                                else if (!strncmp(argv[opt], "-", 1)) {
@@ -1551,7 +1551,7 @@ int main(int argc, char **argv)
 
        /* sadf reads current daily data file by default */
        if (!dfile[0]) {
-               set_default_file(&rectime, dfile);
+               set_default_file(&rectime, dfile, 0);
        }
 
        if (tm_start.use && tm_end.use && (tm_end.tm_hour < tm_start.tm_hour)) {
diff --git a/sar.c b/sar.c
index d09f95911c1116f66e3456ce247de1e6806a7b35..2a03abad04abb2fa50f561898df2f16a30a1579f 100644 (file)
--- a/sar.c
+++ b/sar.c
@@ -107,7 +107,7 @@ void usage(char *progname)
                          "[ -R ] [ -S ] [ -t ] [ -u [ ALL ] ] [ -v ] [ -V ] [ -w ] [ -W ] [ -y ]\n"
                          "[ -I { <int> [,...] | SUM | ALL | XALL } ] [ -P { <cpu> [,...] | ALL } ]\n"
                          "[ -m { <keyword> [,...] | ALL } ] [ -n { <keyword> [,...] | ALL } ]\n"
-                         "[ -o [ <filename> ] | -f [ <filename> ] ]\n"
+                         "[ -o [ <filename> ] | -f [ <filename> ] | -[0-9]+ ]\n"
                          "[ -i <interval> ] [ -s [ <hh:mm:ss> ] ] [ -e [ <hh:mm:ss> ] ]\n"));
        exit(1);
 }
@@ -1083,6 +1083,7 @@ int main(int argc, char **argv)
 {
        int i, opt = 1, args_idx = 2;
        int fd[2];
+       int day_offset = 0;
        char from_file[MAX_FILE_LEN], to_file[MAX_FILE_LEN];
        char ltemp[20];
 
@@ -1148,7 +1149,7 @@ int main(int argc, char **argv)
                                from_file[MAX_FILE_LEN - 1] = '\0';
                        }
                        else {
-                               set_default_file(&rectime, from_file);
+                               set_default_file(&rectime, from_file, day_offset);
                        }
                }
 
@@ -1205,6 +1206,13 @@ int main(int argc, char **argv)
                                usage(argv[0]);
                        }
                }
+               
+               else if ((strlen(argv[opt]) > 1) &&
+                        (strlen(argv[opt]) < 4) &&
+                        !strncmp(argv[opt], "-", 1) &&
+                        (strspn(argv[opt] + 1, DIGITS) == (strlen(argv[opt]) - 1))) {
+                       day_offset = atoi(argv[opt++] + 1);
+               }
 
                else if (!strncmp(argv[opt], "-", 1)) {
                        /* Other options not previously tested */
@@ -1245,7 +1253,7 @@ int main(int argc, char **argv)
        /* 'sar' is equivalent to 'sar -f' */
        if ((argc == 1) ||
            ((interval < 0) && !from_file[0] && !to_file[0])) {
-               set_default_file(&rectime, from_file);
+               set_default_file(&rectime, from_file, day_offset);
        }
 
        if (tm_start.use && tm_end.use && (tm_end.tm_hour < tm_start.tm_hour)) {
@@ -1270,6 +1278,11 @@ int main(int argc, char **argv)
        if (!interval && (from_file[0] || to_file[0])) {
                usage(argv[0]);
        }
+       
+       /* Cannot enter a day shift with -o option */
+       if (to_file[0] && day_offset) {
+               usage(argv[0]);
+       }
 
        if (USE_PRETTY_OPTION(flags)) {
                dm_major = get_devmap_major();