def __init__(self, message, ret):
     self.message_original = message
     self.ret = ret
     self.title = message.get("title", None)
     self.logger = SceneLogFactory(message.get("scene_id", "default"),
                                   __name__)
     self.message = message
Esempio n. 2
0
    def __init__(self, content):
        self.content = content
        self.logger = SceneLogFactory(content.get("scene_id", "default"),
                                      __name__)

        self.mission_connect_cache = ConnectCache(content.get("scene_id"),
                                                  content.get("id"))
Esempio n. 3
0
class BaseChecked(object):
    def __init__(self, content):
        self.content = content
        self.logger = SceneLogFactory(content.get("scene_id", "default"),
                                      __name__)

        self.mission_connect_cache = ConnectCache(content.get("scene_id"),
                                                  content.get("id"))

    def run(self):
        self.logger.info("Mission[%s]: Start check process",
                         self.content.get("title"))
        # 检测参数
        message_parameter_list = [
            "scene_id", "script", "id", "check_type", "first_check_time",
            "target_ip", "checker_ip", "is_polling", "title", "score",
            "is_once", "interval", "checker_port", "scene_name"
        ]
        parameter = parameter_verification(self.content,
                                           message_parameter_list)
        if not parameter:
            self.logger.error("Mission[%s]: Parameter Missing",
                              self.content.get("title"))
            if self.content.get("id"):
                record_status_information(self.content.get("id"),
                                          "Parameter Missing",
                                          MissionStatus.ERROR)
            else:
                self.logger.error("No mission id")

        # 验证脚本是否存在
        script = self.content.get('script')
        script_url = settings.BASE_DIR + "/media/scripts/mission/{mission_id}/{script}".format(
            mission_id=self.content.get("id"), script=script)
        self.logger.debug("Mission[%s]: script path: %s",
                          self.content.get("title"), script_url)
        if not os.path.exists(script_url):
            self.logger.error("Mission[%s] script[%s]: script does not exist",
                              self.content.get("title"), script)

            record_status_information(self.content.get("id"),
                                      "script does not exist",
                                      MissionStatus.ERROR)

        script_client = ScriptClient(self.logger, self.content, script_url)

        # 验证状态
        mission_status = inquire_mission_status(self.content.get('id'))
        if mission_status == MissionStatus.ISPROCESS or mission_status == MissionStatus.DOWN:
            kwargs = self.content.get('kwargs', {})
            self._run(script, script_client, **kwargs)

    def _run(self, script, script_client, **kwargs):
        raise NotImplementedError("implement in sub class")
Esempio n. 4
0
    def create(self, request, *args, **kwargs):

        check_result = request.data
        scene_id = check_result.get("cr_event", None)
        mission_id = check_result.get("machine_id", None)
        result = check_result.get("result", None)

        if mission_id is None or scene_id is None or result is None:
            logger_critical.error(
                "Agent Missing parameter: mission[%s], scene_id[%s], result[%s]",
                mission_id, scene_id, result)
            return Response(status=status.HTTP_200_OK)
        else:
            logger = SceneLogFactory(scene_id, __name__)

            mission = mission_model.Mission.objects.filter(
                id=mission_id).first()
            if mission:
                logger.info("Mission[%s]: Agent check OK", mission.title)
                logger.debug("Mission[%s]:  Check result [%s]", mission.title,
                             result)

                if mission.mission_status == constant.MissionStatus.STOP:
                    record_status_information(
                        mission,
                        "mission is stop",
                    )
                    logger.error("Mission[%s]: mission is stop", mission.title)

                    return Response(status=status.HTTP_200_OK)
                else:
                    try:
                        result = ast.literal_eval(result)
                        self.write_log_table(result, scene_id, mission, logger)
                    except Exception:
                        self.write_log_table(result, scene_id, mission, logger)

            else:
                logger.error("[create]No mission, Id is %s", mission_id)

        return Response(status=status.HTTP_200_OK)
