-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.py
41 lines (35 loc) · 1.28 KB
/
connect.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
import psycopg2
import sys
import settings
import pymysql.cursors
import requests
import json
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)
def connect_mysql(mysql):
try:
con = pymysql.connect(host=mysql['host'],
user=mysql['user'],
password=mysql['pwd'],
db=mysql['db'],
charset='utf8mb4',
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)