脚本场景:当无人在家(dhcp保留手机IP,通过ping手机IP判断是否有人在家),过去10秒所有帧画面平均分值超过10分时,发送报警邮件到QQ邮箱,微信开通邮件提醒后,可以直接经过微信通知。
脚本是很久以前写的了,不知道现在zoneminder是不是可以直接邮件报警了。
状态定义:
- suspend,检测到手机,不报警
- fine,检测不到手机,开启报警,无警报
- alarm,检测不到手机,开启报警,有警报
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
import smtplib
import datetime
import os
from email.mime.text import MIMEText
mail_host = 'smtp.163.com'
mail_user = '***'
mail_pass = '***'
mail_postfix = '163.com'
mail_subject = 'zm_alarm:' + datetime.datetime.now().strftime('%y-%m-%d_%H:%M:%S')
strsql='select e.id,f.score from frames f,events e where f.eventid=e.id and e.monitorid=1 and f.timestamp>date_sub(now(), interval 10 second)'
def send_mail(sub,content):
me='zm<' + mail_user + '@' + mail_postfix + '>'
msg = MIMEText(content,_subtype='html',_charset='utf-8')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = '***@qq.com'
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, '***@qq.com', msg.as_string())
server.close()
except Exception, e:
print str(e)
if len(os.popen('ping -c 2 -W 1 192.168.2.188 | grep time | grep from ').readlines()) == 0:
db = MySQLdb.connect('192.168.2.50','zmuser','zmpass','zm' )
cur = db.cursor()
num = cur.execute(strsql)
data = cur.fetchall()
score = 0
scoremax = 0
eventid = 0
for rec in data:
score = score + rec[1]
if rec[1] > 10:
eventid = rec[0]
if scoremax < rec[1]:
scoremax=rec[1]
if score > num * 10:
print 'ALARM'
send_mail(mail_subject + '[mx:%s]'%(scoremax,),\
'Alarm <br />EventID:<a href="http://***/zm?view=event&mode=stream&mid=1&eid=%s">%s</a>'\
'<br />Frames: %s<br />MaxScore: %s<br />'\
'CAM_VIEW:<a href="http://***/zm/?view=montage&group=0" target="_blank">http://***/zm/?view=montage&group=0</a>'%(eventid,eventid,num,scoremax))
os.system('echo `date "+%Y-%m-%d %H:%M:%S : 1 - ALARM."` >> /var/works/mlog')
else:
os.system('echo `date "+%Y-%m-%d %H:%M:%S : 0 - FINE."` >> /var/works/mlog')
cur.close()
db.close()
else:
os.system('echo `date "+%Y-%m-%d %H:%M:%S : 0 - SUSPEND."` >> /var/works/mlog')