Python SMTP 服务器(响应时间)异常长 PRTG
Python SMTP Server (Response Time) is unusually high PRTG
我是新手所以请原谅我的基础知识。
我有一个 Python 脚本来 接收 SMTP 消息 'Server' 并将其发送到 SMS 服务提供商的 API,该脚本是 运行 Ubuntu 服务器 OS 18.04 带有 nohup 和 & ...虽然我对功能没有问题,但每天早上在 05:00 和 [=42= 之间] 我从服务器收到异常延迟的响应(我可以从 PRTG 和日志中看到)。从下面的代码日志中,您可以看到该功能仍然有效,但它使请求等待的时间比正常时间长 'doing so makes the sent code with SMS unusable.'
非常感谢您的帮助。
OS 信息:
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-101-generic x86_64)
System load: 0.08 Processes: 193
Usage of /: 9.8% of 97.93GB Users logged in: 0
Memory usage: 9% IP address for ens160: XXXX
Swap usage: 0%
Python代码:
import asyncio
from aiosmtpd.controller import Controller
import asyncore
import time
import requests
import logging
from smtplib import SMTP as Client
logging.basicConfig(filename='smtpServer.log',level=logging.INFO, format='%(asctime)s %(message)s')
class DataHandler: #this is the server taking the data and processing it.
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
envelope.rcpt_tos.append(address)
return '250 OK'
async def handle_DATA(self, server, session, envelope):
msg_content = envelope.content.decode('utf8', errors='replace')
sent_to = envelope.rcpt_tos[0].split('@')[0][1:]
api_key = "API_KEY"
word_list = msg_content.split()
code = " "
if 'AuthCode:' in word_list:
code_index = word_list.index('AuthCode:')
code = word_list[code_index+1]
msg= 'Your Token Code to access XXX VPN is ' + code +'. Do not share the password with anyone'
headers = {'Content-Type': 'application/json'}
--- code omitted ---
elif sent_to[:2] == '90': #if it's a local number 'starting with 90'
try:
url = "SMS_ServiceProvider_URL"
payload = '{"api_key":"'+api_key+'","title":"8507013986","text":"'+msg+'","sentto":"'+sent_to+'"}'
print('{} Code has been sent to +{}'.format(code, sent_to))
requests.request("POST", url, headers=headers, data = payload)
logging.info('{} Code has been sent to +{} by local server'.format(code, sent_to))
except Exception as e:
raise e ('Failed sending to local number ERROR')
--- code omitted ---
return '250 Message accepted for delivery'
controller = Controller(DataHandler(), hostname='XXXX', port=25)
controller.start()
while True:
time.sleep(60)
print(end='')
记录消息:
-- 正常情况:
2020-06-04 10:35:25,327 Peer: ('<IP>', 16750)
2020-06-04 10:35:25,327 ('<IP>', 16750) handling connection
2020-06-04 10:35:25,327 ('<IP>', 16750) Data: b'EHLO example.com'
2020-06-04 10:35:25,328 ('<IP>', 16750) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 10:35:25,328 ('<IP>', 16750) sender: example@example.com
2020-06-04 10:35:25,328 ('<IP>', 16750) Data: b'RCPT TO:<+9*******9794@10.90.214.135>'
2020-06-04 10:35:25,329 ('<IP>', 16750) recip: +9*******9794@10.90.214.135
2020-06-04 10:35:25,329 ('<IP>', 16750) Data: b'DATA'
2020-06-04 10:35:26,568 043396 Code has been sent to +9*******9794 by local server
2020-06-04 10:35:26,569 ('<IP>', 16750) Data: b'QUIT'
2020-06-04 10:35:26,570 ('<IP>', 16750) connection lost
-- 停机期间:
2020-06-04 05:12:12,797 Peer: ('<IP>', 59450)
2020-06-04 05:12:12,797 Peer: ('<IP>', 6845)
2020-06-04 05:12:12,797 Peer: ('<IP>', 59798)
2020-06-04 05:12:12,797 Peer: ('<IP>', 10667)
2020-06-04 05:12:12,797 Peer: ('<IP>', 24380)
2020-06-04 05:12:12,797 Peer: ('<IP>', 60299)
2020-06-04 05:12:12,797 Peer: ('<IP>', 23987)
2020-06-04 05:12:12,797 ('<IP>', 7834) connection lost
2020-06-04 05:12:12,798 Connection lost during _handle_client()
2020-06-04 05:12:12,798 ('<IP>', 18527) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 9209) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 59450) handling connection
2020-06-04 05:12:12,798 ('<IP>', 6845) handling connection
2020-06-04 05:12:12,798 ('<IP>', 59798) handling connection
2020-06-04 05:12:12,797 ('<IP>', 7834) connection lost
2020-06-04 05:12:12,798 Connection lost during _handle_client()
2020-06-04 05:12:12,798 ('<IP>', 18527) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 9209) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 59450) handling connection
2020-06-04 05:12:12,798 ('<IP>', 6845) handling connection
2020-06-04 05:12:12,798 ('<IP>', 59798) handling connection
2020-06-04 05:12:12,798 ('<IP>', 10667) handling connection
2020-06-04 05:12:12,799 ('<IP>', 24380) handling connection
2020-06-04 05:12:12,799 ('<IP>', 60299) handling connection
2020-06-04 05:12:12,799 ('<IP>', 23987) handling connection
2020-06-04 05:12:12,799 ('<IP>', 18527) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,799 ('<IP>', 9209) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,799 ('<IP>', 59798) EOF received
2020-06-04 05:12:12,799 ('<IP>', 59450) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,800 ('<IP>', 59798) connection lost
2020-06-04 05:12:12,800 ('<IP>', 6845) Data: b'EHLO example.com'
2020-06-04 05:12:12,800 ('<IP>', 10667) Data: b'EHLO example.com'
2020-06-04 05:12:12,800 ('<IP>', 24380) Data: b'EHLO example.com'
2020-06-04 05:12:12,801 ('<IP>', 60299) Data: b'EHLO TWPRTGIST01'
2020-06-04 05:12:12,801 ('<IP>', 6845) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,801 ('<IP>', 6845) sender: example@example.com
2020-06-04 05:12:12,801 ('<IP>', 10667) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 10667) sender: example@example.com
2020-06-04 05:12:12,802 ('<IP>', 24380) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 24380) sender: example@example.com
2020-06-04 05:12:12,802 ('<IP>', 23987) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 23987) sender: example@example.com
2020-06-04 05:12:12,803 ('<IP>', 60299) Data: b'QUIT'
2020-06-04 05:12:12,803 ('<IP>', 6845) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 6845) recip: +9**********@<IP>
2020-06-04 05:12:12,803 ('<IP>', 10667) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 10667) recip: +9**********@<IP>
2020-06-04 05:12:12,803 ('<IP>', 24380) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 24380) recip: +9**********@<IP>
2020-06-04 05:12:12,804 ('<IP>', 60299) connection lost
2020-06-04 05:12:12,804 Connection lost during _handle_client()
2020-06-04 05:12:12,804 ('<IP>', 23987) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,804 ('<IP>', 23987) recip: +9**********@<IP>
2020-06-04 05:12:12,804 ('<IP>', 6845) Data: b'DATA'
2020-06-04 05:12:12,804 ('<IP>', 10667) Data: b'DATA'
2020-06-04 05:12:12,804 ('<IP>', 24380) Data: b'DATA'
2020-06-04 05:12:12,805 ('<IP>', 23987) Data: b'DATA'
2020-06-04 05:13:52,957 708496 Code has been sent to +9********** by local server
2020-06-04 05:15:33,051 619421 Code has been sent to +9********** by local server
2020-06-04 05:17:13,215 035670 Code has been sent to +9********** by local server
2020-06-04 05:18:53,341 861227 Code has been sent to +9********** by local server
简答:
跳出框框,我可以看到最后每条记录的消息之间有 1 分 40 秒 - 这不可能是巧合。
检查您的 SMS 网关的限制是什么 - 您可以在一分钟内发送多少条消息,以及在发送一定数量后是否应用了任何整形算法。
更长的答案:
从日志中我可以看到 python 处理并发连接没有任何延迟,但最终的 POST 请求被延迟了。这意味着您的服务器应该有足够的 CPU/Memory 来工作,但最后一个操作是瓶颈。既然你 运行 它是异步的,它不应该是 Python 端的问题,而是接收端 - SMS 网关的问题。此服务很可能会接收您的所有请求并将它们添加到某个队列中,因此它们是否在您端异步处理并不重要。
据我所知,此问题与 PRTG/monitoring 无关,但您仍然可以证明我是错误的 - 向该服务器添加一些传感器以监视磁盘 I/O、CPU负载,内存使用情况,查看服务器在白天或晚上是否有任何性能问题
我是新手所以请原谅我的基础知识。 我有一个 Python 脚本来 接收 SMTP 消息 'Server' 并将其发送到 SMS 服务提供商的 API,该脚本是 运行 Ubuntu 服务器 OS 18.04 带有 nohup 和 & ...虽然我对功能没有问题,但每天早上在 05:00 和 [=42= 之间] 我从服务器收到异常延迟的响应(我可以从 PRTG 和日志中看到)。从下面的代码日志中,您可以看到该功能仍然有效,但它使请求等待的时间比正常时间长 'doing so makes the sent code with SMS unusable.'
非常感谢您的帮助。
OS 信息:
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-101-generic x86_64)
System load: 0.08 Processes: 193
Usage of /: 9.8% of 97.93GB Users logged in: 0
Memory usage: 9% IP address for ens160: XXXX
Swap usage: 0%
Python代码:
import asyncio
from aiosmtpd.controller import Controller
import asyncore
import time
import requests
import logging
from smtplib import SMTP as Client
logging.basicConfig(filename='smtpServer.log',level=logging.INFO, format='%(asctime)s %(message)s')
class DataHandler: #this is the server taking the data and processing it.
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
envelope.rcpt_tos.append(address)
return '250 OK'
async def handle_DATA(self, server, session, envelope):
msg_content = envelope.content.decode('utf8', errors='replace')
sent_to = envelope.rcpt_tos[0].split('@')[0][1:]
api_key = "API_KEY"
word_list = msg_content.split()
code = " "
if 'AuthCode:' in word_list:
code_index = word_list.index('AuthCode:')
code = word_list[code_index+1]
msg= 'Your Token Code to access XXX VPN is ' + code +'. Do not share the password with anyone'
headers = {'Content-Type': 'application/json'}
--- code omitted ---
elif sent_to[:2] == '90': #if it's a local number 'starting with 90'
try:
url = "SMS_ServiceProvider_URL"
payload = '{"api_key":"'+api_key+'","title":"8507013986","text":"'+msg+'","sentto":"'+sent_to+'"}'
print('{} Code has been sent to +{}'.format(code, sent_to))
requests.request("POST", url, headers=headers, data = payload)
logging.info('{} Code has been sent to +{} by local server'.format(code, sent_to))
except Exception as e:
raise e ('Failed sending to local number ERROR')
--- code omitted ---
return '250 Message accepted for delivery'
controller = Controller(DataHandler(), hostname='XXXX', port=25)
controller.start()
while True:
time.sleep(60)
print(end='')
记录消息:
-- 正常情况:
2020-06-04 10:35:25,327 Peer: ('<IP>', 16750)
2020-06-04 10:35:25,327 ('<IP>', 16750) handling connection
2020-06-04 10:35:25,327 ('<IP>', 16750) Data: b'EHLO example.com'
2020-06-04 10:35:25,328 ('<IP>', 16750) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 10:35:25,328 ('<IP>', 16750) sender: example@example.com
2020-06-04 10:35:25,328 ('<IP>', 16750) Data: b'RCPT TO:<+9*******9794@10.90.214.135>'
2020-06-04 10:35:25,329 ('<IP>', 16750) recip: +9*******9794@10.90.214.135
2020-06-04 10:35:25,329 ('<IP>', 16750) Data: b'DATA'
2020-06-04 10:35:26,568 043396 Code has been sent to +9*******9794 by local server
2020-06-04 10:35:26,569 ('<IP>', 16750) Data: b'QUIT'
2020-06-04 10:35:26,570 ('<IP>', 16750) connection lost
-- 停机期间:
2020-06-04 05:12:12,797 Peer: ('<IP>', 59450)
2020-06-04 05:12:12,797 Peer: ('<IP>', 6845)
2020-06-04 05:12:12,797 Peer: ('<IP>', 59798)
2020-06-04 05:12:12,797 Peer: ('<IP>', 10667)
2020-06-04 05:12:12,797 Peer: ('<IP>', 24380)
2020-06-04 05:12:12,797 Peer: ('<IP>', 60299)
2020-06-04 05:12:12,797 Peer: ('<IP>', 23987)
2020-06-04 05:12:12,797 ('<IP>', 7834) connection lost
2020-06-04 05:12:12,798 Connection lost during _handle_client()
2020-06-04 05:12:12,798 ('<IP>', 18527) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 9209) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 59450) handling connection
2020-06-04 05:12:12,798 ('<IP>', 6845) handling connection
2020-06-04 05:12:12,798 ('<IP>', 59798) handling connection
2020-06-04 05:12:12,797 ('<IP>', 7834) connection lost
2020-06-04 05:12:12,798 Connection lost during _handle_client()
2020-06-04 05:12:12,798 ('<IP>', 18527) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 9209) Data: b'QUIT'
2020-06-04 05:12:12,798 ('<IP>', 59450) handling connection
2020-06-04 05:12:12,798 ('<IP>', 6845) handling connection
2020-06-04 05:12:12,798 ('<IP>', 59798) handling connection
2020-06-04 05:12:12,798 ('<IP>', 10667) handling connection
2020-06-04 05:12:12,799 ('<IP>', 24380) handling connection
2020-06-04 05:12:12,799 ('<IP>', 60299) handling connection
2020-06-04 05:12:12,799 ('<IP>', 23987) handling connection
2020-06-04 05:12:12,799 ('<IP>', 18527) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,799 ('<IP>', 9209) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,799 ('<IP>', 59798) EOF received
2020-06-04 05:12:12,799 ('<IP>', 59450) connection lost
2020-06-04 05:12:12,799 Connection lost during _handle_client()
2020-06-04 05:12:12,800 ('<IP>', 59798) connection lost
2020-06-04 05:12:12,800 ('<IP>', 6845) Data: b'EHLO example.com'
2020-06-04 05:12:12,800 ('<IP>', 10667) Data: b'EHLO example.com'
2020-06-04 05:12:12,800 ('<IP>', 24380) Data: b'EHLO example.com'
2020-06-04 05:12:12,801 ('<IP>', 60299) Data: b'EHLO TWPRTGIST01'
2020-06-04 05:12:12,801 ('<IP>', 6845) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,801 ('<IP>', 6845) sender: example@example.com
2020-06-04 05:12:12,801 ('<IP>', 10667) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 10667) sender: example@example.com
2020-06-04 05:12:12,802 ('<IP>', 24380) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 24380) sender: example@example.com
2020-06-04 05:12:12,802 ('<IP>', 23987) Data: b'MAIL FROM:<example@example.com>'
2020-06-04 05:12:12,802 ('<IP>', 23987) sender: example@example.com
2020-06-04 05:12:12,803 ('<IP>', 60299) Data: b'QUIT'
2020-06-04 05:12:12,803 ('<IP>', 6845) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 6845) recip: +9**********@<IP>
2020-06-04 05:12:12,803 ('<IP>', 10667) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 10667) recip: +9**********@<IP>
2020-06-04 05:12:12,803 ('<IP>', 24380) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,803 ('<IP>', 24380) recip: +9**********@<IP>
2020-06-04 05:12:12,804 ('<IP>', 60299) connection lost
2020-06-04 05:12:12,804 Connection lost during _handle_client()
2020-06-04 05:12:12,804 ('<IP>', 23987) Data: b'RCPT TO:<+9**********@<IP>>'
2020-06-04 05:12:12,804 ('<IP>', 23987) recip: +9**********@<IP>
2020-06-04 05:12:12,804 ('<IP>', 6845) Data: b'DATA'
2020-06-04 05:12:12,804 ('<IP>', 10667) Data: b'DATA'
2020-06-04 05:12:12,804 ('<IP>', 24380) Data: b'DATA'
2020-06-04 05:12:12,805 ('<IP>', 23987) Data: b'DATA'
2020-06-04 05:13:52,957 708496 Code has been sent to +9********** by local server
2020-06-04 05:15:33,051 619421 Code has been sent to +9********** by local server
2020-06-04 05:17:13,215 035670 Code has been sent to +9********** by local server
2020-06-04 05:18:53,341 861227 Code has been sent to +9********** by local server
简答:
跳出框框,我可以看到最后每条记录的消息之间有 1 分 40 秒 - 这不可能是巧合。
检查您的 SMS 网关的限制是什么 - 您可以在一分钟内发送多少条消息,以及在发送一定数量后是否应用了任何整形算法。
更长的答案:
从日志中我可以看到 python 处理并发连接没有任何延迟,但最终的 POST 请求被延迟了。这意味着您的服务器应该有足够的 CPU/Memory 来工作,但最后一个操作是瓶颈。既然你 运行 它是异步的,它不应该是 Python 端的问题,而是接收端 - SMS 网关的问题。此服务很可能会接收您的所有请求并将它们添加到某个队列中,因此它们是否在您端异步处理并不重要。
据我所知,此问题与 PRTG/monitoring 无关,但您仍然可以证明我是错误的 - 向该服务器添加一些传感器以监视磁盘 I/O、CPU负载,内存使用情况,查看服务器在白天或晚上是否有任何性能问题