Guest User

Untitled

a guest
Jun 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # python 2.6.6 compatible
  3. # Goes through list of customizable varnish metrics and reports to newrelic
  4. # requires config in /etc/newrelic/newrelic_varnish_agent.ini
  5. # sample config:
  6. # [varnish]
  7. # instance: ___varish_instance_name___
  8. # 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
  9. # [newrelic]
  10. # license_key: ___key___
  11. # appname: ___newrelic_app_name___
  12. # sudo pip install newrelic
  13.  
  14. import subprocess, json, os
  15. import newrelic.agent
  16. from ConfigParser import SafeConfigParser
  17.  
  18. class VarnishMetrics():
  19. """
  20. creates an instance to call metrics from varnishstat
  21. """
  22. def __init__(self, metrics=[], instance="lcache6"):
  23. self.instance = instance
  24. self.metrics = metrics
  25. def load_metrics(self):
  26. """
  27. load stats from varnishstat json output
  28. returns a dict
  29. """
  30. p = subprocess.Popen(["varnishstat", "-1", "-j", "-n", self.instance], stdout=subprocess.PIPE)
  31. stdout, err = p.communicate()
  32. return json.loads(stdout)
  33. #with open('example.txt', 'r') as myfile:
  34. # out = myfile.read()
  35. # return json.loads(out)
  36.  
  37. def parse_metrics(self):
  38. """
  39. takes an itemized list of desired metrics to report and returns as list of dicts
  40. returns a list
  41. """
  42. raw_metrics = self.load_metrics()
  43. pstat = []
  44. for metricname in self.metrics:
  45. jsonmetric = "MAIN." + metricname
  46. metric = raw_metrics[jsonmetric]['value']
  47. pstat.append({metricname: metric})
  48. return pstat
  49.  
  50. def read_config():
  51. """
  52. loads the ini file
  53. varnish_instance = varnish instance to access with the stats command
  54. newrelic_appname = the appname to report in customo newrelic metric
  55. returns dict
  56. """
  57. parser = SafeConfigParser({'environment': 'development'})
  58. parser.read('/etc/newrelic/newrelic_varnish_agent.ini')
  59. config = {
  60. 'newrelic_license_key': parser.get('newrelic', 'license_key'),
  61. 'newrelic_environment': parser.get('newrelic', 'environment'),
  62. 'newrelic_appname': parser.get('newrelic', 'appname'),
  63. 'varnish_instance': parser.get('varnish', 'instance'),
  64. 'varnish_metrics': parser.get('varnish', 'metrics').split(','),
  65. }
  66. os.environ["NEW_RELIC_LICENSE_KEY"] = config['newrelic_license_key']
  67. return config
  68.  
  69. def report_custom_metrics(label, value, application=None, appname="varnish"):
  70. """
  71. reports customized newrelic metrics
  72. returns none
  73. """
  74. print("Reporting metric {label} value {value} appname {appname}".format(label=label, value=value, appname=appname))
  75. metric = "Custom/{appname}/{label}".format(appname=appname, label=label)
  76. newrelic.agent.record_custom_metric(metric, value, application)
  77.  
  78. if __name__ == "__main__":
  79. config = read_config()
  80.  
  81. newrelic.agent.initialize()
  82. application = newrelic.agent.register_application(name=config['newrelic_appname'])
  83. m = VarnishMetrics(instance=config['varnish_instance'], metrics=config['varnish_metrics'])
  84. varnish_metrics = m.parse_metrics()
  85. for metric in varnish_metrics:
  86. label, value = metric.items()[0]
  87. report_custom_metrics(label, value, application, config['newrelic_appname'])
Add Comment
Please, Sign In to add comment