使用pymongo校验mongo表数据正确性
背景
当一个事件表中有大量告警时,需要一种方法能校验事件、子事件、告警数据的正确性。
采用 python + pymongo 连接到mongo库,并执行一些sql操作。
相关资料
安装准备
安装pymongo组件
在ubuntu上执行命令:
sudo apt install python3-pip sudo pip3 install pymongo
注意:这里是采用python3
python脚本
创建一个python文件(如:checkIncident.py):
#!/usr/bin/python3 #_*_encoding:utf-8_*_ # -*- coding: utf-8 -*- """ 校验事件、子事件数据的正确性。校验项包括: 1、事件的lastDetectionTime、score、severity,是否和最近一条子事件的这几个字段相等 2、事件的lastDetectionTime是否和告警表排序后的detectionTime一致 3、事件中的告警个数,是否和子事件中的告警个数应该一致 4、事件中的告警个数,是否和告警表对应incidentId的告警个数相等 5、事件中的告警id,是否都在告警中 """ import sys import pymongo mogoclient = pymongo.MongoClient("mongodb://qingteng:q4zek6KLAdA7nH5I@172.16.17.223:27017/") db_detect = mogoclient["wisteria_detect"] def validate(comId, incidentId): #获取到collection对象 ids_incident_coll = db_detect.ids_incident ids_sub_incident_coll = db_detect.ids_sub_incident ids_detection_coll = db_detect.ids_detection #查询sql incident_doc = ids_incident_coll.find({"comId": comId, "incidentId": incidentId}).limit(1) sub_incident_doc = ids_sub_incident_coll.find({"comId": comId, "incidentId": incidentId}).sort("lastDetectionTime", -1) detection_doc = ids_detection_coll.find({"comId": comId, "incidentId": incidentId}).sort("detectionTime", -1) #事件的lastDetectionTime、score、severity,是否和最近一条子事件的这几个字段一致 #事件的lastDetectionTime是否和告警表排序后的detectionTime一致 last_detection_time = incident_doc[0]["lastDetectionTime"] last_sub_incident_doc = sub_incident_doc[0] if last_detection_time != last_sub_incident_doc["lastDetectionTime"]: print("lastDetectionTime error, not pass") return if incident_doc[0]["score"] != last_sub_incident_doc["score"]: print("score error, not pass") return if incident_doc[0]["severity"] != last_sub_incident_doc["severity"]: print("score error, not pass") return if last_detection_time != detection_doc[0]["detectionTime"]: print("detectionTime error, not pass") return incident_detections = incident_doc[0]["detections"] incident_detection_count = len(incident_detections) sub_incident_detection_count = 0 for sub_incident in sub_incident_doc: sub_incident_detections = sub_incident["detections"] sub_incident_detection_count += len(sub_incident_detections) print("incident.detection.count: %d" % incident_detection_count) print("sub_incident.detection.count: %d" % sub_incident_detection_count) #事件中的告警个数,是否和子事件中的告警个数应该一致 if incident_detection_count != sub_incident_detection_count: print("incident.detection != sub_incident.detection, not pass") return detection_count = 0 for detection in detection_doc: detection_count += 1 print("detection.count: %d" % detection_count) #事件中的告警个数,是否和告警表对应incidentId的告警个数相等 if incident_detection_count != detection_count: print("incident.detection != detection_count, not pass") return #事件中的告警id,是否都在告警中 incident_detection_ids = {} incident_detections = incident_doc[0]["detections"] for detection in incident_detections: detectionId = detection["detectionId"] incident_detection_ids[detectionId] = detectionId for detection in detection_doc: detectionId = detection["detectionId"] if detectionId not in incident_detection_ids: print("detection is invalid, not pass, detectionId: %s" % detectionId) return print("pass") if __name__ == "__main__": incidentId = sys.argv[1] validate("4a504f6a3765654d654a", incidentId)
- 如果是连别的环境,需要修改密码和IP地址
- 脚本中的comId是变量,需要手动修改
执行脚本
执行python命令:
python3 checkIncident.py [incidentId]
知识改变世界