Пример #1
0
def main():
    parser = argparse.ArgumentParser(description="Interact with the Datadog API",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--config', help="location of your dogrc file (default ~/.dogrc)",
                        default=os.path.expanduser('~/.dogrc'))
    parser.add_argument('--api-key', help="your API key, from "
                        "https://app.datadoghq.com/account/settings#api. "
                        "You can also set the environment variables DATADOG_API_KEY or DD_API_KEY",
                        dest='api_key', default=os.environ.get('DATADOG_API_KEY', os.environ.get('DD_API_KEY')))
    parser.add_argument('--application-key', help="your Application key, from "
                        "https://app.datadoghq.com/account/settings#api. "
                        "You can also set the environment variables DATADOG_APP_KEY or DD_APP_KEY",
                        dest='app_key', default=os.environ.get('DATADOG_APP_KEY', os.environ.get('DD_APP_KEY')))
    parser.add_argument('--pretty', help="pretty-print output (suitable for human consumption, "
                        "less useful for scripting)", dest='format',
                        action='store_const', const='pretty')
    parser.add_argument('--raw', help="raw JSON as returned by the HTTP service",
                        dest='format', action='store_const', const='raw')
    parser.add_argument('--timeout', help="time to wait in seconds before timing"
                        " out an API call (default 10)", default=10, type=int)
    parser.add_argument('-v', '--version', help='Dog API version', action='version',
                        version='%(prog)s {0}'.format(get_version()))

    config = DogshellConfig()

    # Set up subparsers for each service
    subparsers = parser.add_subparsers(title='Modes', dest='mode')
    subparsers.required = True

    CommentClient.setup_parser(subparsers)
    SearchClient.setup_parser(subparsers)
    MetricClient.setup_parser(subparsers)
    TagClient.setup_parser(subparsers)
    EventClient.setup_parser(subparsers)
    MonitorClient.setup_parser(subparsers)
    TimeboardClient.setup_parser(subparsers)
    DashboardClient.setup_parser(subparsers)
    ScreenboardClient.setup_parser(subparsers)
    DashboardListClient.setup_parser(subparsers)
    HostClient.setup_parser(subparsers)
    DowntimeClient.setup_parser(subparsers)
    ServiceCheckClient.setup_parser(subparsers)
    ServiceLevelObjectiveClient.setup_parser(subparsers)

    args = parser.parse_args()
    config.load(args.config, args.api_key, args.app_key)

    # Initialize datadog.api package
    initialize(**config)

    args.func(args)
Пример #2
0
def main():
    parser = argparse.ArgumentParser(description="Interact with the Datadog API",
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--config', help="location of your dogrc file (default ~/.dogrc)",
                        default=os.path.expanduser('~/.dogrc'))
    parser.add_argument('--api-key', help="your API key, from "
                        "https://app.datadoghq.com/account/settings#api",
                        dest='api_key', default=None)
    parser.add_argument('--application-key', help="your Application key, from "
                        "https://app.datadoghq.com/account/settings#api",
                        dest='app_key', default=None)
    parser.add_argument('--pretty', help="pretty-print output (suitable for human consumption, "
                        "less useful for scripting)", dest='format',
                        action='store_const', const='pretty')
    parser.add_argument('--raw', help="raw JSON as returned by the HTTP service",
                        dest='format', action='store_const', const='raw')
    parser.add_argument('--timeout', help="time to wait in seconds before timing"
                        " out an API call (default 10)", default=10, type=int)
    parser.add_argument('-v', '--version', help='Dog API version', action='version',
                        version='%(prog)s {0}'.format(get_version()))

    config = DogshellConfig()

    # Set up subparsers for each service
    subparsers = parser.add_subparsers(title='Modes', dest='mode')
    subparsers.required = True

    CommentClient.setup_parser(subparsers)
    SearchClient.setup_parser(subparsers)
    MetricClient.setup_parser(subparsers)
    TagClient.setup_parser(subparsers)
    EventClient.setup_parser(subparsers)
    MonitorClient.setup_parser(subparsers)
    TimeboardClient.setup_parser(subparsers)
    ScreenboardClient.setup_parser(subparsers)
    HostClient.setup_parser(subparsers)
    DowntimeClient.setup_parser(subparsers)
    ServiceCheckClient.setup_parser(subparsers)

    args = parser.parse_args()
    config.load(args.config, args.api_key, args.app_key)

    # Initialize datadog.api package
    initialize(**config)

    args.func(args)
