-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_vsphere
More file actions
executable file
·188 lines (170 loc) · 5.6 KB
/
check_vsphere
File metadata and controls
executable file
·188 lines (170 loc) · 5.6 KB
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
#!/usr/bin/env python
from __future__ import print_function
# Copyright: Chris Cowley 2014
# 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 St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""
Usage: check_vsphere TEST
check_vsphere [options] dsusage
check_vsphere --version
Options:
-T Test to run (required)
--version show version and exit
-h --help
-v --verbose Output more information
-H --host vSphere server to interogate
-U --username vSphere Username (username@domain if using SSO/AD)
-P --password Password
-W --warning Warning Threshold
-C --critical Critical Threshold
Host, username and password can also be defined using the environment variable:
VI_SERVER
VI_USERNAME
VI_PASSWORD
Currently the only test is "dsusage" which reports on the amount of space
available on each datastore.
"""
__author__ = "Chris Cowley"
__title__ = "Nagios plugin to check vSphere for various stuff"
__version__ = 0.1
from pysphere import VIServer, VIProperty
from pysphere.resources import VimService_services as VI
import argparse
import os
import errno, sys
# Standard Nagios return codes
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
def error(*objs):
print ("ERROR: ", *objs, file=sys.stderr)
def warning(*objs):
print ("WARNING: ", *objs, file=sys.stderr)
parser = argparse.ArgumentParser(description=__title__)
parser.add_argument('-H', '--hostname', help='vSphere server to interogate')
parser.add_argument('-U', '--username', help='vSphere Username (username@domain')
parser.add_argument('-P', '--password', help='vSpherePassword')
parser.add_argument('-v', '--verbose', help='Verbose output')
parser.add_argument('-T', '--test', help='test to run', required=True)
args = parser.parse_args()
# TODO: add suitable stderr messages
if args.hostname != None:
VI_SERVER = args.hostname
elif "VI_SERVER" in os.environ:
VI_SERVER = os.getenv("VI_SERVER")
else:
error("vSphere server not defined")
sys.exit(UNKNOWN)
if args.username !=None:
VI_USERNAME = args.username
elif "VI_USERNAME" in os.environ:
VI_USERNAME = os.getenv("VI_USERNAME")
else:
error("vSphere username not defined")
sys.exit(UNKNOWN)
if args.password != None:
VI_PASSWORD = args.password
elif "VI_PASSWORD" in os.environ:
VI_PASSWORD = os.getenv("VI_PASSWORD")
else:
error("vSphere password not defined")
sys.exit(UNKNOWN)
TEST = args.test
server = VIServer()
try:
server.connect(VI_SERVER, VI_USERNAME, VI_PASSWORD)
except:
print('Failed to connect to vSphere Server')
exit(3)
def dsSpaceCheck():
status = OK
perfdataarray = []
for ds, name in server.get_datastores().items():
props = VIProperty(server, ds)
capacity = props.summary.capacity
capacityGB = capacity / (1024 ^ 3)
freeSpace = props.summary.freeSpace
freeSpaceGB = freeSpace / 1024 / 1024 / 1024
try:
uncommited = props.summary.uncommited
overprov = used + uncommited
overprovPercent = (overprov / capacity) * 100
if overprovPercent > 100:
message = "WARNING: A datastore is over-commited"
status = WARNING
break
elif overprovPercent > 150:
message = "CRITICAL: A datastore is REALLY over-commited"
status = CRITICAL
break
else:
pass
except AttributeError:
pass
used = capacity - freeSpace
freePercent = round(100 - (abs(float(used) / float(capacity)) * 100),1)
perfdataarray.append (name + '=' + str(freePercent) + '%;20;10')
if freePercent < 10:
message = "CRITICAL: A datastore has less than 10% free"
status = CRITICAL
elif freePercent < 20:
if status != CRITICAL:
message = "WARNING: A datastore has less than 20% free"
status = WARNING
else:
pass
if status == OK:
message = "OK: All Datastores have sufficient space"
perfdata = ""
for dsdata in perfdataarray:
perfdata = perfdata + dsdata + " "
message = message + " | " + perfdata
print (message)
return status
def dsIOCheck():
status = OK
vmlist = server.get_registered_vms(status='poweredOn')
for vmpath in vmlist:
print (vmpath)
pm = server.get_performance_manager()
vm = server.get_vm_by_path(vmpath)
mor = vm._mor
counterValues = pm.get_entity_counters(mor)
#print (counterValues) #virtualDisk.readLatencyUS
# readIOPS = counterValues['virtualDisk.numberReadAveraged']
# writeIOPS = counterValues['virtualDisk.numberWriteAveraged']
readLatency = counterValues['virtualDisk.readLatencyUS']
writeLatency = counterValues['virtualDisk.writeLatencyUS']
# IOPS = readIOPS + writeIOPS
print(readLatency, writeLatency)
return status
def alarmsCheck():
am = server._do_service_content.AlarmManager
request = VI.GetAlarm
def testRun():
pass
if __name__ == '__main__':
if TEST == 'dsusage':
status = dsSpaceCheck()
exit(status)
elif TEST == 'iolatency':
status = dsIOCheck()
exit(status)
elif TEST == 'testrun':
testRun()
else:
print ('UNKNOWN: incorrect test defined (dsusage|iolatency are accepted)')
exit(UNKNOWN)