def request_long(report): # http长请求数(>2s,不含上传操作) exclude_oper = [ 'api_upload_image', 'api_upload_video', 'api_upload_voice', 'api_upload_file', ] today_dt = today(is_str=False) yesterday_dt = yesterday(is_str=False) qs = OperateLog.objects.filter( request_time__gte=yesterday_dt, request_time__lt=today_dt, duration__gte=LONG_REQUEST) \ .exclude(account_id=None).exclude(user_type=None).exclude(user_school_id=None) lines = list() report.writelines([ '\n[http长请求(>2s, 不含上传操作)]: \n', ]) for each in qs: lines.append( u'ID:%d, url:%s, 时长:%d, 请求者:%d_%d_%d, 响应码:%s, c:%s, m:%s, 时间:%s\n' % (each.id, each.url, each.duration, each.account_id, each.user_type, each.user_school_id, each.status_code, each.c, each.m, datetime2str(each.request_time))) report.writelines(lines) return len(lines)
def total_user(): # 总使用人数 today_dt = today(is_str=False) yesterday_dt = yesterday(is_str=False) result = OperateLog.objects.filter(request_time__gte=yesterday_dt, request_time__lt=today_dt) \ .exclude(account_id=None).exclude(user_type=None).exclude(user_school_id=None) \ .values_list("account_id", flat=True).distinct().count() return result
def total_request(): # http所有请求数 today_dt = today(is_str=False) yesterday_dt = yesterday(is_str=False) result = OperateLog.objects.filter(request_time__gte=yesterday_dt, request_time__lt=today_dt) \ .exclude(account_id=None).exclude(user_type=None).exclude(user_school_id=None) \ .count() return result
def daily_report(): report_path = os.path.join( settings.BASE_DIR, 'temp', 'report_%s_%s.log' % (yesterday(), settings.SYSTEM_DESC)) mail_title = u'昨日(%s)%s运行情况' % (yesterday(), settings.SYSTEM_DESC) mail_content = mail_title + '\n\n' with open(report_path, 'w+') as report: # 前一天总用户数 totaluser = total_user() # 前一天总http请求数 totalrequest = total_request() # 前一天总http请求数(非安全操作) totalrequestunsafe = total_request_unsafe() # http请求非200响应 requestnon200 = request_non_200(report) # http请求500响应 request500 = request_500(report) # http请求"200"且包含非0内部状态码 request200witherror = request_200_with_error(report) # http长请求 requestlong = request_long(report) # 日志中异常ERROR数量 reportlogerror = report_log_error(report) send_mail(mail_title, mail_content, report_path, totaluser, totalrequest, totalrequestunsafe, requestnon200, request500, request200witherror, requestlong, reportlogerror, '', '', yesterday())
def request_500(report): # http请求"500"响应数, 详情见邮件附件 today_dt = today(is_str=False) yesterday_dt = yesterday(is_str=False) qs = OperateLog.objects.filter( request_time__gte=yesterday_dt, request_time__lt=today_dt, status_code='500') \ .exclude(account_id=None).exclude(user_type=None).exclude(user_school_id=None) lines = list() report.writelines([ '\n[http请求"500"响应]: \n', ]) for each in qs: lines.append(u'ID:%d, url:%s, 请求者:%d_%d_%d, c:%s, m:%s, 时间:%s\n' % (each.id, each.url, each.account_id, each.user_type, each.user_school_id, each.c, each.m, datetime2str(each.request_time))) report.writelines(lines) return len(lines)
def _report_single_log_error(log_path, report_file): inner_count = 0 yesterday_str = yesterday() with open(log_path, 'r') as f: err_bucket = list() err_flag = False for line in f: if line.startswith(yesterday_str): if err_flag: report_file.writelines(err_bucket) err_flag = False err_bucket[:] = [] if '[ERROR]' in line: inner_count += 1 err_flag = True err_bucket.append(line) else: if err_flag: err_bucket.append(line) return inner_count