Class: VCenterDriver::ClusterComputeResource

Inherits:
Object
  • Object
show all
Includes:
Memoize
Defined in:
lib/host.rb

Overview

Class ClusterComputeResource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memoize

#[], #[]=, #clear, #clear_all

Constructor Details

#initialize(item, vi_client = nil) ⇒ ClusterComputeResource

Returns a new instance of ClusterComputeResource.



79
80
81
82
83
# File 'lib/host.rb', line 79

def initialize(item, vi_client = nil)
    @item = item
    @vi_client = vi_client
    @rp_list # rubocop:disable Lint/Void
end

Instance Attribute Details

#itemObject

Returns the value of attribute item.



74
75
76
# File 'lib/host.rb', line 74

def item
  @item
end

#rp_listObject

Returns the value of attribute rp_list.



75
76
77
# File 'lib/host.rb', line 75

def rp_list
  @rp_list
end

Class Method Details

.new_from_ref(ref, vi_client) ⇒ Object



981
982
983
984
985
986
987
988
989
990
# File 'lib/host.rb', line 981

def self.new_from_ref(ref, vi_client)
    new(
        RbVmomi::VIM::ClusterComputeResource
        .new(
            vi_client.vim,
            ref
        ),
        vi_client
    )
end

.to_one(cluster, con_ops, rp, one_cluster_id) ⇒ Object



907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
# File 'lib/host.rb', line 907

def self.to_one(cluster, con_ops, rp, one_cluster_id)
    one_host = VCenterDriver::VIHelper.new_one_item(OpenNebula::Host)

    if OpenNebula.is_error?(one_host)
        raise "Could not create host: #{one_host.message}"
    end

    one_cluster_id ||= -1

    rc = one_host
         .allocate(
             cluster[:cluster_name],
             'vcenter',
             'vcenter',
             one_cluster_id.to_i
         )

    if OpenNebula.is_error?(rc)
        raise "Could not allocate host: #{rc.message}"
    end

    template = "VCENTER_HOST=\"#{con_ops[:host]}\"\n"\
               "VCENTER_PASSWORD=\"#{con_ops[:password]}\"\n"\
               "VCENTER_USER=\"#{con_ops[:user]}\"\n"\
               "VCENTER_CCR_REF=\"#{cluster[:cluster_ref]}\"\n"\
               "VCENTER_INSTANCE_ID=\"#{cluster[:vcenter_uuid]}\"\n"\
               "VCENTER_VERSION=\"#{cluster[:vcenter_version]}\"\n"\

    template << "VCENTER_RESOURCE_POOL=\"#{rp}\"" if rp

    template << "VCENTER_PORT=\"#{con_ops[:port]}\"" if con_ops[:port]

    rc = one_host.update(template, false)

    if OpenNebula.is_error?(rc)
        update_error = rc.message
        rc = one_host.delete

        unless OpenNebula.is_error?(rc)
            raise "Could not update host: #{rc.message}"; end

        raise "Could not update host: #{update_error} "\
                  "and could not delete host: #{rc.message}"
    end

    rc = one_host.offline

    if OpenNebula.is_error?(rc)
        update_error = rc.message
        rc = one_host.delete

        unless OpenNebula.is_error?(rc)
            raise "Could not offline host: #{rc.message}"; end

        raise "Could not offline host: #{update_error} "\
                  "and could not delete host: #{rc.message}"
    end

    rc = one_host.enable

    if OpenNebula.is_error?(rc)
        update_error = rc.message
        rc = one_host.delete

        unless OpenNebula.is_error?(rc)
            raise "Could not enable host: #{rc.message}"; end

        raise "Could not enable host: #{update_error} "\
                  "and could not delete host: #{rc.message}"
    end

    one_host
end

Instance Method Details

#datacenterObject

rubocop:disable Naming/AccessorMethodName



894
895
896
897
898
899
900
901
902
903
904
905
# File 'lib/host.rb', line 894

def datacenter # rubocop:disable Naming/AccessorMethodName
    item = @item

    until item.instance_of? RbVmomi::VIM::Datacenter
        item = item.parent
        if item.nil?
            raise 'Could not find the parent Datacenter'
        end
    end

    Datacenter.new(item)
end

#error_monitoring(e, id, vm_ref, _vc_uuid, info = {}) ⇒ Object



861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/host.rb', line 861

def error_monitoring(e, id, vm_ref, _vc_uuid, info = {})
    error_info = ''
    vm_name = info['name'] || nil
    tmp_str = e.inspect
    tmp_str << e.backtrace.join("\n")

    error_info << "VM = [ ID=\"#{id}\", "
    error_info << "VM_NAME=\"#{vm_name}\", "
    error_info << "DEPLOY_ID=\"#{vm_ref}\", "
    error_info <<
        "ERROR=\"#{Base64.encode64(tmp_str).gsub("\n", '')}\"]\n"
end

#fetch_resource_pools(rp, rp_array = []) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/host.rb', line 85

def fetch_resource_pools(rp, rp_array = [])
    rp_array << rp

    rp.resourcePool.each do |child_rp|
        fetch_resource_pools(child_rp, rp_array)
    end

    rp_array
end

#filter_hostsObject



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/host.rb', line 512

def filter_hosts
    view =
        @vi_client
        .vim
        .serviceContent
        .viewManager
        .CreateContainerView(
            {
                # View for Hosts inside this cluster
                :container => @item,
                :type => ['HostSystem'],
                :recursive => true
            }
        )

    pc = @vi_client.vim.serviceContent.propertyCollector

    monitored_properties = [
        'name',
        'runtime.connectionState',
        'summary.hardware.numCpuCores',
        'summary.hardware.memorySize',
        'summary.hardware.cpuModel',
        'summary.hardware.cpuMhz',
        'summary.quickStats.overallCpuUsage',
        'summary.quickStats.overallMemoryUsage'
    ]

    filter_spec = RbVmomi::VIM.PropertyFilterSpec(
        :objectSet => [
            { :obj => view,
            :skip => true,
            :selectSet => [
                RbVmomi::VIM.TraversalSpec(
                    :name => 'traverseEntities',
                    :type => 'ContainerView',
                    :path => 'view',
                    :skip => false
                )
            ] }
        ],
        :propSet => [
            { :type => 'HostSystem', :pathSet => monitored_properties }
        ]
    )

    result = pc.RetrieveProperties(:specSet => [filter_spec])
    view.DestroyView # Destroy the view
    result
end

#get_resource_pool_list(rp = @item .resourcePool, parent_prefix = '', rp_array = []) ⇒ Object



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
# File 'lib/host.rb', line 103

def get_resource_pool_list(
    rp = @item
        .resourcePool,
    parent_prefix = '',
    rp_array = []
)
    current_rp = ''

    if !parent_prefix.empty?
        current_rp << parent_prefix
        current_rp << '/'
    end

    resource_pool, name = rp.collect('resourcePool', 'name')
    current_rp << name if name != 'Resources'

    resource_pool.each do |child_rp|
        get_resource_pool_list(child_rp, current_rp, rp_array)
    end

    rp_info = {}
    rp_info[:name] = current_rp
    rp_info[:ref]  = rp._ref
    rp_array << rp_info unless current_rp.empty?

    rp_array
end

#hostname_to_moref(hostname) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/host.rb', line 497

def hostname_to_moref(hostname)
    result = filter_hosts

    moref = ''
    result.each do |r|
        if r.obj.name == hostname
            moref = r.obj._ref
            break
        end
    end
    raise "Host #{hostname} was not found" if moref.empty?

    moref
end

#monitorObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/host.rb', line 300

def monitor
    total_cpu,
    num_cpu_cores,
    effective_cpu,
    total_memory,
    effective_mem,
    num_hosts,
    num_eff_hosts,
    overall_status,
    drs_enabled,
    ha_enabled= @item.collect('summary.totalCpu',
                              'summary.numCpuCores',
                              'summary.effectiveCpu',
                              'summary.totalMemory',
                              'summary.effectiveMemory',
                              'summary.numHosts',
                              'summary.numEffectiveHosts',
                              'summary.overallStatus',
                              'configuration.drsConfig.enabled',
                              'configuration.dasConfig.enabled')

    mhz_core = total_cpu.to_f / num_cpu_cores.to_f
    eff_core = effective_cpu.to_f / mhz_core

    free_cpu  = format('%.2f', eff_core * 100).to_f # rubocop:disable Style/FormatStringToken
    total_cpu = num_cpu_cores.to_f * 100
    used_cpu  = format('%.2f', total_cpu - free_cpu).to_f # rubocop:disable Style/FormatStringToken

    total_mem = total_memory.to_i / 1024
    free_mem  = effective_mem.to_i * 1024

    str_info = ''

    # Get cluster name for informative purposes
    # (replace space with _ if any)
    str_info << 'VCENTER_NAME=' << self['name'].tr(' ', '_') << "\n"

    # System
    str_info << "HYPERVISOR=vcenter\n"
    str_info << 'TOTALHOST=' << num_hosts.to_s << "\n"
    str_info << 'AVAILHOST=' << num_eff_hosts.to_s << "\n"
    str_info << 'STATUS=' << overall_status << "\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 << "\n"

    # DRS enabled
    str_info << 'VCENTER_DRS=' << drs_enabled.to_s << "\n"

    # HA enabled
    str_info << 'VCENTER_HA=' << ha_enabled.to_s << "\n"

    # NSX info
    str_info << nsx_get
    str_info << tz_get

    str_info << monitor_resource_pools(mhz_core)
end

#monitor_customizationsObject



874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/host.rb', line 874

def monitor_customizations
    customizations =
        self['_connection']
        .serviceContent
        .customizationSpecManager
        .info

    text = ''

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

        text << t
    end

    text
end

#monitor_host_systemsObject



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/host.rb', line 563

def monitor_host_systems
    host_info = ''
    result = filter_hosts
    hosts = {}
    result.each do |r|
        hashed_properties = r.to_hash
        if r.obj.is_a?(RbVmomi::VIM::HostSystem)
            hosts[r.obj._ref] = hashed_properties
        end
    end

    hosts.each do |_ref, info|
        next if info['runtime.connectionState'] != 'connected'

        total_cpu = info['summary.hardware.numCpuCores'] * 100
        used_cpu  =
            (
                info['summary.quickStats.overallCpuUsage']
                .to_f / info['summary.hardware.cpuMhz']
                .to_f
            ) * 100
        # Trim precission
        used_cpu  = format('%.2f', used_cpu).to_f # rubocop:disable Style/FormatStringToken
        free_cpu  = total_cpu - used_cpu

        total_memory =
            info['summary.hardware.memorySize']/1024
        used_memory =
            info['summary.quickStats.overallMemoryUsage']*1024
        free_memory = total_memory - used_memory

        host_info <<  "\nHOST=["
        host_info <<  'STATE=on,'
        host_info << 'HOSTNAME="' <<
            info['name'].to_s << '",'
        host_info <<
            'MODELNAME="' <<
            info['summary.hardware.cpuModel'].to_s << '",'
        host_info << 'CPUSPEED=' <<
            info['summary.hardware.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 << ']'
    end

    host_info
end

#monitor_resource_pools(mhz_core) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/host.rb', line 367

def monitor_resource_pools(mhz_core)
    @rp_list = get_resource_pool_list

    view =
        @vi_client
        .vim
        .serviceContent
        .viewManager
        .CreateContainerView(
            {
                :container => @item, # View for RPs inside this cluster
            :type => ['ResourcePool'],
            :recursive => true
            }
        )

    pc = @vi_client.vim.serviceContent.propertyCollector

    monitored_properties = [
        'config.cpuAllocation.expandableReservation',
        'config.cpuAllocation.limit',
        'config.cpuAllocation.reservation',
        'config.cpuAllocation.shares.level',
        'config.cpuAllocation.shares.shares',
        'config.memoryAllocation.expandableReservation',
        'config.memoryAllocation.limit',
        'config.memoryAllocation.reservation',
        'config.memoryAllocation.shares.level',
        'config.memoryAllocation.shares.shares'
    ]

    filter_spec = RbVmomi::VIM.PropertyFilterSpec(
        :objectSet => [
            { :obj => view,
            :skip => true,
            :selectSet => [
                RbVmomi::VIM.TraversalSpec(
                    :name => 'traverseEntities',
                    :type => 'ContainerView',
                    :path => 'view',
                    :skip => false
                )
            ] }
        ],
        :propSet => [
            {
                :type => 'ResourcePool',
                 :pathSet => monitored_properties
            }
        ]
    )

    result = pc.RetrieveProperties(:specSet => [filter_spec])

    rps = {}
    result.each do |r|
        hashed_properties = r.to_hash
        if r.obj.is_a?(RbVmomi::VIM::ResourcePool)
            rps[r.obj._ref] = hashed_properties
        end
    end

    return '' if rps.empty?

    rp_info = ''

    rps.each  do |ref, info|
        # CPU
        if info['config.cpuAllocation.expandableReservation']
            cpu_expandable = 'YES'
        else
            cpu_expandable = 'NO'
        end
        if info['config.cpuAllocation.limit'] == '-1'
            cpu_limit = 'UNLIMITED'
        else
            cpu_limit = info['config.cpuAllocation.limit']
        end
        cpu_reservation  = info['config.cpuAllocation.reservation']
        cpu_num          = cpu_reservation.to_f / mhz_core
        cpu_shares_level = info['config.cpuAllocation.shares.level']
        cpu_shares       = info['config.cpuAllocation.shares.shares']

        # MEMORY
        if info['config.memoryAllocation.expandableReservation']
            mem_expandable = 'YES'
        else
            mem_expandable = 'NO'
        end
        if info['config.memoryAllocation.limit'] == '-1'
            mem_limit = 'UNLIMITED'
        else
            mem_limit = info['config.memoryAllocation.limit']
        end
        mem_reservation =
            info['config.memoryAllocation.reservation'].to_f
        mem_shares_level =
            info['config.memoryAllocation.shares.level']
        mem_shares =
            info['config.memoryAllocation.shares.shares']

        rp_name =
            @rp_list
            .select do |item|
                item[:ref] == ref
            end.first[:name] rescue ''

        rp_name = 'Resources' if rp_name.empty?

        rp_info << "\nVCENTER_RESOURCE_POOL_INFO = ["
        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 << ']'
    end

    view.DestroyView

    rp_info
end

#monitor_vms(host_id, vm_type) ⇒ Object



615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
# File 'lib/host.rb', line 615

def monitor_vms(host_id, vm_type)
    vc_uuid = @vi_client.vim.serviceContent.about.instanceUuid
    cluster_name = self['name']
    cluster_ref = self['_ref']

    # Get info of the host where the VM/template is located
    one_host =
        VCenterDriver::VIHelper
        .one_item(
            OpenNebula::Host,
            host_id
        )
    if !one_host
        STDERR.puts "Failed to retieve host with id #{host.id}"
        if VCenterDriver::CONFIG[:debug_information]
            STDERR.puts "#{message} #{e.backtrace}"
        end
    end

    esx_hosts = {}
    @item.host.each do |esx_host|
        esx_hosts[esx_host._ref] = {
            :name => esx_host.name,
            :cpu  => esx_host.summary.hardware.cpuMhz.to_f
        }
    end

    monitored_vms = Set.new
    str_info = ''

    view =
        @vi_client
        .vim
        .serviceContent
        .viewManager
        .CreateContainerView(
            {
                :container => @item, # View for VMs inside this cluster
                :type => ['VirtualMachine'],
                :recursive => true
            }
        )

    pc = @vi_client.vim.serviceContent.propertyCollector

    monitored_properties = [
        'name', # VM name
        'config.template', # To filter out templates
        'summary.runtime.powerState', # VM power state
        'summary.quickStats.hostMemoryUsage', # Memory usage
        'summary.quickStats.overallCpuUsage', # CPU used by VM
        'runtime.host', # ESX host
        'resourcePool', # RP
        'guest.guestFullName',
        # IP addresses as seen by guest tools,
        'guest.net',
        'guest.guestState',
        'guest.toolsVersion',
        'guest.toolsRunningStatus',
        # IP addresses as seen by guest tools,
        'guest.toolsVersionStatus2',
        # VM extraconfig info e.g opennebula.vm.running
        'config.extraConfig',
        'config.hardware.numCPU',
        'config.hardware.memoryMB',
        'config.annotation',
        'datastore'
    ]

    filter_spec = RbVmomi::VIM.PropertyFilterSpec(
        :objectSet => [
            { :obj => view,
            :skip => true,
            :selectSet => [
                RbVmomi::VIM.TraversalSpec(
                    :name => 'traverseEntities',
                    :type => 'ContainerView',
                    :path => 'view',
                    :skip => false
                )
            ] }
        ],
        :propSet => [
            {
                :type => 'VirtualMachine',
                 :pathSet => monitored_properties
            }
        ]
    )

    result = pc.RetrieveProperties(:specSet => [filter_spec])

    vms = {}
    vm_objects = []
    result.each do |r|
        hashed_properties = r.to_hash
        next unless r.obj.is_a?(RbVmomi::VIM::VirtualMachine)

        # Only take care of VMs, not templates
        if !hashed_properties['config.template']
            vms[r.obj._ref + '_' + vc_uuid] = hashed_properties
            vm_objects << r.obj
        end
    end

    pm = @vi_client.vim.serviceContent.perfManager

    stats = {}

    max_samples = 9
    refresh_rate = 20 # Real time stats takes samples every 20 seconds

    last_mon_time = one_host['TEMPLATE/VCENTER_LAST_PERF_POLL']

    if last_mon_time
        interval = (Time.now.to_i - last_mon_time.to_i)
        interval = 3601 if interval < 0
        samples = (interval / refresh_rate)
        samples = 1 if samples == 0
        interval > 3600 ? max_samples = 9 : max_samples = samples
    end

    if !vm_objects.empty?
        stats = pm.retrieve_stats(
            vm_objects,
            [
                'net.transmitted',
                'net.bytesRx',
                'net.bytesTx',
                'net.received',
                'virtualDisk.numberReadAveraged',
                'virtualDisk.numberWriteAveraged',
                'virtualDisk.read',
                'virtualDisk.write'
            ],
            {
                :max_samples => max_samples
            }
        ) rescue {}
    end

    if !stats.empty?
        last_mon_time = Time.now.to_i.to_s
    end

    @rp_list ||= get_resource_pool_list

    vm_pool =
        VCenterDriver::VIHelper
        .one_pool(
            OpenNebula::VirtualMachinePool
        )
    # We filter to retrieve only those VMs
    # running in the host that we are monitoring
    host_vms =
        vm_pool
        .retrieve_xmlelements(
            "/VM_POOL/VM[HISTORY_RECORDS/HISTORY/HID='#{host_id}']"
        )

    vms.each do |vm_ref, info|
        vm_info = ''
        begin
            esx_host = esx_hosts[info['runtime.host']._ref]
            info[:esx_host_name] = esx_host[:name]
            info[:esx_host_cpu] = esx_host[:cpu]
            info[:cluster_name] = cluster_name
            info[:cluster_ref] = cluster_ref
            info[:vc_uuid] = vc_uuid
            info[:host_id] = host_id
            info[:rp_list] = @rp_list

            # Check the running flag
            running_flag = info['config.extraConfig'].select do |val|
                val[:key] == 'opennebula.vm.running'
            end

            if !running_flag.empty? && running_flag.first
                running_flag = running_flag[0][:value]
            end

            next if running_flag == 'no'

            id = -1
            # Find the VM by its deploy_id,
            # which in the vCenter driver is
            # the vCenter managed object reference
            found_vm =
                host_vms
                .select do |vm|
                    vm['DEPLOY_ID'] == vm_ref ||
                    vm['DEPLOY_ID'] == VIHelper.get_deploy_id(vm_ref)
                end.first
            id = found_vm['ID'] if found_vm

            # skip if it is a wild and
            # we are looking for OpenNebula VMs
            next if (vm_type == 'ones') && (id == -1)
            # skip if it is not a wild and we are looking for wilds
            next if (vm_type == 'wilds') && (id != -1)
            # skip if already monitored
            next if monitored_vms.include? vm_ref

            monitored_vms << vm_ref

            vm =
                VCenterDriver::VirtualMachine
                .new(
                    @vi_client,
                    vm_ref,
                    id
                )
            vm.vm_info = info
            vm.monitor(stats)

            vm_name = "#{info['name']} - #{cluster_name}"
            vm_info << "VM = [ ID=\"#{id}\", "
            vm_info << "VM_NAME=\"#{vm_name}\", "
            vm_info << "DEPLOY_ID=\"#{vm_ref}\", "

            # if the machine does not exist in
            # opennebula it means that is a wild:
            if vm.one_exist?
                mon_s64 = Base64.strict_encode64(vm.info)
                vm_info << "MONITOR=\"#{mon_s64}\"]\n"
            else
                vm_template64 =
                    Base64
                    .encode64(
                        vm.vm_to_one(vm_name)
                    ).gsub("\n", '')
                vm_info << 'VCENTER_TEMPLATE="YES",'
                vm_info << "IMPORT_TEMPLATE=\"#{vm_template64}\"]\n"
            end
        rescue StandardError => e
            vm_info = error_monitoring(e, id, vm_ref, vc_uuid, info)
        end

        str_info << vm_info
    end

    view.DestroyView # Destroy the view

    [str_info, last_mon_time]
