diff --git a/m2g-poller.py b/m2g-poller.py index 966b68c..d1b76aa 100755 --- a/m2g-poller.py +++ b/m2g-poller.py @@ -26,6 +26,7 @@ class Munin(): self.displayname = self.hostname.split(".")[0] self._sock = None self._conn = None + self._carbon_sock = None self.hello_string = None if self.args.displayname: @@ -35,12 +36,14 @@ class Munin(): """Bootstrap method to start processing hosts's Munin stats.""" self.connect() self.update_hostname() - self.process_host_stats() + processing_time = self.process_host_stats() + interval = self.args.interval - while True and self.args.interval != 0: - time.sleep(self.args.interval) + while True and interval != 0: + sleep_time = max(interval - processing_time, 0) + time.sleep(sleep_time) self.connect() - self.process_host_stats() + processing_time = self.process_host_stats() def update_hostname(self): """Updating hostname from connection hello string.""" @@ -70,10 +73,27 @@ class Munin(): logging.exception("Unable to communicate to Munin host %s, port: %s", 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): """Close connection to Munin host.""" self._sock.close() + def close_carbon_connection(self): + """Close connection to Carbon host.""" + if self._carbon_sock: + self._carbon_sock.close() + def _readline(self): """Read one line from Munin output, stripping leading/trailing chars.""" return self._conn.readline().strip() @@ -109,7 +129,8 @@ class Munin(): key_name = multigraph_prefix + full_key_name.split(".")[0] response[multigraph][key_name] = key_value 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 @@ -180,12 +201,13 @@ class Munin(): plugin_config, self.hostname) end_timestamp = time.time() - start_timestamp self.close_connection() + self.close_carbon_connection() logging.info("Finished querying host %s (Execution Time: %.2f sec).", self.hostname, end_timestamp) + return end_timestamp def send_to_carbon(self, timestamp, plugin_name, plugin_config, plugin_data): """Send plugin data to Carbon over Pickle format.""" - carbon_host, carbon_port = self.args.carbon.split(":") if self.args.noprefix: prefix = '' else: @@ -214,15 +236,11 @@ class Munin(): header = struct.pack("!L", len(payload)) message = header + payload try: - carbon_sock = socket.create_connection((carbon_host, carbon_port), 10) - carbon_sock.sendall(message) - carbon_sock.close() + self._carbon_sock.sendall(message) logging.info("Finished sending plugin %s data to Carbon for host %s.", plugin_name, self.hostname) except socket.error: - logging.exception("Unable to connect to Carbon on host %s, port: %s", - carbon_host, carbon_port) - sys.exit(1) + logging.exception("Unable to send data to Carbon") def parse_args():