Skip to content

Commit

Permalink
pull container info through glances instead of docker socket
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrtye committed Nov 18, 2024
1 parent 439e028 commit 5215108
Showing 1 changed file with 22 additions and 52 deletions.
74 changes: 22 additions & 52 deletions telebot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

TELEBOT_TOKEN = os.environ.get("TELEBOT_TOKEN")
NOVEL_URL = os.environ.get("NOVEL_URL")
GLANCES_URL = os.environ.get("GLANCES_URL")

if TELEBOT_TOKEN is None or NOVEL_URL is None:
if TELEBOT_TOKEN is None or NOVEL_URL is None or GLANCES_URL is None:
print("Environment variables not fulfilled")
raise SystemExit

Expand All @@ -34,56 +35,25 @@ def DefaultEncode(reply):
return text


def dockerUsage():
# Create a Docker client
client = docker.DockerClient("unix:///var/run/docker.sock")
def containerUsage():
response = requests.get(GLANCES_URL)

# Get all containers
containers = client.containers.list()
if response.status_code != 200:
return ["Container usage is not currently available"]

total_cpu_usage = 0
total_memory_usage = 0
containers = json.loads(response.content)

reply = []

# Iterate over containers and retrieve resource usage
reply.append(f"{'Name':<12} {'CPU':<5} {'Memory':<5}")

stats1 = dict()
stats2 = dict()

for container in containers:
stats1[container.name] = container.stats(stream=False)

time.sleep(10)
container_name = container["name"]

for container in containers:
stats2[container.name] = container.stats(stream=False)

for container in containers:
# Get container name
container_name = container.name
cpu_percentage = container["cpu"]["total"]

# Get container stats
stats = stats1[container.name]
cpu_stats1 = stats["cpu_stats"]
cpu_usage1 = cpu_stats1["cpu_usage"]["total_usage"]
cpu_system1 = cpu_stats1["system_cpu_usage"]

stats = stats2[container.name]
cpu_stats2 = stats["cpu_stats"]
cpu_usage2 = cpu_stats2["cpu_usage"]["total_usage"]
cpu_system2 = cpu_stats2["system_cpu_usage"]

memory_stats = stats["memory_stats"]

# CPU usage percentage
cpu_delta = cpu_usage2 - cpu_usage1
system_delta = cpu_system2 - cpu_system1
cpu_percentage = (cpu_delta / system_delta) * 100

# Memory usage in MB
memory_usage = memory_stats["usage"]
memory_usage = container["memory"]["usage"]
memory_usage_mb = memory_usage / (1024 * 1024)

total_cpu_usage += cpu_percentage
Expand All @@ -93,7 +63,6 @@ def dockerUsage():
f"{container_name:<12} {cpu_percentage:<5.2f}% {memory_usage_mb:<5.1f} MB"
)

# Total usage
total_memory_usage_mb = total_memory_usage / (1024 * 1024)

reply.append(f"\nDocker CPU Usage: {total_cpu_usage:.2f} %")
Expand All @@ -104,16 +73,17 @@ def dockerUsage():

def novelUpdate():
response = requests.get(NOVEL_URL)

if response.status_code != 200:
return ["Novel update is not currently available"]

reply = []

if response.status_code == 200:
content = response.content.decode("utf-8")
data_dict = json.loads(content)
content = response.content.decode("utf-8")
data_dict = json.loads(content)

for novel, (title, url) in data_dict.items():
reply.append(f"{novel}: \n{title}\n{url}")
else:
reply.append(f"Novel update is not currently available")
for novel, (title, url) in data_dict.items():
reply.append(f"{novel}: \n{title}\n{url}")

return reply

Expand All @@ -122,9 +92,9 @@ def restore():
client = docker.DockerClient("unix:///var/run/docker.sock")
containers = client.containers.list(all=True, filters={"status": "exited"})
exited_containers = [
c
for c in containers
if c.attrs["HostConfig"]["RestartPolicy"]["Name"] != "unless-stopped"
container
for container in containers
if container.attrs["HostConfig"]["RestartPolicy"]["Name"] != "unless-stopped"
]
reply = []

Expand All @@ -144,7 +114,7 @@ def restore():

@bot.message_handler(commands=["info"])
def handle_info_command(message):
bot.reply_to(message, MarkdownV2Encode(dockerUsage()), parse_mode="MarkdownV2")
bot.reply_to(message, MarkdownV2Encode(containerUsage()), parse_mode="MarkdownV2")


@bot.message_handler(commands=["novel"])
Expand Down

0 comments on commit 5215108

Please sign in to comment.