机房环境监测服务端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.9 KiB

import subprocess, platform
import os, sys
import django
from datetime import datetime,timedelta
parDIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
BASE_DIR = os.path.dirname(os.path.join(parDIR, os.pardir))
sys.path.append(BASE_DIR)
print(BASE_DIR)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'envdect.settings')
django.setup()
from env.models import *
from env.envpost import save_alarm, send_alm_message
# from env.sms import sendSMS
from env.mail import sendmail
alert_people = ['nfa@yeah.net',]
def pingTEST(host):
res = False
try:
output = subprocess.check_output("ping -{} 6 {}".format('n' if platform.system().lower()=="windows" else 'c', host), shell=True)
if output.index(b'TTL=') > 0:
res = True
except:
return False
return res
def online_dect():
alms = Alarmset.objects.all()
for alm in alms:
host = alm.spot.ip
print('testing ip :', host)
r = pingTEST(host)
print('Testing result:', r)
if not r:
save_alarm(alm.spot, 7, '设备离线', 0)
if alm.connection:
send_alm_message(alm.spot, 7, '设备离线', 0)
def device_dect():
spots = Spot.objects.all()
for spot in spots:
device = Envdata.objects.filter(spot=spot).order_by('-rectime').first()
if device:
mailtext = '%s机房设备离线,请及时处理,上次提交数据时间:%s' % (device.spot.name, datetime.strftime(device.rectime,'%Y-%m-%d %H:%M:%S'))
if datetime.now() - device.rectime >= timedelta(minutes=20):
sendmail(alert_people, '机房环境监测报警', mailtext)
if __name__ == '__main__':
# host = input('Please input the IP address:')
# r = pingTEST(host)
# print(r)
# online_dect()
device_dect()