Skip to content

Instantly share code, notes, and snippets.

@T31M
Last active October 12, 2023 09:27
Show Gist options
  • Save T31M/a6f0787b9733377f0876341f6b875375 to your computer and use it in GitHub Desktop.
Save T31M/a6f0787b9733377f0876341f6b875375 to your computer and use it in GitHub Desktop.
Get Instance "docker stats" / Disk Space uasge though Fabric / SSH
from fabric import SerialGroup
from fabric import Connection
from invoke.exceptions import Exit
from loguru import logger
hosts_provider_a = [
"hostnames",
"in",
"your",
"/etc/hosts",
"or",
"windows",
"equivalent",
]
hosts_provider_b = [
"hostnames",
"in",
"your",
"/etc/hosts",
"or",
"windows",
"equivalent",
]
# If needed to separate
# hosts_provider_a_group = SerialGroup(
# *hosts_provider_a,
# user="your_ssh_user",
# )
# hosts_provider_b_group = SerialGroup(
# *hosts_provider_b,
# user="your_ssh_user",
# )
all_hosts = SerialGroup(
*hosts_provider_a, *hosts_provider_b, user="your_ssh_user"
)
def get_server_disk_space(c: Connection):
uname = c.run("uname -s", hide=True)
if "Linux" in uname.stdout:
command = "df -h / | tail -n1 | awk '{print $5}'"
return c.run(command, hide=True).stdout.strip()
err = "No idea how to get disk space on {}!".format(uname)
raise Exit(err)
def get_docker_stats(c: Connection):
uname = c.run("uname -s", hide=True)
if "Linux" in uname.stdout:
command = "docker stats --no-stream"
return c.run(command, hide=True).stdout.strip()
err = f"No idea how to get docker stats on {uname}!"
raise Exit(err)
if __name__ == "__main__":
logger.info("Running Orchestrator Fabric")
cxn: Connection
for cxn in all_servers:
logger.info(f"{cxn.host} Disk Space Used: {get_server_disk_space(cxn)}")
# logger.info(f"{cxn.host} Docker Stats:\n{get_docker_stats(cxn)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment