-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonitor.py
More file actions
224 lines (181 loc) · 5.03 KB
/
Copy pathmonitor.py
File metadata and controls
224 lines (181 loc) · 5.03 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
import sys
import os
import datetime
import time
import urllib
import urllib2
import base64
import json
import traceback
import RPi.GPIO as GPIO
#General Options
Green = 8
Yellow = 9
Red = 10
Strobe = 7
# Can't figure out why something isn't working? Turn this on.
DEBUG = False
LoopSleep = 5
# Reverse logic here because a ground for the relay activates it
On = False
Off = True
#Internet monitor options
TestURLList = ["http://www.google.com","http://www.yahoo.com","http://www.twitter.com"]
TestURLTimeout = 5
MonitorSiteURLs = ["[UrlToTest]","[AnotherURLToTest]"]
MonitorSiteTimeout = 6
#Team city monitor options
TeamCityURL = "[TeamcityUrl]/httpAuth/app/rest/builds?locator=running:any"
TeamCityRunningURL = "[TeamcityUrl]/httpAuth/app/rest/builds?locator=running:true"
TeamCityUsername = ""
TeamCityPassword = ""
BuildIdExclusions = ["bt86"] # Ignore builds that always fail for that team that can't get it together
BuildIdStartsWithExclusion = "InDevelopment" # Way to prevent new builds from triggering alarm while being created
def main():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Green,GPIO.OUT)
GPIO.setup(Yellow,GPIO.OUT)
GPIO.setup(Red,GPIO.OUT)
GPIO.setup(Strobe,GPIO.OUT)
Light(Green,On)
time.sleep(1)
Light(Green,Off)
Light(Yellow,On)
time.sleep(1)
Light(Yellow,Off)
Light(Red,On)
time.sleep(1)
Light(Red,Off)
Light(Strobe,On)
time.sleep(1)
Light(Strobe,Off)
greenState = Off
yellowState = Off
redState = Off
strobeState = Off
while True:
if hasFailingBuilds():
console("Build Failure")
if redState == Off: #wasn't already off, checking to see if this is the loop that turns it on
os.system('mpg321 SadTrombone.mp3 &')
redState = On
greenState = Off
Light(Red,On)
Light(Green,Off)
else:
debug("Build OK")
redState = Off
greenState = On
Light(Green,On)
Light(Red,Off)
if hasRunningBuilds():
debug("Build Running")
yellowState = On
Light(Yellow,On)
else:
debug("No Build Running")
yellowState = Off
Light(Yellow,Off)
if connectionWorks() and applicationWorks():
debug("Connection OK")
strobeState = Off
Light(Strobe,Off)
else:
debug("Connection Failure")
if strobeState == Off: #wasn't already off, checking to see if this is the loop that turns it on
os.system('mpg321 SystemIsDown.mp3 &')
strobeState = On
Light(Strobe,On)
time.sleep(LoopSleep)
def debug(message):
if(DEBUG):
print("%s : %s" % (datetime.datetime.now(), message))
def console(message):
print("%s : %s" % (datetime.datetime.now(), message))
def applicationWorks():
for i in MonitorSiteURLs:
try:
debug(i)
urllib2.urlopen(i,timeout=MonitorSiteTimeout).close()
except:
console(i)
console(sys.exc_info()[1])
return False
#pass
return True
def connectionWorks():
#rotate the array so we aren't always trying the same outside site
global TestURLList
TestURLList = rotate(TestURLList)
for i in TestURLList:
try:
debug(i)
urllib2.urlopen(i,timeout=TestURLTimeout).close()
return True
except:
console(i)
console(sys.exc_info()[1])
pass
return False
def hasFailingBuilds():
buildData = get_builds(TeamCityURL)
latestBuilds = get_latest_builds(buildData)
failures = searchForStatus(latestBuilds, "FAILURE")
if len(failures) > 0:
console(failures)
return True
else:
return False
def hasRunningBuilds():
buildData = get_builds(TeamCityRunningURL)
latestBuilds = get_latest_builds(buildData)
if len(latestBuilds) > 0:
return True
else:
return False
def searchForStatus(lastBuildStatus, status):
matchingBuildTypes = []
for i in lastBuildStatus:
if lastBuildStatus[i] == status:
matchingBuildTypes.append(i)
return matchingBuildTypes
def get_latest_builds(jsonBuildData):
lastBuilds = {}
lastBuildStatus = {}
if int(jsonBuildData["count"]) > 0:
for i in jsonBuildData["build"]:
if i["buildTypeId"].startswith(BuildIdStartsWithExclusion):
pass
elif i["buildTypeId"] not in lastBuilds:
lastBuilds[i["buildTypeId"]] = i.get("number", -1)
lastBuildStatus[i["buildTypeId"]] = i.get("status","SUCCESS")
elif int(lastBuilds[i["buildTypeId"]]) < int(i.get("number", -1)):
lastBuilds[i["buildTypeId"]] = i.get("number", -1)
lastBuildStatus[i["buildTypeId"]] = i.get("status","SUCCESS")
for i in BuildIdExclusions:
if i in lastBuilds:
del lastBuilds[i]
del lastBuildStatus[i]
return lastBuildStatus
def get_builds(url):
username = TeamCityUsername
password = TeamCityPassword
userpass = '%s:%s' % (username, password)
request = urllib2.Request(url)
authInfo = base64.encodestring("%s:%s" % (username,password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % authInfo)
request.add_header('Accept', 'application/json')
response = urllib2.urlopen(request)
data = json.loads(response.read())
return data
def Light(pin, status):
GPIO.output(pin,status)
def rotate(l, y=1):
if len(l) == 0:
return l
y = y % len(l) # Why? this works for negative y
return l[y:] + l[:y]
if __name__=="__main__":
main()