-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogentries-dailydownload
executable file
·168 lines (148 loc) · 4.36 KB
/
logentries-dailydownload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
# ****************************************************************************
# .:: LogEntries Downloader - Daily Downloader ::.
#
# Author: Sylvain Mauduit (@Swop)
# Company: Simple IT - http://www.simple-it.fr
#
# Usage:
# $ logentries-dailydownload [-d logs_dir] [-z] [-s] [-m] [-p prefix]
#
# ****************************************************************************
# Check if dependencies are installed
DEPENDANCIES="readlink"
for DEPT in $DEPENDANCIES; do
command -v $DEPT >/dev/null 2>&1 || { echo "'$DEPT' is required but it's not installed. Aborting." >&2; exit 1; }
done
SCRIPT_PATH=$(dirname `readlink -m "$0"`)
# Print program usage
function usage()
{
cat << EOF
usage: $0 options
Download the previous daylogs from Logentries, and organize logs into clean
date-style folder architecture.
OPTIONS:
-h Show this message
-d logs_dir Directory to use for log storage
(default: /var/log/logentries)
-m Create root logs_dir if absent
-p prefix Prefix to use in logfile name (default: app)
-z Compress logfiles
-s Create a symlink to previous day logs, in logs folder root.
EOF
}
function log()
{
echo "`date --iso-860=seconds` $1"
}
function logerr()
{
echo "`date --iso-860=seconds` $1" >&2
}
log "Start download of logs"
# Parse arguments
LOGS_DIRECTORY=/var/log/logentries
PREFIX=app
MKDIR=false
COMPRESS_LOGS=false
CREATE_SYMLINK=false
while getopts “hd:zmp:s” OPTION
do
case $OPTION in
h)
usage
exit 0
;;
d)
LOGS_DIRECTORY=$(readlink -m "$OPTARG")
;;
z)
command -v gzip >/dev/null 2>&1 || { echo "'gzip' is required but it's not installed. Aborting." >&2; exit 1; }
COMPRESS_LOGS=true
;;
m)
MKDIR=true
;;
p)
PREFIX=$OPTARG
;;
s)
CREATE_SYMLINK=true
;;
?)
usage
exit 1
;;
esac
done
# Check the validity of logs directory path
if [[ -d $LOGS_DIRECTORY ]]; then
if [[ ! -w $LOGS_DIRECTORY ]]; then
logerr "Logs directory must have write permission. Aborting."
exit 1
fi
else
if $MKDIR; then
mkdir $LOGS_DIRECTORY
if [ $? -ne 0 ]; then
logerr "Error during creation of root log directory. Aborting."
exit 1
fi
else
logerr "Logs directory doesn't exists. Aborting."
exit 1
fi
fi
# Symlink can't be created if logs are compressed
if $CREATE_SYMLINK && $COMPRESS_LOGS; then
logerr "Symlink creation can't be coupled with log compression. Aborting."
exit 1
fi
YESTERDAY_YEAR=$(date --utc -d "`date +%D` -1day" +%Y)
YESTERDAY_MONTH=$(date --utc -d "`date +%D` -1day" +%m)
YESTERDAY_DAY=$(date --utc -d "`date +%D` -1day" +%d)
LOGFILE_NAME="${PREFIX}_${YESTERDAY_DAY}.log"
LOGFILE_PARENT_PATH=${LOGS_DIRECTORY}/${YESTERDAY_YEAR}/${YESTERDAY_MONTH}
LOGFILE_PATH=${LOGFILE_PARENT_PATH}/${LOGFILE_NAME}
# Create folder architecture
mkdir -p $LOGFILE_PARENT_PATH
if [ $? -ne 0 ]; then
logerr "Error during creation of directory architecture. Aborting."
exit 1
fi
# Check if target log file can be write
if [ -f $LOGFILE_PATH ] && [ ! -w $LOGFILE_PATH ]; then
logerr "The log file '$LOGFILE_NAME' already exists and is not writable. Aborting."
exit 1
fi
# Get the logs
${SCRIPT_PATH}/logentries-downloader -y -q > $LOGFILE_PATH
if [ $? -ne 0 ]; then
logerr "Error during log fetching. Aborting."
exit 1
fi
# Compress logs
if $COMPRESS_LOGS; then
gzip -f $LOGFILE_PATH
if [ $? -ne 0 ]; then
logerr "Error during log compression. Aborting."
exit 1
fi
fi
# Create symlink
if $CREATE_SYMLINK; then
if [[ ! -L ${LOGS_DIRECTORY}/${PREFIX}_yesterday.log || \
! -w ${LOGS_DIRECTORY}/${PREFIX}_yesterday.log || \
! -w ${LOGS_DIRECTORY} ]]; then
logerr "Symlink could not be created (a previous file with the \
same name already exists and could not be overwritten). Aborting."
exit 1
fi
rm -rf ${LOGS_DIRECTORY}/${PREFIX}_yesterday.log
ln -s $LOGFILE_PATH ${LOGS_DIRECTORY}/${PREFIX}_yesterday.log
if [ $? -ne 0 ]; then
logerr "Error during symlink creation. Aborting."
exit 1
fi
fi