Update logrotate cookbook

This commit is contained in:
Greg Karékinian
2017-05-02 11:58:32 +02:00
parent 180b3f5df0
commit ccbf11a407
23 changed files with 679 additions and 506 deletions

View File

@@ -1,41 +1,53 @@
logrotate Cookbook
==================
# logrotate Cookbook
[![Build Status](https://secure.travis-ci.org/stevendanna/logrotate.png?branch=master)](http://travis-ci.org/stevendanna/logrotate)
Manages the logrotate package and provides a definition to manage application specific logrotate configuration.
Manages the logrotate package and provides a definition to manage
application specific logrotate configuration.
Requirements
------------
Should work on any platform that includes a 'logrotate' package and writes logrotate configuration to /etc/logrotate.d. Tested on Ubuntu, Debian and Red Hat/CentOS.
## Requirements
Should work on any platform that includes a 'logrotate' package and
writes logrotate configuration to /etc/logrotate.d. Tested on Ubuntu
and Centos.
Recipes
-------
## Recipes
### global
Generates and controls a global `/etc/logrotate.conf` file that will include additional files generated by the `logrotate_app` definition (see below). The contents of the configuration file is controlled through node attributes under `node['logrotate']['global']`. The default attributes are based on the configuration from the Ubuntu logrotate package.
To define a valueless directive (e.g. `compress`, `copy`) simply add an attribute named for the directive with a truthy value :
Generates and controls a global `/etc/logrotate.conf` file that will
include additional files generated by the `logrotate_app` definition
(see below). The contents of the configuration file is controlled
through node attributes under `node['logrotate']['global']`. The
default attributes are based on the configuration from the Ubuntu
logrotate package.
To define a valueless directive (e.g. `compress`, `copy`) simply add
an attribute named for the directive with a truthy value:
```ruby
node['logrotate']['global']['compress'] = 'any value here'
```
Note that defining a valueless directive with a falsey value will not make it false, but will remove it:
Note that defining a valueless directive with a falsey value will not
make it false, but will remove it:
```ruby
# Removes a defaulted 'compress' directive; does not add a 'nocompress' directive.
node.override['logrotate']['global']['compress'] = false
```
To fully override a booleanish directive like `compress`, you should probably remove the positive form and add the negative form:
To fully override a booleanish directive like `compress`, you should
probably remove the positive form and add the negative form:
```ruby
node.override['logrotate']['global']['compress'] = false
node.override['logrotate']['global']['nocompress'] = true
```
The same is true of frequency directives; to be certain the frequency directive you want is included in the global configuration, you should override the ones you don't want as false:
The same is true of frequency directives; to be certain the frequency
directive you want is included in the global configuration, you should
override the ones you don't want as false:
```ruby
%w[ daily weekly yearly ].each do |freq|
@@ -44,13 +56,17 @@ end
node.override['logrotate']['global']['monthly'] = true
```
To define a parameter with a value (e.g. `create`, `mail`) add an attribute with the desired value:
To define a parameter with a value (e.g. `create`, `mail`) add an
attribute with the desired value:
```ruby
node['logrotate']['global']['create'] = '0644 root adm'
```
To define a path stanza in the global configuration (generally unneeded because of the `logrotate_app` definition) just add an attribute with the path as the name and a hash containing directives and parameters as described above:
To define a path stanza in the global configuration (generally
unneeded because of the `logrotate_app` definition) just add an
attribute with the path as the name and a hash containing directives
and parameters as described above:
```ruby
node['logrotate']['global']['/var/log/wtmp'] = {
@@ -61,7 +77,9 @@ node['logrotate']['global']['/var/log/wtmp'] = {
}
```
`firstaction`, `prerotate`, `postrotate`, and `lastaction` scripts can be defined either as arrays of the lines to put in the script or multiline strings:
`firstaction`, `prerotate`, `postrotate`, and `lastaction` scripts can
be defined either as arrays of the lines to put in the script or
multiline strings:
```ruby
node['logrotate']['global']['/var/log/foo/*.log'] = {
@@ -78,43 +96,76 @@ node['logrotate']['global']['/var/log/foo/*.log'] = {
```
Definitions
Resources
-----------
### logrotate_app
This definition can be used to drop off customized logrotate config files on a per application basis.
The definition takes the following params:
This resource can be used to drop off customized logrotate config
files on a per application basis.
- `path`: specifies a single path (string) or multiple paths (array) that should have logrotation stanzas created in the config file. No default, this must be specified.
- `enable`: true/false, if true it will create the template in /etc/logrotate.d.
- `frequency`: sets the frequency for rotation. Default value is 'weekly'. Valid values are: daily, weekly, monthly, yearly, see the logrotate man page for more information.
- `dateformat`: specifies date extension with %Y, %m, %d, and %s. The default value is -%Y%m%d.
- `size`: Log files are rotated when they grow bigger than size bytes.
- `maxsize`: Log files are rotated when they grow bigger than size bytes even before the additionally specified time interval.
- `su`: Rotate log files set under this user and group instead of using default user/group.
- `template`: sets the template source, default is "logrotate.erb".
- `template_mode`: the mode to create the logrotate template with (default "0440")
- `template_owner`: the owner of the logrotate template (default "root")
- `template_group`: the group of the logrotate template (default "root")
- `cookbook`: select the template source from the specified cookbook. By default it will use the template from the logrotate cookbook.
- `create`: creation parameters for the logrotate "create" config, follows the form "mode owner group". This is an optional parameter, and is nil by default.
- `firstaction`: lines to be executed once before all log files that match the wildcarded pattern are rotated, before pre-rotate script is run and only if at least one log will actually be rotated
- `postrotate`: lines to be executed after the log file is rotated
- `prerotate`: lines to be executed before the log file is rotated
- `lastaction`: lines to be executed once after all log files that match the wildcarded pattern are rotated, after postrotate script is run and only if at least one log is rotated
- `rotate`: Log files are rotated this many times before being removed or mailed.
- `sharedscripts`: if true, the sharedscripts options is specified which makes sure prescript and postscript commands are run only once (even if multiple files match the path)
The resource takes the following properties:
- `path`: specifies a single path (string) or multiple paths (array)
that should have logrotation stanzas created in the config file. No
default, this must be specified.
Usage
-----
The default recipe will ensure logrotate is always up to date.
- `cookbook`: The cookbook that continues the template for
logrotate_app config definitions. By default this is `logrotate`.
Users can provide their own template by setting this attribute to
point at a different cookbook.
To create application specific logrotate configs, use the `logrotate_app` definition. For example, to rotate logs for a tomcat application named myapp that writes its log file to `/var/log/tomcat/myapp.log`:
- `template_name`: sets the template source, default is
"logrotate.erb".
- `template_mode`: the mode to create the logrotate template with
(default: "0440")
- `template_owner`: the owner of the logrotate template (default:
"root")
- `template_group`: the group of the logrotate template (default:
"root")
- `frequency`: sets the frequency for rotation. Default value is
'weekly'. Valid values are: hourly, daily, weekly, monthly, yearly,
see the logrotate man page for more information. Note that usually
logrotate is configured to be run by cron daily. You have to change
this configuration and run logrotate hourly to be able to really
rotate logs hourly. Hourly rotation requires logrotate v3.8.5 or
higher.
- `options`: Any logrotate configuration option that doesn't specify a
value. See the logrotate(8) manual page of v3.9.2 or earlier for
details.
In addition to these properties, any logrotate option that takes a
parameter can be used as a logrotate_app property. For example, to set
the `rotate` option you can use a resource declaration such as:
```ruby
logrotate_app 'tomcat-myapp' do
path '/var/log/tomcat/myapp.log'
frequency 'daily'
rotate 30
create '644 root adm'
end
```
See the logrotate(8) manual page of v3.9.2 or earlier for the list of
available options.
## Usage
The default recipe will ensure logrotate is always up to date.
To create application specific logrotate configs, use the
`logrotate_app` definition. For example, to rotate logs for a tomcat
application named myapp that writes its log file to
`/var/log/tomcat/myapp.log`:
```ruby
logrotate_app 'tomcat-myapp' do
cookbook 'logrotate'
path '/var/log/tomcat/myapp.log'
frequency 'daily'
rotate 30
@@ -126,7 +177,6 @@ To rotate multiple logfile paths, specify the path as an array:
```ruby
logrotate_app 'tomcat-myapp' do
cookbook 'logrotate'
path ['/var/log/tomcat/myapp.log', '/opt/local/tomcat/catalina.out']
frequency 'daily'
create '644 root adm'
@@ -138,7 +188,6 @@ To specify which logrotate options, specify the options as an array:
```ruby
logrotate_app 'tomcat-myapp' do
cookbook 'logrotate'
path '/var/log/tomcat/myapp.log'
options ['missingok', 'delaycompress', 'notifempty']
frequency 'daily'
@@ -147,15 +196,16 @@ logrotate_app 'tomcat-myapp' do
end
```
## License & Authors
License & Authors
-----------------
- Author:: Steven Danna (<steve@chef.io>)
- Author:: Scott M. Likens (<scott@likens.us>)
- Author:: Joshua Timberman (<joshua@chef.io>)
```text
Copyright 2009, Scott M. Likens
Copyright 2011-2012, Chef Software, Inc.
Copyright 2016, Steven Danna
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.