-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstorage-report.py
executable file
·142 lines (111 loc) · 2.82 KB
/
storage-report.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
#!/usr/bin/env python
"""
storage-report.py
Report storage utilization.
Supported on NexentaStor 4.0.3 or greater.
Copyright (c) 2016 Nexenta Systems
William Kettler <william.kettler@nexenta.com>
"""
import sys
import time
import commands
import socket
def pprint_table(table):
"""
Pretty print table.
Inputs:
table (list): table
Outputs:
None
"""
padding = []
# Get the max width of each column
for i in range(len(table[0])):
padding.append(max([len(row[i]) for row in table]))
# Print header
header = table.pop(0)
width = []
for index, item in enumerate(header):
width.append(len(item))
col = item.rjust(padding[index] + 2)
print col,
print ""
# Print header delimiter
for index, item in enumerate(width):
under = "-" * item
col = under.rjust(padding[index] + 2)
print col,
print ""
# Print table contents
for row in table:
for index, item in enumerate(row):
col = item.rjust(padding[index] + 2)
print col,
print ""
def get_hostname():
"""
Get hostname.
Inputs:
None
Ouputs:
hostname (str): hostname
"""
hostname = socket.gethostname()
return hostname
def get_zfs_space(unit):
"""
Parse the output of `zfs list -o space` and convert to the defined unit.
Inputs:
unit (str): Unit, e.g. B,K,M,G,T
Outputs:
table (list): Pased output of `zfs list -o space`
"""
units = {
"B": 1,
"K": 1024,
"M": 1024 ** 2,
"G": 1024 ** 3,
"T": 1024 ** 4
}
table = []
retcode, output = commands.getstatusoutput("zfs list -p -o space")
if retcode:
print "[ERROR] Command execution failed"
print output
sys.exit(1)
# Iterate over the command output
header = True
for line in output.splitlines():
row = line.split()
if header:
header = False
else:
for i in range(1, len(row)):
row[i] = "%.1f%s" % (float(int(row[i]) / units[unit]), unit)
table.append(row)
return table
def main():
"""
Main function.
"""
unit = "K"
date = time.strftime('%Y%m%d-%H%M', time.localtime(int(time.time())))
hostname = get_hostname()
ofile = "%s-%s-storage-report.csv" % (date, hostname)
# Open output file
try:
fhandle = open(ofile, 'w')
except IOError, err:
sys.stderr.write("[ERROR] opening %s" % ofile)
sys.stderr.write(err)
sys.exit(1)
space = get_zfs_space(unit)
for row in space:
fhandle.write("%s\n" % ",".join(row))
# Close file
fhandle.close()
pprint_table(space)
print ""
print "Output saved to %s." % ofile
if __name__ == "__main__":
main()