Skip to content

Instantly share code, notes, and snippets.

@mirajavora
Created February 3, 2016 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirajavora/af8a2298542b8a23d694 to your computer and use it in GitHub Desktop.
Save mirajavora/af8a2298542b8a23d694 to your computer and use it in GitHub Desktop.
import urllib2
import diamond.collector
AVAILABLE_METRIC = 'available'
class HealthCheckCollector(diamond.collector.Collector):
"""
Publish 0 or 1 depending on status code of healthcheck endpoint.
Lifted from https://github.com/BrightcoveOS/Diamond/tree/master/src/collectors/websitemonitor.
"""
def collect(self):
req = urllib2.Request(self.config['url'])
try:
resp = urllib2.urlopen(req)
available = 1 if resp.code == 200 else 0
# Publish metrics 20X code
self.publish_gauge(AVAILABLE_METRIC, available)
# urllib2 will puke on non HTTP 200/OK URLs
except urllib2.URLError as e:
# Publish metrics non 200 code
self.publish_gauge(AVAILABLE_METRIC, 0)
except IOError, e:
self.log.error('Unable to open %s' % (self.config['url']))
except Exception, e:
self.log.error("Unknown error opening url: %s", e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment