Merge pull request #119 from itoffshore/alpine

add option for less verbose logging
This commit is contained in:
Mitchell Krog 2018-01-07 10:07:24 +02:00 committed by GitHub
commit a73ae34285
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 24 deletions

View file

@ -50,12 +50,14 @@ Usage: $script [OPTIONS]
[ -s ] : Script directory (default: $SCRIPT_DIR) [ -s ] : Script directory (default: $SCRIPT_DIR)
[ -r ] : Change repo url (default: $REPO) [ -r ] : Change repo url (default: $REPO)
[ -x ] : Actually change the files (default: don't change anything) [ -x ] : Actually change the files (default: don't change anything)
[ -s ] : Suppress non error messages
[ -v ] : Print blacklist version [ -v ] : Print blacklist version
[ -h ] : this help message [ -h ] : this help message
Examples: Examples:
$script (Don't change anything: display results on stdout) $script (Don't change anything: display results on stdout)
$script -x (Download / update config files) $script -x (Download / update config files)
$script -s (Less verbose messages for cron)
EOF EOF
exit 0 exit 0
} }
@ -137,7 +139,7 @@ download_files() {
fi fi
done done
else else
printf "Nothing to update for directory: $local_dir\n" print_message "Nothing to update for directory: $local_dir\n"
fi fi
} }
@ -146,7 +148,7 @@ set_mode() {
local file_list="$(echo $@ | awk '{$1=$2=""; print}' | sed -e 's/^[ \t]*//')" local file_list="$(echo $@ | awk '{$1=$2=""; print}' | sed -e 's/^[ \t]*//')"
for file in $file_list; do for file in $file_list; do
printf "Setting mode: $mode => $dir/$file\n" print_message "Setting mode: $mode => $dir/$file\n"
chmod $mode $dir/$file chmod $mode $dir/$file
done done
} }
@ -193,10 +195,18 @@ check_args() {
esac esac
} }
print_message() {
local msg="$@"
if [ "$VERBOSE" != "N" ]; then
printf "$msg"
fi
}
get_options() { get_options() {
local arg= opts= local arg= opts=
while getopts :b:c:s:r:xvh opts "$@" while getopts :b:c:s:r:xvsh opts "$@"
do do
if [ -n "${OPTARG}" ]; then if [ -n "${OPTARG}" ]; then
case "$opts" in case "$opts" in
@ -212,6 +222,7 @@ get_options() {
r) REPO=$arg; check_args $opts url $arg ;; r) REPO=$arg; check_args $opts url $arg ;;
x) DRY_RUN=N ;; x) DRY_RUN=N ;;
v) check_version ;; v) check_version ;;
s) VERBOSE=N ;;
h) usage ;; h) usage ;;
\?) usage ;; \?) usage ;;
:) check_args $OPTARG none none ;; :) check_args $OPTARG none none ;;
@ -264,7 +275,7 @@ main() {
include_url=$REPO/include_filelist.txt include_url=$REPO/include_filelist.txt
# check repo is online & source includes # check repo is online & source includes
printf "Checking url: $include_url\n" print_message "Checking url: $include_url\n"
if [ -n "$(check_online $include_url)" ]; then if [ -n "$(check_online $include_url)" ]; then
local tmp=$(mktemp) local tmp=$(mktemp)
wget -q $include_url -O $tmp wget -q $include_url -O $tmp

View file

@ -56,6 +56,7 @@ Usage: $script [OPTIONS]
[ -e ] : Change @email address (default: $EMAIL) [ -e ] : Change @email address (default: $EMAIL)
[ -m ] : Change mail (system alias) (default: $EMAIL) [ -m ] : Change mail (system alias) (default: $EMAIL)
[ -n ] : Do not send email report (default: $SEND_EMAIL) [ -n ] : Do not send email report (default: $SEND_EMAIL)
[ -s ] : Suppress non error messages
[ -v ] : Print blacklist version [ -v ] : Print blacklist version
[ -h ] : this help message [ -h ] : this help message
@ -65,6 +66,7 @@ Examples:
$script -b /my/custom/bots.d (Download globalblacklist.conf & update with your custom bots.d location) $script -b /my/custom/bots.d (Download globalblacklist.conf & update with your custom bots.d location)
$script -e yourname@youremailaddress.com (Download globalblacklist.conf specifying your email address for the notification) $script -e yourname@youremailaddress.com (Download globalblacklist.conf specifying your email address for the notification)
$script -u /path/to/install-ngxblocker (Use custom path to install-ngxblocker to update bots.d / conf.d include files) $script -u /path/to/install-ngxblocker (Use custom path to install-ngxblocker to update bots.d / conf.d include files)
$script -s -m webmaster (Send mail to a system alias address & give less verbose messages for cron)
EOF EOF
exit 0 exit 0
} }
@ -77,21 +79,22 @@ check_version() {
# local version # local version
version=$(grep "Version:" $file | sed 's|^.*: V||g') version=$(grep "Version:" $file | sed 's|^.*: V||g')
date=$(grep "Updated:" $file | sed 's|^.*: ||g') date=$(grep "Updated:" $file | sed 's|^.*: ||g')
printf "\nLOCAL Version: $BOLDWHITE$version$RESET\n" print_message "\nLOCAL Version: $BOLDWHITE$version$RESET\n"
printf "Updated: $date\n\n" print_message "Updated: $date\n\n"
# remote version # remote version
curl -s --limit-rate 5k -r $range --location $url -o $tmp curl -s --limit-rate 5k -r $range --location $url -o $tmp
remote_ver=$(grep "Version:" $tmp | sed 's|^.*: V||g') remote_ver=$(grep "Version:" $tmp | sed 's|^.*: V||g')
remote_date=$(grep "Updated:" $tmp | sed 's|^.*: ||g') remote_date=$(grep "Updated:" $tmp | sed 's|^.*: ||g')
printf "REMOTE Version: $BOLDWHITE$remote_ver$RESET\n" print_message "REMOTE Version: $BOLDWHITE$remote_ver$RESET\n"
printf "Updated: $remote_date\n" print_message "Updated: $remote_date\n"
rm -f $tmp rm -f $tmp
if [ "$version" != "$remote_ver" ]; then if [ "$version" != "$remote_ver" ]; then
printf "\nUpdate Available => $BOLDMAGENTA$remote_ver$RESET\n\n" print_message "\nUpdate Available => $BOLDMAGENTA$remote_ver$RESET\n\n"
return 1
else else
printf "\nLatest Blacklist Already Installed: $BOLDGREEN$version$RESET\n\n" print_message "\nLatest Blacklist Already Installed: $BOLDGREEN$version$RESET\n\n"
fi fi
else else
printf "${BOLDRED}ERROR${RESET}: Missing '$file' => ${BOLDWHITE}running $INSTALLER:${RESET}\n" printf "${BOLDRED}ERROR${RESET}: Missing '$file' => ${BOLDWHITE}running $INSTALLER:${RESET}\n"
@ -100,8 +103,6 @@ check_version() {
check_version check_version
fi fi
fi fi
exit 0
} }
check_dirs() { check_dirs() {
@ -215,10 +216,18 @@ check_depends() {
fi fi
} }
print_message() {
local msg="$@"
if [ "$VERBOSE" != "N" ]; then
printf "$msg"
fi
}
get_options() { get_options() {
local arg= opts= local arg= opts=
while getopts :c:b:u:r:e:m:nvh opts "$@" while getopts :c:b:u:r:e:m:nvsh opts "$@"
do do
if [ -n "${OPTARG}" ]; then if [ -n "${OPTARG}" ]; then
case "$opts" in case "$opts" in
@ -237,6 +246,7 @@ get_options() {
m) EMAIL=$arg ;; # /etc/aliases no sanity checks m) EMAIL=$arg ;; # /etc/aliases no sanity checks
n) SEND_EMAIL=N ;; n) SEND_EMAIL=N ;;
v) check_depends; check_version ;; v) check_depends; check_version ;;
s) export VERBOSE=N ;;
h) usage ;; h) usage ;;
\?) usage ;; \?) usage ;;
:) check_args $OPTARG none none ;; :) check_args $OPTARG none none ;;
@ -267,25 +277,25 @@ main() {
output=$CONF_DIR/$file output=$CONF_DIR/$file
# check for updated blacklist # check for updated blacklist
update=$(check_version | tail -n 2) check_version
printf "\n$update\n\n" update=$?
if echo $update | grep ^Update 1>/dev/null; then if [ $update = 1 ]; then
# download globalblacklist update # download globalblacklist update
tmp=$(mktemp) tmp=$(mktemp)
mkdir -p $CONF_DIR mkdir -p $CONF_DIR
printf "${BOLDWHITE}Downloading: $file " local dl_msg="${BOLDWHITE}Downloading: $file "
curl --fail --connect-timeout 60 --retry 10 --retry-delay 5 -so $tmp $url curl --fail --connect-timeout 60 --retry 10 --retry-delay 5 -so $tmp $url
retval=$? retval=$?
case "$retval" in case "$retval" in
0) printf "...${BOLDGREEN}OK${RESET}\n\n" 0) print_message "$dl_msg...${BOLDGREEN}[OK]${RESET}\n\n"
mv $tmp $output mv $tmp $output
;; ;;
22) printf "...${BOLDRED}ERROR 404: $url${RESET}\n\n";; 22) printf "$dl_msg...${BOLDRED}ERROR 404: $url${RESET}\n\n";;
28) printf "...${BOLDRED}ERROR TIMEOUT: $url${RESET}\n\n";; 28) printf "$dl_msg...${BOLDRED}ERROR TIMEOUT: $url${RESET}\n\n";;
*) printf "...${BOLDRED}ERROR CURL: ($retval){RESET}\n\n";; *) printf "$dl_msg...${BOLDRED}ERROR CURL: ($retval){RESET}\n\n";;
esac esac
# download new bots.d / conf.d files # download new bots.d / conf.d files
@ -297,13 +307,16 @@ main() {
# re-read nginx configuration # re-read nginx configuration
if [ $retval = 0 ]; then if [ $retval = 0 ]; then
$service nginx reload $service nginx reload 2>&1 >/dev/null
if [ $? = 0 ]; then if [ $? = 0 ]; then
status="${BOLDGREEN}[OK]${RESET}" status="${BOLDGREEN}[OK]${RESET}"
print_message "\nReloading NGINX configuration...$status\n"
else else
status="${BOLDRED}[FAILED]${RESET}" status="${BOLDRED}[FAILED]${RESET}"
printf "\nReloading NGINX configuration...$status\n"
fi fi
printf "\nReloading NGINX configuration...$status\n"
else else
printf "\n${BOLDRED}Download failed${RESET}: not reloading NGINX config\n" printf "\n${BOLDRED}Download failed${RESET}: not reloading NGINX config\n"
fi fi
@ -315,7 +328,10 @@ main() {
# email report # email report
check_mail_depends check_mail_depends
case "$SEND_EMAIL" in case "$SEND_EMAIL" in
y*|Y*) printf "Emailing report to: ${BOLDWHITE}$EMAIL${RESET}\n\n"; y*|Y*) if [ "$VERBOSE" = "N" ]; then
grep "Version:" $CONF_DIR/globalblacklist.conf | tr -d '#'
fi
print_message "Emailing report to: ${BOLDWHITE}$EMAIL${RESET}\n\n";
# remove ansi colour codes # remove ansi colour codes
sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT sed -i 's/\x1b\[[0-9;]*m//g' $EMAIL_REPORT
cat $EMAIL_REPORT | mail -s "Nginx Bad Bot Blocker Updated" $EMAIL cat $EMAIL_REPORT | mail -s "Nginx Bad Bot Blocker Updated" $EMAIL
@ -338,5 +354,8 @@ exit $?
# Here's another example to run it daily at midday using a command line switch to set the email address for the notification # Here's another example to run it daily at midday using a command line switch to set the email address for the notification
# 00 12 * * * sudo /usr/sbin/update-ngxblocker -e yourname@youremailprovider.com # 00 12 * * * sudo /usr/sbin/update-ngxblocker -e yourname@youremailprovider.com
# Less verbose logging to a system alias mail address (root crontab)
# 00 12 * * * /usr/sbin/update-ngxblocker -s -m webmaster
# better logging for cron jobs: # better logging for cron jobs:
# https://serverfault.com/questions/137468/better-logging-for-cronjobs-send-cron-output-to-syslog # https://serverfault.com/questions/137468/better-logging-for-cronjobs-send-cron-output-to-syslog