def test_default_route(self): options = { 'statsd_use_default_route': True, } initialize(**options) t.assert_equal(statsd.use_default_route, True)
def _send_datadog_event(self, title, text, context, project): if not self.is_configured(project): return False tags = dict(context.get('tags')) data_dog_tags = ast.literal_eval(tags.pop('data_dog_tags', '[]')) kv_type_tags = ["%s:%s" % (k, v) for k, v in tags.iteritems()] tags = data_dog_tags + kv_type_tags # TODO aggregation_key = alert_type = 'error' source_type_name = 'sentry' api_key = self.get_option('SENTRY_DATADOG_API_KEY', project) or \ sentry.options.get('SENTRY_DATADOG_API_KEY') app_key = self.get_option('SENTRY_DATADOG_APP_KEY', project) or \ sentry.options.get('SENTRY_DATADOG_APP_KEY') options = { 'api_key': api_key, 'app_key': app_key, } initialize(**options) DataDogEvent.create(title=title, text=text, tags=tags, alert_type=alert_type, source_type_name=source_type_name, attach_host_name=False)
def __init__(self, *args, **kwargs): self._config = datadog(**kwargs) initialize(api_key=self._config.api_key, app_key=self._config.app_key, statsd_host=self._config.statsd_host, statsd_port=self._config.statsd_port)
def main(): module = AnsibleModule( argument_spec=dict( api_key=dict(required=True), app_key=dict(required=True), host=dict(required=True), state=dict(required=True, choices=['present', 'absent']), source=dict(required=False, default=None), by_source=dict(required=False, default=False, choices=BOOLEANS), tags=dict(required=False, default=None) ) ) # Prepare Datadog if not HAS_DATADOG: module.fail_json(msg='datadogpy required for this module') options = { 'api_key': module.params['api_key'], 'app_key': module.params['app_key'] } initialize(**options) if module.params['state'] == 'present': add_tags(module) elif module.params['state'] == 'absent': delete_tags(module)
def get_all_input_fields_and_add_to_data_dog(customer_name, api_key, app_key,support_mail, access_key, secret_key, slack_channels, customer_email_id, nginx_net_connections, alerts_list): options = { 'api_key': str(api_key), 'app_key': str(app_key) } initialize(**options) options = dict(notify_no_data=False, no_data_timeframe=0) options2 = dict(notify_no_data=False, no_data_timeframe=0, renotify_interval=10) tags = 'datadog' if "nginx" in alerts_list: print json.dumps (api.Monitor.create(type="metric alert", query="min(last_5m):avg:nginx.net.connections{*} by {host,name,region} >" + str(nginx_net_connections), name="["+str(customer_name)+"]" + " - " + "NGINX net connection is high", message="Team,NGINX net connection is high. Please take a look into this." + " @"+"[email protected] " + str(slack_channels) + " @"+str(customer_email_id) , tags=tags, options=options2)), print json.dumps (api.Monitor.create(type="metric alert", query="min(last_5m):avg:nginx.net.connections{*} by {host,name,region} >" + str(nginx_net_connections), name="[Ticket]" + " - " + "["+str(customer_name)+"]" + " - " + "NGINX net connection is high", message="Team,NGINX net connection is high. Please take a look into this. @"+str(support_mail), tags=tags, options=options)), return True
def main(): module = AnsibleModule( argument_spec=dict( api_key=dict(required=True), app_key=dict(required=True), state=dict(required=True, choices=['present', 'absent']), scope=dict(required=True), start=dict(required=False, default=None), end=dict(requried=False, default=None), message=dict(required=False, default=None), id=dict(required=False), current_only=dict(required=False, default=False), ) ) # Prepare Datadog if not HAS_DATADOG: module.fail_json(msg='datadogpy required for this module') options = { 'api_key': module.params['api_key'], 'app_key': module.params['app_key'] } initialize(**options) downtimes = _get_downtime(module) if module.params['state'] == 'present': schedule_downtime(module, downtimes) elif module.params['state'] == 'absent': cancel_downtime(module, downtimes)
def __init__(self, config): parser = argparse.ArgumentParser() parser.add_argument('--from-cron', action='store_true') parser.add_argument('--debug', action='store_true') args = parser.parse_args() log_level = logging.INFO if args.from_cron: log_level = logging.WARN elif args.debug: log_level = logging.DEBUG log_format = config.get_log_format(self.__class__.__name__) logging.basicConfig( filename=config.get_logging_config().get('collector_log_file'), format=log_format, level=log_level, ) if not args.from_cron: console = logging.StreamHandler() console.setLevel(log_level) console.setFormatter(logging.Formatter(log_format)) logging.getLogger('').addHandler(console) try: agent_config = get_config() if 'api_key' in agent_config: api_key = agent_config['api_key'] except: api_key = os.environ['DD_API_KEY'] initialize(api_key=api_key)
def __datadog(self, results): if (self.datadog): try: initialize(**datadog_keys) metrics = [] for target in self.targets: metrics.append({'metric' : "udh_webhook.%s.failure_rate" % self.test_id, 'points' : int(results['failure rate (%s)' % target].metric.value), 'tags' : ["region:%s" % results['region'].metric.value, "target:%s" % target]}) metrics.append({'metric' : "udh_webhook.%s.avg_flight_time" % self.test_id, 'points' : float(results['average flight time (%s)' % target].metric.value), 'tags' : ["region:%s" % results['region'].metric.value, "target:%s" % target]}) metrics.append({'metric' : "udh_webhook.%s.events_sent" % self.test_id, 'points' : float(results['events sent (%s)' % target].metric.value), 'tags' : ["region:%s" % results['region'].metric.value, "target:%s" % target]}) metrics.append({'metric' : "udh_webhook.%s.events_recv" % self.test_id, 'points' : float(results['events recv (%s)' % target].metric.value), 'tags' : ["region:%s" % results['region'].metric.value, "target:%s" % target]}) logging.debug("datadog metrics: %s" % str(metrics)) api.Metric.send(metrics) except Exception as e: logging.error("error: datadog submission failed: %s" % e)
def _datadog_init(self): datadog_options = { 'api_key': self.datadog_api_key, 'app_key': self.datadog_app_key } initialize(**datadog_options)
def main(board_id): # Configure keys in settings.json try: API_KEY = os.environ['DD_API'] APP_KEY = os.environ['DD_APP'] except: API_KEY = raw_input("API_KEY: ") APP_KEY = raw_input("APP_KEY: ") options = { 'api_key': API_KEY, 'app_key': APP_KEY } try: screen = json.load(open(board_id + '.txt')) except IOError: print "File not found" initialize(**options) try: # If it's a screenboard board_title = screen['board_title'] description = "Copy of " + board_title width = screen['width'] height = screen['height'] widgets = screen['widgets'] template_variables = screen['template_variables'] result = api.Screenboard.create(board_title=board_title, description=description, widgets=widgets, template_variables=template_variables, width=width, height=height) if 'errors' in result: print "\nInvalid board id. Maybe try a different account?\nQuitting..." return -1 print "Cloned board URL: https://app.datadoghq.com/screen/" + str(result['id']) except KeyError: # It's a dashboard print "Dashboard detected" board_title = screen['dash']['title'] description = "Copy of " + board_title graphs = screen['dash']['graphs'] try: # Get the template variables, if any template_variables = screen['dash']['tempate_variables'] except: template_variables = [] result = api.Timeboard.create(title=board_title, description=description, graphs=graphs, template_variables=template_variables) if 'errors' in result: print "\nInvalid board id. Maybe try a different account?\nQuitting..." return -1 print "Cloned board URL: https://app.datadoghq.com/dash/" + str(result['dash']['id'])
def get_stats(): ''' Configure a shared ThreadStats instance for datadog ''' global __stats if __stats is not None: return __stats if secrets.DATADOG_API_KEY: datadog.initialize( api_key=secrets.DATADOG_API_KEY, host_name='coverage.{}.moz.tools'.format(secrets.APP_CHANNEL), ) else: logger.info('No datadog credentials') # Must be instantiated after initialize # https://datadogpy.readthedocs.io/en/latest/#datadog-threadstats-module __stats = datadog.ThreadStats( constant_tags=[ config.PROJECT_NAME, 'channel:{}'.format(secrets.APP_CHANNEL), ], ) __stats.start(flush_in_thread=True) return __stats
def __init__(self, auth, app_key=None, host=None, **options): import datadog # datadog we use username in auth as api_key datadog.initialize(api_key=auth['username'], app_key=app_key, host_name=host, **options) self.client = datadog.api
def api(self): """The Datadog API stub for interacting with Datadog.""" if self.__api is None: datadog.initialize(api_key=self.__api_key, app_key=self.__app_key, host_name=self.__host) self.__api = datadog.api return self.__api
def get_all_dashboards(api_key, api_app_key, instance_name, ns_name=None): global datadog_flag if not datadog_flag: return None options = { 'api_key': api_key, 'app_key': api_app_key } if ns_name is None: title = 'Aerospike Dashboard: ' + str(instance_name) else: title = 'Aerospike Namespace: ' + str(ns_name) + ' Dashboard: ' + str( instance_name) title = title.lower() try: initialize(**options) response = api.Timeboard.get_all() dashboards = response['dashes'] for dash in dashboards: if str(dash['title']).lower() == title: return True return False except: """This except cannot be kept specific. It has to be generic one. As It is used to detect failure of initialize function and erroneous response""" return None
def main(): parser = argparse.ArgumentParser() parser.add_argument("-i", "--api_key") parser.add_argument("-p", "--app_key") parser.add_argument("-e", "--events") args = parser.parse_args() options = { 'api_key': args["api_key"], 'app_key': args["app_key"] } events_file = args["events"] initialize(**options) with open(events_file) as f: events_json = json.load(f) for e in events_json["events"]: zulip_event = zulip_objects.ZulipEvent(e) if zulip_event.is_message(): if zulip_event.message.is_stream(): stream = zulip_event.message.get_stream() metric_name = 'zulip.test.streams.' + stream + '.message_count' # api.Metric.send(metric=metric_name, points=1, host="test.example.com", tags=["environment:test"], type="counter") statsd.increment('zulip.test.streams.' + stream + '.message_count', tags=["environment:test"])
def submit_custom_metric(metric_name, value, api_key, app_key, timestamp=int(time.time()), tags=None): options = { 'api_key': api_key, 'app_key': app_key } parsed_metric_name = metric_name.replace("-", "_").replace(" ", "_") data_point = (timestamp, value) initialize(**options) try: # Submit a point with a timestamp (must be ~current) if tags: for i in range(0, len(tags)): tags[i] = tags[i].replace("-", "_").replace(" ", "_").lower() response = api.Metric.send(metric=parsed_metric_name, points=data_point, tags=tags) else: response = api.Metric.send(metric=parsed_metric_name, points=data_point) assert response['status'] == "ok" tag_string = "<NO TAGS ADDED>" if tags is None else ";".join(tags) print("Published {}: {} : {}".format(parsed_metric_name, value, tag_string)) except Exception as e: print('Failed to submit custom metric.') print(e)
def stats(cls): """ Get the threaded datadog client (singleton): `datadog.ThreadStats`. This will return a `mock.Mock` instance if the `DATADOG_ENABLED` setting is `False`. This makes it possible to run this in development without having to make any additional changes or conditional checks. :return datadog.ThreadState """ if cls._stats_instance: return cls._stats_instance # If datadog is disabled by the Django setting DATADOG_ENABLED, we use # a mock object instead of the actual datadog client. This makes it # easier to switch it out without too much additional work and should # be good enough for development. api_key = cls.settings.get(cls.KEY_DATADOG_API_KEY, None) if cls.STATS_ENABLED is False or not api_key: cls._stats_instance = mock.Mock() else: datadog.initialize(api_key=api_key) cls._stats_instance = datadog.ThreadStats() cls._stats_instance.start(roll_up_interval=cls.ROLLUP_INTERVAL, flush_interval=cls.FLUSH_INTERVAL) return cls._stats_instance
def __init__(self): # Read config and set up API client api_key, url = self._load_conf(os.path.join(os.path.dirname(__file__), "datadog_callback.yml")) datadog.initialize(api_key=api_key, api_host=url) self._playbook_name = None self._start_time = time.time()
def test_initialize_options(self): """ HTTP client and API options are set with `initialize` method. """ initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST, proxies=FAKE_PROXY, cacert=False) # Make a simple API call MyCreatable.create() _, options = self.request_mock.request.call_args # Assert `requests` parameters self.assertIn('proxies', options) self.assertEquals(options['proxies'], FAKE_PROXY) self.assertIn('verify', options) self.assertEquals(options['verify'], False) # Arm the `requests` to raise self.arm_requests_to_raise() # No exception should be raised (mute=True by default) MyCreatable.create() # Repeat with mute to False initialize(api_key=API_KEY, mute=False) self.assertRaises(ApiError, MyCreatable.create)
def main(): module = AnsibleModule( argument_spec=dict( api_key=dict(required=True, no_log=True), app_key=dict(required=True, no_log=True), title=dict(required=True), text=dict(required=True), date_happened=dict(required=False, default=None, type='int'), priority=dict( required=False, default='normal', choices=['normal', 'low'] ), host=dict(required=False, default=None), tags=dict(required=False, default=None, type='list'), alert_type=dict( required=False, default='info', choices=['error', 'warning', 'info', 'success'] ), aggregation_key=dict(required=False, default=None), validate_certs = dict(default='yes', type='bool'), ) ) # Prepare Datadog if not HAS_DATADOG: module.fail_json(msg='datadogpy required for this module') options = { 'api_key': module.params['api_key'], 'app_key': module.params['app_key'] } initialize(**options) _post_event(module)
def configure_callback(conf): """Receive configuration block""" global DD_CONFIG api_key = None tags = None tag_by_dev = False dryrun = False for node in conf.children: key = node.key.lower() val = node.values[0] log_verbose('Analyzing config %s key (value: %s)' % (key, val)) if key == 'api_key': api_key = val elif key == 'tags': tags = [tok.strip() for tok in val.split(',')] elif key == 'tag_by_device': tag_by_dev = (val.lower() == "true") or (val.lower == "yes") elif key == 'dryrun': dryrun = (val.lower() == "true") or (val.lower == "yes") elif key == 'verbose': global VERBOSE_LOGGING VERBOSE_LOGGING = (val.lower() == "true") or (val.lower == "yes") or VERBOSE_LOGGING else: collectd.warning('datadog plugin: Unknown config key: %s.' % key) continue if api_key: DD_CONFIG = {'api_key': api_key, 'tags': tags, 'by_dev': tag_by_dev, 'dryrun': dryrun} if not dryrun: initialize(api_key=DD_CONFIG['api_key'])
def lambda_handler(event, context): print "Gathering stats from cassandra at {0}".format(str(datetime.now())) key = 'default' try: key = event['key'] except: print "No monitoring key set, using: {0}".format(key) dd_options = configuration[key]['dd_options'] initialize(**dd_options) auth_details = HTTPBasicAuth(username=configuration[key]['ic_options']['user_name'], password=configuration[key]['ic_options']['api_key']) print "https://api.instaclustr.com/monitoring/v1/clusters/{0}?metrics={1}".format(configuration[key]['cluster_id'], configuration[key]['metrics_list']) response = requests.get(url="https://api.instaclustr.com/monitoring/v1/clusters/{0}?metrics={1}".format(configuration[key]['cluster_id'], configuration[key]['metrics_list']), auth=auth_details) #print "Got Response: {0}".format(response.content) if not response.ok: print "Could not fetch metrics from IC: {0} - {1}".format(response.status_code, response.content) else: metric_count = 0 metrics = json.loads(response.content) for node in metrics: send_list = [] public_ip = node["publicIp"] az = node["rack"]["name"] print "{0} : {1}".format(public_ip, az) for metric in node["payload"]: dd_metric_name = 'instaclustr.{0}'.format(metric["metric"]) if (not metric["values"]): continue mydt = datetime.strptime(metric["values"][0]["time"], myformat) time_val= int((mydt - epoch).total_seconds()) tags=["environment:{0}".format(configuration[key]['env']), "availability_zone:{0}".format(az)] if metric["metric"] in cf_specific_metrics: dd_metric_name += ".{0}".format(metric["type"]) tags.append("keyspace:{0}".format(metric["keyspace"])) tags.append("columnFamily:{0}".format(metric["columnFamily"])) if metric["metric"] != "nodeStatus": #print "{0} : {1}".format(dd_metric_name, metric["values"][0]["value"]) send_list.append({'metric' : dd_metric_name, 'points' : [(time_val,metric["values"][0]["value"])], 'host' : public_ip, 'tags' : tags}) metric_count += 1 if (send_list): print "Sending: {0}".format(send_list) dd_response = api.Metric.send(send_list) print dd_response else: print "Nothing to send for node: {0}".format(public_ip) print('{0} cassandra stats uploaded. completed at {1}'.format(metric_count, str(datetime.now())))
def __init__(self, prefix=None, **kwargs): self.tags = kwargs.pop('tags', None) if 'host' in kwargs: self.host = kwargs.pop('host') else: self.host = get_hostname() initialize(**kwargs) super(DatadogMetricsBackend, self).__init__(prefix=prefix)
def test_api_params_from_env(env_name, attr_name, env_value): """ Set env_name environment variable to env_value Assert api.attr_name = env_value """ os.environ[env_name] = env_value initialize() self.assertEquals(getattr(api, attr_name), env_value)
def __init__(self, app_key, api_key): super(DataDogExporter, self).__init__() options = { 'api_key': api_key, 'app_key': app_key } initialize(**options) self._suffix = "twindb."
def initializeDatadog(api_key, app_key): if not api_key: api_key = os.environ.get('DATADOG_API_KEY') if not app_key: app_key = os.environ.get('DATADOG_APP_KEY') if not api_key or not app_key: raise Exception("please set the datadog apiKey and appKey options (or the environment variables DATADOG_API_KEY and DATADOG_APP_KEY)") options = {'api_key': api_key, 'app_key': app_key} initialize(**options) return (api, options)
def initialize(cls): # Init method for keys options = { 'api_key': 'dc375d35***0d2afe02a', 'app_key': '365a*********e442753309950d283' } initialize(**options)
def __init__(self, api_key, app_key, flush_interval=10, namespace="aplt"): datadog.initialize(api_key=api_key, app_key=app_key) self._client = ThreadStats() self._flush_interval = flush_interval self._host = get_hostname() self._namespace = namespace
def datadogdel(): options = { 'api_key': ddapikey, 'app_key': ddappkey } initialize(**options) rdddel = api.User.delete(email) print(rdddel)
def __init__(self, datadogConfig, logger, publisherName): initialize(api_key=datadogConfig['apiKey'], app_key=datadogConfig['appKey']) self.batchSize = int(datadogConfig['batchSize']) self.config = datadogConfig self.logger = logger self.publisherType = 'Datadog' self.name = publisherName
import humanfriendly from fire.push import pushbullet from fire import exceptions import sentry_sdk from sentry_sdk import push_scope from sentry_sdk.integrations.aiohttp import AioHttpIntegration from datadog import initialize, statsd, ThreadStats with open('config.json', 'r') as cfg: config = json.load(cfg) logging.basicConfig(filename='bot.log', level=logging.INFO) sentry_sdk.init(config['sentry'], integrations=[AioHttpIntegration()]) datadogopt = {'api_key': config['datadogapi'], 'app_key': config['datadogapp']} initialize(**datadogopt) def isadmin(ctx): if str(ctx.author.id) not in config['admins']: admin = False else: admin = True return admin # async def get_pre(bot, message): # if not message.guild: # return "$" # query = 'SELECT * FROM prefixes WHERE gid = $1;' # prefixraw = await bot.db.fetch(query, message.guild.id)
def lambda_handler(event, context): print "Gathering stats from cassandra at {0}".format(str(datetime.now())) key = 'default' try: key = event['key'] except: print "No monitoring key set, using: {0}".format(key) dd_options = configuration[key]['dd_options'] initialize(**dd_options) auth_details = HTTPBasicAuth( username=configuration[key]['ic_options']['user_name'], password=configuration[key]['ic_options']['api_key']) print "https://api.instaclustr.com/monitoring/v1/clusters/{0}?metrics={1}".format( configuration[key]['cluster_id'], configuration[key]['metrics_list']) response = requests.get( url="https://api.instaclustr.com/monitoring/v1/clusters/{0}?metrics={1}" .format(configuration[key]['cluster_id'], configuration[key]['metrics_list']), auth=auth_details) #print "Got Response: {0}".format(response.content) if not response.ok: print "Could not fetch metrics from IC: {0} - {1}".format( response.status_code, response.content) else: metric_count = 0 metrics = json.loads(response.content) for node in metrics: send_list = [] public_ip = node["publicIp"] az = node["rack"]["name"] print "{0} : {1}".format(public_ip, az) for metric in node["payload"]: dd_metric_name = 'instaclustr.{0}'.format(metric["metric"]) if (not metric["values"]): continue mydt = datetime.strptime(metric["values"][0]["time"], myformat) time_val = int((mydt - epoch).total_seconds()) tags = [ "environment:{0}".format(configuration[key]['env']), "availability_zone:{0}".format(az) ] if metric["metric"] in cf_specific_metrics: dd_metric_name += ".{0}".format(metric["type"]) tags.append("keyspace:{0}".format(metric["keyspace"])) tags.append("columnFamily:{0}".format( metric["columnFamily"])) if metric["metric"] != "nodeStatus": #print "{0} : {1}".format(dd_metric_name, metric["values"][0]["value"]) send_list.append({ 'metric': dd_metric_name, 'points': [(time_val, metric["values"][0]["value"])], 'host': public_ip, 'tags': tags }) metric_count += 1 if (send_list): print "Sending: {0}".format(send_list) dd_response = api.Metric.send(send_list) print dd_response else: print "Nothing to send for node: {0}".format(public_ip) print('{0} cassandra stats uploaded. completed at {1}'.format( metric_count, str(datetime.now())))
import time from tasks.add_comment_counts import add_comment_counts from tasks.add_domains import add_domains from tasks.populate import populate from tasks.populate import update_hash_list from tasks.prune import prune_starred from utility import client_factory from utility import nb_logging rollbar.init('00b402fc0da54ed1af8687d4c4389911') logger = nb_logging.setup_logger('app') from datadog import initialize initialize(statsd_host='dd_agent') def get_config(task): db_client = client_factory.get_db_client() config = db_client.read_config() # logger.debug('Config for %s: %s', task, config) return config def periodic_update_hash_list(): logger.info('Running scheduled update hash list task') update_hash_list() logger.info('Finished scheduled update hash list task')
def __init__(self, **kwargs): if "cache_dir" in kwargs: self.cache_dir = kwargs["cache_dir"] elif "local_path" in kwargs: local_path_dir = os.path.dirname( os.path.abspath(kwargs["local_path"])) self.cache_dir = os.path.join(local_path_dir, "cache") else: raise ValueError( "cache_dir must be specified (or inferred from local_path)") util.mkdir_p(self.cache_dir) if "local_path" in kwargs: self.ctx = util.read_msgpack(kwargs["local_path"]) elif "obj" in kwargs: self.ctx = kwargs["obj"] elif "raw_obj" in kwargs: self.ctx = kwargs["raw_obj"] elif "s3_path": local_ctx_path = os.path.join(self.cache_dir, "context.msgpack") bucket, key = S3.deconstruct_s3_path(kwargs["s3_path"]) S3(bucket, client_config={}).download_file(key, local_ctx_path) self.ctx = util.read_msgpack(local_ctx_path) else: raise ValueError("invalid context args: " + kwargs) self.workload_id = kwargs.get("workload_id") self.id = self.ctx["id"] self.key = self.ctx["key"] self.metadata_root = self.ctx["metadata_root"] self.cluster_config = self.ctx["cluster_config"] self.deployment_version = self.ctx["deployment_version"] self.root = self.ctx["root"] self.status_prefix = self.ctx["status_prefix"] self.app = self.ctx["app"] self.apis = self.ctx["apis"] or {} self.api_version = self.cluster_config["api_version"] self.monitoring = None self.project_id = self.ctx["project_id"] self.project_key = self.ctx["project_key"] if "local_storage_path" in kwargs: self.storage = LocalStorage(base_dir=kwargs["local_storage_path"]) else: self.storage = S3( bucket=self.cluster_config["bucket"], region=self.cluster_config["region"], client_config={}, ) host_ip = os.environ["HOST_IP"] datadog.initialize(statsd_host=host_ip, statsd_port="8125") self.statsd = datadog.statsd if self.api_version != consts.CORTEX_VERSION: raise ValueError( "API version mismatch (Context: {}, Image: {})".format( self.api_version, consts.CORTEX_VERSION)) # This affects TensorFlow S3 access os.environ["AWS_REGION"] = self.cluster_config.get("region", "") # ID maps self.apis_id_map = ResourceMap(self.apis) if self.apis else None self.id_map = self.apis_id_map
from datadog import initialize, statsd import time options = {'statsd_host': '127.0.0.1', 'statsd_port': 8125} initialize(**options) while (1): statsd.increment('example5_metric.increment', tags=["env:dev", "dummy:123"]) statsd.decrement('example5_metric.decrement', tags=["env:dev", "dummy:123"]) time.sleep(10)
def main(): global lang_aliases parser = configargparse.ArgumentParser() parser.add( '--agent-ip-override', type=ipaddr, default=None, dest='agent_ip', help='Manually set the IP address of this agent to report to the ' 'manager.') parser.add('--agent-port', type=port_no, default=6001, help='The port number to listen on.') parser.add('--redis-addr', type=host_port_pair, default=HostPortPair(ip_address('127.0.0.1'), 6379), help='The host:port pair of the Redis (agent registry) server.') parser.add('--event-addr', type=host_port_pair, default=HostPortPair(ip_address('127.0.0.1'), 5002), help='The host:port pair of the Gateway event server.') parser.add( '--exec-timeout', type=positive_int, default=180, help='The maximum period of time allowed for kernels to run user ' 'codes.') parser.add('--idle-timeout', type=positive_int, default=600, help='The maximum period of time allowed for kernels to wait ' 'further requests.') parser.add('--max-kernels', type=positive_int, default=1, help='Set the maximum number of kernels running in parallel.') parser.add('--debug', action='store_true', default=False, help='Enable more verbose logging.') parser.add('--kernel-aliases', type=str, default=None, help='The filename for additional kernel aliases') parser.add( '--volume-root', type=Path, default=Path('/var/lib/sorna-volumes'), help='The scratch directory to store container working directories.') if datadog_available: parser.add('--datadog-api-key', env_var='DATADOG_API_KEY', type=str, default=None, help='The API key for Datadog monitoring agent.') parser.add('--datadog-app-key', env_var='DATADOG_APP_KEY', type=str, default=None, help='The application key for Datadog monitoring agent.') if raven_available: parser.add('--raven-uri', env_var='RAVEN_URI', type=str, default=None, help='The sentry.io event report URL with DSN.') args = parser.parse_args() logging.config.dictConfig({ 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'colored': { '()': 'coloredlogs.ColoredFormatter', 'format': '%(asctime)s %(levelname)s %(name)s %(message)s', 'field_styles': { 'levelname': { 'color': 'black', 'bold': True }, 'name': { 'color': 'black', 'bold': True }, 'asctime': { 'color': 'black' } }, 'level_styles': { 'info': { 'color': 'cyan' }, 'debug': { 'color': 'green' }, 'warning': { 'color': 'yellow' }, 'error': { 'color': 'red' }, 'critical': { 'color': 'red', 'bold': True } }, }, }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'level': 'DEBUG', 'formatter': 'colored', 'stream': 'ext://sys.stderr', }, 'null': { 'class': 'logging.NullHandler', }, }, 'loggers': { '': { 'handlers': ['console'], 'level': 'DEBUG' if args.debug else 'INFO', }, }, }) if args.agent_ip: args.agent_ip = str(args.agent_ip) if not args.redis_addr: args.redis_addr = ('sorna-manager.lablup', 6379) assert args.volume_root.exists() assert args.volume_root.is_dir() ddagent = DummyDatadog() sentry = DummySentry() if datadog_available and args.datadog_api_key: datadog.initialize(api_key=args.datadog_api_key, app_key=args.datadog_app_key) ddagent = datadog if raven_available and args.raven_uri: sentry = raven.Client(args.raven_uri) # Load language aliases config. lang_aliases = {lang: lang for lang in supported_langs} lang_aliases.update({ 'python': 'python3', 'python26': 'python2', 'python27': 'python2', 'python34': 'python3', 'python35': 'python3', 'python36': 'python3', 'python3-deeplearning': 'python3-tensorflow', 'tensorflow-python3': 'python3-tensorflow', 'tensorflow-python3-gpu': 'python3-tensorflow-gpu', 'caffe-python3': 'python3-caffe', 'theano-python3': 'python3-theano', 'r': 'r3', 'R': 'r3', 'Rscript': 'r3', 'php': 'php7', 'node': 'nodejs6', 'nodejs': 'nodejs6', 'js': 'nodejs6', 'javascript': 'nodejs6', 'lua': 'lua5', 'git-shell': 'git', 'shell': 'git', 'ocatve': 'octave4', 'cpp': 'cpp', 'c': 'c', 'java': 'java', 'go': 'go', 'rust': 'rust', }) if args.kernel_aliases: # for when we want to add extra with open(args.kernel_aliases, 'r') as f: for line in f: alias, target = line.strip().split() assert target in supported_langs lang_aliases[alias] = target asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() log.info(f'Sorna Agent {VERSION}') log.info(f'runtime: {utils.env_info()}') log_config = logging.getLogger('sorna.agent.config') if args.debug: log_config.debug('debug mode enabled.') def handle_signal(loop, term_ev): if term_ev.is_set(): log.warning('Forced shutdown!') sys.exit(1) else: term_ev.set() loop.stop() term_ev = asyncio.Event() loop.add_signal_handler(signal.SIGTERM, handle_signal, loop, term_ev) loop.add_signal_handler(signal.SIGINT, handle_signal, loop, term_ev) docker = None agent = None server = None hb_task = None stats_task = None timer_task = None monitor_handle_task = None monitor_fetch_task = None events = None async def initialize(): nonlocal docker, agent, server, events nonlocal hb_task, stats_task, timer_task nonlocal monitor_handle_task, monitor_fetch_task args.inst_id = await utils.get_instance_id() args.inst_type = await utils.get_instance_type() if not args.agent_ip: args.agent_ip = await utils.get_instance_ip() log.info(f'myself: {args.inst_id} ({args.inst_type}), ' f'ip: {args.agent_ip}') log.info('using gateway event server at ' f'tcp://{args.event_addr}') # Connect to the events server. event_addr = f'tcp://{args.event_addr}' try: with timeout(5.0): events = await aiozmq.rpc.connect_rpc(connect=event_addr) events.transport.setsockopt(zmq.LINGER, 50) await events.call.dispatch('instance_started', args.inst_id) except asyncio.TimeoutError: events.close() await events.wait_closed() log.critical('cannot connect to the manager.') raise SystemExit(1) # Initialize Docker docker = Docker() docker_version = await docker.version() log.info('running with Docker {0} with API {1}'.format( docker_version['Version'], docker_version['ApiVersion'])) # Start RPC server. agent_addr = 'tcp://*:{}'.format(args.agent_port) agent = AgentRPCServer(docker, args, events, ddagent, sentry, loop=loop) await agent.init() server = await aiozmq.rpc.serve_rpc(agent, bind=agent_addr) server.transport.setsockopt(zmq.LINGER, 200) log.info('serving at {0}'.format(agent_addr)) # Send the first heartbeat. hb_task = loop.create_task(heartbeat_timer(agent)) stats_task = loop.create_task(stats_timer(agent)) timer_task = loop.create_task(cleanup_timer(agent)) monitor_fetch_task = loop.create_task(agent.fetch_docker_events()) monitor_handle_task = loop.create_task(agent.monitor()) await asyncio.sleep(0.01) async def shutdown(): log.info('shutting down...') # Stop receiving further requests. server.close() await server.wait_closed() # Clean all kernels. await agent.clean_all_kernels(blocking=True) log.debug('shutdown: kernel cleanup done.') # Stop timers. hb_task.cancel() stats_task.cancel() timer_task.cancel() await hb_task await stats_task await timer_task # Stop event monitoring. monitor_fetch_task.cancel() monitor_handle_task.cancel() await monitor_fetch_task await monitor_handle_task try: await docker.events.stop() except: pass docker.session.close() log.debug('shutdown: docker cleanup done.') try: with timeout(1.0): await events.call.dispatch('instance_terminated', args.inst_id, 'destroyed') except asyncio.TimeoutError: log.warning('event dispatch timeout: instance_terminated') await asyncio.sleep(0.01) events.close() await events.wait_closed() # Finalize. await agent.shutdown() await loop.shutdown_asyncgens() try: loop.run_until_complete(initialize()) loop.run_forever() # interrupted loop.run_until_complete(shutdown()) finally: loop.close() log.info('exit.')
import boto3 from datadog import initialize, api REGIONS = [ 'us-west-2', 'us-east-1', 'eu-west-1', 'ap-southeast-1', 'ap-northeast-1' ] dd_auth = {'api_key': "$API_KEY", 'app_key': "$APP_KEY"} initialize(**dd_auth) def enumerate_reserved_instances(reserved_response): reserved_classic = {} reserved_vpc = {} for i, v in enumerate(reserved_response['ReservedInstances']): instance_type = reserved_response['ReservedInstances'][i][ 'InstanceType'] instance_count = reserved_response['ReservedInstances'][i][ 'InstanceCount'] az = reserved_response['ReservedInstances'][i]['AvailabilityZone'] description = reserved_response['ReservedInstances'][i][ 'ProductDescription'] if description == 'Linux/UNIX (Amazon VPC)': reserved_vpc[(instance_type, az)] = reserved_vpc.get( (instance_type, az), 0) + instance_count elif description == 'Linux/UNIX': reserved_classic[(instance_type, az)] = reserved_classic.get( (instance_type, az), 0) + instance_count else: exit('Not sure whats going on') return (reserved_classic, reserved_vpc)
def __init__(self, api_key, app_key): options = { 'api_key': api_key, 'app_key': app_key, } initialize(**options)
def __init__(self, key, deploy_context): # type: (DeployContext) -> None super(DataDogNotifier, self).__init__() self.deploy_context = deploy_context dd.initialize(api_key=key)
def __init__(self, aws_regions, aws_access_key, aws_secret_key, azure_client_id, azure_client_secret, azure_subscription_id, azure_tenant_id, azure_resource_group_names, azure_slow_scale_classes, kubeconfig, idle_threshold, type_idle_threshold, pod_namespace, instance_init_time, cluster_name, notifier, max_scale_in_fraction=0.1, scale_up=True, maintainance=True, datadog_api_key=None, over_provision=5, dry_run=False): if kubeconfig: # for using locally logger.debug('Using kubeconfig %s', kubeconfig) self.api = pykube.HTTPClient( pykube.KubeConfig.from_file(kubeconfig)) else: # for using on kube logger.debug('Using kube service account') self.api = pykube.HTTPClient( pykube.KubeConfig.from_service_account()) if pod_namespace is None: self.pod_namespace = pykube.all else: self.pod_namespace = pod_namespace self.max_scale_in_fraction = max_scale_in_fraction self._drained = {} self.session = None if aws_access_key and aws_secret_key: self.session = boto3.session.Session( aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key, region_name=aws_regions[0]) # provide a default region else: # let boto do the setup. allows for other methods like kube2iam self.session = boto3.session.Session(region_name=aws_regions[0]) self.autoscaling_groups = autoscaling_groups.AutoScalingGroups( session=self.session, regions=aws_regions, cluster_name=cluster_name) self.autoscaling_timeouts = autoscaling_groups.AutoScalingTimeouts( self.session) azure_regions = [] resource_groups = [] self.azure_client = None if azure_client_id: azure_credentials = ServicePrincipalCredentials( client_id=azure_client_id, secret=azure_client_secret, tenant=azure_tenant_id ) # Setup the Azure client resource_client = ResourceManagementClient(azure_credentials, azure_subscription_id) resource_client.providers.register('Microsoft.Compute') resource_client.providers.register('Microsoft.Network') resource_client.providers.register('Microsoft.Insights') region_map = {} for resource_group_name in azure_resource_group_names: resource_group = resource_client.resource_groups.get(resource_group_name) location = resource_group.location if location in region_map: logger.fatal("{} and {} are both in {}. May only have one resource group per region".format( resource_group_name, region_map[location], location )) region_map[location] = resource_group_name azure_regions.append(location) resource_groups.append(resource_group) compute_client = ComputeManagementClient(azure_credentials, azure_subscription_id) compute_client.config.retry_policy.policy = azure.AzureBoundedRetry.from_retry(compute_client.config.retry_policy.policy) monitor_client = MonitorClient(azure_credentials, azure_subscription_id) monitor_client.config.retry_policy.policy = azure.AzureBoundedRetry.from_retry(monitor_client.config.retry_policy.policy) self.azure_client = AzureWriteThroughCachedApi(AzureWrapper(compute_client, monitor_client, resource_client)) self.azure_groups = azure.AzureGroups(resource_groups, azure_slow_scale_classes, self.azure_client) # config self.azure_resource_group_names = azure_resource_group_names self.azure_regions = azure_regions self.aws_regions = aws_regions self.idle_threshold = idle_threshold self.instance_init_time = instance_init_time self.type_idle_threshold = type_idle_threshold self.over_provision = over_provision self.scale_up = scale_up self.maintainance = maintainance self.notifier = notifier if datadog_api_key: datadog.initialize(api_key=datadog_api_key) logger.info('Datadog initialized') self.stats = datadog.ThreadStats() self.stats.start() self.dry_run = dry_run
def main(): start = time.time() parser = argparse.ArgumentParser() parser.add_argument("--artifacts-dir", required=True) parser.add_argument("--sha1-signing-cert", required=True) parser.add_argument("--sha384-signing-cert", required=True) parser.add_argument("--task-definition", required=True, type=argparse.FileType('r')) parser.add_argument("--allow-staging-prefixes", action="store_true", default=strtobool( os.environ.get('FUNSIZE_ALLOW_STAGING_PREFIXES', "false")), help="Allow files from staging buckets.") parser.add_argument("--filename-template", default=DEFAULT_FILENAME_TEMPLATE) parser.add_argument("-q", "--quiet", dest="log_level", action="store_const", const=logging.WARNING, default=logging.DEBUG) parser.add_argument('--arch', type=str, required=True, choices=BCJ_OPTIONS.keys(), help='The archtecture you are building.') args = parser.parse_args() logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s") log.setLevel(args.log_level) signing_certs = { 'sha1': open(args.sha1_signing_cert, 'rb').read(), 'sha384': open(args.sha384_signing_cert, 'rb').read(), } assert (get_keysize(signing_certs['sha1']) == 2048) assert (get_keysize(signing_certs['sha384']) == 4096) # Intended for local testing. dd_api_key = os.environ.get('DATADOG_API_KEY') # Intended for Taskcluster. if not dd_api_key and os.environ.get('DATADOG_API_SECRET'): dd_api_key = get_secret( os.environ.get('DATADOG_API_SECRET')).get('key') if dd_api_key: dd_options = { 'api_key': dd_api_key, } log.info("Starting metric collection") initialize(**dd_options) ddstats.start(flush_interval=1) # For use in shell scripts. write_dogrc(dd_api_key) else: log.info("No metric collection") loop = asyncio.get_event_loop() manifest = loop.run_until_complete(async_main(args, signing_certs)) loop.close() manifest_file = os.path.join(args.artifacts_dir, "manifest.json") with open(manifest_file, "w") as fp: json.dump(manifest, fp, indent=2, sort_keys=True) log.debug("{}".format(json.dumps(manifest, indent=2, sort_keys=True))) # Warning: Assumption that one partials task will always be for one branch. metric_tags = [ "branch:{}".format(manifest[0]['branch']), "platform:{}".format(manifest[0]['platform']), ] ddstats.timing('task_duration', time.time() - start, start, tags=metric_tags) # Wait for all the metrics to flush. If the program ends before # they've been sent, they'll be dropped. # Should be more than the flush_interval for the ThreadStats object if dd_api_key: time.sleep(10)
def __init__(self, app_key, api_key): super(DataDogExporter, self).__init__() options = {"api_key": api_key, "app_key": app_key} initialize(**options) self._suffix = "twindb."
def authenticate(cred_json): initialize(**cred_json)
def setUp(self): super(DatadogAPIWithInitialization, self).setUp() initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST)
def post_dd_metrics(metrics, keys): # Post to Corpsite Datadog account options = {'api_key': keys.corpapikey, 'app_key': keys.corpappkey} initialize(**options) api.Metric.send(metrics)
def __init__(self): datadog.initialize() self.set_ndays(90) # default is 90 days self.print_configured = True self.map_aws_dd = None
#!/usr/bin/env python3 # pylint: disable=invalid-name import time import datadog from armory.hellodeploy import kv_parser datadog_options = { 'api_key': '71120e33fa59c2bf8af09b6d881344b4', 'app_key': 'hellodeploy' } datadog.initialize(**datadog_options) def send_events(env_kv): # Loop because DataDog doesn't reliably tag the events with ASG, # so the monitor doesn't have that info when it alerts, # and Barometer can't tell which canary to fail. asg = env_kv["CLOUD_SERVER_GROUP"][1:-1] tags = ["#autoscaling_group:%s" % asg] while True: for _ in range(5): datadog.dogstatsd.statsd.event('CanaryTest', 'canarytest: autoscaling_group:%s' %
async def init(self, context: Any = None) -> None: datadog.initialize(api_key=self.plugin_config['datadog_api_key'], app_key=self.plugin_config['datadog_app_key']) self.statsd = datadog.statsd self.statsd.__enter__()
import time ALOOMA_USERNAME = os.environ.get('ALOOMA_USERNAME') ALOOMA_PASSWORD = os.environ.get('ALOOMA_PASSWORD') DATADOG_API_KEY = os.environ.get('DATADOG_API_KEY') MINUTES_SLEEP = 10 SECONDS_SLEEP = MINUTES_SLEEP * 60 api = alooma.Alooma( hostname='app.alooma.com', port=443, username=ALOOMA_USERNAME, password=ALOOMA_PASSWORD, ) datadog.initialize(api_key=DATADOG_API_KEY) def posix_timestamp(): d = datetime.datetime.now() return str(int(time.mktime(d.timetuple()))) def send_metric(data): for d in data: metric_name = "alooma.{}".format(d['target'].lower()) values = d['datapoints'] for x in values: x.reverse()
def get_all_input_fields_and_add_to_data_dog( customer_name, api_key, app_key, support_mail, access_key, secret_key, slack_channels, customer_email_id, queue_consumer_count, queue_producer_count, queue_memory_pct, queue_size, alerts_list): options = {'api_key': str(api_key), 'app_key': str(app_key)} initialize(**options) options = dict(notify_no_data=False, no_data_timeframe=0) options2 = dict(notify_no_data=False, no_data_timeframe=0, renotify_interval=10) tags = 'datadog' if "activemq" in alerts_list: print json.dumps( api.Monitor.create( type="metric alert", query= "min(last_5m):avg:activemq.queue.consumer_count{*} by {host,name,region} >" + str(queue_consumer_count), name="[" + str(customer_name) + "]" + " - " + "JVM Thread count is high", message= "Team,JVM Thread count is high. Please take a look into this." + " @" + "[email protected] " + str(slack_channels) + " @" + str(customer_email_id), tags=tags, options=options2)), print json.dumps( api.Monitor.create( type="metric alert", query= "min(last_5m):avg:activemq.queue.consumer_count{*} by {host,name,region} >" + str(queue_consumer_count), name="[Ticket]" + " - " + "[" + str(customer_name) + "]" + " - " + "JVM Thread count is high", message= "Team,JVM Thread count is high. Please take a look into this. @" + str(support_mail), tags=tags, options=options)), #Producer_Count print json.dumps( api.Monitor.create( type="metric alert", query= "min(last_5m):avg:activemq.queue.producer_count{*} by {host,name,region} >" + str(queue_producer_count), name="[" + str(customer_name) + "]" + " - " + "JVM heap memory is high", message= "Team,JVM heap memory is high. Please take a look into this." + " @" + "[email protected] " + str(slack_channels) + " @" + str(customer_email_id), tags=tags, options=options2)), print json.dumps( api.Monitor.create( type="metric alert", query= "min(last_5m):avg:activemq.queue.producer_count{*} by {host,name,region} >" + str(queue_producer_count), name="[Ticket]" + " - " + "[" + str(customer_name) + "]" + " - " + "JVM heap memory is high", message= "Team,JVM heap memory is high. Please take a look into this. @" + str(support_mail), tags=tags, options=options)), #queue.memory_pct print json.dumps( api.Monitor.create( type="metric alert", query= "min(last_5m):avg:activemq.queue.memory_pct{*} by {host,name,region} >" + str(queue_memory_pct), name="[" + str(customer_name) + "]" + " - " + "JVM heap memory is high", message= "Team,JVM heap memory is high. Please take a look into this." + " @" + "[email protected] " + str(slack_channels) + " @" + str(customer_email_id), tags=tags, options=options2)), print json.dumps( api.Monitor.create( type="metric alert", query= "min(last_5m):avg:activemq.queue.memory_pct{*} by {host,name,region} >" + str(queue_memory_pct), name="[Ticket]" + " - " + "[" + str(customer_name) + "]" + " - " + "JVM heap memory is high", message= "Team,JVM heap memory is high. Please take a look into this. @" + str(support_mail), tags=tags, options=options)), #queue_size print json.dumps( api.Monitor.create( type="metric alert", query= "min(last_5m):avg:activemq.queue.size{*} by {host,name,region} >" + str(queue_size), name="[" + str(customer_name) + "]" + " - " + "JVM heap memory is high", message= "Team,JVM heap memory is high. Please take a look into this." + " @" + "[email protected] " + str(slack_channels) + " @" + str(customer_email_id), tags=tags, options=options2)), print json.dumps( api.Monitor.create( type="metric alert", query= "min(last_5m):avg:activemq.queue.size{*} by {host,name,region} >" + str(queue_size), name="[Ticket]" + " - " + "[" + str(customer_name) + "]" + " - " + "JVM heap memory is high", message= "Team,JVM heap memory is high. Please take a look into this. @" + str(support_mail), tags=tags, options=options)), return True
def main(): datadog.initialize(**CONFIG['datadog']) args.command(args)
def main(): module = AnsibleModule(argument_spec=dict( api_key=dict(required=True, no_log=True), api_host=dict(), app_key=dict(required=True, no_log=True), state=dict(required=True, choices=['present', 'absent', 'mute', 'unmute']), type=dict(choices=[ 'metric alert', 'service check', 'event alert', 'process alert', 'log alert', 'query alert', 'trace-analytics alert', 'rum alert' ]), name=dict(required=True), query=dict(), notification_message=dict(no_log=True), silenced=dict(type='dict'), notify_no_data=dict(default=False, type='bool'), no_data_timeframe=dict(), timeout_h=dict(), renotify_interval=dict(), escalation_message=dict(), notify_audit=dict(default=False, type='bool'), thresholds=dict(type='dict', default=None), tags=dict(type='list', elements='str', default=None), locked=dict(default=False, type='bool'), require_full_window=dict(type='bool'), new_host_delay=dict(), evaluation_delay=dict(), id=dict(), include_tags=dict(required=False, default=True, type='bool'), )) # Prepare Datadog if not HAS_DATADOG: module.fail_json(msg=missing_required_lib('datadogpy'), exception=DATADOG_IMP_ERR) options = { 'api_key': module.params['api_key'], 'api_host': module.params['api_host'], 'app_key': module.params['app_key'] } initialize(**options) # Check if api_key and app_key is correct or not # if not, then fail here. response = api.Monitor.get_all() if isinstance(response, dict): msg = response.get('errors', None) if msg: module.fail_json( msg= "Failed to connect Datadog server using given app_key and api_key : {0}" .format(msg[0])) if module.params['state'] == 'present': install_monitor(module) elif module.params['state'] == 'absent': delete_monitor(module) elif module.params['state'] == 'mute': mute_monitor(module) elif module.params['state'] == 'unmute': unmute_monitor(module)
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from datadog import initialize, api import json, os, sys initialize() # <- dd libs class Controller(BaseHTTPRequestHandler): def sync(self, parent, finalizing): mid = parent.get("status", {}).get("id", None) if finalizing: api.Monitor.delete(mid) return {"finalized": True} if mid: res = api.Monitor.update(mid, **parent.get("spec", {})) if res.get("errors", {}) == ["Monitor not found"]: mid = None if not mid: res = api.Monitor.create(**parent.get("spec", {})) mid = res.get("id") return {"status": {"id": mid}} def do_POST(self): observed = json.loads( self.rfile.read(int(self.headers.getheader("content-length")))) desired = self.sync(observed["parent"], observed["finalizing"])
# each may potentially hold many individual endpoints. from domains.cho import domain as cho_domain # c[e4-6]?.ppy.sh from domains.osu import domain as osu_domain # osu.ppy.sh from domains.ava import domain as ava_domain # a.ppy.sh app.add_domains({cho_domain, osu_domain, ava_domain}) # enqueue tasks to run once the server # begins, and stops serving connections. # these make sure we set everything up # and take it down nice and graceful. app.before_serving = before_serving app.after_serving = after_serving # support for https://datadoghq.com if all(glob.config.datadog.values()): datadog.initialize(**glob.config.datadog) glob.datadog = datadog.ThreadStats() glob.datadog.start(flush_in_thread=True, flush_interval=15) # wipe any previous stats from the page. glob.datadog.gauge('gulag.online_players', 0) else: glob.datadog = None # start up the server; this starts # an event loop internally, using # uvloop if it's installed. app.run(glob.config.server_addr, handle_signals=True, # SIGHUP, SIGTERM, SIGINT sigusr1_restart=True) # use SIGUSR1 for restarts
def setup_metrics() -> None: # pragma: no cover initialize(api_key=settings.DD_API_KEY, app_key=settings.DD_APP_KEY) patch_discord()
def setUp(self): initialize(api_key=API_KEY, app_key=APP_KEY, api_host=API_HOST) dog._swallow = False
import time from http.server import BaseHTTPRequestHandler, HTTPServer from urllib.parse import urlparse, parse_qs import json import calculate2 import sentry_sdk import logging import time import os import locationcache sentry_sdk.init("https://[email protected]/1339196") from datadog import initialize, statsd initialize(statsd_host=os.environ.get('DATADOG_HOST')) HOST_NAME = '0.0.0.0' PORT_NUMBER = 9002 class NearestVehicle(BaseHTTPRequestHandler): def do_HEAD(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() def do_GET(self): paths = {'/classify/location': {'status': 200}} o = urlparse(self.path) if not o.path in paths: self.respond({'status': 500})
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)
from datadog import initialize, ThreadStats import sync config = sync.app.config initialize(api_key=config['DATADOG_API_KEY']) t_stats = ThreadStats() t_stats.start(flush_in_thread=False) from integration import app as application
def handler(event, context): ### setup log function #### func_log = logger('func_log') func_log.log_entry('RDS events function begin.') print(event) alrt_mess = {'alert_source':'rds_events'} tags=['lambda:{0}'.format(os.environ['AWS_LAMBDA_FUNCTION_NAME']), 'alerttime:{0}'.format(datetime.now().strftime('%Y%m%d|%H%M%S'))] dd_metric = 'aws.lambda.alert.rds.monitor' ### get environment variables #### stage = os.environ['stage'] mod_prefix = os.environ['module_prefix'] region = os.environ['aws_region'] stage_pre = os.environ['stage_prefix'] rds_mas_id = os.environ['rds_master_instance_id'] nlb_tar = os.environ['nlb_target_group'] session = boto3.Session() s3c = session.client('s3', region_name=region) stsc = session.client('sts') acc_id = stsc.get_caller_identity()['Account'] ssmc = session.client('ssm') elbc = session.client('elbv2') rdsc = session.client('rds') dd_api_key = ssmc.get_parameter(Name='/{0}/datadog-agent-api-key'.format(stage_pre), WithDecryption=True)['Parameter']['Value'] os.environ['DD_API_KEY'] = dd_api_key dd_app_key = ssmc.get_parameter(Name='/{0}/datadog-agent-app-key'.format(stage_pre), WithDecryption=True)['Parameter']['Value'] options = {'api_key': '{0}'.format(dd_api_key), 'app_key': '{0}'.format(dd_app_key)} initialize(**options) rds_event_info = json.loads(event['Records'][0]['Sns']['Message']) rds_info_id = rds_event_info['Source ID'] print(rds_event_info) if rds_event_info['Event Message'] == 'DB instance shutdown': a_mess = 'RDS instance {0} is shutting down'.format(rds_info_id) func_log.log_entry(a_mess) alrt_mess['alert_subject'] = 'RDS shutdown {0}'.format(stage_pre) alrt_mess['alert_message'] = a_mess alrt_mess['datadog_metric'] = 'rds.shutdown' alerting.handler(alrt_mess, '') print(e) api.Event.create(title='RDS Alert', text=a_mess, alert_type='warning', aggregation_key=dd_metric, source_type_name='{0}'.format(os.environ['AWS_LAMBDA_FUNCTION_NAME']), tags=tags) return ####### checking for proper rr nlb target ips ########## ### get list of rds instances ### resp = rdsc.describe_db_instances() # print(resp) ### get master instance and read recplica ids ### try: func_log.log_entry('Checking db instance {0}'.format(rds_mas_id)) mas_resp = rdsc.describe_db_instances(DBInstanceIdentifier = rds_mas_id) # print(mas_resp) rds_mas_endpoint = mas_resp['DBInstances'][0]['Endpoint']['Address'] func_log.log_entry('Master endpoint: {0}'.format(rds_mas_endpoint)) rds_rr_ids = mas_resp['DBInstances'][0]['ReadReplicaDBInstanceIdentifiers'] func_log.log_entry('Found {0} read replicas'.format(len(rds_rr_ids))) func_log.log_entry(rds_rr_ids) ### get ip addresses ### rds_mas_ip = socket.gethostbyname(rds_mas_endpoint) func_log.log_entry('Master IP address: {0}'.format(rds_mas_ip)) rr_ips = [] for r in rds_rr_ids: resp = rdsc.describe_db_instances(DBInstanceIdentifier = r) rds_rr_endpoint = resp['DBInstances'][0]['Endpoint']['Address'] func_log.log_entry('Replica endpoint: {0}'.format(rds_rr_endpoint)) if resp['DBInstances'][0]['DBInstanceStatus'] == 'available': rr_ips.append({'Id': socket.gethostbyname(rds_rr_endpoint)}) ### nlb for targets ### func_log.log_entry('Checking NLB targets') resp = elbc.describe_target_groups(Names=[nlb_tar]) print(resp) tar_grp_arn = resp['TargetGroups'][0]['TargetGroupArn'] func_log.log_entry('Target group arn: {0}'.format(tar_grp_arn)) func_log.log_entry('Target IPs: {0}'.format(rr_ips)) resp = elbc.register_targets(TargetGroupArn = tar_grp_arn, Targets = rr_ips) print(resp) except Exception as e: a_mess = 'Error updating NLB IP targets' func_log.log_entry(a_mess) alrt_mess['alert_subject'] = 'Error updating NLB targets {0}'.format(stage_pre) alrt_mess['alert_message'] = a_mess + '\n{0}'.format(e) alrt_mess['datadog_metric'] = 'monitor' alerting.handler(alrt_mess, '') print(e) text = 'Lambda alert was triggered: {0}'.format(a_mess) api.Event.create(title='Lambda Alert', text=text, alert_type='error', aggregation_key=dd_metric, source_type_name='{0}'.format(os.environ['AWS_LAMBDA_FUNCTION_NAME']), tags=tags) func_log.log_entry('RDS events function end.')