summaryrefslogtreecommitdiffstats
path: root/plugins/check_vol_server.py
blob: 1cc043f82b7f539f88668fe8ca19422e1a74746c (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/python
import sys
import commands
import json
import random
import argparse
import livestatus
import os

from glusternagios import utils
from constants import NRPE_PATH


def _getListHosts(args):
    table = livestatus.readLiveStatus("GET hostgroups\nColumns: members\n"
                                      "Filter: name = "
                                      + args.hostgroup + "\n")
    tab1 = table[0]
    list_hosts = tab1[0].split(",")
    #First take a random host from the group and send the request
    return list_hosts


def _getHostAddress(host):
    # Get the address of the host
    host_address = livestatus.checkLiveStatus("GET hosts\nColumns: address\n"
                                              "Filter: display_name = "
                                              + host + "\n")
    return host_address.rstrip()


def _getVolUtilizationNRPECommand(args):
    return ("check_vol_utilization -a " + args.volume + " " +
            str(args.warning) + " " + str(args.critical))


def _getVolStatusNRPECommand(args):
    return ("check_vol_status -a %s %s" % (args.volume, 'info'))


def _getVolQuotaStatusNRPECommand(args):
    return ("check_vol_status -a %s %s" % (args.volume, 'quota'))


def _getVolSelfHealStatusNRPECommand(args):
    return ("check_vol_status -a %s %s" % (args.volume, 'self-heal'))


def _getVolGeoRepStatusNRPECommand(args):
    return ("check_vol_status -a %s %s" % (args.volume, 'geo-rep'))


def _getNRPEBaseCmd(host):
    return NRPE_PATH + " -H " + host + " -c "


def execNRPECommand(command):
    status, output = commands.getstatusoutput(command)
    return os.WEXITSTATUS(status), output


def _getVolumeStatusOutput(args):
    status, output_text = _executeRandomHost(_getVolStatusNRPECommand(args))

    output = output_text
    # If status OK, volume info will be available as part of the output
    if status == utils.PluginStatusCode.OK:
        lines = output_text.split('\n')
        if len(lines) > 1:
            output = lines[0]
            volumes = json.loads(lines[1])
            volume = volumes[args.volume]
            criticalBricks = 0
            for brick in volume['bricks']:
                brick_status = livestatus.checkLiveStatus(
                    "GET services\n"
                    "Columns: state\n"
                    "Filter: description = "
                    "Brick Status - %s\n"
                    % brick)
                if brick_status and brick_status.strip():
                    servicestatus = brick_status.strip()
                    if int(servicestatus) == utils.PluginStatusCode.CRITICAL:
                        criticalBricks += 1

            if criticalBricks > 0:
                if int(volume['brickCount']) == criticalBricks:
                    status = utils.PluginStatusCode.CRITICAL
                    output = "All the bricks are in CRITICAL state"
                else:
                    status = utils.PluginStatusCode.WARNING
                    output = "One or more bricks are in CRITICAL state"
    return status, output


def _getVolumeQuotaStatusOutput(args):
    # get current volume quota status
    table = livestatus.readLiveStatus("GET services\n"
                                      "Columns: state long_plugin_output\n"
                                      "Filter: description = "
                                      "Volume Status Quota - %s" % args.volume)
    servicestatus = utils.PluginStatusCode.UNKNOWN
    statusoutput = ''
    if len(table) > 0:
        servicetab = table[0]
        servicestatus = servicetab[0]
        statusoutput = servicetab[1]
    if (int(servicestatus) == utils.PluginStatusCode.OK and
            statusoutput.find("QUOTA: OK") > -1):
        # if ok, don't poll
        return servicestatus, statusoutput
    return _executeRandomHost(_getVolQuotaStatusNRPECommand(args))


def _executeRandomHost(command):
    list_hosts = _getListHosts(args)
    host = random.choice(list_hosts)
    #Get the address of the host
    host_address = _getHostAddress(host)

    status, output = execNRPECommand(_getNRPEBaseCmd(host_address) + command)

    if status != utils.PluginStatusCode.UNKNOWN:
        return status, output
    #random host is not able to execute the command
    #Now try to iterate through the list of hosts
    #in the host group and send the command until
    #the command is successful
    for host in list_hosts:
        status, output = execNRPECommand(_getNRPEBaseCmd(_getHostAddress(host))
                                         + command)
        if status != utils.PluginStatusCode.UNKNOWN:
            return status, output
    return status, output


def showVolumeOutput(args):

    if args.option == 'status':
        return _getVolumeStatusOutput(args)
    elif args.option == 'utilization':
        command = _getVolUtilizationNRPECommand(args)
    elif args.option == 'quota':
        return _getVolumeQuotaStatusOutput(args)
    elif args.option == 'self-heal':
        command = _getVolSelfHealStatusNRPECommand(args)
    elif args.option == 'geo-rep':
        command = _getVolGeoRepStatusNRPECommand(args)

    return _executeRandomHost(command)


def parse_input():
    parser = argparse.ArgumentParser(
        usage='%(prog)s [-h] <hostgroup>  <volume> -w <Warning>'
        ' -c <Critical> [-o|--option]')
    parser.add_argument(
        "hostgroup",
        help="Name of the hostgroup to which the volume belongs")
    parser.add_argument(
        "volume",
        help="Name of the volume being queried")
    parser.add_argument(
        "-w",
        "--warning",
        action="store",
        type=int,
        default=70,
        help="Warning Threshold in percentage")
    parser.add_argument(
        "-c",
        "--critical",
        action="store",
        type=int,
        default=90,
        help="Critical Threshold in percentage")
    parser.add_argument('-o', '--option',
                        action='store',
                        help='the volume option to check',
                        choices=['utilization',
                                 'status',
                                 'quota',
                                 'self-heal',
                                 'geo-rep'])
    args = parser.parse_args()
    if args.critical <= args.warning:
        print "UNKNOWN:Critical must be greater than Warning."
        sys.exit(utils.PluginStatusCode.UNKNOWN)
    return args

if __name__ == '__main__':
    args = parse_input()
    status, output = showVolumeOutput(args)
    print (output)
    exit(status)