def start(self, api_key=None, flush_interval=10, roll_up_interval=10, host=None, device=None, api_host=None, use_ec2_instance_ids=False, flush_in_thread=True, flush_in_greenlet=False, disabled=False, statsd=False, statsd_host='localhost', statsd_port=8125): """ Configure the DogStatsApi instance and optionally, begin auto-flusing metrics. :param api_key: Your DataDog API key. :param flush_interval: The number of seconds to wait between flushes. :param flush_in_thread: True if you'd like to spawn a thread to flush metrics. It will run every `flush_interval` seconds. :param flush_in_greenlet: Set to true if you'd like to flush in a gevent greenlet. """ self.flush_interval = flush_interval self.roll_up_interval = roll_up_interval self.device = device self._disabled = disabled self.host = host or socket.gethostname() if use_ec2_instance_ids: self.host = get_ec2_instance_id() self._is_auto_flushing = False if statsd: # If we're configured to send to a statsd instance, use an aggregator # which forwards packets over UDP. log.info("Initializing dog api to use statsd: %s, %s" % (statsd_host, statsd_port)) self._needs_flush = False self._aggregator = StatsdAggregator(statsd_host, statsd_port) else: # Otherwise create an aggreagtor that while aggregator metrics # in process. self._needs_flush = True self._aggregator = MetricsAggregator(self.roll_up_interval) # The reporter is responsible for sending metrics off to their final destination. # It's abstracted to support easy unit testing and in the near future, forwarding # to the datadog agent. self.reporter = HttpReporter(api_key=api_key, api_host=api_host) self._is_flush_in_progress = False self.flush_count = 0 if self._disabled: log.info("dogapi is disabled. No metrics will flush.") else: if flush_in_greenlet: self._start_flush_greenlet() elif flush_in_thread: self._start_flush_thread()
def main(): parser = OptionParser() parser.add_option('-n', '--name', action='store', type='string') parser.add_option('-k', '--api_key', action='store', type='string') parser.add_option('-m', '--submit_mode', action='store', type='choice', default='errors', choices=['errors', 'all']) parser.add_option('-t', '--timeout', action='store', type='int', default=60*60*24) parser.add_option('--sigterm_timeout', action='store', type='int', default=60*2) parser.add_option('--sigkill_timeout', action='store', type='int', default=60) parser.add_option('--proc_poll_interval', action='store', type='float', default=0.5) options, args = parser.parse_args() dog.api_key = options.api_key cmd = [] for part in args: cmd.extend(part.split(' ')) returncode, stdout, stderr, duration = execute(cmd, options.timeout, options.sigterm_timeout, options.sigkill_timeout, options.proc_poll_interval) host = get_ec2_instance_id() if returncode == 0: alert_type = 'success' event_title = u'[%s] %s succeeded in %.2fs' % (host, options.name, duration) elif returncode is Timeout: alert_type = 'error' event_title = u'[%s] %s timed out after %.2fs' % (host, options.name, duration) returncode = -1 else: alert_type = 'error' event_title = u'[%s] %s failed in %.2fs' % (host, options.name, duration) event_body = [u'%%%\n', u'commmand:\n```\n', u' '.join(cmd), u'\n```\n', u'exit code: %s\n\n' % returncode, ] if stdout: event_body.extend([u'stdout:\n```\n', stdout, u'\n```\n']) if stderr: event_body.extend([u'stderr:\n```\n', stderr, u'\n```\n']) event_body.append(u'%%%\n') event_body = u''.join(event_body) event = { 'alert_type': alert_type, 'aggregation_key': options.name, 'host': host, } print >> sys.stderr, stderr.strip() print >> sys.stdout, stdout.strip() if options.submit_mode == 'all' or returncode != 0: dog.event(event_title, event_body, **event) sys.exit(returncode)
def main(): parser = OptionParser() parser.add_option('-n', '--name', action='store', type='string', help="The name of the event") parser.add_option('-k', '--api_key', action='store', type='string') parser.add_option('-m', '--submit_mode', action='store', type='choice', default='errors', choices=['errors', 'all']) parser.add_option('-t', '--timeout', action='store', type='int', default=60 * 60 * 24) parser.add_option('--sigterm_timeout', action='store', type='int', default=60 * 2) parser.add_option('--sigkill_timeout', action='store', type='int', default=60) parser.add_option('--proc_poll_interval', action='store', type='float', default=0.5) parser.add_option('--notify_success', action='store', type='string', default='') parser.add_option('--notify_error', action='store', type='string', default='') options, args = parser.parse_args() dog.api_key = options.api_key cmd = [] for part in args: cmd.extend(part.split(' ')) returncode, stdout, stderr, duration = execute(cmd, options.timeout, options.sigterm_timeout, options.sigkill_timeout, options.proc_poll_interval) host = get_ec2_instance_id() if returncode == 0: alert_type = SUCCESS event_title = u'[%s] %s succeeded in %.2fs' % (host, options.name, duration) elif returncode is Timeout: alert_type = ERROR event_title = u'[%s] %s timed out after %.2fs' % (host, options.name, duration) returncode = -1 else: alert_type = ERROR event_title = u'[%s] %s failed in %.2fs' % (host, options.name, duration) event_body = [ u'%%%\n', u'commmand:\n```\n', u' '.join(cmd), u'\n```\n', u'exit code: %s\n\n' % returncode, ] if stdout: event_body.extend([u'stdout:\n```\n', stdout, u'\n```\n']) if stderr: event_body.extend([u'stderr:\n```\n', stderr, u'\n```\n']) notifications = "" if alert_type == SUCCESS and options.notify_success: notifications = options.notify_success elif alert_type == ERROR and options.notify_error: notifications = options.notify_error if notifications: event_body.extend([u'notifications: %s\n' % (notifications)]) event_body.append(u'%%%\n') event_body = u''.join(event_body) event = { 'alert_type': alert_type, 'aggregation_key': options.name, 'host': host, } print >> sys.stderr, stderr.strip() print >> sys.stdout, stdout.strip() if options.submit_mode == 'all' or returncode != 0: dog.event(event_title, event_body, **event) sys.exit(returncode)