pythonサーバステータスの監視
6267 ワード
pythonによるサーバcpu、メモリ、トラフィック情報の取得
cpu
メモリ
ネットワークカード
cpu
def get_cpu_status(path='/proc/loadavg'):
'''
CPU
'''
loadavg = {}
with open(path, 'r', encoding='utf-8') as f1:
list_content = f1.read().split()
# ['0.00', '0.03', '0.05', '2/114', '1752'] 1、5、15 , ,
loadavg['lavg_1'] = list_content[0]
loadavg['lavg_2'] = list_content[1]
loadavg['lavg_15'] = list_content[2]
return loadavg
メモリ
def get_memory_status(path='/proc/meminfo'):
'''
'''
mem_dic = {}
with open(path, 'r', encoding='utf-8') as f2:
lines = f2.readlines()
for line in lines:
name = line.strip().split(':')[0]
data = line.split(":")[1].split()[0]
mem_dic[name] = float(data)
return mem_dic['MemTotal'] - mem_dic['MemFree'] - mem_dic['Buffers'] - mem_dic['Cached']
ネットワークカード
def get_network_status(path='/proc/net/dev'):
'''
'''
net_info = {}
with open(path, 'r', encoding='utf-8') as f3:
lines = f3.readlines()
for line in lines[2:]:
line = line.split(":")
eth_name = line[0].strip()
if eth_name != 'lo':
io_dic = {}
io_dic['receive'] = round(float(line[1].split()[0]) / (1024.0 * 1024.0), 2)
io_dic['transmit'] = round(float(line[1].split()[8]) / (1024.0 * 1024.0), 2)
net_info[eth_name] = io_dic
return net_info