Last active
December 17, 2015 19:29
-
-
Save user454322/5660771 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -o nounset | |
set -o errexit | |
# | |
#----Settings | |
# | |
readonly DEFAULT_APP_DIR="/tmp/logs_dir" | |
# DEFAULT_MAX_SIZE in MB. Files bigger than MAX_SIZE are not compressed | |
readonly DEFAULT_MAX_SIZE="1" | |
readonly DEFAULT_TO_MAIL="mail@mail.com" | |
# The name of the current APP log or other files to be ignored | |
readonly IGNORED_FILES=("today.log" "ignored.dmg") | |
# MB since last check. Set this with cron each minute to have MB /min. | |
# Files that have changed more than DEFAULT_MAX_RATE since last check | |
# will be reported. | |
readonly DEFAULT_MAX_RATE="2" | |
# File to store the size of the logs files. | |
readonly DB_FILE="/var/db/applogs.txt" | |
readonly GZIP_CMD=/opt/local/bin/pigz | |
#----End of Settings | |
## Functions | |
function compress(){ | |
EXEC_DIR=$(pwd) | |
while read -r -d '' log_file; do | |
file_name=$(basename "$log_file") | |
for ignored in "${IGNORED_FILES[@]}"; do | |
if [ "$file_name" == "$ignored" ]; then | |
continue 2 | |
fi; done | |
cd $(dirname "$log_file") | |
"$GZIP_CMD" --quiet --best "$file_name" | |
done < <(find "$APP_DIR"/*/log -size -"${MAX_SIZE}M" -type f -print0) | |
cd "$EXEC_DIR" | |
} | |
function monitor(){ | |
local BIG_FILES=$(find "$APP_DIR"/*/log -size +"${MAX_SIZE}M" -type f -print | | |
xargs du -sh) | |
if [ ! -z "$BIG_FILES" ]; then | |
echo "$BIG_FILES" | mail -s "APP log monitor" "$TO_MAIL" | |
fi | |
} | |
function monitor_grow(){ | |
declare -A previous_files | |
while read file_size file_name; do | |
previous_files[$file_name]="$file_size" | |
done < <(cat "$DB_FILE") | |
readonly previous_files | |
declare -A current_files | |
while read file_size file_name; do | |
current_files[$file_name]="$file_size" | |
done < <(du -m "$APP_DIR"/*/log/* |tee "$DB_FILE") | |
readonly current_files | |
#set -x | |
#echo "$MAX_RATE" | |
big_files="" | |
for i in "${!current_files[@]}"; do | |
fsize="${current_files[$i]}" | |
previous="${previous_files[$i]}" | |
growth=$(( fsize - previous )) | |
if [ "$growth" -gt "$MAX_RATE" ]; then | |
big_files="${big_files}\n${fsize}MB\t${i}" | |
fi | |
done | |
#set +x | |
#echo ">>$big_files<<" | |
if [ ! -z "$big_files" ]; then | |
printf "$big_files" | mail -s "APP log's growth monitor" "$TO_MAIL" | |
fi | |
} | |
function usage(){ | |
echo "Usage: $0 [-c <compress|monitor|monitor-grow>] [-d APP_DIR] " \ | |
"[-s MAX_SIZE in MB] [-m TO_MAIL] [-r MAX_RATE]" | |
} | |
readonly -f compress | |
readonly -f monitor | |
readonly -f monitor_grow | |
readonly -f usage | |
## Get options | |
while getopts "c:d:s:m:r:" option; do | |
case "${option}" | |
in | |
c) COMMAND=${OPTARG};; | |
d) APP_DIR=${OPTARG};; | |
s) MAX_SIZE=${OPTARG};; | |
m) TO_MAIL=${OPTARG};; | |
r) MAX_RATE=${OPTARG};; | |
esac | |
done | |
if [ "x${APP_DIR}" == "x" ]; then | |
readonly APP_DIR="$DEFAULT_APP_DIR" | |
fi | |
if [ "x${MAX_SIZE}" == "x" ]; then | |
readonly MAX_SIZE="$DEFAULT_MAX_SIZE" | |
fi | |
if [ "x${TO_MAIL}" == "x" ]; then | |
readonly TO_MAIL="$DEFAULT_TO_MAIL" | |
fi | |
if [ "x${MAX_RATE}" == "x" ]; then | |
readonly MAX_RATE="$DEFAULT_MAX_RATE" | |
fi | |
if ! [[ "$MAX_RATE" =~ ^[0-9]+$ ]]; then | |
echo "Error: MAX_RATE Is not a number" | |
exit 1 | |
fi | |
## Execute | |
case "x${COMMAND}" in | |
"xcompress") | |
compress | |
;; | |
"xmonitor") | |
monitor | |
;; | |
"xmonitor-grow") | |
monitor_grow | |
;; | |
"x") | |
compress | |
monitor | |
;; | |
*) | |
usage | |
exit 1; | |
esac | |
exit $?; | |
#find app_dir -name "*.gz" -exec gunzip {} \; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment