def handle(self, *args, **options): now = time.localtime() now_hour_Tuple = (now.tm_year,now.tm_mon,now.tm_mday,now.tm_hour,00,00,now.tm_wday,now.tm_yday,now.tm_isdst) intnow_hour = time.mktime(now_hour_Tuple) intnow_hour_8p = intnow_hour + 8*60*60 intnow_hour_9p = intnow_hour + 9*60*60 formatnow_hour_8p = stamp2str(intnow_hour_8p, formt='%Y-%m-%d %H:%M') formatnow_hour_9p = stamp2str(intnow_hour_9p, formt='%Y-%m-%d %H:%M') if now.tm_hour >=16: day = '明日' else: day = '今日' duty_rota = Rota.objects.filter(duty_date_start__gte=formatnow_hour_8p,duty_date_start__lt = formatnow_hour_9p) if duty_rota: for item in duty_rota: # if item.rota_activity_id !=11: # continue activity=RotaActivity.objects.get(id=item.rota_activity_id) promotion=item.promotion if ([object.id for object in activity.domains.all()]!=[DOMAIN_HEAD_ID] and promotion==0) or promotion!=0: continue duty_start = item.duty_date_start.strftime('%Y-%m-%d %H:%M') duty_end = item.duty_date_end.strftime('%Y-%m-%d %H:%M') duty_domain_id = item.duty_domain_id duty_domain_name = DdDomain.objects.get(id=duty_domain_id).domainname duty_mans = item.duty_man.all() duty_man_names=[man.username_ch+'('+man.telephone+')' for man in duty_mans] duty_man_names=','.join(duty_man_names) duty_man_tels = [man.telephone for man in duty_mans] way=['on call','on site','at home'] duty_way = item.duty_way duty_backups = item.duty_backup.all() duty_backup_names=[man.username_ch+'('+man.telephone+')' for man in duty_backups] duty_backup_names=','.join(duty_backup_names) duty_backup_tels = [man.telephone for man in duty_backups] if [object.id for object in activity.domains.all()]==[DOMAIN_HEAD_ID] and promotion==0: head='IT_OndutyManager值班提醒: ' message_content= head + 'IT_OndutyManager值班人员:'+duty_man_names+ '; 后备人员:'+duty_backup_names + '; 值班时间:'+duty_start +' 至 '+duty_end else: head=activity.name+'值班提醒: ' message_content= head + duty_domain_name +'值班人员:'+duty_man_names+ '; 后备人员:'+duty_backup_names + '; 值班时间:'+duty_start +' 至 '+duty_end +'; 值班方式:'+way[duty_way] # for tel in duty_man_tels: # sendmessage_v2(0,"yihaodian/techdep-yellowpages",message_content,tel) # for tel in duty_backup_tels: # sendmessage_v2(0,"yihaodian/techdep-yellowpages",message_content,tel) duty_man_tels.extend(duty_backup_tels) datas={'level_id': 500,'message':message_content ,'caller': ','.join(duty_man_tels),'caller_message': message_content, 'get_time': time.time()} process_notification(json.dumps(datas)) print stamp2str(time.time()) + ':success--' + '值班短信'
def sendmail(request): if request.method == 'POST': activityid=request.POST['activityid'] recipient=request.POST['recipient'] recipient=recipient.replace(';',';').split(';') # addrecipient=[] # for id in recipient: # try: # email=DdUsers.objects.get(id=id,enable=0).email # except DdUsers.DoesNotExist: # email=None # addrecipient.append(email) try: activity=RotaActivity.objects.get(id=activityid) except RotaActivity.DoesNotExist: return HttpResponse("<title>发送失败</title>发送失败!该活动不存在,请检查。") domains=activity.domains.all() unfinished=[] if activity.promotion==1: shifts=activity.shift_times.count() for domain in domains: if Rota.objects.filter(rota_activity=activity.id,duty_domain=domain.id).count()<shifts: unfinished.append(domain.domainemailgroup) if activity.promotion==0: ym=re.findall(r"\d+",activity.name) day=calendar.monthrange(int(ym[0]),int(ym[1])) for domain in domains: try: dailyfrequency=DailyDutyConfig.objects.get(domain=domain.id,enable=1).dailyfrequency except DailyDutyConfig.DoesNotExist: dailyfrequency=0 if Rota.objects.filter(rota_activity=activity.id,duty_domain=domain.id).count()<dailyfrequency*day[1]: unfinished.append(domain.domainemailgroup) # if [domain.id for domain in domains] !=[106]: # for domain in domains: # if Rota.objects.filter(rota_activity=activity.id,duty_domain=domain.id).count()<day[1]: # unfinished.append(domain.domainemailgroup) # else: # for domain in domains: # if Rota.objects.filter(rota_activity=activity.id,duty_domain=domain.id).count()<2*day[1]: # unfinished.append(domain.domainemailgroup) if activity.promotion==2: for domain in domains: if Rota.objects.filter(rota_activity=activity.id,duty_domain=domain.id).count()==0: unfinished.append(domain.domainemailgroup) recipient_list=unfinished subject=activity.name+'录入提醒' if activity.promotion ==2: message='您Domain的反馈信息还未录入,请尽快录入。链接:http://oms.yihaodian.com.cn/cmdbv2/cmdb/rotaenter/?activity_id='+str(activity.id) html_content = loader.render_to_string('cmdbv2/cmdb/text_mail.html', { 'content': '您Domain的反馈信息还未录入,请尽快录入。链接:', 'url': 'http://oms.yihaodian.com.cn/cmdbv2/cmdb/rotaenter/?activity_id='+str(activity.id) }) else: message='您Domain的值班信息还未完整录入,请尽快录入。链接:http://oms.yihaodian.com.cn/cmdbv2/cmdb/rotaenter/?activity_id='+str(activity.id) html_content = loader.render_to_string('cmdbv2/cmdb/text_mail.html', { 'content': '您Domain的值班信息还未完整录入,请尽快录入。链接:', 'url': 'http://oms.yihaodian.com.cn/cmdbv2/cmdb/rotaenter/?activity_id='+str(activity.id) }) if recipient_list: datas={'level_id': 500,'title': subject,'message': message,'send_to': ','.join(recipient_list), 'cc':','.join(recipient), 'get_time': time.time()} process_notification(json.dumps(datas),template= html_content.encode('utf8')) return HttpResponse('<title>发送成功</title><div class="text" style="text-align:center;">发送成功!(实际邮件或有延时,请等候)</div>') else: return HttpResponse('<title>发送失败</title>所有domain的信息均已录入,邮件无需发送!')
def run_notification(jsonj): process_notification(jsonj)
def handle(self, *args, **options): now = time.localtime() year = now.tm_year month = now.tm_mon day = now.tm_mday if month == 12: start_year = str(year + 1) start_month = str(1) end_year = start_year end_month = str(2) else: start_year = str(year) start_month = str(month + 1) end_year = start_year end_month = str(month + 2) if month == 11: end_year = str(year + 1) end_month = str(1) # acname=['值班经理','值班监控','问题诊断专家'] # domainid=[800,106,49] # recipient_list=[['*****@*****.**','*****@*****.**'],['*****@*****.**','*****@*****.**'],['*****@*****.**']] # error=[] for obj in DailyDutyConfig.objects.filter(enable=1): # for i in range(3): activity, created = RotaActivity.objects.get_or_create( name=start_year + '年' + start_month + '月' + obj.name, defaults={ 'name': start_year + '年' + start_month + '月' + obj.name, 'start_time': start_year + '-' + start_month + '-01 00:00', 'end_time': end_year + '-' + end_month + '-01 00:00', 'promotion': 0, }) if created: activity.domains.add(obj.domain) subject = activity.name + '值班录入提醒' message = activity.name + '值班活动已创建,请及时录入值班信息。链接:http://oms.yihaodian.com.cn/cmdbv2/cmdb/rotaenter/?activity_id=' + str( activity.id) html_content = loader.render_to_string( 'cmdbv2/cmdb/text_mail.html', { 'content': activity.name + '值班活动已创建,请及时录入值班信息。链接:', 'url': 'http://oms.yihaodian.com.cn/cmdbv2/cmdb/rotaenter/?activity_id=' + str(activity.id) }) if obj.domain_id == DOMAIN_HEAD_ID: recipient_list = [ '*****@*****.**', '*****@*****.**' ] else: recipient_list = [ obj.domain.domainemailgroup, '*****@*****.**' ] if obj.sendmail == 1: # sendmail_co(subject, html_content, recipient_list, 0,"yihaodian/techdep-yellowpages") # None datas = { 'level_id': 500, 'title': subject, 'message': message, 'send_to': ','.join(recipient_list), 'get_time': time.time() } process_notification(json.dumps(datas), template=html_content) print stamp2str(time.time()) + ':success'
def handle(self, *args, **options): exception_reports = ExceptionReport.objects.filter(status=1) current_time = int(time.time()) current_tuple = time.localtime() current_hour = current_tuple.tm_hour current_wday = current_tuple.tm_wday time_str = date.today().strftime("%Y-%m-%d") + ' 00:00:00' update_time = int( time.mktime(time.strptime(time_str, '%Y-%m-%d %H:%M:%S'))) #控制每天只保留一条记录 for exception_report in exception_reports: if exception_report.type == 0: cursor = connections[exception_report.use_db].cursor() cursor.execute(exception_report.cmdbsql) data = cursor.fetchall() mail_list = str(exception_report.db_mail).split(',') #排除备注数据已恢复正常的情况 indexs = [rs[0] for rs in data] comments = ExceptionDetailComment.objects.filter( exception_id=exception_report.id) comments_id = [c.index for c in comments] for comment in comments: if comment.index not in indexs: sendmail.sendmail_html( subject='CMDB异常报表存在备注数据已恢复正常', html_content= '<h1>Hi:</h1><br><h3>异常报表存在备注数据恢复正常,备注信息已删除,原始记录如下</h3>' + '<br> 异常名称:<strong>' + exception_report.cname + '</strong><br> 查询日期:<strong>' + stamp2str(time.time()) + '</strong><br> 索引值:<strong>' + comment.index + '</strong><br> 备注信息:<strong>' + comment.comment + '</strong><br> 详细信息请查看以下页面: ' + '<br> http://oms.yihaodian.com.cn/cmdbv2/change/exception_detail/' + str(exception_report.id) + '/ <br> 请核实被删除备注信息是否恢复正常!<br> 谢谢!', recipient_list=mail_list) comment.delete() new_exceptions = [] for new in data: if new[0] not in comments_id: new_exceptions.append(new) comment_count = ExceptionDetailComment.objects.filter( exception_id=exception_report.id).count() new_count = len(data) - comment_count #与前一天的异常数做对比,出现新的异常则报警发送邮件 # if new_count > exception_report.exception_count: # new_exp = '' # for new1 in new_exceptions: # new_exp += str(new1[0]) + ' ' # sendmail.sendmail_html(subject='CMDB异常报表出现' + str(new_count - exception_report.exception_count) + '条新的异常数据', # html_content='<h1>Hi:</h1><br><h3>异常报表新增' + str(new_count - exception_report.exception_count) + '条异常数据</h3>' + # '<br> 异常名称:<strong>'+ exception_report.cname + # '</strong><br> 出现日期:<strong>'+ stamp2str(time.time()) + # '</strong><br> 异常数据:<strong>'+ new_exp + # '</strong><br> 详细信息请查看以下页面: ' + # '<br> http://oms.yihaodian.com.cn/cmdbv2/change/exception_detail/' + str(exception_report.id) + # '/ <br> 请尽快排查数据数据出现的原因并做处理!<br> 谢谢!', # recipient_list = mail_list) #更新异常数据数量 exception_report.last_update = current_time exception_report.exception_count = new_count exception_report.save() connections[exception_report.use_db].close() daily, created = ExceptionReportDaily.objects.get_or_create( report_id=exception_report.id, create_time=update_time, defaults={ 'exception_count': new_count, }) if not created: daily.exception_count = new_count daily.save() #if exception_report.type==1 and current_wday==1: if exception_report.type == 1: url = exception_report.api_url.strip() headers = { 'Authorization': 'Basic Y21kYjpmQWIoeFVZWTVuOSkqXmdhXmE=' } content = requests.get(url, headers=headers).json() new_count = content['count'] exception_report.exception_count = new_count exception_report.last_update = current_time exception_report.save() daily, created = ExceptionReportDaily.objects.get_or_create( report_id=exception_report.id, create_time=update_time, defaults={ 'exception_count': new_count, }) if exception_report.frequency and exception_report.last_email_date and new_count > 0: next_mail_date = exception_report.last_email_date + datetime.timedelta( days=exception_report.frequency) if date.today() == next_mail_date: title = '异常报表异常提醒' message = '异常名称:' + exception_report.cname + '。详细信息请查看链接,请尽快排查数据数据出现的原因并做处理!http://oms.yihaodian.com.cn/cmdbv2/change/exception_detail/' + str( exception_report.id) + '/' html_content = loader.render_to_string( 'cmdbv2/cmdb/text_mail.html', { 'content': '您Domain发生' + exception_report.cname + '异常,详细信息请查看以下链接,请尽快排查数据数据出现的原因并做处理!', 'url': 'http://oms.yihaodian.com.cn/cmdbv2/change/exception_detail/' + str(exception_report.id) + '/' }) recipient_list = [] if exception_report.owner_domain: recipient_list = exception_report.owner_domain.domainemailgroup elif exception_report.type == 1: exceptionconfigs = ExceptionConfigAccessDetail.objects.filter( error=int(exception_report.api_url[-1])) for exceptionconfig in exceptionconfigs: domainemail = exceptionconfig.domain.domainemailgroup if exceptionconfig.domain else None if domainemail and domainemail not in recipient_list: recipient_list.append(domainemail) recipient_list = ','.join(recipient_list) else: recipient_list = ','.join( list(set([rs[-1] for rs in data]))) datas = { 'level_id': exception_report.level_id, 'source_id': 29, 'title': title, 'message': message, 'send_to': recipient_list, 'cc': exception_report.db_mail, 'get_time': time.time() } process_notification(json.dumps(datas)) exception_report.last_email_date = date.today() exception_report.save() print message print recipient_list print stamp2str(time.time()) + ':success'