Skip to content

Commit c30925c

Browse files
committed
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 <[email protected]>
1 parent 9837813 commit c30925c

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

atop.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ unsigned long sampcnt;
158158
char screen;
159159
int linelen = 80;
160160
int generations = 28; /* By default, keep recent 30 days' log */
161+
char logpath[256] = "/var/log/atop"; /*support writing to other paths, to avoid system disk being fully occupied */
161162
char acctreason; /* accounting not active (return val) */
162163
char rawname[RAWNAMESZ];
163164
char rawreadflag;
@@ -202,6 +203,7 @@ static void readrc(char *, int);
202203
static void do_interval(char *, char *);
203204
static void do_linelength(char *, char *);
204205
static void do_generations(char *, char *);
206+
static void do_logpath(char *, char *);
205207

206208
static struct {
207209
char *tag;
@@ -212,6 +214,7 @@ static struct {
212214
{ "interval", do_interval, 0, },
213215
{ "linelen", do_linelength, 0, },
214216
{ "generations", do_generations, 0, },
217+
{ "logpath", do_logpath, 0, },
215218
{ "username", do_username, 0, },
216219
{ "procname", do_procname, 0, },
217220
{ "maxlinecpu", do_maxcpu, 0, },
@@ -984,6 +987,12 @@ do_generations(char *name, char *val)
984987
generations = get_posval(name, val);
985988
}
986989

990+
static void
991+
do_logpath(char *name, char *val)
992+
{
993+
strcpy(logpath, val);
994+
}
995+
987996
/*
988997
** read RC-file and modify defaults accordingly
989998
*/

atop.daily

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,25 @@ if [ -f $ATOPRC ]; then
6060
fi
6161
fi
6262

63+
#e.g. logpath /data00/log/atop
64+
ATOPRC="/etc/atoprc"
65+
if [ -f $ATOPRC ]; then
66+
RCLOGPATH=`cat $ATOPRC | grep -w '^logpath' -m 1 | awk '{print $2}'`
67+
if [ -n "$RCLOGPATH" ]; then
68+
LOGPATH=$RCLOGPATH
69+
mkdir -p $LOGPATH
70+
fi
71+
fi
72+
6373
# delete logfiles older than N days (configurable)
6474
# start a child shell that activates another child shell in
6575
# the background to avoid a zombie
6676
#
6777
( (sleep 3; find "$LOGPATH" -name 'atop_*' -mtime +"$LOGGENERATIONS" -exec rm {} \;)& )
78+
# In case we change the logpath, ensure consistent log storage status
79+
if [ "$LOGPATH" != "/var/log/atop" ];then
80+
( (sleep 3; find "/var/log/atop" -name 'atop_*' -mtime +"$LOGGENERATIONS" -exec rm {} \;)& )
81+
fi
6882

6983
# activate atop with an interval of S seconds (configurable),
7084
# replacing the current shell

atop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ extern char rawreadflag;
9595
extern char rmspaces;
9696
extern time_t begintime, endtime, cursortime; // epoch or time in day
9797
extern char flaglist[];
98+
extern char logpath[];
9899
extern struct visualize vis;
99100

100101
extern int osrel;

man/atoprc.5

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ The length of a screen line when sending output to a file or pipe (default 80).
4747
The number of day logs need to keep, considering disk space usage and other needs.
4848
.PP
4949
.TP 4
50+
.B logpath
51+
The log path atop writes to. This is meaningful especially to avoid the root directory being fully occupied.
52+
.PP
53+
.TP 4
5054
.B username
5155
The default regular expression for the users for which active
5256
processes will be shown.
@@ -238,6 +242,8 @@ interval\ \ \ \ \ \ 5
238242
.br
239243
generations\ \ \ 3
240244
.br
245+
logpath\ \ \ \ \ \ \ /data00/log/atop
246+
.br
241247
username
242248
.br
243249
procname

rawlog.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
#include "photosyst.h"
5454
#include "rawlog.h"
5555

56-
#define BASEPATH "/var/log/atop"
5756
#define BINPATH "/usr/bin/atop"
5857

5958
static int getrawrec (int, struct rawrecord *, int);
@@ -333,7 +332,7 @@ rawread(void)
333332
tp = localtime(&timenow);
334333

335334
snprintf(rawname, RAWNAMESZ, "%s/atop_%04d%02d%02d",
336-
BASEPATH,
335+
logpath,
337336
tp->tm_year+1900,
338337
tp->tm_mon+1,
339338
tp->tm_mday);
@@ -355,7 +354,7 @@ rawread(void)
355354
strcpy(savedname, rawname); // no overflow (len=8)
356355

357356
snprintf(rawname, RAWNAMESZ, "%s/atop_%s",
358-
BASEPATH,
357+
logpath,
359358
savedname);
360359
break;
361360
}
@@ -387,7 +386,7 @@ rawread(void)
387386
tp = localtime(&timenow);
388387

389388
snprintf(rawname, RAWNAMESZ, "%s/atop_%04d%02d%02d",
390-
BASEPATH,
389+
logpath,
391390
tp->tm_year+1900,
392391
tp->tm_mon+1,
393392
tp->tm_mday);

0 commit comments

Comments
 (0)