-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuptime.py
117 lines (101 loc) · 3.84 KB
/
uptime.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
import requests
import json
import db
import reporting_db as rdb
import config.config as config
green = config.LED_PINS['green']
yellow = config.LED_PINS['yellow']
red = config.LED_PINS['red']
global upSites
upSites = []
global downSites
downSites = []
# Main method - reads the list of sites in sites.txt and checks using isItUp() method
def sites():
global js
global totalSites
totalSites = 0
# gets sites from database now - dependency on up.json was removed
json_object = (rdb.getSitesForCheck())
for item in json_object:
status = isItUp(item['url'])
try:
if status == True:
upSites.append(item['url'])
dataOutput(item['id'], item['url'], item['siteName'], 'up')
else:
downSites.append(item['url'])
dataOutput(item['id'], item['url'], item['siteName'], 'down')
except:
downSites.append(item['url'])
dataOutput(item['id'], item['url'], item['siteName'], 'down')
# insert into `outages` table with a list of sites in an array and the length of the downSites arr
if len(downSites) > 0:
db.insertSites(str(downSites), str(len(downSites)))
# trigger updateDownSites() method to store data in `outages` table
updateDownSites()
# trigger checkSite() method to trigger double checking of sites that are up
checkSite()
# Does an HTTP GET on the site URLs being passed and looks for status code 200
# removed dependency on up.json
def isItUp(site):
try:
data = requests.get(site)
if data.status_code == 200:
return True
else:
return False
except:
return False
# Outputting data to database tables as well as to lights
def dataOutput(id, siteUrl, siteName, status):
if status == 'up':
# upSites.append(site.replace('\n', ''))
# send to `activity` table as site that is online
db.addActivity("up", siteName)
db.currentStatus(siteUrl, "up")
db.updateSiteStatus(id, "up")
else:
# downSites.append(site.replace('\n', ''))
# send to activity table as site that is offline
db.addActivity("down", siteName)
db.currentStatus(siteUrl, "down")
db.updateSiteStatus(id, "down")
if len(downSites) >= 3:
changeLight(red, 'high')
changeLight(yellow, 'low')
changeLight(green, 'low')
elif len(downSites) >= 1:
changeLight(yellow, 'high')
changeLight(red, 'low')
else:
changeLight(green, 'high')
changeLight(red, 'low')
changeLight(yellow, 'low')
# Checks upsites separately
# If at least one site is online, green light is on. Indicates that at least the world isn't ending
if len(upSites) >= 1:
changeLight(green, 'high')
else:
changeLight(green, 'low')
# Changes LED on/off status
def changeLight(color, status):
# will only turn on green led if it hasn't been turned off by the cron jobs at night
# red and yellow lights are not affected by those values
if (color == green and rdb.getLedActive('green') == 1) or color == red or color == yellow:
if status == "high":
db.changeLedStatus(color, 1)
else:
db.changeLedStatus(color, 0)
# Inserts data into downtimeCounts() table to either add or update the number of times a site has failed to respond
def updateDownSites():
for site in downSites:
# insert into `downsitesCount` table and increment downCount by 1 if applicable
db.insertDownSite(site)
# Check sites in upSites to against the downsiteCounts table to see if a site has come back to life
def checkSite():
for site in upSites:
# check the downsiteCount table for all sites in the upSites[] list -
# if record exists, then it'll set the downCount to 0
db.checkSite(site)
# sites()