Пример #3
0
def telemetry_metrics(metrics=1,
                      events=0,
                      service_checks=0,
                      bytes_sent=0,
                      bytes_dropped=0,
                      packets_sent=0,
                      packets_dropped=0,
                      transport="udp",
                      tags="",
                      namespace=""):
    version = get_version()
    if tags:
        tags = "," + tags

    return "\n{}datadog.dogstatsd.client.metrics:{}|c|#client:py,client_version:{},client_transport:{}{}\n".format(namespace, metrics, version, transport, tags) \
        + "{}datadog.dogstatsd.client.events:{}|c|#client:py,client_version:{},client_transport:{}{}\n".format(namespace, events, version, transport, tags) \
        + "{}datadog.dogstatsd.client.service_checks:{}|c|#client:py,client_version:{},client_transport:{}{}\n".format(namespace, service_checks, version, transport, tags) \
        + "{}datadog.dogstatsd.client.bytes_sent:{}|c|#client:py,client_version:{},client_transport:{}{}\n".format(namespace, bytes_sent, version, transport, tags) \
        + "{}datadog.dogstatsd.client.bytes_dropped:{}|c|#client:py,client_version:{},client_transport:{}{}\n".format(namespace, bytes_dropped, version, transport, tags) \
        + "{}datadog.dogstatsd.client.packets_sent:{}|c|#client:py,client_version:{},client_transport:{}{}\n".format(namespace, packets_sent, version, transport, tags) \
        + "{}datadog.dogstatsd.client.packets_dropped:{}|c|#client:py,client_version:{},client_transport:{}{}".format(namespace, packets_dropped, version, transport, tags)
