parent
3e379403df
commit
b97f549d89
@ -0,0 +1,210 @@ |
||||
# coding:utf-8 |
||||
|
||||
import sys |
||||
import uuid |
||||
import json |
||||
import time |
||||
import random |
||||
|
||||
path = str(sys.path[0]) |
||||
home_path = path.split("isop_uebaapiData")[0] |
||||
sys.path.append(home_path) |
||||
from isop_uebaapiData.util import send_logs |
||||
|
||||
def alarm(cookies, api): |
||||
"""2、HTTP日志""" |
||||
inputstr = '''[{"msgtype":1,"hash":"8DE9-BDAB-F622-2FA8","dev_ip":"10.67.5.17","product":"uts"},{"sid":"6004744450036c44f815500016d00a5f5151105430a3ed","timestamp":1567673939,"sip":"10.67.0.52","sport":5624,"dip":"10.67.0.53","dport":80,"protocol":6,"app":3087428650795009,"app_proto":8,"direct":4,"app.detail":{"method":"GET","http_protocol":"1.1","ret_code":200,"host":"10.67.1.1","uri":"/webtest/uploadFile.php","referer":"http://[2222::65]/webtest/","content_type":" multipart/form-data; boundary=----WebKitFormBoundary2zcCUl4lQf1h7A7S","content_type_server":" text/html","server":"Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.19","user_agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36","link":"","cookies":"loginmainacctid=wangshiguang;operatorId=d2601586;com.huawei.boss.CURRENT_MENUID=BLAR_ChargeCrm3_WEB;","content_encoding":"","location":"","content_length":70080,"content_length_server":200,"set_cookie":"","range":"","connection":"keep-alive","connection_server":"Keep-Alive","x_forwarded_for":"","post_data":"LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5MnpjQ1VsNGxRZjFoN0E3Uw0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmaWxlIjsgZmlsZW5hbWU9IjAwMDFhZDQ0MWRjYjM4NjIxOGE3Njk5MmFjZjhiNzA1Ig0=","response_body":"VXBsb2FkOiAwMDAxYWQ0NDFkY2IzODYyMThhNzY5OTJhY2Y4YjcwNTxiciAvPlR5cGU6IGFwcGxpY2F0aW9uL29jdGV0LXN0cmVhbTxiciAvPlNpemU6IDY4LjEyNzkyOTY4NzUgS2I8YnIgLz5UZW1wIGZpbGU6IEQ6XHhhbXBwXHRtcFxwaHA2ODI1LnRtcDxiciAvPjAwMDFhZDQ0MWRjYjM4NjIxOGE3Njk5MmFjZjhiNzA1IGFscmVhZHkgZXhpc3RzLiA="}}]''' |
||||
inputarr = json.loads(inputstr, strict=False) |
||||
# 随机生成timestamp |
||||
inputarr[1]["timestamp"] = 1720782138 |
||||
inputarr[1]["sid"] = str(uuid.uuid1()) |
||||
# inputarr[1]["sip"] = "10.67.4.33" |
||||
inputarr[1]["sip"] = generate_random_ip() |
||||
inputarr[1]["dip"] = "10.67.1.1" |
||||
inputarr[1]["dport"] = "8180" |
||||
inputarr[1]["app.detail"]["uri"] = "/alarmtest.action?BMECID=352432757&BMETimestamp=1692788489260&queryNumber=158713459" |
||||
inputarr[1]["app.detail"]["host"] = api |
||||
inputarr[1]["app.detail"]["cookies"] = cookies |
||||
return json.dumps(inputarr) |
||||
|
||||
def generate_random_ip(): |
||||
# 固定前缀 "192.168." |
||||
prefix = "192.168." |
||||
# 生成随机的第三和第四段IP地址 |
||||
third_octet = random.randint(0, 255) |
||||
fourth_octet = random.randint(0, 255) |
||||
# 拼接IP地址 |
||||
ip = "{}{}.{}".format(prefix, third_octet, fourth_octet) |
||||
return ip |
||||
|
||||
def AbIDVisitAPINums510404(): |
||||
datalist = {"TCP_5011": list()} |
||||
ID2Area = { |
||||
"武汉": ["1101820", "1101821", "1101822", "1101823", "1101825"], |
||||
"荆州": ["2001800", "2001801", "2001808"], |
||||
"江汉": ["1801820", "1801810"], |
||||
"省公司市场部": ["1002011", "1002012", "1002013"] |
||||
} |
||||
api_list = ["test.alarm.com/webtest", "alarm.com/testalarm", "business.system..alarmcom", "hhh.alarm.com", |
||||
"good.alarm.com"] |
||||
info_list = [ |
||||
["u-locale=zh_CN;loginmainacctid=zhang3;operatorId=" + ID2Area["武汉"][ |
||||
0] + ";com.huawei.boss.CURRENT_MENUID=BLAR_ChargeCrm3_WEB;", 60], |
||||
["u-locale=zh_CN;loginmainacctid=zhang3;operatorId=" + ID2Area["荆州"][ |
||||
2] + ";com.huawei.boss.CURRENT_MENUID=BLAR_ChargeCrm3_WEB;", 120] |
||||
] |
||||
for i in range(len(info_list)): |
||||
cookies = info_list[i][0] |
||||
count = info_list[i][1] |
||||
for j in range(count): |
||||
api = random.choice(api_list) |
||||
datalist["TCP_5011"].append(alarm(cookies, api)) |
||||
for key in datalist.keys(): |
||||
send_logs(datalist[key]) |
||||
return "510405场景的告警数据已生成" |
||||
|
||||
def get_random_jobnum(): |
||||
# 定义包含不同前缀的字符串数组 |
||||
prefix_strings = [ |
||||
['10243', '10895', '10134', '10781', '10962'], # 10打头的字符串示例 |
||||
['11089', '11057', '11023', '11016', '11030'], # 110打头的字符串示例 |
||||
['14076', '14049', '14098', '14032', '14061'], # 140打头的字符串示例 |
||||
['26054', '26013', '26087', '26029', '26061'], # 260打头的字符串示例 |
||||
['20083', '20015', '20072', '20096', '20048'], # 200打头的字符串示例 |
||||
['19035', '19017', '19049', '19082', '19096'], # 190打头的字符串示例 |
||||
['180237', '180276', '180204', '180295', '180219'] # 1802打头的字符串示例 |
||||
] |
||||
|
||||
# 随机选择一个前缀数组 |
||||
selected_prefix_array = random.choice(prefix_strings) |
||||
# 随机选择一个具体的字符串 |
||||
selected_string = random.choice(selected_prefix_array) |
||||
return selected_string |
||||
|
||||
def get_random_person(): |
||||
people_list = [ |
||||
"Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace", "Henry", "Isabel", "Jack", |
||||
"Kate", "Liam", "Mia", "Noah", "Olivia", "Patrick", "Quinn", "Rachel", "Samuel", "Taylor", |
||||
"Ursula", "Victor", "Wendy", "Xavier", "Yvonne", "Zachary", "Amelia", "Benjamin", "Catherine", |
||||
"Daniel", "Ella", "Finn", "Gabriella", "Hugo", "Isabella", "Jacob", "Katherine", "Lucas", |
||||
"Madeline", "Nathan", "Olivia", "Peter", "Quincy", "Riley", "Sophia", "Thomas", "Uma", |
||||
"Vincent", "Willa", "Xander", "Yasmine", "Zoe", "Aaron", "Bella", "Connor", "Daisy", "Ethan", |
||||
"Freya", "George", "Hannah", "Isaac", "Julia", "Kevin", "Lily", "Matthew", "Nora", "Owen", |
||||
"Penelope", "Quentin", "Rebecca", "Samantha", "Tristan", "Ursula", "Violet", "Wyatt", "Ximena", |
||||
"Yara", "Zane", "Anna", "Blake", "Charlotte", "David", "Eva", "Felix", "Grace", "Hector", |
||||
"Ivy", "James", "Kylie", "Luna", "Milo", "Natalie", "Oscar", "Paige", "Quinn", "Ruby", |
||||
"Simon", "Tessa", "Uriel", "Victoria", "Wesley", "Xavier", "Yasmine", "Zara" |
||||
# 继续添加更多的名称... |
||||
] |
||||
|
||||
random_person = random.choice(people_list) |
||||
return random_person |
||||
|
||||
def get_random_menu(): |
||||
# 定义系统菜单列表 |
||||
system_menu = [ |
||||
"主页", "设置", "个人资料", "消息", "通知", "帮助", "帐户", "关于", "联系我们", "服务", |
||||
"购物车", "订单", "支付", "地址", "密码", "登出", "登入", "注册", "搜索", "反馈", |
||||
"隐私政策", "条款与条件", "FAQ", "文档", "论坛", "博客", "新闻", "视频", "图片", "音频", |
||||
"下载", "上传", "社交", "分享", "喜欢", "收藏", "评论", "点赞", "订阅", "播放列表", |
||||
"播放历史", "推荐", "推广", "活动", "招聘", "加入我们", "团队", "合作伙伴", "协议", |
||||
"项目", "贡献", "捐赠", "赞助", "开发", "设计", "产品", "技术支持", "客户支持", |
||||
"销售", "市场营销", "业务", "管理", "数据", "分析", "报告", "绩效", "策略", |
||||
"创新", "优化", "测试", "安全", "备份", "恢复", "更新", "版本", "发布", |
||||
"变更日志", "许可证", "授权", "注册码", "订购", "付款方式", "配置", "设置向导", "快捷方式", |
||||
"自定义", "调整", "模板", "样式", "主题", "颜色", "字体", "大小", "布局", |
||||
"格式", "检查更新", "下载中心", "资源", "链接", "网站地图", "计划", "时间表", "日历", |
||||
"事件", "提醒", "警报", "通讯录", "联系人", "目录", "分类", "标签", "搜索结果", |
||||
"页面", "文章", "产品", "服务", "项目", "案例", "作品", "示例", "演示", |
||||
"展示", "参考", "指南", "教程", "培训", "学习", "认证", "证书", "奖章", |
||||
"徽章", "勋章", "成就", "积分", "排名", "比赛", "竞赛", "评估", "评价", |
||||
"考核", "调查", "研究", "分析", "文章", "书籍", "参考文献", "论文", "报告", |
||||
"期刊", "杂志", "图书馆", "书架", "存档", "档案", "历史", "数据", "统计", |
||||
"指标", "指数", "系列", "序列", "集合", "列表", "图表", "图形", "统计", |
||||
"数字", "计数", "数量", "比率", "百分比", "概述", "汇总", "详情", "全球", |
||||
"国家", "地区", "城市", "位置", "地点", "位置", "方向", "距离", "路线", |
||||
"导航", "地图", "位置", "坐标", "GPS", "导航", "位置", "追踪", "监控", |
||||
"控制台", "管理面板", "仪表板", "仪表盘", "仪表板", "仪表盘", "指示灯", "信号", "状态", |
||||
"进度", "完成", "处理", "操作", "任务", "流程", "工作流", "记录", "日志", |
||||
"日志", "评论", "反馈", "意见", "建议", "建议", "改进建议", "问题", "解决方案", |
||||
"答案", "解释", "说明", "描述", "详情", "信息", "数据", "内容", "媒体", |
||||
"文档", "文件", "附件", "图像", "图片", "照片", "图表", "图形", "表格", |
||||
"表单", "输入", "输出", "导入", "导出", "分享", "链接", "电子邮件", "消息", |
||||
"聊天", "对话", "会话", "会议", "通话", "视频", "音频", "音乐", "歌曲", |
||||
"播放", "暂停", "停止", "跳过", "前进", "回放", "录制", "编辑", "剪辑", |
||||
"修剪", "调整", "滤镜", "效果", "转换", "格式", "编码", "解码", "播放器", |
||||
"播放列表", "收藏夹", "书签", "标签", "标签", "评论", "反馈", "评分", "评级", |
||||
"排名", "推荐", "推广", "广告", "宣传", "促销", "优惠", "折扣", "优惠券", |
||||
"礼品卡", "优惠码", "资料", "信息", "内容", "资源", "资产", "库存", "存储", |
||||
"存储", "备份", "还原", "升级", "更新", "版本", "修复", "修复", "故障", |
||||
"错误", "问题", "错误", "故障", "问题", "警告", "异常", "异常", "异常", |
||||
"重试", "恢复", "恢复", "取消", "撤销", "回滚", "复制", "粘贴", "剪切", |
||||
"移动", "重命名", "删除", "清除", "清理", "清除", "清理", "优化", "优化", |
||||
"增加", "增强", "强化", "加强", "改进", "改善", "优化", "优化", "设计", |
||||
"开发", "测试", "部署", "配置", "设置", "安装", "卸载", "升级", "更新", |
||||
"修复", "修正", "修补", "更新", "安全", "保护", "防护", "防御", "防止", |
||||
"检查", "扫描", "监控", "跟踪", "追踪", "审计", "审查", "测试", "测量" |
||||
] |
||||
|
||||
# 随机选择一个菜单项 |
||||
random_menu_item = random.choice(system_menu) |
||||
return random_menu_item |
||||
|
||||
if __name__ == '__main__': |
||||
datalist = {"TCP_5011": list()} |
||||
ID2Area = { |
||||
"武汉": ["1101820", "1101821", "1101822", "1101823", "1101825"], |
||||
"荆州": ["2001800", "2001801", "2001808"], |
||||
"江汉": ["1801820", "1801810"], |
||||
"省公司市场部": ["1002011", "1002012", "1002013"] |
||||
} |
||||
api_list = ["test.alarm.com/webtest", "alarm.com/testalarm", "business.system..alarmcom", "hhh.alarm.com", "good.alarm.com","baidu.com","sohu.com","xinlang.com","erpx.com"] |
||||
info_list = [ |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 1000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 5000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 5000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 5000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 2000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 2000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 2000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 2000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 3000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 1000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 1000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 1000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 1000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 1000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 1000], |
||||
["u-locale=zh_CN; loginmainacctid="+get_random_person()+"; userticket=209@9889@23223@10.0.1.183@lis8; operatorId=" + get_random_jobnum() + "; com.huawei.boss.CURRENT_MENUID="+get_random_menu()+";", 1000] |
||||
] |
||||
|
||||
|
||||
for i in range(len(info_list)): |
||||
cookies = info_list[i][0] |
||||
count = info_list[i][1] |
||||
for j in range(count): |
||||
api = random.choice(api_list) |
||||
datalist["TCP_5011"].append(alarm(cookies, api)) |
||||
for key in datalist.keys(): |
||||
send_logs(datalist[key]) |
||||
print "510405场景的告警数据已生成" |
@ -1,18 +0,0 @@ |
||||
#encoding=utf-8 |
||||
import json |
||||
|
||||
from isoc.utils.esUtil import EsUtil |
||||
|
||||
|
||||
def createIndex(): |
||||
map={ |
||||
"field1": "text", |
||||
"field2": "text" |
||||
} |
||||
es_util_instance = EsUtil() |
||||
res = es_util_instance.create_index_simple("bsa_traffic*",3,scroll_search) |
||||
return res |
||||
|
||||
res = createIndex() |
||||
|
||||
print(res) |
@ -0,0 +1,129 @@ |
||||
#!/usr/bin/python |
||||
#encoding=utf-8 |
||||
# author: tangwy |
||||
import re,os,json |
||||
from db2json import DBUtils |
||||
from datetime import datetime, timedelta |
||||
from ext_logging import logger_cron,get_clean_file_path |
||||
date_pattern = re.compile(r'\d{4}-\d{2}-\d{2}') |
||||
|
||||
DATA_TYPE = { |
||||
"IP": 1, |
||||
"ACCOUNT": 2, |
||||
"INTERFACE": 3, |
||||
"MENU": 4, |
||||
} |
||||
|
||||
def get_all_files(path): |
||||
# 列出所有包含匹配模式的文件名 |
||||
files = [] |
||||
for filename in os.listdir(path): |
||||
if date_pattern.search(filename): |
||||
files.append({"filename": filename, "path": os.path.join(path,filename)}) |
||||
return files |
||||
|
||||
def insert_data(files): |
||||
for item in files: |
||||
if os.path.exists(item.get("path",'')): |
||||
with open(os.path.join(item.get('path','')), 'r') as file: |
||||
data = json.load(file) |
||||
|
||||
ip_list = data.get('ip', []) |
||||
account_list = data.get('account', []) |
||||
interface_list = data.get('interface', []) |
||||
menu_list = data.get('menu', []) |
||||
|
||||
logger_cron.info("INSERT: IP维度 " +str(len(ip_list))) |
||||
logger_cron.info("INSERT: ACCOUNT维度 " +str(len(account_list))) |
||||
logger_cron.info("INSERT: INTERFACE维度 " +str(len(interface_list))) |
||||
logger_cron.info("INSERT: MENU维度 " +str(len(menu_list))) |
||||
|
||||
basename, extension = os.path.splitext(item.get('filename', '')) |
||||
log_date = basename |
||||
print ("filename:"+log_date) |
||||
records = [] |
||||
for item in ip_list: |
||||
menu = item.get('menu', '') |
||||
ip = item.get('ip', '0.0.0.0') |
||||
account = item.get('account', '') |
||||
jobnum = item.get('jobnum', '') |
||||
count = item.get('count', 0) |
||||
logdate = log_date |
||||
datatype = DATA_TYPE.get("IP",1) |
||||
interface = item.get('interface', '') |
||||
records.append((menu, ip, account, jobnum, count, logdate,datatype,interface)) |
||||
for item in account_list: |
||||
menu = item.get('menu', '') |
||||
ip = item.get('ip', '0.0.0.0') |
||||
account = item.get('account', '') |
||||
jobnum = item.get('jobnum', '') |
||||
count = item.get('count', 0) |
||||
logdate = log_date |
||||
datatype = DATA_TYPE.get("ACCOUNT",2) |
||||
interface = item.get('interface', '') |
||||
records.append((menu, ip, account, jobnum, count, logdate,datatype,interface)) |
||||
for item in interface_list: |
||||
menu = item.get('menu', '') |
||||
ip = item.get('ip', '0.0.0.0') |
||||
account = item.get('account', '') |
||||
jobnum = item.get('jobnum', '') |
||||
count = item.get('count', 0) |
||||
logdate = log_date |
||||
datatype = DATA_TYPE.get("INTERFACE",3) |
||||
interface = item.get('interface', '') |
||||
records.append((menu, ip, account, jobnum, count, logdate,datatype,interface)) |
||||
for item in menu_list: |
||||
menu = item.get('menu', '') |
||||
ip = item.get('ip', '0.0.0.0') |
||||
account = item.get('account', '') |
||||
jobnum = item.get('jobnum', '') |
||||
count = item.get('count', 0) |
||||
logdate = log_date |
||||
datatype = DATA_TYPE.get("MENU",4) |
||||
interface = item.get('interface', '') |
||||
records.append((menu, ip, account, jobnum, count, logdate,datatype,interface)) |
||||
# 构建SQL插入语句 |
||||
sql = "INSERT INTO ueba_analysis_schema.logs (menu, ip, account, jobnum, count, logdate,data_type,interface) VALUES " |
||||
values = ["('%s', '%s', '%s', '%s', %d, '%s','%s','%s')" % (menu, ip, account, jobnum, count, logdate,datatype,interface) |
||||
for menu, ip, account, jobnum, count, logdate,datatype,interface in records] |
||||
sql += ",".join(values) |
||||
|
||||
logger_cron.info("INSERT: 准备数据插入") |
||||
DBUtils.execute(sql) |
||||
logger_cron.info("INSERT: 数据插入完成") |
||||
|
||||
#重命名文件 |
||||
os.rename(item.get('path',''),"done_"+item.get('path','')) |
||||
logger_cron.info("INSERT: 重命名文件完成,"+item.get('path','')) |
||||
|
||||
def delete_files(directory_path): |
||||
""" |
||||
删除指定目录下所有形如 'done_YYYY-MM-DD' 的文件, |
||||
其中日期部分早于10天以前。 |
||||
|
||||
:param directory_path: 要检查的目录的绝对路径 |
||||
""" |
||||
# 计算10天前的日期 |
||||
ten_days_ago = datetime.now() - timedelta(days=10) |
||||
ten_days_ago_str = ten_days_ago.strftime('%Y-%m-%d') |
||||
|
||||
# 正则表达式模式,匹配形如 YYYY-MM-DD 的文件名 |
||||
date_pattern = re.compile(r'done_(\d{4}-\d{2}-\d{2})') |
||||
|
||||
# 遍历目录中的文件 |
||||
for filename in os.listdir(directory_path): |
||||
match = date_pattern.search(filename) |
||||
if match: |
||||
file_date_str = match.group(1) |
||||
file_date = datetime.strptime(file_date_str, '%Y-%m-%d') |
||||
|
||||
# 检查文件日期是否在10天前 |
||||
if file_date <= ten_days_ago: |
||||
file_path = os.path.join(directory_path, filename) |
||||
os.remove(file_path) |
||||
logger_cron.info("INSERT: 删除文件"+file_path) |
||||
|
||||
def entry(): |
||||
files = get_all_files(get_clean_file_path()) |
||||
insert_data(files) |
||||
delete_files() |
Loading…
Reference in new issue