/
novac-quota-report
executable file
·107 lines (93 loc) · 2.72 KB
/
novac-quota-report
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
#!/usr/bin/env ruby
# Usage: novac quota-report [projectID]
# Summary: Displays the Quota and Usage for every project or a sepcified project
# Help: Optional argument: <projectID> may be specified for just one project.
$:.unshift File.expand_path("../../share/novac/lib/rb", __FILE__)
require 'rubygems'
require 'novadb'
require 'quotas'
require 'projects'
# List of required libraries
required_libs = ['terminal-table']
# Try to load each of the libraries
# Fail if unable to and list what libs are needed
begin
required_libs.each { |l| require l }
rescue LoadError
puts "This script needs the following external libraries: "
required_libs.each { |l| puts " * #{l}" }
exit 1
end
# Ensure root or sudo is used for this command
if Process.euid != 0
throw "Must run this script as root or sudo."
end
# Was a project specified?
if ARGV[0]
project = ARGV[0]
else
project = nil
end
# create quota object
quotas = Quotas.new
# Build a list of project quotas
p = Projects.new
# Get the quota for each project
# As well as resources used
quota = {}
resources_used = {}
to_report = {}
if project
total_usage = {}
p.projects.each do |project_id, project_name|
if project_name == project
total_usage[project_id] = quotas.calculate_used(project_id)
to_report[project_id] = project_name
quota[project_id] = quotas.project_quota(project_id)
end
end
else
total_usage = quotas.all_projects_used
p.projects.each do |project_id, project_name|
to_report[project_id] = project_name
quota[project_id] = quotas.project_quota(project_id)
end
end
# Load start dates into memory
start_dates = {}
File.open("/etc/openstack-dashboard/start-dates.txt", "r") do |file|
while (line = file.gets)
if line != ""
foo = line.split(":")
start_dates[foo[0]] = foo[1]
end
end
end
# Report quota usage
x = to_report.sort_by { |k,v| v }
x.each do |project_id, project_name|
rows = []
q = quota[project_id].sort_by { |k,v| k }
q.each do |resource, limit|
used = 0
if total_usage[project_id].has_key?(resource)
used = total_usage[project_id][resource]
end
#Catch 0 or unlimited quota users
if limit.to_f > 0.0
percent = ((used.to_f / limit.to_f) * 100).to_i
else
percent = 0
end
rows << [resource,used,limit,"#{percent} %"]
end
next unless rows.any?
start_date = start_dates[project_id]
# Just in case - truncate Project Name to < 80 characters (:width)
project_name = "#{project_name[0..60]}\n#{start_date}"
table = Terminal::Table.new :title => project_name, :headings => ['Resource', 'Used', 'Limit',''], :rows => rows, :style => { :width => 80 }
table.align_column(-1, :right)
table.align_column(-2, :right)
table.align_column(-3, :right)
puts table
end