Created
March 19, 2018 21:10
-
-
Save banyek/7ed6f2defdd682d5e886f6d8235ff669 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
from logentries import LogentriesHandler | |
import pymysql | |
import os | |
import logging | |
from ManageCluster import ManageAuroraCluster as mc | |
def handler(event, context): | |
hostsandentries = {} | |
mysqluser = os.environ['MYSQLUSER'] | |
mysqlpassword = os.environ['MYSQLPASSWORD'] | |
log = logging.getLogger('logentries') | |
log.setLevel(logging.INFO) | |
log.addHandler(LogentriesHandler(os.environ['LOGENTRIES'])) | |
prodclusters = mc.getClustersByTag(mc.getClusters(), "realm", os.environ['REALM']) | |
prodnodes = [] | |
for cluster in prodclusters: | |
nodes = mc.getClusterNodes(cluster) | |
for node in nodes: | |
prodnodes.append(mc.getInstanceAddress(node)) | |
lognodes = [] | |
for node in prodnodes: | |
db = pymysql.connect(node, user=mysqluser, passwd=mysqlpassword, db='mysql', connect_timeout=5) | |
cursor = db.cursor() | |
cursor.execute("SELECT COUNT(*) FROM slow_log") | |
for row in cursor.fetchone(): | |
if row > 0: | |
hostsandentries[node]=row | |
cursor.callproc('mysql.rds_rotate_slow_log') | |
cursor.execute("SELECT * FROM slow_log_backup") | |
for row in cursor.fetchall(): | |
resultstring = "\n Start time: %s \n User and host: %s\n Query Time:%s\n Lock time:%s\n Rows sent:%s\n Rows examined: %s\n Database: %s\n Last insert id: %s\n Insert id: %s\n Server id:%s\n SQL text: %s\n Thread id: %s" % (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]) | |
log.info(resultstring) | |
return hostsandentries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment