Cfengine 3 can be used to tidy your systems up on regular basis. Depending on the time, day of the week of the file extensions you are able to define how Cfengine 3 shall delete old and obsolete files. In today’s example a code snippet is shown which deletes all .gz files in /var/log which […]

Author:

Cfengine 3 can be used to tidy your systems up on regular basis. Depending on the time, day of the week of the file extensions you are able to define how Cfengine 3 shall delete old and obsolete files.
In today’s example a code snippet is shown which deletes all .gz files in /var/log which are at least eleven days old.

Writing the Cfengine 3 code for deleting old files

body common control {
        version         => "1.0";
        inputs          => { "cfengine_stdlib.cf" };
        bundlesequence  => { "delete_old_files" };
}

bundle agent delete_old_files {
  files:  # <--- promise
        "/var/log/.*gz" # <--- promiser
          file_select   => days_old("11"), # <--- promise body
          depth_search  => recurse("inf"), # <--- promise body
          delete        => tidy; # <--- promise body
}

Save the file as /etc/cfengine3/example11.cf and look for typos or other syntax errors:

/var/cfengine/bin/cf-promises -f /etc/cfengine3/example11.cf

For verifying purposes you should rsync your current state in /var/log to another directory:

rsync -Ha /var/log --progress --delete /root/log

No errors found in the previous check? Great! Let’s run our script then:

/var/cfengine/bin/cf-promises -f /etc/cfengine3/example11.cf

Now have a look at the diff:

diff -r /root/log/log /var/log

You should be able to see that all .gz files which are at least eleven days old were removed from your system.

Analyzing the Cfengine 3 code snippet
We jump directly to the interesting part:

bundle agent delete_old_files {
  files:  # <--- promise
        "/var/log/.*gz" # <--- promiser
          file_select   => days_old("11"), # <--- promise body
          depth_search  => recurse("inf"), # <--- promise body
          delete        => tidy; # <--- promise body
}

The promise of the type “files” contains a promiser which defines the affected objects. In this case, all .gz files in the /var/log directory will be our target.
The “file_select” attribute is the key line here – with “days_old(“INT”)” some sort of filter is created and somehow “tuned” with the “depth_search” line.
The action to be conducted is “delete => tidy” which means nothing else than the files being affected by the promise will be deleted.

You surely can imagine that this promise can be modified to serve as a typical log rotation feature.
Attention: The “file_select” attribute with “days_old(“INT”)” may seem like only files being eleven days old get deleted – but in my test I observed Cfengine 3 removing all files which are eleven days old or older.

Of course you are able to use your own definition. The figure eleven was just used to serve as an example but has no further meaning.

As usual, you can download today’s Cfengine 3 code snippet here.

Comments on this entry (1 comment)

Did you like this post? You can share your opinion with us! Simply click here.

Jason

This is very useful, great thanks. Keep working on cf3. Expecting more.

Reply

Add Your Comment

Powered by sweet Captcha


two − 1 =