Skip to content

Commit

Permalink
Support 'logpath' flag to change log path
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ShirleyFei committed Mar 7, 2023
1 parent 9837813 commit c30925c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
9 changes: 9 additions & 0 deletions atop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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, },
Expand Down Expand Up @@ -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
*/
Expand Down
14 changes: 14 additions & 0 deletions atop.daily
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions atop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions man/atoprc.5
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -238,6 +242,8 @@ interval\ \ \ \ \ \ 5
.br
generations\ \ \ 3
.br
logpath\ \ \ \ \ \ \ /data00/log/atop
.br
username
.br
procname
Expand Down
7 changes: 3 additions & 4 deletions rawlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -355,7 +354,7 @@ rawread(void)
strcpy(savedname, rawname); // no overflow (len=8)

snprintf(rawname, RAWNAMESZ, "%s/atop_%s",
BASEPATH,
logpath,
savedname);
break;
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c30925c

Please sign in to comment.