-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_storage.py
executable file
·83 lines (74 loc) · 3.46 KB
/
check_storage.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import sys
import math
import argparse
# https://code.google.com/p/pysphere/
from pysphere import VIServer, VIProperty
# From http://stackoverflow.com/a/14822210/2001268
def convertSize(size):
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(math.fabs(size),1024)))
p = math.pow(1024,i)
s = round(size/p,2)
if (s > 0):
return '%s%s' % (s,size_name[i])
else:
return '0B'
parser = argparse.ArgumentParser(description='Check for WMware datastore space')
parser.add_argument('-H', '--host', type=str, help='vSphere Host', required=True)
parser.add_argument('-u', '--user', type=str, help='Username', required=True)
parser.add_argument('-p', '--password', type=str, help='Password', required=True)
parser.add_argument('-w', '--warn', type=int, help='Warn percent left', required=False, default=20)
parser.add_argument('-c', '--critical', type=int, help='Critical percent left', required=False, default=10)
args = parser.parse_args()
server = VIServer()
server.connect(args.host, args.user, args.password)
serviceoutput = "UNKNOWN Error"
perfdata = ""
actualexitflag = 0
actualserviceoutput = "OK all storage ok"
for ds_mor, name in server.get_datastores().items():
exitflag = 3
props = VIProperty(server, ds_mor)
datastorename = name
if datastorename == 'VMArchives': # Ignores a datastore. This one is always full...
continue
# print "----------------------------------------"
# print "Datastore %s" % datastorename
# print "capacity %d" % props.summary.capacity
# print "freeSpace %d" % props.summary.freeSpace
if hasattr(props.summary, "uncommitted"):
# print "uncommitted %d" % props.summary.uncommitted
usedspace = (props.summary.capacity - props.summary.freeSpace) + props.summary.uncommitted
freespace = props.summary.freeSpace - props.summary.uncommitted
else:
usedspace = props.summary.capacity - props.summary.freeSpace
freespace = props.summary.freeSpace
warnsize = props.summary.capacity * (1 - args.warn / 100)
critsize = props.summary.capacity * (1 - args.critical / 100)
maxspace = props.summary.capacity
while exitflag >= 3:
if usedspace >= critsize:
exitflag = 2
# print "CRITICAL: %d > %d" % (usedspace, critsize)
serviceoutput = "CRITICAL " + datastorename + " has " + convertSize(usedspace) + " provisioned. Alerts at " + convertSize(critsize) + ". Datastore maxsize is " + convertSize(maxspace) + "|"
elif usedspace >= warnsize:
exitflag = 1
# print "WARNING: %d > %d" % (usedspace, warnsize)
serviceoutput = "WARN " + datastorename + " has " + convertSize(usedspace) + " provisioned. Alerts at " + convertSize(warnsize) + ". Datastore maxsize is " + convertSize(maxspace) + "|"
else:
exitflag = 0
# print "OK: %d" % (usedspace)
serviceoutput = "OK " + datastorename + " has " + convertSize(usedspace) + " provisioned. Alerts at " + convertSize(warnsize) + ". Datastore maxsize is " + convertSize(maxspace) + "|"
if exitflag >= 1:
actualexitflag = exitflag
actualserviceoutput = serviceoutput
perfdata += " '"+ datastorename +"'="
perfdata += "%dB;" % usedspace
perfdata += "%d;%d;0;%d" % (warnsize, critsize, maxspace)
print actualserviceoutput,
print perfdata
server.disconnect()
sys.exit(actualexitflag)