-
Notifications
You must be signed in to change notification settings - Fork 16
/
cloudwatch.elb.py
62 lines (51 loc) · 2.33 KB
/
cloudwatch.elb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
"""
http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-cloudwatch-metrics.html
"""
import datetime
import sys
from boto.ec2 import cloudwatch
AWS_REGION = ''
AWS_KEY = ''
AWS_SECRET = ''
ELB_NAME = 'tester'
PERIOD = 60
MINUTES = 1 # minutes of data to retrieve
### Real code
metrics = {"HealthyHostCount": {"stat": "Average","type": "int", "value":None, "uom": ""},
"UnHealthyHostCount": {"stat": "Average", "type": "int", "value":None, "uom": ""},
"RequestCount": {"stat": "Sum", "type": "int", "value":None, "uom": ""},
"Latency": {"stat": "Average", "type": "int", "value":None, "uom": ""},
"HTTPCode_ELB_4XX": {"stat": "Sum", "type": "int", "value":None, "uom": ""},
"HTTPCode_ELB_5XX": {"stat": "Sum", "type": "int", "value":None, "uom": ""},
"HTTPCode_Backend_2XX": {"stat": "Sum", "type": "int", "value":None, "uom": ""},
"HTTPCode_Backend_3XX": {"stat": "Sum", "type": "int", "value":None, "uom": ""},
"HTTPCode_Backend_4XX": {"stat": "Sum", "type": "int", "value":None, "uom": ""},
"HTTPCode_Backend_5XX": {"stat": "Sum", "type": "int", "value":None, "uom": ""},
"BackendConnectionErrors": {"stat": "Sum", "type": "int", "value":None, "uom": ""},
"SurgeQueueLength": {"stat": "Maximum", "type": "int", "value":None, "uom": ""},
"SpilloverCount": {"stat": "Sum", "type": "int", "value":None, "uom": ""}
}
# Build the exit message`
message = "OK | "
conn = cloudwatch.connect_to_region(AWS_REGION, aws_access_key_id=AWS_KEY,
aws_secret_access_key=AWS_SECRET)
end = datetime.datetime.utcnow()
start = end - datetime.timedelta(minutes=MINUTES)
for k,vh in metrics.items():
try:
# print k
res = conn.get_metric_statistics(PERIOD, start, end, k, "AWS/ELB", vh['stat'],
dimensions={"LoadBalancerName": ELB_NAME})
except Exception, e:
print "WARN - status err Error running elb stats: %s" % e.message
sys.exit(1)
# deal with the metrics returned
# print res
for stat in res:
message += "%s=%s%s;;;; " % (k.lower(), stat[vh['stat']], vh['uom'])
if message == "OK | ":
print "CRITICAL - no ELB metrics received"
sys.exit(2)
else:
print message