-
Notifications
You must be signed in to change notification settings - Fork 0
/
website_watchdog
executable file
·75 lines (62 loc) · 2.79 KB
/
website_watchdog
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
#!/usr/bin/env python
import os, sys, httplib, logging
from urlparse import urlparse
import boto.ec2
# Configure this to a rotated log file
LOG_FILE = '/var/log/httpd/website_watchdog.log'
# Configure your keys here
AWS_ACCESS_KEY = 'XXX'
AWS_SECRET_KEY = 'YYY'
# Configure the name of the custom cloudwatch metric
CW_METRIC_NAME = 'webserver-response'
# All the servers should be listed here with the elements:
# Server ID, Server Name, URL, Snippet
aSERVERS = [
( 'i-1111111', 'Production', 'https://www.yourdomain.com/page', 'some sample text in page'),
]
oBotoEc2 = boto.connect_cloudwatch(aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY)
def postCloudWatch (sServerId, sServerDesc, sStatus):
sNamespace = 'EC2: ' + sServerDesc
logging.info('Posting to EC2 CloudWatch: namespace: %s, metric: %s, instance: %s, value: %s' %
(sNamespace, CW_METRIC_NAME, sServerId, sStatus))
# put_metric_data(namespace, name, value=None, timestamp=None, unit=None, dimensions=None, statistics=None)
oBotoEc2.put_metric_data(sNamespace, CW_METRIC_NAME, value=sStatus,
timestamp=None, unit='Count', dimensions= { 'InstanceId' : [ sServerId ] } )
def getSiteContent (sUrl):
try:
oUrl = urlparse(sUrl)
if (oUrl.scheme == 'https'):
oConnect = httplib.HTTPConnection(oUrl.netloc)
else:
oConnect = httplib.HTTPSConnection(oUrl.netloc)
oConnect.request('GET', oUrl.path)
oResponse = oConnect.getresponse()
if (oResponse.status in [ 200, 302 ] ):
mReturn = oResponse.read()
else:
logging.error('URL did not return valid response: %s - status: %d' % (sUrl, oResponse.status))
mReturn = False
oConnect.close()
return mReturn
except:
logging.error('Failed getting response from URL: ' + sUrl)
oConnect.close()
return False
def validateSite (sId, sServerDesc, sUrl, sSnippet):
logging.info('Validating site: %s - %s - %s - %s' % (sId, sServerDesc, sUrl, sSnippet))
sContent = getSiteContent(sUrl)
if (sContent != False and sSnippet in sContent):
# logging.info('Site appears valid - match found in: ' + sUrl)
postCloudWatch(sId, sServerDesc, '1')
else:
if (sContent != False):
logging.error('No match found in URL: %s Match:%s' % (sUrl, sSnippet))
postCloudWatch(sId, sServerDesc, '0')
def main():
logging.basicConfig(level=logging.INFO, filename=LOG_FILE,
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
for sId, sServerDesc, sUrl, sSnippet in aSERVERS:
validateSite(sId, sServerDesc, sUrl, sSnippet)
if __name__ == '__main__':
main()