该脚本功能
1、检测IP连接数;
2、1分钟内的cpu静态负载情况
3、网络连接数量
4、CPU空闲率
5、等待队列长度插差值
#!bin/python
import os
#
def get_net():
'''网络是否异常>500'''
cmd="netstat -ant | wc -l "
res=os.popen(cmd).read()
rest=(res.strip())
return rest
#
def get_uptime():
'''uptime 1min'''
cmd = " uptime |awk -F ',' '{print $3}' | awk '{print $3}' "
res = os.popen(cmd).read()
res = (res.strip())
return res
#
def get_top():
try:
cmd = " top -n 1 | grep %Cpu |awk -F ',' '{print $4}' | awk -F 'i' '{print $1}' "
res = os.popen(cmd).read()
res = res.strip().split(' ')
a = float(res[1])
except:
a=100.0
return a
#
def get_queue(n):
'''ss -lnt | grep : 99 Recv_Q的值'''
cmd="ss -lnt|grep :8888| awk '{print $%s}' "%n
res=os.popen(cmd).read()
res = (res.strip())
return res
#
#
def get_que_diff():
#print(worning_dict)
'''采集队列长度的差值:(Send_Q - Recv_Q)'''
after=int(get_queue(3))
befor = int(get_queue(3))
res=after-befor
return res
#
#
def get_net_recv():
cmd="netstat -anpt | awk '{print $4,$6}' | sort | uniq -c "
res = os.popen(cmd).readlines()
try:
worning_dict={}
for i in res:
make_lis = i.strip().split(' ')
if int(make_lis[0]) > 50:
worning_dict.setdefault(f"IP:{make_lis[1]}",f"连接数:{make_lis[0]}")
#print(worning_dict)
except:
pass
return worning_dict
print("相通IP连接数量过高:",get_net_recv())
print("1分钟内CPU静态负载情况:",get_uptime())
print("网络连接数量:",get_net())
print("CPU空闲率:",get_top())
print("等待队列长度差值:",get_que_diff())
worning_dict=get_net_recv() #获取syn的连接数量
for k,v in worning_dict.items():
print(k,v)