Blame view

check_btrfs/check_btrfs.sh 1.52 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
5
6
7
8
9
#!/bin/bash

function smart_disks() {
  if [[ $# != 1 ]]; then
    echo "Wrong number of arguments"
    return 3
  fi

  POOL=$1
10
  DISKS=$(sudo btrfs device stats -c / | grep -oP "^\[[^\]]+\]" | sed 's/[][]//g' | sort -u)
Imanol-Mikel Barba Sabariego authored
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
  ERR_OUTPUT=""
  HIGHEST_RES=0
  for disk in $DISKS; do
    OUTPUT=""
    RES=0
    if echo $disk | grep "nvme" > /dev/null; then
      OUTPUT=$(/usr/lib64/nagios/plugins/check_nvme $disk)
      RES=$?
    else
      OUTPUT=$(/usr/lib64/nagios/plugins/check_smart $disk)
      RES=$?
    fi
    if [[ $RES != 0 ]]; then
      ERR_OUTPUT="$ERR_OUTPUT - $OUTPUT"
    fi
    if [[ $RES -gt $HIGHEST_RES ]]; then
      HIGHEST_RES=$RES
    fi
  done

  echo -n "BTRFS POOL SMART $POOL "
  if [[ $HIGHEST_RES == 1 ]]; then
    echo "WARNING${ERR_OUTPUT}"
    return 1
  elif [[ $HIGHEST_RES == 2 ]]; then
    echo "CRITICAL${ERR_OUTPUT}"
    return 2
  elif [[ $HIGHEST_RES == 3 ]]; then
    echo "UNKNOWN${ERR_OUTPUT}"
    return 3
  fi

  echo "OK"
  return 0
}

function pool_errors() {
  if [[ $# != 1 ]]; then
    echo "Wrong number of arguments"
    return 3
  fi
53
  OUTPUT="$(sudo btrfs device stats -c $1)"
Imanol-Mikel Barba Sabariego authored
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
  STATUS=$?

  if [[ $STATUS != 0 ]]; then
    echo "BTRFS POOL ERRORS $1 - CRITICAL: $OUTPUT"
    return 2
  fi

  echo "BTRFS POOL ERRORS $1 - OK"
  return 0
}

if [[ $# -lt 2 ]]; then
  echo "Wrong number of arguments"
  exit 3
fi

ACTION=$1
POOL=$2

if [[ $ACTION == "smart" ]]; then
  smart_disks $POOL
  exit $?
elif [[ $ACTION == "errors" ]]; then
  pool_errors $POOL
  exit $?
fi
echo "Unknown command: $ACTION"
exit 3