4

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

mraj
  • 128
  • 1
  • 5
  • Why don't you use Google and tell us what you find? – John Zwinck Mar 08 '13 at 03:27
  • I googled and got a solution but that is not optimal ..am asking about a direct python module ..? – mraj Mar 08 '13 at 03:31
  • Got the solution, I used the method " rrdtool.fetch(args)" in the module rrdtool, for extracting the data from rrd file and json convertion part manually by appending extracted strings with keys. – mraj Mar 08 '13 at 03:43
  • @mrag: Please enter that as an answer - preferably with more detail - not a comment, so it's more obvious for future searchers. – GreenMatt Mar 08 '13 at 04:57
  • will share the code.. – mraj Mar 08 '13 at 06:14

2 Answers2

2

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()
Kir Chou
  • 2,980
  • 1
  • 36
  • 48
mraj
  • 128
  • 1
  • 5
  • 2
    Consider using python json library. using json.dump() will take care about encoding python data structure (i.e. list of dictionaries) into json format and sending result into the STDOUT (or file) stream. – vvladymyrov Mar 08 '13 at 15:42
-1

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

Ivo
  • 5,378
  • 2
  • 18
  • 18