-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCeph-GetPGReplicationStatus.py
411 lines (335 loc) · 12.5 KB
/
Ceph-GetPGReplicationStatus.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
#python3 Ceph-EmptyOSD.py -EmptyOSDs=1 -MaxThreads=8 -DontTargetSourceHosts
import sys
import json;
import subprocess;
import time;
import argparse;
from os import system, name
from datetime import datetime
# import sleep to show output for some time period
from time import sleep
OSDToEmpty = -1;
IntGB = 1073741824;
arguments = sys.argv[1:]
Usage="""
Usage:
-EmptyOSDs=#
Can accept a one or more OSD IDs (as an integer), comma sepperated.
Can be used in combination with: -EmptyHost
-EmptyHosts=#
Can accept a one or more hosts, comma sepperated.
Can be used in combination with: -EmptyOSD
-DontTargetSourceHosts
Host that cerrently owns an OSD that is being emptied will not be used as a target for any data.
-DoNotUseOSDs=#
Can accept a one or more OSD IDs (as an integer), comma sepperated.
Mannually adds OSDs to the list calculated by DontTargetSourceHosts. Because of that, it does NOT combine with DontTargetSourceHosts.
Note: This feature is usefull if you want don't want data sent to an OSD, but also don't want to empty it for some reason...
-MaxThreads=#
Default is 4, up to 8 seems to be safe. Your millage may very!
Examples:
pythong3 Ceph-EmptyOSD.py
"""
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
def RemoveCommon(array1, array2):
#Removes and items from arr1 that are in arr2 and returns it as a new array.
return [item for item in array1 if item not in array2]
def GetOSDMap():
#Get a list of OSDs with class info:
command = "ceph osd df --format json"
with open("/dev/shm/tmp.OSDs", "w") as outfile:
subprocess.run(command.split(), stdout=outfile, stderr=subprocess.DEVNULL)
f = open('/dev/shm/tmp.OSDs');
js = json.load(f);
return js['nodes'];
def GetDeviceClass(osd_map, osd_id):
device_class = '';
for osd in osd_map:
if osd['id'] == osd_id:
#print(osd);
return osd['device_class'];
def GetPGMap():
command = "ceph pg dump_json pgs"
with open("/dev/shm/tmp.CephPGs", "w") as outfile:
subprocess.run(command.split(), stdout=outfile, stderr=subprocess.DEVNULL)
f = open('/dev/shm/tmp.CephPGs');
js = json.load(f);
return js['pg_map'];
def GetRemappedCount():
command = "ceph -s --format json"
with open("/dev/shm/tmp.Status", "w") as outfile:
subprocess.run(command.split(), stdout=outfile, stderr=subprocess.DEVNULL)
f = open('/dev/shm/tmp.Status');
js = json.load(f);
return js['osdmap']['num_remapped_pgs'];
def OSDPGCount(OSDsToEmpty, osd_map):
PGsToMove = 0;
for osd in osd_map:
if(osd['id'] in OSDsToEmpty):
PGsToMove += osd['pgs'];
return PGsToMove;
def GetOSDTree():
#Get a list of OSDs and what host they are on:
command = "ceph osd tree --format json"
with open("/dev/shm/tmp.OSDTree", "w") as outfile:
subprocess.run(command.split(), stdout=outfile, stderr=subprocess.DEVNULL)
f = open('/dev/shm/tmp.OSDTree');
js = json.load(f);
return js['nodes'];
def GetOSDsOnSameHost(OSDToEmpty, osd_tree):
#print(OSDToEmpty);
for host in osd_tree:
if host['type_id'] == 1:
children = host['children'];
#print(host);
if OSDToEmpty in children:
return children;
def GetOSDsOnHost(Host, osd_tree):
#print(OSDToEmpty);
for host in osd_tree:
if host['type_id'] == 1:
if host['name'].lower() == Host.lower():
return host['children'];
def FindBusyTargets(pg_map):
#Who is already recieving, so we don't send too much!
#Note to self:
#Active is our source, up is our target.
#active+remapped+backfilling
busytargets = [];
for pg in pg_map['pg_stats']:
pgid = pg['pgid'];
up = pg['up'];
acting = pg['acting'];
width = len(up);
width2 = len(acting);
if(width != width2):
print('EGAD! We have a miss-matched count of "up" VS "acting" PGs on [', pgid, ']! This usualy means something is down, and I cannot deal with that, sorry! Bye... :<');
sys.exit(1)
if pg['state'].find('remapped') != -1:
#print("Detected remapping on:", pgid);
for x in range(0, width):
if(up[x] != acting[x]):
#print("OSD", up[x], "is already a target for repliation.");
busytargets.append(up[x]);
return busytargets;
def FindPGToMove(pg_map, RemmappingPGs, OSDToEmpty):
#Who is already recieving, so we don't send too much!
#We are needing to stay here (all in one function) to optimize the parsing of pg_map. No need to burn the poor CPU up....
for pg in pg_map['pg_stats']:
up = pg['up'];
pgid = pg['pgid']
if pgid not in RemmappingPGs:
#print("Detected remapping on:", pg['pgid']);
if OSDToEmpty in up:
RemmappingPGs.append(pgid);
return pg;
return -1;
def FindBestTargets(osd_map, OSDToEmpty, PGToMigrate):
#Who is already recieving, so we don't send too much!
#We are needing to stay here (all in one function) to optimize the parsing of pg_map. No need to burn the poor CPU up....
targets = [];
device_class = GetDeviceClass(osd_map, OSDToEmpty);
for osd in osd_map:
osdid = osd['id'];
if osdid not in PGToMigrate['up']:
if(device_class == osd['device_class']):
targets.append([osdid, osd['utilization']]);
targets.sort(key=lambda x: x[1])
return targets;
#Parse inputs.
def parse_osd_ids(osd_ids):
try:
return [int(id) for id in osd_ids.split(",")]
except ValueError:
raise argparse.ArgumentTypeError("Invalid OSD IDs. Must be a comma-separated list of integers.")
def parse_hosts(hosts):
return hosts.split(",")
def parse_arguments():
parser = argparse.ArgumentParser(description="This script is designed to empty one or more OSDs in a controlled way, preventing slow IOPs.")
parser.add_argument("-EmptyOSDs", type=parse_osd_ids, default=[], metavar="#", help="One or more OSD IDs (as an integer), comma separated.")
parser.add_argument("-EmptyHosts", type=parse_hosts, default=[], metavar="#", help="One or more hosts, comma separated.")
parser.add_argument("-DontTargetSourceHosts", action="store_true", help="No host that currently owns a OSD that is being emptied will not be used as a target.")
parser.add_argument("-DoNotUseOSDs", type=parse_osd_ids, default=[], metavar="#", help="One or more OSD IDs (as an integer), comma separated.")
parser.add_argument("-MaxThreads", type=int, default=4, metavar="#", help="Default is 4, up to 8 seems to be safe.")
parser.add_argument("-ShowStatus", action="store_true", help="!NOT IMPLEMENTED! Shows details on what PGs are being remapped, and how far along they are.")
return parser.parse_args()
args = parse_arguments()
if len(arguments) == 0:
print(Usage);
sys.exit(1)
#OSDToEmpty = 1
# Threads total? Threads per OSD? Think on it...
#Threads = arguments[1];
#NoTargetsOnHost = True;
#DoNotUseOSDs = [];
Header = """
########################## ! NOTICE ! ##########################
# This script can be stopped and restarted at any time. #
# DO NOT run multiple copies at once. Bad things! #
################################################################
"""
clear();
print(Header);
print("Loading...");
# Build out what we want to do.
osd_map = GetOSDMap();
osd_map = GetOSDMap();
pg_map = GetPGMap();
osd_tree = GetOSDTree();
OSDsToEmpty = []
DoNotUseOSDs = []
for OSD in args.EmptyOSDs:
OSDsToEmpty.append(OSD)
DoNotUseOSDs.append(OSD)
for OSD in args.DoNotUseOSDs:
DoNotUseOSDs.append(OSD)
if args.DontTargetSourceHosts:
for OSD in args.EmptyOSDs:
HostOSDs = GetOSDsOnSameHost(OSD, osd_tree);
for HostOSD in HostOSDs:
DoNotUseOSDs.append(HostOSD)
for Host in args.EmptyHosts:
HostOSDs = GetOSDsOnHost(Host, osd_tree);
for HostOSD in HostOSDs:
OSDsToEmpty.append(HostOSD)
DoNotUseOSDs.append(HostOSD)
OSDsToEmpty = list(set(sorted(OSDsToEmpty)));
DoNotUseOSDs = list(set(sorted(DoNotUseOSDs)));
Header += """
########################## The Plan ##########################
Will empty these OSDs: """
Header += ', '.join(str(element) for element in OSDsToEmpty)
Header += "\nWill NOT use these OSDs: "
Header += ', '.join(str(element) for element in DoNotUseOSDs)
Header += "\n################################################################\n"
clear();
print(Header);
time.sleep(3)
#Exit
#sys.exit(1)
#Prime the loop...
PGsLeft = OSDPGCount(OSDsToEmpty, osd_map);
LastMessage = '';
#If we get into a situation where all possible targets are being used, this will keep the script from doing too much extra work:
OSDLimitHit = False;
#Note to self:
#Active is our source, up is our target.
#active+remapped+backfilling
RemmappingPGs = [];
for pg in pg_map['pg_stats']:
if('remapped' in pg['state']):
RemmappingPGs.append(pg['pgid']);
#strWarning = '';
while PGsLeft > 0:
#Main Loop
CurrentThreads = GetRemappedCount();
LastCycleStart = datetime.now()
clear();
print(Header);
Message = 'There are currently ' + str(CurrentThreads) + ' of a maximum of ' + str(args.MaxThreads) + ' remaps running with ' + str(PGsLeft) + ' remaining.\n';
print(Message);
#LastCycleStart
RemmappingPGs = [];
print('Replication state:');
for pg in pg_map['pg_stats']:
pgid = pg['pgid'];
state = pg['state'];
if('remapped' in state):
RemmappingPGs.append(pgid);
found = 1;
stat_sum = pg['stat_sum'];
num_bytes = stat_sum['num_bytes'];
num_objects = stat_sum['num_objects'];
num_objects_misplaced = stat_sum['num_objects_misplaced'];
up = pg['up'];
acting = pg['acting'];
width=len(up)
if(num_bytes > 0 and num_objects > 0 and num_objects_misplaced > 0):
misplaced_ratio = (float(num_objects_misplaced) / (float(num_objects)));
gbs = round(float(num_bytes / IntGB * misplaced_ratio * 10)) / 10;
source = [];
target = [];
hits = 0;
for x in range(1, width + 1):
if(up[x-1] != acting[x-1]):
hits += 1;
source.append(acting[x-1])
target.append(up[x-1])
print(' -', pg['pgid'], state, "- Left:", int(num_bytes * misplaced_ratio), "(GBs:", str(gbs * hits) + ') - Pools:', source, '-->', target, '-', round(100-misplaced_ratio*100,1),'% Complete');
# if LastMessage != Message:
# #sys.stdout.write();
# #sys.stdout.flush()
# LastMessage = Message;
#Don't try if not needed.
RemapsDone = 0;
LastRemapsDone = -1;
while CurrentThreads < args.MaxThreads and RemapsDone != LastRemapsDone:
for OSDToEmpty in OSDsToEmpty:
LastRemapsDone = RemapsDone;
#Need to re-check to make sure to stop when needed.
if CurrentThreads < args.MaxThreads:
# Load updated maps.
osd_map = GetOSDMap();
pg_map = GetPGMap();
osd_tree = GetOSDTree();
PGsLeft = OSDPGCount(OSDsToEmpty, osd_map);
#JustUsedOSDs = [];
#Loops though possible PGs to migrate:
print('\nSearching for PGs to move...');
PGToMigrate = FindPGToMove(pg_map, RemmappingPGs, OSDToEmpty);
while PGToMigrate != -1:
# Find all targets sorted by free space:
TargetsWithUtilization = FindBestTargets(osd_map, OSDToEmpty, PGToMigrate);
#Trimming this down for easy coding later, just don't want to toss the data for usilization yet. Might be usefull later..
targets = [element[0] for element in TargetsWithUtilization];
#print(targets, 1);
# Remove all busy migration targets.
targets = RemoveCommon(targets, FindBusyTargets(pg_map));
#print(targets, 2);
#targets = RemoveCommon(targets, JustUsedOSDs);
#print(targets, 3);
# Remove targets on do-not-use list.
targets = RemoveCommon(targets, DoNotUseOSDs);
#print(targets, 4);
# OPTIONAL: Remove targets on the same host.
if args.DontTargetSourceHosts:
targets = RemoveCommon(targets, GetOSDsOnSameHost(OSDToEmpty, osd_tree));
#print(targets, 5);
#Can we do it?
pgid = PGToMigrate['pgid'];
if len(targets) > 0:
print('Lets move', pgid, 'from OSD', OSDToEmpty, 'to', targets[0]);
command = 'ceph osd pg-upmap-items '
command += pgid
command += ' '
command += str(OSDToEmpty)
command += ' '
command += str(targets[0])
print('Executing:', command);
subprocess.run(command.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
RemapsDone += 1;
CurrentThreads += 1;
# print("JustUsedOSDs 1 ", JustUsedOSDs);
# JustUsedOSDs.append(targets[0])
# print("JustUsedOSDs 2 ", JustUsedOSDs);
# Wait a few seconds, let ceph do its thing.
time.sleep(30)
PGToMigrate = -1;
else:
PGToMigrate = FindPGToMove(pg_map, RemmappingPGs, OSDToEmpty);
if RemapsDone == 0:
if CurrentThreads == 0:
print('Could not start any remaps... Exiting!');
sys.exit(1)
else:
print('WARNING: Could not start any additional remaps. No targets avalible.');
if PGsLeft > 0:
print('\nLast Updated:', datetime.now());
time.sleep(30)