Commits

hoop committed 2897f03

Accept plugin_name as part of Connection object instead of hardcoding as "any"

  • Participants
  • Parent commits 32df409

Comments (0)

Files changed (2)

 SEND_INTERVAL = 10      # seconds
 MAX_PACKET_SIZE = 1024  # bytes
 
-PLUGIN_NAME = "any"
 PLUGIN_TYPE = "gauge"
 
 TYPE_HOST            = 0x0000
     else:
         raise AssertionError("invalid type code " + str(id))
 
-def message_start(when=None, host=socket.gethostname(), plugin_inst=""):
+def message_start(when=None, host=socket.gethostname(), plugin_name="", plugin_inst=""):
     return "".join([
         pack(TYPE_HOST, host),
         pack(TYPE_TIME, when or time.time()),
-        pack(TYPE_PLUGIN, PLUGIN_NAME),
+        pack(TYPE_PLUGIN, plugin_name),
         pack(TYPE_PLUGIN_INSTANCE, plugin_inst),
         pack(TYPE_TYPE, PLUGIN_TYPE),
         pack(TYPE_INTERVAL, SEND_INTERVAL)
     ])
 
-def messages(counts, when=None, host=socket.gethostname(), plugin_inst=""):
+def messages(counts, when=None, host=socket.gethostname(), plugin_name="", plugin_inst=""):
     packets = []
-    start = message_start(when, host, plugin_inst)
+    start = message_start(when, host, plugin_name, plugin_inst)
     parts = [pack(name, count) for name,count in counts.items()]
     parts = [p for p in parts if len(start) + len(p) <= MAX_PACKET_SIZE]
     if parts:
     @synchronized
     def __new__(cls, hostname = socket.gethostname(),
                      collectd_host = "localhost", collectd_port = 25826,
-                     plugin_inst = ""):
-        id = (hostname, collectd_host, collectd_port, plugin_inst)
+                     plugin_name = "any", plugin_inst = ""):
+        id = (hostname, collectd_host, collectd_port, plugin_name, plugin_inst)
         if id in cls.instances:
             return cls.instances[id]
         else:
     
     def __init__(self, hostname = socket.gethostname(),
                        collectd_host = "localhost", collectd_port = 25826,
-                       plugin_inst = ""):
+                       plugin_name = "", plugin_inst = ""):
         self._lock = RLock()
         self._counters = {}
+        self._plugin_name = plugin_name
         self._plugin_inst = plugin_inst
         self._hostname = hostname
         self._collectd_addr = (collectd_host, collectd_port)
 def send_stats(raise_on_empty = False):
     try:
         when, stats, conn = snaps.get(timeout = 0.1)
-        for message in messages(stats, when, conn._hostname, conn._plugin_inst):
+        for message in messages(stats, when, conn._hostname, conn._plugin_name, conn._plugin_inst):
             sock.sendto(message, conn._collectd_addr)
     except Empty:
         if raise_on_empty:
             self.assertTrue(struct.pack("<d", value) in packet)
             self.assertTrue(collectd.pack("test-"+name, value) in packet)
     
+    def test_plugin_name(self):
+        conn = collectd.Connection(collectd_port = self.TEST_PORT,
+                                   plugin_name = "dckx")
+        self.assertTrue("dckx" in self.send_and_recv(conn, foo=5))
+
     def test_plugin_inst(self):
         conn = collectd.Connection(collectd_port = self.TEST_PORT,
                                    plugin_inst = "xkcd")