Class: Kakin::CLI
- Inherits:
-
Thor
- Object
- Thor
- Kakin::CLI
- Defined in:
- lib/kakin/cli.rb
Instance Method Summary collapse
Instance Method Details
#calc ⇒ Object
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 |
# File 'lib/kakin/cli.rb', line 20 def calc Kakin::Configuration.setup yaml = YAML.load_file([:f]) start_time = Time.parse([:s]).strftime("%FT%T") end_time = Time.parse([:e]).strftime("%FT%T") STDERR.puts "Start: #{start_time}" STDERR.puts "End: #{end_time}" url = URI.parse("#{Kakin::Configuration.management_url}/#{Yao::Tenant.get_by_name(Kakin::Configuration.tenant).id}/os-simple-tenant-usage?start=#{start_time}&end=#{end_time}") req = Net::HTTP::Get.new(url) req["Accept"] = "application/json" req["X-Auth-Token"] = Yao::Auth.try_new.token res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) } if res.code != "200" raise "usage data fatch is failed" else result = Hash.new tenant_usages = JSON.load(res.body)["tenant_usages"] tenants = Yao::Tenant.list unless [:t].empty? tenant = tenants.find { |tenant| tenant.name == [:t] } raise "Not Found tenant #{[:t]}" unless tenant tenant_usages = tenant_usages.select { |tenant_usage| tenant_usage["tenant_id"] == tenant.id } end tenant_usages.each do |usage| tenant = tenants.find { |tenant| tenant.id == usage["tenant_id"] } total_vcpus_usage = usage["total_vcpus_usage"] total_memory_mb_usage = usage["total_memory_mb_usage"] total_local_gb_usage = usage["total_local_gb_usage"] bill_vcpu = total_vcpus_usage * yaml["vcpu_per_hour"] bill_memory = total_memory_mb_usage * yaml["memory_mb_per_hour"] bill_disk = total_local_gb_usage * yaml["disk_gb_per_hour"] result[tenant.name] = { 'bill_total' => bill_vcpu + bill_memory + bill_disk, 'bill_vcpu' => bill_vcpu, 'bill_memory' => bill_memory, 'bill_disk' => bill_disk, 'total_hours' => usage["total_hours"], 'total_vcpus_usage' => total_vcpus_usage, 'total_memory_mb_usage' => total_memory_mb_usage, 'total_local_gb_usage' => total_local_gb_usage, } end puts YAML.dump(result) end end |
#ip ⇒ Object
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 |
# File 'lib/kakin/cli.rb', line 117 def ip Kakin::Configuration.setup yaml = YAML.load_file([:f]) ip_regexp = Regexp.new(yaml["ip_regexp"]) result = Hash.new tenants = unless [:t].empty? Yao::Tenant.list(name: [:t]) else Yao::Tenant.list end tenants = [tenants] unless tenants.is_a?(Array) tenants.each do |tenant| count = tenant.ports.select {|p| p.fixed_ips[0]["ip_address"] =~ ip_regexp}.count count += Yao::NetworkingFloatingIP.list(tenant_id: tenant.id).select {|p| p.floating_ip_address =~ ip_regexp}.count result[tenant.name] = { 'count' => count, 'total_usage' => count * yaml["cost_per_ip"], } end puts YAML.dump(result) end |
#network ⇒ Object
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 |
# File 'lib/kakin/cli.rb', line 83 def network Kakin::Configuration.setup yaml = YAML.load_file([:f]) start_time = Time.parse([:s]) end_time = Time.parse([:e]) STDERR.puts "Start: #{start_time}" STDERR.puts "End: #{end_time}" result = Hash.new tenants = unless [:t].empty? Yao::Tenant.list(name: [:t]) else Yao::Tenant.list end tenants = [tenants] unless tenants.is_a?(Array) tenants.each do |tenant| incoming = tenant.network_usage(Regexp.new(yaml["ip_regexp"]), :incoming, start_time.iso8601, end_time.iso8601) outgoing = tenant.network_usage(Regexp.new(yaml["ip_regexp"]), :outgoing, start_time.iso8601, end_time.iso8601) result[tenant.name] = { 'incoming_usage' => incoming, 'outgoing_usage' => outgoing, 'total_usage' => incoming + outgoing } end puts YAML.dump(result) end |
#volume ⇒ Object
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 |
# File 'lib/kakin/cli.rb', line 146 def volume Kakin::Configuration.setup yaml = YAML.load_file([:f]) result = Hash.new tenants = unless [:t].empty? Yao::Tenant.list(name: [:t]) else Yao::Tenant.list end tenants = [tenants] unless tenants.is_a?(Array) volume_types = Yao::VolumeType.list volumes = Yao::Volume.list_detail(all_tenants: true) tenants.each do |tenant| result[tenant.name] ||= {} volume_types.each do |volume_type| total = volumes.select { |volume| volume.tenant_id == tenant.id && volume.volume_type == volume_type.name }.map(&:size).sum result[tenant.name][volume_type.name] = { 'total': total, 'total_usage': total * yaml['volume_cost_per_gb'][volume_type.name] } end end puts YAML.dump(result) end |