0

I have written a bash script which I want to use to monitor backups on a synology via pushgateway.

The script should search for subfolders in the backup folder, write the newest file into a variable and write the age and size of the file into an array.

To give it finally to the push gateway, I list all metrics with indexes. All folders or files are available. If I execute the script, often one or more indexes are not found. If I execute the commands manually one by one, I get a correct output.

Here is the script:

#!/bin/bash
set -e

backup_dir=$1

for dir in $(find "$backup_dir" -maxdepth 1 -mindepth 1 -type d \( ! -name @eaDir \)); do
    if compgen -G "${dir}/*.vib" > /dev/null; then
        latest_vib=$(ls -t1 "$dir"/*.vib | head -1)
        age_vib=$(( ( $(date +%s) - $(stat -c %Y "$latest_vib") ) ))
        size_vib=$(stat -c %s "$latest_vib")

        arrage_vib+=("${age_vib}")
        arrsize_vib+=("${size_vib}")
    fi
    if compgen -G "${dir}/*.vbk" > /dev/null; then
        latest_vbk=$(ls -t1 "$dir"/*.vbk | head -1)
        age_vbk=$(( ( $(date +%s) - $(stat -c %Y "$latest_vbk") ) ))
        size_vbk=$(stat -c %s "$latest_vbk")

        arrage_vbk+=("${age_vbk}")
        arrsize_vbk+=("${size_vbk}")
    fi

    min_dir=$(echo "$dir" | cut -d'/' -f4- | tr '[:upper:]' '[:lower:]')
    sign_dir=${min_dir//_/-}
    arrdir+=("${sign_dir}")
done

echo "${arrdir[4]}"
echo "${arrage_vib[4]}"

cat << EOF | curl -ks -u user:pw --data-binary @- https://pushgateway/metrics/job/backup/instance/instance_name
    # HELP backup_age displays the age of backups in seconds
    # TYPE backup_age gauge
    backup_age_vib{dir="${arrdir[1]}"} ${arrage_vib[1]}
    backup_age_vib{dir="${arrdir[2]}"} ${arrage_vib[2]}
    backup_age_vib{dir="${arrdir[3]}"} ${arrage_vib[3]}
    backup_age_vib{dir="${arrdir[4]}"} ${arrage_vib[4]}
    backup_age_vbk{dir="${arrdir[1]}"} ${arrage_vbk[1]}
    ...
    # HELP backup_size displays the size of backups in bytes
    # TYPE backup_size gauge
    backup_size_vib{dir="${arrdir[1]}"} ${arrsize_vib[1]}
    ...
EOF

I hope you can help me and point out where I made a mistake. I am also open for general optimizations of the script, because I assume that it can be solved better and more performant or optimal. I have a few lines of code from here ;-).

Many thanks in advance.

  • 1
    What output do you get with `[@]` instead of `[4]`? – choroba Apr 01 '21 at 10:45
  • If I change the echoes to `[@]` I will get all the correct paths. I think here is the problem, that I do not count from 0 (I deploy the script with ansible). But I have another node, where the errors happen randomly. – rhizoet Apr 01 '21 at 10:51
  • after the 2x `echo` commands add `typeset -p arrdir arrage_vib arrsize_vbk`; this will display the indices and values for all 3x arrays; perhaps this could shine a light on what's missing/incorrect? – markp-fuso Apr 01 '21 at 12:48
  • After I started with the command above I saw that I had a `elif`, instead of a `if` as in the script, locally. This caused me not to see `arrsize_vbk` in the first run. Now all values are displayed to me and the script runs through. In the pushgateway I can also see the metrics. I think it was also because I had used `loop.index` instead of `loop.index0` with Ansible. Now the script starts counting at 0 and runs through. Thanks for all the tips. This has brought me to the solution. Do you guys have any tips on how to optimize the code, if possible? – rhizoet Apr 01 '21 at 14:15

0 Answers0