summaryrefslogtreecommitdiffstats
path: root/plugins/check_cluster_vol_usage.py
blob: 22256343ead35cdd788b5ae4c2e20a128daaf69b (plain)
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
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
53
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python
#
# check_cluster_vol_usage
# Aggregated cluster capacity utilization for a
# gluster cluster
# The plugin reads status data using mk-livestatus
# Assumptions:
#  - Volume utilization service names begin with "Volume-"
#  - Host name associated is cluster name
#  - All volume utilization output is of form
#    "used=<val>;warn;crit;min;max"
#
# Copyright (C) 2014 Red Hat Inc
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#

import sys
import re
from argparse import ArgumentParser
from plugins import livestatus


def checkVolumePerfData(clusterName):

    # Write command to socket
    cmd = "GET services\nColumns: description host_name " \
          "perf_data custom_variables\nFilter: " \
          "description ~~ %s\n" % 'Volume-'
    table = livestatus.readLiveStatus(cmd)
    totalUsed = 0.0
    totalAvail = 0.0
    for row in table:
        if len(row) <= 3:
            return 0.0, 0.0
        host = row[1]
        perf_data = row[2]
        if len(perf_data) > 2:
            perf_arr = perf_data.split(' ')
            used = perf_arr[2].split('=')[1]
            avail = perf_arr[1].split('=')[1]
            if host == clusterName:
                totalUsed += float(re.match(r'\d*\.?\d+', used).group())
                totalAvail += float(re.match(r'\d*\.?\d+', avail).group())
    return totalUsed, totalAvail

# Main method
if __name__ == "__main__":

    parser = ArgumentParser(description=
                            "Calculate the aggregate "
                            "capacity usage in cluster")
    parser.add_argument('-w', '--warning',
                        action='store',
                        type=int,
                        dest='warn',
                        help='Warning in %',
                        default=70)
    parser.add_argument('-c', '--critical',
                        action='store',
                        type=int,
                        dest='crit',
                        help='Critical threshold Warning in %',
                        default=95)
    parser.add_argument('-hg', '--host-group',
                        action='store',
                        type=str,
                        dest='hostgroup',
                        help='Name of cluster or hostgroup',
                        required=True)
    args = parser.parse_args()
    # Check the various performance statuses for the host
    used, avail = checkVolumePerfData(args.hostgroup)
    statusstr = "OK"
    exitstatus = 0
    if used == 0 and avail == 0:
        statusstr = "UNKNOWN"
        exitstatus = 3
    else:
        warn = int((args.warn * avail) / 100.0)
        crit = int((args.crit * avail) / 100.0)
        usedpercent = int((used/avail) * 100.0)
        if (usedpercent >= args.warn):
            statusstr = "WARNING"
            exitstatus = 1
        if (usedpercent >= args.crit):
            statusstr = "CRITICAL"
            exitstatus = 2

    print ("%s - used %s%% of available %s|used=%s;%s;%s;0;%s;"
           % (statusstr, usedpercent,
              avail, usedpercent, args.warn, args.crit, 100))
    sys.exit(exitstatus)