X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=log-once;h=8f2fafeab79c16e5f879db96f836ca1e38d057c3;hb=d05b05db1c99982622ee70186c02d54f7d5cb5f9;hp=f8938c83b23511eb211642b06ea24280fe60dd12;hpb=8e2977d8ed9a02302be6481f1a3ac22d7ff23e5a;p=log-quiet diff --git a/log-once b/log-once index f8938c8..8f2fafe 100755 --- a/log-once +++ b/log-once @@ -14,89 +14,93 @@ # limitations under the License. append() { - cat >> "$1" + cat >> "$1" } log-once() { - local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE] + local cbase c log x i out file o + cbase=/var/local/cron-errors + [[ $EUID == 0 ]] || cbase=$HOME/cron-errors + local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE] -For cronjobs, email only once for repeated failures, and for success after failure. +For cronjobs, email log on repeated failure and success after failure. Meant for use in cronjobs where LOG_MESSAGE or STDIN represents an error, but we only want to output that to STDOUT if we've seen this type of error ERRORS(default 3) number of times in a row, then we don't want to output anything again until we've seen a success (an empty LOG_MESSAGE). -Logs LOG_MESSAGE or STDIN to ~/.cron_errors/LOG_NAME, and keeps +Logs LOG_MESSAGE or STDIN to /var/local/cron-errors/LOG_NAME\$error_count +or $HOME/cron-errors if not root, and keeps state in the same directory. --e ERRORS: ERRORS is the number of errors to accumulate before outputing the error" - local cbase c c1 c2 log x i out file - errors=3 - while true; do - if [[ $1 == --help ]]; then - echo "$help" - return - elif [[ $1 == -[0-9]* ]]; then - errors={$1#-} - shift - elif [[ $1 == -- ]]; then - shift - break - else - break - fi - done - log_name=$1 - # todo, make option & make them overridable based on command line or env variable - cbase=$HOME/.cron-errors - [[ -d $cbase ]] || mkdir -p $cbase - c=$cbase/$log_name - # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script - log=false - if [[ $2 ]]; then - log=true - # read stdin for anything which is not just a newline - elif [[ ! -t 0 ]]; then - while read -r x; do - output+=( $x ) - [[ $x ]] && log=true - done +-ERRORS: ERRORS is the number of errors to accumulate before outputing the error" + errors=3 + while true; do + if [[ $1 == --help ]]; then + echo "$help" + return + elif [[ $1 == -[0-9]* ]]; then + errors=${1#-} + shift + elif [[ $1 == -- ]]; then + shift + break + else + break fi - if $log; then - file= - if [[ -f $c$((errors-1)) ]]; then - out="tee -a" - else - out=append - fi - for ((i=errors; i>=1; i--)); do - if [[ -f $c$((i-1)) ]]; then - file=$c$i - mv $c$((i-1)) $file - break - fi - done - if [[ ! $file ]]; then - if [[ -f $c$errors ]]; then - file=$c$errors - else - file=${c}1 - fi - fi - $out $file <<<"log-once: $(date "+%A, %B %d, %r")" - if [[ $2 ]]; then - $out $file <<<"$2" - else - $out $file <<<"${output[@]}" - $out $file + done + log_name=$1 + # todo, make option & make them overridable based on command line or env variable + [[ -d $cbase ]] || mkdir -p $cbase + c=$cbase/$log_name + # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script + log=false + if [[ $2 ]]; then + log=true + # read stdin for anything which is not just a newline + elif [[ ! -t 0 ]]; then + while read -r x; do + output+=( "$x" ) + [[ $x ]] && log=true + done + fi + glob="$c[0-9]*" + file=($glob); [[ $file != "$glob" ]] || file= + if $log; then + out=append + if [[ $file ]]; then + i="${file#$c}" + if (( i < errors )); then + new_file=$c$((i+1)) + mv $file $new_file + file=$new_file + if [[ $file == $c$errors ]]; then + out="tee -a" fi - return 1 - elif [[ -f $c$errors ]]; then - echo "log-once success after failure for $c" - rm -f $c$errors + fi else - rm -f $c[0-9]* # assuming no one is putting crazy files names, as this is not exact + file=${c}1 + if (( errors == 1 )); then + out="tee -a" + fi + fi + $out $file <<<"log-once: $(date "+%A, %B %d, %r")" + if [[ $2 ]]; then + $out $file <<<"$2" + else + for o in "${output[@]}"; do + $out $file <<<"$o" + done + $out $file fi return 0 + fi + if [[ $file ]]; then + rm -f $file + if [[ $file == $c$errors ]]; then + echo "log-once success after failure for $c" + fi + fi + return 0 } log-once "$@"