check_lvmthin
changeset 4 896ffeb44d7c
parent 3 11e1f1430bfd
equal deleted inserted replaced
3:11e1f1430bfd 4:896ffeb44d7c
     1 #!/bin/sh -e
       
     2 
       
     3 ME=$(basename $0)
       
     4 NAME='LVMTHIN'
       
     5 E=3
       
     6 INFO=
       
     7 PERFDATA=
       
     8 
       
     9 lvsresults=$(mktemp /tmp/$ME.XXX)
       
    10 
       
    11 cleanup() {
       
    12     [ -n "$lvsresults" ] && rm $lvsresults;
       
    13 }
       
    14 
       
    15 nagiosexit() {
       
    16     local STATUS='UNKNOWN'
       
    17     if [ $E = '2' ]; then STATUS='CRITICAL'
       
    18     elif [ $E = '1' ]; then STATUS='WARNING'
       
    19     elif [ $E = '0' ]; then STATUS='OK'
       
    20     fi
       
    21     echo "$NAME $STATUS${INFO:+: ${INFO}}${PERFDATA:+|$PERFDATA}"
       
    22     exit $E
       
    23 }
       
    24 
       
    25 trap 'trap - EXIT; cleanup; nagiosexit $E' INT QUIT TERM EXIT
       
    26 
       
    27 die() {
       
    28     INFO="$@"
       
    29     exit 3
       
    30 }
       
    31 
       
    32 parse_lvs() {
       
    33     results=$1
       
    34     label=$2
       
    35     local id=
       
    36     if [ ! -s "$results" ]; then
       
    37         id=$(id -u)
       
    38         die "No volumes found (effective user ID: $id)"
       
    39     fi
       
    40     while read vg lv u; do
       
    41         [ -n "$vg" -a -n "$lv" -a -n "$u" ] || die "Can't parse results"
       
    42         addinfo=
       
    43         if [ "$u" '>' "$C" ]; then
       
    44             E=2
       
    45             addinfo='yes'
       
    46         elif [ "$u" '>' "$W" ]; then
       
    47             [ $E != '2' ] && E=1
       
    48             addinfo='yes'
       
    49         fi 
       
    50         [ -n "$addinfo" ] && INFO="${INFO:+${INFO}; }$vg/$lv$label $u%"
       
    51         PERFDATA="${PERFDATA:+${PERFDATA} }$vg/$lv$label=$u%;$W;$C"
       
    52     done <$results
       
    53 }
       
    54 
       
    55 W='80.00'
       
    56 C='90.00'
       
    57 T=
       
    58 P=
       
    59 
       
    60 while getopts "hm?c:p:t:w:" opt; do
       
    61   case $opt in
       
    62     h|\?)
       
    63         cleanup; exec pod2usage -verbose 1 -exit 0 $0
       
    64         ;;
       
    65     m)
       
    66         cleanup; exec pod2usage -verbose 3 -exit 0 $0
       
    67         ;;
       
    68     c)
       
    69         C="$OPTARG"
       
    70         ;;
       
    71     p)
       
    72         P="$P $OPTARG"
       
    73         ;;
       
    74     t)
       
    75         T=$T $OPTARG
       
    76         ;;
       
    77     w)
       
    78         W=$OPTARG
       
    79         ;;
       
    80   esac
       
    81 done
       
    82 shift $((OPTIND-1))
       
    83 
       
    84 lvs --noheadings -S 'segtype=thin||segtype=thin-pool' -o vg_name,lv_name,data_percent $T $P >$lvsresults; parse_lvs $lvsresults
       
    85 [ -n "$T" -a -z "$P" ] || lvs --noheadings -S 'segtype=thin-pool' -o vg_name,lv_name,metadata_percent $P >$lvsresults; parse_lvs $lvsresults '(meta)'
       
    86 
       
    87 [ $E = '3' ] && E=0
       
    88 exit $E
       
    89 
       
    90 : <<EOP
       
    91 
       
    92 =encoding utf8
       
    93 
       
    94 =pod
       
    95 
       
    96 =head1 NAME
       
    97 
       
    98 check_lvmthin - check usage of thin provisioned logical volumes (lvm)
       
    99 
       
   100 =head1 SYNOPSIS
       
   101 
       
   102 check_lvmthin [-t volume] [-p volume]
       
   103 
       
   104 check_lvmthin -h|-?|-m
       
   105 
       
   106 =head1 DESCRIPTION
       
   107 
       
   108 This script checks the usage of thin provisioned logical volumes.
       
   109 
       
   110 =head1 OPTIONS
       
   111 
       
   112 =over
       
   113 
       
   114 =item B<-h|-?>
       
   115 
       
   116 Display help.
       
   117 
       
   118 =item B<-m>
       
   119 
       
   120 Display man page.
       
   121 
       
   122 =item B<-c> I<threshold>
       
   123 
       
   124 Exit with status 'CRITICAL' if any checked volume uses more than I<threshold> percent of its size.
       
   125 
       
   126 =item B<-t> I<thinlv>
       
   127 
       
   128 check usage of thin provisioned logical volume I<thinlv>.
       
   129 
       
   130 =item B<-p> I<poollv>
       
   131 
       
   132 check data and metadata usage of pool volume I<poollv>.
       
   133 
       
   134 =item B<-w> I<threshold>
       
   135 
       
   136 Exit with status 'WARNING' if any checked volume uses more than I<threshold> percent of its size.
       
   137 
       
   138 =back
       
   139 
       
   140 I<thinlv> and I<poollv> should be given as names understood by lvs. If both are omitted, then data usage for all thinlv and poollv and metadata usage for all poollv is checked. Thresholds should be given in decimal notation with 2 positions after the decimal point.
       
   141 
       
   142 =head1 EXAMPLE
       
   143 
       
   144 check_lvmthin -w 76.54 -t vg/lv
       
   145 
       
   146 =head1 AUTHOR
       
   147 
       
   148 Matthias Förste <foerste@schlittermann.de>
       
   149 
       
   150 The lastest version is available at L<https://ssl.schlittermann.de/hg/ius/nagios/nagios-plugin-lvmthin/>
       
   151 
       
   152 =cut
       
   153 
       
   154 EOP