module ElasticityGrammar::Variable3

Public Instance Methods

result(role, vm_pool) click to toggle source
# 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