Class: VCenterDriver::VCenterHost

Inherits:
OpenNebula::Host show all
Defined in:
lib/vcenter_driver.rb

Overview

This class is an OpenNebula hosts that abstracts a vCenter cluster. It includes the functionality needed to monitor the cluster and report the ESX hosts and VM status of the cluster.

Constant Summary

Constants inherited from OpenNebula::Host

OpenNebula::Host::HOST_METHODS, OpenNebula::Host::HOST_STATES, OpenNebula::Host::HOST_STATUS, OpenNebula::Host::SHORT_HOST_STATES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenNebula::Host

#allocate, build_xml, #delete, #disable, #enable, #flush, #import_wild, #importable_wilds, #info, #monitoring, #monitoring_xml, #offline, #rename, #short_state_str, #state, #state_str, #template_str, #update, #wilds

Methods inherited from OpenNebula::PoolElement

#id, #name, new_with_id, #to_str

Methods inherited from OpenNebula::XMLElement

#[], #add_element, #attr, build_xml, #delete_element, #each, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?

Constructor Details

#initialize(client) ⇒ VCenterHost

Initialize the VCenterHost by looking for the associated objects of the VIM hierarchy client [VIClient] to interact with the associated vCenter



1059
1060
1061
1062
1063
1064
# File 'lib/vcenter_driver.rb', line 1059

def initialize(client)
    @client  = client
    @cluster = client.cluster

    @resource_pools = client.resource_pool
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



1052
1053
1054
# File 'lib/vcenter_driver.rb', line 1052

def client
  @client
end

#clusterObject (readonly)

Returns the value of attribute cluster.



1052
1053
1054
# File 'lib/vcenter_driver.rb', line 1052

def cluster
  @cluster
end

#hostObject (readonly)

Returns the value of attribute host.



1052
1053
1054
# File 'lib/vcenter_driver.rb', line 1052

def host
  @host
end

#vc_clientObject (readonly)

Returns the value of attribute vc_client.



1052
1053
1054
# File 'lib/vcenter_driver.rb', line 1052

def vc_client
  @vc_client
end

#vc_rootObject (readonly)

Returns the value of attribute vc_root.



1052
1053
1054
# File 'lib/vcenter_driver.rb', line 1052

def vc_root
  @vc_root
end

Class Method Details

.to_one(cluster_name, client) ⇒ Object

Creates an OpenNebula host representing a cluster in this VCenter

@param cluster_name[String] the name of the cluster in the vcenter
@param client [VIClient] to create the host
@return In case of success [0, host_id] or [-1, error_msg]


1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
# File 'lib/vcenter_driver.rb', line 1072

def self.to_one(cluster_name, client)
    one_host = ::OpenNebula::Host.new(::OpenNebula::Host.build_xml,
        client.one)

    rc = one_host.allocate(cluster_name, 'vcenter', 'vcenter',
            ::OpenNebula::ClusterPool::NONE_CLUSTER_ID)

    return -1, rc.message if ::OpenNebula.is_error?(rc)

    template = "VCENTER_HOST=\"#{client.host}\"\n"\
               "VCENTER_PASSWORD=\"#{client.pass}\"\n"\
               "VCENTER_USER=\"#{client.user}\"\n"

    rc = one_host.update(template, false)

    if ::OpenNebula.is_error?(rc)
        error = rc.message

        rc = one_host.delete

        if ::OpenNebula.is_error?(rc)
            error << ". Host #{cluster_name} could not be"\
                " deleted: #{rc.message}."
        end

        return -1, error
    end

    return 0, one_host.id
end

Instance Method Details

#get_available_dsObject



1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
# File 'lib/vcenter_driver.rb', line 1339

def get_available_ds
    str_info = ""

    datastores = VIClient.get_entities(client.dc.datastoreFolder,
                                       'Datastore')
    datastores.each { |ds|
        str_info += "VCENTER_DATASTORE=\"#{ds.name}\"\n"
    }
    str_info.chomp
end

#monitor_clusterObject

Generate an OpenNebula monitor string for this host. Reference: www.vmware.com/support/developer/vc-sdk/visdk25pubs/Reference Guide/vim.ComputeResource.Summary.html

- effectiveCpu: Effective CPU resources (in MHz) available to run
  VMs. This is the aggregated from all running hosts excluding hosts in
  maintenance mode or unresponsive are not counted.
- effectiveMemory: Effective memory resources (in MB) available to run
  VMs. Equivalente to effectiveCpu.
