btrfs-textcollector.sh 1.33 KB
Newer Older
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
#!/bin/bash

set -o errexit
set -o nounset

if (( $# != 1 )); then
  echo "Missing textcollector directory argument"
  exit 1
fi

TEXTFILE_COLLECTOR_DIR=${1}
PROM_FILE=$TEXTFILE_COLLECTOR_DIR/btrfs.prom

TMP_FILE=$PROM_FILE.$$
[ -e $TMP_FILE ] && rm -f $TMP_FILE

trap "rm -f $TMP_FILE" EXIT

list_btrfs_submounts=$(sudo btrfs filesystem show | awk '/ path /{print $NF}')

# Errors outputed by btrfs device stats /
btrfs_errors=(write_io_errs read_io_errs flush_io_errs corruption_errs generation_errs)

for btrfs_mount in ${list_btrfs_submounts[@]}; do
  for btrfs_error in "${btrfs_errors[@]}"
26
27
28
29
30
31
32
33
34
35
36
37
  do
    echo "# HELP btrfs_${btrfs_error} error" >> $TMP_FILE
    echo "# TYPE btrfs_${btrfs_error} gauge" >> $TMP_FILE
  done
  # Break, as we can only have one HELP/TYPE line
  break
done

for btrfs_mount in ${list_btrfs_submounts[@]}; do
  for btrfs_error in "${btrfs_errors[@]}"
    #echo "# HELP btrfs_${btrfs_error} error" >> $TMP_FILE
    #echo "# TYPE btrfs_${btrfs_error} gauge" >> $TMP_FILE
38
39
40
41
42
43
44
45
46
47
48
  do
    jq_filter=".[\"device-stats\"][].${btrfs_error}"
    errors=$(sudo btrfs --format json device stats $btrfs_mount | jq -r ${jq_filter})

    device=$(sudo btrfs --format json device stats $btrfs_mount | jq -r '.["device-stats"][].device')

    echo "btrfs_${btrfs_error}{device=\"${device}\"} ${errors}" >> $TMP_FILE
  done
done

mv -f $TMP_FILE $PROM_FILE