Skip to content

Commit ecf4bb0

Browse files
committed
add option for less verbose logging
* adds -s command line switch to update-ngxblocker / install-ngxblocker for less verbose messages to reduce unnecessary cron job logs. -s reduces messages to errors only & the Blacklist version number.
1 parent f1a26fc commit ecf4bb0

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

install-ngxblocker

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ Usage: $script [OPTIONS]
5050
[ -s ] : Script directory (default: $SCRIPT_DIR)
5151
[ -r ] : Change repo url (default: $REPO)
5252
[ -x ] : Actually change the files (default: don't change anything)
53+
[ -s ] : Suppress non error messages
5354
[ -v ] : Print blacklist version
5455
[ -h ] : this help message
5556
5657
Examples:
5758
$script (Don't change anything: display results on stdout)
5859
$script -x (Download / update config files)
60+
$script -s (Less verbose messages for cron)
5961
EOF
6062
exit 0
6163
}
@@ -137,7 +139,7 @@ download_files() {
137139
fi
138140
done
139141
else
140-
printf "Nothing to update for directory: $local_dir\n"
142+
print_message "Nothing to update for directory: $local_dir\n"
141143
fi
142144
}
143145

@@ -146,7 +148,7 @@ set_mode() {
146148
local file_list="$(echo $@ | awk '{$1=$2=""; print}' | sed -e 's/^[ \t]*//')"
147149

148150
for file in $file_list; do
149-
printf "Setting mode: $mode => $dir/$file\n"
151+
print_message "Setting mode: $mode => $dir/$file\n"
150152
chmod $mode $dir/$file
151153
done
152154
}
@@ -193,10 +195,18 @@ check_args() {
193195
esac
194196
}
195197

198+
print_message() {
199+
local msg="$@"
200+
201+
if [ "$VERBOSE" != "N" ]; then
202+
printf "$msg"
203+
fi
204+
}
205+
196206
get_options() {
197207
local arg= opts=
198208

199-
while getopts :b:c:s:r:xvh opts "$@"
209+
while getopts :b:c:s:r:xvsh opts "$@"
200210
do
201211
if [ -n "${OPTARG}" ]; then
202212
case "$opts" in
@@ -212,6 +222,7 @@ get_options() {
212222
r) REPO=$arg; check_args $opts url $arg ;;
213223
x) DRY_RUN=N ;;
214224
v) check_version ;;
225+
s) VERBOSE=N ;;
215226
h) usage ;;
216227
\?) usage ;;
217228
:) check_args $OPTARG none none ;;
@@ -264,7 +275,7 @@ main() {
264275
include_url=$REPO/include_filelist.txt
265276

266277
# check repo is online & source includes
267-
printf "Checking url: $include_url\n"
278+
print_message "Checking url: $include_url\n"
268279
if [ -n "$(check_online $include_url)" ]; then
269280
local tmp=$(mktemp)
270281
wget -q $include_url -O $tmp

update-ngxblocker

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Usage: $script [OPTIONS]
5656
[ -e ] : Change @email address (default: $EMAIL)
5757
[ -m ] : Change mail (system alias) (default: $EMAIL)
5858
[ -n ] : Do not send email report (default: $SEND_EMAIL)
59+
[ -s ] : Suppress non error messages
5960
[ -v ] : Print blacklist version
6061
[ -h ] : this help message
6162
@@ -65,6 +66,7 @@ Examples:
6566
$script -b /my/custom/bots.d (Download globalblacklist.conf & update with your custom bots.d location)
6667
$script -e yourname@youremailaddress.com (Download globalblacklist.conf specifying your email address for the notification)
6768
$script -u /path/to/install-ngxblocker (Use custom path to install-ngxblocker to update bots.d / conf.d include files)
69+
$script -s -m webmaster (Send mail to a system alias address & give less verbose messages for cron)
6870
EOF
6971
exit 0
7072
}
@@ -77,21 +79,22 @@ check_version() {
7779
# local version
7880
version=$(grep "Version:" $file | sed 's|^.*: V||g')
7981
date=$(grep "Updated:" $file | sed 's|^.*: ||g')
80-
printf "\nLOCAL Version: $BOLDWHITE$version$RESET\n"
81-
printf "Updated: $date\n\n"
82+
print_message "\nLOCAL Version: $BOLDWHITE$version$RESET\n"
83+
print_message "Updated: $date\n\n"
8284

8385
# remote version
8486
curl -s --limit-rate 5k -r $range --location $url -o $tmp
8587
remote_ver=$(grep "Version:" $tmp | sed 's|^.*: V||g')
8688
remote_date=$(grep "Updated:" $tmp | sed 's|^.*: ||g')
87-
printf "REMOTE Version: $BOLDWHITE$remote_ver$RESET\n"
88-
printf "Updated: $remote_date\n"
89+
print_message "REMOTE Version: $BOLDWHITE$remote_ver$RESET\n"
90+
print_message "Updated: $remote_date\n"
8991
rm -f $tmp
9092

9193
if [ "$version" != "$remote_ver" ]; then
92-
printf "\nUpdate Available => $BOLDMAGENTA$remote_ver$RESET\n\n"
94+
print_message "\nUpdate Available => $BOLDMAGENTA$remote_ver$RESET\n\n"
95+
return 1
9396
else
94-
printf "\nLatest Blacklist Already Installed: $BOLDGREEN$version$RESET\n\n"
97+
print_message "\nLatest Blacklist Already Installed: $BOLDGREEN$version$RESET\n\n"
9598
fi
9699
else
97100
printf "${BOLDRED}ERROR${RESET}: Missing '$file' => ${BOLDWHITE}running $INSTALLER:${RESET}\n"
@@ -100,8 +103,6 @@ check_version() {
100103
check_version
101104
fi
102105
fi
103-
104-
exit 0
105106
}
106107

107108
check_dirs() {
@@ -215,10 +216,18 @@ check_depends() {
215216
fi
216217
}
217218

219+
print_message() {
220+
local msg="$@"
221+
222+
if [ "$VERBOSE" != "N" ]; then
223+
printf "$msg"
224+
fi
225+
}
226+
218227
get_options() {
219228
local arg= opts=
220229

221-
while getopts :c:b:u:r:e:m:nvh opts "$@"
230+
while getopts :c:b:u:r:e:m:nvsh opts "$@"
222231
do
223232
if [ -n "${OPTARG}" ]; then
224233
case "$opts" in
@@ -237,6 +246,7 @@ get_options() {
237246
m) EMAIL=$arg ;; # /etc/aliases no sanity checks
238247
n) SEND_EMAIL=N ;;
239248
v) check_depends; check_version ;;
249+
s) export VERBOSE=N ;;
240250
h) usage ;;
241251
\?) usage ;;
242252
:) check_args $OPTARG none none ;;
@@ -267,25 +277,25 @@ main() {
267277
output=$CONF_DIR/$file
268278

269279
# check for updated blacklist
270-
update=$(check_version | tail -n 2)
271-
printf "\n$update\n\n"
280+
check_version
281+
update=$?
272282

273-
if echo $update | grep ^Update 1>/dev/null; then
283+
if [ $update = 1 ]; then
274284

275285
# download globalblacklist update
276286
tmp=$(mktemp)
277287
mkdir -p $CONF_DIR
278-
printf "${BOLDWHITE}Downloading: $file "
288+
local dl_msg="${BOLDWHITE}Downloading: $file "
279289
curl --fail --connect-timeout 60 --retry 10 --retry-delay 5 -so $tmp $url
280290
retval=$?
281291

282292
case "$retval" in
283-
0) printf "...${BOLDGREEN}OK${RESET}\n\n"
293+
0) print_message "$dl_msg...${BOLDGREEN}[OK]${RESET}\n\n"
284294
mv $tmp $output
285295
;;
286-
22) printf "...${BOLDRED}ERROR 404: $url${RESET}\n\n";;
287-
28) printf "...${BOLDRED}ERROR TIMEOUT: $url${RESET}\n\n";;
288-
*) printf "...${BOLDRED}ERROR CURL: ($retval){RESET}\n\n";;
296+
22) printf "$dl_msg...${BOLDRED}ERROR 404: $url${RESET}\n\n";;
297+
28) printf "$dl_msg...${BOLDRED}ERROR TIMEOUT: $url${RESET}\n\n";;
298+
*) printf "$dl_msg...${BOLDRED}ERROR CURL: ($retval){RESET}\n\n";;
289299
esac
290300

