Convert .rrd file to json in python

2019-06-28 05:08发布

Is there any python module available for converting the .rrd file to json format ?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-28 05:34

Following is the code I tried for generating json from rrd file.

#!/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()
查看更多
爷、活的狠高调
3楼-- · 2019-06-28 05:48

There won't be an inbuilt library module for it.

查看更多
登录 后发表回答