Browse Source

renamed stats-collector directory as stats
renamed b10-stats-collector.py as statsd.py
test code has been splitted as test_total.py


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

Kazunori Fujiwara 15 years ago
parent
commit
f74ad39629

+ 18 - 65
src/bin/stats-collector/b10-stats-collector.py

@@ -1,14 +1,14 @@
 #!/usr/bin/python
 #
-# This program collects "counters" from "statistics" channel.
-# It accepts one command: "Boss" group "shutdown"
+# This program collects 'counters' from 'statistics' channel.
+# It accepts one command: 'Boss' group 'shutdown'
 
 import ISC.CC
 import time
 import select
 import os
 
-bossgroup = "Boss"
+bossgroup = 'Boss'
 
 def total(s):
     def totalsub(d,s):
@@ -50,12 +50,14 @@ def total(s):
 def dicttoxml(stats):
     def dicttoxmlsub(s, level):
         output = ''
-        spaces = '  ' * level
+        spaces = ' ' * level
         for k in s.keys():
             if (isinstance(s[k], dict)):
-                output += spaces + ('<%s>\n' %k) + dicttoxmlsub(s[k], level+1) + spaces + '</%s>\n' %k
+                output += spaces + ('<%s>\n' %k) \
+                  + dicttoxmlsub(s[k], level+1) \
+                  + spaces + '</%s>\n' %k
             else:
-                output += spaces + '<%s>%s</%s>\n' %(k, s[k], k)
+                output += spaces + '<%s>%s</%s>\n' % (k, s[k], k)
         return output
 
     for k in stats.keys():
@@ -65,8 +67,10 @@ def dicttoxml(stats):
             output = output + dicttoxmlsub(s, 1)
         else:
             for l in s.keys():
-                output = output + '  <from="%s">\n' %l + dicttoxmlsub(s[l], 2) + "  </from>\n"
-        output = output + "</component>\n"
+                output +=   ' <from="%s">\n' % l \
+                          + dicttoxmlsub(s[l], 2) \
+                          + '  </from>\n'
+        output += '</component>\n'
         return output
 
 def dump_stats(statpath, statcount, stat):
@@ -75,9 +79,9 @@ def dump_stats(statpath, statcount, stat):
     newfile.close()
     loop = statcount
     while(loop > 0):
-        old = statpath + '.%d'%loop
+        old = statpath + '.%d' % loop
         loop -= 1
-        new = statpath + '.%d'%loop
+        new = statpath + '.%d' % loop
         if (os.access(new, os.F_OK)):
             os.rename(new, old)
     if (os.access(statpath, os.F_OK)):
@@ -107,10 +111,10 @@ 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 ("shutdown" in data):
+                if (envelope['group'] == 'Boss'):
+                    if ('shutdown' in data):
                         exit()
-                if (envelope["group"] == statgroup):
+                if (envelope['group'] == statgroup):
                     # Check received data
                     if (not('component' in data and 'version' in data
                         and 'stats' in data)):
@@ -123,57 +127,6 @@ def collector(statgroup,step,statpath,statcount):
                     (stats[component])[_from] = data;
                     statstotal[component] = total(stats[component])
                     last_recvd_time = time.time()
-                    #print (stats)
-                    #print (statstotal)
-                    #print (dicttoxml(statstotal))
 
-def test_total():
-    stats = {
-              'auth': {
-                     'from1': {
-                               'component':'auth',
-                               'version':1,
-                               'from':'from1',
-                               'timestamp':20100125,
-                               'stats': {
-                                   'AUTH': {
-                                       'counterid': 1,
-                                       'requestv4': 2,
-                                       'requestv6': 4,
-                                   },
-                                   'SYS': {
-                                       'sockets': 8,
-                                       'memory': 16,
-                                   },
-                                },
-                     },
-                     'from2': {
-                               'component':'auth',
-                               'version':1,
-                               'from':'from1',
-                               'timestamp':20100126,
-                               'stats': {
-                                   'AUTH': {
-                                       'counterid': 256,
-                                       'requestv4': 512,
-                                       'requestv6': 1024,
-                                   },
-                                   'SYS': {
-                                       'sockets': 2048,
-                                       'memory': 4096,
-                                   },
-                                },
-                     },
-              },
-            };
-    t = {}
-    for key in stats:
-        t[key] = total(stats[key])
-    print (stats)
-    print (dicttoxml(stats))
-    print (t)
-    print (dicttoxml(t))
-
-if __name__ == "__main__":
+if __name__ == '__main__':
     collector('statistics', 10, '/tmp/stats', 100)
