Created
February 3, 2016 22:16
-
-
Save mirajavora/af8a2298542b8a23d694 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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