performance - reducing carbon footprint :)
* Establishing one carbon connection for all plugins, instead of one connection for each. This rather drastically improves performance. From a few measurements this reduces processing time by over 50% * This can be even further enhanced, to keep one persistent carbon (/munin?) connection and only re-establish it if it drops (For Future)
This commit is contained in:
parent
aec4f100ee
commit
3c57345a4e
@ -26,6 +26,7 @@ class Munin():
|
|||||||
self.displayname = self.hostname.split(".")[0]
|
self.displayname = self.hostname.split(".")[0]
|
||||||
self._sock = None
|
self._sock = None
|
||||||
self._conn = None
|
self._conn = None
|
||||||
|
self._carbon_sock = None
|
||||||
self.hello_string = None
|
self.hello_string = None
|
||||||
|
|
||||||
if self.args.displayname:
|
if self.args.displayname:
|
||||||
@ -72,10 +73,27 @@ class Munin():
|
|||||||
logging.exception("Unable to communicate to Munin host %s, port: %s",
|
logging.exception("Unable to communicate to Munin host %s, port: %s",
|
||||||
self.hostname, self.port)
|
self.hostname, self.port)
|
||||||
|
|
||||||
|
if self.args.carbon:
|
||||||
|
self.connect_carbon()
|
||||||
|
|
||||||
|
def connect_carbon(self):
|
||||||
|
carbon_host, carbon_port = self.args.carbon.split(":")
|
||||||
|
try:
|
||||||
|
self._carbon_sock = socket.create_connection((carbon_host, carbon_port), 10)
|
||||||
|
except socket.error:
|
||||||
|
logging.exception("Unable to connect to Carbon on host %s, port: %s",
|
||||||
|
carbon_host, carbon_port)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
def close_connection(self):
|
def close_connection(self):
|
||||||
"""Close connection to Munin host."""
|
"""Close connection to Munin host."""
|
||||||
self._sock.close()
|
self._sock.close()
|
||||||
|
|
||||||
|
def close_carbon_connection(self):
|
||||||
|
"""Close connection to Carbon host."""
|
||||||
|
if self._carbon_sock:
|
||||||
|
self._carbon_sock.close()
|
||||||
|
|
||||||
def _readline(self):
|
def _readline(self):
|
||||||
"""Read one line from Munin output, stripping leading/trailing chars."""
|
"""Read one line from Munin output, stripping leading/trailing chars."""
|
||||||
return self._conn.readline().strip()
|
return self._conn.readline().strip()
|
||||||
@ -183,13 +201,13 @@ class Munin():
|
|||||||
plugin_config, self.hostname)
|
plugin_config, self.hostname)
|
||||||
end_timestamp = time.time() - start_timestamp
|
end_timestamp = time.time() - start_timestamp
|
||||||
self.close_connection()
|
self.close_connection()
|
||||||
|
self.close_carbon_connection()
|
||||||
logging.info("Finished querying host %s (Execution Time: %.2f sec).",
|
logging.info("Finished querying host %s (Execution Time: %.2f sec).",
|
||||||
self.hostname, end_timestamp)
|
self.hostname, end_timestamp)
|
||||||
return end_timestamp
|
return end_timestamp
|
||||||
|
|
||||||
def send_to_carbon(self, timestamp, plugin_name, plugin_config, plugin_data):
|
def send_to_carbon(self, timestamp, plugin_name, plugin_config, plugin_data):
|
||||||
"""Send plugin data to Carbon over Pickle format."""
|
"""Send plugin data to Carbon over Pickle format."""
|
||||||
carbon_host, carbon_port = self.args.carbon.split(":")
|
|
||||||
if self.args.noprefix:
|
if self.args.noprefix:
|
||||||
prefix = ''
|
prefix = ''
|
||||||
else:
|
else:
|
||||||
@ -218,15 +236,11 @@ class Munin():
|
|||||||
header = struct.pack("!L", len(payload))
|
header = struct.pack("!L", len(payload))
|
||||||
message = header + payload
|
message = header + payload
|
||||||
try:
|
try:
|
||||||
carbon_sock = socket.create_connection((carbon_host, carbon_port), 10)
|
self._carbon_sock.sendall(message)
|
||||||
carbon_sock.sendall(message)
|
|
||||||
carbon_sock.close()
|
|
||||||
logging.info("Finished sending plugin %s data to Carbon for host %s.",
|
logging.info("Finished sending plugin %s data to Carbon for host %s.",
|
||||||
plugin_name, self.hostname)
|
plugin_name, self.hostname)
|
||||||
except socket.error:
|
except socket.error:
|
||||||
logging.exception("Unable to connect to Carbon on host %s, port: %s",
|
logging.exception("Unable to send data to Carbon")
|
||||||
carbon_host, carbon_port)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user