在.thon中将.rrd文件转换为json

时间:2013-03-08 03:23:53

标签: python rrd ganglia

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

2 个答案:

答案 0 :(得分:2)

以下是我尝试从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()

答案 1 :(得分:0)

不会有内置的库模块。