Commit 1c1e3be0 authored by Anastasia Galkin's avatar Anastasia Galkin
Browse files

Logger on, email send, format shiny

parent 3d533086
......@@ -4,7 +4,6 @@ import time
import MySQLdb
import logging
import smtplib
import email.message
import socket
from settings import *
......@@ -18,11 +17,6 @@ logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
# 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
......@@ -50,7 +44,7 @@ for instance, wp in WORDPRESS_DATABASES.items():
if int(count[0]) > 0:
if nonews:
news = '\ninfo: ' + db_name + ' ' + wp['URL'] + '\n'
news = '\nINFO: ' + db_name + ' ' + wp['URL'] + '\n'
nonews = False
news = news + "{1}: {0}\n".format(''.join(map(str, count)), queries)
......@@ -58,25 +52,23 @@ for instance, wp in WORDPRESS_DATABASES.items():
cursor.execute(stats['query'])
for row in cursor:
news = news + ', '.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")
logger.debug(db_name + " checked")
except MySQLdb.Error as err:
news = '\nwarning: ' + db_name + err.__str__() + '\n'
news = '\nWARNING: ' + db_name + err.__str__() + '\n'
logger.warning(db_name + err.__str__())
msg_body = msg_body + news
msg.set_payload(msg_body)
try:
server = smtplib.SMTP(EMAIL_CONF['SMTP_SERVER'], EMAIL_CONF['PORT'])
server.sendmail(EMAIL_CONF['FROM'],EMAIL_CONF['TO'],msg)
server.sendmail(EMAIL_CONF['FROM'],EMAIL_CONF['TO'], msg_body)
server.close()
except (smtplib.SMTPException, KeyError, TypeError, socket.error) as err:
logger.error('SMTP server: ' + err.__str__())
......@@ -3,7 +3,7 @@ LOG_FILE = ''
EMAIL_CONF = {
'FROM': '',
'TO': '',
'TO': [''],
'SUBJECT': '[Wordpress daily]',
'SMTP_SERVER': '',
'PORT': '',
......@@ -24,8 +24,8 @@ WORDPRESS_DATABASES = {
WORDPRESS_STATS = {
'users': {
'count': "SELECT COUNT(*) 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",
'header': 'user_registered, display_name',
'query': "SELECT `user_registered`, `display_name` 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",
......
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