Commit 3d533086 authored by Anastasia Galkin's avatar Anastasia Galkin
Browse files

logger complete, email body complete

parent 58607a74
......@@ -3,32 +3,42 @@
import time
import MySQLdb
import logging
import smtplib
import email.message
import socket
from settings import *
# logger
logger = logging.getLogger('check-wp')
hdlr = logging.FileHandler(LOG_DIR + LOG_FILE)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('start check')
# old logfile
log_name = LOG_DIR + time.strftime("%Y%m%d_%H%M%S") + '_wordpress_stats.log'
logfile = open(log_name, "w")
# email
msg = email.message.Message()
msg['Subject'] = EMAIL_CONF['SUBJECT']
msg['From'] = EMAIL_CONF['FROM']
msg['To'] = EMAIL_CONF['TO']
msg.add_header('Content-Type','text')
msg_body = EMAIL_CONF['FROM'] + ':\n'
# Open database connection
for instance, wp in WORDPRESS_DATABASES.items():
db_name = "{0} ".format(wp['NAME'])
logfile.write(db_name)
nonews = True
news = ''
try:
if not wp['PORT']:
db_port = 3306
else:
db_port = int(wp['PORT'])
db = MySQLdb.connect(wp['HOST'], wp['USER'], wp['PASSWORD'], wp['DATABASE'])
db = MySQLdb.connect(wp['HOST'], wp['USER'], wp['PASSWORD'], wp['DATABASE'], port=db_port)
cursor = db.cursor()
# users, posts, pages
......@@ -38,26 +48,35 @@ for instance, wp in WORDPRESS_DATABASES.items():
count = cursor.fetchone()
if int(count[0]) > 0:
nonews = False
logfile.write(' '+ wp['URL'] + '\n')
logfile.write("{0} new {1}\n".format(''.join(map(str, count)), queries))
logfile.write(stats['header'] + "\n")
cursor.execute(stats['query'])
if nonews:
news = '\ninfo: ' + db_name + ' ' + wp['URL'] + '\n'
nonews = False
news = news + "{1}: {0}\n".format(''.join(map(str, count)), queries)
logger.info(db_name + " - {1}: {0}".format(''.join(map(str, count)), queries))
cursor.execute(stats['query'])
for row in cursor:
logfile.write(', '.join(map(str, row)) + '\n')
news = news + ', '.join(map(str, row)) + '\n'
logger.info(db_name + ' ,'.join(map(str, row)))
db.close()
if nonews:
logger.info(db_name + " checked")
except MySQLdb.Error as err:
print(err)
news = '\nwarning: ' + db_name + err.__str__() + '\n'
logger.warning(db_name + err.__str__())
# logger.warning("{0} : no connection".format(wp['NAME']))
#logger.warning(''.format(err))
if nonews:
logfile.write(' checked \n')
msg_body = msg_body + news
logfile.write('\n')
msg.set_payload(msg_body)
logfile.close
try:
server = smtplib.SMTP(EMAIL_CONF['SMTP_SERVER'], EMAIL_CONF['PORT'])
server.sendmail(EMAIL_CONF['FROM'],EMAIL_CONF['TO'],msg)
server.close()
except (smtplib.SMTPException, KeyError, TypeError, socket.error) as err:
logger.error('SMTP server: ' + err.__str__())
LOG_DIR = ''
LOG_FILE = ''
EMAIL_CONF = {
'FROM': '',
'TO': '',
'SUBJECT': '[Wordpress daily]',
'SMTP_SERVER': '',
'PORT': '',
}
WORDPRESS_DATABASES = {
'default': {
......@@ -11,20 +20,21 @@ WORDPRESS_DATABASES = {
'DATABASE': '',
}
}
WORDPRESS_STATS = {
'users': {
'count': "SELECT COUNT(*) FROM `wp_users` WHERE `user_registered` >= NOW() - INTERVAL 1 DAY",
'header': 'ID | user_login | display_name | user_registered',
'query': "SELECT `ID`, `user_login`, `display_name`, `user_registered` FROM `wp_users` WHERE `user_registered` >= NOW() - INTERVAL 1 DAY",
'header': 'display_name, user_registered',
'query': "SELECT `display_name`, `user_registered` FROM `wp_users` WHERE `user_registered` >= NOW() - INTERVAL 1 DAY",
},
'posts': {
'count': "SELECT COUNT(*) FROM `wp_posts` WHERE `post_type` LIKE \'post\' AND `post_modified` >= NOW() - INTERVAL 1 DAY",
'header': 'ID | post_author | post_modified | post_title',
'query': "SELECT `ID`, `post_author`, `post_modified`, `post_title` FROM `wp_posts` WHERE `post_type` LIKE \'post\' AND `post_modified` >= NOW() - INTERVAL 1 DAY",
'header': 'post_modified, post_title',
'query': "SELECT `post_modified`, `post_title` FROM `wp_posts` WHERE `post_type` LIKE \'post\' AND `post_modified` >= NOW() - INTERVAL 1 DAY",
},
'pages': {
'count': "SELECT COUNT(*) FROM `wp_posts` WHERE `post_type` LIKE \'page\' AND `post_modified` >= NOW() - INTERVAL 1 DAY",
'header': 'ID | post_author | post_modified | post_title',
'query': 'SELECT `ID`, `post_author`, `post_modified`, `post_title` FROM `wp_posts` WHERE `post_type` LIKE \'page\' AND `post_modified` >= NOW() - INTERVAL 1 DAY',
'header': 'post_modified, post_title',
'query': 'SELECT `post_modified`, `post_title` FROM `wp_posts` WHERE `post_type` LIKE \'page\' AND `post_modified` >= NOW() - INTERVAL 1 DAY',
},
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment