Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Allyn Treshansky committed Apr 4, 2016
1 parent c5d49da commit fc10195
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
5 changes: 5 additions & 0 deletions check_sites.conf.TEMPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[email]
host=smtp.gmail.com
port=587
username=MY EMAIL ADDRESS
password=MY PWD (use 2-step-verification app pwd as needed)
9 changes: 9 additions & 0 deletions check_sites.json.TEMPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"name": "site name",
"url": "complete site url"
},
{ "name": "site name",
"url": "complete site url"
}
]
127 changes: 127 additions & 0 deletions check_sites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
__author__="allyn.treshansky"

"""
stand-alone script to check the status of a bunch of websites
requires a configuration file "~/check_sites.conf" which specifies who to email log to
requeres 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:], 's: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 == '-s':
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
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 #
###########

0 comments on commit fc10195

Please sign in to comment.