def handle(options, args): """ `YOMP export` handler. """ (server, apikey) = YOMPcli.getCommonArgs(parser, args) dump = partial(json.dumps, indent=2) if hasattr(options, "useYaml"): if options.useYaml: dump = partial(yaml.safe_dump, default_flow_style=False) YOMP = YOMPSession(server=server, apikey=apikey) if options.output is not None: outp = open(options.output, "w") else: outp = sys.stdout models = YOMP.exportModels() if models: try: print >> outp, dump(models) finally: outp.flush() if outp != sys.stdout: outp.close()
def setupYOMPAWSCredentials(publicDnsName, config): """ Using the YOMP CLI, connect to YOMP to obtain the API Key for the instance. :param publicDnsName: A reachable DNS entry for the YOMP server that needs to be configured :param config: A dict containing values for `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` :raises: infrastructure.utilities.exceptions.YOMPConfigError if it is unable to obtain the API Key :returns: The API Key of the YOMP server """ credentials = { "aws_access_key_id": config["AWS_ACCESS_KEY_ID"], "aws_secret_access_key": config["AWS_SECRET_ACCESS_KEY"] } server = "https://%s" % publicDnsName YOMP = YOMPSession(server=server) YOMP.apikey = YOMP.verifyCredentials(**credentials) if YOMP.apikey: YOMP.updateSettings(settings=credentials, section="aws") return YOMP.apikey else: raise YOMPConfigError("Unable to obtain YOMP API Key")
def handle(options, args): """ `YOMP custom` handler. """ try: resource = args.pop(0) action = args.pop(0) except IndexError: printHelpAndExit() (server, apikey) = YOMPcli.getCommonArgs(parser, args) YOMP = YOMPSession(server=server, apikey=apikey) if resource == "metrics": if action == "list": handleListRequest(YOMP, options.format) elif action == "monitor": if not options.id: printHelpAndExit() handleMonitorRequest(YOMP, options.id) elif action == "unmonitor": if not options.name: printHelpAndExit() handleUnmonitorRequest(YOMP, options.name) else: printHelpAndExit() else: printHelpAndExit()
def handle(options, args): """ `YOMP DELETE` handler. """ (endpoint, apikey) = YOMPcli.getCommonArgs(parser, args) if options.data: data = options.data else: # Pop data source off args try: data = args.pop(0) except IndexError: data = "" server = "%(scheme)s://%(netloc)s" % urlparse(endpoint)._asdict() YOMP = YOMPSession(server=server, apikey=apikey) delete = partial(YOMP.delete, endpoint) response = None if data.strip() == "-" or not data: if select.select([sys.stdin], [], [], 0.0)[0]: response = delete(data=sys.stdin) else: response = delete() elif data: with open(data, "r") as fp: response = delete(data=fp) if isinstance(response, Response): print response.text sys.exit(not int(bool(response)))
def sendDataToDatadog(datadogApiKey, YOMPServer, YOMPApiKey, numRecords, metricId): """Get data from YOMP and send to Datadog. This gets metric data for the metric matching metricId and converts it into two datasets in the Datadog format: one for the values and one for the anomaly scores. """ # Configure the Datadog library dog_http_api.api_key = datadogApiKey YOMP = YOMPSession(server=YOMPServer, apikey=YOMPApiKey) server, metricName = _getMetricServerAndName(YOMP, metricId) valuesData, anomaliesData = _getMetricData(YOMP, metricId, numRecords) # Hack to limit number of records for YOMP instances prior to version 1.3 # that don't respect the limit parameter when getting metric data. valuesData = valuesData[-numRecords:] anomaliesData = anomaliesData[-numRecords:] print "Sending %i records for metric %s on server %s" % ( len(valuesData), metricName, server) response = dog_http_api.metric(metricName + ".value", valuesData, host=server) if response["status"] != "ok": print "Datadog upload failed with response:\n\n%r" % response response = dog_http_api.metric(metricName + ".anomalyScore", anomaliesData, host=server) if response["status"] != "ok": print "Datadog upload failed with response:\n\n%r" % response
def handle(options, args): """ `YOMP GET` handler. """ (endpoint, apikey) = YOMPcli.getCommonArgs(parser, args) server = "%(scheme)s://%(netloc)s" % urlparse(endpoint)._asdict() YOMP = YOMPSession(server=server, apikey=apikey) response = YOMP.get(endpoint) if isinstance(response, Response): if hasattr(options, "useYaml"): if options.useYaml: print yaml.safe_dump(yaml.load(response.text), default_flow_style=False) else: print response.text sys.exit(not int(bool(response)))
def handle(options, args): """ `YOMP credentials` handler. Extracts credentials from command-line interface, updates YOMP server using web API. """ try: server = args.pop(0) except IndexError: parser.print_help(sys.stderr) sys.exit(1) if not options.acceptEULA: print >> sys.stderr, ( "Please read and accept the product End User License Agreement " "(EULA) before proceeding.\n" "The EULA can be found here: " "https://aws.amazon.com/marketplace/agreement?asin=B00I18SNQ6\n\n" "To accept the EULA, re-run this command with the " "--accept-eula option.") sys.exit(1) credentials = { "aws_access_key_id": options.AWS_ACCESS_KEY_ID, "aws_secret_access_key": options.AWS_SECRET_ACCESS_KEY } if options.data: if options.data.strip() == "-": updateCredentialsFromFile(sys.stdin, credentials) elif options.data: with open(options.data, "r") as fp: updateCredentialsFromFile(fp, credentials) elif options.use_boto: updateCredentialsFromBoto(credentials) if not (credentials["aws_access_key_id"] and credentials["aws_secret_access_key"]): parser.print_help(sys.stderr) sys.exit(1) usertrack = { "optin": "false" if options.optOutOfDataCollection else "true" } YOMP = YOMPSession(server=server) YOMP.apikey = YOMP.verifyCredentials(**credentials) YOMP.updateSettings(settings=credentials, section="aws") YOMP.updateSettings(settings=usertrack, section="usertrack") print YOMP.apikey
def handle(options, args): """ `YOMP metrics` handler. """ try: action = args.pop(0) except IndexError: printHelpAndExit() (server, apikey) = YOMPcli.getCommonArgs(parser, args) YOMP = YOMPSession(server=server, apikey=apikey) if action == "list": handleListRequest(YOMP, options.format, region=options.region, namespace=options.namespace, instance=options.instance) elif action == "unmonitor": if not options.id: printHelpAndExit() handleUnmonitorRequest(YOMP, options.id) else: printHelpAndExit()
def handle(options, args): """ `YOMP import` handler. """ (server, apikey) = YOMPcli.getCommonArgs(parser, args) if options.data: data = options.data else: # Pop data source off args try: data = args.pop(0) except IndexError: data = "-" YOMP = YOMPSession(server=server, apikey=apikey) if data.strip() == "-": if select.select([sys.stdin,],[],[],0.0)[0]: importMetricsFromFile(YOMP, sys.stdin, **vars(options)) else: parser.print_help() sys.exit(1) elif data: with open(data, "r") as fp: importMetricsFromFile(YOMP, fp, **vars(options))
from YOMPcli.api import YOMPSession try: from sample_credentials import (YOMP_API_KEY, YOMP_SERVER, METRIC_NAME) except (SyntaxError, ImportError): print ("\nERROR: You must update YOMP credentials in sample_credentials.py " "before you can continue.\n") import sys sys.exit(1) if __name__ == "__main__": # YOMP client YOMP = YOMPSession(server=YOMP_SERVER, apikey=YOMP_API_KEY) # Check metric created for metric in YOMP.listMetrics("custom"): if metric["name"] == METRIC_NAME: uid = metric["uid"] print 'Metric "%s" has uid: %s' % (METRIC_NAME, uid) break else: print ('"%s" metric does not exist (yet). You can create the metric by' ' sending data to YOMP. See "sample_collect_data.py" for a' " simple script that you can use to periodically sample open" " file descriptors, and report the results to the YOMP Custom" " Metrics endpoint" % METRIC_NAME) # Send model creation request to create a model connected to the metric
def handle(options, args): """ `YOMP cloudwatch` handler. """ try: resource = args.pop(0) action = args.pop(0) except IndexError: printHelpAndExit() (server, apikey) = YOMPcli.getCommonArgs(parser, args) YOMP = YOMPSession(server=server, apikey=apikey) if resource == "metrics": if action == "monitor": nativeMetric = { "datasource": "cloudwatch", "metric": options.metric, "namespace": options.namespace, "region": options.region } if hasattr(dimensions_callback, "dimensions"): nativeMetric["dimensions"] = dimensions_callback.dimensions else: printHelpAndExit() handleMetricsMonitorRequest(YOMP, nativeMetric) elif action == "unmonitor": if not (options.region and options.namespace and options.instance and options.metric): printHelpAndExit() handleMetricsUnmonitorRequest(YOMP, options.region, options.namespace, options.instance, options.metric) elif action == "list": handleMetricsListRequest(YOMP, options.format, region=options.region, namespace=options.namespace, metricName=options.metric, instance=options.instance) else: printHelpAndExit() elif resource == "instances": if action == "monitor": if not (options.region and options.namespace and options.instance): printHelpAndExit() handleInstanceMonitorRequest(YOMP, options.region, options.namespace, options.instance) elif action == "unmonitor": if not (options.region and options.namespace and options.instance): printHelpAndExit() handleInstanceUnmonitorRequest(YOMP, options.region, options.namespace, options.instance) elif action == "list": print "Not yet implemented" else: printHelpAndExit() else: printHelpAndExit()
import time from YOMPcli.api import YOMPSession try: from sample_credentials import (YOMP_API_KEY, YOMP_SERVER, METRIC_NAME) except (SyntaxError, ImportError): print ("\nERROR: You must update YOMP credentials in sample_credentials.py " "before you can continue.\n") import sys sys.exit(1) if __name__ == "__main__": # YOMP client YOMP = YOMPSession(server=YOMP_SERVER, apikey=YOMP_API_KEY) # Add custom metric data with YOMP.connect() as sock: print 'Collecting "Open file descriptors" sample...', count = subprocess.check_output("/usr/sbin/lsof | /usr/bin/wc -l", shell=True).strip() print count print 'Sending sample to YOMP Metric named "%s"' % METRIC_NAME ts = time.mktime(datetime.datetime.utcnow().timetuple()) sock.sendall("%s %s %d\n" % (METRIC_NAME, count, ts)) print "Done!"
def handle(options, args): """ `YOMP autostacks` handler. """ try: resource = args.pop(0) action = args.pop(0) except IndexError: printHelpAndExit() (server, apikey) = YOMPcli.getCommonArgs(parser, args) YOMP = YOMPSession(server=server, apikey=apikey) if resource == "stacks": if action == "list": handleListRequest(YOMP, options.format) elif action == "create": if not (options.region and options.filters): printHelpAndExit() filters = json.loads(options.filters) if options.preview: handlePreviewRequest(YOMP, options.format, options.region, filters) else: if not options.name: printHelpAndExit() handleCreateRequest(YOMP, options.name, options.region, filters) elif action == "delete": if not (options.id or (options.name and options.region)): printHelpAndExit() handleDeleteRequest(YOMP, options.id, options.name, options.region) else: printHelpAndExit() elif resource == "metrics": if not (options.id or (options.name and options.region)): printHelpAndExit() if action == "list": handleMetricsListRequest(YOMP, options.id, options.name, options.region, options.format) elif action == "add": if not (options.metricNamespace and options.metricName): printHelpAndExit() handleMetricsAddRequest(YOMP, options.id, options.name, options.region, options.metricNamespace, options.metricName) elif action == "remove": if not options.metricID: printHelpAndExit() handleMetricsRemoveRequest(YOMP, options.id, options.name, options.region, options.metricID) elif resource == "instances": if not (options.id or (options.name and options.region)): printHelpAndExit() if action == "list": handleInstancesListRequest(YOMP, options.id, options.name, options.region, options.format) else: printHelpAndExit()