Last active
August 29, 2015 13:55
-
-
Save jcockhren/8722076 to your computer and use it in GitHub Desktop.
Example of an IronWorker that talks to couchDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from couchdbkit import * | |
from models import Check | |
import requests | |
import datetime | |
# We need to preload the memory with a list of sites to check. | |
server = Server('somecouchserverhere') | |
sitedb = server.get_db('yourdbname') | |
sites_view = sitedb.view('someview', key="Site") | |
sites = sites_view.all() | |
db = server.get_db('yourdbname') | |
Check.set_db(db) | |
checks = list() | |
print "Checking {0} sites".format(len(sites)) | |
for s in sites: | |
print s['value']['url'] | |
r = requests.get("{0}://{1}{2}".format(s['value']['check_type'], s['value']['url'], s['value']['path']), verify=False) | |
check_time = datetime.datetime.utcnow() | |
check = Check(url=s['value']['url'], | |
path=s['value']['path'], | |
owner_id=s['value']['owner_id'], | |
check_type=s['value']['check_type'], | |
port=s['value']['port']) | |
check.response_time = r.elapsed.microseconds | |
print "Elapsed Time: {0} ms".format(r.elapsed.microseconds/1000.0) | |
check.time = check_time | |
if s['value']['check_type'] in ['https', 'http']: | |
if r.status_code in [200, 301, 301]: | |
check.response_value = 0 | |
else: | |
check.response_value = 1 | |
checks.append(check) | |
check.bulk_save(checks) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from couchdbkit import * | |
import datetime | |
class Site(Document): | |
url = StringProperty() | |
path = StringProperty() | |
check_type = StringProperty() | |
port = IntegerProperty() | |
owner_id = StringProperty() | |
class Check(Site): | |
time = DateTimeProperty() | |
response_time = IntegerProperty() # microseconds | |
response_value = IntegerProperty() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
runtime "python" | |
exec "checks.py" | |
file "models.py" | |
pip "requests" | |
pip "couchdbkit" | |
pip "datetime" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment