-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new feature to monitor all things
docker
Update sample values
- Loading branch information
1 parent
3c9cdce
commit 5887b7e
Showing
14 changed files
with
445 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,11 @@ Routers | |
Monitors | ||
======== | ||
|
||
Docker | ||
------ | ||
|
||
.. automodule:: pyninja.dockerized | ||
|
||
Process | ||
------- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,11 @@ Routers | |
Monitors | ||
======== | ||
|
||
Docker | ||
------ | ||
|
||
.. automodule:: pyninja.dockerized | ||
|
||
Process | ||
------- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import logging | ||
from collections.abc import Generator | ||
from typing import Dict, List | ||
|
||
import docker | ||
from docker.errors import DockerException | ||
|
||
LOGGER = logging.getLogger("uvicorn.default") | ||
|
||
|
||
def get_container_status(name: str = None) -> str | None: | ||
"""Get container status by name. | ||
Args: | ||
name: Name of the container or image used to run the container. | ||
Returns: | ||
str: | ||
Container status as a string. | ||
""" | ||
try: | ||
containers = docker.from_env().api.containers() | ||
except DockerException as error: | ||
LOGGER.error(error) | ||
return | ||
for container in containers: | ||
if name in container.get("Image") or name in container.get("Names"): | ||
return ( | ||
f"{container.get('Id')[:12]} - {container.get('Names')} - " | ||
f"{container.get('State')} - {container.get('Status')}" | ||
) | ||
|
||
|
||
def get_running_containers() -> Generator[Dict[str, str]]: | ||
"""Get running containers. | ||
Yields: | ||
Dict[str, str]: | ||
Yields a dictionary of running containers with the corresponding metrics. | ||
""" | ||
try: | ||
containers = docker.from_env().api.containers() | ||
except DockerException as error: | ||
LOGGER.error(error) | ||
return [] | ||
for container in containers: | ||
if container.get("State") == "running": | ||
yield container | ||
|
||
|
||
def get_all_containers() -> List[Dict[str, str]] | None: | ||
"""Get all containers and their metrics. | ||
Returns: | ||
List[Dict[str, str]]: | ||
Returns a list of all the containers and their stats. | ||
""" | ||
try: | ||
return docker.from_env().api.containers(all=True) | ||
except DockerException as error: | ||
LOGGER.error(error) | ||
return | ||
|
||
|
||
def get_all_images() -> Dict[str, str] | None: | ||
"""Get all docker images. | ||
Returns: | ||
Dict[str, str]: | ||
Returns a dictionary with image stats. | ||
""" | ||
try: | ||
return docker.from_env().api.images(all=True) | ||
except DockerException as error: | ||
LOGGER.error(error) | ||
return | ||
|
||
|
||
def get_all_volumes() -> Dict[str, str] | None: | ||
"""Get all docker volumes. | ||
Returns: | ||
Dict[str, str]: | ||
Returns a dictionary with list of volume objects. | ||
""" | ||
try: | ||
return docker.from_env().api.volumes() | ||
except DockerException as error: | ||
LOGGER.error(error) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
click==8.1.7 | ||
docker==7.1.0 | ||
fastapi==0.112.0 | ||
psutil==6.0.0 | ||
pydantic==2.8.2 | ||
|
Oops, something went wrong.