-    #test_total()

+ 6 - 0
src/bin/stats/test/shutdown.py

@@ -0,0 +1,6 @@
+#!/usr/bin/python
+
+import ISC
+cc = ISC.CC.Session()
+cc.group_subscribe("Boss")
+cc.group_sendmsg({ "command":"shutdown"},"Boss")

+ 57 - 0
src/bin/stats/test/test_agent.py

@@ -0,0 +1,57 @@
+#!/usr/bin/python
+
+# This program acts statistics agent.
+# It has pseudo counters which is incremented each 10 second and
+# sends data to "statistics" channel periodically.
+# One command is available
+#   "Boss"       group: "shutdown"
+
+import ISC
+import time
+import select
+import random
+
+step_time = 10
+statgroup = "statistics"
+
+cc = ISC.CC.Session()
+print (cc.lname)
+#cc.group_subscribe(statgroup)
+cc.group_subscribe("Boss")
+
+# counters
+
+AUTH={}
+AUTH['counterid'] = 0
+AUTH['requestv4'] = 2
+AUTH['requestv6'] = 1
+SYS={}
+SYS['sockets'] = 0
+SYS['memory'] = 0
+
+sent = -1
+last_sent = -1
+loop = 0
+
+while 1:
+    wait = sent + step_time - time.time()
+    if wait <= 0:
+        last_sent = sent;
+        sent = time.time();
+        msg = {'component':'auth', 'version':1, 'timestamp':time.time(),'stats':{'AUTH':AUTH,'SYS':SYS}}
+        print (msg)
+        print (cc.group_sendmsg(msg, statgroup))
+        wait = last_sent + step_time - time.time()
+        if wait < 0:
+            wait = step_time
+        loop += 1
+    r,w,e = select.select([cc._socket],[],[], wait)
+    for sock in r:
+        if sock == cc._socket:
+            data,envelope = cc.group_recvmsg(False)
+            print (data)
+            if (envelope["group"] == "Boss"):
+                if ("shutdown" in data):
+                    exit()
+            else:
+                print ("Unknown data: ", envelope,data)

+ 54 - 0
src/bin/stats/test_total.py

@@ -0,0 +1,54 @@
+import sys
+sys.path.insert(0, '.')
+from statsd import *
+
+def test_total():
+    stats = {
+              'auth': {
+                     'from1': {
+                               'component':'auth',
+                               'version':1,
+                               'from':'from1',
+                               'timestamp':20100125,
+                               'stats': {
+                                   'AUTH': {
+                                       'counterid': 1,
+                                       'requestv4': 2,
+                                       'requestv6': 4,
+                                   },
+                                   'SYS': {
+                                       'sockets': 8,
+                                       'memory': 16,
+                                   },
+                                },
+                     },
+                     'from2': {
+                               'component':'auth',
+                               'version':1,
+                               'from':'from1',
+                               'timestamp':20100126,
+                               'stats': {
+                                   'AUTH': {
+                                       'counterid': 256,
+                                       'requestv4': 512,
+                                       'requestv6': 1024,
+                                   },
+                                   'SYS': {
+                                       'sockets': 2048,
+                                       'memory': 4096,
+                                   },
+                                },
+                     },
+              },
+            };
+    t = {}
+    for key in stats:
+        t[key] = total(stats[key])
+    print (stats)
+    print (dicttoxml(stats))
+    print (t)
+    print (dicttoxml(t))
+
+
+if __name__ == "__main__":
+    test_total()