Skip to content

Instantly share code, notes, and snippets.

@andyshinn
Created April 12, 2018 20:07
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 andyshinn/30c0a62f60307e7edefa4a83b8f147fd to your computer and use it in GitHub Desktop.
Save andyshinn/30c0a62f60307e7edefa4a83b8f147fd to your computer and use it in GitHub Desktop.
Google Compute Engine backend service health check to Datadog
from datadog import statsd
import googleapiclient.discovery
PROJECT = 'myproj-153506'
SERVICES = [
'internal',
'www',
'backend'
]
METRIC = 'gcp.gce.backend.health'
compute = googleapiclient.discovery.build('compute', 'v1')
for service in SERVICES:
backend_service = compute.backendServices().get(backendService=service,project=PROJECT).execute()
for backend in backend_service.get('backends'):
group = backend.get('group')
health = compute.backendServices().getHealth(backendService=service,project=PROJECT,body={'group':group}).execute()
for instance in health.get('healthStatus'):
tags = [
"instance:%s" % instance.get('instance'),
"ip:%s" % instance.get('ipAddress'),
"port:%s" % instance.get('port'),
"state:%s" % instance.get('healthState'),
"group:%s" % group,
"service:%s" % service
]
if instance.get('healthState') == 'HEALTHY':
statsd.service_check(METRIC, statsd.OK, tags=tags)
else:
statsd.service_check(METRIC, stats.CRITICAL, tags=tags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment