# coding:utf-8 from re import sub import traceback import datetime import socket import base64 import os import sys import traceback sys.path.append('/var/local/mode2/') from functions.mode2.getpiwik import get_piwik from functions.mwDB import mwDB from functions.utility import ( get_ctrinfo, random_str, subprocess_run ) # アクセス推移 def getVisitsSummary(period="week", date="previous1", domain=False): try: get_data = get_piwik('VisitsSummary.get', period, date, domain) return get_data except Exception as e: raise e def send_report_mail(report_email, body, domain=False): try: ctrinfo = get_ctrinfo() print(f'd_cma: {ctrinfo["d_cma"]}') if not domain: domain = socket.gethostname() account = ctrinfo["server_admin"] else: account = 'postmaster' now = datetime.datetime.now() todaydate = now.strftime("%Y/%m/%d") mailfrom = account + '@' + domain print(mailfrom) if ctrinfo["d_cma"] == "cma": subject = f'COTOHA Meeting Assist ウェブ分析 サマリーレポート {todaydate}' else: subject = f'Bizメール&ウェブ ウェブ分析 サマリーレポート {todaydate}' print(subject) print(body) base_message = base64.b64encode( body.encode('utf-8')).decode('utf-8') subject = base64.b64encode(subject.encode('utf-8')).decode('utf-8') msg_template = f'From: <{mailfrom}>\n'\ f'To: <{report_email}>\n'\ f'Subject: =?UTF-8?B?{subject}?=\n'\ f'Content-Type: text/plain;charset="UTF-8"\n'\ f'Content-Transfer-Encoding: base64\n\n'\ f'{base_message}' tmp_file = random_str(5) msg_file = f'/var/tmp/{tmp_file}.tmpmsg' with open(msg_file, "w") as f: f.write(msg_template) cmd = f'/usr/bin/sh /usr/local/bin/send_mail.sh'\ f' {tmp_file} {report_email}' print(cmd) subprocess_run(cmd) return True except Exception as e: raise e def make_body(visits, domain=False): try: for v in visits.items(): print(f'v: {v}') target_week = v[0] if len(v[1]) >0: nb_uniq_visitors = v[1]['nb_uniq_visitors'] nb_actions = v[1]['nb_actions'] else: nb_uniq_visitors = 0 nb_actions = 0 err = False except Exception as e: err = True raise e if not domain: domain = socket.gethostname() ctrinfo = get_ctrinfo() body = '' if ctrinfo["d_cma"] == "cma": body = 'COTOHA Meeting Assist ウェブサイト分析サマリーレポートです。\n' else: body = 'Bizメール&ウェブ ウェブサイト分析サマリーレポートです。\n' body += '\n' if err == True: body += '先週のアクセスデータを正常に取得できませんでした。 サイトへのアクセスが無かった可能性があります。\n' else: body += '先週(' + target_week + u')は、\n' + str(nb_uniq_visitors) + \ u'ユニークユーザから' + str(nb_actions) + u'のアクセス(ページビュー)ありました。\n' body += '\n' body += '分析結果の詳細はコントロールパネルから ウェブサーバ > ウェブサイト分析 をご参照ください。\n' body += 'https://bizmw-login.com/' + ctrinfo["server_admin"] +'/login \n\n本メールはウェブ分析機能より自動配信されています。\n' \ '設定の変更/解除はコントロールパネルの ウェブサーバ > ウェブサイト分析 > 分析設定 からご設定ください。\n' return body def get_reportmail_setting(domain=False): # レポートメール配信設定情報取得 report_enable = False report_email = "" try: conn = mwDB(domain) sql = "SELECT setting_value FROM web_setting WHERE setting_name='report_enable'" res = conn.fetchone(sql) print(res[0]) if int(res[0]): report_enable = True sql = "SELECT setting_value FROM web_setting WHERE setting_name='report_email'" res = conn.fetchone(sql) print(res[0]) if res[0] != "": report_email = res[0] except Exception as e: # print e raise e return report_enable, report_email if __name__ == "__main__": debug = False try: # レポート配信 タイミング判定 d = datetime.datetime.now() week = d.weekday() if week == 0 or debug == True: # 月曜日に送信 settings_list = [] target_path = f'/var/tmp/' for x in os.listdir(target_path): if x.startswith('mode2_webanalysis_settings'): if x == 'mode2_webanalysis_settings': settings_list.append(False) else: domain = x.replace("mode2_webanalysis_settings_", "") settings_list.append(domain) for domain in settings_list: try: report_enable, report_email = get_reportmail_setting(domain) if report_enable and report_email != "": # レポート送信有効 visits = getVisitsSummary("week", "previous1", domain) body = make_body(visits, domain) send_report_mail(report_email, body, domain) except Exception as e: raise e except Exception as e: print(traceback(e))