Skip to content

Commit a128dd3

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 2bef045 commit a128dd3

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

atop.c

+12
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ unsigned long sampcnt;
312312
char screen;
313313
int linelen = 80;
314314
int generations = 28; /* By default, keep recent 30 days' log */
315+
char logpath[256] = "/var/log/atop"; /*support writing to other paths, to avoid system disk being fully occupied */
315316
char acctreason; /* accounting not active (return val) */
316317
char rawname[RAWNAMESZ];
317318
char rawreadflag;
@@ -354,6 +355,7 @@ void do_flags(char *, char *);
354355
void do_interval(char *, char *);
355356
void do_linelength(char *, char *);
356357
void do_generations(char *, char *);
358+
void do_logpath(char *, char *);
357359
void do_username(char *, char *);
358360
void do_procname(char *, char *);
359361
void do_maxcpu(char *, char *);
@@ -406,6 +408,7 @@ static struct {
406408
{ "interval", do_interval, 0, },
407409
{ "linelen", do_linelength, 0, },
408410
{ "generations", do_generations, 0, },
411+
{ "logpath", do_logpath, 0, },
409412
{ "username", do_username, 0, },
410413
{ "procname", do_procname, 0, },
411414
{ "maxlinecpu", do_maxcpu, 0, },
@@ -1164,6 +1167,15 @@ do_generations(char *name, char *val)
11641167
generations = get_posval(name, val);
11651168
}
11661169

1170+
void
1171+
do_logpath(char *name, char *val)
1172+
{
1173+
//For safety purpose, check logpath's permission
1174+
check_file_perm(val);
1175+
1176+
strcpy(logpath, val);
1177+
}
1178+
11671179
/*
11681180
** read RC-file and modify defaults accordingly
11691181
*/

atop.daily

+14
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

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ extern char rawname[];
9090
extern char rawreadflag;
9191
extern time_t begintime, endtime, cursortime; // epoch or time in day
9292
extern char flaglist[];
93+
extern char logpath[];
9394
extern struct visualize vis;
9495

9596
extern int osrel;

man/atoprc.5

+6
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

+3-4
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);
@@ -330,7 +329,7 @@ rawread(void)
330329
tp = localtime(&timenow);
331330

332331
snprintf(rawname, RAWNAMESZ, "%s/atop_%04d%02d%02d",
333-
BASEPATH,
332+
logpath,
334333
tp->tm_year+1900,
335334
tp->tm_mon+1,
336335
tp->tm_mday);
@@ -352,7 +351,7 @@ rawread(void)
352351
strcpy(savedname, rawname); // no overflow (len=8)
353352

354353
snprintf(rawname, RAWNAMESZ, "%s/atop_%s",
355-
BASEPATH,
354+
logpath,
356355
savedname);
357356
break;
358357
}
@@ -384,7 +383,7 @@ rawread(void)
384383
tp = localtime(&timenow);
385384

386385
snprintf(rawname, RAWNAMESZ, "%s/atop_%04d%02d%02d",
387-
BASEPATH,
386+
logpath,
388387
tp->tm_year+1900,
389388
tp->tm_mon+1,
390389
tp->tm_mday);

0 commit comments

Comments
 (0)