| 1 |
# Public: Configure logrotate to rotate a logfile. |
| 2 |
# |
| 3 |
# namevar - The String name of the rule. |
| 4 |
# path - The path String to the logfile(s) to be rotated. |
| 5 |
# ensure - The desired state of the logrotate rule as a String. Valid |
| 6 |
# values are 'absent' and 'present' (default: 'present'). |
| 7 |
# compress - A Boolean value specifying whether the rotated logs should |
| 8 |
# be compressed (optional). |
| 9 |
# compresscmd - The command String that should be executed to compress the |
| 10 |
# rotated logs (optional). |
| 11 |
# compressext - The extention String to be appended to the rotated log files |
| 12 |
# after they have been compressed (optional). |
| 13 |
# compressoptions - A String of command line options to be passed to the |
| 14 |
# compression program specified in `compresscmd` (optional). |
| 15 |
# copy - A Boolean specifying whether logrotate should just take a |
| 16 |
# copy of the log file and not touch the original (optional). |
| 17 |
# copytruncate - A Boolean specifying whether logrotate should truncate the |
| 18 |
# original log file after taking a copy (optional). |
| 19 |
# create - A Boolean specifying whether logrotate should create a new |
| 20 |
# log file immediately after rotation (optional). |
| 21 |
# create_mode - An octal mode String logrotate should apply to the newly |
| 22 |
# created log file if create => true (optional). |
| 23 |
# create_owner - A username String that logrotate should set the owner of the |
| 24 |
# newly created log file to if create => true (optional). |
| 25 |
# create_group - A String group name that logrotate should apply to the newly |
| 26 |
# created log file if create => true (optional). |
| 27 |
# dateext - A Boolean specifying whether rotated log files should be |
| 28 |
# archived by adding a date extension rather just a number |
| 29 |
# (optional). |
| 30 |
# dateformat - The format String to be used for `dateext` (optional). |
| 31 |
# Valid specifiers are '%Y', '%m', '%d' and '%s'. |
| 32 |
# delaycompress - A Boolean specifying whether compression of the rotated |
| 33 |
# log file should be delayed until the next logrotate run |
| 34 |
# (optional). |
| 35 |
# extension - Log files with this extension String are allowed to keep it |
| 36 |
# after rotation (optional). |
| 37 |
# ifempty - A Boolean specifying whether the log file should be rotated |
| 38 |
# even if it is empty (optional). |
| 39 |
# mail - The email address String that logs that are about to be |
| 40 |
# rotated out of existence are emailed to (optional). |
| 41 |
# mailfirst - A Boolean that when used with `mail` has logrotate email the |
| 42 |
# just rotated file rather than the about to expire file |
| 43 |
# (optional). |
| 44 |
# maillast - A Boolean that when used with `mail` has logrotate email the |
| 45 |
# about to expire file rather than the just rotated file |
| 46 |
# (optional). |
| 47 |
# maxage - The Integer maximum number of days that a rotated log file |
| 48 |
# can stay on the system (optional). |
| 49 |
# minsize - The String minimum size a log file must be to be rotated, |
| 50 |
# but not before the scheduled rotation time (optional). |
| 51 |
# The default units are bytes, append k, M or G for kilobytes, |
| 52 |
# megabytes and gigabytes respectively. |
| 53 |
# missingok - A Boolean specifying whether logrotate should ignore missing |
| 54 |
# log files or issue an error (optional). |
| 55 |
# olddir - A String path to a directory that rotated logs should be |
| 56 |
# moved to (optional). |
| 57 |
# postrotate - A command String that should be executed by /bin/sh after |
| 58 |
# the log file is rotated (optional). |
| 59 |
# prerotate - A command String that should be executed by /bin/sh before |
| 60 |
# the log file is rotated and only if it will be rotated |
| 61 |
# (optional). |
| 62 |
# firstaction - A command String that should be executed by /bin/sh once |
| 63 |
# before all log files that match the wildcard pattern are |
| 64 |
# rotated (optional). |
| 65 |
# lastaction - A command String that should be execute by /bin/sh once |
| 66 |
# after all the log files that match the wildcard pattern are |
| 67 |
# rotated (optional). |
| 68 |
# rotate - The Integer number of rotated log files to keep on disk |
| 69 |
# (optional). |
| 70 |
# rotate_every - How often the log files should be rotated as a String. |
| 71 |
# Valid values are 'day', 'week', 'month' and 'year' |
| 72 |
# (optional). |
| 73 |
# size - The String size a log file has to reach before it will be |
| 74 |
# rotated (optional). The default units are bytes, append k, |
| 75 |
# M or G for kilobytes, megabytes or gigabytes respectively. |
| 76 |
# sharedscripts - A Boolean specifying whether logrotate should run the |
| 77 |
# postrotate and prerotate scripts for each matching file or |
| 78 |
# just once (optional). |
| 79 |
# shred - A Boolean specifying whether logs should be deleted with |
| 80 |
# shred instead of unlink (optional). |
| 81 |
# shredcycles - The Integer number of times shred should overwrite log files |
| 82 |
# before unlinking them (optional). |
| 83 |
# start - The Integer number to be used as the base for the extensions |
| 84 |
# appended to the rotated log files (optional). |
| 85 |
# uncompresscmd - The String command to be used to uncompress log files |
| 86 |
# (optional). |
| 87 |
# |
| 88 |
# Examples |
| 89 |
# |
| 90 |
# # Rotate /var/log/syslog daily and keep 7 days worth of compressed logs. |
| 91 |
# logrotate::rule { 'messages': |
| 92 |
# path => '/var/log/messages', |
| 93 |
# copytruncate => true, |
| 94 |
# missingok => true, |
| 95 |
# rotate_every => 'day', |
| 96 |
# rotate => 7, |
| 97 |
# compress => true, |
| 98 |
# ifempty => true, |
| 99 |
# } |
| 100 |
# |
| 101 |
# # Rotate /var/log/nginx/access_log weekly and keep 3 weeks of logs |
| 102 |
# logrotate::rule { 'nginx_access_log': |
| 103 |
# path => '/var/log/nginx/access_log', |
| 104 |
# missingok => true, |
| 105 |
# rotate_every => 'week', |
| 106 |
# rotate => 3, |
| 107 |
# postrotate => '/etc/init.d/nginx restart', |
| 108 |
# } |
| 109 |
define logrotate::rule( |
| 110 |
$path = 'undef', |
| 111 |
$ensure = 'present', |
| 112 |
$compress = 'undef', |
| 113 |
$compresscmd = 'undef', |
| 114 |
$compressext = 'undef', |
| 115 |
$compressoptions = 'undef', |
| 116 |
$copy = 'undef', |
| 117 |
$copytruncate = 'undef', |
| 118 |
$create = 'undef', |
| 119 |
$create_mode = 'undef', |
| 120 |
$create_owner = 'undef', |
| 121 |
$create_group = 'undef', |
| 122 |
$dateext = 'undef', |
| 123 |
$dateformat = 'undef', |
| 124 |
$delaycompress = 'undef', |
| 125 |
$extension = 'undef', |
| 126 |
$ifempty = 'undef', |
| 127 |
$mail = 'undef', |
| 128 |
$mailfirst = 'undef', |
| 129 |
$maillast = 'undef', |
| 130 |
$maxage = 'undef', |
| 131 |
$minsize = 'undef', |
| 132 |
$missingok = 'undef', |
| 133 |
$olddir = 'undef', |
| 134 |
$postrotate = 'undef', |
| 135 |
$prerotate = 'undef', |
| 136 |
$firstaction = 'undef', |
| 137 |
$lastaction = 'undef', |
| 138 |
$rotate = 'undef', |
| 139 |
$rotate_every = 'undef', |
| 140 |
$size = 'undef', |
| 141 |
$sharedscripts = 'undef', |
| 142 |
$shred = 'undef', |
| 143 |
$shredcycles = 'undef', |
| 144 |
$start = 'undef', |
| 145 |
$uncompresscmd = 'undef' |
| 146 |
) { |
| 147 |
|
| 148 |
############################################################################# |
| 149 |
# SANITY CHECK VALUES |
| 150 |
|
| 151 |
if $name !~ /^[a-zA-Z0-9\._-]+$/ { |
| 152 |
fail("Logrotate::Rule[${name}]: namevar must be alphanumeric") |
| 153 |
} |
| 154 |
|
| 155 |
case $ensure { |
| 156 |
'present': { |
| 157 |
if $path == 'undef' { |
| 158 |
fail("Logrotate::Rule[${name}]: path not specified") |
| 159 |
} |
| 160 |
} |
| 161 |
'absent': {} |
| 162 |
default: { |
| 163 |
fail("Logrotate::Rule[${name}]: invalid ensure value") |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
case $compress { |
| 168 |
'undef': {} |
| 169 |
true: { $_compress = 'compress' } |
| 170 |
false: { $_compress = 'nocompress' } |
| 171 |
default: { |
| 172 |
fail("Logrotate::Rule[${name}]: compress must be a boolean") |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
case $copy { |
| 177 |
'undef': {} |
| 178 |
true: { $_copy = 'copy' } |
| 179 |
false: { $_copy = 'nocopy' } |
| 180 |
default: { |
| 181 |
fail("Logrotate::Rule[${name}]: copy must be a boolean") |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
case $copytruncate { |
| 186 |
'undef': {} |
| 187 |
true: { $_copytruncate = 'copytruncate' } |
| 188 |
false: { $_copytruncate = 'nocopytruncate' } |
| 189 |
default: { |
| 190 |
fail("Logrotate::Rule[${name}]: copytruncate must be a boolean") |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
case $create { |
| 195 |
'undef': {} |
| 196 |
true: { $_create = 'create' } |
| 197 |
false: { $_create = 'nocreate' } |
| 198 |
default: { |
| 199 |
fail("Logrotate::Rule[${name}]: create must be a boolean") |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
case $delaycompress { |
| 204 |
'undef': {} |
| 205 |
true: { $_delaycompress = 'delaycompress' } |
| 206 |
false: { $_delaycompress = 'nodelaycompress' } |
| 207 |
default: { |
| 208 |
fail("Logrotate::Rule[${name}]: delaycompress must be a boolean") |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
case $dateext { |
| 213 |
'undef': {} |
| 214 |
true: { $_dateext = 'dateext' } |
| 215 |
false: { $_dateext = 'nodateext' } |
| 216 |
default: { |
| 217 |
fail("Logrotate::Rule[${name}]: dateext must be a boolean") |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
case $mail { |
| 222 |
'undef': {} |
| 223 |
false: { $_mail = 'nomail' } |
| 224 |
default: { |
| 225 |
$_mail = "mail ${mail}" |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
case $missingok { |
| 230 |
'undef': {} |
| 231 |
true: { $_missingok = 'missingok' } |
| 232 |
false: { $_missingok = 'nomissingok' } |
| 233 |
default: { |
| 234 |
fail("Logrotate::Rule[${name}]: missingok must be a boolean") |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
case $olddir { |
| 239 |
'undef': {} |
| 240 |
false: { $_olddir = 'noolddir' } |
| 241 |
default: { |
| 242 |
$_olddir = "olddir ${olddir}" |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
case $sharedscripts { |
| 247 |
'undef': {} |
| 248 |
true: { $_sharedscripts = 'sharedscripts' } |
| 249 |
false: { $_sharedscripts = 'nosharedscripts' } |
| 250 |
default: { |
| 251 |
fail("Logrotate::Rule[${name}]: sharedscripts must be a boolean") |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
case $shred { |
| 256 |
'undef': {} |
| 257 |
true: { $_shred = 'shred' } |
| 258 |
false: { $_shred = 'noshred' } |
| 259 |
default: { |
| 260 |
fail("Logrotate::Rule[${name}]: shred must be a boolean") |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
case $ifempty { |
| 265 |
'undef': {} |
| 266 |
true: { $_ifempty = 'ifempty' } |
| 267 |
false: { $_ifempty = 'notifempty' } |
| 268 |
default: { |
| 269 |
fail("Logrotate::Rule[${name}]: ifempty must be a boolean") |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
case $rotate_every { |
| 274 |
'undef': {} |
| 275 |
'hour', 'hourly': {} |
| 276 |
'day': { $_rotate_every = 'daily' } |
| 277 |
'week': { $_rotate_every = 'weekly' } |
| 278 |
'month': { $_rotate_every = 'monthly' } |
| 279 |
'year': { $_rotate_every = 'yearly' } |
| 280 |
'daily', 'weekly','monthly','yearly': { $_rotate_every = $rotate_every } |
| 281 |
default: { |
| 282 |
fail("Logrotate::Rule[${name}]: invalid rotate_every value") |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
case $maxage { |
| 287 |
'undef': {} |
| 288 |
/^\d+$/: {} |
| 289 |
default: { |
| 290 |
fail("Logrotate::Rule[${name}]: maxage must be an integer") |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
case $minsize { |
| 295 |
'undef': {} |
| 296 |
/^\d+[kMG]?$/: {} |
| 297 |
default: { |
| 298 |
fail("Logrotate::Rule[${name}]: minsize must match /\\d+[kMG]?/") |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
case $rotate { |
| 303 |
'undef': {} |
| 304 |
/^\d+$/: {} |
| 305 |
default: { |
| 306 |
fail("Logrotate::Rule[${name}]: rotate must be an integer") |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
case $size { |
| 311 |
'undef': {} |
| 312 |
/^\d+[kMG]?$/: {} |
| 313 |
default: { |
| 314 |
fail("Logrotate::Rule[${name}]: size must match /\\d+[kMG]?/") |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
case $shredcycles { |
| 319 |
'undef': {} |
| 320 |
/^\d+$/: {} |
| 321 |
default: { |
| 322 |
fail("Logrotate::Rule[${name}]: shredcycles must be an integer") |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
case $start { |
| 327 |
'undef': {} |
| 328 |
/^\d+$/: {} |
| 329 |
default: { |
| 330 |
fail("Logrotate::Rule[${name}]: start must be an integer") |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
case $mailfirst { |
| 335 |
'undef',false: {} |
| 336 |
true: { |
| 337 |
if $maillast == true { |
| 338 |
fail("Logrotate::Rule[${name}]: Can't set both mailfirst and maillast") |
| 339 |
} |
| 340 |
|
| 341 |
$_mailfirst = 'mailfirst' |
| 342 |
} |
| 343 |
default: { |
| 344 |
fail("Logrotate::Rule[${name}]: mailfirst must be a boolean") |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
case $maillast { |
| 349 |
'undef',false: {} |
| 350 |
true: { |
| 351 |
$_maillast = 'maillast' |
| 352 |
} |
| 353 |
default: { |
| 354 |
fail("Logrotate::Rule[${name}]: maillast must be a boolean") |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if ($create_group != 'undef') and ($create_owner == 'undef') { |
| 359 |
fail("Logrotate::Rule[${name}]: create_group requires create_owner") |
| 360 |
} |
| 361 |
|
| 362 |
if ($create_owner != 'undef') and ($create_mode == 'undef') { |
| 363 |
fail("Logrotate::Rule[${name}]: create_owner requires create_mode") |
| 364 |
} |
| 365 |
|
| 366 |
if ($create_mode != 'undef') and ($create != true) { |
| 367 |
fail("Logrotate::Rule[${name}]: create_mode requires create") |
| 368 |
} |
| 369 |
|
| 370 |
############################################################################# |
| 371 |
# |
| 372 |
|
| 373 |
include logrotate::base |
| 374 |
|
| 375 |
case $rotate_every { |
| 376 |
'hour', 'hourly': { |
| 377 |
include logrotate::hourly |
| 378 |
$rule_path = "/etc/logrotate.d/hourly/${name}" |
| 379 |
} |
| 380 |
default: { |
| 381 |
$rule_path = "/etc/logrotate.d/${name}" |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
file { $rule_path: |
| 386 |
ensure => $ensure, |
| 387 |
owner => 'root', |
| 388 |
group => 'root', |
| 389 |
mode => '0444', |
| 390 |
content => template('logrotate/etc/logrotate.d/rule.erb'), |
| 391 |
require => Class['logrotate::base'], |
| 392 |
} |
| 393 |
} |