Linux – Script to modify logrotate configuration files

linuxlogrotate

I'm looking for a script that will allow me to programatically modify logrotate configuration files. At the least I'd like it to be able to replace a block such as

/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

with a new block given on the command line or via stdin or via file (I don't care which). It must be able to operate on arbitrary config files.

I'd really like it if it understood the configuration enough that I could say something like

logrotateupdate /etc/logrotate.conf /var/log/wtmp weekly

and have it do the right thing (remove the monthly and replace it with weekly in the above example).

This may be something that's commonly known to others, but it's something I've never seen.

My system is linux (Ubuntu).

Best Answer

Augeas is the tool for this but it has a fairly steep learning curve. Here's how to set /var/log/wtmp to rotate weekly (some /etc/logrotate.conf trimmed for brevity):

[root@dev ~]# cat /etc/logrotate.conf 
# trimmed

# no packages own wtmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    minsize 1M
    create 0664 root utmp
    rotate 1
}

[root@dev ~]# (echo "set \
  /files/etc/logrotate.conf/rule[file='/var/log/wtmp']/schedule weekly"; \
  echo save) | augtool
Saved 1 file(s)

[root@dev ~]# cat /etc/logrotate.conf 
# trimmed

# no packages own wtmp -- we'll rotate them here
/var/log/wtmp {
    weekly
    minsize 1M
    create 0664 root utmp
    rotate 1
}

There are lenses (as the descriptions of file structures are known) for many configuration files. Writing new lenses isn't too hard once you've got your head around how it all works.

Augeas also ties in nicely with Puppet to make a very powerful configuration management system.