从Ruby中的键值文件中获取统计信息

时间:2014-04-15 13:29:04

标签: ruby

我有一个内容

的文件
Test_CG:
    Copy stats:
      Test_CG:
        SAN traffic:
          Current throughput: 22832 bps
          Average  throughput: 1 Mbps
          Current write IOPS: 1
          Average write IOPS: 4

      Test_CG_DR:
        Journal:
          Usage: 477.13MB
          Total: 2.45GB
          Latest image: Fri Apr 11 11:03:04.561405 2014
          Current image: Fri Apr 11 11:03:04.561405 2014
          Journal lag: 130.18MB
          Protection window:N/A
          Average journal compression ratio:N/A
          Mode: Normal
    Link stats:
      Test_CG -> Test_CG_DR:
        Init:
          SAN traffic: 134 Mbps
          WAN traffic: 79 Mbps
          Progress: 80.68%
        Replication:
          Lag:
            Time: 30 sec
            Data: 52.57MB
            Writes: 886
          WAN traffic: 13 Mbps
          Current bandwidth reduction ratio: 1.08711
          Average bandwidth reduction ratio: 1.09669
          Current deduplication ratio:N/A
          Average deduplication ratio:N/A

我想从2个部分,Link Stats和Replication

中获取WAN流量数据

我使用ruby进行了很多文本处理,但我认为它必须是结构化的,

我可以用ruby做JSON或YAML吗?

实际上任何技术都会有所帮助。

此致

A

1 个答案:

答案 0 :(得分:0)

如果要将其解析为YAML,则必须修复格式:N/A之前应该有一个空格。如果这是一致的格式化故障,您可以使用gsub

执行此操作
require 'yaml'
file_contents = load_my_file()
cleaned_yaml = file_contents.gsub(/:N\/A/, ': N/A')
hash = YAML.load(cleaned_yaml)

hash['Test_CG']['Link stats']['Test_CG -> Test_CG_DR']['Init']['WAN traffic']
# => "79 Mbps"
hash['Test_CG']['Link stats']['Test_CG -> Test_CG_DR']['Replication']['WAN traffic']
# => "13 Mbps"

这适用于提供的示例。

使用真实数据运行时,您的里程可能会有所不同。