Пример #4
0
def main():
    parser = optparse.OptionParser(usage="%prog -n [event_name] -k [api_key] --submit_mode \
[ all | errors ] [options] \"command\". \n\nNote that you need to enclose your command in \
quotes to prevent python executing as soon as there is a space in your command. \n \nNOTICE: In \
normal mode, the whole stderr is printed before stdout, in flush_live mode they will be mixed but \
there is not guarantee that messages sent by the command on both stderr and stdout are printed in \
the order they were sent.", version="%prog {0}".format(get_version()))

    parser.add_option('-n', '--name', action='store', type='string', help="the name of the event \
as it should appear on your Datadog stream")
    parser.add_option('-k', '--api_key', action='store', type='string',
                      help="your DataDog API Key")
    parser.add_option('-m', '--submit_mode', action='store', type='choice',
                      default='errors', choices=['errors', 'all'], help="[ all | errors ] if set \
to error, an event will be sent only of the command exits with a non zero exit status or if it \
times out.")
    parser.add_option('-p', '--priority', action='store', type='choice', choices=['normal', 'low'],
                      help="the priority of the event (default: 'normal')")
    parser.add_option('-t', '--timeout', action='store', type='int', default=60 * 60 * 24,
                      help="(in seconds)  a timeout after which your command must be aborted. An \
event will be sent to your DataDog stream (default: 24hours)")
    parser.add_option('--sigterm_timeout', action='store', type='int', default=60 * 2,
                      help="(in seconds)  When your command times out, the \
process it triggers is sent a SIGTERM. If this sigterm_timeout is reached, it will be sent a \
SIGKILL signal. (default: 2m)")
    parser.add_option('--sigkill_timeout', action='store', type='int', default=60,
                      help="(in seconds) how long to wait at most after SIGKILL \
                              has been sent (default: 60s)")
    parser.add_option('--proc_poll_interval', action='store', type='float', default=0.5,
                      help="(in seconds). interval at which your command will be polled \
(default: 500ms)")
    parser.add_option('--notify_success', action='store', type='string', default='',
                      help="a message string and @people directives to send notifications in \
case of success.")
    parser.add_option('--notify_error', action='store', type='string', default='',
                      help="a message string and @people directives to send notifications in \
case of error.")
    parser.add_option('-b', '--buffer_outs', action='store_true', dest='buffer_outs', default=False,
                      help="displays the stderr and stdout of the command only once it has \
returned (the command outputs remains buffered in dogwrap meanwhile)")
    parser.add_option('--tags', action='store', type='string', dest='tags', default='',
                      help="comma separated list of tags")

    options, args = parser.parse_args()

    cmd = []
    for part in args:
        cmd.extend(part.split(' '))
    # If silent is checked we force the outputs to be buffered (and therefore
    # not forwarded to the Terminal streams) and we just avoid printing the
    # buffers at the end
    returncode, stdout, stderr, duration = execute(
        cmd, options.timeout,
        options.sigterm_timeout, options.sigkill_timeout,
        options.proc_poll_interval, options.buffer_outs)

    initialize(api_key=options.api_key)
    host = api._host_name

    if returncode == 0:
        alert_type = SUCCESS
        event_priority = 'low'
        event_title = u'[%s] %s succeeded in %.2fs' % (host, options.name,
                                                       duration)
    else:
        alert_type = ERROR
        event_priority = 'normal'

        if returncode is Timeout:
            event_title = u'[%s] %s timed out after %.2fs' % (host, options.name, duration)
            returncode = -1
        else:
            event_title = u'[%s] %s failed in %.2fs' % (host, options.name, duration)

    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 options.tags:
        tags = [t.strip() for t in options.tags.split(',')]
    else:
        tags = None

    event_body = build_event_body(cmd, returncode, stdout, stderr, notifications)

    event = {
        'alert_type': alert_type,
        'aggregation_key': options.name,
        'host': host,
        'priority': options.priority or event_priority,
        'tags': tags
    }

    if options.buffer_outs:
        print >> sys.stderr, stderr.strip().encode('utf8')
        print >> sys.stdout, stdout.strip().encode('utf8')

    if options.submit_mode == 'all' or returncode != 0:
        api.Event.create(title=event_title, text=event_body, **event)

    sys.exit(returncode)
Пример #5
0
* datadog.dogshell: a command-line tool, wrapping datadog.api, to interact with Datadog REST API.
"""
# stdlib
import logging
import os
import os.path

# datadog
from datadog import api
from datadog.dogstatsd import DogStatsd, statsd  # noqa
from datadog.threadstats import ThreadStats, datadog_lambda_wrapper, lambda_metric  # noqa
from datadog.util.compat import iteritems, NullHandler, text
from datadog.util.config import get_version
from datadog.util.hostname import get_hostname

__version__ = get_version()

# Loggers
logging.getLogger('datadog.api').addHandler(NullHandler())
logging.getLogger('datadog.dogstatsd').addHandler(NullHandler())
logging.getLogger('datadog.threadstats').addHandler(NullHandler())


def initialize(api_key=None,
               app_key=None,
               host_name=None,
               api_host=None,
               statsd_host=None,
               statsd_port=None,
               statsd_use_default_route=False,
               statsd_socket_path=None,
Пример #6
0
    def __init__(self,
                 host=DEFAULT_HOST,
                 port=DEFAULT_PORT,
                 max_buffer_size=50,
                 namespace=None,
                 constant_tags=None,
                 use_ms=False,
                 use_default_route=False,
                 socket_path=None,
                 default_sample_rate=1,
                 disable_telemetry=False):
        """
        Initialize a DogStatsd object.

        >>> statsd = DogStatsd()

        :envvar DD_AGENT_HOST: the host of the DogStatsd server.
        If set, it overrides default value.
        :type DD_AGENT_HOST: string

        :envvar DD_DOGSTATSD_PORT: the port of the DogStatsd server.
        If set, it overrides default value.
        :type DD_DOGSTATSD_PORT: integer

        :param host: the host of the DogStatsd server.
        :type host: string

        :param port: the port of the DogStatsd server.
        :type port: integer

        :param max_buffer_size: Maximum number of metrics to buffer before sending to the server
        if sending metrics in batch
        :type max_buffer_size: integer

        :param namespace: Namespace to prefix all metric names
        :type namespace: string

        :param constant_tags: Tags to attach to all metrics
        :type constant_tags: list of strings

        :param use_ms: Report timed values in milliseconds instead of seconds (default False)
        :type use_ms: boolean

        :envvar DATADOG_TAGS: Tags to attach to every metric reported by dogstatsd client
        :type DATADOG_TAGS: list of strings

        :envvar DD_ENTITY_ID: Tag to identify the client entity.
        :type DD_ENTITY_ID: string

        :param use_default_route: Dynamically set the DogStatsd host to the default route
        (Useful when running the client in a container) (Linux only)
        :type use_default_route: boolean

        :param socket_path: Communicate with dogstatsd through a UNIX socket instead of
        UDP. If set, disables UDP transmission (Linux only)
        :type socket_path: string

        :param default_sample_rate: Sample rate to use by default for all metrics
        :type default_sample_rate: float
        """

        self.lock = Lock()

        # Check host and port env vars
        agent_host = os.environ.get('DD_AGENT_HOST')
        if agent_host and host == DEFAULT_HOST:
            host = agent_host

        dogstatsd_port = os.environ.get('DD_DOGSTATSD_PORT')
        if dogstatsd_port and port == DEFAULT_PORT:
            try:
                port = int(dogstatsd_port)
            except ValueError:
                log.warning(
                    "Port number provided in DD_DOGSTATSD_PORT env var is not an integer: \
                %s, using %s as port number", dogstatsd_port, port)

        # Connection
        if socket_path is not None:
            self.socket_path = socket_path
            self.host = None
            self.port = None
            transport = "uds"
        else:
            self.socket_path = None
            self.host = self.resolve_host(host, use_default_route)
            self.port = int(port)
            transport = "udp"

        # Socket
        self.socket = None
        self.max_buffer_size = max_buffer_size
        self._send = self._send_to_server
        self.encoding = 'utf-8'

        # Options
        env_tags = [
            tag for tag in os.environ.get('DATADOG_TAGS', '').split(',') if tag
        ]
        if constant_tags is None:
            constant_tags = []
        self.constant_tags = constant_tags + env_tags
        entity_id = os.environ.get('DD_ENTITY_ID')
        if entity_id:
            entity_tag = '{name}:{value}'.format(name=ENTITY_ID_TAG_NAME,
                                                 value=entity_id)
            self.constant_tags.append(entity_tag)
        if namespace is not None:
            namespace = text(namespace)
        self.namespace = namespace
        self.use_ms = use_ms
        self.default_sample_rate = default_sample_rate

        # init telemetry
        self._client_tags = [
            "client:py",
            "client_version:{}".format(get_version()),
            "client_transport:{}".format(transport),
        ]
        self._reset_telementry()
        self._telemetry = not disable_telemetry
Пример #7
0
def main():
    parser = optparse.OptionParser(
        usage="%prog -n [event_name] -k [api_key] --submit_mode \
[ all | errors ] [options] \"command\". \n\nNote that you need to enclose your command in \
quotes to prevent python as soon as there is a space in your command. \n \nNOTICE: In normal \
mode, the whole stderr is printed before stdout, in flush_live mode they will be mixed but there \
is not guarantee that messages sent by the command on both stderr and stdout are printed in the \
order they were sent.",
        version="%prog {0}".format(get_version()))

    parser.add_option('-n',
                      '--name',
                      action='store',
                      type='string',
                      help="the name of the event \
as it should appear on your Datadog stream")
    parser.add_option('-k',
                      '--api_key',
                      action='store',
                      type='string',
                      help="your DataDog API Key")
    parser.add_option('-m',
                      '--submit_mode',
                      action='store',
                      type='choice',
                      default='errors',
                      choices=['errors', 'all'],
                      help="[ all | errors ] if set \
to error, an event will be sent only of the command exits with a non zero exit status or if it \
times out.")
    parser.add_option('-p',
                      '--priority',
                      action='store',
                      type='choice',
                      choices=['normal', 'low'],
                      help="the priority of the event (default: 'normal')")
    parser.add_option(
        '-t',
        '--timeout',
        action='store',
        type='int',
        default=60 * 60 * 24,
        help=
        "(in seconds)  a timeout after which your command must be aborted. An \
event will be sent to your DataDog stream (default: 24hours)")
    parser.add_option('--sigterm_timeout',
                      action='store',
                      type='int',
                      default=60 * 2,
                      help="(in seconds)  When your command times out, the \
process it triggers is sent a SIGTERM. If this sigterm_timeout is reached, it will be sent a \
SIGKILL signal. (default: 2m)")
    parser.add_option(
        '--sigkill_timeout',
        action='store',
        type='int',
        default=60,
        help="(in seconds) how long to wait at most after SIGKILL \
                              has been sent (default: 60s)")
    parser.add_option(
        '--proc_poll_interval',
        action='store',
        type='float',
        default=0.5,
        help="(in seconds). interval at which your command will be polled \
(default: 500ms)")
    parser.add_option(
        '--notify_success',
        action='store',
        type='string',
        default='',
        help="a message string and @people directives to send notifications in \
case of success.")
    parser.add_option(
        '--notify_error',
        action='store',
        type='string',
        default='',
        help="a message string and @people directives to send notifications in \
case of error.")
    parser.add_option(
        '-b',
        '--buffer_outs',
        action='store_true',
        dest='buffer_outs',
        default=False,
        help="displays the stderr and stdout of the command only once it has \
returned (the command outputs remains buffered in dogwrap meanwhile)")
    parser.add_option('--tags',
                      action='store',
                      type='string',
                      dest='tags',
                      default='',
                      help="comma separated list of tags")

    options, args = parser.parse_args()

    cmd = []
    for part in args:
        cmd.extend(part.split(' '))
    # If silent is checked we force the outputs to be buffered (and therefore
    # not forwarded to the Terminal streams) and we just avoid printing the
    # buffers at the end
    returncode, stdout, stderr, duration = execute(cmd, options.timeout,
                                                   options.sigterm_timeout,
                                                   options.sigkill_timeout,
                                                   options.proc_poll_interval,
                                                   options.buffer_outs)

    initialize(api_key=options.api_key)
    host = api._host_name

    if returncode == 0:
        alert_type = SUCCESS
        event_priority = 'low'
        event_title = u'[%s] %s succeeded in %.2fs' % (host, options.name,
                                                       duration)
    else:
        alert_type = ERROR
        event_priority = 'normal'

        if returncode is Timeout:
            event_title = u'[%s] %s timed out after %.2fs' % (
                host, options.name, duration)
            returncode = -1
        else:
            event_title = u'[%s] %s failed in %.2fs' % (host, options.name,
                                                        duration)

    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 options.tags:
        tags = [t.strip() for t in options.tags.split(',')]
    else:
        tags = None

    event_body = build_event_body(cmd, returncode, stdout, stderr,
                                  notifications)

    event = {
        'alert_type': alert_type,
        'aggregation_key': options.name,
        'host': host,
        'priority': options.priority or event_priority,
        'tags': tags
    }

    if options.buffer_outs:
        print >> sys.stderr, stderr.strip().encode('utf8')
        print >> sys.stdout, stdout.strip().encode('utf8')

    if options.submit_mode == 'all' or returncode != 0:
        api.Event.create(title=event_title, text=event_body, **event)

    sys.exit(returncode)
Пример #8
0
"""
# stdlib
import logging
import os
import os.path

