3

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).

Michael Kohne
  • 2,334
  • 1
  • 16
  • 29

2 Answers2

3

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.

markdrayton
  • 2,449
  • 1
  • 20
  • 24
  • This is a little heavier weight than I'd like, but it's LGPL, so it's really usable. If I don't get a more light-weight answer, I'll probably go with it. – Michael Kohne Sep 15 '09 at 14:27
0

In addition to markdrayton's answer, I'd add that Augeas can now be used as an interpreter, so you can actually write an executable script with the following content:

#!/usr/bin/augtool -f
set /files/etc/logrotate.conf/rule[file='/var/log/wtmp']/schedule weekly
save

and execute it. You could even make it shorter by using the autosave option (-s):

#!/usr/bin/augtool -sf
set /files/etc/logrotate.conf/rule[file='/var/log/wtmp']/schedule weekly
raphink
  • 11,987
  • 6
  • 37
  • 48