Rich Megginson (richmegginson) wrote,
Rich Megginson
richmegginson

Using inotifywait to compress 389 log files

inotifywait is used to efficiently poll for changes to a file or directory. On Red Hat/Fedora family operating systems, this is provided by the inotify-tools package - yum install inotify-tools. inotifywait can be used to watch for rotated access log files to compress. For example, if your access logs grow large due to a lot of traffic, you may want to compress the files to save disk space. In my testing, I have found that compressing an access log file using bzip2 reduces the size to about 3% of the original size. When the directory server rotates a log file, it issues a rename(), then recreates the access log file "access" again. You can use a shell script like this to monitor and compress rotated access log files:
# monitor and compress access logs
mon_and_comp_logs() {
    # the server does rename access -> access.20..... when rotating, then
    # creates a new file named "access"
    inotifywait -e move /var/log/dirsrv/slapd-INST --exclude '.bz2$' -m | while read dir act name ; do
        file=${dir}$name
        case "$file" in
        *access.20*)
            echo compressing $file
            bzip2 $file &
            ;;
        esac
    done
}

Then run this in the background:
# mon_and_comp_logs > mon_and_comp_logs.log 2>&1 &
Tags: 389, bzip2, inotify
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments