Module: ElasticityGrammar::Variable3

Defined in:
lib/opennebula/flow/grammar.rb

Instance Method Summary collapse

Instance Method Details

#result(role, vm_pool) ⇒ Object



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
# File 'lib/opennebula/flow/grammar.rb', line 926

def result(role, vm_pool)
    nodes = role.info_nodes(vm_pool)
    total = 0
    n_nodes = 0
    att = text_value.upcase

    nodes.each { |node|
        if node && node['vm_info']

            vm_state = node['vm_info']['VM']['STATE']
            lcm_state = node['vm_info']['VM']['LCM_STATE']

            # Use values from VMs in RUNNING only

            if vm_state != '3' || lcm_state != '3'
                next
            end

            value = nil
            if node['vm_info']['VM']['USER_TEMPLATE'][att]
                value = node['vm_info']['VM']['USER_TEMPLATE'][att]
            elsif node['vm_info']['VM']['MONITORING'][att]
                value = node['vm_info']['VM']['MONITORING'][att]
            elsif node['vm_info']['VM']['TEMPLATE'][att]
                value = node['vm_info']['VM']['TEMPLATE'][att]
            elsif node['vm_info']['VM'][att]
                value = node['vm_info']['VM'][att]
            end

            if value && value.respond_to?(:to_f)
                total += value.to_f
                n_nodes += 1
            end
        end
    }

    # The attribute wasn't found for any of the nodes
    if n_nodes == 0
        val = nil
        st = "#{att}[--]"
    else
        val = ((total / n_nodes)*100).round/100.0
        st = "#{att}[#{val.to_s}]"
    end

    return [val, st]
end