Example #1
0
def _phantomjs(settings, health_check):
    phantomjs_settings = settings["phantomjs"]
    for route in phantomjs_settings["routes"]:
        if route.get("checker_name",
                     route["name"]) in phantomjs_settings["disable"]:
            continue

        class _Check:
            def __init__(self, route):
                self.route = route

            def __call__(self, request):
                path = request.route_path(self.route["name"],
                                          _query=self.route.get("params", {}))
                url = build_url("Check", path, request)["url"]

                cmd = ["node", "/usr/bin/check-example.js", url]

                try:
                    subprocess.check_output(cmd, timeout=10)
                except subprocess.CalledProcessError as exception:
                    raise Exception("{} exit with code: {}\n{}".format(
                        ' '.join(exception.cmd), exception.returncode,
                        exception.output.decode("utf-8")))
                except subprocess.TimeoutExpired as exception:
                    raise Exception("""Timeout:
command: {}
output:
{}""".format(" ".join(exception.cmd), exception.output.decode("utf-8")))

        name = "checker_phantomjs_" + route.get("checker_name", route["name"])
        health_check.add_custom_check(name=name,
                                      check_cb=_Check(route),
                                      level=route["level"])
Example #2
0
def _themes_errors(settings, health_check):
    from c2cgeoportal_commons.models import DBSession
    from c2cgeoportal_commons.models.main import Interface

    themes_settings = settings["themes"]
    default_params = themes_settings.get("params", {})
    interfaces_settings = themes_settings["interfaces"]

    def check(request):
        path = request.route_path("themes")
        session = requests.session()
        for interface, in DBSession.query(Interface.name).all():
            params = {}
            params.update(default_params)
            params.update(
                interfaces_settings.get(interface, {}).get("params", {}))
            params["interface"] = interface

            interface_url_headers = build_url("checker_themes " + interface,
                                              path, request)

            response = session.get(params=params,
                                   timeout=120,
                                   **interface_url_headers)
            response.raise_for_status()

            result = response.json()
            if len(result["errors"]) != 0:
                raise c2cwsgiutils.health_check.JsonCheckException(
                    "Interface '{}' has error in Theme.".format(interface),
                    result["errors"])

    health_check.add_custom_check(name="checker_themes",
                                  check_cb=check,
                                  level=themes_settings["level"])
Example #3
0
def _phantomjs(settings, health_check):
    phantomjs_settings = settings["phantomjs"]
    for route in phantomjs_settings["routes"]:
        if route.get("checker_name", route["name"]) in phantomjs_settings["disable"]:
            continue

        def check(request):
            path = request.route_path(route["name"], _query=route.get("params", {}))
            url = build_url("Check", path, request)["url"]

            cmd = [
                "phantomjs", "--local-to-remote-url-access=true",
                "/usr/lib/node_modules/ngeo/buildtools/check-example.js", url
            ]

            try:
                subprocess.check_output(cmd, timeout=10)
            except subprocess.CalledProcessError as e:
                raise Exception(e.output.decode("utf-8"))
            except subprocess.TimeoutExpired as e:
                raise Exception("""Timeout:
command: {}
output:
{}""".format(" ".join(e.cmd), e.output.decode("utf-8")))
        name = "checker_phantomjs_" + route.get("checker_name", route["name"])
        health_check.add_custom_check(name=name, check_cb=check, level=route["level"])
def init(config: pyramid.config.Configurator,
         health_check: c2cwsgiutils.health_check.HealthCheck) -> None:
    """
    Initialize the check collector.

    Add him in the c2cwsgichecks.
    """
    global_settings = config.get_settings()
    if "check_collector" not in global_settings:
        return
    settings = global_settings["check_collector"]
    c2c_base = global_settings.get("c2c.base_path", "")

    max_level = settings["max_level"]

    for host in settings["hosts"]:

        class Check:
            def __init__(self, host: Dict[str, Any]):
                self.host = host

            def __call__(
                    self, request: pyramid.request.Request
            ) -> Optional[Dict[str, Any]]:
                params = request.params
                display = self.host["display"]
                if "host" not in params or display == params["host"]:
                    url_headers = build_url(
                        "check_collector",
                        f"{self.host['url'].rstrip('/')}/{c2c_base.strip('/')}/health_check",
                        request,
                    )
                    r = requests.get(
                        params={
                            "max_level":
                            str(self.host.get("max_level", max_level))
                        },
                        timeout=120,
                        **url_headers,  # type: ignore
                    )
                    r.raise_for_status()
                    return cast(Dict[str, Any], r.json())
                return None

        health_check.add_custom_check(name="check_collector_" +
                                      host["display"],
                                      check_cb=Check(host),
                                      level=settings["level"])
Example #5
0
def _phantomjs(settings: Dict[str, Any],
               health_check: c2cwsgiutils.health_check.HealthCheck) -> None:
    phantomjs_settings = settings["phantomjs"]
    for route in phantomjs_settings["routes"]:
        if route.get("checker_name",
                     route["name"]) in phantomjs_settings["disable"]:
            continue

        class _Check:
            def __init__(self, route: Dict[str, Any]) -> None:
                self.route = route

            def __call__(self, request: pyramid.request.Request) -> None:
                path = request.route_path(self.route["name"],
                                          _query=self.route.get("params", {}))
                url: str = cast(str, build_url("Check", path, request)["url"])

                cmd: List[str] = ["node", "/usr/bin/check-example.js", url]
                env = dict(os.environ)
                for name, value in self.route.get("environment", {}).items():
                    if isinstance(value, (list, dict)):
                        value = json.dumps(value)
                    elif not isinstance(value, str):
                        value = str(value)
                    env[name] = value

                try:
                    subprocess.check_output(cmd, env=env, timeout=70)
                except subprocess.CalledProcessError as exception:
                    raise Exception(
                        f"{' '.join(exception.cmd)} exit with code: {exception.returncode}\n"
                        f"{exception.output.decode('utf-8')[:10000]}"
                    ) from exception
                except subprocess.TimeoutExpired as exception:
                    raise Exception(f"""Timeout:
command: {' '.join(exception.cmd)}
output:
{exception.output.decode('utf-8')}""") from exception

        name = "checker_phantomjs_" + route.get("checker_name", route["name"])
        health_check.add_custom_check(name=name,
                                      check_cb=_Check(route),
                                      level=route["level"])
Example #6
0
def _pdf3(settings: Dict[str, Any],
          health_check: c2cwsgiutils.health_check.HealthCheck) -> None:
    print_settings = settings["print"]
    if "spec" not in print_settings:
        return

    def check(request: pyramid.request.Request) -> None:
        path = request.route_path("printproxy_report_create", format="pdf")
        url_headers = build_url("Check the printproxy request (create)", path,
                                request)

        session = requests.session()
        resp = session.post(json=print_settings["spec"],
                            timeout=30,
                            **url_headers)  # type: ignore
        resp.raise_for_status()

        job = resp.json()

        path = request.route_path("printproxy_status", ref=job["ref"])
        url_headers = build_url("Check the printproxy pdf status", path,
                                request)
        done = False
        while not done:
            sleep(1)
            resp = session.get(timeout=30, **url_headers)  # type: ignore
            resp.raise_for_status()

            status = resp.json()
            if "error" in status:
                raise Exception(
                    f"Failed to do the printing: {status['error']}")
            done = status["done"]

        path = request.route_path("printproxy_report_get", ref=job["ref"])
        url_headers = build_url("Check the printproxy pdf retrieve", path,
                                request)
        resp = session.get(timeout=30, **url_headers)  # type: ignore
        resp.raise_for_status()

    health_check.add_custom_check(name="checker_print",
                                  check_cb=check,
                                  level=print_settings["level"])
Example #7
0
def _pdf3(settings, health_check):
    print_settings = settings["print"]
    if "spec" not in print_settings:
        return

    def check(request):
        path = request.route_path("printproxy_report_create", format="pdf")
        url_headers = build_url("Check the printproxy request (create)", path,
                                request)

        session = requests.session()
        resp = session.post(json=print_settings["spec"],
                            timeout=30,
                            **url_headers)
        resp.raise_for_status()

        job = resp.json()

        path = request.route_path("printproxy_status", ref=job["ref"])
        url_headers = build_url("Check the printproxy pdf status", path,
                                request)
        done = False
        while not done:
            sleep(1)
            resp = session.get(timeout=30, **url_headers)
            resp.raise_for_status()

            status = resp.json()
            if "error" in status:
                raise Exception("Failed to do the printing: {0!s}".format(
                    status["error"]))
            done = status["done"]

        path = request.route_path("printproxy_report_get", ref=job["ref"])
        url_headers = build_url("Check the printproxy pdf retrieve", path,
                                request)
        resp = session.get(timeout=30, **url_headers)
        resp.raise_for_status()

    health_check.add_custom_check(name="checker_print",
                                  check_cb=check,
                                  level=print_settings["level"])
Example #8
0
def _themes_errors(
        settings: Dict[str, Any],
        health_check: c2cwsgiutils.health_check.HealthCheck) -> None:
    from c2cgeoportal_commons.models import DBSession  # pylint: disable=import-outside-toplevel
    from c2cgeoportal_commons.models.main import Interface  # pylint: disable=import-outside-toplevel

    themes_settings = settings["themes"]
    default_params = themes_settings.get("params", {})
    interfaces_settings = themes_settings["interfaces"]

    def check(request: pyramid.request.Request) -> None:
        path = request.route_path("themes")
        session = requests.session()
        for (interface, ) in DBSession.query(Interface.name).all():
            params: Dict[str, str] = {}
            params.update(default_params)
            params.update(
                interfaces_settings.get(interface, {}).get("params", {}))
            params["interface"] = interface

            interface_url_headers = build_url("checker_themes " + interface,
                                              path, request)

            response = session.get(params=params,
                                   timeout=120,
                                   **interface_url_headers)  # type: ignore
            response.raise_for_status()

            result = response.json()
            if result["errors"]:
                raise c2cwsgiutils.health_check.JsonCheckException(
                    f"Interface '{interface}' has error in Theme.",
                    result["errors"])

    health_check.add_custom_check(name="checker_themes",
                                  check_cb=check,
                                  level=themes_settings["level"])