Convert .rrd file to json in python

耗尽温柔 提交于 2019-12-10 16:57:58

问题


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


回答1:


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()



回答2:


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



来源:https://stackoverflow.com/questions/15286169/convert-rrd-file-to-json-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!