diff --git a/atop.c b/atop.c index de880549..ec7795f4 100644 --- a/atop.c +++ b/atop.c @@ -157,6 +157,8 @@ unsigned long interval = 10; unsigned long sampcnt; char screen; int linelen = 80; +int generations = 28; /* By default, keep recent 30 days' log */ +char logpath[256] = "/var/log/atop"; /*support writing to other paths, to avoid system disk being fully occupied */ char acctreason; /* accounting not active (return val) */ char rawname[RAWNAMESZ]; char rawreadflag; @@ -200,6 +202,8 @@ static void readrc(char *, int); static void do_interval(char *, char *); static void do_linelength(char *, char *); +static void do_generations(char *, char *); +static void do_logpath(char *, char *); static struct { char *tag; @@ -209,6 +213,8 @@ static struct { { "flags", do_flags, 0, }, { "interval", do_interval, 0, }, { "linelen", do_linelength, 0, }, + { "generations", do_generations, 0, }, + { "logpath", do_logpath, 0, }, { "username", do_username, 0, }, { "procname", do_procname, 0, }, { "maxlinecpu", do_maxcpu, 0, }, @@ -975,6 +981,18 @@ do_linelength(char *name, char *val) linelen = get_posval(name, val); } +static void +do_generations(char *name, char *val) +{ + generations = get_posval(name, val); +} + +static void +do_logpath(char *name, char *val) +{ + strcpy(logpath, val); +} + /* ** read RC-file and modify defaults accordingly */ diff --git a/atop.daily b/atop.daily index 55737fda..dace91e0 100755 --- a/atop.daily +++ b/atop.daily @@ -51,11 +51,34 @@ then rm "$PIDFILE" fi +#e.g. generations 5 +ATOPRC="/etc/atoprc" +if [ -f $ATOPRC ]; then + RCGENERATIONS=`cat $ATOPRC | grep -w '^generations' -m 1 | awk '{print $2}'` + if [ -n "$RCGENERATIONS" ]; then + LOGGENERATIONS=$RCGENERATIONS + fi +fi + +#e.g. logpath /data00/log/atop +ATOPRC="/etc/atoprc" +if [ -f $ATOPRC ]; then + RCLOGPATH=`cat $ATOPRC | grep -w '^logpath' -m 1 | awk '{print $2}'` + if [ -n "$RCLOGPATH" ]; then + LOGPATH=$RCLOGPATH + mkdir -p $LOGPATH + fi +fi + # delete logfiles older than N days (configurable) # start a child shell that activates another child shell in # the background to avoid a zombie # ( (sleep 3; find "$LOGPATH" -name 'atop_*' -mtime +"$LOGGENERATIONS" -exec rm {} \;)& ) +# In case we change the logpath, ensure consistent log storage status +if [ "$LOGPATH" != "/var/log/atop" ];then + ( (sleep 3; find "/var/log/atop" -name 'atop_*' -mtime +"$LOGGENERATIONS" -exec rm {} \;)& ) +fi # activate atop with an interval of S seconds (configurable), # replacing the current shell diff --git a/atop.h b/atop.h index e8078b8c..4d5492a6 100644 --- a/atop.h +++ b/atop.h @@ -95,6 +95,7 @@ extern char rawreadflag; extern char rmspaces; extern time_t begintime, endtime, cursortime; // epoch or time in day extern char flaglist[]; +extern char logpath[]; extern struct visualize vis; extern int osrel; diff --git a/man/atoprc.5 b/man/atoprc.5 index cb9fab19..68926eb9 100644 --- a/man/atoprc.5 +++ b/man/atoprc.5 @@ -43,6 +43,14 @@ The default interval value in seconds. The length of a screen line when sending output to a file or pipe (default 80). .PP .TP 4 +.B generations +The number of day logs need to keep, considering disk space usage and other needs. +.PP +.TP 4 +.B logpath +The log path atop writes to. This is meaningful especially to avoid the root directory being fully occupied. +.PP +.TP 4 .B username The default regular expression for the users for which active processes will be shown. @@ -232,6 +240,10 @@ flags\ \ \ \ \ \ \ \ \ Aaf .br interval\ \ \ \ \ \ 5 .br +generations\ \ \ 3 +.br +logpath\ \ \ \ \ \ \ /data00/log/atop +.br username .br procname diff --git a/rawlog.c b/rawlog.c index fc9ff727..d8ec9cd8 100644 --- a/rawlog.c +++ b/rawlog.c @@ -53,7 +53,6 @@ #include "photosyst.h" #include "rawlog.h" -#define BASEPATH "/var/log/atop" #define BINPATH "/usr/bin/atop" static int getrawrec (int, struct rawrecord *, int); @@ -333,7 +332,7 @@ rawread(void) tp = localtime(&timenow); snprintf(rawname, RAWNAMESZ, "%s/atop_%04d%02d%02d", - BASEPATH, + logpath, tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday); @@ -355,7 +354,7 @@ rawread(void) strcpy(savedname, rawname); // no overflow (len=8) snprintf(rawname, RAWNAMESZ, "%s/atop_%s", - BASEPATH, + logpath, savedname); break; } @@ -387,7 +386,7 @@ rawread(void) tp = localtime(&timenow); snprintf(rawname, RAWNAMESZ, "%s/atop_%04d%02d%02d", - BASEPATH, + logpath, tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday);