Merge pull request #4811 from sarroutbi/max_error_file_size

Add option to limit error file to configured size
This commit is contained in:
Rainer Gerhards 2022-03-07 12:41:31 +01:00 committed by GitHub
commit b4de264582
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 10 deletions

View File

@ -189,6 +189,7 @@ static struct cnfparamdescr cnfparamdescr[] = {
{ "name", eCmdHdlrGetWord, 0 }, /* legacy: actionname */
{ "type", eCmdHdlrString, CNFPARAM_REQUIRED }, /* legacy: actionname */
{ "action.errorfile", eCmdHdlrString, 0 },
{ "action.errorfile.maxsize", eCmdHdlrInt, 0 },
{ "action.writeallmarkmessages", eCmdHdlrBinary, 0 }, /* legacy: actionwriteallmarkmessages */
{ "action.execonlyeverynthtime", eCmdHdlrInt, 0 }, /* legacy: actionexeconlyeverynthtime */
{ "action.execonlyeverynthtimetimeout", eCmdHdlrInt, 0 }, /* legacy: actionexeconlyeverynthtimetimeout */
@ -391,6 +392,8 @@ rsRetVal actionConstruct(action_t **ppThis)
pThis->iResumeRetryCount = 0;
pThis->pszName = NULL;
pThis->pszErrFile = NULL;
pThis->maxErrFileSize = 0;
pThis->errFileWritten = 0;
pThis->pszExternalStateFile = NULL;
pThis->fdErrFile = -1;
pThis->bWriteAllMarkMsgs = 1;
@ -1445,16 +1448,27 @@ actionWriteErrorFile(action_t *__restrict__ const pThis, const rsRetVal ret,
char *const rendered = strdup((char*)fjson_object_to_json_string(etry));
if(rendered == NULL)
goto done;
const size_t toWrite = strlen(rendered) + 1;
/* note: we use the '\0' inside the string to store a LF - we do not
* otherwise need it and it safes us a copy/realloc.
*/
rendered[toWrite-1] = '\n'; /* NO LONGER A STRING! */
const ssize_t wrRet = write(pThis->fdErrFile, rendered, toWrite);
if(wrRet != (ssize_t) toWrite) {
LogError(errno, RS_RET_IO_ERROR,
"action %s: error writing errorFile %s, write returned %lld",
pThis->pszName, pThis->pszErrFile, (long long) wrRet);
size_t toWrite = strlen(rendered) + 1;
// Check if need to truncate the amount of bytes to write
if (pThis->maxErrFileSize > 0) {
if (pThis->errFileWritten + toWrite > pThis->maxErrFileSize) {
// Truncate to the pending available
toWrite = pThis->maxErrFileSize - pThis->errFileWritten;
}
pThis->errFileWritten += toWrite;
}
if(toWrite > 0) {
/* note: we use the '\0' inside the string to store a LF - we do not
* otherwise need it and it safes us a copy/realloc.
*/
rendered[toWrite-1] = '\n'; /* NO LONGER A STRING! */
const ssize_t wrRet = write(pThis->fdErrFile, rendered, toWrite);
if(wrRet != (ssize_t) toWrite) {
LogError(errno, RS_RET_IO_ERROR,
"action %s: error writing errorFile %s, write returned %lld",
pThis->pszName, pThis->pszErrFile, (long long) wrRet);
}
}
free(rendered);
@ -2039,6 +2053,8 @@ actionApplyCnfParam(action_t * const pAction, struct cnfparamvals * const pvals)
continue; /* this is handled seperately during module select! */
} else if(!strcmp(pblk.descr[i].name, "action.errorfile")) {
pAction->pszErrFile = es_str2cstr(pvals[i].val.d.estr, NULL);
} else if(!strcmp(pblk.descr[i].name, "action.errorfile.maxsize")) {
pAction->maxErrFileSize = pvals[i].val.d.n;
} else if(!strcmp(pblk.descr[i].name, "action.externalstate.file")) {
pAction->pszExternalStateFile = es_str2cstr(pvals[i].val.d.estr, NULL);
} else if(!strcmp(pblk.descr[i].name, "action.writeallmarkmessages")) {

View File

@ -74,6 +74,8 @@ struct action_s {
/* error file */
const char *pszErrFile;
int fdErrFile;
size_t maxErrFileSize;
size_t errFileWritten;
pthread_mutex_t mutErrFile;
/* external stat file system */
const char *pszExternalStateFile;

View File

@ -718,6 +718,7 @@ TESTS += \
mysql-actq-mt.sh \
mysql-actq-mt-withpause.sh \
action-tx-single-processing.sh \
action-tx-errfile-maxsize.sh \
action-tx-errfile.sh
mysql-basic.log: mysqld-start.log
@ -2333,6 +2334,8 @@ EXTRA_DIST= \
sndrcv_omudpspoof_nonstdpt.sh \
sndrcv_gzip.sh \
action-tx-single-processing.sh \
omfwd-errfile-maxsize.sh \
action-tx-errfile-maxsize.sh \
action-tx-errfile.sh \
testsuites/action-tx-errfile.result \
pipeaction.sh \

View File

@ -0,0 +1,35 @@
#!/bin/bash
# part of the rsyslog project, released under ASL 2.0
. ${srcdir:=.}/diag.sh init
export NUMMESSAGES=50 # enough to generate big file
export MAX_ERROR_SIZE=100
generate_conf
add_conf '
$ModLoad ../plugins/ommysql/.libs/ommysql
global(errormessagestostderr.maxnumber="5")
template(type="string" name="tpl" string="insert into SystemEvents (Message, Facility) values (\"%msg%\", %$!facility%)" option.sql="on")
if((not($msg contains "error")) and ($msg contains "msgnum:")) then {
set $.num = field($msg, 58, 2);
if $.num % 2 == 0 then {
set $!facility = $syslogfacility;
} else {
set $/cntr = 0;
}
action(type="ommysql" name="mysql_action_errfile_maxsize" server="127.0.0.1" template="tpl"
db="'$RSYSLOG_DYNNAME'" uid="rsyslog" pwd="testbench" action.errorfile="'$RSYSLOG2_OUT_LOG'" action.errorfile.maxsize="'$MAX_ERROR_SIZE'")
}
'
mysql_prep_for_test
startup
injectmsg
shutdown_when_empty
wait_shutdown
mysql_get_data
check_file_exists ${RSYSLOG2_OUT_LOG}
file_size_check ${RSYSLOG2_OUT_LOG} ${MAX_ERROR_SIZE}
exit_test

View File

@ -2522,6 +2522,17 @@ wait_for_stats_flush() {
echo "stats push registered"
}
# Check file exists and is of a particular size
# $1 - file to check
# $2 - size to check
file_size_check() {
local size=$(ls -l $1 | awk {'print $5'})
if [ "${size}" != "${2}" ]; then
printf 'File:[%s] has unexpected size. Expected:[%d], Size:[%d]\n', $1 $2 $size
error_exit 1
fi
return 0
}
case $1 in
'init') $srcdir/killrsyslog.sh # kill rsyslogd if it runs for some reason

17
tests/omfwd-errfile-maxsize.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
# part of the rsyslog project, released under ASL 2.0
. ${srcdir:=.}/diag.sh init
export MAX_ERROR_SIZE=1999
generate_conf
add_conf '
action(type="omfwd" target="1.2.3.4" port="1234" Protocol="tcp" NetworkNamespace="doesNotExist"
action.errorfile="'$RSYSLOG2_OUT_LOG'" action.errorfile.maxsize="'$MAX_ERROR_SIZE'")
'
startup
shutdown_when_empty
wait_shutdown
check_file_exists ${RSYSLOG2_OUT_LOG}
file_size_check ${RSYSLOG2_OUT_LOG} ${MAX_ERROR_SIZE}
exit_test