当前位置: 首页 > 文档资料 > Ceph 运维手册 >

第三部分:Ceph 进阶 - 9. 统计 OSD 上 PG 的数量

优质
小牛编辑
136浏览
2023-12-01

我们可以通过一个 Python 脚本,统计出每个 OSD 上分布了多少个 PG ,以此判断集群的数据分布是否均衡。

  1. #!/usr/bin/env python
  2. import sys
  3. import os
  4. import json
  5. cmd = '''
  6. ceph pg dump | awk ' /^pg_stat/ { col=1; while($col!="up") {col++}; col++ } /^[0-9a-f]+\.[0-9a-f]+/ {print $1,$col}'
  7. '''
  8. body = os.popen(cmd).read()
  9. SUM = {}
  10. for line in body.split('\n'):
  11. if not line.strip():
  12. continue
  13. SUM[line.split()[0]] = json.loads(line.split()[1])
  14. pool = set()
  15. for key in SUM:
  16. pool.add(key.split('.')[0])
  17. mapping = {}
  18. for number in pool:
  19. for k,v in SUM.items():
  20. if k.split('.')[0] == number:
  21. if number in mapping:
  22. mapping[number] += v
  23. else:
  24. mapping[number] = v
  25. MSG = """%(pool)-6s: %(pools)s | SUM
  26. %(line)s
  27. %(dy)s
  28. %(line)s
  29. %(sun)-6s: %(end)s |"""
  30. pools = " ".join(['%(a)-6s' % {"a": x} for x in sorted(list(mapping))])
  31. line = len(pools) + 20
  32. MA = {}
  33. OSD = []
  34. for p in mapping:
  35. osd = sorted(list(set(mapping[p])))
  36. OSD += osd
  37. count = sum([mapping[p].count(x) for x in osd])
  38. osds = {}
  39. for x in osd:
  40. osds[x] = mapping[p].count(x)
  41. MA[p] = {"osd": osds, "count": count}
  42. MA = sorted(MA.items(), key=lambda x:x[0])
  43. OSD = sorted(list(set(OSD)))
  44. DY = ""
  45. for osd in OSD:
  46. count = sum([x[1]["osd"].get(osd,0) for x in MA])
  47. w = ["%(x)-6s" % {"x": x[1]["osd"].get(osd,0)} for x in MA]
  48. #print w
  49. w.append("| %(x)-6s" % {"x": count})
  50. DY += 'osd.%(osd)-3s %(osds)s\n' % {"osd": osd, "osds": " ".join(w)}
  51. SUM = " ".join(["%(x)-6s" % {"x": x[1]["count"]} for x in MA])
  52. msg = {"pool": "pool", "pools": pools, "line": "-" * line, "dy": DY, "end": SUM, "sun": "SUM"}
  53. print MSG % msg

执行效果如下:

  1. root@OPS-ceph1:~# ./cal_pg_per_osd.py
  2. dumped all in format plain
  3. pool : 0 1 2 3 4 5 7 | SUM
  4. --------------------------------------------------------------------
  5. osd.0 2 9 11 10 5 6 1 | 44
  6. osd.1 1 7 8 10 8 9 3 | 46
  7. osd.2 2 11 7 6 6 8 4 | 44
  8. osd.3 1 11 7 9 7 4 1 | 40
  9. osd.4 2 12 12 12 11 13 0 | 62
  10. osd.5 2 10 10 5 9 11 0 | 47
  11. osd.6 3 56 47 49 38 43 16 | 252
  12. osd.7 6 36 48 45 55 42 10 | 242
  13. osd.8 4 41 42 37 35 49 15 | 223
  14. osd.9 6 42 52 41 55 36 12 | 244
  15. osd.10 10 36 47 51 39 43 15 | 241
  16. osd.11 6 56 47 44 41 46 12 | 252
  17. osd.12 6 40 45 45 51 46 11 | 244
  18. osd.13 6 42 40 56 46 44 9 | 243
  19. osd.14 5 44 41 49 48 52 7 | 246
  20. osd.15 4 50 42 38 49 38 12 | 233
  21. osd.16 3 9 5 11 8 8 2 | 46
  22. osd.17 2 10 13 4 7 10 2 | 48
  23. osd.18 0 12 10 10 10 9 6 | 57
  24. osd.19 0 13 8 4 9 15 2 | 51
  25. osd.20 1 11 7 9 9 15 2 | 54
  26. osd.21 0 7 12 10 6 12 1 | 48
  27. osd.22 4 39 45 30 43 46 12 | 219
  28. osd.23 6 44 38 44 38 41 6 | 217
  29. osd.24 7 37 50 49 49 36 8 | 236
  30. osd.25 10 28 38 44 40 32 11 | 203
  31. osd.26 1 44 33 47 47 37 13 | 222
  32. osd.27 3 47 38 42 44 55 14 | 243
  33. osd.28 4 47 47 34 33 48 11 | 224
  34. osd.29 3 40 32 34 40 51 10 | 210
  35. osd.30 8 39 46 47 55 35 10 | 240
  36. osd.31 5 48 48 45 41 36 11 | 234
  37. osd.32 5 46 48 53 42 48 7 | 249
  38. --------------------------------------------------------------------
  39. SUM : 128 1024 1024 1024 1024 1024 256 |