end

#nsx_getObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
# File 'lib/host.rb', line 131

def nsx_get
    nsx_info = ''
    nsx_obj = {}
    # In the future add more than one nsx manager
    extension_list =
        @vi_client
        .vim
        .serviceContent
        .extensionManager
        .extensionList
    extension_list.each do |ext_list|
        case ext_list.key
        when NSXDriver::NSXConstants::NSXV_EXTENSION_LIST
            nsx_obj['type'] = NSXDriver::NSXConstants::NSXV
            url_full = ext_list.client[0].url
            url_split = url_full.split('/')
            # protocol = "https://"
            protocol = url_split[0] + '//'
            # ip_port = ip:port
            ip_port = url_split[2]
            nsx_obj['url'] = protocol + ip_port
            nsx_obj['version'] = ext_list.version
            nsx_obj['label'] = ext_list.description.label
        when NSXDriver::NSXConstants::NSXT_EXTENSION_LIST
            nsx_obj['type'] = NSXDriver::NSXConstants::NSXT
            nsx_obj['url'] = ext_list.server[0].url
            nsx_obj['version'] = ext_list.version
            nsx_obj['label'] = ext_list.description.label
        else
            next
        end
    end
    unless nsx_obj.empty?
        nsx_info << "NSX_MANAGER=\"#{nsx_obj['url']}\"\n"
        nsx_info << "NSX_TYPE=\"#{nsx_obj['type']}\"\n"
        nsx_info << "NSX_VERSION=\"#{nsx_obj['version']}\"\n"
        nsx_info << "NSX_LABEL=\"#{nsx_obj['label']}\"\n"
    end
    nsx_info
end

#nsx_ready?Boolean

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/host.rb', line 172

