diff --git a/doc_gen/index.rst b/doc_gen/index.rst index e9e0a26..89e0e8b 100644 --- a/doc_gen/index.rst +++ b/doc_gen/index.rst @@ -31,6 +31,14 @@ Routers .. automodule:: pyninja.routers +Monitors +======== + +Process +======= + +.. automodule:: pyninja.process + Service ======= diff --git a/docs/_sources/index.rst.txt b/docs/_sources/index.rst.txt index e9e0a26..89e0e8b 100644 --- a/docs/_sources/index.rst.txt +++ b/docs/_sources/index.rst.txt @@ -31,6 +31,14 @@ Routers .. automodule:: pyninja.routers +Monitors +======== + +Process +======= + +.. automodule:: pyninja.process + Service ======= diff --git a/docs/genindex.html b/docs/genindex.html index 7fe4da7..49b6318 100644 --- a/docs/genindex.html +++ b/docs/genindex.html @@ -110,10 +110,12 @@

F

G

@@ -131,6 +133,8 @@

M

  • pyninja.exceptions
  • pyninja.main +
  • +
  • pyninja.process
  • pyninja.routers
  • @@ -157,7 +161,7 @@

    N

    P

    + + +
    • + pyninja.process + +
    • +
    • pyninja.routers
        diff --git a/docs/index.html b/docs/index.html index 8c8cfe3..839441c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -119,6 +119,23 @@

        Welcome to PyNinja’s documentation!

        Routers

        +
        +
        +async pyninja.routers.process_status(payload: StatusPayload)
        +

        API function to monitor a process.

        +
        +
        Parameters:
        +

        payload (StatusPayload) – Payload received as request body.

        +
        +
        Raises:
        +
          +
        • APIResponse

        • +
        • Raises the HTTPStatus object with a status code and detail as response.

        • +
        +
        +
        +
        +
        async pyninja.routers.service_status(payload: StatusPayload)
        @@ -143,25 +160,45 @@

        Welcome to PyNinja’s documentation! -

        Service

        +
        +

        Monitors

        +
        +
        +

        Process

        -
        -pyninja.service.get_pid(service_name: str) int
        +
        +pyninja.process.get_process_status(process_name: str) Generator[Dict[str, int]]

        Get process ID for a particular service.

        Parameters:

        service_name (str) – Name of the service.

        +
        Yields:
        +

        Generator[Dict[str, int]] – Yields the process metrics as a dictionary of key-value pairs.

        +
        +
        +
        + +
        +
        +pyninja.process.get_performance(process_name: str, process: Process) Dict[str, int]
        +

        Checks performance by monitoring CPU utilization, number of threads and open files.

        +
        +
        Parameters:
        +

        process – Process object.

        +
        Returns:
        -

        Process ID running the service.

        +

        Returns the process metrics as key-value pairs.

        Return type:
        -

        int

        +

        Dict[str, int]

        +
        +
        +

        Service

        pyninja.service.get_service_status(service_name: str) ServiceStatus
        @@ -204,11 +241,6 @@

        Squire
        >>> ServiceStatus
         
        -
        -
        -pid: int
        -
        -
        status_code: int
        @@ -328,6 +360,8 @@

        Table of Contents

      • Authenticator
      • Exceptions
      • Routers
      • +
      • Monitors
      • +
      • Process
      • Service
      • Squire
      • Indices and tables
      • diff --git a/docs/objects.inv b/docs/objects.inv index 6cefa34..6d06bf3 100644 Binary files a/docs/objects.inv and b/docs/objects.inv differ diff --git a/docs/py-modindex.html b/docs/py-modindex.html index 847f769..5d71750 100644 --- a/docs/py-modindex.html +++ b/docs/py-modindex.html @@ -73,6 +73,11 @@

        Python Module Index

        pyninja.main
        + pyninja.process +
        diff --git a/docs/searchindex.js b/docs/searchindex.js index 45b8dbc..70ab184 100644 --- a/docs/searchindex.js +++ b/docs/searchindex.js @@ -1 +1 @@ -Search.setIndex({"docnames": ["README", "index"], "filenames": ["README.md", "index.rst"], "titles": ["PyNinja", "Welcome to PyNinja\u2019s documentation!"], "terms": {"light": 0, "weight": 0, "o": [0, 1], "agnost": 0, "servic": 0, "monitor": [0, 1], "api": [0, 1], "platform": 0, "support": 0, "deploy": 0, "recommend": 0, "instal": 0, "python": 0, "3": 0, "10": 0, "11": 0, "us": [0, 1], "dedic": 0, "virtual": 0, "m": 0, "pip": 0, "initi": 0, "id": [0, 1], "import": 0, "__name__": 0, "__main__": 0, "start": [0, 1], "cli": 0, "help": 0, "usag": 0, "instruct": 0, "sourc": 0, "from": [0, 1], "an": [0, 1], "env": [0, 1], "file": [0, 1], "By": 0, "default": 0, "look": 0, "current": 0, "work": 0, "directori": 0, "ninja": 0, "_": 0, "host": 0, "hostnam": 0, "server": [0, 1], "port": 0, "number": 0, "worker": [0, 1], "uvicorn": [0, 1], "apikei": [0, 1], "kei": 0, "authent": 0, "log": 0, "ini": 0, "configur": [0, 1], "custom": [0, 1], "just": 0, "place": 0, "refer": [0, 1], "sampl": 0, "exampl": 0, "docstr": 0, "format": 0, "googl": 0, "style": 0, "convent": 0, "pep": 0, "8": 0, "isort": 0, "requir": 0, "gitvers": 0, "revers": 0, "f": 0, "release_not": 0, "rst": 0, "t": 0, "pre": 0, "commit": 0, "ensur": 0, "run": [0, 1], "pytest": 0, "gener": 0, "valid": [0, 1], "hyperlink": 0, "all": 0, "markdown": 0, "includ": 0, "wiki": 0, "page": [0, 1], "sphinx": 0, "5": 0, "1": 0, "recommonmark": 0, "http": 0, "org": 0, "project": 0, "thevickypedia": 0, "github": 0, "io": 0, "vignesh": 0, "rao": 0, "under": 0, "mit": 0, "kick": 1, "off": 1, "environ": 1, "variabl": 1, "code": 1, "standard": 1, "releas": 1, "note": 1, "lint": 1, "pypi": 1, "packag": 1, "runbook": 1, "licens": 1, "copyright": 1, "env_fil": 1, "str": 1, "none": 1, "starter": 1, "function": 1, "which": 1, "trigger": 1, "paramet": 1, "filepath": 1, "async": 1, "auth": 1, "token": 1, "httpbasiccredenti": 1, "depend": 1, "httpbearer": 1, "mention": 1, "take": 1, "author": 1, "header": 1, "argument": 1, "rais": 1, "apirespons": 1, "401": 1, "If": 1, "i": 1, "invalid": 1, "status_cod": 1, "int": 1, "detail": 1, "ani": 1, "option": 1, "dict": 1, "httpexcept": 1, "fastapi": 1, "wrap": 1, "respons": 1, "unsupportedo": 1, "class": 1, "unsupport": 1, "service_statu": 1, "payload": 1, "statuspayload": 1, "receiv": 1, "request": 1, "bodi": 1, "httpstatu": 1, "object": 1, "statu": 1, "doc": 1, "redirect": 1, "get_pid": 1, "service_nam": 1, "get": 1, "process": 1, "particular": 1, "name": 1, "return": 1, "type": 1, "get_service_statu": 1, "servicestatu": 1, "instanc": 1, "basemodel": 1, "handl": 1, "input": 1, "data": 1, "load": 1, "descript": 1, "pid": 1, "envconfig": 1, "set": 1, "ninja_host": 1, "ninja_port": 1, "classmethod": 1, "from_env_fil": 1, "creat": 1, "model": 1, "config": 1, "extra": 1, "ignor": 1, "env_load": 1, "filenam": 1, "pathlik": 1, "base": 1, "filetyp": 1, "where": 1, "var": 1, "have": 1, "alia": 1, "index": 1, "modul": 1, "search": 1}, "objects": {"pyninja": [[1, 0, 0, "-", "auth"], [1, 0, 0, "-", "exceptions"], [1, 0, 0, "-", "main"], [1, 0, 0, "-", "routers"], [1, 0, 0, "-", "service"], [1, 0, 0, "-", "squire"]], "pyninja.auth": [[1, 1, 1, "", "authenticator"]], "pyninja.exceptions": [[1, 2, 1, "", "APIResponse"], [1, 2, 1, "", "UnSupportedOS"]], "pyninja.main": [[1, 1, 1, "", "start"]], "pyninja.routers": [[1, 1, 1, "", "docs"], [1, 1, 1, "", "service_status"]], "pyninja.service": [[1, 1, 1, "", "get_pid"], [1, 1, 1, "", "get_service_status"]], "pyninja.squire": [[1, 3, 1, "", "EnvConfig"], [1, 3, 1, "", "ServiceStatus"], [1, 3, 1, "", "StatusPayload"], [1, 4, 1, "", "env"], [1, 1, 1, "", "env_loader"]], "pyninja.squire.EnvConfig": [[1, 3, 1, "", "Config"], [1, 4, 1, "", "apikey"], [1, 5, 1, "", "from_env_file"], [1, 4, 1, "", "ninja_host"], [1, 4, 1, "", "ninja_port"], [1, 4, 1, "", "workers"]], "pyninja.squire.EnvConfig.Config": [[1, 4, 1, "", "extra"]], "pyninja.squire.ServiceStatus": [[1, 4, 1, "", "description"], [1, 4, 1, "", "pid"], [1, 4, 1, "", "status_code"]], "pyninja.squire.StatusPayload": [[1, 4, 1, "", "service_name"]]}, "objtypes": {"0": "py:module", "1": "py:function", "2": "py:exception", "3": "py:class", "4": "py:attribute", "5": "py:method"}, "objnames": {"0": ["py", "module", "Python module"], "1": ["py", "function", "Python function"], "2": ["py", "exception", "Python exception"], "3": ["py", "class", "Python class"], "4": ["py", "attribute", "Python attribute"], "5": ["py", "method", "Python method"]}, "titleterms": {"pyninja": [0, 1], "kick": 0, "off": 0, "environ": 0, "variabl": 0, "code": 0, "standard": 0, "releas": 0, "note": 0, "lint": 0, "pypi": 0, "packag": 0, "runbook": 0, "licens": 0, "copyright": 0, "welcom": 1, "": 1, "document": 1, "content": 1, "main": 1, "authent": 1, "except": 1, "router": 1, "servic": 1, "squir": 1, "indic": 1, "tabl": 1}, "envversion": {"sphinx.domains.c": 2, "sphinx.domains.changeset": 1, "sphinx.domains.citation": 1, "sphinx.domains.cpp": 6, "sphinx.domains.index": 1, "sphinx.domains.javascript": 2, "sphinx.domains.math": 2, "sphinx.domains.python": 3, "sphinx.domains.rst": 2, "sphinx.domains.std": 2, "sphinx": 56}}) \ No newline at end of file +Search.setIndex({"docnames": ["README", "index"], "filenames": ["README.md", "index.rst"], "titles": ["PyNinja", "Welcome to PyNinja\u2019s documentation!"], "terms": {"light": 0, "weight": 0, "o": [0, 1], "agnost": 0, "servic": 0, "monitor": 0, "api": [0, 1], "platform": 0, "support": 0, "deploy": 0, "recommend": 0, "instal": 0, "python": 0, "3": 0, "10": 0, "11": 0, "us": [0, 1], "dedic": 0, "virtual": 0, "m": 0, "pip": 0, "initi": 0, "id": [0, 1], "import": 0, "__name__": 0, "__main__": 0, "start": [0, 1], "cli": 0, "help": 0, "usag": 0, "instruct": 0, "sourc": 0, "from": [0, 1], "an": [0, 1], "env": [0, 1], "file": [0, 1], "By": 0, "default": 0, "look": 0, "current": 0, "work": 0, "directori": 0, "ninja": 0, "_": 0, "host": 0, "hostnam": 0, "server": [0, 1], "port": 0, "number": [0, 1], "worker": [0, 1], "uvicorn": [0, 1], "apikei": [0, 1], "kei": [0, 1], "authent": 0, "log": 0, "ini": 0, "configur": [0, 1], "custom": [0, 1], "just": 0, "place": 0, "refer": [0, 1], "sampl": 0, "exampl": 0, "docstr": 0, "format": 0, "googl": 0, "style": 0, "convent": 0, "pep": 0, "8": 0, "isort": 0, "requir": 0, "gitvers": 0, "revers": 0, "f": 0, "release_not": 0, "rst": 0, "t": 0, "pre": 0, "commit": 0, "ensur": 0, "run": 0, "pytest": 0, "gener": [0, 1], "valid": [0, 1], "hyperlink": 0, "all": 0, "markdown": 0, "includ": 0, "wiki": 0, "page": [0, 1], "sphinx": 0, "5": 0, "1": 0, "recommonmark": 0, "http": 0, "org": 0, "project": 0, "thevickypedia": 0, "github": 0, "io": 0, "vignesh": 0, "rao": 0, "under": 0, "mit": 0, "kick": 1, "off": 1, "environ": 1, "variabl": 1, "code": 1, "standard": 1, "releas": 1, "note": 1, "lint": 1, "pypi": 1, "packag": 1, "runbook": 1, "licens": 1, "copyright": 1, "env_fil": 1, "str": 1, "none": 1, "starter": 1, "function": 1, "which": 1, "trigger": 1, "paramet": 1, "filepath": 1, "async": 1, "auth": 1, "token": 1, "httpbasiccredenti": 1, "depend": 1, "httpbearer": 1, "mention": 1, "take": 1, "author": 1, "header": 1, "argument": 1, "rais": 1, "apirespons": 1, "401": 1, "If": 1, "i": 1, "invalid": 1, "status_cod": 1, "int": 1, "detail": 1, "ani": 1, "option": 1, "dict": 1, "httpexcept": 1, "fastapi": 1, "wrap": 1, "respons": 1, "unsupportedo": 1, "class": 1, "unsupport": 1, "process_statu": 1, "payload": 1, "statuspayload": 1, "receiv": 1, "request": 1, "bodi": 1, "httpstatu": 1, "object": 1, "statu": 1, "service_statu": 1, "doc": 1, "redirect": 1, "get_process_statu": 1, "process_nam": 1, "get": 1, "particular": 1, "service_nam": 1, "name": 1, "yield": 1, "metric": 1, "dictionari": 1, "valu": 1, "pair": 1, "get_perform": 1, "check": 1, "perform": 1, "cpu": 1, "util": 1, "thread": 1, "open": 1, "return": 1, "type": 1, "get_service_statu": 1, "servicestatu": 1, "instanc": 1, "basemodel": 1, "handl": 1, "input": 1, "data": 1, "load": 1, "descript": 1, "envconfig": 1, "set": 1, "ninja_host": 1, "ninja_port": 1, "classmethod": 1, "from_env_fil": 1, "creat": 1, "model": 1, "config": 1, "extra": 1, "ignor": 1, "env_load": 1, "filenam": 1, "pathlik": 1, "base": 1, "filetyp": 1, "where": 1, "var": 1, "have": 1, "alia": 1, "index": 1, "modul": 1, "search": 1}, "objects": {"pyninja": [[1, 0, 0, "-", "auth"], [1, 0, 0, "-", "exceptions"], [1, 0, 0, "-", "main"], [1, 0, 0, "-", "process"], [1, 0, 0, "-", "routers"], [1, 0, 0, "-", "service"], [1, 0, 0, "-", "squire"]], "pyninja.auth": [[1, 1, 1, "", "authenticator"]], "pyninja.exceptions": [[1, 2, 1, "", "APIResponse"], [1, 2, 1, "", "UnSupportedOS"]], "pyninja.main": [[1, 1, 1, "", "start"]], "pyninja.process": [[1, 1, 1, "", "get_performance"], [1, 1, 1, "", "get_process_status"]], "pyninja.routers": [[1, 1, 1, "", "docs"], [1, 1, 1, "", "process_status"], [1, 1, 1, "", "service_status"]], "pyninja.service": [[1, 1, 1, "", "get_service_status"]], "pyninja.squire": [[1, 3, 1, "", "EnvConfig"], [1, 3, 1, "", "ServiceStatus"], [1, 3, 1, "", "StatusPayload"], [1, 4, 1, "", "env"], [1, 1, 1, "", "env_loader"]], "pyninja.squire.EnvConfig": [[1, 3, 1, "", "Config"], [1, 4, 1, "", "apikey"], [1, 5, 1, "", "from_env_file"], [1, 4, 1, "", "ninja_host"], [1, 4, 1, "", "ninja_port"], [1, 4, 1, "", "workers"]], "pyninja.squire.EnvConfig.Config": [[1, 4, 1, "", "extra"]], "pyninja.squire.ServiceStatus": [[1, 4, 1, "", "description"], [1, 4, 1, "", "status_code"]], "pyninja.squire.StatusPayload": [[1, 4, 1, "", "service_name"]]}, "objtypes": {"0": "py:module", "1": "py:function", "2": "py:exception", "3": "py:class", "4": "py:attribute", "5": "py:method"}, "objnames": {"0": ["py", "module", "Python module"], "1": ["py", "function", "Python function"], "2": ["py", "exception", "Python exception"], "3": ["py", "class", "Python class"], "4": ["py", "attribute", "Python attribute"], "5": ["py", "method", "Python method"]}, "titleterms": {"pyninja": [0, 1], "kick": 0, "off": 0, "environ": 0, "variabl": 0, "code": 0, "standard": 0, "releas": 0, "note": 0, "lint": 0, "pypi": 0, "packag": 0, "runbook": 0, "licens": 0, "copyright": 0, "welcom": 1, "": 1, "document": 1, "content": 1, "main": 1, "authent": 1, "except": 1, "router": 1, "monitor": 1, "process": 1, "servic": 1, "squir": 1, "indic": 1, "tabl": 1}, "envversion": {"sphinx.domains.c": 2, "sphinx.domains.changeset": 1, "sphinx.domains.citation": 1, "sphinx.domains.cpp": 6, "sphinx.domains.index": 1, "sphinx.domains.javascript": 2, "sphinx.domains.math": 2, "sphinx.domains.python": 3, "sphinx.domains.rst": 2, "sphinx.domains.std": 2, "sphinx": 56}}) \ No newline at end of file diff --git a/pyninja/process.py b/pyninja/process.py new file mode 100644 index 0000000..1e1c9f3 --- /dev/null +++ b/pyninja/process.py @@ -0,0 +1,43 @@ +import logging +from collections.abc import Generator +from typing import Dict + +import psutil + +LOGGER = logging.getLogger("uvicorn.error") + + +def get_process_status(process_name: str) -> Generator[Dict[str, int]]: + """Get process ID for a particular service. + + Args: + service_name (str): Name of the service. + + Yields: + Generator[Dict[str, int]]: + Yields the process metrics as a dictionary of key-value pairs. + """ + for proc in psutil.process_iter(["pid", "name"]): + if proc.info["name"].lower() == process_name.lower(): + process = psutil.Process(proc.info["pid"]) + yield get_performance(process_name, process) + + +def get_performance(process_name: str, process: psutil.Process) -> Dict[str, int]: + """Checks performance by monitoring CPU utilization, number of threads and open files. + + Args: + process: Process object. + + Returns: + Dict[str, int]: + Returns the process metrics as key-value pairs. + """ + cpu = process.cpu_percent(interval=0.5) + threads = process.num_threads() + open_files = len(process.open_files()) + info_dict = {"cpu": cpu, "threads": threads, "open_files": open_files} + LOGGER.info({f"{process_name} [{process.pid}]": info_dict}) + info_dict["pid"] = process.pid.real + info_dict["pname"] = process_name + return info_dict diff --git a/pyninja/routers.py b/pyninja/routers.py index 7094f8d..86b7335 100644 --- a/pyninja/routers.py +++ b/pyninja/routers.py @@ -1,12 +1,33 @@ import logging +from http import HTTPStatus from fastapi import Depends from fastapi.responses import RedirectResponse from fastapi.routing import APIRoute -from pyninja import auth, exceptions, service, squire +from pyninja import auth, exceptions, process, service, squire -LOGGER = logging.getLogger(__name__) +LOGGER = logging.getLogger("uvicorn.error") + + +async def process_status(payload: squire.StatusPayload): + """API function to monitor a process. + + Args: + payload (StatusPayload): Payload received as request body. + + Raises: + APIResponse: + Raises the HTTPStatus object with a status code and detail as response. + """ + if service_status := list(process.get_process_status(payload.service_name)): + raise exceptions.APIResponse( + status_code=HTTPStatus.OK.real, detail=service_status + ) + LOGGER.error("%s: 404 - No such process", payload.service_name) + raise exceptions.APIResponse( + status_code=404, detail=f"Process {payload.service_name} not found." + ) async def service_status(payload: squire.StatusPayload): @@ -21,9 +42,8 @@ async def service_status(payload: squire.StatusPayload): """ service_status = service.get_service_status(payload.service_name) LOGGER.info( - "%s[%d]: %d - %s", + "%s: %d - %s", payload.service_name, - service_status.pid, service_status.status_code, service_status.description, ) @@ -44,5 +64,11 @@ async def docs(): methods=["POST"], dependencies=[Depends(auth.authenticator)], ), + APIRoute( + path="/process-status", + endpoint=process_status, + methods=["POST"], + dependencies=[Depends(auth.authenticator)], + ), APIRoute(path="/", endpoint=docs, methods=["GET"], include_in_schema=False), ] diff --git a/pyninja/service.py b/pyninja/service.py index 00131f2..eb88ed8 100644 --- a/pyninja/service.py +++ b/pyninja/service.py @@ -2,8 +2,6 @@ import subprocess from http import HTTPStatus -import psutil - from pyninja import exceptions, squire current_os = platform.system() @@ -15,21 +13,6 @@ ) -def get_pid(service_name: str) -> int: - """Get process ID for a particular service. - - Args: - service_name (str): Name of the service. - - Returns: - int: - Process ID running the service. - """ - for proc in psutil.process_iter(["pid", "name"]): - if proc.info["name"] == service_name: - return proc.info["pid"] - - def get_service_status(service_name: str) -> squire.ServiceStatus: """Get service status. @@ -40,30 +23,22 @@ def get_service_status(service_name: str) -> squire.ServiceStatus: ServiceStatus: Returns an instance of the ServiceStatus. """ - # A service (eg: docker) may have multiple process IDs with different suffix names - if not (pid := get_pid(service_name)): - pid = 0000 - running = squire.ServiceStatus( - pid=pid, status_code=HTTPStatus.OK.real, description=f"{service_name} is running", ) stopped = squire.ServiceStatus( - pid=pid, status_code=HTTPStatus.NOT_IMPLEMENTED.real, description=f"{service_name} has been stopped", ) unknown = squire.ServiceStatus( - pid=pid, status_code=HTTPStatus.SERVICE_UNAVAILABLE.real, description=f"{service_name} - status unknwon", ) unavailable = squire.ServiceStatus( - pid=pid, status_code=HTTPStatus.NOT_FOUND.real, description=f"{service_name} - not found", ) diff --git a/pyninja/squire.py b/pyninja/squire.py index 2d0f067..ad39f23 100644 --- a/pyninja/squire.py +++ b/pyninja/squire.py @@ -26,7 +26,6 @@ class ServiceStatus(BaseModel): """ - pid: int status_code: int description: str