Nagios check_datastore

Check esxi datastore free space

#!/usr/bin/python
from pysphere import VIServer, VIProperty
import sys

ok = 160000
warn_value = 150000
crit_value = 100000
status = { 'OK' : 0 , 'WARNING' : 1, 'CRITICAL' : 2 , 'UNKNOWN' : 3}

server = sys.argv[1]
user = sys.argv[2]
pwd = sys.argv[3]

warn = False
crit = False
lowestFreeSpace = None

s = VIServer()
s.connect(server, user, pwd)
for ds, name in s.get_datastores().items():
props = VIProperty(s, ds)
freeSpace = props.summary.freeSpace / 1024**2
if lowestFreeSpace == None or freeSpace < lowestFreeSpace: lowestFreeSpace = freeSpace lowestDatastoreName = name s.disconnect() if lowestFreeSpace < crit_value: crit = True if lowestFreeSpace < warn_value: warn = True if crit_value >= warn_value:
crit = True

if warn == True:
if crit == True:
print lowestDatastoreName, "Critical space, less than 100G"
sys.exit(status['CRITICAL'])
else:
print lowestDatastoreName, "Running low on space, less than 150G"
sys.exit(status['WARNING'])

else:
print 'status OK, datastore has enough space'
sys.exit(status['OK'])