From 0c110e90b6de47a380078dc566683d40bef0849e Mon Sep 17 00:00:00 2001 From: Fei Li Date: Wed, 11 Aug 2021 14:23:58 +0800 Subject: [PATCH] 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 ++++++++++++++ man/atoprc.5 | 6 ++++++ 3 files changed, 29 insertions(+) diff --git a/atop.c b/atop.c index 26675953..8edb3dbc 100644 --- a/atop.c +++ b/atop.c @@ -312,6 +312,7 @@ unsigned long sampcnt; char screen; int linelen = 80; int generations = 28; /* By default, keep recent 30 days' log */ +char *logpath = "/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; @@ -354,6 +355,7 @@ void do_flags(char *, char *); void do_interval(char *, char *); void do_linelength(char *, char *); void do_generations(char *, char *); +void do_logpath(char *, char *); void do_username(char *, char *); void do_procname(char *, char *); void do_maxcpu(char *, char *); @@ -406,6 +408,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, }, @@ -1164,6 +1167,12 @@ do_generations(char *name, char *val) generations = get_posval(name, val); } +void +do_logpath(char *name, char *val) +{ + logpath = "/var/log/atop"; +} + /* ** read RC-file and modify defaults accordingly */ diff --git a/atop.daily b/atop.daily index b59f36c7..e408fc55 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 ^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/man/atoprc.5 b/man/atoprc.5 index 39140bf1..ef22ffd6 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