def _info_all(self): CollectorStatus.print_latest_status(verbose=True) if not _is_affirmative(self._config.get('dogstatsd6_enable')): DogstatsdStatus.print_latest_status(verbose=True) else: dsd6_status = Dogstatsd6._get_dsd6_stats(self._config) if dsd6_status: dsd6_status.render() ForwarderStatus.print_latest_status(verbose=True)
def _get_dsd6_stats(self, cfg={}): port = cfg.get('dogstatsd6_stats_port', 5000) try: dsd6_agg_stats = get_expvar_stats('aggregator', port=port) dsd6_stats = get_expvar_stats('dogstatsd', port=port) except Exception as e: log.info("Unable to collect dogstatsd6 statistics: %s", e) return None if dsd6_stats is not None and dsd6_agg_stats is not None: packet_count = dsd6_stats.get("ServiceCheckPackets", 0) + \ dsd6_stats.get("EventPackets", 0) + \ dsd6_stats.get("MetricPackets", 0) flush_counts = dsd6_agg_stats.get("FlushCount", {}) dsd6_status = DogstatsdStatus( flush_count=dsd6_agg_stats.get('NumberOfFlush', 0), packet_count=packet_count, packets_per_second="N/A", # unavailable metric_count=flush_counts.get("Series", {}).get("LastFlush", 0), event_count=flush_counts.get("Events", {}).get("LastFlush", 0), service_check_count=flush_counts.get("ServiceChecks", {}).get("LastFlush", 0)) return dsd6_status return None
def flush(self): try: self.flush_count += 1 packets_per_second = self.metrics_aggregator.packets_per_second( self.interval) packet_count = self.metrics_aggregator.total_count metrics = self.metrics_aggregator.flush() count = len(metrics) if not count: logger.info("Flush #%s: No metrics to flush." % self.flush_count) else: logger.info("Flush #%s: flushing %s metrics" % (self.flush_count, count)) self.submit(metrics) # Persist a status message. packet_count = self.metrics_aggregator.total_count DogstatsdStatus(flush_count=self.flush_count, packet_count=packet_count, packets_per_second=packets_per_second, metric_count=count).persist() except: logger.exception("Error flushing metrics")
def main(config_path=None): """ The main entry point for the unix version of dogstatsd. """ parser = optparse.OptionParser("%prog [start|stop|restart|status]") parser.add_option('-u', '--use-local-forwarder', action='store_true', dest="use_forwarder", default=False) opts, args = parser.parse_args() reporter, server = init(config_path, use_watchdog=True, use_forwarder=opts.use_forwarder) pid_file = PidFile('dogstatsd') daemon = Dogstatsd(pid_file.get_path(), server, reporter) # If no args were passed in, run the server in the foreground. if not args: daemon.run() return 0 # Otherwise, we're process the deamon command. else: command = args[0] if command == 'info': DogstatsdStatus.print_latest_status() return 0 if command == 'start': daemon.start() elif command == 'stop': daemon.stop() elif command == 'restart': daemon.restart() elif command == 'status': pid = pid_file.get_pid() if pid: message = 'dogstatsd is running with pid %s' % pid else: message = 'dogstatsd is not running' logger.info(message) sys.stdout.write(message + "\n") else: sys.stderr.write("Unknown command: %s\n\n" % command) parser.print_help() return 1 return 0
def info(self, cfg=None): logging.getLogger().setLevel(logging.ERROR) if cfg and not _is_affirmative(cfg.get('dogstatsd6_enable', False)): message = DogstatsdStatus._dogstatsd6_unavailable_message() exit_code = -1 else: alt_title = "{} (v BETA)".format(self.DSD6_BIN_NAME) dsd6_status = Dogstatsd6._get_dsd6_stats(cfg) if dsd6_status: message = dsd6_status.render(alt_title) exit_code = 0 else: message = DogstatsdStatus._dogstatsd6_unavailable_message(alt_title) exit_code = -1 sys.stdout.write(message) return exit_code
def run(self): logger.info("Reporting to %s every %ss" % (self.api_host, self.interval)) logger.debug("Watchdog enabled: %s" % bool(self.watchdog)) # Persist a start-up message. DogstatsdStatus().persist() while not self.finished.isSet(): # Use camel case isSet for 2.4 support. self.finished.wait(self.interval) self.metrics_aggregator.send_packet_count('datadog.dogstatsd.packet.count') self.flush() if self.watchdog: self.watchdog.reset() # Clean up the status messages. logger.debug("Stopped reporter") DogstatsdStatus.remove_latest_status()
def flush(self): try: self.flush_count += 1 self.log_count += 1 packets_per_second = self.metrics_aggregator.packets_per_second( self.interval) packet_count = self.metrics_aggregator.total_count metrics = self.metrics_aggregator.flush() count = len(metrics) if self.flush_count % FLUSH_LOGGING_PERIOD == 0: self.log_count = 0 if count: self.submit(metrics) events = self.metrics_aggregator.flush_events() event_count = len(events) if event_count: self.submit_events(events) service_checks = self.metrics_aggregator.flush_service_checks() service_check_count = len(service_checks) if service_check_count: self.submit_service_checks(service_checks) should_log = self.flush_count <= FLUSH_LOGGING_INITIAL or self.log_count <= FLUSH_LOGGING_COUNT log_func = log.info if not should_log: log_func = log.debug log_func( "Flush #%s: flushed %s metric%s, %s event%s, and %s service check run%s" % (self.flush_count, count, plural(count), event_count, plural(event_count), service_check_count, plural(service_check_count))) if self.flush_count == FLUSH_LOGGING_INITIAL: log.info( "First flushes done, %s flushes will be logged every %s flushes." % (FLUSH_LOGGING_COUNT, FLUSH_LOGGING_PERIOD)) # Persist a status message. packet_count = self.metrics_aggregator.total_count DogstatsdStatus( flush_count=self.flush_count, packet_count=packet_count, packets_per_second=packets_per_second, metric_count=count, event_count=event_count, service_check_count=service_check_count, ).persist() except Exception: if self.finished.isSet(): log.debug( "Couldn't flush metrics, but that's expected as we're stopping" ) else: log.exception("Error flushing metrics")
def run(self): log.info("Reporting to %s every %ss" % (self.api_host, self.interval)) log.debug("Watchdog enabled: %s" % bool(self.watchdog)) # Persist a start-up message. DogstatsdStatus().persist() while not self.finished.isSet(): # Use camel case isSet for 2.4 support. self.finished.wait(self.interval) self.metrics_aggregator.send_packet_count('datadog.dogstatsd.packet.count') self.flush() if self.watchdog: self.watchdog.reset() # Clean up the status messages. log.debug("Stopped reporter") DogstatsdStatus.remove_latest_status()
def flush(self): try: self.flush_count += 1 packets_per_second = self.metrics_aggregator.packets_per_second( self.interval) packet_count = self.metrics_aggregator.total_count metrics = self.metrics_aggregator.flush() count = len(metrics) should_log = self.flush_count < LOGGING_INTERVAL or self.flush_count % LOGGING_INTERVAL == 0 if not count: if should_log: log.info("Flush #%s: No metrics to flush." % self.flush_count) else: if should_log: log.info("Flush #%s: flushing %s metrics" % (self.flush_count, count)) self.submit(metrics) events = self.metrics_aggregator.flush_events() event_count = len(events) if not event_count: if should_log: log.info("Flush #%s: No events to flush." % self.flush_count) else: log.debug("Flush #%s: No events to flush." % self.flush_count) else: if should_log: log.info("Flush #%s: flushing %s events" % (self.flush_count, len(events))) else: log.debug("Flush #%s: flushing %s events" % (self.flush_count, len(events))) self.submit_events(events) # Persist a status message. packet_count = self.metrics_aggregator.total_count DogstatsdStatus( flush_count=self.flush_count, packet_count=packet_count, packets_per_second=packets_per_second, metric_count=count, event_count=event_count, ).persist() except Exception, e: log.exception("Error flushing metrics")
def get(self): dogstatsd_status = DogstatsdStatus.load_latest_status() forwarder_status = ForwarderStatus.load_latest_status() collector_status = CollectorStatus.load_latest_status() self.render(os.path.join(agent_root, "pup", "status.html"), port=port, platform=platform.platform(), agent_version=get_version(), python_version=platform.python_version(), logger_info=logger_info(), dogstatsd=dogstatsd_status.to_dict(), forwarder=forwarder_status.to_dict(), collector=collector_status.to_dict(), )
def get(self): dogstatsd_status = DogstatsdStatus.load_latest_status() forwarder_status = ForwarderStatus.load_latest_status() collector_status = CollectorStatus.load_latest_status() self.render( os.path.join(agent_root, "pup", "status.html"), port=port, platform=platform.platform(), agent_version=get_version(), python_version=platform.python_version(), logger_info=logger_info(), dogstatsd=dogstatsd_status.to_dict(), forwarder=forwarder_status.to_dict(), collector=collector_status.to_dict(), )
def main(config_path=None): """ The main entry point for the unix version of dogstatsd. """ parser = optparse.OptionParser("%prog [start|stop|restart|status]") parser.add_option("-u", "--use-local-forwarder", action="store_true", dest="use_forwarder", default=False) opts, args = parser.parse_args() # commands that don't need the daemon if args and args[0] in ["info", "status"]: command = args[0] if command == "info": logging.getLogger().setLevel(logging.ERROR) return DogstatsdStatus.print_latest_status() elif command == "status": pid = pid_file.get_pid() if pid: message = "dogstatsd is running with pid %s" % pid else: message = "dogstatsd is not running" log.info(message) sys.stdout.write(message + "\n") return 0 reporter, server = init(config_path, use_watchdog=True, use_forwarder=opts.use_forwarder) pid_file = PidFile("dogstatsd") daemon = Dogstatsd(pid_file.get_path(), server, reporter) # If no args were passed in, run the server in the foreground. if not args: daemon.run() return 0 # Otherwise, we're process the deamon command. else: command = args[0] if command == "start": daemon.start() elif command == "stop": daemon.stop() elif command == "restart": daemon.restart() else: sys.stderr.write("Unknown command: %s\n\n" % command) parser.print_help() return 1 return 0
def flush(self): try: self.flush_count += 1 self.log_count += 1 packets_per_second = self.metrics_aggregator.packets_per_second( self.interval) packet_count = self.metrics_aggregator.total_count metrics = self.metrics_aggregator.flush() count = len(metrics) if self.flush_count % FLUSH_LOGGING_PERIOD == 0: self.log_count = 0 if count: self.submit(metrics) events = self.metrics_aggregator.flush_events() event_count = len(events) if event_count: self.submit_events(events) should_log = self.flush_count <= FLUSH_LOGGING_INITIAL or self.log_count <= FLUSH_LOGGING_COUNT log_func = log.info if not should_log: log_func = log.debug log_func("Flush #%s: flushed %s metric%s and %s event%s" % (self.flush_count, count, plural(count), event_count, plural(event_count))) if self.flush_count == FLUSH_LOGGING_INITIAL: log.info( "First flushes done, %s flushes will be logged every %s flushes." % (FLUSH_LOGGING_COUNT, FLUSH_LOGGING_PERIOD)) # Persist a status message. packet_count = self.metrics_aggregator.total_count DogstatsdStatus( flush_count=self.flush_count, packet_count=packet_count, packets_per_second=packets_per_second, metric_count=count, event_count=event_count, ).persist() except Exception, e: log.exception("Error flushing metrics")
def latest_status(self): try: loaded_template = template.Loader(".") dogstatsd_status = DogstatsdStatus.load_latest_status() forwarder_status = ForwarderStatus.load_latest_status() collector_status = CollectorStatus.load_latest_status() generated_template = loaded_template.load("status.html").generate( port=22, platform=platform.platform(), agent_version=get_version(), python_version=platform.python_version(), logger_info=logger_info(), dogstatsd=dogstatsd_status.to_dict(), forwarder=forwarder_status.to_dict(), collector=collector_status.to_dict(), ) return generated_template except Exception: return "Unable to fetch latest status"
def latest_status(self): try: loaded_template = template.Loader(".") dogstatsd_status = DogstatsdStatus.load_latest_status() forwarder_status = ForwarderStatus.load_latest_status() collector_status = CollectorStatus.load_latest_status() generated_template = loaded_template.load("status.html").generate( port=22, platform=platform.platform(), agent_version=get_version(), python_version=platform.python_version(), logger_info=logger_info(), dogstatsd=dogstatsd_status.to_dict(), forwarder=forwarder_status.to_dict(), collector=collector_status.to_dict(), ) return generated_template except Exception: return ("Unable to fetch latest status")
def _info_all(self): CollectorStatus.print_latest_status(verbose=True) DogstatsdStatus.print_latest_status(verbose=True) ForwarderStatus.print_latest_status(verbose=True)
def info(self): logging.getLogger().setLevel(logging.ERROR) return DogstatsdStatus.print_latest_status()