def handle(self, *args, **options): state = True try: sendmail_html(subject='test', html_content='<h1>test</h1>', recipient_list=['*****@*****.**']) except Exception, e: if "{u'*****@*****.**': (550, '5.1.1 User unknown')}" == str( e): pass else: print('error:%s' % str(e)) state = False
def handle(self, *args, **options): base_url = 'http://%s:15672/api/queues/%%2f/' % RABBIT_MQ['HOST'] url_list = [ base_url + 'assetv2_change?lengths_age=150&lengths_incr=30', base_url + 'assetv2_deploy?lengths_age=150&lengths_incr=30', base_url + 'assetv2_stg_deploy?lengths_age=150&lengths_incr=30' ] req = requests.session() req.auth = (RABBIT_MQ['USER'], RABBIT_MQ['PASSWORD']) queue_list = [] for url in url_list: response = req.get(url) if (response.status_code == 200): try: item = response.json() queue_list.append({ 'name': item['name'], 'messages': item['messages'] if item.has_key('messages') else 0, 'messages_details': item['messages_details']['samples'] }) flag = 0 for detail in item['messages_details']['samples']: if detail['sample'] >= 10: flag = 1 else: flag = 0 if flag: sendmail_html(subject='报警:消息队列阻塞', html_content='<h1>消息队列' + item['name'] + '阻塞</h1><br><h2>消息数超过5</h2>', recipient_list=['*****@*****.**']) except Exception, e: print('error:%s' % str(e)) else: print('error:get rabbitMQ api error')
def handle(self, *args, **options): error = False message = [] try: action_list = AccidentAction.objects.filter( accident_id__gte=2016010101, status=1) user_names = [] for act in action_list: du = act.duty_users.split(',') if len(du) > 1: for u in du: user_names.append(u) elif len(du) == 1: user_names.append(act.duty_users) user_names = list(set(user_names)) for username in user_names: try: user = DdUsers.objects.using('default').get( username=username, enable=0) except DdUsers.DoesNotExist: user = None own_actions = AccidentAction.objects.filter( duty_users__contains=username, accident_id__gte=2016010101, status=1) act_list = [] act_user_email = [] for action in own_actions: try: accident = Accident.objects.get( accidentid=action.accident_id, is_accident=0) except Accident.DoesNotExist, Accident.MultipleObjectsReturned: accident = None if accident: act_list.append({ 'level': accident.get_level_display, 'duty_manager_ch': accident.duty_manager_name_ch, 'accidentid': accident.accidentid, 'title': accident.title, 'action': action.action, 'duty_dept': action.dutydept_name, 'duty_users': action.duty_users, 'expect_time': action.expect_time_format }) try: act_user_email.append( DdUsers.objects.using('default').get( username=username, enable=0).email) except DdUsers.DoesNotExist, DdUsers.MultipleObjectsReturned: error = True message.append( '### duty user mail does not exist: username=%s' % username) act_user_email = list(set(act_user_email)) # act_user_email = ['*****@*****.**'] # 发送Action提醒邮件 if act_list and act_user_email: t = get_template('mail/accident/action_delay_notice.html') html_content = t.render(Context(locals())) try: sendmail_html(u'【Action提醒】您还有未完成改进措施', html_content, act_user_email) except Exception, e: error = True message.append( '### send mail error: username=%s, detail: %s' % (username, str(e))) break
fields_count = len(fields) is_superuser = request.user.is_superuser mail_list = str(exception_report.db_mail).split(',') #排除备注数据已恢复正常的情况 indexs_ids = [rs[0] for rs in results] comments = ExceptionDetailComment.objects.filter(exception_id=id) comments_id = [c.index for c in comments] for comment in comments: if comment.index not in indexs_ids: 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 results: if new[0] not in comments_id: new_exceptions.append(new) #将异常数据添加备注信息,并分类传送给前台 data_danger = []
message.append( '### duty user mail does not exist: username=%s' % username) act_user_email = list(set(act_user_email)) # act_user_email = ['*****@*****.**'] # 发送Action提醒邮件 if act_list and act_user_email: t = get_template('mail/accident/action_delay_notice.html') html_content = t.render(Context(locals())) try: sendmail_html(u'【Action提醒】您还有未完成改进措施', html_content, act_user_email) except Exception, e: error = True message.append( '### send mail error: username=%s, detail: %s' % (username, str(e))) break message.append(stamp2str(time.time()) + ':finish') except Exception, e: error = True message.append('error: %s' % str(e)) print('\n'.join(message)) # 脚本报错邮件通知 if error: t = get_template('mail/cmdb/cron_sync_by_cmis_error.html') title = '【Action邮件提醒脚本失败】%s' % stamp2str(int(time.time()), formt='%Y-%m-%d %H:%M:%S') html_content = t.render(Context(locals())) sendmail_html(title, html_content, ACCIDENT_CRON_MAILLIST)
if not created: cmis_sync_user.display_name = item.displayName if item.displayName else '' cmis_sync_user.email = item.email cmis_sync_user.enable = 0 cmis_sync_user.telephone = item.phone cmis_sync_user.username_ch = str( item.displayName[( item.displayName.rfind('_') + 1):-1]) if item.displayName else '' cmis_sync_user.save() except Exception, e: error = True message.append('error: id=%d, detail: %s' % (item['id'], str(e))) break message.append(stamp2str(time.time()) + ':finish') # 反向查询更新已离职人员状态 DdUsers.objects.filter(enable=0).exclude( username__in=hr_users).update(enable=1) except Exception, e: error = True message.append('error: %s' % str(e)) print('\n'.join(message)) # 邮件通知 if error: t = get_template('mail/cmdb/cron_sync_by_cmis_error.html') title = '【同步用户信息失败】%s' % stamp2str(int(time.time()), formt='%Y-%m-%d %H:%M:%S') html_content = t.render(Context(locals())) sendmail_html(title, html_content, CMDB_CRON_MAILLIST)
def perform_destroy(self, instance): #server判断 if Server.objects.filter(assetid=instance.assetid).exclude( server_status_id=400).exists(): raise YAPIException('该设备有正在服务的server,请将该server报废后再删除设备!') if instance.rack_id != 0: rack_name = instance.rack.name rack_id = instance.rack.id room_id = instance.rack.room_id else: rack_name = None rack_id = None room_id = None #机架、机位处理 CB_asset_info = { 'assetid': instance.assetid, 'service_tag': instance.service_tag, 'asset_type_id': instance.asset_type_id, 'asset_model_id': instance.asset_model_id, 'create_time': instance.create_time, 'last_modified': instance.last_modified, 'expiration_time': instance.expiration_time, 'rack_id': rack_id, 'room_id': room_id, } CB_asset_delete(instance.assetid, self.request.user.username, json.dumps(CB_asset_info, ensure_ascii=False)) rackspace = RackSpace.objects.filter(assetid=instance.assetid) rack_space = [] for item in rackspace: rack_space.append(str(item.id)) CB_rack_info = { 'assetid': instance.assetid, 'rack_id': rack_id, 'rack_name': rack_name, 'room_id': room_id, 'rack_space': ",".join(rack_space), 'asset_type_id': instance.asset_type_id, } CB_rack_deletasset(instance.assetid, self.request.user.username, json.dumps(CB_rack_info, ensure_ascii=False)) rackspace.update(assetid="") ip_tmp = IpTotal.objects.exclude(type=1).filter( asset_info=instance.assetid) for item in ip_tmp: CB_ip_info = { 'assetid': instance.assetid, 'ip': item.ip, } item.is_used = 0 item.asset_info = "" item.save() if item.type == 2: CB_mgip_unbind_by_deleteasset( instance.assetid, self.request.user.username, json.dumps(CB_ip_info, ensure_ascii=False)) elif item.type == 3: CB_ip_unbind_by_deleteasset( instance.assetid, self.request.user.username, json.dumps(CB_ip_info, ensure_ascii=False)) if instance.asset_type_id != 1: action_name = u'设备删除' mail_list = SERVER_MAILLIST['scrap'] api_url = SERVER_ZABBIX_API['DELETE'] % (SERVER_ZABBIX_ID, item.ip) res = json.loads(urllib.urlopen(api_url).read()) zabbix_content = u'请求URL:%s, 请求结果:%s' % ( api_url, res['msg'].encode('utf8')) if res['success'] == True: CB_zabbix_delete(item.ip, self.request.user.username) #zabbix调用失败后发送邮件 poolname = None sitename = None if res['success'] == False: logger.info(__name__ + ": send failed email.") mail_title = action_name + u',zabbix状态更新异常:%s' % item.ip html_content = loader.render_to_string( 'mail/server.html', { 'action': action_name, 'action_time': stamp2str(time.time()), 'poolname': poolname, 'ips': item.ip, 'username': self.request.user.username, 'sitename': sitename, 'server_change_content': zabbix_content }) sendmail_html(mail_title, html_content, mail_list) instance.delete()
class Command(BaseCommand): args = '' help = 'sync domain info from cmis' def handle(self, *args, **options): error = False message = [] try: head_users_domains = DdUsersDomains.objects.filter(dddomain_id=DOMAIN_HEAD_ID) head_user_ids = [uu.ddusers_id for uu in head_users_domains] response = json.loads(urllib.urlopen(CRON_DOMAIN_API).read()) for item in response['response']: cmis_sync_domain, created = DdDomain.objects.get_or_create(id=item['id'], defaults={ 'domaincode': HTMLParser.HTMLParser().unescape(item['domainCode']), 'domainname': HTMLParser.HTMLParser().unescape(item['domainName']), 'domainemailgroup': item['domainEmailGroup'], 'domainleaderaccount': item['domainLeaderAccount'], 'backupdomainleaderaccount': item['backupDomainLeaderAccount'], 'enable': item['enable'], 'departmentid': item['departmentId'], 'departmentname': item['departmentName'], }) if not created: cmis_sync_domain.domaincode = HTMLParser.HTMLParser().unescape(item['domainCode']) cmis_sync_domain.domainname = HTMLParser.HTMLParser().unescape(item['domainName']) cmis_sync_domain.domainemailgroup = item['domainEmailGroup'] cmis_sync_domain.domainleaderaccount = item['domainLeaderAccount'] cmis_sync_domain.backupdomainleaderaccount = item['backupDomainLeaderAccount'] cmis_sync_domain.enable = item['enable'] cmis_sync_domain.departmentid = item['departmentId'] cmis_sync_domain.departmentname = item['departmentName'] cmis_sync_domain.save() if cmis_sync_domain.enable == 0: # Domain可用,同步Domain下的用户 user_domain = DdUsersDomains.objects.filter(dddomain=cmis_sync_domain.id) user_ids = [uu.ddusers_id for uu in user_domain] res = json.loads(urllib.urlopen(CRON_DOMAIN_USERS_API % cmis_sync_domain.id).read()) new_user_ids = [nu['id'] for nu in res['response']] for item1 in res['response']: display_name = item1['displayName'].strip() cmis_sync_user, created = DdUsers.objects.get_or_create(id=item1['id'], username=item1['adAccount'], defaults={ # 'username': item1['adAccount'], 'display_name': display_name, 'email': item1['email'], # 'enable': item1['enable'], 'enable': 0, 'telephone': item1['mobilePhoneNo'], 'username_ch': str(display_name[(display_name.rfind('_') + 1):-1]), }) if not created: # cmis_sync_user.username = item1['adAccount'] cmis_sync_user.display_name = item1['displayName'] cmis_sync_user.email = item1['email'] # cmis_sync_user.enable = item1['enable'] cmis_sync_user.enable = 0 cmis_sync_user.telephone = item1['mobilePhoneNo'] cmis_sync_user.username_ch = str(display_name[(display_name.rfind('_') + 1):-1]) cmis_sync_user.save() cmis_sync_user.domains.add(cmis_sync_domain) # for head in DdUsers.objects.filter(username=item1['adAccount']): # if head.id in head_user_ids: # 如果是乐道自建的head,废弃用户重新写入 # head.enable = 1 # head.save() # head_users_domains.filter(ddusers=head.id).update(ddusers=item1['id']) # user_domain.exclude(ddusers__in=new_user_ids).delete() # 删除已不在该Domain的用户对应关系 # DdUsers.objects.filter(id__in=user_ids, enable=0).exclude(id__in=new_user_ids).update(enable=1) # 废弃已不在该Domain的用户 else: # Domain已废弃,删除该Domain下的用户 DdUsersDomains.objects.filter(dddomain=cmis_sync_domain.id).delete() message.append(stamp2str(time.time()) + ':finish') except Exception, e: error = True message.append('error: %s' % str(e)) print('\n'.join(message)) if error: # 邮件通知 t = get_template('mail/cmdb/cron_sync_by_cmis_error.html') title = '【同步Domain和用户信息失败】%s' % stamp2str(int(time.time()), formt='%Y-%m-%d %H:%M:%S') html_content = t.render(Context(locals())) sendmail_html(title, html_content, CMDB_CRON_MAILLIST)
class Command(BaseCommand): args = '' help = 'update accident action status per day' def handle(self, *args, **options): error = False message = [] try: action_list = AccidentAction.objects.using('accident').filter( status=1) #进行中的action today = str2stamp(date.today().strftime("%Y-%m-%d"), formt='%Y-%m-%d') for action in action_list: if action.expect_time < today: action.status = 2 action.save() accident_list = Accident.objects.using('accident').filter( is_accident=0, status_id=5) #改进措施进行中的事故列表 for accident in accident_list: cur_actions = AccidentAction.objects.using('accident').filter( accident_id=accident.accidentid) action_status = [c_act.status for c_act in cur_actions] if 2 in action_status: accident.status_id = 6 accident.save() #发送值班日报邮件 lastday_str = stamp2str(today - 86400) try: cur_rota = Rota.objects.using('default').get( promotion=0, duty_domain=DOMAIN_HEAD_ID, duty_date_start__lt=lastday_str, duty_date_end__gte=lastday_str) duty_manager = RotaMan.objects.using('default').get( rota_id=cur_rota.id).man back_duty_manager = RotaBackup.objects.using('default').get( rota_id=cur_rota.id).backup cur_duty_manager = duty_manager.username cur_back_duty_manager = back_duty_manager.username if duty_manager.display_name != '': cur_duty_manager_ch = str(duty_manager.display_name[( duty_manager.display_name.rfind('_') + 1):-1]) else: cur_duty_manager_ch = '无' if back_duty_manager.display_name != '': cur_back_duty_manager_ch = str( back_duty_manager.display_name[( back_duty_manager.display_name.rfind('_') + 1):-1]) else: cur_back_duty_manager_ch = '无' if cur_rota is not None: duty_date_start = cur_rota.duty_date_start.strftime( '%Y-%m-%d %H:%M:%S') duty_date_end = cur_rota.duty_date_end.strftime( '%Y-%m-%d %H:%M:%S') except Exception, e: cur_duty_manager = '' cur_back_duty_manager = '' cur_duty_manager_ch = '' cur_back_duty_manager_ch = '' acc = Accident.objects.using('accident').exclude(status=1).filter( is_accident=0, happened_time__lt=today - 14400, happened_time__gte=today - 100800) accidents = [] for accident in acc: accidents.append({ 'level': accident.get_level_display, 'title': accident.title, 'happened_time': stamp2str(accident.happened_time, formt='%Y-%m-%d %H:%M'), 'finish_time': stamp2str(accident.finish_time, formt='%Y-%m-%d %H:%M'), 'affect': accident.affect, 'reason': accident.reason, 'duty_dept_names': accident.duty_dept_names, 'process': accident.process, 'action': accident.action }) delay_actions = AccidentAction.objects.using('accident').filter( accident_id__gte=2016010101, expect_time__gte=1451577600, status=2) delay_action = [] delay_action_users = [] for da in delay_actions: try: act = Accident.objects.using('accident').get( accidentid=da.accident_id, is_accident=0) except Accident.DoesNotExist: act = None if act: delay_action.append({ 'level': act.get_level_display, 'duty_manager_ch': act.duty_manager_name_ch, 'accidentid': act.accidentid, 'title': act.title, 'action': da.action, 'duty_dept': da.dutydept_name, 'duty_users': da.duty_users, 'expect_time': da.expect_time_format }) for user in da.duty_users.split(','): try: delay_action_users.append( DdUsers.objects.using('default').get( username=user, enable=0).email) except DdUsers.DoesNotExist, DdUsers.MultipleObjectsReturned: error = True message.append( '### duty user mail does not exist: username=%s' % user) t = get_template('mail/accident/daily_on_build.html') html_content = t.render(Context(locals())) cc = ACCIDENT_DAILY_MAIL_CC + list(set(delay_action_users)) # cc = ['*****@*****.**'] try: sendmail_html(u'【值班日报】昨日事故汇总与延迟Action通告', html_content, ACCIDENT_DAILY_MAIL, cc=cc) except Exception, e: error = True message.append('### send mail error: detail: %s' % str(e))
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'