Add custom logrotate config for Apache
Apache package provides logrotate script which starts in the same
time on different nodes. Since reload of Apache is needed after log
compression on multinode environment the race is possible when all
nodes
simultaneously reload apache. This leads to small service downtime,
more details may be found in related prod.
Starting with this patch formula will manage logrotate conf for
apache.
It also adds randomness to the apache reload. The random start/end
periods are configurable via the following pillar
apache:
server:
logrotate:
start_period: 600
end_period: 1200
Change-Id: I8c37c4392db0c4aaf7659be52e94652b4b63d703
Related-Prod: PROD-19346
diff --git a/README.rst b/README.rst
index b4b7220..d31caed 100644
--- a/README.rst
+++ b/README.rst
@@ -209,6 +209,17 @@
- mail01.example.com
- mail01
+Logrotate settings which allow you to rotate the logs in
+a random time in a given time interval. Time in seconds
+
+.. code-block:: yaml
+
+ apache:
+ server:
+ logrotate:
+ start_period: 600
+ end_period: 1200
+
More Information
================
diff --git a/apache/map.jinja b/apache/map.jinja
index ab062ec..8d89577 100644
--- a/apache/map.jinja
+++ b/apache/map.jinja
@@ -16,6 +16,10 @@
'www_dir': '/var/www',
'service_user': 'www-data',
'service_group': 'www-data',
+ 'logrotate': {
+ 'start_period': '1',
+ 'end_period': '3600',
+ },
},
'Arch': {
'pkgs': ['apache'],
@@ -75,6 +79,10 @@
'www_dir': '/var/www',
'service_user': 'www-data',
'service_group': 'www-data',
+ 'logrotate': {
+ 'start_period': '1',
+ 'end_period': '3600',
+ },
},
'xenial': {
'pkgs': ['apache2'],
@@ -93,6 +101,10 @@
'www_dir': '/var/www',
'service_user': 'www-data',
'service_group': 'www-data',
+ 'logrotate': {
+ 'start_period': '1',
+ 'end_period': '3600',
+ },
},
}, grain='oscodename', merge=salt['pillar.get']('apache:server'))) %}
diff --git a/apache/meta/logrotate.yml b/apache/meta/logrotate.yml
new file mode 100644
index 0000000..24135e6
--- /dev/null
+++ b/apache/meta/logrotate.yml
@@ -0,0 +1,22 @@
+{%- from "apache/map.jinja" import server with context %}
+job:
+ apache2:
+ - files:
+ - /var/log/apache2/*.log
+ options:
+ - daily
+ - missingok
+ - rotate: 14
+ - compress
+ - delaycompress
+ - notifempty
+ - sharedscripts
+ - prerotate:
+ RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 ));
+ RANDOM_START={{ server.logrotate.start_period }};
+ RANDOM_END={{ server.logrotate.end_period }};
+ RANGE=$(( $RANDOM_END - $RANDOM_START ));
+ RESULT=$(( $RANDOM % $RANGE));
+ RESULT=$(( $RESULT + $RANDOM_START ));
+ sleep $RESULT
+ - postrotate: "if /etc/init.d/apache2 status > /dev/null; then /etc/init.d/apache2 reload > /dev/null; fi"
\ No newline at end of file