Last active
June 18, 2018 16:49
-
-
Save jondkelley/299f8be97de2fe95cfa5d6d295d65f88 to your computer and use it in GitHub Desktop.
newrelic varnish python plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# python 2.6.6 compatible | |
# Goes through list of customizable varnish metrics and reports to newrelic | |
# requires config in /etc/newrelic/newrelic_varnish_agent.ini | |
# sample config: | |
# [varnish] | |
# instance: ___varish_instance_name___ | |
# metrics: sess_conn,client_req,backend_fail,cache_miss,threads,threads_created,threads_failed,threads_limited,sess_drop,n_lru_nuked,esi_errors,n_expired | |
# [newrelic] | |
# license_key: ___key___ | |
# appname: ___newrelic_app_name___ | |
# sudo pip install newrelic | |
import subprocess, json, os | |
import newrelic.agent | |
from ConfigParser import SafeConfigParser | |
class VarnishMetrics(): | |
""" | |
creates an instance to call metrics from varnishstat | |
""" | |
def __init__(self, metrics=[], instance="lcache6"): | |
self.instance = instance | |
self.metrics = metrics | |
def load_metrics(self): | |
""" | |
load stats from varnishstat json output | |
returns a dict | |
""" | |
p = subprocess.Popen(["varnishstat", "-1", "-j", "-n", self.instance], stdout=subprocess.PIPE) | |
stdout, err = p.communicate() | |
return json.loads(stdout) | |
#with open('example.txt', 'r') as myfile: | |
# out = myfile.read() | |
# return json.loads(out) | |
def parse_metrics(self): | |
""" | |
takes an itemized list of desired metrics to report and returns as list of dicts | |
returns a list | |
""" | |
raw_metrics = self.load_metrics() | |
pstat = [] | |
for metricname in self.metrics: | |
jsonmetric = "MAIN." + metricname | |
metric = raw_metrics[jsonmetric]['value'] | |
pstat.append({metricname: metric}) | |
return pstat | |
def read_config(): | |
""" | |
loads the ini file | |
varnish_instance = varnish instance to access with the stats command | |
newrelic_appname = the appname to report in customo newrelic metric | |
returns dict | |
""" | |
parser = SafeConfigParser({'environment': 'development'}) | |
parser.read('/etc/newrelic/newrelic_varnish_agent.ini') | |
config = { | |
'newrelic_license_key': parser.get('newrelic', 'license_key'), | |
'newrelic_environment': parser.get('newrelic', 'environment'), | |
'newrelic_appname': parser.get('newrelic', 'appname'), | |
'varnish_instance': parser.get('varnish', 'instance'), | |
'varnish_metrics': parser.get('varnish', 'metrics').split(','), | |
} | |
os.environ["NEW_RELIC_LICENSE_KEY"] = config['newrelic_license_key'] | |
return config | |
def report_custom_metrics(label, value, application=None, appname="varnish"): | |
""" | |
reports customized newrelic metrics | |
returns none | |
""" | |
print("Reporting metric {label} value {value} appname {appname}".format(label=label, value=value, appname=appname)) | |
metric = "Custom/{appname}/{label}".format(appname=appname, label=label) | |
newrelic.agent.record_custom_metric(metric, value, application) | |
if __name__ == "__main__": | |
config = read_config() | |
newrelic.agent.initialize() | |
application = newrelic.agent.register_application(name=config['newrelic_appname']) | |
m = VarnishMetrics(instance=config['varnish_instance'], metrics=config['varnish_metrics']) | |
varnish_metrics = m.parse_metrics() | |
for metric in varnish_metrics: | |
label, value = metric.items()[0] | |
report_custom_metrics(label, value, application, config['newrelic_appname']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment