virtlogd: make max file size & number of backups configurable

Currently virtlogd has a hardcoded max file size of 128kb
and max of 3 backups. This adds two new config parameters
to /etc/libvirt/virtlogd.conf to let these be customized.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2016-07-01 17:40:55 +01:00
parent e114b09157
commit 24aacfa8e8
8 changed files with 57 additions and 13 deletions

View File

@ -165,6 +165,8 @@ virLogDaemonNew(virLogDaemonConfigPtr config, bool privileged)
goto error; goto error;
if (!(logd->handler = virLogHandlerNew(privileged, if (!(logd->handler = virLogHandlerNew(privileged,
config->max_size,
config->max_backups,
virLogDaemonInhibitor, virLogDaemonInhibitor,
logd))) logd)))
goto error; goto error;
@ -185,7 +187,8 @@ virLogDaemonGetHandler(virLogDaemonPtr dmn)
static virLogDaemonPtr static virLogDaemonPtr
virLogDaemonNewPostExecRestart(virJSONValuePtr object, bool privileged) virLogDaemonNewPostExecRestart(virJSONValuePtr object, bool privileged,
virLogDaemonConfigPtr config)
{ {
virLogDaemonPtr logd; virLogDaemonPtr logd;
virJSONValuePtr child; virJSONValuePtr child;
@ -226,6 +229,8 @@ virLogDaemonNewPostExecRestart(virJSONValuePtr object, bool privileged)
if (!(logd->handler = virLogHandlerNewPostExecRestart(child, if (!(logd->handler = virLogHandlerNewPostExecRestart(child,
privileged, privileged,
config->max_size,
config->max_backups,
virLogDaemonInhibitor, virLogDaemonInhibitor,
logd))) logd)))
goto error; goto error;
@ -717,7 +722,8 @@ static int
virLogDaemonPostExecRestart(const char *state_file, virLogDaemonPostExecRestart(const char *state_file,
const char *pid_file, const char *pid_file,
int *pid_file_fd, int *pid_file_fd,
bool privileged) bool privileged,
virLogDaemonConfigPtr config)
{ {
const char *gotmagic; const char *gotmagic;
char *wantmagic = NULL; char *wantmagic = NULL;
@ -766,7 +772,9 @@ virLogDaemonPostExecRestart(const char *state_file,
(*pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0) (*pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0)
goto cleanup; goto cleanup;
if (!(logDaemon = virLogDaemonNewPostExecRestart(object, privileged))) if (!(logDaemon = virLogDaemonNewPostExecRestart(object,
privileged,
config)))
goto cleanup; goto cleanup;
ret = 1; ret = 1;
@ -1086,7 +1094,8 @@ int main(int argc, char **argv) {
if ((rv = virLogDaemonPostExecRestart(state_file, if ((rv = virLogDaemonPostExecRestart(state_file,
pid_file, pid_file,
&pid_file_fd, &pid_file_fd,
privileged)) < 0) { privileged,
config)) < 0) {
ret = VIR_LOG_DAEMON_ERR_INIT; ret = VIR_LOG_DAEMON_ERR_INIT;
goto cleanup; goto cleanup;
} }

View File

@ -128,6 +128,8 @@ virLogDaemonConfigNew(bool privileged ATTRIBUTE_UNUSED)
return NULL; return NULL;
data->max_clients = 1024; data->max_clients = 1024;
data->max_size = 128 * 1024;
data->max_backups = 3;
return data; return data;
} }
@ -154,6 +156,9 @@ virLogDaemonConfigLoadOptions(virLogDaemonConfigPtr data,
GET_CONF_STR(conf, filename, log_outputs); GET_CONF_STR(conf, filename, log_outputs);
GET_CONF_UINT(conf, filename, max_clients); GET_CONF_UINT(conf, filename, max_clients);
GET_CONF_UINT(conf, filename, max_size);
GET_CONF_UINT(conf, filename, max_backups);
return 0; return 0;
error: error:

View File

@ -34,6 +34,9 @@ struct _virLogDaemonConfig {
char *log_filters; char *log_filters;
char *log_outputs; char *log_outputs;
int max_clients; int max_clients;
size_t max_backups;
size_t max_size;
}; };

View File

@ -41,8 +41,6 @@ VIR_LOG_INIT("logging.log_handler");
#define VIR_FROM_THIS VIR_FROM_LOGGING #define VIR_FROM_THIS VIR_FROM_LOGGING
#define DEFAULT_FILE_SIZE (128 * 1024)
#define DEFAULT_MAX_BACKUP 3
#define DEFAULT_MODE 0600 #define DEFAULT_MODE 0600
typedef struct _virLogHandlerLogFile virLogHandlerLogFile; typedef struct _virLogHandlerLogFile virLogHandlerLogFile;
@ -62,6 +60,9 @@ struct _virLogHandler {
virObjectLockable parent; virObjectLockable parent;
bool privileged; bool privileged;
size_t max_size;
size_t max_backups;
virLogHandlerLogFilePtr *files; virLogHandlerLogFilePtr *files;
size_t nfiles; size_t nfiles;
@ -184,6 +185,8 @@ virLogHandlerDomainLogFileEvent(int watch,
virLogHandlerPtr virLogHandlerPtr
virLogHandlerNew(bool privileged, virLogHandlerNew(bool privileged,
size_t max_size,
size_t max_backups,
virLogHandlerShutdownInhibitor inhibitor, virLogHandlerShutdownInhibitor inhibitor,
void *opaque) void *opaque)
{ {
@ -196,6 +199,8 @@ virLogHandlerNew(bool privileged,
goto error; goto error;
handler->privileged = privileged; handler->privileged = privileged;
handler->max_size = max_size;
handler->max_backups = max_backups;
handler->inhibitor = inhibitor; handler->inhibitor = inhibitor;
handler->opaque = opaque; handler->opaque = opaque;
@ -254,8 +259,8 @@ virLogHandlerLogFilePostExecRestart(virLogHandlerPtr handler,
} }
if ((file->file = virRotatingFileWriterNew(path, if ((file->file = virRotatingFileWriterNew(path,
DEFAULT_FILE_SIZE, handler->max_size,
DEFAULT_MAX_BACKUP, handler->max_backups,
false, false,
DEFAULT_MODE)) == NULL) DEFAULT_MODE)) == NULL)
goto error; goto error;
@ -283,6 +288,8 @@ virLogHandlerLogFilePostExecRestart(virLogHandlerPtr handler,
virLogHandlerPtr virLogHandlerPtr
virLogHandlerNewPostExecRestart(virJSONValuePtr object, virLogHandlerNewPostExecRestart(virJSONValuePtr object,
bool privileged, bool privileged,
size_t max_size,
size_t max_backups,
virLogHandlerShutdownInhibitor inhibitor, virLogHandlerShutdownInhibitor inhibitor,
void *opaque) void *opaque)
{ {
@ -292,6 +299,8 @@ virLogHandlerNewPostExecRestart(virJSONValuePtr object,
size_t i; size_t i;
if (!(handler = virLogHandlerNew(privileged, if (!(handler = virLogHandlerNew(privileged,
max_size,
max_backups,
inhibitor, inhibitor,
opaque))) opaque)))
return NULL; return NULL;
@ -396,8 +405,8 @@ virLogHandlerDomainOpenLogFile(virLogHandlerPtr handler,
goto error; goto error;
if ((file->file = virRotatingFileWriterNew(path, if ((file->file = virRotatingFileWriterNew(path,
DEFAULT_FILE_SIZE, handler->max_size,
DEFAULT_MAX_BACKUP, handler->max_backups,
trunc, trunc,
DEFAULT_MODE)) == NULL) DEFAULT_MODE)) == NULL)
goto error; goto error;
@ -487,7 +496,7 @@ virLogHandlerDomainReadLogFile(virLogHandlerPtr handler,
virObjectLock(handler); virObjectLock(handler);
if (!(file = virRotatingFileReaderNew(path, DEFAULT_MAX_BACKUP))) if (!(file = virRotatingFileReaderNew(path, handler->max_backups)))
goto error; goto error;
if (virRotatingFileReaderSeek(file, inode, offset) < 0) if (virRotatingFileReaderSeek(file, inode, offset) < 0)
@ -542,8 +551,8 @@ virLogHandlerDomainAppendLogFile(virLogHandlerPtr handler,
if (!writer) { if (!writer) {
if (!(newwriter = virRotatingFileWriterNew(path, if (!(newwriter = virRotatingFileWriterNew(path,
DEFAULT_FILE_SIZE, handler->max_size,
DEFAULT_MAX_BACKUP, handler->max_backups,
false, false,
DEFAULT_MODE))) DEFAULT_MODE)))
goto cleanup; goto cleanup;

View File

@ -34,10 +34,14 @@ typedef void (*virLogHandlerShutdownInhibitor)(bool inhibit,
void *opaque); void *opaque);
virLogHandlerPtr virLogHandlerNew(bool privileged, virLogHandlerPtr virLogHandlerNew(bool privileged,
size_t max_size,
size_t max_backups,
virLogHandlerShutdownInhibitor inhibitor, virLogHandlerShutdownInhibitor inhibitor,
void *opaque); void *opaque);
virLogHandlerPtr virLogHandlerNewPostExecRestart(virJSONValuePtr child, virLogHandlerPtr virLogHandlerNewPostExecRestart(virJSONValuePtr child,
bool privileged, bool privileged,
size_t max_size,
size_t max_backups,
virLogHandlerShutdownInhibitor inhibitor, virLogHandlerShutdownInhibitor inhibitor,
void *opaque); void *opaque);

View File

@ -2,9 +2,13 @@ module Test_virtlogd =
let conf = "log_level = 3 let conf = "log_level = 3
log_filters=\"3:remote 4:event\" log_filters=\"3:remote 4:event\"
log_outputs=\"3:syslog:virtlogd\" log_outputs=\"3:syslog:virtlogd\"
max_size = 131072
max_backups = 3
" "
test Virtlogd.lns get conf = test Virtlogd.lns get conf =
{ "log_level" = "3" } { "log_level" = "3" }
{ "log_filters" = "3:remote 4:event" } { "log_filters" = "3:remote 4:event" }
{ "log_outputs" = "3:syslog:virtlogd" } { "log_outputs" = "3:syslog:virtlogd" }
{ "max_size" = "131072" }
{ "max_backups" = "3" }

View File

@ -29,6 +29,8 @@ module Virtlogd =
| str_entry "log_outputs" | str_entry "log_outputs"
| int_entry "log_buffer_size" | int_entry "log_buffer_size"
| int_entry "max_clients" | int_entry "max_clients"
| int_entry "max_size"
| int_entry "max_backups"
(* Each enty in the config is one of the following three ... *) (* Each enty in the config is one of the following three ... *)
let entry = logging_entry let entry = logging_entry

View File

@ -57,3 +57,11 @@
# The maximum number of concurrent client connections to allow # The maximum number of concurrent client connections to allow
# over all sockets combined. # over all sockets combined.
#max_clients = 1024 #max_clients = 1024
# Maximum file size before rolling over. Defaults to 128 KB
#max_size = 131072
# Maximum number of backup files to keep. Defaults to 3,
# not including the primary active file
#max_backups = 3