Commit fb3dc1a5 authored by E-Science Admin's avatar E-Science Admin
Browse files

Date added, headers changed to MIME

parent e65002dd
#!/usr/bin/python
import time
import MySQLdb
import logging
import smtplib
import socket
import time
from email.mime.text import MIMEText
from email.utils import formatdate
from settings import *
log_name = '' + time.strftime("%Y%m%d_%H%M%S") + '_wordpress_stats.log'
logfile = open(log_name, "w")
# logger
logger = logging.getLogger('check-wp')
hdlr = logging.FileHandler(LOG_DIR + LOG_FILE)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
# email payload
msg_body = EMAIL_CONF['FROM'] + ':\n'
# Open database connection
for i, wp in WORDPRESS_DATABASES.items():
for instance, wp in WORDPRESS_DATABASES.items():
logfile.write("Wordpress: '{0}' ".format(i) + '\n')
db_name = "{0} ".format(wp['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'], port=db_port)
cursor = db.cursor()
# users, posts, pages
for queries, stats in WORDPRESS_STATS.items():
cursor.execute(stats['count'])
count = cursor.fetchone()
if int(count[0]) > 0:
db = MySQLdb.connect(wp['HOST'], wp['USER'], wp['PASSWORD'], wp['DATABASE'])
cursor = db.cursor()
if nonews:
news = '\nINFO: ' + db_name + ' ' + wp['URL'] + '\n'
nonews = False
# users, posts, pages
for j, stats in WORDPRESS_STATS.items():
news = news + "{1}: {0}\n".format(''.join(map(str, count)), queries)
cursor.execute(stats['count'])
count = cursor.fetchone()
cursor.execute(stats['query'])
for row in cursor:
news = news + ', '.join(map(str, row)) + '\n'
logger.info(db_name + queries + ' ' + ' '.join(map(str, row)))
if int(count[0]) > 0:
nonews = False
logfile.write("{0} new {1}\n".format(''.join(map(str, count)), j))
logfile.write(stats['header'] + "\n")
cursor.execute(stats['query'])
db.close()
for row in cursor:
logfile.write(' | '.join(map(str, row)) + '\n')
if nonews:
logger.debug(db_name + " checked")
if nonews:
logfile.write(' Nothing new here.')
except MySQLdb.Error as err:
news = '\nWARNING: ' + db_name + err.__str__() + '\n'
logger.warning(db_name + err.__str__())
logfile.write(' \n')
msg_body = msg_body + news
db.close()
# email
msg = MIMEText(msg_body)
msg['Subject'] = EMAIL_CONF['SUBJECT']
msg['From'] = EMAIL_CONF['FROM']
msg['To'] = ' ,'.join(EMAIL_CONF['TO'])
msg['Date'] = formatdate(localtime=True)
logfile.close()
try:
server = smtplib.SMTP(EMAIL_CONF['SMTP_SERVER'], EMAIL_CONF['PORT'])
server.sendmail(EMAIL_CONF['FROM'], EMAIL_CONF['TO'], msg.as_string())
server.close()
except (smtplib.SMTPException, KeyError, TypeError, socket.error) as err:
logger.error('SMTP server: ' + err.__str__())
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