获取系统cpu和内存参数

# -*- coding: utf-8 -*-
# 获取server运行情况
# import json
# from datetime import datetime
# 
# import paramiko
# import xlwt, xlrd
# from xlutils.copy import copy
# 
# def get_host_cpu_memory():
#     client = paramiko.SSHClient()
#     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#     print('------------开始连接服务器-----------')
#     client.connect('192.168.0.102', 22, username='hui', password='199317', timeout=4)
#     print('------------认证成功!.....-----------')
# 
#     sheet_name = 'host-' + datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
#     # print('本次生成的表名为{}'.format(sheet_name))
#     # 结果写入excel中
#     book = xlwt.Workbook()
#     sheet = book.add_sheet(sheetname='{}'.format(sheet_name))
#     sheet_title = ['id', 'cpu_info', 'cpu_average', 'memory_info', 'memory_average']
#     for i in range(0, len(sheet_title)):
#         sheet.write(0, i, sheet_title[i])
#     book.save('{}.xls'.format(sheet_name))
# 
#     # 远程环境中一定要导包。循环去获取server情况。不在这里控制频率
#     cpu_average = 0
#     memory_average = 0
#     id = 1
#     while True:
#         stdin, stdout, stderr = client.exec_command('cd Desktop/code/25_processings; python host_script.py')
#         content = stdout.read().decode('utf-8').split('\n')[:2:]
#         # print(content)
#         # print(len(content))
#         # print(type(content))
# 
#         cpu_info = json.loads(content[0])
#         memory_info = float(content[1])
# 
#         # cpu平均使用率
#         cpu_info = sum(cpu_info) / len(cpu_info)
#         cpu_average = cpu_info if cpu_average == 0 else (cpu_average + cpu_info) / 2
#         cpu_average = round(cpu_average, 4)
# 
#         # memory平均使用率
#         memory_average = memory_info if memory_average == 0 else (memory_average + memory_info) / 2
#         memory_average = round(memory_average, 4)
# 
#         # 数据加入表中
#         # ret_data = [cpu_average, memory_average]
#         # print(ret_data)
#         data = xlrd.open_workbook('{}.xls'.format(sheet_name), formatting_info=True)
#         excel = copy(wb=data)  # 完成xlrd对象向xlwt对象转换
#         excel_table = excel.get_sheet(0)  # 获得要操作的页
#         table = data.sheets()[0]
#         nrows = table.nrows  # 获得行数
#         ret_list = ['%.4f' % i for i in [cpu_info, cpu_average, memory_info, memory_average]]
#         # print(ret_list)
# 
# 
#         excel_table.write(nrows, 0, id)
#         excel_table.write(nrows, 1, ret_list[0])
#         excel_table.write(nrows, 2, ret_list[1])
#         excel_table.write(nrows, 3, ret_list[2])
#         excel_table.write(nrows, 4, ret_list[3])
#         excel.save('{}.xls'.format(sheet_name))
#         id += 1
# 
# get_host_cpu_memory()



# 获取系统数据
# import psutil
# def get_sys_info():
#     cpu_percent = psutil.cpu_percent(percpu=True, interval=5)
#     rel_memory = psutil.virtual_memory()
#     print(cpu_percent)
#     print(rel_memory[2])
#
# if __name__ == '__main__':
#     get_sys_info()


# 主要的code,多进程任务
# import time
# from multiprocessing import Process
#
#
#
# start_time = time.time()
#
#
# def get_host_info():
#     while True:
#         time.sleep(0.3)
#         print('processing---1')
#
# def get_ctr_info():
#     while True:
#         time.sleep(0.3)
#         print('processing---2')
#
# def get_train_info():
#     print('开始计算,用时10秒')
#     while time.time() - start_time < 10:
#         time.sleep(0.3)
#         print('计算')
#     print('结束计算,用时10秒')
#
#
# def main():
#     host_info = Process(target=get_host_info)
#     ctr_info = Process(target=get_ctr_info)
#     train_info = Process(target=get_train_info)
#
#     host_info.start()
#     ctr_info.start()
#     train_info.start()
#
#     # 等待大规模计算完成
#     train_info.join()
#
#     # 结束系统数据统计
#     host_info.terminate()
#     ctr_info.terminate()
#
# if __name__ == '__main__':
#     main()
posted @ 2020-04-12 18:27  疯狂列表推导式  阅读(248)  评论(0编辑  收藏  举报