I am using logrotate to rotate my files, with the option copytruncate.
http://linuxcommand.org/man_pages/logrotate8.html
And It is causing splunk to reindex the file during the rotation.
see my rotate script /etc/logrotate.d/test
"/home/feed/test.log" {
rotate 5
size=50M
sharedscripts
copytruncate
compress
}
What happens is :
- the rotation creates a new rotated file. (test.log.1)
- the rotation copy the content of the file to the new file (from line 1 to line X), then compress it.
- the rotation then truncates the original file by removing the first lines per chunks, until reaching the line X.
At the same time, splunk is monitoring the file, and check the first 256 chars for the crc.
- during the truncate, because the first lines are changing, splunk redetect the file as new, and reindex it all.
- we find duplicates of the lines, the first lines with fewer duplicates, the last lines with more duplicates.
How to identify the issue :
- look for duplicates and the time when they were generated. (not the event timestamp)
source=path/to/my/logfile | convert ctime(_indextime) AS indextime| stats count values(indextime) AS indextime by _raw | where count >1
- at the time of the rotation, look on the forwarder splunkd.log, check for multiple events like :
11-05-2014 10:48:33.924 +0000 INFO WatchedFile - Will begin reading at offset=0 for file='/home/feed/test.log
11-05-2014 10:48:34.234 +0000 INFO WatchedFile - Will begin reading at offset=0 for file='/home/feed/test.log
etc...
The workarounds I found are :
A - replace the copytruncate rotation by a move rotation .
It may not always be a solution, some applications are pretty limited and need to keep the handle of the log file always open.
B- disable the monitoring just before the rotation.
The idea is to disable the monitoring before the rotation and re-enable it just after.
using the logrotate options. Splunk will simply detect the new files, and resume.
Here is my configuration :
Inputs.conf in splunk in a specific app "input_rotate"
cat ./opt/splunk/etc/apps/input_rotate/local/inputs.conf
[monitor://home/feed/.log]
disabled = false
logrotate script /etc/logrotate.d/test
"/home/feed/test.log" {
rotate 5
size=50M
sharedscripts
copytruncate
compress
prerotate
/opt/splunk/bin/splunk disable app input_rotate -auth admin:changeme
endscript
postrotate
/opt/splunk/bin/splunk enable app input_rotate -auth admin:changeme
endscript
}
Remarks :
/opt/splunk/bin/splunk edit monitor "/home/feed/*.log*" -disabled true -auth admin:changeme