forked from cyclingzealot/dumuzid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.bash
executable file
·171 lines (132 loc) · 4.29 KB
/
main.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
START=$(date +%s.%N)
arg1=${1:-''}
if [[ $arg1 == '--help' || $arg1 == '-h' ]]; then
echo "Script author should have provided documentation"
exit 0
fi
#exit when command fails (use || true when a command can fail)
set -o errexit
#exit when your script tries to use undeclared variables
set -o nounset
# in scripts to catch mysqldump fails
set -o pipefail
# Resolve first directory of script
PRG="$BASH_SOURCE"
progname=`basename "$BASH_SOURCE"`
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
__dir=$(dirname "$PRG")
# Set magic variables for current file & dir
__root="$(cd "$(dirname "${__dir}")" && pwd)" # Dir of the dir of the script
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" # Full path of the script
__base="$(basename ${__file})" # Name of the script
ts=`date +'%Y%m%d-%H%M%S'`
ds=`date +'%Y%m%d'`
pid=`ps -ef | grep ${__base} | grep -v 'vi ' | head -n1 | awk ' {print $2;} '`
formerDir=`pwd`
# If you require named arguments, see
# http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
#Set the config file
configFile="$HOME/.dumuzid.conf"
#=== BEGIN Unique instance ============================================
#Ensure only one copy is running
pidfile=$HOME/.${__base}.pid
if [ -f ${pidfile} ]; then
#verify if the process is actually still running under this pid
oldpid=`cat ${pidfile}`
result=`ps -ef | grep ${oldpid} | grep ${__base} || true`
if [ -n "${result}" ]; then
echo "Script already running! Exiting"
exit 255
fi
fi
#grab pid of this process and update the pid file with it
echo ${pid} > ${pidfile}
# Create trap for lock file in case it fails
trap "rm -f $pidfile" INT QUIT TERM ERR
#=== END Unique instance ============================================
#Capture everything to log
mkdir -p ~/log
log=~/log/dumuzid-${ts}.log
exec > >(tee -a $log)
exec 2> >(tee -a $log >&2)
touch $log
chmod 600 $log
#Check that the config file exists
if [[ ! -f "$configFile" ]] ; then
echo "I need a file at $configFile with an email address to warn"
exit 1
fi
export DISPLAY=`cat ~/dumuzid/main.bash`
echo Begin `date` .....
echo; echo; echo;
### BEGIN SCRIPT ###############################################################
#(a.k.a set -x) to trace what gets executed
set -o xtrace
recipient=`cat $configFile`
hostname=`hostname`
scratchFile=/tmp/dumuzid.scratch
echo > $scratchFile
if [[ ! -f $__dir/dontRun.txt ]]; then
touch $__dir/dontRun.txt
fi
sendAlert=0
echo > $scratchFile
for script in $__dir/*.bash ; do
localAlert=0
bn=`basename $script`
if [[ "$bn" == "$__base" ]] || grep $bn $__dir/dontRun.txt; then
continue
fi
$script > $scratchFile.one || localAlert=1
if [ "$localAlert" -eq "1" ] ; then
sendAlert=1
echo >> $scratchFile.one
~/bin/headline.bash "$script raised an alert" >> $scratchFile.one
echo >> $scratchFile.one
cat $scratchFile.one >> $scratchFile
fi
#Delete log files more than 1.5 days ago (36 hours)
find ~/log -name "$bn.*.log" -mmin +60 -exec rm -v {} \;
done
if [ "$sendAlert" -eq "1" ] ; then
subject="Alerts on `hostname`"
echo >> $scratchFile
echo "Running on `hostname` by user `whoami`" >> $scratchFile
cat $scratchFile
if [[ "$recipient" != 'onscreen' ]]; then
if ~/bin/flagger.bash dumuzid 3600; then
cat $scratchFile | mail -s "$subject" $recipient
else
echo "Too early to send another notice"
fi
elif [[ "$recipient" == 'onscreen' ]]; then
notify-send -t 60000 "`cat $scratchFile | grep -v '===' | grep -v 'Running on' `"
else
echo "Unrecognized recipient"
exit 1
fi
else
~/bin/flagger.bash dumuzid -1
echo "No errors detected"
fi
set +x
### END SCIPT ##################################################################
cd $formerDir
END=$(date +%s.%N)
DIFF=$(echo "($END - $START)" | bc)
echo; echo; echo;
echo Done. `date` - $DIFF seconds
#=== BEGIN Unique instance ============================================
if [ -f ${pidfile} ]; then
rm ${pidfile}
fi
#=== END Unique instance ============================================