客户端脚本:
端脚本:
import io
import os
import sys
import time
import datetime
import socket
import commands
import logging
from logging import handlers
import psutil
import struct
import fcntl
if sys.getdefaultencoding() != 'utf-8':
reload(sys)
sys.setdefaultencoding('utf-8')
class Logger(object):
level_relations = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'crit': logging.CRITICAL
} # 日志级别关系映射
def init (self,logname, level='info', when='D', backCount=10, fmt='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'):
CURRENT_DIR = os.path.dirname(file)
LOG_FILE = os.path.abspath(os.path.join(CURRENT_DIR, logname))
self.logger = logging.getLogger(LOG_FILE)
format_str = logging.Formatter(fmt) # 设置日志格式
self.logger.setLevel(self.level_relations.get(level)) # 设置日志级别
sh = logging.StreamHandler() # 往屏幕上输出
sh.setFormatter(format_str) # 设置屏幕上显示的格式
th = handlers.TimedRotatingFileHandler(
filename=LOG_FILE, when=when, backupCount=backCount, encoding='utf-8') # 往文件里写入#指定间隔时间自动生成文件的处理器
#实例化TimedRotatingFileHandler
#interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种:
th.setFormatter(format_str) # 设置文件里写入的格式
#self.logger.addHandler(sh) # 把对象加到logger里
if not self.logger.handlers:
self.logger.addHandler(th)
class clientMonitor(object):
#获取指定网卡ip
def getIpAddress(self,dev):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
a = s.fileno()
b = 0x8915
c = struct.pack('256s', dev[:15])
res = fcntl.ioctl(a, b, c)[20:24]
return socket.inet_ntoa(res)
#获取网络的使用情况 ,取的是eth0的发送和收取的总字节数
#readNetInfo('eth0')
def readNetInfo(self,dev):
f = open('/proc/net/dev')
lines = f.readlines()
f.close()
res = {'in':0, 'out':0}
for line in lines:
if line.lstrip().startswith(dev):
line = line.replace(':', ' ')
items = line.split()
res['in'] = long(items[1])#/1024
res['out'] = long(items[len(items)/2 + 1])#/1024
return res
def readNetInfo_new(self,dev):
res = {'in':0, 'out':0}
res['in'] = psutil.net_io_counters(pernic=True).get(dev).bytes_recv
res['out'] = psutil.net_io_counters(pernic=True).get(dev).bytes_sent
return res
#磁盘使用率,path:磁盘路径
def disk_stat(self,path):
hd={}
disk = os.statvfs(path)
percent = (disk.f_blocks - disk.f_bfree) * 100 / (disk.f_blocks -disk.f_bfree + disk.f_bavail) + 1
return percent
def net_loop(self,dev):
#end = {'in':0, 'out':0}
res = self.readNetInfo_new(dev)
#推迟执行的秒数
time.sleep(2)
#new_recv,new_send = get_net_data()
new_res = self.readNetInfo_new(dev)
recv_data = (new_res['in']-res['in'])/1024/1024
send_data = (new_res['out']-res['out'])/1024/1024
print ("recv_data: %s M, send_data: %s M"%(recv_data, send_data))
return recv_data,send_data
def processcheck(self,cmd):
#cmd='ps -aux | sort -k3nr | head -1'
(status,output) = commands.getstatusoutput(cmd)
#Pid= output.split(' ')[6]
log.logger.info('资源占用top:\\n'+output)
#查看占用内存最高的进程的PID
#ps aux|head -1;ps aux|grep -v PID|sort -rn -k +4|head
#ps -aux | sort -k4nr | head -1 ,-k3 cpu占用最高,-k4 内存占用最高
#root 14668 1.9 0.0 90504 3256 ? Ss 4月23 2811:48 /sbin/rngd -f
#索引:-k3 b.split(' ')[6] 28进程路径(/sbin/rngd)
#索引:-k4 b.split(' ')[4]
if name == " main ":
#10分钟执行一次,数据上报到服务端,服务端负责报警
#需要修改的参数:custom,deviceType,netName
custom ='test'
deviceType ='客户端服务器'
#网卡名称
netName = 'ens3f0'
log = Logger('socketclient.logs')
log.logger.info("----start----")
info=clientMonitor()
locatIp = info.getIpAddress(netName)
recv_data,send_data = info.net_loop(netName)
cpuinfo = psutil.cpu_percent(1)
#svmem(total=67268558848, available=32022245376, percent=52.4, used=34601009152, free=29655695360, active=17274105856, inactive=2927910912, buffers=10100736, cached=3001753600, shared=298610688, slab=11243315200)
svmem = psutil.virtual_memory()
meminfo = svmem[2]
disk_root = info.disk_stat('/')
disk_appslog = info.disk_stat('/appslog')
disk_bigdata = info.disk_stat('/bigdata')
#如果CPU或内存的占用率大于80%,将占用CPU或内存资源最多的进程找出来
issendmsg =1
if(cpuinfo>80 or meminfo>80 or disk_root>80 or disk_appslog>80 or disk_bigdata>80 or recv_data>3000 or send_data>3000):
#发送预警邮件
sendmsg=locatIp +' 服务器资源占用高!请检查!\\n'
sendmsg += "CPU占用:"+str(cpuinfo)+'\\n'
sendmsg += "内存占用:"+str(meminfo)+'\\n'
sendmsg += "/目录占用:"+str(disk_root)+'\\n'
sendmsg += "/appslog目录占用:"+str(disk_appslog)+'\\n'
sendmsg += "/bigdata目录占用:"+str(disk_bigdata)+'\\n'
sendmsg += "网卡接收流量:"+str(recv_data)+'M,发送流量 '+str(send_data)+'M \\n'
#sendmsg += "网卡10秒发送流量:"+str(send_data)+'\\n'
log.logger.info(sendmsg)
if cpuinfo>80 :
info.processcheck('ps -aux | sort -k3nr | head -10')
if meminfo>80 :
info.processcheck('ps -aux | sort -k4nr | head -10')
issendmsg = 1
else:
#log.logger.info(locatIp+" 正常")
log.logger.info("CPU使用率:"+str(cpuinfo))
log.logger.info("内存使用率:"+str(meminfo))
log.logger.info("/目录使用率:"+str(disk_root))
log.logger.info("/appslog使用率:"+str(disk_appslog))
log.logger.info("/bigdata使用率:"+str(disk_bigdata))
log.logger.info("网卡接收和发送情况:接收"+str(recv_data) +"M, 发送 "+str(send_data)+"M")
#Id,custom,deviceType,IP,cpu,mem,disk_root,disk_appslog,disk_bigdata,networkRecv,networkSend,uploadTime,temp2,temp3,temp4
msg = '1'+'|'+custom+'|'+deviceType+'|'+locatIp+'|'+str(cpuinfo)+'|'+str(meminfo)+'|'+str(disk_root)+'|'+str(disk_appslog)+'|'+str(disk_bigdata)+'|'+str(recv_data)+'|'+str(send_data)+'|'+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if issendmsg ==1:
ip_port = ('IP',port)
sk = socket.socket()
sk.connect(ip_port)
sk.sendall(msg)
data = sk.recv(1024)
if data=='1':
log.logger.info("本地预警信息传输成功!")
else:
log.logger.info("本地预警信息传输失败!")
sk.sendall('exit')
sk.close()
原文链接:https://blog.csdn.net/eagle89/article/details/128579002
全部0条评论
快来发表一下你的评论吧 !