112 lines
3.8 KiB
Python
112 lines
3.8 KiB
Python
import mysql.connector
|
|
from datetime import date, datetime
|
|
from datetime import timedelta
|
|
|
|
import smtplib
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
|
|
multiple_lines = ""
|
|
dbhost = '192.168.x'
|
|
port = '3306'
|
|
username = 'xxx'
|
|
password = 'xxx'
|
|
database = 'xxx'
|
|
|
|
|
|
def add_line_email(line):
|
|
global multiple_lines
|
|
multiple_lines += line + "\n"
|
|
|
|
|
|
def send_email(subject, body, recipients):
|
|
# Email configuration
|
|
sender_email = ''
|
|
smtp_server = 'smtp.eu.mailgun.org'
|
|
smtp_port = 587
|
|
|
|
# Your email login credentials
|
|
email_username = 'xxx'
|
|
email_password = 'xxx'
|
|
|
|
# Create a multipart message and set headers
|
|
message = MIMEMultipart()
|
|
message['From'] = sender_email
|
|
message['To'] = ', '.join(recipients)
|
|
message['Subject'] = subject
|
|
|
|
# Add body to email
|
|
message.attach(MIMEText(body, 'plain'))
|
|
|
|
# Connect to SMTP server and send email
|
|
with smtplib.SMTP(smtp_server, smtp_port) as server:
|
|
server.starttls() # Secure the connection
|
|
server.login(email_username, email_password)
|
|
server.send_message(message)
|
|
|
|
|
|
d = date.today()
|
|
first_last_month = (d - timedelta(days=d.day)).replace(day=1)
|
|
end_last_month = (d - timedelta(days=d.day))
|
|
first_last_month_str = first_last_month.strftime("%Y-%m-%d")
|
|
end_of_last_month_str = end_last_month.strftime("%Y-%m-%d")
|
|
first_last_month_str = datetime.strptime(first_last_month_str, "%Y-%m-%d")
|
|
end_of_last_month_str = datetime.strptime(end_of_last_month_str, "%Y-%m-%d")
|
|
first_timestamp = int(first_last_month_str.timestamp())
|
|
last_timestamp = int(end_of_last_month_str.timestamp())
|
|
|
|
try:
|
|
connection = mysql.connector.connect(
|
|
host=dbhost,
|
|
port=port,
|
|
user=username,
|
|
password=password,
|
|
database=database
|
|
)
|
|
if connection.is_connected():
|
|
print('Connected to database, starting report generation')
|
|
|
|
cursor = connection.cursor()
|
|
|
|
msgidend = '0000000000'
|
|
startdate = (str(first_timestamp) + msgidend)
|
|
enddate = (str(last_timestamp) + msgidend)
|
|
print('Start messageid ID: ' + startdate + ' ' + str(first_last_month))
|
|
add_line_email('Start messageid ID: ' + startdate + ' ' + str(first_last_month))
|
|
print('End messageid ID: ' + enddate + ' ' + str(end_last_month))
|
|
add_line_email('End messageid ID: ' + enddate + ' ' + str(end_last_month))
|
|
prefix52 = '52%'
|
|
prefix55 = '55%'
|
|
query_all = "SELECT COUNT(*) FROM smpp_server.smpp_log WHERE message_id > %s AND message_id < %s"
|
|
query_prefix = "SELECT COUNT(*) FROM smpp_server.smpp_log WHERE message_id > %s AND message_id < %s " \
|
|
"AND destination_address LIKE %s"
|
|
|
|
cursor.execute(query_all, (startdate, enddate))
|
|
rows = cursor.fetchall()
|
|
result = rows
|
|
print('Count total: ' + str(result[0][0]))
|
|
add_line_email('Count total: ' + str(result[0][0]))
|
|
|
|
cursor.execute(query_prefix, (startdate, enddate, prefix52))
|
|
rows_prefix = cursor.fetchall()
|
|
result_prefix = rows_prefix
|
|
print('Count for prefix +52: ' + str(result_prefix[0][0]))
|
|
add_line_email('Count for prefix +52: ' + str(result_prefix[0][0]))
|
|
|
|
cursor.execute(query_prefix, (startdate, enddate, prefix55))
|
|
rows_prefix2 = cursor.fetchall()
|
|
result_prefix2 = rows_prefix2
|
|
print('Count for prefix +55: ' + str(result_prefix2[0][0]))
|
|
add_line_email('Count for prefix +55: ' + str(result_prefix2[0][0]))
|
|
|
|
except mysql.connector.Error as e:
|
|
print(f'Error connecting to database: {e}')
|
|
|
|
subject = ('SoMo Report for' + ' ' + str(first_last_month))
|
|
body = multiple_lines
|
|
recipients = ['mark@42tele.com', 'xxx@xxx, 'xxx@xxx']
|
|
#recipients = ['mark@42tele.com']
|
|
|
|
print('Sending email..')
|
|
send_email(subject, body, recipients)
|