dimanche 30 décembre 2018

ruby hash get from dynamic key and value (MIB SNMP)

Im a newbie on ruby, i have hash data like below.

person = {"PoolName.11.22.33":"pool_a","PoolMemberName.11.22.33.11":"member_pool_a1","PoolMemberScore.11.22.33.11":0,"PoolName.11.22.44":"pool_b","PoolMemberName.11.22.44.11":"member_pool_b1","PoolMemberName.11.22.44.12":"member_pool_b2","PoolMemberScore.11.22.44.11":2,"PoolMemberScore.11.22.44.12":3,"PoolName.11.22.55":"pool_c","PoolMemberName.11.22.55.11":"member_pool_c1","PolMemberName.11.22.55.12":"member_pool_c2","PoolMemberName.11.22.55.13":"member_pool_c3","PoolMemberScore.11.22.55.11":11,"PoolMemberScore.11.22.55.12":22,"PoolMemberScore.11.22.55.13":33}

Results i should get like following below:

"pool_a.member_pool_a1" : 0,
"pool_b.member_pool_b1" : 2,
"pool_b.member_pool_b2" : 3,
"pool_c.member_pool_c1" : 11,
"pool_c.member_pool_c2" : 22,
"pool_c.member_pool_c3" : 33

I have code like following below :

begin
        person.each{|key|
            if ( key =~ /PoolName/ || key =~ /PoolMemberName/ )
                newid = key.gsub(/PoolName./, "").gsub(/PoolMemberName./, "")
                val = event.get(key).gsub(/\/Common\//, "").gsub(/\./, "_")
                event.set(newid, val)
                event.remove(key)
            end
        }

    rescue Exception => e
        event.set("logstash_ruby_exception", "underscores: " + e.message)
    end

Aucun commentaire:

Enregistrer un commentaire