From 98378130a734ba1c5c19a64e249e508c1b55973e Mon Sep 17 00:00:00 2001 From: Fei Li Date: Thu, 12 Sep 2019 18:18:36 +0800 Subject: [PATCH 1/2] Support 'generations' flag to mark days of logs we want to keep The "generations" flag indicates days of logs we'd like to keep considering disk space usage. Users can define this flag in atoprc according to their own needs. Signed-off-by: Fei Li --- atop.c | 9 +++++++++ atop.daily | 9 +++++++++ man/atoprc.5 | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/atop.c b/atop.c index de880549..bd885309 100644 --- a/atop.c +++ b/atop.c @@ -157,6 +157,7 @@ unsigned long interval = 10; unsigned long sampcnt; char screen; int linelen = 80; +int generations = 28; /* By default, keep recent 30 days' log */ char acctreason; /* accounting not active (return val) */ char rawname[RAWNAMESZ]; char rawreadflag; @@ -200,6 +201,7 @@ static void readrc(char *, int); static void do_interval(char *, char *); static void do_linelength(char *, char *); +static void do_generations(char *, char *); static struct { char *tag; @@ -209,6 +211,7 @@ static struct { { "flags", do_flags, 0, }, { "interval", do_interval, 0, }, { "linelen", do_linelength, 0, }, + { "generations", do_generations, 0, }, { "username", do_username, 0, }, { "procname", do_procname, 0, }, { "maxlinecpu", do_maxcpu, 0, }, @@ -975,6 +978,12 @@ do_linelength(char *name, char *val) linelen = get_posval(name, val); } +static void +do_generations(char *name, char *val) +{ + generations = get_posval(name, val); +} + /* ** read RC-file and modify defaults accordingly */ diff --git a/atop.daily b/atop.daily index 55737fda..3625282f 100755 --- a/atop.daily +++ b/atop.daily @@ -51,6 +51,15 @@ 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 + # delete logfiles older than N days (configurable) # start a child shell that activates another child shell in # the background to avoid a zombie diff --git a/man/atoprc.5 b/man/atoprc.5 index cb9fab19..672c0989 100644 --- a/man/atoprc.5 +++ b/man/atoprc.5 @@ -43,6 +43,10 @@ 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 username The default regular expression for the users for which active processes will be shown. @@ -232,6 +236,8 @@ flags\ \ \ \ \ \ \ \ \ Aaf .br interval\ \ \ \ \ \ 5 .br +generations\ \ \ 3 +.br username .br procname From c30925ca1851f9726fc5c7b9f6c3f30057fb548e Mon Sep 17 00:00:00 2001 From: Fei Li Date: Wed, 11 Aug 2021 14:23:58 +0800 Subject: [PATCH 2/2] Support 'logpath' flag to change log path The "logpath" indicates log path atop writes. Users can define this flag in atoprc to change the logpath according to their own needs. This is meaningful especially to avoid the system disk being fully occupied. Signed-off-by: Fei Li --- atop.c | 9 +++++++++ atop.daily | 14 ++++++++++++++ atop.h | 1 + man/atoprc.5 | 6 ++++++ rawlog.c | 7 +++---- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/atop.c b/atop.c index bd885309..ec7795f4 100644 --- a/atop.c +++ b/atop.c @@ -158,6 +158,7 @@ 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; @@ -202,6 +203,7 @@ 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; @@ -212,6 +214,7 @@ static struct { { "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, }, @@ -984,6 +987,12 @@ 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 3625282f..dace91e0 100755 --- a/atop.daily +++ b/atop.daily @@ -60,11 +60,25 @@ if [ -f $ATOPRC ]; then 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 672c0989..68926eb9 100644 --- a/man/atoprc.5 +++ b/man/atoprc.5 @@ -47,6 +47,10 @@ The length of a screen line when sending output to a file or pipe (default 80). 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. @@ -238,6 +242,8 @@ 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);