Added Logging by mail functionality, added --mail parameter, added logfilehandling

This commit is contained in:
2023-06-13 16:13:23 +02:00
parent a5ea397d11
commit b8d2386e69

View File

@@ -53,6 +53,8 @@ declare -g -i perf_bytes_total=0
declare -g -i perf_vm_running=0 declare -g -i perf_vm_running=0
declare -g -i perf_vm_stopped=0 declare -g -i perf_vm_stopped=0
declare -g -i perf_snaps_removed=0 declare -g -i perf_snaps_removed=0
declare -g -i perf_vm_total=0
declare -g -i perf_vm_ok=0
declare opt_destination declare opt_destination
declare opt_vm_ids='' declare opt_vm_ids=''
@@ -140,7 +142,7 @@ function parse_opts(){
local args local args
args=$(getopt \ args=$(getopt \
--options '' \ --options '' \
--longoptions=vmid:,prefixid:,excludevmids:,destination:,pool:,keeplocal:,keepremote:,rewrite:,influxurl:,influxorg:,influxtoken:,influxbucket:,jobname:,online,migrate,nolock,keep-slock,keep-dlock,overwrite,dry-run,debug \ --longoptions=vmid:,prefixid:,excludevmids:,destination:,pool:,keeplocal:,keepremote:,rewrite:,influxurl:,influxorg:,influxtoken:,influxbucket:,jobname:,mail:,online,migrate,nolock,keep-slock,keep-dlock,overwrite,dry-run,debug \
--name "$PROGNAME" \ --name "$PROGNAME" \
-- "$@") \ -- "$@") \
|| end_process 128 || end_process 128
@@ -162,6 +164,7 @@ function parse_opts(){
--influxtoken) opt_influx_token=$2; shift 2;; --influxtoken) opt_influx_token=$2; shift 2;;
--influxbucket) opt_influx_bucket=$2; shift 2;; --influxbucket) opt_influx_bucket=$2; shift 2;;
--jobname) opt_influx_jobname=$2; shift 2;; --jobname) opt_influx_jobname=$2; shift 2;;
--mail) opt_addr_mail="$2"; shift 2;;
--online) opt_online=1; shift ;; --online) opt_online=1; shift ;;
--migrate) opt_migrate=1; shift ;; --migrate) opt_migrate=1; shift ;;
--dry-run) opt_dry_run=1; shift;; --dry-run) opt_dry_run=1; shift;;
@@ -499,7 +502,7 @@ function mirror() {
vm_freeze "$vm_id" "${pvnode[$vm_id]}" >/dev/null vm_freeze "$vm_id" "${pvnode[$vm_id]}" >/dev/null
freezerc=$? freezerc=$?
if [ $freezerc -gt 0 ]; then if [ $freezerc -gt 0 ]; then
log error "VM $vm_id - QEMU-Guest could not fsfreeze on guest." log warn "VM $vm_id - QEMU-Guest could not fsfreeze on guest."
(( perf_freeze_failed++ )) (( perf_freeze_failed++ ))
else else
(( perf_freeze_ok++ )) (( perf_freeze_ok++ ))
@@ -510,7 +513,7 @@ function mirror() {
create_snapshot "$src_image_spec@$opt_snapshot_prefix$timestamp" 2>/dev/null create_snapshot "$src_image_spec@$opt_snapshot_prefix$timestamp" 2>/dev/null
ssrc=$? ssrc=$?
if [ $ssrc -gt 0 ]; then if [ $ssrc -gt 0 ]; then
log error "VM $vm_id - rbd snap failed." log warn "VM $vm_id - rbd snap failed."
(( perf_ss_failed++ )) (( perf_ss_failed++ ))
else else
(( perf_ss_ok++ )) (( perf_ss_ok++ ))
@@ -653,6 +656,8 @@ function mirror() {
cmd="curl --request POST \"$opt_influx_api_url/v2/write?org=$opt_influx_api_org&bucket=$opt_influx_bucket&precision=ns\" --header \"Authorization: Token $opt_influx_token\" --header \"Content-Type: text/plain; charset=utf-8\" --header \"Accept: application/json\" --data-binary '$influxlp'" cmd="curl --request POST \"$opt_influx_api_url/v2/write?org=$opt_influx_api_org&bucket=$opt_influx_bucket&precision=ns\" --header \"Authorization: Token $opt_influx_token\" --header \"Content-Type: text/plain; charset=utf-8\" --header \"Accept: application/json\" --data-binary '$influxlp'"
do_run "$cmd" do_run "$cmd"
fi fi
(( perf_vm_ok++ ))
end_process 0
} }
function do_housekeeping(){ function do_housekeeping(){
@@ -781,22 +786,28 @@ function do_run(){
function end_process(){ function end_process(){
local -i rc=$1; local -i rc=$1;
# if ! [[ -z "$startts" && -z "$endts" ]]; then local -i runtime
# local -i runtime=$(expr $endts - $startts) local -i bps
# local -i bps=$(expr $bytecount/$runtime) local -i ss_total
# fi local subject
# local subject="Ceph [VM:$vmok/$vmtotal SS:$snapshotok/$snapshottotal EX:$exportok/$exporttotal] [$(bytesToHuman "$bytecount")@$(bytesToHuman "$bps")/s]" if ! [[ -z "$startjob" && -z "$endjob" ]]; then
# [ $rc != 0 ] && subject="$subject [ERROR]" runtime=$(expr $endjob - $startjob)
bps=$(expr $perf_bytes_total/$runtime)
fi
ss_total=$(expr $perf_ss_ok + $perf_ss_failed)
subject="Crossover [VM:$perf_vm_ok/$vmcount SS:$perf_ss_ok/$ss_total]"
[ $rc != 0 ] && subject="[ERROR] $subject" || subject="[OK] $subject"
echo "Subject: $subject"
#send email #send email
# local mail; local mail;
# local mailhead="Backup $imgcount Images in $vmcount VMs (Bytes: $bytecount)" # local mailhead="Backup $perf Images in $vmcount VMs (Bytes: $bytecount)"
# for mail in $(echo "$opt_addr_mail" | tr "," "\n"); do for mail in $(echo "$opt_addr_mail" | tr "," "\n"); do
# do_run "cat '$LOG_FILE' | mail -s '$subject' '$mail'" do_run "cat '$LOG_FILE' | mail -s '$subject' '$mail'"
# done done
#remove log #remove log
# rm "$LOG_FILE" rm "$LOG_FILE"
exit "$rc"; exit "$rc";
} }