summaryrefslogtreecommitdiffstats
path: root/plugins/check_vol_utilization_server.py
blob: 8b08f21b74e1112c4cd6b6e851da94c6667922a6 (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
#!/usr/bin/python
import sys
import commands
import random
import argparse
import livestatus

_NRPEPath = "/usr/lib64/nagios/plugins/check_nrpe"


def excecNRPECommand(host):
    #Get the address of the host
    answer = livestatus.checkLiveStatus("GET hosts\nColumns: address\n"
                                        "Filter: display_name = "
                                        + host + "\n")
    command = _NRPEPath + " -H " + answer.rstrip() + " -c " \
        "check_vol_utilization -a " + args.volume + " " + \
        str(args.warning) + " " + str(args.critical)
    status = commands.getoutput(command)
    return status


def showVolumeUtilization(args):
    table = livestatus.readLiveStatus("GET hostgroups\nColumns: members name\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
    host = random.choice(list_hosts)
    status = excecNRPECommand(host)
    #if success return from here
    if "Volume Utilization" in status:
        return status
    #radom 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 = excecNRPECommand(host)
        #if success return from here
        if "Volume Utilization" in status:
            return status
            break
    return status


def parse_input():
    parser = argparse.ArgumentParser(
        usage='%(prog)s [-h] <hostgroup>  <volume> -w <Warning> -c <Critical>')
    parser.add_argument(
        "hostgroup",
        help="Name of the hostgroup in which the volume belongs to")
    parser.add_argument(
        "volume",
        help="Name of the volume to get the Utilization")
    parser.add_argument(
        "-w",
        "--warning",
        action="store",
        type=int,
        help="Warning Threshold in percentage")
    parser.add_argument(
        "-c",
        "--critical",
        action="store",
        type=int,
        help="Critical Threshold in percentage")
    args = parser.parse_args()
    if not args.critical or not args.warning:
        print "UNKNOWN:Missing critical/warning threshold value."
        sys.exit(3)
    if args.critical <= args.warning:
        print "UNKNOWN:Critical must be greater than Warning."
        sys.exit(3)
    return args

if __name__ == '__main__':
    args = parse_input()
    status = showVolumeUtilization(args)
    print status