def nsx_ready?
    @one_item =
        VCenterDriver::VIHelper
        .one_item(
            OpenNebula::Host,
            @vi_client
            .instance_variable_get(
                :@host_id
            ).to_i
        )

    # Check if NSX_MANAGER is into the host template
    if [nil, ''].include?(@one_item['TEMPLATE/NSX_MANAGER'])
        @nsx_status = "NSX_STATUS = \"Missing NSX_MANAGER\"\n"
        return false
    end

    # Check if NSX_USER is into the host template
    if [nil, ''].include?(@one_item['TEMPLATE/NSX_USER'])
        @nsx_status = "NSX_STATUS = \"Missing NSX_USER\"\n"
        return false
    end

    # Check if NSX_PASSWORD is into the host template
    if [nil, ''].include?(@one_item['TEMPLATE/NSX_PASSWORD'])
        @nsx_status = "NSX_STATUS = \"Missing NSX_PASSWORD\"\n"
        return false
    end

    # Check if NSX_TYPE is into the host template
    if [nil, ''].include?(@one_item['TEMPLATE/NSX_TYPE'])
        @nsx_status = "NSX_STATUS = \"Missing NSX_TYPE\"\n"
        return false
    end

    # Try a connection as part of NSX_STATUS
    nsx_client = NSXDriver::NSXClient
                 .new_from_id(
                     @vi_client
                     .instance_variable_get(
                         :@host_id
                     ).to_i
                 )

    if @one_item['TEMPLATE/NSX_TYPE'] == NSXDriver::NSXConstants::NSXV
        # URL to test a connection
        url = '/api/2.0/vdn/scopes'
        begin
            if nsx_client.get(url)
                @nsx_status = "NSX_STATUS = \"OK\"\n"
                return true
            else
                @nsx_status =
                    "NSX_STATUS = \"Response code incorrect\"\n"
                return false
            end
        rescue StandardError
            @nsx_status = 'NSX_STATUS = "Error connecting to ' \
                          "NSX_MANAGER\"\n"
            return false
        end
    end

    nxs_type = @one_item['TEMPLATE/NSX_TYPE']
    return unless nxs_type == NSXDriver::NSXConstants::NSXT

    # URL to test a connection
    url = '/api/v1/transport-zones'
    begin
        if nsx_client.get(url)
            @nsx_status = "NSX_STATUS = \"OK\"\n"
            true
        else
            @nsx_status =
                "NSX_STATUS = \"Response code incorrect\"\n"
            false
        end
    rescue StandardError
        @nsx_status = 'NSX_STATUS = "Error connecting to '\
                        "NSX_MANAGER\"\n"
        false
    end
end

#resource_poolsObject



95
96
97
98
99
100
101
# File 'lib/host.rb', line 95

def resource_pools
    if @resource_pools.nil?
        @resource_pools = fetch_resource_pools(@item.resourcePool)
    end

    @resource_pools
end

#tz_getObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/host.rb', line 256

def tz_get
    @nsx_status = ''
    if !nsx_ready?
        tz_info = @nsx_status
    else
        tz_info = "NSX_STATUS = OK\n"
        tz_info << 'NSX_TRANSPORT_ZONES = ['

        nsx_client =
            NSXDriver::NSXClient
            .new_from_id(
                @vi_client
                .instance_variable_get(
                    :@host_id
                ).to_i
            )
        tz_object = NSXDriver::TransportZone.new_child(nsx_client)

        # NSX request to get Transport Zones
        case @one_item['TEMPLATE/NSX_TYPE']
        when NSXDriver::NSXConstants::NSXV
            tzs = tz_object.tzs
            tzs.each do |tz|
                tz_info << tz.xpath('name').text << '="'
                tz_info << tz.xpath('objectId').text << '",'
            end
            tz_info.chomp!(',')
        when NSXDriver::NSXConstants::NSXT
            r = tz_object.tzs
            r['results'].each do |tz|
                tz_info << tz['display_name'] << '="'
                tz_info << tz['id'] << '",'
            end
            tz_info.chomp!(',')
        else
            raise "Unknown Port Group type \
            #{@one_item['TEMPLATE/NSX_TYPE']}"
        end
        tz_info << ']'
        return tz_info
    end
    tz_info
end