Created
April 12, 2018 20:07
-
-
Save andyshinn/30c0a62f60307e7edefa4a83b8f147fd to your computer and use it in GitHub Desktop.
Google Compute Engine backend service health check to Datadog
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
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