Exemplo n.º 1
0
    async def update_specific_config(self, filename: str):
        """Reads the provided config file and updates the interested monitors/scrapers"""
        self.general_logger.debug(f"File {filename} has changed!")
        try:
            with open(filename, "r") as f:
                j = json.load(f)
        except JSONDecodeError:
            self.general_logger.warning(
                f"File {filename} has changed but contains invalid json data")
            return

        splits = filename.split(os.path.sep)
        commands = []  # List[Cmd]
        sock_paths = []  # type: List[str]

        # if it's from the monitors folder:
        if "monitors" in filename.split(os.path.sep):
            sockets = self.monitor_sockets
        elif "scrapers" in filename.split(os.path.sep):
            sockets = self.scraper_sockets
        else:
            self.general_logger.debug("File not useful.")
            return

        # we are interested in configs, whitelists, blacklists, webhooks
        if splits[-1] == "whitelists.json":
            cmd = COMMANDS.SET_SPECIFIC_WHITELIST
        elif splits[-1] == "configs.json":
            cmd = COMMANDS.SET_SPECIFIC_CONFIG
        elif splits[-1] == "blacklists.json":
            cmd = COMMANDS.SET_SPECIFIC_BLACKLIST
        elif splits[-1] == "webhooks.json":
            cmd = COMMANDS.SET_SPECIFIC_WEBHOOKS
        else:
            return

        # for every monitor socket
        for name in sockets:
            if name in j:
                sock_path = sockets[name]
                c = Cmd()
                c.cmd = cmd
                # send only the corresponding part to the monitor
                c.payload = j[name]
                commands.append(c)
                sock_paths.append(sock_path)

        # prepare to make all the async requests
        tasks = []
        for sock_path, command in zip(sock_paths, commands):
            tasks.append(self.make_request(sock_path, command))

        # send the requests
        responses = await asyncio.gather(*tasks)  # List[Response]

        for response in responses:
            if response.error.value:
                self.general_logger.warning(
                    f"Failed to update config: {response.error}")
Exemplo n.º 2
0
    async def delete(self):
        cmd = Cmd()
        cmd.cmd = COMMANDS.MM_STOP_MONITOR_SCRAPER
        cmd.payload = json.loads(self.request.body)
        r = await send_to_moman(cmd)

        if not r.error.value:
            self.set_status(400, r.reason)
        self.write(r.get_json())
Exemplo n.º 3
0
    async def post(self):
        cmd = Cmd()
        cmd.cmd = COMMANDS.MM_ADD_MONITOR
        cmd.payload = json.loads(self.request.body)
        r = await send_to_moman(cmd)

        if r.error.value:
            self.set_status(400, r.reason)
        self.write(r.get_json())
Exemplo n.º 4
0
            try:
                COMMANDS[key]
                print(key)
            except:
                pass
        exit(0)
    cmd = COMMANDS.__dict__.get(args[1], None)
    if cmd is None:
        try:
            cmd = int(args[1])
        except:
            print("cmd is not a valid COMMANDS nor an int.")
            exit(1)
    string_cmd = args[1]
    payload = {}
    if len(args) > 2:
        if not len(args) % 2:
            for index, term in enumerate(args[2:]):
                if not index % 2:
                    if not term.startswith("--"):
                        print('You must start every payload key with "--"')
                        exit(4)
                    payload[term[2:]] = args[3 + index]
        else:
            print("Incorrect number of payload options!")
            exit(3)
    command = Cmd()
    command.cmd = cmd
    command.payload = payload
    send(command)