Sensors监控服务器温度并通过qq邮件通知

由于正值酷暑,公司机房又是无人值守的,所以需要对机房机器进行一个温度监控,温度过高时通过邮箱或短信报警处理

通过搜寻网络发现,sensors这个服务挺不错的,于是记录并下载用于使用。

一:安装sensors

此方法用于centos7.0上

sudo yum install lm_sensorssudo sensors-detect

二:使用sensors

使用sensors只需输入

sensors

显示内容:

coretemp-isa-0000
Adapter: ISA adapter
Physical id 0:  +52.0°C  (high = +74.0°C, crit = +84.0°C)
Core 0:         +47.0°C  (high = +74.0°C, crit = +84.0°C)
Core 1:         +48.0°C  (high = +74.0°C, crit = +84.0°C)
Core 2:         +45.0°C  (high = +74.0°C, crit = +84.0°C)
Core 3:         +45.0°C  (high = +74.0°C, crit = +84.0°C)
Core 4:         +44.0°C  (high = +74.0°C, crit = +84.0°C)
Core 5:         +46.0°C  (high = +74.0°C, crit = +84.0°C)coretemp-isa-0001
Adapter: ISA adapter
Physical id 1:  +54.0°C  (high = +74.0°C, crit = +84.0°C)
Core 0:         +46.0°C  (high = +74.0°C, crit = +84.0°C)
Core 1:         +47.0°C  (high = +74.0°C, crit = +84.0°C)
Core 2:         +44.0°C  (high = +74.0°C, crit = +84.0°C)
Core 3:         +44.0°C  (high = +74.0°C, crit = +84.0°C)
Core 4:         +46.0°C  (high = +74.0°C, crit = +84.0°C)
Core 5:         +44.0°C  (high = +74.0°C, crit = +84.0°C)

三:通过python进行监控

如下代码还有部分欠缺,但勉强可以使用,邮件服务器使用了qq,可以发送给多人

# -*- coding: UTF-8 -*-
import os
import re
import smtplib
from email.mime.text import MIMEText
import time
import datetime
def sleeptime(hour,min,sec):return hour*3600 + min*60 + sec;
#每隔10分钟检测一次
second = sleeptime(0,10,0);
while 1==1:logName = "TemplateMonitor.log" if not os.path.exists(logName):touch = 'touch ' + logNameos.system(touch)time.sleep(second);nowTime=datetime.datetime.now().strftime('%H')nowTimeDate=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')cmdline = 'sensors'#运行sensorssensors = os.popen(cmdline).read()sensors = re.sub('\s','',sensors)template = []#取出所有内核温度for i in sensors.split('Core'):for j in i.split('('):if j[0] not in ['a','c','i','h']:template.append(j[-7:-3])#取出最大内核温度max_template = max(template)#发送人msg_from=''  passwd=''             #接受人msg_to=[]subject="【OpenERP.HK】机房温度提示"   content= 'The ip is 192.168.1.100'+'\n'+'Now time is '+nowTimeDate+'\n'+'The Template is  ' + max_template +'\n'#写log文件f=file(logName, "a+")f.write(content)f.close()
    if datetime.datetime.now().strftime('%H') in ['9','12','18']:msg = MIMEText(content)msg['Subject'] = subjectmsg['From'] = msg_fromfor i in range(len(msg_to)):msg['To'] = msg_to[i]if not int(nowTime) > 22 or int(nowTime) < 8:try:s = smtplib.SMTP_SSL("smtp.qq.com",465)s.login(msg_from, passwd)s.sendmail(msg_from, msg_to[i], msg.as_string())finally:s.quit()if int(max_template[0:1]) > '55':content  += 'WARMING! The Template is too HIGH'msg = MIMEText(content)msg['Subject'] = subjectmsg['From'] = msg_fromfor i in range(len(msg_to)):msg['To'] = msg_to[i]#免打扰时间if not int(nowTime) > 22 or int(nowTime) < 8:try:s = smtplib.SMTP_SSL("smtp.qq.com",465)s.login(msg_from, passwd)s.sendmail(msg_from, msg_to[i], msg.as_string())finally:s.quit()


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部