Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atop log feature enhancement #175

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions atop.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ unsigned long interval = 10;
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 @@ -200,6 +202,8 @@ 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 @@ -209,6 +213,8 @@ static struct {
{ "flags", do_flags, 0, },
{ "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 @@ -975,6 +981,18 @@ do_linelength(char *name, char *val)
linelen = get_posval(name, val);
}

static void
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
23 changes: 23 additions & 0 deletions atop.daily
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,34 @@ 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

#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
12 changes: 12 additions & 0 deletions man/atoprc.5
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ 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 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 @@ -232,6 +240,10 @@ flags\ \ \ \ \ \ \ \ \ Aaf
.br
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