Browse Source

Output XML file passes xmllint.
Added all components data into XML output. (using total and each tag).


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@664 e5f2f494-b856-4b98-b285-d166d9295462

Kazunori Fujiwara 15 years ago
parent
commit
8aac83ba01
1 changed files with 25 additions and 12 deletions
  1. 25 12
      src/bin/stats/statsd.py

+ 25 - 12
src/bin/stats/statsd.py

@@ -9,6 +9,7 @@ import select
 import os
 
 bossgroup = 'Boss'
+myname = 'statsd'
 
 def total(s):
     def totalsub(d,s):
@@ -47,7 +48,7 @@ def total(s):
     out['timestamp2'] = _time2;
     return out
 
-def dicttoxml(stats):
+def dicttoxml(stats, level = 0):
     def dicttoxmlsub(s, level):
         output = ''
         spaces = ' ' * level
@@ -61,21 +62,33 @@ def dicttoxml(stats):
         return output
 
     for k in stats.keys():
-        output = '<component="%s">\n' % k
+        space = ' ' * level
+        output = space + '<component component="%s">\n' % k
         s = stats[k]
         if ('component' in s or 'components' in s):
-            output = output + dicttoxmlsub(s, 1)
+            output += dicttoxmlsub(s, level+1)
         else:
             for l in s.keys():
-                output +=   ' <from="%s">\n' % l \
-                          + dicttoxmlsub(s[l], 2) \
-                          + '  </from>\n'
-        output += '</component>\n'
+                output +=  space + ' <from from="%s">\n' % l \
+                          + dicttoxmlsub(s[l], level+2) \
+                          + space + '  </from>\n'
+        output += space + '</component>\n'
         return output
 
-def dump_stats(statpath, statcount, stat):
+def dump_stats(statpath, statcount, stat, statraw):
     newfile = open(statpath + '.new', 'w')
-    newfile.write(dicttoxml(stat))
+    newfile.write('<?xml version="1.0" encoding="UTF-8"?>\n')
+    newfile.write('<!-- created at '+time.strftime('%Y%m%d %H%M%S')+' -->\n')
+    newfile.write('<isc version="0.0">\n')
+    newfile.write(' <bind10>\n')
+    newfile.write('  <total>\n')
+    newfile.write(dicttoxml(stat, 3))
+    newfile.write('  </total>\n')
+    newfile.write('  <each>\n')
+    newfile.write(dicttoxml(statraw, 3))
+    newfile.write('  </each>\n')
+    newfile.write(' </bind10>\n')
+    newfile.write('</isc>\n')
     newfile.close()
     loop = statcount
     while(loop > 0):
@@ -92,7 +105,7 @@ def collector(statgroup,step,statpath,statcount):
     cc = ISC.CC.Session()
     print (cc.lname)
     cc.group_subscribe(statgroup)
-    cc.group_subscribe(bossgroup)
+    cc.group_subscribe(bossgroup, myname)
     wrote_time = -1
     last_wrote_time = -1
     last_recvd_time = -1
@@ -101,7 +114,7 @@ def collector(statgroup,step,statpath,statcount):
     while 1:
         wait = wrote_time + step - time.time()
         if wait <= 0 and last_recvd_time > wrote_time:
-            dump_stats(statpath, statcount, statstotal)
+            dump_stats(statpath, statcount, statstotal, stats)
             last_wrote_time = wrote_time;
             wrote_time = time.time();
             wait = last_wrote_time + step - time.time()
@@ -111,7 +124,7 @@ def collector(statgroup,step,statpath,statcount):
         for sock in r:
             if sock == cc._socket:
                 data,envelope = cc.group_recvmsg(False)
-                if (envelope['group'] == 'Boss'):
+                if (envelope['group'] == bossgroup):
                     if ('shutdown' in data):
                         exit()
                 if (envelope['group'] == statgroup):