【问题标题】:Convert .rrd file to json in python在python中将.rrd文件转换为json
【发布时间】:2013-02-23 12:51:10
【问题描述】:

有没有可用于将 .rrd 文件转换为 json 格式的 python 模块?

【问题讨论】:

  • 您为什么不使用 Google 并告诉我们您发现了什么?
  • 我用谷歌搜索并得到了一个解决方案,但这不是最佳的..我问的是直接 python 模块..?
  • 得到了解决方案,我使用模块 rrdtool 中的方法“rrdtool.fetch(args)”,通过在提取的字符串中附加键来手动从 rrd 文件和 json 转换部分提取数据。跨度>
  • @mrag:请输入它作为答案 - 最好是更详细的 - 而不是评论,因此对于未来的搜索者来说更明显。
  • 将分享代码..

标签: python rrd ganglia


【解决方案1】:

以下是我尝试从 rrd 文件生成 json 的代码。

#!/usr/bin/python
import rrdtool
import sys

def printMetric():

  args = ["/var/lib/ganglia/rrds/__SummaryInfo__/cpu_system.rrd", "AVERAGE"]
  rrdMetric = rrdtool.fetch(args)

  time = rrdMetric[0][0]
  step = rrdMetric[0][2]

  sys.stdout.write("  {\n    \"Key1\":\"" + rrdMetric[1][0] +\
                   "\",\n    \"Key2\":\"" + "abcd" +\
                   "\",\n    \"metric_name\":\"" + "cpu_system" + "\",\n")  

  firstDP = True
  sys.stdout.write("    \"datapoints\":[\n")
  for tuple in rrdMetric[2]:
    if tuple[0] is not None:
      if not firstDP:
        sys.stdout.write(",\n")
      firstDP = False
      sys.stdout.write("      [")
      sys.stdout.write(str(tuple[0]))
      sys.stdout.write(",")
      sys.stdout.write(str(time))
      sys.stdout.write("]")
    time = time + step
  sys.stdout.write("\n    ]\n  }")

printMetric()

【讨论】:

  • 考虑使用 python json 库。使用 json.dump() 将负责将 python 数据结构(即字典列表)编码为 json 格式并将结果发送到 STDOUT(或文件)流。
【解决方案2】:

不会有内置的库模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 2020-06-12
    • 2016-02-27
    • 2023-03-03
    相关资源
    最近更新 更多