【发布时间】:2020-04-09 18:51:48
【问题描述】:
import psutil
STATS_URL = ' http://172.20.10.2:8080/report'
SERVER_NAME="test_local_server"
def get_health():
print('generating health report')
cpu_percent = psutil.cpu_percent(interval=2.0)
ctime = psutil.cpu_times()
disk_usage = psutil.disk_usage("/")
net_io_counters = psutil.net_io_counters()
virtual_memory = psutil.virtual_memory()
# The keys in this dict should match the db cols
report = dict (
USER_NAME="USER",
SERVER_NAME="Test_Server",
cpupercent = cpu_percent,
cpu_total = ctime.user + ctime.system,
free_Percnt=((disk_usage.free/disk_usage.used)*100),
bytes_sent = net_io_counters.bytes_sent,
bytes_received = net_io_counters.bytes_recv,
packets_sent = net_io_counters.packets_sent,
packets_received = net_io_counters.packets_recv,
memory_Free = virtual_memory.free,
)
return report
if __name__=='__main__':
print(f'starting health report stream for server :\t{SERVER_NAME}')
while True:
report = get_health()
r = requests.post(STATS_URL, json=report)
time.sleep(20)
我需要将 AWS 远程服务器上的这个脚本的值发送回我的 Flask APP,它是我的 Windows 笔记本电脑上的主服务器。
@app.route("/report",methods=['POST'])
def report():
if request.method=='POST':
time_epoch=time.time()
incoming_report = request.get_json()
print("Generating Health report")
username=incoming_report["USER_NAME"]
server_name=incoming_report["SERVER_NAME"]
disk_free=incoming_report["free_Percnt"]
bytes_sent=incoming_report["bytes_sent"]
bytes_received=incoming_report["bytes_received"]
packets_sent=incoming_report["packets_sent"]
packets_received=incoming_report["packets_received"]
memory_free=incoming_report["memory_Free"]
cpu_percent=incoming_report["cpupercent"]
cpu_total=incoming_report["cpu_total"]
conn=sqlite3.connect("Health.db")
conn.execute(f"create table if not exists {username}_{server_name}
(HEALTH_ID integer primary key AUTOINCREMENT,Time_Epoch integer,Disk_Free varchar(80),Bytes_Sent varchar(80),Bytes_Received varchar(80),Packets_Sent varchar(80),Packets_Received varchar(80),Memory_Free varchar(80),Cpu_Usage_Percent varchar(80),Cpu_Time varchar(80));")
conn.execute(f'insert into {username}_{server_name} (Time_Epoch,Disk_Free,Bytes_Sent,Bytes_Received,Packets_Sent,Packets_Received,Memory_Free,Cpu_Usage_Percent,Cpu_Time) values {time_epoch,disk_free,bytes_sent,bytes_received,packets_sent,packets_received,memory_free,cpu_percent,cpu_total}')
conn.commit()
return {'message': 'success'}
if __name__ ==("__main__"):
app.run(host='0.0.0.0',port=8080)
所以我将主机更改为“0.0.0.0”,因为当我在网上检查时,我发现这就是让烧瓶应用程序公开但来自脚本的连接说连接超时。有人可以帮我发送来自 python 的值吗脚本回到我的烧瓶应用程序?
【问题讨论】: