diff --git a/docs/outputs.md b/docs/outputs.md index 8cc6ee9..25b125f 100644 --- a/docs/outputs.md +++ b/docs/outputs.md @@ -26,7 +26,9 @@ you'd like to use. ## Available Third-party outputs -- +- * [New Relic Insights][insights] (`insights`): An Analytics platform that allows for querying, dashboard + and alerting on the data collected byt logster. Setup details can be found here: [New Relic Insights Output](https://github.com/asomensari/logster/blob/master/logster/outputs/insights.md) + If you have an output you'd like to be included here, please open a pull request with a link to its source/GitHub repo and a brief description of its diff --git a/logster/outputs/builtin.py b/logster/outputs/builtin.py index f383f9e..94d78d5 100644 --- a/logster/outputs/builtin.py +++ b/logster/outputs/builtin.py @@ -1,9 +1,10 @@ -from logster.outputs.stdout import StdoutOutput -from logster.outputs.graphite import GraphiteOutput -from logster.outputs.ganglia import GangliaOutput -from logster.outputs.statsd import StatsdOutput from logster.outputs.cloudwatch import CloudwatchOutput +from logster.outputs.ganglia import GangliaOutput +from logster.outputs.graphite import GraphiteOutput +from logster.outputs.insights import InsightsOutput from logster.outputs.nsca import NSCAOutput +from logster.outputs.statsd import StatsdOutput +from logster.outputs.stdout import StdoutOutput builtin_output_classes = ( StdoutOutput, @@ -12,6 +13,7 @@ StatsdOutput, CloudwatchOutput, NSCAOutput, + InsightsOutput ) builtin_outputs = dict([(a.shortname, a) for a in builtin_output_classes]) diff --git a/logster/outputs/insights.md b/logster/outputs/insights.md new file mode 100644 index 0000000..4a40469 --- /dev/null +++ b/logster/outputs/insights.md @@ -0,0 +1,9 @@ +# New Relic Insights Output + +This output class reports data to New Relic Insights Platform [https://newrelic.com/insights] + +These are the configuration options: + +- '--event_type_name': Defines the eventType name that will used to report the data (default: 'Logster') +- '--insights_api_key': New Relic Insights API Insert key. Can be configured via enviroment variable 'INSIGHTS_API_KEY_ID' +- '--newrelic_account_number': New Relic Account Number. Can be configured via environment variable 'NEW_RELIC_ACCOUNT' diff --git a/logster/outputs/insights.py b/logster/outputs/insights.py new file mode 100644 index 0000000..e338fe1 --- /dev/null +++ b/logster/outputs/insights.py @@ -0,0 +1,88 @@ +from logster.logster_helper import LogsterOutput +from httplib import * + +import json +import os +import jsonpickle +import platform + + +class InsightsOutput(LogsterOutput): + shortname = 'insights' + + @classmethod + def add_options(cls, parser): + parser.add_option('--stdout-separator', action='store', default="_", dest="stdout_separator", + help='Separator between prefix/suffix and name for stdout. Default is \"%default\".') + parser.add_option('--event_type_name', action='store', default="Logster", dest="event_type_name", + help='Event Type as it will be used by Insights. Default is \"%default\".') + parser.add_option('--insights_api_key', action='store', + default=os.getenv('INSIGHTS_API_KEY_ID'), help='New Relic Insights API Insert key') + parser.add_option('--newrelic_account_number', action='store', + default=os.getenv('NEW_RELIC_ACCOUNT'), help='New Relic Account Number') + parser.add_option('--integration_type', action='store', + default='standalone', help='Define New Relic Integration Type (ohi or standalone)') + + def __init__(self, parser, options, logger): + super(InsightsOutput, self).__init__(parser, options, logger) + self.separator = options.stdout_separator + self.insights_url = 'insights-collector.newrelic.com' + + self.integration_type = options.integration_type + + if not options.newrelic_account_number or not options.insights_api_key: + parser.print_help() + parser.error( + "You must supply --insights_api_key and --newrelic_account_number or Set environment variables. INSIGHTS_API_KEY_ID for --insights_api_key, NEW_RELIC_ACCOUNT for --newrelic_account_number") + + self.eventType = options.event_type_name + self.newrelic_account_number = options.newrelic_account_number + self.insights_api_key = options.insights_api_key + self.full_url = '/v1/accounts/' + self.newrelic_account_number + '/events' + + def submit(self, metrics): + + eventData = [] + + if self.integration_type == "standalone": + + for metric in metrics: + metric.eventType = self.eventType + metric.osName = os.name + metric.platformSystem = platform.system() + metric.platformRelease = platform.release() + platform.python_version() + eventData.append(metric) + + try: + conn = HTTPSConnection(self.insights_url) + headers = { + "Content-Type": "application/json", + "X-Insert-Key": self.insights_api_key + } + + conn.request("POST", self.full_url, json.dumps([ob.__dict__ for ob in eventData.__iter__()]), headers) + + response = conn.getresponse() + + print response.status, response.reason + + except Exception as ex: + raise Exception("Can't connect ", ex) + else: + for metric in metrics: + metric.event_type = self.eventType + eventData.append(metric) + + print jsonpickle.encode(self.getohistructure(eventData)) + + def getohistructure(self, metrics): + OHIDataEvent = {} + OHIDataEvent["name"] = "com.newrelic.ohi" + OHIDataEvent["protocol_version"] = "1" + OHIDataEvent["integration_version"] = "1.0.0" + OHIDataEvent["metrics"] = {} + OHIDataEvent["metrics"] = metrics + + return OHIDataEvent + diff --git a/setup.py b/setup.py index 8f93e33..58869f0 100755 --- a/setup.py +++ b/setup.py @@ -19,7 +19,8 @@ 'logster/outputs' ], install_requires = [ - 'pygtail>=0.5.1' + 'pygtail>=0.5.1', + 'jsonpickle>=0.9.5' ], zip_safe=False, scripts=[