-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_openstack_universal.py
More file actions
executable file
·393 lines (317 loc) · 14.5 KB
/
Copy pathcheck_openstack_universal.py
File metadata and controls
executable file
·393 lines (317 loc) · 14.5 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/python3 -W ignore
import subprocess, sys, json
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
parser = ArgumentParser(description=__doc__,
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-s", "--service",
dest="openstack_service",
type=str,
default='nova',
help="Choose which openstack service to check")
"""
parser.add_argument("-m", "--metric",
dest="smart_metrics",
default=[],
action='append',
help="Smart Metric to check")
"""
parser.add_argument("-i", "--instance",
dest="openstack_instance",
type=str,
help="Choose which openstack instance to check")
parser.add_argument("-d", "--debug",
dest="action_debug",
action='store_true',
default=False,
help="Enables output mode: debug")
parser.add_argument("--ignore",
dest="ignore_list",
default=[],
action='append',
help="List of Containers to ignore from status check")
args = parser.parse_args()
def sanity_check():
openstack_services = ["nova", "cinder", "lxc", "compute", "neutron", "galera", "instance"]
if args.openstack_service not in openstack_services:
print(f"Unexpected service {args.openstack_service}")
sys.exit(3)
else:
if args.action_debug:
print(f"Matched servicecheck {args.openstack_service}. Proceeding with check")
def get_lxc_containers():
container_cmd = ["sudo", "/usr/bin/lxc-ls", "--fancy"]
containers = subprocess.check_output(container_cmd).decode('utf-8').splitlines()
for line in containers:
utility_container = [s for s in line.split() if "utility_container" in s]
if len(utility_container) > 0:
break
for line in containers:
galera_container = [s for s in line.split() if "galera_container" in s]
if len(galera_container) > 0:
break
return containers, utility_container[0], galera_container[0]
def check_lxc_status(input):
input.pop(0)
status = 0
message = ""
for line in input:
line_list = line.split()
name = line_list[0]
state = line_list[1]
if name in args.ignore_list:
continue
if state != 'RUNNING':
message += f"PROBLEM: Found container {name} in state ({state})\n"
if status > 1:
status = 2
else:
status = 1
if message == "":
message = "OK: All LXC Containers are running"
return status, message
def check_nova_services(utility_container):
nova_services_cmd = f"sudo su - root -c \"lxc-attach {utility_container} -- /usr/bin/bash -c \'source /root/openrc && nova service-list\'\""
nova_services = subprocess.check_output(nova_services_cmd, shell=True).decode('utf-8').splitlines()
nova_services.pop(0)
nova_services.pop(0)
nova_services.pop(0)
nova_services.pop(-1)
message = ""
status = 0
for line in nova_services:
line_splt = line.split('|')
line_splt.remove('')
svc_type = line_splt[1].strip()
svc_name = line_splt[2].strip()
svc_status = line_splt[5].strip()
if args.action_debug:
print(f"Checking Nova Service {svc_type} {svc_name} - Status: {svc_status}")
if svc_status.lower() != "up":
message += f"PROBLEM: Found NOVA service {svc_type} {svc_name} in state ({svc_status})\n"
if status > 1:
status = 2
else:
status = 1
if message == "":
message = "OK: All Nova Services are running"
return status, message
def check_cinder_services(utility_container):
cinder_services_cmd = f"sudo su - root -c \"lxc-attach {utility_container} -- /usr/bin/bash -c \'source /root/openrc && cinder service-list\'\""
cinder_services = subprocess.check_output(cinder_services_cmd, shell=True).decode('utf-8').splitlines()
cinder_services.pop(0)
cinder_services.pop(0)
cinder_services.pop(0)
cinder_services.pop(-1)
message = ""
status = 0
for line in cinder_services:
line_splt = line.split('|')
line_splt.remove('')
svc_type = line_splt[0].strip()
svc_name = line_splt[1].strip()
svc_status = line_splt[4].strip()
if args.action_debug:
print(f"Checking Cinder Service {svc_type} {svc_name} - Status: {svc_status}")
if svc_status.lower() != "up":
message += f"PROBLEM: Found Cinder service {svc_type} {svc_name} in state ({svc_status})\n"
if status > 1:
status = 2
else:
status = 1
if message == "":
message = "OK: All Cinder Services are running"
return status, message
def check_compute_services(utility_container):
compute_services_cmd = f"sudo su - root -c \"lxc-attach {utility_container} -- /usr/bin/bash -c \'source /root/openrc && openstack compute service list\'\""
compute_services = subprocess.check_output(compute_services_cmd, shell=True).decode('utf-8').splitlines()
compute_services.pop(0)
compute_services.pop(0)
compute_services.pop(0)
compute_services.pop(-1)
message = ""
status = 0
for line in compute_services:
line_splt = line.split('|')
line_splt.remove('')
svc_type = line_splt[1].strip()
svc_name = line_splt[2].strip()
svc_status = line_splt[5].strip()
if args.action_debug:
print(f"Checking Compute Service {svc_type} {svc_name} - Status: {svc_status}")
if svc_status.lower() != "up":
message += f"PROBLEM: Found Compute service {svc_type} {svc_name} in state ({svc_status})\n"
if status > 1:
status = 2
else:
status = 1
if message == "":
message = "OK: All Compute Services are running"
return status, message
def check_neutron_services(utility_container):
neutron_services_cmd = f"sudo su - root -c \"lxc-attach {utility_container} -- /usr/bin/bash -c \'source /root/openrc && openstack network agent list\'\""
neutron_services = subprocess.check_output(neutron_services_cmd, shell=True).decode('utf-8').splitlines()
neutron_services.pop(0)
neutron_services.pop(0)
neutron_services.pop(0)
neutron_services.pop(-1)
message = ""
status = 0
for line in neutron_services:
line_splt = line.split('|')
line_splt.remove('')
svc_type = line_splt[1].strip()
svc_name = line_splt[2].strip()
svc_status = line_splt[5].strip()
if args.action_debug:
print(f"Checking Neutron Agent {svc_type} {svc_name} - Status: {svc_status}")
if svc_status.lower() != "up":
message += f"PROBLEM: Found Neutron Agent {svc_type} {svc_name} in state ({svc_status})\n"
if status > 1:
status = 2
else:
status = 1
if message == "":
message = "OK: All Neutron Agents are running"
return status, message
def check_galera_status(galera_container):
wsrep_cmd = f"sudo lxc-attach {galera_container} -- /usr/bin/bash -c \"mysql -e \'SHOW GLOBAL STATUS;\';\""
wsrep_out = subprocess.check_output(wsrep_cmd, shell=True).decode('utf-8').splitlines()
max_connections_cmd = f"sudo lxc-attach {galera_container} -- /usr/bin/bash -c \"mysql -e \'SHOW VARIABLES LIKE \\\"max_connections\\\";\';\""
max_connections_out = subprocess.check_output(max_connections_cmd, shell=True).decode('utf-8').splitlines()
perf_dict = {}
for line in wsrep_out:
perf_keys_list = (
"wsrep_ready",
"wsrep_local_state_comment",
"wsrep-cluster_size",
"wsrep_connected",
"Bytes_received",
"Bytes_sent",
"Com_select",
"Com_update",
"Com_delete",
"Com_insert",
"Com_rollback",
"Open_files",
"Opened_files",
"wsrep_cluster_size",
"wsrep_local_state_comment",
"wsrep_cluster_status",
"Connection_errors_internal",
"Connection_errors_max_connections",
"Aborted_connects",
"Aborted_clients",
"Innodb_buffer_pool_reads",
"Innodb_buffer_pool_read_requests",
"Innodb_buffer_pool_pages_total",
"Innodb_buffer_pool_pages_free",
"Innodb_page_size",
"Innodb_row_lock_waits",
)
perf_key = line.split()[0]
if perf_key in perf_keys_list:
try:
perf_value = int(line.split()[1])
except ValueError as e:
if args.action_debug:
message += f'DEBUG: Key {perf_key} does NOT have an integer value : {line.split()[1]}\n{e}'
perf_value = line.split()[1]
perf_dict[perf_key] = perf_value
for line in max_connections_out:
if 'max_connections' in line:
perf_value = int(line.split()[1])
perf_dict['max_connections'] = perf_value
perf_dict['db_writes'] = perf_dict['Com_insert'] + perf_dict['Com_update'] + perf_dict['Com_delete']
perf_dict['innodb_buffer_pool_efficiency'] = (perf_dict['Innodb_buffer_pool_reads'] / perf_dict ['Innodb_buffer_pool_read_requests']) * 100
perf_dict['innodb_buffer_pool_utilization'] = ((perf_dict['Innodb_buffer_pool_pages_total'] - perf_dict['Innodb_buffer_pool_pages_free']) / perf_dict['Innodb_buffer_pool_pages_total']) * 100
perf_dict['innodb_buffer_pool_size'] = perf_dict['Innodb_buffer_pool_pages_total'] * perf_dict['Innodb_page_size']
message = ""
status = 0
if perf_dict['wsrep_ready'] != "ON":
message += f'Problem with wsrep_ready - status {wsrep_ready}\n' # critical
status = 2
if perf_dict['wsrep_local_state_comment'] != "Synced":
message += f'Problem with galera sync - status {wsrep_local_state_comment}\n' # critical
status = 2
if perf_dict['wsrep_connected'] != "ON":
message += f'Problem with wsrep connection - status: {wsrep_connected}\n' # critical
status = 2
if perf_dict['wsrep_cluster_size'] == 2:
if status != 2:
status = 1
message += f'Problem with galera cluster size - nodes: {wsrep_cluster_size}\n' # critical if <2; warning if ==2
elif perf_dict['wsrep_cluster_size'] < 2:
status = 2
message += f'Problem with galera cluster size - nodes: {wsrep_cluster_size}\n' # critical if <2; warning if ==2
if message == "":
message = "OK: No problems found with wsrep_ready, wsrep_local_state_comment, wsrep_cluster_size, wsrep_connected"
perf_dict['wsrep_local_state_comment'] = 2 if perf_dict['wsrep_local_state_comment'] == 'Synced' else 0
perf_dict['wsrep_cluster_status'] = 2 if perf_dict['wsrep_cluster_status'] == 'Primary' else 0
perfdata = ''
for perf_key, perf_value in perf_dict.items():
if isinstance(perf_value, int) or isinstance(perf_value, float):
perfdata += f" {perf_key}={perf_value};"
else:
if args.action_debug:
message += f'DEBUG: {perf_key} does NOT have a number value ({perf_value}) hence will not be set as perf data'
message += f" |{perfdata}"
return status, message
def check_openstack_instance(utility_container, instance_id):
message = ""
status = 0
openstack_instance_cmd = f"sudo su - root -c \"lxc-attach {utility_container} -- /usr/bin/bash -c \'source /root/openrc && openstack server show -f value -c OS-EXT-STS:power_state -c OS-EXT-STS:vm_state -c status {instance_id}\'\""
openstack_instance = []
try:
openstack_instance = subprocess.check_output(openstack_instance_cmd, shell=True, stderr=subprocess.STDOUT).decode('utf-8').splitlines()
except subprocess.CalledProcessError as e:
error_output = e.output
message += f"{error_output}\n"
message = message[2:-2]
status = 2
return status, message
output_lines_count = len(openstack_instance)
if output_lines_count == 3:
power_state, vm_state, instance_status = openstack_instance
if (power_state != "Running" and power_state != "1") or vm_state != "active" or instance_status != "ACTIVE":
message += f"PROBLEM: Instance details - power_state: {power_state}, vm_state: {vm_state}, status: {instance_status}\n"
status = 1
else:
message += f"Unexpected result, try running the following command manually:\nopenstack server show -f value -c OS-EXT-STS:power_state -c OS-EXT-STS:vm_state -c status {instance_id}\n"
status = 2
if message == "":
message = f"OK: Instance {instance_id} was queried successfully"
return status, message
def main():
sanity_check()
containers, utility_container, galera_container = get_lxc_containers()
if args.openstack_service == "instance" and args.openstack_instance != "":
instance_id = args.openstack_instance
status, message = check_openstack_instance(utility_container, instance_id)
print(message)
sys.exit(status)
elif args.openstack_service == "lxc":
status, message = check_lxc_status(containers)
print(message)
sys.exit(status)
elif args.openstack_service == "nova":
status, message = check_nova_services(utility_container)
print(message)
sys.exit(status)
elif args.openstack_service == "cinder":
status, message = check_cinder_services(utility_container)
print(message)
sys.exit(status)
elif args.openstack_service == "compute":
status, message = check_compute_services(utility_container)
print(message)
sys.exit(status)
elif args.openstack_service == "neutron":
status, message = check_neutron_services(utility_container)
print(message)
sys.exit(status)
elif args.openstack_service == "galera":
status, message = check_galera_status(galera_container)
print(message)
sys.exit(status)
if __name__ == '__main__':
main()