291301
# download new bots.d / conf.d files
@@ -297,13 +307,16 @@ main() {
297307
# re-read nginx configuration
298308
if [ $retval = 0 ]; then
299309

300-
$service nginx reload
310+
$service nginx reload 2>&1 >/dev/null
301311
if [ $? = 0 ]; then
302312
status="${BOLDGREEN}[OK]${RESET}"
313+
print_message "\nReloading NGINX configuration...$status\n"
314+
303315
else
304316
status="${BOLDRED}[FAILED]${RESET}"
317+
printf "\nReloading NGINX configuration...$status\n"
318+
305319
fi
306-
printf "\nReloading NGINX configuration...$status\n"
307320
else
308321
printf "\n${BOLDRED}Download failed${RESET}: not reloading NGINX config\n"
309322
fi
@@ -315,7 +328,10 @@ main() {
315328
# email report
316329
check_mail_depends
317330
case "$SEND_EMAIL" in
318-
y*|Y*)printf "Emailing report to: ${BOLDWHITE}$EMAIL${RESET}\n\n";
331+
y*|Y*)if [ "$VERBOSE" = "N" ]; then
332+
grep "Version:" $CONF_DIR/globalblacklist.conf | tr -d '#'
333+
fi
334+
print_message "Emailing report to: ${BOLDWHITE}$EMAIL${RESET}\n\n";
319335
# remove ansi colour codes
320336
sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
321337
cat $EMAIL_REPORT | mail -s "Nginx Bad Bot Blocker Updated" $EMAIL
@@ -338,5 +354,8 @@ exit $?
338354
# Here's another example to run it daily at midday using a command line switch to set the email address for the notification
339355
# 00 12 * * * sudo /usr/sbin/update-ngxblocker -e yourname@youremailprovider.com
340356

357+
# Less verbose logging to a system alias mail address (root crontab)
358+
# 00 12 * * * /usr/sbin/update-ngxblocker -s -m webmaster
359+
341360
# better logging for cron jobs:
342361
# https://serverfault.com/questions/137468/better-logging-for-cronjobs-send-cron-output-to-syslog

0 commit comments

Comments
 (0)