add systemd timer email script
[log-quiet] / sysd-mail-once
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16
17 set -eE -o pipefail
18 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
19
20 errors=3
21 cbase=/var/local/cron-errors
22 case "$1" in
23 -h|--help)
24 cat <<EOF
25 Usage: sysd-log-once [-ERRORS] SERVICE COMMAND
26
27 Use to wrap systemd timers and to email on repeated failure & success after failure
28
29 Stores error counts in $c\$error_count
30
31 -ERRORS: ERRORS is the number of failurs to accumulate before mailing the error
32 EOF
33 exit 0
34 ;;
35 -[0-9]*)
36 errors=${1#-}
37 shift
38 ;;
39 esac
40 service=$1
41 shift
42
43 c=$cbase/$service
44
45 glob="$c[0-9]*"
46 arr=($glob); file="${arr[0]}"; [[ $glob != "$file" ]] || file=
47 u=${USER:-root}
48 [[ -d $cbase ]] || mkdir -p $cbase
49
50 cursor=$(journalctl --show-cursor -qn0|sed 's/^\s*--\scursor:\s*//')
51 if "$@"; then
52 if [[ $file ]]; then
53 rm -f $file
54 if [[ $file == $c$errors ]]; then
55 echo | mail -s "$HOSTNAME: $service success" $u@localhost
56 fi
57 fi
58 else
59 if [[ $file ]]; then
60 i=${file#$c}
61 if (( i < errors )); then
62 new_file=$c$((i+1))
63 mv $file $new_file
64 file=$new_file
65 if [[ $file == $c$errors ]]; then
66 journalctl -u $service.service --after-cursor=$cursor | \
67 mail -s "$HOSTNAME: $service failure" $u@localhost
68 fi
69 fi
70 else
71 file=${c}1
72 touch $file
73 fi
74 fi