监控服务器资源利用率的服务端脚本

描述

其主要目的是:基于 Python 编程语言来监控服务器的 CPU、内存、/目录、/appslog、/bigdata目录使用率以及网卡接收和发送情况。

该脚本部署场景分为:服务端和客户端。

服务端:一台固定 IP 地址的服务器

客户端:N 台指定固定 IP 地址的服务器

服务端脚本:

- - coding:utf-8 - -

import io

import os

import sys

import logging

from logging import handlers

import MySQLdb

import smtplib

from email.mime.text import MIMEText

from email.header import Header

from email.utils import formataddr

import requests, json

import datetime

import time

import shutil,re

import uuid

import socket

import SocketServer

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是间隔的时间单位,单位有以下几种:

S 秒

M 分

H 小时、

D 天、

W 每星期(interval==0时代表星期一)

midnight 每天凌晨

th.setFormatter(format_str) # 设置文件里写入的格式

#self.logger.addHandler(sh) # 把对象加到logger里

if not self.logger.handlers:

self.logger.addHandler(th)

class Analysis(object):

def buildMsg(self,msg):

print('构造预警信息'+str(msg))

icount = 0

if(float(msg[4]) > 90):

icount+=1

CPU ="> CPU预警:使用率高于90%,使用"+str(msg[4])+"% \\n"

else:

CPU=""

if(float(msg[5]) > 90):

icount+=1

mem ="> 内存预警:使用率高于90%,使用"+str(msg[5])+"% \\n"

else:

mem=""

if(float(msg[6]) > 85):

icount+=1

disk_root ="> 磁盘根目录预警:使用率高于85%,使用"+str(msg[6])+"% \\n"

else:

disk_root=""

if(float(msg[7]) > 85):

icount+=1

disk_appslog ="> 业务磁盘预警:使用率高于85%,使用"+str(msg[7])+"% \\n"

else:

disk_appslog=""

if(float(msg[8]) > 3000):

icount+=1

networkRecv ="> 网卡10秒内接收数据预警:接收数据大于4000M,接收"+str(msg[8])+"M \\n"

else:

networkRecv=""

if(float(msg[9]) > 3000):

icount+=1

networkSend ="> 网卡10秒内发送数据预警:发送数据大于4000M,发送"+str(msg[9])+"M \\n"

else:

networkSend=""

s= alarmName+"\\n"+msg[2]+":" +msg[3]+"\\n" +CPU+mem+disk_root+disk_appslog+networkRecv+networkSend

#print(s)

log.logger.info('预警信息:'+s)

#发送预警
   if(icount>0):
       #发送预警邮件、企业微信
       if mailconf ==1:
           self.send_mail(s,msg[3])
       if wxconf ==1:
           self.send_WX(s)

def send_mail(self,content,ip):

smtpserver = 'smtp.163.com'

mail_user="xxx@163.com"

mail_pass="passwordxxx"

mail_res=["xxx@163.com","xxx@163.com","xxx@163.com","xxx@163.com","xxx@163.com","xxx@163.com","xxx@163.com"]

sub = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

msg = MIMEText( sub + "\\n"+content, _subtype='plain',_charset='utf-8')

msg['Subject'] = Header(alarmName+':'+ip, 'utf-8' )

#msg['From'] = Header("系统预警", 'utf-8')

msg['From'] = formataddr(pair=('设备预警', mail_user))

msg['To'] = ', '.join(mail_res)

smtp = smtplib.SMTP()

smtp.connect(smtpserver)

smtp.starttls()

smtp.login(mail_user, mail_pass)

smtp.sendmail(mail_user, mail_res, msg.as_string())

smtp.quit()

def send_WX(self,msg):

headers = {"Content-Type": "text/plain"}

#s="服务器预警:{},验证码{}".format({str(printCode)},{str(verifyCode)})

data = {

"msgtype": "text",

"text": {

"content": msg,

}

}

r = requests.post(

url='企业微信机器人地址(需要根据实际机器人地址配置)',

headers=headers, json=data)

print(r.text)

def Write_to_Mysql_alarm(self,valuelist):

#log = Logger('all.log',level='debug')

#业务监控:id,project,tpye,exceptiontype,details(xx,大数据,无es进程/es集群不健康,)

try:

db = MySQLdb.connect("xxx", "xxx", "xxx", "xxx", charset='utf8' )

log.logger.info("数据库连接成功")

except:

log.logger.info("数据库连接失败")

创建游标

cursor = db.cursor()

uid = uuid.uuid1()

result=0

sql =''

try:

sql = 'insert into test_serverresourcealarm values (%s, %s, %s, %s,%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)'

#val = (str(uid),valuelist[1], valuelist[2], valuelist[3], valuelist[4],valuelist[5], valuelist[6],'',valuelist[7], valuelist[8],valuelist[9],valuelist[10],'','','')

val = (str(uid),valuelist[2], valuelist[3], valuelist[4], valuelist[5],valuelist[6], valuelist[7],'',valuelist[8], valuelist[9],valuelist[10],'','','')

cursor.execute(sql,val)

db.commit()

log.logger.error('设备预警信息已入库!')

#发送企业微信预警信息

self.buildMsg(valuelist)

except:

into = sys.exc_info()

#log.logger.error('插入数据失败!')

log.logger.error('设备预警信息入库失败!'+str(into))

result=0

#str = self.obj_to_string(sys.exc_info(), self)

print('error',into)

# 关闭游标
   db.close()
   return result

def Write_to_Mysql_temp(self,valuelist):

打开数据库连接

#db = MySQLdb.connect("xxx", "xxx", "xxx", "xxx", charset='utf8' )

try:

db = MySQLdb.connect("xxx", "xxx", "xxx", "xxx", charset='utf8' )

log.logger.info("数据库连接成功")

except:

log.logger.info("数据库连接失败")

使用cursor()方法获取操作游标

cursor = db.cursor()

uid = uuid.uuid1()

result=0

try:

sql = 'insert into test_serverresourcetemp values (%s, %s, %s, %s,%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)'

val = (str(uid),valuelist[2], valuelist[3], valuelist[4], valuelist[5],valuelist[6], valuelist[7],'',valuelist[8], valuelist[9],valuelist[10],'','','')

cursor.execute(sql,val)

db.commit()

result=1

log.logger.info("临时表sql执行状态:"+str(result))

except:

into = sys.exc_info()

result = 0

print(into)

log.logger.info('临时表sql执行失败: '+str(into))

关闭数据库连接

db.close()
   return result

class MyServer(SocketServer.BaseRequestHandler):

def handle(self):

conn = self.request

log.logger.info('... connected from {}'.format(self.client_address))

#print('1多线程监控')

Flag = True

while Flag:

data = conn.recv(1024)

#print(data)

if len(data)>10:

log.logger.info('接收到的客户端数据:'+data)

conn.sendall('1')

sub = data.strip('\\n')

str = sub.split('|')

#print(str)

a = Analysis()

#报警信息入库,#将监控数据写入临时表中test_serverresourcetemp_lty

result = a.Write_to_Mysql_temp(str)

if(float(str[4])>90 or float(str[5])>90 or float(str[6])>85 or float(str[7])>85 or float(str[8])>3000 or float(str[9])>3000):

result1 = a.Write_to_Mysql_alarm(str)

#result = 1

if result == 0:

log.logger.info('预警信息入库失败!')

else:

log.logger.info('预警信息入库完成!')

#发送预警邮件、企业微信

#a.buildMsg(str)

if data =='exit':
           log.logger.info('... connecte end ...')
           Flag = False

if name == " main ":

#每分钟执行一次

log = Logger('socketservice.logs')

log.logger.info('----start----')

alarmName ='服务器资源预警'

#是否开启邮件报警,1为开启,0为关闭

mailconf =1

#是否开启企业微信报警,1为开启,0为关闭

wxconf =0

server = SocketServer.ThreadingTCPServer(('IP',port),MyServer)

server.serve_forever()

原文链接:https://blog.csdn.net/eagle89/article/details/128579002

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分