-
Notifications
You must be signed in to change notification settings - Fork 14
/
vee-mail.sh
executable file
·306 lines (271 loc) · 12.7 KB
/
vee-mail.sh
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
VERSION=0.5.49
HDIR=$(dirname "$0")
DEBUG=0
INFOMAIL=1
# INFOMAIL 1=ALWAYS (DEFAULT), 2=WARN, 3=ERROR
SENDM=0
sleep 1
if [ ! $SLEEP ]; then
SLEEP=60
fi
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
logger -t vee-mail "This script must be run as root"
exit 1
fi
. $HDIR/vee-mail.config
STARTEDFROM=$(ps -p $PPID -hco cmd)
if [ "$1" == "--bg" ]; then
if [ "$STARTEDFROM" == "veeamjobman" ]; then
logger -t vee-mail "waiting for 30 seconds"
sleep $SLEEP
fi
fi
VC=$(which veeamconfig)
if [ ! "$VC" ]; then
echo "No Veeam Agent for Linux installed!"
logger -t vee-mail "No Veeam Agent for Linux installed!"
exit
fi
VV=$(veeamconfig -v|cut -c2)
YUM=$(which yum)
SQLITE=$(which sqlite3)
if [ "$SQLITE" != "/usr/bin/sqlite3" ] && [ "$SQLITE" != "/bin/sqlite3" ]; then
if [ "$YUM" ]; then
yum install -y sqlite3
else
apt-get install -y sqlite3
fi
fi
CURL=$(which curl)
if [ $USECURL -eq 1 ] && [ ! "$CURL" ]; then
if [ "$YUM" ]; then
yum install -y curl
else
apt-get install -y curl
fi
fi
SENDMAIL=$(which sendmail)
if [ $USECURL -ne 1 ] &&[ "$SENDMAIL" != "/usr/sbin/sendmail" ] && [ "$SENDMAIL" != "/bin/sendmail" ]; then
if [ "$YUM" ]; then
yum install -y sendmail
else
apt-get install -y sendmail
fi
fi
if [ $SKIPVERSIONCHECK -ne 1 ]; then
if [ "$CURL" ]; then
AKTVERSION=$($CURL -m2 -f -s https://raw.githubusercontent.com/grufocom/vee-mail/master/vee-mail.sh --stderr - | grep "^VERSION=" | awk -F'=' '{print $2}')
if [ "$AKTVERSION" ]; then
HIGHESTVERSION=$(echo -e "$VERSION\n$AKTVERSION" | sort -rV | head -n1)
if [ "$VERSION" != "$HIGHESTVERSION" ]; then
logger -t vee-mail "new Vee-Mail version $AKTVERSION available"
AKTVERSION="\(new Vee-Mail version $AKTVERSION available\)"
else
AKTVERSION=""
fi
fi
else
AKTVERSION="\(you need curl to use the upgrade check, please install\)"
fi
else
VERSION="$VERSION \(upgrade check disabled\)"
fi
AGENT=$($VC -v)
# get last session id
if [ $VV -ge 6 ]; then
SESSID=$($VC session list|grep -v "Total amount"|tail -1|awk '{print $(NF-7)}')
else
SESSID=$($VC session list|grep -v "Total amount"|tail -1|awk '{print $(NF-5)}')
fi
SESSID=${SESSID:1:${#SESSID}-2}
# state 1=Running, 6=Success, 7=Failed, 9=Warning
# get data from sqlite db
if [ $VV -ge 6 ]; then
SESSDATA=$(sqlite3 /var/lib/veeam/veeam_db.sqlite "select start_time_utc, end_time_utc, state, progress_details, job_id, job_name from JobSessions order by start_time_utc DESC limit 1;")
else
SESSDATA=$(sqlite3 /var/lib/veeam/veeam_db.sqlite "select start_time, end_time, state, progress_details, job_id, job_name from JobSessions order by start_time DESC limit 1;")
fi
STARTTIME=$(echo $SESSDATA|awk -F'|' '{print $1}')
ENDTIME=$(echo $SESSDATA|awk -F'|' '{print $2}')
STATE=$(echo $SESSDATA|awk -F'|' '{print $3}')
DETAILS=$(echo $SESSDATA|awk -F'|' '{print $4}')
JOBID=$(echo $SESSDATA|awk -F'|' '{print $5}')
JOBNAME=$(echo $SESSDATA|awk -F'|' '{print $6}')
if [ $DEBUG -gt 0 ]; then
echo -e -n "STARTTIME: $STARTTIME, ENDTIME: $ENDTIME, STATE: $STATE, JOBID: $JOBID, JOBNAME: $JOBNAME\nDETAILS: $DETAILS\n"
logger -t vee-mail "STARTTIME: $STARTTIME, ENDTIME: $ENDTIME, STATE: $STATE, JOBID: $JOBID, JOBNAME: $JOBNAME\nDETAILS: $DETAILS"
fi
if [ "$JOBID" ]; then
RAWTARGET=$(sqlite3 /var/lib/veeam/veeam_db.sqlite "SELECT a1.options FROM BackupRepositories AS a1 LEFT JOIN BackupJobs AS a2 ON a1.id=a2.repository_id WHERE a2.id=\"$JOBID\"")
TARGET=$(echo $RAWTARGET|awk -F'Address="' '{print $2}'|awk -F'"' '{print $1}'|sed -e "s/^\/\///g")
FST=$(echo $RAWTARGET|awk -F'FsType="' '{print $2}'|awk -F'"' '{print $1}')
LOGIN=$(echo $RAWTARGET|awk -F'Login="' '{print $2}'|awk -F'"' '{print $1}')
DOMAIN=$(echo $RAWTARGET|awk -F'Domain="' '{print $2}'|awk -F'"' '{print $1}')
if [ $DEBUG -gt 0 ]; then
echo -e -n "TARGET: $TARGET, FST: $FST, LOGIN: $LOGIN, DOMAIN: $DOMAIN\n"
logger -t vee-mail "TARGET: $TARGET, FST: $FST, LOGIN: $LOGIN, DOMAIN: $DOMAIN"
fi
if [ ! "$TARGET" ]; then
TARGET=$(echo $RAWTARGET|awk -F'DeviceMountPoint="' '{print $2}'|awk -F'"' '{print $1}')
AWKTARGET=$(echo $TARGET | sed 's@/@\\/@g')
FST=$(mount | awk -vORS=" " "\$3 ~ /^${AWKTARGET}\$/ {print \$5}")
FSD=$(mount | awk -vORS=" " "\$3 ~ /^${AWKTARGET}\$/ {print \$1}")
# Filesystem Size Used Avail Use% Mounted on
DEVSIZE=$(df -hP | awk "\$6 ~ /^${AWKTARGET}\$/ {print \$2}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVUSED=$(df -hP | awk "\$6 ~ /^${AWKTARGET}\$/ {print \$3}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVAVAIL=$(df -hP | awk "\$6 ~ /^${AWKTARGET}\$/ {print \$4}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVUSEP=$(df -hP | awk "\$6 ~ /^${AWKTARGET}\$/ {print \$5}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
LOGIN=""
DOMAIN=""
LOCALDEV=1
fi
if [ "$FST" == "cifs" ] || [ "$FST" == "smb" ] && [ "$LOCALDEV" != "1" ]; then
if [ "$SMBUSER" ] && [ "$SMBPWD" ]; then
MPOINT=$(mktemp -d)
AWKMPOINT=$(echo $MPOINT | sed 's@/@\\/@g')
mount -t cifs -o username=$SMBUSER,password=$SMBPWD,domain=$DOMAIN //$TARGET $MPOINT
# Filesystem Size Used Avail Use% Mounted on
DEVSIZE=$(df -hP | awk "\$6 ~ /^${AWKMPOINT}\$/ {print \$2}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVUSED=$(df -hP | awk "\$6 ~ /^${AWKMPOINT}\$/ {print \$3}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVAVAIL=$(df -hP | awk "\$6 ~ /^${AWKMPOINT}\$/ {print \$4}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVUSEP=$(df -hP | awk "\$6 ~ /^${AWKMPOINT}\$/ {print \$5}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
umount $MPOINT
rmdir $MPOINT
fi
fi
if [ "$FST" == "nfs" ] && [ "$LOCALDEV" != "1" ]; then
MPOINT=$(mktemp -d)
mount -t nfs $TARGET $MPOINT
AWKMPOINT=$(echo $MPOINT | sed 's@/@\\/@g')
# Filesystem Size Used Avail Use% Mounted on
DEVSIZE=$(df -hP | awk "\$6 ~ /^${AWKMPOINT}\$/ {print \$2}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVUSED=$(df -hP | awk "\$6 ~ /^${AWKMPOINT}\$/ {print \$3}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVAVAIL=$(df -hP | awk "\$6 ~ /^${AWKMPOINT}\$/ {print \$4}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
DEVUSEP=$(df -hP | awk "\$6 ~ /^${AWKMPOINT}\$/ {print \$5}" | sed -e "s/,/\./g" -e "s/M/ M/g" -e "s/G/ G/g" -e "s/T/ T/g" -e "s/P/ P/g")
umount $MPOINT
rmdir $MPOINT
fi
fi
if [ ! "$1" == "--bg" ] && [ "$STARTEDFROM" == "veeamjobman" ]; then
nohup $0 --bg >/dev/null 2>/dev/null &
exit
fi
if [ "$STATE" == "6" ]; then
SUCCESS=1; BGCOLOR="#00B050"; STAT="Success";
if [ $INFOMAIL -eq 1 ]; then
SENDM=1
fi
else
SUCCESS=0;
fi
if [ "$STATE" == "7" ]; then
ERROR=1; BGCOLOR="#fb9895"; STAT="Failed";
if [ $INFOMAIL -ge 1 ]; then
SENDM=1
fi
else
ERROR=0;
fi
if [ "$STATE" == "9" ]; then
WARNING=1; BGCOLOR="#fbcb95"; STAT="Warning";
if [ $INFOMAIL -le 2 ]; then
SENDM=1
fi
else
WARNING=0;
fi
PROCESSED=$(echo $DETAILS|awk -F'processed_data_size_bytes="' '{print $2}'|awk -F'"' '{print $1}')
PROCESSED=$(awk "BEGIN {printf \"%.1f\n\", $PROCESSED/1024/1024/1024}")" GB"
READ=$(echo $DETAILS|awk -F'read_data_size_bytes="' '{print $2}'|awk -F'"' '{print $1}')
READ=$(awk "BEGIN {printf \"%.1f\n\", $READ/1024/1024/1024}")" GB"
TRANSFERRED=$(echo $DETAILS|awk -F'transferred_data_size_bytes="' '{print $2}'|awk -F'"' '{print $1}')
if [ $DEBUG -gt 0 ]; then
echo -e -n "PROCESSED: $PROCESSED, READ: $READ, TRANSFERRED: $TRANSFERRED\n"
logger -t vee-mail "PROCESSED: $PROCESSED, READ: $READ, TRANSFERRED: $TRANSFERRED"
fi
if [ $TRANSFERRED -gt 1073741824 ]; then
TRANSFERRED=$(awk "BEGIN {printf \"%.1f\n\", $TRANSFERRED/1024/1024/1024}")" GB"
else
TRANSFERRED=$(awk "BEGIN {printf \"%.0f\n\", $TRANSFERRED/1024/1024}")" MB"
fi
SPEED=$(echo $DETAILS|awk -F'processing_speed="' '{print $2}'|awk -F'"' '{print $1}')
SPEED=$(awk "BEGIN {printf \"%.1f\n\", $SPEED/1024/1024}")
SOURCELOAD=$(echo $DETAILS|awk -F'source_read_load="' '{print $2}'|awk -F'"' '{print $1}')
SOURCEPLOAD=$(echo $DETAILS|awk -F'source_processing_load="' '{print $2}'|awk -F'"' '{print $1}')
NETLOAD=$(echo $DETAILS|awk -F'network_load="' '{print $2}'|awk -F'"' '{print $1}')
TARGETLOAD=$(echo $DETAILS|awk -F'target_write_load="' '{print $2}'|awk -F'"' '{print $1}')
if [ $DEBUG -gt 0 ]; then
echo -e -n "SPEED: $SPEED, SOURCELOAD: $SOURCELOAD, NETLOAD: $NETLOAD, TARGETLOAD: $TARGETLOAD\n"
logger -t vee-mail "SPEED: $SPEED, SOURCELOAD: $SOURCELOAD, NETLOAD: $NETLOAD, TARGETLOAD: $TARGETLOAD"
fi
if [ "$SOURCELOAD" -gt "$SOURCEPLOAD" ] && [ "$SOURCELOAD" -gt "$NETLOAD" ] && [ "$SOURCELOAD" -gt "$TARGETLOAD" ]; then
BOTTLENECK="Source"
fi
if [ "$SOURCEPLOAD" -gt "$SOURCELOAD" ] && [ "$SOURCEPLOAD" -gt "$NETLOAD" ] && [ "$SOURCEPLOAD" -gt "$TARGETLOAD" ]; then
BOTTLENECK="Source CPU"
fi
if [ "$NETLOAD" -gt "$SOURCELOAD" ] && [ "$NETLOAD" -gt "$SOURCEPLOAD" ] && [ "$NETLOAD" -gt "$TARGETLOAD" ]; then
BOTTLENECK="Network"
fi
if [ "$TARGETLOAD" -gt "$SOURCELOAD" ] && [ "$TARGETLOAD" -gt "$SOURCEPLOAD" ] && [ "$TARGETLOAD" -gt "$NETLOAD" ]; then
BOTTLENECK="Target"
fi
let DUR=ENDTIME-STARTTIME DURATIONSEC=DUR%60 DURATIONMIN=\(DUR-DURATIONSEC\)/60%60 DURATIONHOUR=\(DUR-DURATIONSEC-\(DURATIONMIN*60\)\)/3600
DURATION=$(printf "%d:%02d:%02d\n" $DURATIONHOUR $DURATIONMIN $DURATIONSEC)
START=$(date -d "@$STARTTIME" +"%A, %d %B %Y %H:%M:%S")
END=$(date -d "@$ENDTIME" +"%A, %d.%m.%Y %H:%M:%S")
STIME=$(date -d "@$STARTTIME" +"%H:%M:%S")
ETIME=$(date -d "@$ENDTIME" +"%H:%M:%S")
if [ $DEBUG -gt 0 ]; then
echo -e -n "DURATION: $DURATION, START: $START, END: $END, STIME: $STIME, ETIME: $ETIME\n"
logger -t vee-mail "DURATION: $DURATION, START: $START, END: $END, STIME: $STIME, ETIME: $ETIME"
fi
# get session error
ERRLOG=$($VC session log --id $SESSID|egrep 'error|warn'|sed ':a;N;$!ba;s/\n/<br>/g'|sed -e "s/ /\ /g")
ERRLOG=$(printf "%q" $ERRLOG)
if [ "$ERRLOG" == "''" ]; then
ERRLOG=""
fi
if [ $DEBUG -gt 0 ]; then
echo -e -n "ERRLOG: $ERRLOG\n"
logger -t vee-mail "ERRLOG: $ERRLOG"
fi
# create temp file for mail
TEMPFILE=$(mktemp)
# uppercase hostname
HN=${HOSTNAME^^}
# build email
SUBJECT=$(echo "[$STAT] $HN - $START"|base64 -w0)
echo -en "From: $EMAILFROM
To: $EMAILTO
Subject: =?UTF-8?B?$SUBJECT?=
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit\r
\r
" > $TEMPFILE
# debug output
if [ $DEBUG -gt 0 ]; then
echo -e -n "HN: $HN\nSTAT: $STAT\nBGCOLOR: $BGCOLOR\nSTART: $START\nSUCCESS: $SUCCESS\nERROR: $ERROR\nWARNING: $WARNING\nSTIME: $STIME\nETIME: $ETIME\nREAD: $READ\nTRANSFERRED: $TRANSFERRED\nDURATION: $DURATION\nPROCESSED: $PROCESSED\nBOTTLENECK: $BOTTLENECK\nERRLOG: $ERRLOG\nSPEED: $SPEED\nTARGET: $TARGET\nFST: $FST\nLOGIN: $LOGIN\nDOMAIN: $DOMAIN\n DEVUSEP: $DEVUSEP\n"
logger -t vee-mail "HN: $HN; STAT: $STAT; BGCOLOR: $BGCOLOR; START: $START; SUCCESS: $SUCCESS; ERROR: $ERROR; WARNING: $WARNING; STIME: $STIME; ETIME: $ETIME; READ: $READ; TRANSFERRED: $TRANSFERRED; DURATION: $DURATION; PROCESSED: $PROCESSED; BOTTLENECK: $BOTTLENECK; ERRLOG: $ERRLOG; SPEED: $SPEED; TARGET: $TARGET; FST: $FST; LOGIN: $LOGIN; DOMAIN: $DOMAIN; DEVUSEP: $DEVUSEP"
fi
sed -e "s/XXXJOBNAMEXXX/$JOBNAME/g" -e "s/XXXHOSTNAMEXXX/$HN/g" -e "s/XXXSTATXXX/$STAT/g" -e "s/XXXBGCOLORXXX/$BGCOLOR/g" -e "s/XXXBACKUPDATETIMEXXX/$START/g" -e "s/XXXSUCCESSXXX/$SUCCESS/g" -e "s/XXXERRORXXX/$ERROR/g" -e "s/XXXWARNINGXXX/$WARNING/g" -e "s/XXXSTARTXXX/$STIME/g" -e "s/XXXENDXXX/$ETIME/g" -e "s/XXXDATAREADXXX/$READ/g" -e "s/XXXREADXXX/$READ/g" -e "s/XXXTRANSFERREDXXX/$TRANSFERRED/g" -e "s/XXXDURATIONXXX/$DURATION/g" -e "s/XXXSTATUSXXX/$STAT/g" -e "s/XXXTOTALSIZEXXX/$PROCESSED/g" -e "s/XXXBOTTLENECKXXX/$BOTTLENECK/g" -e "s|XXXDETAILSXXX|$ERRLOG|g" -e "s/XXXRATEXXX/$SPEED MB\/s/g" -e "s/XXXBACKUPSIZEXXX/$TRANSFERRED/g" -e "s/XXXAGENTXXX/$AGENT/g" -e "s|XXXTARGETXXX|$TARGET|g" -e "s|XXXFSTXXX|$FST|g" -e "s|XXXLOGINXXX|$LOGIN|g" -e "s|XXXDOMAINXXX|$DOMAIN|g" -e "s|XXXVERSIONXXX|$VERSION|g" -e "s|XXXAKTVERSIONXXX|$AKTVERSION|g" -e "s|XXXDISKSIZEXXX|$DEVSIZE|g" -e "s|XXXDISKUSEDXXX|$DEVUSED|g" -e "s|XXXDISKAVAILXXX|$DEVAVAIL|g" -e "s|XXXDISKUSEPXXX|$DEVUSEP|g" $HTMLTEMPLATE >> $TEMPFILE
# send email
if [ $SENDM -eq 1 ]; then
if [ $USECURL -eq 1 ]; then
CURLPARAMS=""
if [ $CURLSTARTTLS -eq 1 ]; then
CURLPARAMS=" $CURLPARAMS --ssl-reqd"
fi
if [ $CURLINSECURE -eq 1 ]; then
CURLPARAMS=" $CURLPARAMS --insecure"
fi
$CURL -sS --url $CURLSMTPSERVER --mail-from $EMAILFROM --mail-rcpt $EMAILTO --upload-file $TEMPFILE ${CURLUSERNAME:+-u $CURLUSERNAME:"$CURLPASSWORD"} $CURLPARAMS
else
cat $TEMPFILE | $SENDMAIL -f $EMAILFROM -t
fi
fi
rm $TEMPFILE
exit