-
-
Save william-p/a5cfe29a1598f63a5263 to your computer and use it in GitHub Desktop.
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 | |
"""Check my Website to Graphite | |
Usage: | |
cmws2graphite.py <check_id>... [--graphite=<graphite>] | |
Options: | |
-h --help Show this screen. | |
<check_id> Check id | |
--graphite=<graphite> Graphite URI [default: 127.0.0.1:2003] | |
""" | |
from docopt import docopt | |
import socket | |
try: | |
from urllib.parse import urlparse | |
except: | |
# Python 2 | |
from urlparse import urlparse | |
from checkmyws import CheckmywsClient | |
def get_data_from_cmws(check_id): | |
# Get data from Check my Website | |
cmws = CheckmywsClient() | |
raw = cmws.status(check_id) | |
metrics = [] | |
# Format data | |
url = urlparse(raw["url"]) | |
name = url.netloc.replace(":", ".") | |
timestamp = raw["metas"]["lastcheck"] | |
## Httptime by location | |
for (location, value) in raw["lastvalues"]["httptime"].items(): | |
location = location.replace(":", ".").lower() | |
metric = "cmws.{0}.httptime.{1} {2} {3}\n".format( | |
name, | |
location, | |
value, | |
timestamp | |
) | |
metrics.append(metric) | |
## States by location | |
for (location, value) in raw["states"].items(): | |
location = location.replace(":", ".").lower() | |
metric = "cmws.{0}.states.{1} {2} {3}\n".format( | |
name, | |
location, | |
value, | |
timestamp | |
) | |
metrics.append(metric) | |
## Metas | |
blacklist = ('title', 'lastcheck', 'laststatechange_bin', 'laststatechange', 'code') | |
for (label, value) in raw["metas"].items(): | |
if label in blacklist: | |
continue | |
metric = "cmws.{0}.metas.{1} {2} {3}\n".format( | |
name, | |
label, | |
value, | |
timestamp | |
) | |
metrics.append(metric) | |
return metrics | |
def send_to_graphite(host, port, metrics): | |
# Send to Graphite compatible input | |
graphite = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
graphite.connect((host, port)) | |
for metric in metrics: | |
graphite.send(metric) | |
graphite.close() | |
if __name__ == '__main__': | |
arguments = docopt(__doc__) | |
graphite_uri = arguments['--graphite'] | |
graphite_host = graphite_uri.split(':')[0] | |
graphite_port = int(graphite_uri.split(':')[1]) | |
if arguments['<check_id>'] is not None: | |
check_ids = arguments['<check_id>'] | |
check_ids = set(check_ids) | |
metrics = [] | |
for check_id in check_ids: | |
metrics += get_data_from_cmws(check_id) | |
send_to_graphite( | |
graphite_host, | |
graphite_port, | |
metrics | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment