|
| 1 | +"""Thios is Jenkins HTTP check remote modue.""" |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import requests |
| 5 | +from datadog import initialize |
| 6 | +from datadog import api |
| 7 | +import yaml |
| 8 | + |
| 9 | + |
| 10 | +metrics_token = os.environ['JENKINS_METRICS_YOKEN'] |
| 11 | +jenkins_uri = ("https://{0}").format(os.environ['JENKINS_HOST']) |
| 12 | +api_key = os.environ('DATADOG_API_KEY') |
| 13 | +app_key = os.environ('DATADOG_APP_KEY') |
| 14 | + |
| 15 | + |
| 16 | +options = { |
| 17 | + 'api_key': api_key, |
| 18 | + 'app_key': app_key |
| 19 | +} |
| 20 | + |
| 21 | +initialize(**options) |
| 22 | +metrics_url = ("{0}/metrics/{1}/metrics").format(jenkins_uri, metrics_token) |
| 23 | +ping_url = ("{0}/metrics/{1}/ping").format(jenkins_uri, metrics_token) |
| 24 | +healthcheck_url = ("{0}/metrics/{1}healthcheck").format(jenkins_uri, metrics_token) # noqa |
| 25 | + |
| 26 | +# Parse config file |
| 27 | +with open("config.yaml", 'r') as stream: |
| 28 | + try: |
| 29 | + config = yaml.load(stream) |
| 30 | + except yaml.YAMLError as exc: |
| 31 | + print(exc) |
| 32 | + sys.exit(1) |
| 33 | + |
| 34 | +cfg_metrics = config['metrics'] |
| 35 | + |
| 36 | +# Request metrics data from Jenkins server |
| 37 | +try: |
| 38 | + request = requests.get(metrics_url) |
| 39 | + metrics = request.json() |
| 40 | +except requests.exceptions.RequestException as e: |
| 41 | + print e |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | +# populate mentrics tags |
| 45 | +tags = [] |
| 46 | +if 'tags' in config: |
| 47 | + for tag in config['tags']: |
| 48 | + tags.append(("{0}:{1}").format(tag['name'], tag['value'])) |
| 49 | + |
| 50 | +# Send gauges |
| 51 | +if 'gauges' in cfg_metrics: |
| 52 | + if 'gauges' in metrics: |
| 53 | + for gauge in cfg_metrics['gauges']: |
| 54 | + api.Metric.send( |
| 55 | + metric=gauge, |
| 56 | + host=config['host'], |
| 57 | + points=metrics['gauges'][gauge]['value'], |
| 58 | + tags=tags, |
| 59 | + type='gauge' |
| 60 | + ) |
| 61 | + |
| 62 | +# Send meters |
| 63 | +if 'meters' in cfg_metrics: |
| 64 | + if 'meters' in metrics: |
| 65 | + for meter in cfg_metrics['meters']: |
| 66 | + api.Metric.send( |
| 67 | + metric=meter, |
| 68 | + host=config['host'], |
| 69 | + points=metrics['meters'][meter]['count'], |
| 70 | + tags=tags, |
| 71 | + type='counter' |
| 72 | + ) |
0 commit comments