Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ve
venv
settings.py
__pycache__
*.pyc
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
This Python 3.x script tries to connect to databases and sends a Slack alert with error text if it fails.
This Python 3.6 script tries to connect to databases and sends a Slack alert with error text if it fails. Note: Python3.6 is compatible with AWS Lambda deployment. If you're not deploying to AWS Lambda, any other verson of Python3 will do.

## Setup

* Create a Python 3.x virtualenv: `virtualenv -p /path/to/python3 venv`
* Create a Python 3.6 virtualenv: `virtualenv -p /path/to/python3.6 venv`
* Activate the virtualenv: `source venv/bin/activate`
* Install requirements: `pip install -r requirements.txt`
* Create a settings file and fill it out: `cp settings.py.example settings.py`
* Create a settings file and fill it out: `cp settings.py.example settings.py`.
* If you want to deploy the script to lambda, set `lambda_deploy = True` and `pip install -r requirements-lambda.txt`
* Else, leave `lambda_deploy = False` and `pip install -r requirements-nolambda.txt`
* Run script with `python check_dbs.py`

## Deploy to AWS Lambda with zappa

* in settings.py, set `lambda_deploy = True`
* copy zappa_settings.json.example to zappa_settings.json and fill it out
* set up your AWS creds that allow you to create s3 buckets, and add your target s3 bucket to zappa_settings.json
* `zappa deploy production` or whatever the name of your stage is

## TODO:

* Add support for email and SMS alerts.
2 changes: 1 addition & 1 deletion check_dbs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from connect import connect_psql, connect_mysql, post_to_slack
from connect import connect_psql, connect_mysql
import settings

def main():
Expand Down
54 changes: 32 additions & 22 deletions connect.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import psycopg2
import sys
import settings
import pymysql.cursors
import requests
import json
if settings.lambda_deploy:
import pg8000
else:
import psycopg2

def slack_alert(name, slack_webhook_url = settings.slack_webhook_url):
message = "db-status {} connection error: {}".format(name,sys.exc_info())
text = json.dumps({ 'text' : message })
print(text)
if settings.slack_alerts:
r = requests.post(slack_webhook_url, data = text)

def connect_psql(psql):
try:
con = psycopg2.connect(dbname = psql['db'],
host = psql['host'],
port = psql['port'],
user = psql['user'],
password = psql['pwd'])
con.close()
except:
message = "{} connection error: {}".format(psql['name'],sys.exc_info())
if settings.slack_alerts:
post_to_slack(message)

if settings.lambda_deploy:
try:
con = pg8000.connect(database = psql['db'],
host = psql['host'],
port = psql['port'],
user = psql['user'],
password = psql['pwd'])
con.close()
except:
slack_alert(psql['name'])

else:
try:
con = psycopg2.connect(dbname = psql['db'],
host = psql['host'],
port = psql['port'],
user = psql['user'],
password = psql['pwd'])
con.close()
except:
slack_alert(psql['name'])

def connect_mysql(mysql):
try:
Expand All @@ -30,12 +48,4 @@ def connect_mysql(mysql):
cursorclass=pymysql.cursors.DictCursor)
con.close()
except:
message = "{} connection error: {}".format(mysql['name'],sys.exc_info())
if settings.slack_alerts:
post_to_slack(message)


def post_to_slack(message, slack_webhook_url=settings.slack_webhook_url):
text = json.dumps({ 'text' : message })
print(text)
r = requests.post(slack_webhook_url, data = text)
slack_alert(mysql['name'])
3 changes: 3 additions & 0 deletions requirements-lambda.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r requirements.txt
zappa==0.44.3
pg8000==1.11.0
2 changes: 2 additions & 0 deletions requirements-nolambda.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-r requirements.txt
psycopg2==2.7.1
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
psycopg2==2.7.1
PyMySQL==0.7.11
3 changes: 2 additions & 1 deletion settings.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ databases = {
}
}

slack_alerts = False
lambda_deploy = False
slack_alerts = True
slack_webhook_url = 'set this up at https://yourslackteam.slack.com/apps/search?q=webhooks'
15 changes: 15 additions & 0 deletions zappa_settings.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"production": {
"app_function": "check_dbs.main",
"aws_region": "us-west-1",
"profile_name": "default",
"project_name": "db-status",
"runtime": "python3.6",
"s3_bucket": "",
"keep_warm": false,
"events": [{
"function": "check_dbs.main", // The function to execute
"expression": "rate(15 minutes)" // When to execute it (in cron or rate format)
}],
}
}