forked from skyscrapers/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_lb.py
executable file
·156 lines (129 loc) · 5.67 KB
/
check_lb.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
import boto3
import getopt
import sys
import botocore
def main(argv):
helptext = 'check_lb.py -l <loadbalancer_name>'
try:
opts, args = getopt.getopt(argv, "hl:", ["loadbalancer-name="])
except getopt.GetoptError:
print helptext
sys.exit(2)
if opts:
for opt, arg in opts:
if opt == '-h':
print helptext
sys.exit(2)
elif opt in ("-l", "--loadbalancer-name"):
lb_name = arg
else:
print helptext
sys.exit(2)
global albClient
global elbClient
session = botocore.session.get_session()
session.user_agent_name = 'check_lb'
session.set_config_variable('profile', 'nagiosro')
albClient = session.create_client('elbv2')
elbClient = session.create_client('elb')
messages = []
criticalExit = False
warningExit = False
okExit = False
lb_health = GetLbHealth(lb_name)
if alb_exist(lb_name):
for targetGroup, health in lb_health["targetGroups"].iteritems():
if health["healthyCount"] == 0 and health["instanceCount"] > 0:
messages.append('CRITICAL - Loadbalancer %s has Targetgroup %s with %s healthy targets out of %s instances' % (lb_name, targetGroup, health["healthyCount"], health["instanceCount"]))
criticalExit = True
elif health["healthyCount"] > 0 and health["unHealthyCount"] > 0:
messages.append('WARNING - Loadbalancer %s has Targetgroup %s with %s healthy targets out of %s instances' % (lb_name, targetGroup, health["healthyCount"], health["instanceCount"]))
warningExit = True
elif health["unHealthyCount"] == 0:
messages.append('OK - Loadbalancer %s has Targetgroup %s with %s healthy targets out of %s instances' % (lb_name, targetGroup, health["healthyCount"], health["instanceCount"]))
okExit = True
elif elb_exist(lb_name):
if lb_health["healthyCount"] == 0 and lb_health["instanceCount"] > 0:
messages.append('CRITICAL - Loadbalancer %s has %s healthy instances out of %s instances' % (lb_name, lb_health["healthyCount"], lb_health["instanceCount"]))
criticalExit = True
elif lb_health["unHealthyCount"] > 0:
messages.append('WARNING - Loadbalancer %s has %s healthy instances out of %s instances' % (lb_name, lb_health["healthyCount"], lb_health["instanceCount"]))
warningExit = True
elif lb_health["unHealthyCount"] == 0:
messages.append('OK - Loadbalancer %s has %s healthy instances out of %s instances' % (lb_name, lb_health["healthyCount"], lb_health["instanceCount"]))
okExit = True
else:
print "CRITICAL - elb or alb not found"
sys.exit(2)
print "\n".join(messages)
if criticalExit:
sys.exit(2)
elif warningExit:
sys.exit(1)
elif okExit:
sys.exit(0)
sys.exit(2)
def alb_exist(lb_name):
try:
response = albClient.describe_load_balancers(Names=[lb_name])
except botocore.exceptions.ClientError as e:
if 'LoadBalancerNotFound' in e.response['Error']['Code']:
return False
else:
return True
def elb_exist(lb_name):
try:
response = elbClient.describe_load_balancers(LoadBalancerNames=[lb_name])
except botocore.exceptions.ClientError as e:
if 'LoadBalancerNotFound' in e.response['Error']['Code']:
return False
else:
return True
def GetLbHealth(lb_name):
healthyCount = 0
unHealthyCount = 0
instanceCount = 0
targetGroupCount = 0
targetGroups = {}
data = {}
if alb_exist(lb_name):
alb_arn = albClient.describe_load_balancers(Names=[lb_name]).get('LoadBalancers', [])[0].get('LoadBalancerArn', [])
for target_group in albClient.describe_target_groups(LoadBalancerArn=alb_arn)["TargetGroups"]:
targetGroups[target_group["TargetGroupName"]] = {"healthyCount": 0, "unHealthyCount": 0, "instanceCount": 0}
TargetGroupArn = target_group["TargetGroupArn"]
for target_group_health in albClient.describe_target_health(TargetGroupArn=TargetGroupArn)["TargetHealthDescriptions"]:
health = target_group_health["TargetHealth"]["State"]
if health == "healthy":
healthyCount += 1
targetGroups[target_group["TargetGroupName"]]["healthyCount"] += 1
elif health == "unhealthy":
unHealthyCount += 1
targetGroups[target_group["TargetGroupName"]]["unHealthyCount"] += 1
instanceCount += 1
targetGroups[target_group["TargetGroupName"]]["instanceCount"] += 1
targetGroupCount += 1
data["healthyCount"] = healthyCount
data["unHealthyCount"] = unHealthyCount
data["instanceCount"] = instanceCount
data["lbName"] = lb_name
data["targetGroups"] = targetGroups
return data
elif elb_exist(lb_name):
elbHealth = elbClient.describe_instance_health(LoadBalancerName=lb_name).get("InstanceStates", [])
for instanceHealth in elbHealth:
health = instanceHealth["State"]
if health == "InService":
healthyCount += 1
elif health == "OutOfService":
unHealthyCount += 1
instanceCount += 1
data["healthyCount"] = healthyCount
data["unHealthyCount"] = unHealthyCount
data["instanceCount"] = instanceCount
data["lbName"] = lb_name
return data
print "CRITICAL - elb or alb not found"
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])