-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_sites.py
129 lines (106 loc) · 3.1 KB
/
check_sites.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
__author__="allyn.treshansky"
"""
stand-alone script to check the status of a bunch of websites
requires a configuration file "~/.config/check_sites.conf" which specifies who to email log to
requires a configuration file "check_sites.json" which defines the sites to check
note - the entries in "check_sites.json" must be completely well-formed URLs (ie: "http://google.com" as opposed to "google.com")
"""
from ConfigParser import SafeConfigParser
import os, sys, getopt, json, urllib2, smtplib
##############
# global fns #
##############
rel = lambda *x: os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
def usage():
"""
print usage instructions
:return: usage string
"""
print(u"usage: %s -f <sites file> [-v (verbose flag)]" % sys.argv[0])
#######################
# get config settings #
#######################
CONF_PATH = os.path.join(os.path.expanduser('~'), '.config', 'check_sites.conf')
parser = SafeConfigParser()
parser.read(CONF_PATH)
EMAIL_HOST = parser.get('email', 'host')
EMAIL_PORT = parser.get('email', 'port')
EMAIL_USER = parser.get('email', 'username')
EMAIL_PWD = parser.get('email', 'password')
##########################
# parse cmd-line options #
##########################
sites_file = None
verbose = False
try:
opts, args = getopt.getopt(sys.argv[1:], 'f:v')
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
for o, a in opts:
if o == '-h':
usage()
sys.exit(2)
elif o == '-f':
sites_file = rel(a)
elif o == '-v':
verbose = True
else:
usage()
sys.exit(2)
if not sites_file:
usage()
sys.exit(2)
############
# do stuff #
############
# get the sites to check...
with open(sites_file, "r") as f:
sites = json.load(f)
f.closed
# check each site...
for site in sites:
try:
request = urllib2.urlopen(site["url"])
code = request.code
# if you wanted to add more logic or fine-grained detail to the log
# here is where you would do it; using a bunch of if statments on 'code'
site.update({
"code": code,
"up": True,
"msg": "",
})
except Exception as e:
site.update({
"code": None,
"up": False,
"msg": e,
})
finally:
request.close()
# create a log...
log = "\n".join([
"'{0}' [{1}]: {2} ({3})".format(site["name"], site["url"], site["code"], site["msg"])
for site in sites if not site["up"] or verbose
])
# email the log...
if log:
mail_from = EMAIL_USER
mail_to = EMAIL_USER
mail_subject = "output from {0}".format(sys.argv[0])
mail_msg = "To: {0}\nFrom: {1}\nSubject:{2}\n\n{3}".format(
mail_from, mail_to, mail_subject, log
)
# have to actually login to smtp server & authenticate
# b/c of google security
smtpserver = smtplib.SMTP(EMAIL_HOST, EMAIL_PORT)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(mail_from, EMAIL_PWD)
smtpserver.sendmail(mail_from, [mail_to], mail_msg)
smtpserver.close()
###########
# the end #
###########