Esempio n. 1
0
    async def make_request(self,
                           socket_path: str,
                           cmd: Cmd,
                           expect_response: bool = True) -> Response:
        if os.path.exists(socket_path):
            try:
                reader, writer = await asyncio.open_unix_connection(socket_path
                                                                    )
                writer.write(cmd.get_bytes())
                writer.write_eof()

                if expect_response:
                    r = Response(await reader.read())

                    writer.close()
                    return r
                return okResponse()
            except ConnectionRefusedError:
                self.server_logger.exception(
                    f"Couldn't connect to socket {socket_path}")
                r = badResponse()
                r.error = ERRORS.SOCKET_COULDNT_CONNECT
                return r
        r = badResponse()
        r.error = ERRORS.SOCKET_DOESNT_EXIST
        return r
Esempio n. 2
0
 async def specific_config_setter(self, cmd: Cmd, filename: str,
                                  is_monitor: bool):
     success, missing = cmd.has_valid_args(self.setter_args)
     if success:
         payload = cast(Dict[str, Any], cmd.payload)
         cp = os.path.sep.join((
             self.config["GlobalConfig"]["config_path"],
             "monitors" if is_monitor else "scrapers",
             filename,
         ))
         with open(
                 cp,
                 "r",
         ) as rf:
             f = json.load(rf)
         f[payload["name"]] = payload["payload"]
         with open(
                 cp,
                 "w",
         ) as wf:
             json.dump(f, wf)
     else:
         r = badResponse()
         r.error = ERRORS.MISSING_PAYLOAD_ARGS
         r.info = f"{missing}"
         return r
Esempio n. 3
0
 async def on_stop_scraper(self, cmd: Cmd) -> Response:
     r = badResponse()
     success, missing = cmd.has_valid_args(self.stop_args)
     if success:
         payload = cast(Dict[str, Any], cmd.payload)
         socket = f"{self.config['GlobalConfig']['socket_path']}/Scraper.{payload['name']}"
         command = Cmd()
         command.cmd = COMMANDS.STOP
         self.general_logger.debug(f"Sending STOP to {socket}...")
         r = await self.make_request(socket, command)
         self.general_logger.debug(f"Sent STOP to {socket}")
     else:
         r.error = ERRORS.MISSING_PAYLOAD_ARGS
         r.info = f"Missing arguments: {missing}"
     return r
Esempio n. 4
0
 async def on_get_monitor_shoes(self, cmd: Cmd) -> Response:
     success, missing = cmd.has_valid_args(self.getter_args)
     if success:
         payload = cast(Dict[str, Any], cmd.payload)
         c = Cmd()
         c.cmd = COMMANDS.SET_SHOES
         r = await self.make_request(
             f"{self.config['GlobalConfig']['socket_path']}/Monitor.{payload['name']}",
             c,
         )
         return r
     else:
         r = badResponse()
         r.error = ERRORS.MISSING_PAYLOAD_ARGS
         r.info = f"{missing}"
         return r
Esempio n. 5
0
 async def specific_config_getter(self, cmd: Cmd, command: COMMANDS,
                                  is_monitor: bool):
     success, missing = cmd.has_valid_args(self.getter_args)
     if success:
         payload = cast(Dict[str, Any], cmd.payload)
         c = Cmd()
         c.cmd = command
         r = await self.make_request(
             f"{self.config['GlobalConfig']['socket_path']}/{'Monitor' if is_monitor else 'Scraper'}.{payload['name']}",
             c,
         )
         return r
     else:
         r = badResponse()
         r.error = ERRORS.MISSING_PAYLOAD_ARGS
         r.info = f"{missing}"
         return r
Esempio n. 6
0
 async def on_stop_monitor_scraper(self, cmd: Cmd) -> Response:
     r = badResponse()
     success, missing = cmd.has_valid_args(self.stop_args)
     if success:
         r1, r2 = await asyncio.gather(self.on_stop_monitor(cmd),
                                       self.on_stop_scraper(cmd))
         r.error = (ERRORS.OK if not r1.error.value and not r2.error.value
                    else ERRORS.MM_COULDNT_STOP_MONITOR_SCRAPER)
         r.info = f"Monitor: {r1.error.name}, Scraper: {r2.error.name}"
         if r.error.value and r.error:
             self.general_logger.warning(
                 f"Couldn't stop monitor and scraper")
             kekmonitors.utils.tools.dump_error(self.general_logger, r)
     else:
         r.error = ERRORS.MISSING_PAYLOAD_ARGS
         r.info = f"Missing arguments: {missing}"
     return r
Esempio n. 7
0
 async def on_add_monitor(self, cmd: Cmd) -> Response:
     r = badResponse()
     success, missing = cmd.has_valid_args(self.add_args)
     if success:
         payload = cast(Dict[str, Any], cmd.payload)
         db_monitor = self.register_db["monitors"].find_one(
             {"name": payload["name"]})
         if db_monitor:
             success, reason = await self.add_monitor(
                 db_monitor["path"], payload)
             if success:
                 r = okResponse()
             else:
                 r.error = ERRORS.MM_COULDNT_ADD_MONITOR
                 r.info = reason
         else:
             r.error = ERRORS.MONITOR_NOT_REGISTERED
             r.info = f"Tried to add monitor {payload['name']} but it was not found in the db. Did you start it at least once manually?"
     else:
         r.error = ERRORS.MISSING_PAYLOAD_ARGS
         r.info = f"Missing arguments: {missing}"
     return r
Esempio n. 8
0
async def make_request(socket_path: str,
                       cmd: Cmd,
                       expect_response=True) -> Response:
    """
    Send `cmd` to `socket_path` and return the response if `expect_response` is True, else `okResponse()`.
    """
    if os.path.exists(socket_path):
        try:
            reader, writer = await asyncio.open_unix_connection(socket_path)
            writer.write(cmd.get_bytes())
            writer.write_eof()

            if expect_response:
                response = Response(await reader.read())

                writer.close()
                return response
            return okResponse()
        except ConnectionRefusedError:
            pass
    r = badResponse()
    r.error = ERRORS.SOCKET_DOESNT_EXIST
    return r
Esempio n. 9
0
    async def _handle_msg(self, reader: asyncio.StreamReader,
                          writer: asyncio.StreamWriter):
        """Handle incoming messages."""
        msg = Cmd(await reader.read())
        addr = writer.get_extra_info("peername")
        print_addr = addr if addr else "localhost"
        self.server_logger.debug(f"Received from {print_addr}")

        if msg.cmd in self.cmd_to_callback:
            self.server_logger.debug(f"Got cmd: {msg.cmd}")
            response = await self.cmd_to_callback[msg.cmd](msg)
        else:
            self.server_logger.warning(
                f"Got unrecognized command: {msg.cmd.value}")
            response = badResponse()
            response.error = ERRORS.UNRECOGNIZED_COMMAND

        writer.write(response.get_bytes())
        writer.write_eof()
        await writer.drain()

        self.server_logger.debug(f"Closed connection from {print_addr}")
        writer.close()