-
Notifications
You must be signed in to change notification settings - Fork 2
/
fail2map-merge.sh
323 lines (272 loc) · 10.3 KB
/
fail2map-merge.sh
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/bin/bash
set -f
##############################################################################
# Script-Name : fail2map-merge.sh #
# Description : Merge various places.geojson file to one. #
# #
# #
# #
# #
# #
# Last update : 25.11.2016 #
# Version : 1.00 #
# #
# Author : Klaus Tachtler, <[email protected]> #
# DokuWiki : http://www.dokuwiki.tachtler.net #
# Homepage : http://www.tachtler.net #
# #
# +----------------------------------------------------------------------+ #
# | This program is free software; you can redistribute it and/or modify | #
# | it under the terms of the GNU General Public License as published by | #
# | the Free Software Foundation; either version 2 of the License, or | #
# | (at your option) any later version. | #
# +----------------------------------------------------------------------+ #
# #
# Copyright (c) 2016 by Klaus Tachtler. #
# #
#############################################################################
##############################################################################
# H I S T O R Y #
##############################################################################
# Version : x.xx #
# Description : <Description> #
# -------------------------------------------------------------------------- #
# Version : x.xx #
# Description : <Description> #
# -------------------------------------------------------------------------- #
##############################################################################
# Source function library.
. /etc/init.d/functions
# Variable declarations.
##############################################################################
# >>> Please edit following lines for personal command and/or configuration! #
##############################################################################
# CUSTOM - Script-Name.
SCRIPT_NAME='fail2map-merge.sh'
# CUSTOM - PATH/FILE/PARAMETER variables.
FAIL2MAP_FILE_MERGED="/var/www/fail2map/places.geojson"
FAIL2MAP_FILE_LIST="/var/www/fail2map/server1/file1.places.geojson,/var/www/fail2map/server2/file2.places.geojson,/var/www/fail2map/server3/file3.places.geojson"
# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='[email protected]'
# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
##############################################################################
# >>> Normaly there is no need to change anything below this comment line. ! #
##############################################################################
# Variables.
TOUCH_COMMAND=`command -v touch`
RM_COMMAND=`command -v rm`
CAT_COMMAND=`command -v cat`
DATE_COMMAND=`command -v date`
PROG_SENDMAIL=`command -v sendmail`
TAIL_COMMAND=`command -v tail`
HEAD_COMMAND=`command -v head`
SED_COMMAND=`command -v sed`
FILE_LOCK='/tmp/'$SCRIPT_NAME'.lock'
FILE_LOG='/var/log/'$SCRIPT_NAME'.log'
FILE_LAST_LOG='/tmp/'$SCRIPT_NAME'.log'
FILE_MAIL='/tmp/'$SCRIPT_NAME'.mail'
VAR_HOSTNAME=`uname -n`
VAR_SENDER='root@'$VAR_HOSTNAME
VAR_EMAILDATE=`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'`
# Functions.
function log() {
echo $1
echo `$DATE_COMMAND '+%Y/%m/%d %H:%M:%S'` " INFO:" $1 >>${FILE_LAST_LOG}
}
function retval() {
if [ "$?" != "0" ]; then
case "$?" in
*)
log "ERROR: Unknown error $?"
;;
esac
fi
}
function movelog() {
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_LOG
$RM_COMMAND -f $FILE_LAST_LOG
$RM_COMMAND -f $FILE_LOCK
}
function sendmail() {
case "$1" in
'STATUS')
MAIL_SUBJECT='Status execution '$SCRIPT_NAME' script.'
;;
*)
MAIL_SUBJECT='ERROR while execution '$SCRIPT_NAME' script !!!'
;;
esac
$CAT_COMMAND <<MAIL >$FILE_MAIL
Subject: $MAIL_SUBJECT
Date: $VAR_EMAILDATE
From: $VAR_SENDER
To: $MAIL_RECIPIENT
MAIL
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_MAIL
$PROG_SENDMAIL -f $VAR_SENDER -t $MAIL_RECIPIENT < $FILE_MAIL
$RM_COMMAND -f $FILE_MAIL
}
# Main.
log ""
log "+-----------------------------------------------------------------+"
log "| Start copying the fail2map places.geojson file to the fail2map. |"
log "+-----------------------------------------------------------------+"
log ""
log "Run script with following parameter:"
log ""
log "SCRIPT_NAME...........: $SCRIPT_NAME"
log ""
log "FAIL2MAP_FILE_MERGED..: $FAIL2MAP_FILE_MERGED"
log "FAIL2MAP_FILE_LIST....: $FAIL2MAP_FILE_LIST"
log ""
log "MAIL_RECIPIENT........: $MAIL_RECIPIENT"
log "MAIL_STATUS...........: $MAIL_STATUS"
log ""
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$TOUCH_COMMAND" ]; then
log "Check if command '$TOUCH_COMMAND' was found....................[FAILED]"
sendmail ERROR
movelog
exit 10
else
log "Check if command '$TOUCH_COMMAND' was found....................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$RM_COMMAND" ]; then
log "Check if command '$RM_COMMAND' was found.......................[FAILED]"
sendmail ERROR
movelog
exit 11
else
log "Check if command '$RM_COMMAND' was found.......................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CAT_COMMAND" ]; then
log "Check if command '$CAT_COMMAND' was found......................[FAILED]"
sendmail ERROR
movelog
exit 12
else
log "Check if command '$CAT_COMMAND' was found......................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$DATE_COMMAND" ]; then
log "Check if command '$DATE_COMMAND' was found.....................[FAILED]"
sendmail ERROR
movelog
exit 13
else
log "Check if command '$DATE_COMMAND' was found.....................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$PROG_SENDMAIL" ]; then
log "Check if command '$PROG_SENDMAIL' was found................[FAILED]"
sendmail ERROR
movelog
exit 14
else
log "Check if command '$PROG_SENDMAIL' was found................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$TAIL_COMMAND" ]; then
log "Check if command '$TAIL_COMMAND' was found.....................[FAILED]"
sendmail ERROR
movelog
exit 15
else
log "Check if command '$TAIL_COMMAND' was found.....................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$HEAD_COMMAND" ]; then
log "Check if command '$HEAD_COMMAND' was found.....................[FAILED]"
sendmail ERROR
movelog
exit 16
else
log "Check if command '$HEAD_COMMAND' was found.....................[ OK ]"
fi
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$SED_COMMAND" ]; then
log "Check if command '$SED_COMMAND' was found......................[FAILED]"
sendmail ERROR
movelog
exit 17
else
log "Check if command '$SED_COMMAND' was found......................[ OK ]"
fi
# Check if LOCK file NOT exist.
if [ ! -e "$FILE_LOCK" ]; then
log "Check if script is NOT already runnig .....................[ OK ]"
$TOUCH_COMMAND $FILE_LOCK
else
log "Check if script is NOT already runnig .....................[FAILED]"
log ""
log "ERROR: The script was already running, or LOCK file already exists!"
log ""
sendmail ERROR
movelog
exit 20
fi
# Start update.
log ""
log "+-----------------------------------------------------------------+"
log "| Run execute of $SCRIPT_NAME ............................. |"
log "+-----------------------------------------------------------------+"
log ""
if [ -f $FAIL2MAP_FILE_MERGED ] ; then
$RM_COMMAND $FAIL2MAP_FILE_MERGED
if [ "$?" != 0 ]; then
retval $?
log "Delete '$FAIL2MAP_FILE_MERGED' .................[FAILED]"
$RM_COMMAND -f $FILE_LOCK
sendmail ERROR
movelog
exit 50
else
log "Delete '$FAIL2MAP_FILE_MERGED' .................[ OK ]"
fi
else
log "Delete '$FAIL2MAP_FILE_MERGED' .................[NOTFND]"
fi
# Determine files.
array=(${FAIL2MAP_FILE_LIST//,/ })
# Add HEADER
$CAT_COMMAND <<FAIL2BANFILEMERGEHEADER >$FAIL2MAP_FILE_MERGED
{
"features": [
FAIL2BANFILEMERGEHEADER
for i in "${!array[@]}"
do
if [ -s "${array[i]}" ]
then
$TAIL_COMMAND -n +3 ${array[i]} | $HEAD_COMMAND -n -4 >>$FAIL2MAP_FILE_MERGED
$CAT_COMMAND <<FAIL2BANFILEMERGESEPARATOR >>$FAIL2MAP_FILE_MERGED
},
FAIL2BANFILEMERGESEPARATOR
fi
done
# Delete last }, separator
$SED_COMMAND -i '$ d' $FAIL2MAP_FILE_MERGED
# Add FOOTER
$CAT_COMMAND <<FAIL2BANFILEMERGEFOOTER >>$FAIL2MAP_FILE_MERGED
}
],
"type": "FeatureCollection"
}
FAIL2BANFILEMERGEFOOTER
log ""
log "Create '$FAIL2MAP_FILE_MERGED' .................[ OK ]"
# Finish execute.
log ""
log "+-----------------------------------------------------------------+"
log "| End execute of $SCRIPT_NAME ............................. |"
log "+-----------------------------------------------------------------+"
log ""
# Status e-mail.
if [ $MAIL_STATUS = 'Y' ]; then
sendmail STATUS
fi
# Move temporary log to permanent log
movelog
exit 0