예제 #1
0
 def send_statsd(*args, **kwargs):
     result = method(*args, **kwargs)
     bucket = GAUGE_BUCKET_PATTERN % result
     # TODO: deal with more than one performance data
     if len(result.perf_data_list):
         value = result.perf_data_list[0]['value']
         Statsd.gauge(bucket, value)
     return result
예제 #2
0
 def send_statsd(*args, **kwargs):
     result = method(*args, **kwargs)
     bucket = GAUGE_BUCKET_PATTERN % result
     # TODO: deal with more than one performance data
     if len(result.perf_data_list):
         value = result.perf_data_list[0]['value']
         Statsd.gauge(bucket, value)
     return result
예제 #3
0
'''
Created on Jun 14, 2012

@author: Yangming
'''
import os, sys
_rootpath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(_rootpath, "statsd"))
try:
    from afstatsd import Statsd, AFTransport
    Statsd.set_transport(AFTransport())
except:
    print "Statsd Library is not available, check PYTHON_PATH"
    Statsd = None

TIMER_BUCKET_PATTERN = "sys.app.timer.%(appname)s.%(name)s"
COUNTER_BUCKET_PATTERN = "sys.app.counter.%(appname)s.%(name)s"
GAUGE_BUCKET_PATTERN = "sys.app.gauge.%(appname)s.%(name)s"


def set_timer_bucket_pattern(pattern):
    global TIMER_BUCKET_PATTERN
    TIMER_BUCKET_PATTERN = pattern


def set_counter_bucket_pattern(pattern):
    global COUNTER_BUCKET_PATTERN
    COUNTER_BUCKET_PATTERN = pattern


def set_gauge_bucket_pattern(pattern):
예제 #4
0
def main():
    try:


        parser = argparse.ArgumentParser( epilog="use -h/--help to see full help", conflict_handler="resolve")
        options = get_options(parser)
        setup_logger(options)
        plugin = None

        # check if configuration file path is set
        if options.config is not None:
            LOGGER.debug("Loading configuration from file %s", options.config)
            if os.path.isfile(options.config):
                config = ConfigParser.RawConfigParser()
                config.read(options.config)

                argumentsFromFile = config.items("common")

                argumentsFromFile = parse_cfg(config, ["common", "newrelic", "appdynamics", "cloudwatch"])
                arguments = parse_arguments(argumentsFromFile)

                options = get_options(parser, config = arguments)
            else:
                parser.error("Configuration file not found")




        if options.plugin is None:
            parser.error("You must provide plug-in name")

        if options.plugin.lower() == "appdynamics":
            if not (options.url or (options.hostname and options.metricpath)):
                parser.error("You must supply either a URL or hostname and metrics path")
            if options.url and (options.hostname or options.metricpath):
                parser.error("You must supply either a URL or hostname and metrics path, not both")
            if not(options.username and options.password and options.appname):
                parser.error("You must provide username, password and application name")
            if options.url is None:
                url = AppDynamics.default_url(options.hostname, options.appname, options.metricpath)
            else:
                url = options.url
        
            plugin = AppDynamics(url=url,
                                 username=options.username,
                                 password=options.password
                    )
	    # Set plugin name to Site filterable identifier
	    options.plugin = "AppDynamics"

        elif options.plugin.lower() == "cloudwatch":
            if not options.region:
                parser.error("You must provide an Amazon Region name like us-east-1 or us-west-2")
            if not options.appname:
                parser.error("You must provide an Application Name")
            if not(options.amazon_key_id and options.amazon_secret_key):
                parser.error("You must supply your Amazon Access Key Id and Amazon Secret Key")
            if not(options.namespace and options.metric_name and options.dimension):
                parser.error("You must provide an Amazon Cloudwatch namespace and metric name and at least one dimension")

            options.statistic = "Average" if not options.statistic else options.statistic
            hostname = get_region_url(options.region)
            plugin = CloudWatch(appname=options.appname,
                                key_id=options.amazon_key_id,
                                secret_key=options.amazon_secret_key,
                                action="ListMetrics",
                                params={},
                                dimension=options.dimension,
                                metricname=options.metric_name,
                                hostname=hostname,
                                aws_namespace=options.namespace,
                                statistic=options.statistic,
                                unit=options.unit,
                                offset=options.offset)

	    # Set plugin name to Site filterable identifier
	    options.plugin = "AWSCloudWatch"
	
        elif options.plugin.lower() == "newrelic":

            if (not options.nrelic_key) and (not options.nrelic_app_id):
                parser.error("You must provide New Relic API key and application ID")

            if (not options.metricpath):
                parser.error("You must provide metric path")

            if not options.appname:
                parser.error("You must provide an Application Name")


            plugin = NewRelic(
                            key=options.nrelic_key,
                            app_id=options.nrelic_app_id,
                            metricpath=options.metricpath,
                            appname = options.appname
                        )

	    # Set plugin name to Site filterable identifier
	    options.plugin = "NewRelic"

        else:
            parser.print_help()
            exit()

        if not options.dryrun:
            from afstatsd import Statsd
            from afstatsd.afclient import AFTransport

            Statsd.set_aggregation(True)
            Statsd.set_transport(AFTransport())
        else:
            #noinspection PyPep8Naming
            Statsd = None

        plugin.poll()
        # Need to add connection checking here, pulling data on a failed
        #  connection will generate a critical - response code and friendlier
        #  output is expected
        LOGGER.debug("plugin poll done")
        data = plugin.metric_data
        if data is None:
            raise Exception("No metric data recived from plug-in")
        else:
            for (statsd_key, value) in data.get('metrics',{}).iteritems():

                if not options.dryrun:
                    if plugin.ignoreCommonAppName:
                        LOGGER.info(" *** polling metrics %s.%s %s" % (option.plugin, statsd_key, value))
                        Statsd.gauge(str("%s.%s" % (options.plugin, statsd_key)),value)
                    else:
                        LOGGER.info(" *** polling metrics %s.%s.%s %s" % (options.plugin, options.appname, statsd_key, value))
                        Statsd.gauge(str("%s.%s.%s" % (options.plugin, options.appname, statsd_key)),value)

    except Exception as e:
        LOGGER.critical('Serious Error occured: %s', e)
예제 #5
0
'''
Created on Jun 14, 2012

@author: Yangming
'''
import os, sys
_rootpath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(_rootpath, "statsd"))
try:
    from afstatsd import Statsd, AFTransport
    Statsd.set_transport(AFTransport())
except:
    print "Statsd Library is not available, check PYTHON_PATH"
    Statsd = None

TIMER_BUCKET_PATTERN = "sys.app.timer.%(appname)s.%(name)s"
COUNTER_BUCKET_PATTERN = "sys.app.counter.%(appname)s.%(name)s"
GAUGE_BUCKET_PATTERN = "sys.app.gauge.%(appname)s.%(name)s"

def set_timer_bucket_pattern(pattern):
    global TIMER_BUCKET_PATTERN
    TIMER_BUCKET_PATTERN = pattern

def set_counter_bucket_pattern(pattern):
    global COUNTER_BUCKET_PATTERN
    COUNTER_BUCKET_PATTERN = pattern

def set_gauge_bucket_pattern(pattern):
    global GAUGE_BUCKET_PATTERN
    GAUGE_BUCKET_PATTERN = pattern