iankelling.org
/
git
/
log-quiet
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
bug fix: prints dry statement when not in dry run
[log-quiet]
/
sysd-mail-once
diff --git
a/sysd-mail-once
b/sysd-mail-once
index 3542200d12d52b437362d252a3047315d34d0bd7..659fa92633860892a18a4b430ed1c027a3197cbb 100755
(executable)
--- a/
sysd-mail-once
+++ b/
sysd-mail-once
@@
-18,9
+18,11
@@
set -eE -o pipefail
trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
errors=3
trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
errors=3
-cbase=$HOME/sysd-mail-once-state
+tmp=(~)
+cbase="${tmp[0]}/sysd-mail-once-state"
to=root
dryrun=false
to=root
dryrun=false
+print_all=false
while [[ $1 == -* ]]; do
case "$1" in
-h|--help)
while [[ $1 == -* ]]; do
case "$1" in
-h|--help)
@@
-39,6
+41,7
@@
Stores error counts in $cbase
-ERRORS ERRORS is the number of failurs to accumulate before mailing the error.
Default is 3.
-ERRORS ERRORS is the number of failurs to accumulate before mailing the error.
Default is 3.
+-a Email the logs of all errors intead of just the last one.
-t TO_ADDRESS Address to email about errors
-n Dry run. Execute command but only print out what we would do in response.
EOF
-t TO_ADDRESS Address to email about errors
-n Dry run. Execute command but only print out what we would do in response.
EOF
@@
-47,6
+50,9
@@
EOF
-[0-9]*)
errors=${1#-}
;;
-[0-9]*)
errors=${1#-}
;;
+ -a)
+ print_all=true
+ ;;
-t)
to="$2"
shift
-t)
to="$2"
shift
@@
-90,18
+96,18
@@
mi() {
fi
}
e() {
fi
}
e() {
- printf "dryrun: %s\n" "$*"
+ if $dryrun; then
+ printf "dryrun: %s\n" "$*"
+ fi
}
c=$cbase/$service # c for command file path base
}
c=$cbase/$service # c for command file path base
-if $dryrun; then
- e "c=$c"
-fi
+e "c=$c"
glob="${c}[0-9]*"
arr=($glob); file="${arr[0]}"; [[ $glob != "$file" ]] || file=
glob="${c}[0-9]*"
arr=($glob); file="${arr[0]}"; [[ $glob != "$file" ]] || file=
-if [[ $
dryrun && $
file ]]; then
+if [[ $file ]]; then
e "file=$file"
fi
e "file=$file"
fi
@@
-109,14
+115,12
@@
if ! [[ -d $cbase ]]; then
mkdir -p $cbase
fi
mkdir -p $cbase
fi
-if [[ ! $file ]]; then
- cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
-fi
code=0
"$@" || code=$?
if (( code )); then
send_mail=false
code=0
"$@" || code=$?
if (( code )); then
send_mail=false
+ cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
if [[ $file ]]; then
i=${file#"$c"}
if (( i < errors )); then
if [[ $file ]]; then
i=${file#"$c"}
if (( i < errors )); then
@@
-125,6
+129,12
@@
if (( code )); then
file=$new_file
if [[ $file == $c$errors ]]; then
send_mail=true
file=$new_file
if [[ $file == $c$errors ]]; then
send_mail=true
+ else
+ if $dryrun; then
+ printf "dryrun: appending to file $file: %s\n" "$cursor"
+ else
+ printf "%s\n" "$cursor" >>$file
+ fi
fi
fi
else
fi
fi
else
@@
-139,13
+149,18
@@
if (( code )); then
fi
fi
if $send_mail; then
fi
fi
if $send_mail; then
+ if $print_all; then
+ cursor=$(head -n1 $file)
+ else
+ cursor=$(tail -n1 $file)
+ fi
echo "sysd-mail-once: emailing on $errors errors. exit code: $code"
mi exim -odf -t <<EOF
To: $to
echo "sysd-mail-once: emailing on $errors errors. exit code: $code"
mi exim -odf -t <<EOF
To: $to
-From: $
USER
@$(hostname -f)
+From: $
(id -u -n)
@$(hostname -f)
Subject: $HOSTNAME: $service exit code: $code
Subject: $HOSTNAME: $service exit code: $code
-$(journalctl -u $service.service --after-cursor="$
(<$file)
")
+$(journalctl -u $service.service --after-cursor="$
cursor
")
EOF
fi
else
EOF
fi
else
@@
-155,7
+170,7
@@
else
echo "sysd-mail-once: emailing success after >= $errors errors."
mi exim -odf -t <<EOF
To: $to
echo "sysd-mail-once: emailing success after >= $errors errors."
mi exim -odf -t <<EOF
To: $to
-From: $
USER
@$(hostname -f)
+From: $
(id -u -n)
@$(hostname -f)
Subject: $HOSTNAME: $service success
EOF
Subject: $HOSTNAME: $service success
EOF