-
Notifications
You must be signed in to change notification settings - Fork 1
/
ceph_space.py
executable file
·128 lines (100 loc) · 4.43 KB
/
ceph_space.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
:copyright: (c) 2015 by Alvaro Soto
:license: GPL v3, see LICENSE for more details.
:contact info: http://headup.ws / alsotoes@gmail.com
"""
import argparse
import textwrap
import rados
import math
import sys
import csv
import re
import os
# http://stackoverflow.com/questions/1094841/
def sizeof_fmt(num):
for x in ['bytes', 'KB', 'MB', 'GB']:
if num < 1024.0 and num > -1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, 'TB')
def args_parse():
parser = argparse.ArgumentParser(
prog=sys.argv[0],
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''\
===============
Connect to local ceph cluster to get available and total space.
* Works only locally reading ceph.conf.
* Uses a ratio to set working storage space on calculations.
===============
'''))
parser.add_argument("--config", type=str, default="/etc/ceph/ceph.conf", help='Ceph config file (/etc/ceph/ceph.conf by default).')
parser.add_argument("--clustername", type=str, default="ceph", help='Cluster name (ceph by default).')
parser.add_argument("--ratio", type=float, default=0.3, help='Reserved storage ratio, by default 0.3 as a 30%%.')
parser.add_argument("--output", type=str, required=False, choices=["csv", "txt"], help='Output format to print in stdout.')
parser.add_argument("--file", type=str, required=False, help='Output CSV file, this option override --output.')
parser.add_argument("name", type=str, help='Cluster fancy name.')
return parser.parse_args()
def print_txt(ceph):
print "==============="
print "Cluster codename: " + ceph['name']
print "Cluster ID: " + ceph['fsid']
print "Cluster mon to connect: " + ceph['mon']
print "Cluster Statistics"
print "\tTotal: " + sizeof_fmt(ceph['total'] * 1024)
print "\tReserved: " + sizeof_fmt(ceph['reserved'] * 1024)
print "\tUsable (before warning): " + sizeof_fmt(ceph['usable'] * 1024)
print "\tUsed: " + sizeof_fmt(ceph['used'] * 1024)
print "\n"
print "\tTotal Available: " + sizeof_fmt(ceph['available'] * 1024)
print "\tPercentage Available: " + str(ceph['p_available']) + "%"
print "==============="
def print_csv(ceph, filename, let):
f = open(filename+".csv", 'wt')
try:
writer = csv.writer(f)
writer.writerow(["Cluster Name", "Cluster ID", "Cluster MON", "Total", "Reserved", "Usable", "Used", "Available", "Percentage Available"])
writer.writerow([ceph['name'], ceph['fsid'], ceph['mon'], sizeof_fmt(ceph['total'] * 1024),
sizeof_fmt(ceph['reserved'] * 1024), sizeof_fmt(ceph['usable'] * 1024),
sizeof_fmt(ceph['used'] * 1024), sizeof_fmt(ceph['available'] * 1024),
str(ceph['p_available']) + "%"])
finally:
f.close()
print open(filename+".csv", 'rt').read()
if not let:
os.remove(filename+".csv")
def get_cluster_info(cluster, ceph):
ceph['default_size'] = int(cluster.conf_get('osd pool default size'))
ceph['mon'] = cluster.conf_get('mon initial members')
ceph['fsid'] = cluster.get_fsid()
return cluster.get_cluster_stats()
def get_and_calculate(cluster_stats, ceph, args):
ceph['name'] = args.name
ceph['reserved_ratio'] = float(args.ratio)
ceph['total'] = long(cluster_stats['kb']) / ceph['default_size']
ceph['reserved'] = long(cluster_stats['kb'] * (ceph['reserved_ratio'])) / ceph['default_size']
ceph['usable'] = long(cluster_stats['kb'] * (1 - ceph['reserved_ratio'])) / ceph['default_size']
ceph['used'] = long(cluster_stats['kb_used']) / ceph['default_size']
ceph['available'] = ceph['usable'] - ceph['used']
ceph['p_available'] = (ceph['available'] * 100) / ceph['usable']
def main():
ceph = dict()
args = args_parse()
cluster = rados.Rados(conffile=args.config, clustername=args.clustername)
cluster.connect()
cluster_stats = get_cluster_info(cluster, ceph)
get_and_calculate(cluster_stats, ceph, args)
if not args.output:
args.output = "txt"
if not args.file:
args.file = "output"
let = 0
else:
let = 1
print_txt(ceph) if "txt" == args.output else print_csv(ceph, re.sub('[^0-9a-zA-Z]+', '_', args.file), let)
# Start program
if __name__ == "__main__":
main()