Merge pull request #2 from gingerlime/master

updated logger message
This commit is contained in:
Zdenek Pizl 2014-02-14 20:17:54 +01:00
commit f05d3b597b

View File

@ -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:
@ -35,12 +36,14 @@ class Munin():
"""Bootstrap method to start processing hosts's Munin stats.""" """Bootstrap method to start processing hosts's Munin stats."""
self.connect() self.connect()
self.update_hostname() self.update_hostname()
self.process_host_stats() processing_time = self.process_host_stats()
interval = self.args.interval
while True and self.args.interval != 0: while True and interval != 0:
time.sleep(self.args.interval) sleep_time = max(interval - processing_time, 0)
time.sleep(sleep_time)
self.connect() self.connect()
self.process_host_stats() processing_time = self.process_host_stats()
def update_hostname(self): def update_hostname(self):
"""Updating hostname from connection hello string.""" """Updating hostname from connection hello string."""
@ -70,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()
@ -109,7 +129,8 @@ class Munin():
key_name = multigraph_prefix + full_key_name.split(".")[0] key_name = multigraph_prefix + full_key_name.split(".")[0]
response[multigraph][key_name] = key_value response[multigraph][key_name] = key_value
except (KeyError, AttributeError): except (KeyError, AttributeError):
logging.info("plugin multi_graph %s returns invalid data for host %s\n ", multigraph, self.hostname) logging.info("plugin %s returned invalid data [%s] for host"
" %s\n", plugin, current_line, self.hostname)
return response return response
@ -180,12 +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
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:
@ -214,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():