From: Sebastien Date: Sat, 30 Jun 2012 20:32:38 +0000 (+0200) Subject: Added option -[0-9]* to sar to show data of that days ago. X-Git-Tag: v10.1.1~10 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=00472302ed16b410703b3678281e5a4f3349d66c;p=sysstat Added option -[0-9]* to sar to show data of that days ago. Mail from Don 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. --- diff --git a/CHANGES b/CHANGES index e035280..685ed7d 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,7 @@ Changes: xxxx/xx/xx: Version 10.1.1 - Sebastien Godard (sysstat 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 orange.fr) * [Alain Chereau]: Options -g and -T added to iostat. These diff --git a/cifsiostat.c b/cifsiostat.c index 3cf8cc0..7e36920 100644 --- a/cifsiostat.c +++ b/cifsiostat.c @@ -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); diff --git a/common.c b/common.c index eb63389..ea8267d 100644 --- 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 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); } /* diff --git a/common.h b/common.h index 3ad5544..283cd3f 100644 --- 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 diff --git a/iostat.c b/iostat.c index 07cc8d6..61fb2a6 100644 --- 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); diff --git a/man/sar.in b/man/sar.in index 6e9bd22..47a3c15 100644 --- a/man/sar.in +++ b/man/sar.in @@ -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 diff --git a/mpstat.c b/mpstat.c index e54cbe5..322bb38 100644 --- 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); diff --git a/nfsiostat.c b/nfsiostat.c index bf0ba17..c6dca8f 100644 --- a/nfsiostat.c +++ b/nfsiostat.c @@ -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); diff --git a/pidstat.c b/pidstat.c index 1e03c7f..4fa1550 100644 --- 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 f4ebe6f..3bac346 100644 --- 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 *); diff --git a/sa_common.c b/sa_common.c index 4297087..925b93e 100644 --- a/sa_common.c +++ b/sa_common.c @@ -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 af42fba..f5fd382 100644 --- 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 d6a2b76..70b284b 100644 --- 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 d09f959..2a03aba 100644 --- 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 { [,...] | SUM | ALL | XALL } ] [ -P { [,...] | ALL } ]\n" "[ -m { [,...] | ALL } ] [ -n { [,...] | ALL } ]\n" - "[ -o [ ] | -f [ ] ]\n" + "[ -o [ ] | -f [ ] | -[0-9]+ ]\n" "[ -i ] [ -s [ ] ] [ -e [ ] ]\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();