# datadog
from datadog import api
from datadog.dogstatsd import DogStatsd, statsd  # noqa
from datadog.threadstats import ThreadStats  # noqa
from datadog.util.compat import iteritems, NullHandler
from datadog.util.config import get_version
from datadog.util.hostname import get_hostname


__version__ = get_version()

# Loggers
logging.getLogger('datadog.api').addHandler(NullHandler())
logging.getLogger('datadog.dogstatsd').addHandler(NullHandler())
logging.getLogger('datadog.threadstats').addHandler(NullHandler())


def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
               statsd_host=None, statsd_port=None, statsd_use_default_route=False, **kwargs):
    """
    Initialize and configure Datadog.api and Datadog.statsd modules

    :param api_key: Datadog API key
    :type api_key: string
Пример #9
0
def parse_options(raw_args=None):
    '''
    Parse the raw command line options into an options object and the remaining command string
    '''
    parser = optparse.OptionParser(
        usage="%prog -n [event_name] -k [api_key] --submit_mode \
[ all | errors | warnings] [options] \"command\". \n\nNote that you need to enclose your command in \
quotes to prevent python executing as soon as there is a space in your command. \n \nNOTICE: In \
normal mode, the whole stderr is printed before stdout, in flush_live mode they will be mixed but \
there is not guarantee that messages sent by the command on both stderr and stdout are printed in \
the order they were sent.",
        version="%prog {0}".format(get_version()),
        option_class=DogwrapOption)

    parser.add_option('-n',
                      '--name',
                      action='store',
                      type='string',
                      help="the name of the event \
as it should appear on your Datadog stream")
    parser.add_option('-k',
                      '--api_key',
                      action='store',
                      type='string',
                      help="your DataDog API Key")
    parser.add_option('-s',
                      '--site',
                      action='store',
                      type='choice',
                      default='datadoghq.com',
                      choices=['datadoghq.com', 'us', 'datadoghq.eu', 'eu'],
                      help="The site \
to send data, US (datadoghq.com) or EU (datadoghq.eu), default: US")
    parser.add_option('-m',
                      '--submit_mode',
                      action='store',
                      type='choice',
                      default='errors',
                      choices=['errors', 'warnings', 'all'],
                      help="[ all | errors | warnings ] if set \
to error, an event will be sent only of the command exits with a non zero exit status or if it \
times out. If set to warning, a list of exit codes need to be provided")
    parser.add_option(
        '--warning_codes',
        action='store',
        type='warning_codes',
        dest='warning_codes',
        help="comma separated list of warning codes, e.g: 127,255")
    parser.add_option('-p',
                      '--priority',
                      action='store',
                      type='choice',
                      choices=['normal', 'low'],
                      help="the priority of the event (default: 'normal')")
    parser.add_option(
        '-t',
        '--timeout',
        action='store',
        type='int',
        default=60 * 60 * 24,
        help=
        "(in seconds)  a timeout after which your command must be aborted. An \
event will be sent to your DataDog stream (default: 24hours)")
    parser.add_option('--sigterm_timeout',
                      action='store',
                      type='int',
                      default=60 * 2,
                      help="(in seconds)  When your command times out, the \
process it triggers is sent a SIGTERM. If this sigterm_timeout is reached, it will be sent a \
SIGKILL signal. (default: 2m)")
    parser.add_option(
        '--sigkill_timeout',
        action='store',
        type='int',
        default=60,
        help="(in seconds) how long to wait at most after SIGKILL \
                              has been sent (default: 60s)")
    parser.add_option(
        '--proc_poll_interval',
        action='store',
        type='float',
        default=0.5,
        help="(in seconds). interval at which your command will be polled \
(default: 500ms)")
    parser.add_option(
        '--notify_success',
        action='store',
        type='string',
        default='',
        help="a message string and @people directives to send notifications in \
case of success.")
    parser.add_option(
        '--notify_error',
        action='store',
        type='string',
        default='',
        help="a message string and @people directives to send notifications in \
case of error.")
    parser.add_option(
        '--notify_warning',
        action='store',
        type='string',
        default='',
        help="a message string and @people directives to send notifications in \
    case of warning.")
    parser.add_option(
        '-b',
        '--buffer_outs',
        action='store_true',
        dest='buffer_outs',
        default=False,
        help="displays the stderr and stdout of the command only once it has \
returned (the command outputs remains buffered in dogwrap meanwhile)")
    parser.add_option('--tags',
                      action='store',
                      type='string',
                      dest='tags',
                      default='',
                      help="comma separated list of tags")

    options, args = parser.parse_args(args=raw_args)

    if is_p3k():
        cmd = ' '.join(args)
    else:
        cmd = b' '.join(args).decode('utf-8')

    return options, cmd