Skip to content

Commit 735f56b

Browse files
committed
Merge pull request #434 from CodeNow/SAN-4042-log-cleanup
SAN-4042; Log clean up scripts
2 parents b75f5a8 + 3f47155 commit 735f56b

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# This is to be run only for legacy logs in /var/log, not application logs in /var/log/runnable
5+
#
6+
7+
# we only want this run as root
8+
if [ "root" != `whoami` ] ; then
9+
echo "${0}: ERROR - This script needs to be run as root."
10+
exit 127
11+
fi
12+
13+
# legacy log path
14+
logdir=/var/log
15+
# store log archives here, purge manually
16+
archdir=/docker/archive
17+
datetime=`date +%Y%m%d%H%m`
18+
19+
# these logfiles haven't been modified in > 24 hours, so moving them without cleanup up filehandles first should be OK:
20+
echo "Compressing logs > 24h"
21+
find "${logdir}" -maxdepth 2 -type f -mmin +1440 -name '*.log' -exec bzip2 -9 {} \; -print
22+
echo "Restarting rsyslogd"
23+
# but we do need to clean the filehandles after, just in case
24+
service rsyslog restart
25+
26+
# archive anything > 6h
27+
echo "Archiving logs > 6h"
28+
mkdir -p "${archdir}" 2>&1
29+
find "${logdir}" -maxdepth 2 -type f -mtime +6 -name '*z' | xargs tar jcvpf "${archdir}"/log-archive-"${datetime}".tbz
30+
echo "Purging logs > 1wk"
31+
find "${logdir}" -maxdepth 2 -type f -mtime +6 -exec rm -f {} \; -print
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# This is to be run logs in {{ app_log_dir }}.
5+
# Runs from crontab.
6+
#
7+
8+
logdir="${1}"
9+
10+
# We can compress anything older than 6 hours
11+
find "${logdir}" -mindepth 2 -type f -mmin +360 -name '*.log' -exec bzip2 -9 {} \;
12+
13+
# We automatically purge anything > 1wk
14+
find "${logdir}" -maxdepth 2 -type f -mtime +7 -exec rm -f {} \;

ansible/roles/loggly/tasks/main.yml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
recurse=yes
8989

9090
- name: restart rsyslog
91-
tags: [loggly, deploy]
91+
tags: [ loggly, deploy ]
9292
become: true
9393
service: name=rsyslog state=restarted
9494

@@ -99,3 +99,37 @@
9999
minute="*/2"
100100
job="echo 'loggly monitoring' 2>&1 | /usr/bin/logger -t LogMonitor"
101101
state=present
102+
103+
- name: runnable bin directory
104+
tags: [ loggly, clean ]
105+
become: true
106+
file:
107+
path=/opt/runnable/bin
108+
state=directory
109+
owner=ubuntu
110+
group=ubuntu
111+
mode=0700
112+
113+
- name: copy log purge script
114+
tags: [ loggly, clean ]
115+
copy:
116+
src=purgeLogs.sh
117+
dest=/opt/runnable/bin/purgeLogs.sh
118+
owner=ubuntu
119+
group=ubuntu
120+
mode=0700
121+
122+
- name: purge log files
123+
tags: [ loggly, clean ]
124+
become: true
125+
cron:
126+
name="purge log files"
127+
minute=0
128+
job="/opt/runnable/bin/purgeLogs.sh {{ app_log_dir }}"
129+
state=present
130+
131+
- name: clean legacy log files
132+
tags: [ loggly, clean ]
133+
become: true
134+
script: archiveOldLogs.sh
135+
register: purge_out

0 commit comments

Comments
 (0)