forked from srflaxu40/rancher-firedrill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firedrill.py
executable file
·121 lines (82 loc) · 3.79 KB
/
firedrill.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
#!/usr/local/bin/python
import requests, urllib, urllib2, sys, json, time, subprocess, re, base64, os
slack_channel = os.environ.get('SLACK_CHANNEL')
webhook_url = os.environ.get('WEBHOOK_URL')
rancher_url = str(os.environ.get('RANCHER_URL'))
rancher_access_key = os.environ.get('RANCHER_ACCESS_KEY')
rancher_secret_key = os.environ.get('RANCHER_SECRET_KEY')
alert_time = os.environ.get('ALERT_TIME')
# NOTES:
# python firedrill.py
# Return a list of containers with label
def get_containers():
containerData = None
try:
url = "https://" + rancher_url + "/v1?limit=1000"
print url
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, rancher_access_key, rancher_secret_key)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
req = urllib2.Request(url)
response = urllib2.urlopen(req)
html = response.read()
jsonObj = json.loads(html)
url = "https://" + rancher_url + "/v1/containers?limit=1000"
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, rancher_access_key, rancher_secret_key)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
req = urllib2.Request(url)
response = urllib2.urlopen(req)
html = response.read()
containerData = json.loads(html)
degradedArr = []
data = None
for container in containerData['data']:
if container['state'] == "stopped" and (container['transitioning'] != "yes") and not ('io.rancher.container.system' in container['labels']):
data = {}
host_data = get_host_data(container['links']['hosts'])
hostname = host_data['hostname']
data['hostname'] = hostname
ip_address = host_data['publicEndpoints'][0]['ipAddress']
data['ip'] = ip_address
host_id = container['hostId']
data['host-id'] = host_id
image_id = container['imageUuid']
data['image_id'] = image_id
service_name = container['labels']['io.rancher.stack_service.name']
data['service_name'] = '*' + service_name + '*'
print "CONTAINER FOUND IN DEGRADED STATE!!" + str(data)
print "WARNING SLACK!" + slack_channel
degradedArr.append(data)
if len(degradedArr) > 0:
post_slack(degradedArr, slack_channel)
except subprocess.CalledProcessError as e:
print e.output
def get_host_data( link ):
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, link, rancher_access_key, rancher_secret_key)
urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman)))
req = urllib2.Request(link)
response = urllib2.urlopen(req)
html = response.read()
hostData = json.loads(html)
return hostData['data'][0]
def post_slack( degraded_arr, slack_channel ):
#data = json.dumps(degraded_arr)
url = webhook_url
payload = {
"username": "rancher-alerts",
"text": json.dumps(degraded_arr), #message you want to send
"channel": slack_channel #Channel set above
}
req = requests.post(webhook_url, json.dumps(payload), headers={'content-type': 'application/json'}) #request to post the message
def main():
# Continually iterate and do your thing:
while 1:
print "URL: " + rancher_url
print "another iteration..."
# change this into an argument in the future to slow the pace of processing.
get_containers()
time.sleep(int(alert_time))
if __name__ == "__main__":
main()