- numCpuCores: Number of physical CPU cores.
- numEffectiveHosts: Total number of effective hosts.
- numHosts:Total number of hosts.
- totalCpu: Aggregated CPU resources of all hosts, in MHz.
- totalMemory: Aggregated memory resources of all hosts, in bytes.


1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/vcenter_driver.rb', line 1118

def monitor_cluster
    #Load the host systems
    summary = @cluster.summary

    mhz_core = summary.totalCpu.to_f / summary.numCpuCores.to_f
    eff_core = summary.effectiveCpu.to_f / mhz_core

    free_cpu  = sprintf('%.2f', eff_core * 100).to_f
    total_cpu = summary.numCpuCores.to_f * 100
    used_cpu  = sprintf('%.2f', total_cpu - free_cpu).to_f

    total_mem = summary.totalMemory.to_i / 1024
    free_mem  = summary.effectiveMemory.to_i * 1024

    str_info = ""

    # System
    str_info << "HYPERVISOR=vcenter\n"
    str_info << "PUBLIC_CLOUD=YES\n"
    str_info << "TOTALHOST=" << summary.numHosts.to_s << "\n"
    str_info << "AVAILHOST=" << summary.numEffectiveHosts.to_s << "\n"

    # CPU
    str_info << "CPUSPEED=" << mhz_core.to_s   << "\n"
    str_info << "TOTALCPU=" << total_cpu.to_s << "\n"
    str_info << "USEDCPU="  << used_cpu.to_s  << "\n"
    str_info << "FREECPU="  << free_cpu.to_s << "\n"

    # Memory
    str_info << "TOTALMEMORY=" << total_mem.to_s << "\n"
    str_info << "FREEMEMORY="  << free_mem.to_s << "\n"
    str_info << "USEDMEMORY="  << (total_mem - free_mem).to_s

    str_info << monitor_resource_pools(@cluster.resourcePool, "", mhz_core)
end

#monitor_customizationsObject



1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
# File 'lib/vcenter_driver.rb', line 1323

def monitor_customizations
    customizations = client.vim.serviceContent.customizationSpecManager.info

    text = ''

    customizations.each do |c|
        t = "CUSTOMIZATION = [ "
        t << %Q<NAME = "#{c.name}", >
        t << %Q<TYPE = "#{c.type}" ]\n>

        text << t
    end

    text
end

#monitor_host_systemsObject

Generate a template with information for each ESX Host. Reference: pubs.vmware.com/vi-sdk/visdk250/ReferenceGuide/vim.HostSystem.html

- Summary: Basic information about the host, including connection state
  - hardware: Hardware configuration of the host. This might not be
    available for a disconnected host.
  - quickStats: Basic host statistics.


1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
# File 'lib/vcenter_driver.rb', line 1217

def monitor_host_systems
    host_info = ""

    @cluster.host.each{|h|
        next if h.runtime.connectionState != "connected"

        summary = h.summary
        hw      = summary.hardware
        stats   = summary.quickStats

        total_cpu = hw.numCpuCores * 100
        used_cpu  = (stats.overallCpuUsage.to_f / hw.cpuMhz.to_f) * 100
        used_cpu  = sprintf('%.2f', used_cpu).to_f # Trim precission
        free_cpu  = total_cpu - used_cpu

        total_memory = hw.memorySize/1024
        used_memory  = stats.overallMemoryUsage*1024
        free_memory  = total_memory - used_memory

        host_info << "\nHOST=["
        host_info << "STATE=on,"
        host_info << "HOSTNAME=\""  << h.name.to_s  << "\","
        host_info << "MODELNAME=\"" << hw.cpuModel.to_s  << "\","
        host_info << "CPUSPEED="    << hw.cpuMhz.to_s    << ","
        host_info << "MAX_CPU="    << total_cpu.to_s << ","
        host_info << "USED_CPU="     << used_cpu.to_s  << ","
        host_info << "FREE_CPU="     << free_cpu.to_s << ","
        host_info << "MAX_MEM=" << total_memory.to_s << ","
        host_info << "USED_MEM="  << used_memory.to_s  << ","
        host_info << "FREE_MEM="  << free_memory.to_s
        host_info << "]"
    }

    return host_info
end

#monitor_resource_pools(parent_rp, parent_prefix, mhz_core) ⇒ Object

Generate an OpenNebula monitor string for all resource pools of a cluster Reference: pubs.vmware.com/vsphere-60/index.jsp#com.vmware.wssdk.apiref.doc  /vim.ResourcePool.html



1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
# File 'lib/vcenter_driver.rb', line 1160

def monitor_resource_pools(parent_rp, parent_prefix, mhz_core)
    return "" if parent_rp.resourcePool.size == 0

    rp_info = ""

    parent_rp.resourcePool.each{|rp|
        rpcpu     = rp.config.cpuAllocation
        rpmem     = rp.config.memoryAllocation
        # CPU
        cpu_expandable   = rpcpu.expandableReservation ? "YES" : "NO"
        cpu_limit        = rpcpu.limit == "-1" ? "UNLIMITED" : rpcpu.limit
        cpu_reservation  = rpcpu.reservation
        cpu_num          = rpcpu.reservation.to_f / mhz_core
        cpu_shares_level = rpcpu.shares.level
        cpu_shares       = rpcpu.shares.shares

        # MEMORY
        mem_expandable   = rpmem.expandableReservation ? "YES" : "NO"
        mem_limit        = rpmem.limit == "-1" ? "UNLIMITED" : rpmem.limit
        mem_reservation  = rpmem.reservation.to_f
        mem_shares_level = rpmem.shares.level
        mem_shares       = rpmem.shares.shares

        rp_name          = (parent_prefix.empty? ? "" : parent_prefix + "/")
        rp_name         += rp.name

        rp_info << "\nRESOURCE_POOL = ["
        rp_info << "NAME=\"#{rp_name}\","
        rp_info << "CPU_EXPANDABLE=#{cpu_expandable},"
        rp_info << "CPU_LIMIT=#{cpu_limit},"
        rp_info << "CPU_RESERVATION=#{cpu_reservation},"
        rp_info << "CPU_RESERVATION_NUM_CORES=#{cpu_num},"
        rp_info << "CPU_SHARES=#{cpu_shares},"
        rp_info << "CPU_SHARES_LEVEL=#{cpu_shares_level},"
        rp_info << "MEM_EXPANDABLE=#{mem_expandable},"
        rp_info << "MEM_LIMIT=#{mem_limit},"
        rp_info << "MEM_RESERVATION=#{mem_reservation},"
        rp_info << "MEM_SHARES=#{mem_shares},"
        rp_info << "MEM_SHARES_LEVEL=#{mem_shares_level}"
        rp_info << "]"

        if rp.resourcePool.size != 0
           rp_info << monitor_resource_pools(rp, rp_name, mhz_core)
        end
    }

    return rp_info
end

#monitor_vmsObject



1253
1254
1255
1256
# File 'lib/vcenter_driver.rb', line 1253

def monitor_vms
    # Only monitor from top level (Resource) Resource Pool
    monitor_vms_in_rp(@resource_pools[-1])
end

#monitor_vms_in_rp(rp) ⇒ Object



1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
# File 'lib/vcenter_driver.rb', line 1259

def monitor_vms_in_rp(rp)
    str_info = ""

    if rp.resourcePool.size != 0
        rp.resourcePool.each{|child_rp|
            str_info += monitor_vms_in_rp(child_rp)
        }
    end

    host_cache = {}

    rp.vm.each { |v|
      begin
          # Check cached objects
          if !host_cache[v.runtime.host.to_s]
              host_cache[v.runtime.host.to_s] =
                     VCenterCachedHost.new v.runtime.host
          end

          host = host_cache[v.runtime.host.to_s]

          name            = v.name
          number          = -1
          vm_extra_config = v.config.extraConfig

          # Check the running flag
          running_flag = v.config.extraConfig.select{|val|
                                     val[:key]=="opennebula.vm.running"}
          if running_flag.size > 0 and running_flag[0]
              running_flag = running_flag[0][:value]
          end                                

          next if running_flag == "no"

          # Extract vmid if possible
          matches = name.match(/^one-(\d*)(-(.*))?$/)
          number  = matches[1] if matches
          extraconfig_vmid = v.config.extraConfig.select{|val|
                                     val[:key]=="opennebula.vm.id"}
          if extraconfig_vmid.size > 0 and extraconfig_vmid[0]
              number = extraconfig_vmid[0][:value]
          end
          vm = VCenterVm.new(@client, v)
          vm.monitor(host)
          next if !vm.vm.config
          str_info << "\nVM = ["
          str_info << "ID=#{number},"
          str_info << "DEPLOY_ID=\"#{vm.vm.config.uuid}\","
          str_info << "VM_NAME=\"#{name} - "\
                      "#{host.cluster_name}\","
          if number == -1
              vm_template_to_one =
                  Base64.encode64(vm.vm_to_one(host)).gsub("\n","")
              str_info << "IMPORT_TEMPLATE=\"#{vm_template_to_one}\","
          end
          str_info << "POLL=\"#{vm.info}\"]"
      rescue Exception => e
          STDERR.puts e.inspect
          STDERR.puts e.backtrace
      end
    }
  return str_info
end