class ResultProcess(object):
    def __init__(self, message, ret):
        self.message_original = message
        self.ret = ret
        self.title = message.get("title", None)
        self.logger = SceneLogFactory(message.get("scene_id", "default"),
                                      __name__)
        self.message = message

    def check_over(self, mission):
        """
        check over do something, Write database,is polling
        :return:
        """
        parameter_list = [
            "scene_id", "script", "id", "check_type", "target_ip",
            "checker_ip", "is_polling", "title", "score", "is_once",
            "interval", "checker_port", "scene_name"
        ]

        self.message = parameter_verification(self.message_original,
                                              parameter_list)

        if not self.message:
            self.logger.error("Parameter Missing")
            # 有问题
            record_status_information(self.message_original.get("id"),
                                      "Parameter Missing", MissionStatus.STOP)
            return

        script = self.message.get('script')
        mission_id = self.message.get("id")

        # 记录状态
        self.logger.info("Mission[%s]: Check status %s, [%s]",
                         self.message.get("title"),
                         self.ret.get("status", None), self.ret.get("content"))

        # Write to the log database
        self.record_results(script)

        # Whether to continue and Write result database
        is_continue = self.polling_results(script)

        self._check_is_continue(mission, is_continue, mission_id, script)

    def polling_results(self, script):
        """
        polling results, judge whether to continue execution
        :param script:
        :return: False:stop polling, True: continue polling
        """
        if self.ret.get("status", None) in ["ok", "up"]:
            results = self.calculation_results(script)

        else:
            results = False
        results_is_poll = self.is_polling(results)
        return results_is_poll

    def is_polling(self, results):
        """
        Judging is to continue
        :param results:
        :return:
        """
        continue_check = True
        stop_check = False

        if results:
            self.record_check_scores()
            if not self.message.get("is_polling"):
                return stop_check
            else:
                return self.message.get("is_polling")
        else:
            self.logger.info("Mission[%s]: Check fail!",
                             self.message.get("title"))

            return continue_check

    def calculation_results(self, script):
        """
        Judge the result of the check
        :param script:
        :return: results(True, False)
        """
        if script.split(".")[-1] == "py":
            results = True if self.ret["content"].get(
                "check") == "success" else False
        else:
            results = True if "CheckUp" in self.ret["content"] else False
        return results

    def record_results(self, script):
        """
        write result in databases(cr_scene_models.CmsTestCheckLog)
        :param script:script name
        :return:
        """
        if self.ret.get("status", None) in ["ok", "up"]:
            results = self.calculation_results(script)

            score = self.message.get("score") if results else 0

        else:
            results = False
            score = 0

        scene_id = self.message.get("scene_id")
        cr_event_scene = cr_scene_models.CrEventScene.objects.filter(
            cr_scene_instance=scene_id).first()
        if cr_event_scene:
            cr_scene_models.CrSceneMissionCheckLog.objects.create(
                mission_id=self.message["id"],
                score=float(self.message["score"]),
                cr_event=cr_event_scene.cr_event
                if cr_event_scene.cr_event else None,
                is_solved=results,
                target_ip=self.message.get('target_ip'),
                script=self.message.get('script'),
            )
        else:
            cr_scene = cr_scene_models.CrScene.objects.filter(
                scene_id=scene_id).first()

            cr_scene_models.CmsTestCheckLog.objects.create(
                mission_id=self.message.get("id"),
                target_ip=self.message.get('target_ip'),
                cr_scene=cr_scene if cr_scene else None,
                is_solved=results,
                score=score,
                script=script,
            )

        self.logger.debug("Mission[%s]: Write data to the log table",
                          self.title)

    def record_check_scores(self):
        """
        write check Grade in databases(cr_scene_models.CmsTestCheckRecord)
        :return:
        """
        scene_id = self.message["scene_id"]
        cr_event_scene = cr_scene_models.CrEventScene.objects.filter(
            cr_scene_instance=scene_id).first()
        if not cr_event_scene:
            record = cr_scene_models.CmsTestCheckRecord.objects.filter(
                mission=self.message.get("id"))
            if not record.exists():
                cr_scene = cr_scene_models.CrScene.objects.filter(
                    scene_id=scene_id).first()

                cr_scene_models.CmsTestCheckRecord.objects.create(
                    mission_id=self.message["id"],
                    score=float(self.message["score"]),
                    cr_scene=cr_scene if cr_scene else None,
                    target_ip=self.message.get('target_ip'),
                    script=self.message.get('script'),
                )
            else:
                _score = float(record.first().score +
                               float(self.message.get("score")))
                record.update(score=_score)

        else:
            record = cr_scene_models.CrSceneEventUserAnswer.objects.filter(
                mission=self.message.get("id"))

            if not record.exists():
                cr_scene_models.CrSceneEventUserAnswer.objects.create(
                    mission_id=self.message["id"],
                    score=float(self.message["score"]),
                    cr_event=cr_event_scene.cr_event
                    if cr_event_scene.cr_event else None,
                )
            else:
                _score = float(
                    record.first().score) + self.message.get("score")
                record.update(score=_score)

        self.logger.debug(
            "Mission[%s]: Check success! Write data"
            "to the result table", self.message.get("title"))
        return True

    def _check_is_continue(self, mission, is_continue, mission_id, script):
        """
        Determine if check functions continues
        :param is_continue: is continue
        :param mission_id: mission id
        :param script: script name
        :return:
        """
        if mission:
            if is_continue:
                mission_status = inquire_mission_status(mission_id)

                if mission_status != MissionStatus.STOP and not self.message.get(
                        "is_once"):
                    self.logger.info("Mission[%s]: Check carry on",
                                     self.message.get("title"))

                    params = mission.checkmission.params if object_attr(
                        mission.checkmission,
                        "params") else self.message.get("params")

                    self.message = kwargs_process(self.message, params)

                    self.message.update({
                        "first_check_time":
                        mission.checkmission.interval
                        if mission.checkmission.interval else 0,
                        "is_once":
                        mission.checkmission.is_once,
                        "is_polling":
                        mission.checkmission.is_polling
                    })

                    start_checker(self.message, self.logger, False)
                else:
                    record_status_information(mission, "Check over",
                                              constant.MissionStatus.STOP)
                    self.logger.info("Mission[%s]: Check over",
                                     self.message.get("title"))
            else:
                record_status_information(mission, "Check over",
                                          constant.MissionStatus.STOP)
                self.logger.info("Mission[%s]: Check over",
                                 self.message.get("title"))
        else:
            self.logger.error("Mission[%s]: Mission id is None",
                              self.message.get("title"))
Esempio n. 6
0
 def __init__(self, mission, cr_scene, scene_id):
     self.mission = mission
     self.cr_scene = cr_scene
     self.scene_id = scene_id
     self.logger = SceneLogFactory(
         self.scene_id, __name__) if scene_id else logger_critical
Esempio n. 7
0
class MissionCheckerManager(object):
    def __init__(self, mission, cr_scene, scene_id):
        self.mission = mission
        self.cr_scene = cr_scene
        self.scene_id = scene_id
        self.logger = SceneLogFactory(
            self.scene_id, __name__) if scene_id else logger_critical

    # no_delay是否有延迟的执行, 为True的时候是立即执行
    def start(self, no_delay=False):
        # 更改mission的状态
        record_status_information(self.mission, "Start up mission check",
                                  constant.MissionStatus.ISPROCESS)

        # 参数处理
        script_data = self._param_check()

        if script_data is None or script_data == {}:
            record_status_information(self.mission,
                                      "Parameter detection does not pass",
                                      constant.MissionStatus.ERROR)
            self.logger.error("Mission[%s]: Parameter detection does not pass",
                              self.mission.title)
        else:
            # 发布任务
            self.logger.info("Mission[%s]: Publish mission",
                             self.mission.title)
            start_checker(script_data, self.logger, no_delay)

    def stop(self):
        # 更改Mission状态是停止
        record_status_information(self.mission, "Has stopped check",
                                  constant.MissionStatus.STOP)

        delete_channel_delay({"id": self.mission.id})
        if self.scene_id:
            ConnectCache(self.scene_id, self.mission.id).blank_cache()

        self.logger.info("Mission[%s]: Has stopped check", self.mission.title)

    def _param_check(self):
        """
        参数验证,是否完整
        :return:
        """
        data = self._data_process()

        self.logger.debug("Mission[%s]: data:[%s]", self.mission.title, data)

        if data == {}:
            self.logger.error("Mission[%s]: missing parameters",
                              self.mission.title)

        return data

    def _data_process(self):
        """
        处理数据,获取所需参数
        :return:
        """
        parameter_list = ["id", "score", "title"]
        check_mission_list = [
            "interval", "is_once", "is_polling", "first_check_time", "scripts",
            "check_type", "checker_id", "target_net", "target"
        ]

        self.logger.debug("Mission[%s]: Data process start",
                          self.mission.title)

        check_mission = object_attr(self.mission, "checkmission")
        obj_data = parameter_verification(self.mission, parameter_list)

        if check_mission.check_type == constant.CheckType.AGENT:
            check_mission_list.remove("checker_id")
            check_mission_list.remove("target_net")

        check_mission_data = parameter_verification(check_mission,
                                                    check_mission_list)

        if {} in [obj_data, check_mission_data]:
            self.logger.error("Mission[%s]: Parameter verification failed",
                              self.mission.title)
            return obj_data

        script = check_mission_data.get("scripts")

        # 获取checker_ip, port, target_ip, 并验证
        target_id = check_mission_data.get("target")
        checker_id = check_mission_data.get("checker_id")
        target_net_id = check_mission_data.get("target_net")

        self.logger.debug(
            "Mission[%s]: target_id[%s], checker_id[%s], target_net_id[%s]",
            self.mission.title, target_id, checker_id, target_net_id)
        if checker_id is None or target_net_id is None:
            checker_ip, port = checker_info.get_checker_ip(
                self.scene_id, target_id)
            target_ip = checker_ip
        else:
            checker_ip, port = checker_info.get_checker_ip(
                self.scene_id, checker_id)
            target_ip = checker_info.get_terminal_ip(self.scene_id, target_id,
                                                     target_net_id)

        self.logger.debug(
            "Mission[%s]: target_ip[%s], checker_ip[%s], port[%s]",
            self.mission.title, target_ip, checker_ip, port)

        if checker_ip is None or port is None:
            self.logger.error(
                "Mission[%s]: get checker ip[%s] or port[%s] error",
                self.mission.title, checker_ip, port)
            return {}

        else:
            if len(checker_ip) == 0 or port == 0:
                self.logger.error(
                    "Mission[%s]: get checker ip[%s] or port[%d] error",
                    self.mission.title, checker_ip, port)
                return {}

        if not ip_verification(checker_ip) or not ip_verification(target_ip):
            self.logger.error(
                "Mission[%s]: target ip[%s] or checker ip[%s] error",
                self.mission.title, target_ip, checker_ip)

            return {}

        data = {
            "scene_id": self.scene_id,
            "scene_name": self.cr_scene.name,
            "checker_ip": checker_ip,
            "target_ip": target_ip,
            "checker_port": port,
            "script": script
        }

        data.update(obj_data)
        data.update(check_mission_data)

        mission_params = object_attr(check_mission, "params")
        if mission_params:
            data = kwargs_process(data, mission_params)

        return data