Skip to content

Commit

Permalink
add topology
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamilcuk committed Sep 28, 2024
1 parent 0608425 commit 371b916
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/nomad_tools/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
entry_nodenametoid,
entry_port,
entry_task,
entry_topology,
entry_vardir,
entry_watch,
)
Expand Down Expand Up @@ -80,6 +81,7 @@ def cli():
cli.add_command(entry_listnodeattributes.cli)
cli.add_command(entry_nodenametoid.cli)
cli.add_command(entry_port.cli)
cli.add_command(entry_topology.cli)
cli.add_command(entry_task.cli)
cli.add_command(entry_vardir.cli)
cli.add_command(entry_watch.cli)
Expand Down
71 changes: 71 additions & 0 deletions src/nomad_tools/entry_topology.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from __future__ import annotations

import logging
from typing import List

import click

from . import common_click
from .common_nomad import mynomad

log = logging.getLogger(__name__)


@click.command(
"topology",
help="Like the topology web interface",
)
@common_click.common_options()
@common_click.verbose_option()
def cli():
logging.basicConfig()
allocations = mynomad.get(
"allocations", params=dict(resources=True, task_states=False)
)
nodes = mynomad.get("nodes", params=dict(resources=True))
tooutput: List[List[str]] = []
for node in nodes:
nodeallocs = [x for x in allocations if x["NodeID"] == node["ID"]]
alloc_taskname_tasks = [
(alloc, taskname, task)
for alloc in nodeallocs
for taskname, task in alloc["AllocatedResources"]["Tasks"].items()
]
alloc_taskname_tasks.sort(
key=lambda x: x[2]["Memory"]["MemoryMB"], reverse=True
)
used_cpu = sum(
(task["Cpu"]["CpuShares"] for _, _, task in alloc_taskname_tasks),
0,
)
used_memory = sum(
(task["Memory"]["MemoryMB"] for _, _, task in alloc_taskname_tasks),
0,
)
tooutput.append(
[
node["Datacenter"],
node["Name"],
node["Status"],
f'{used_memory}MB/{node["NodeResources"]["Memory"]["MemoryMB"]}MB',
f'{used_cpu}MHz/{node["NodeResources"]["Cpu"]["CpuShares"]}MHz',
f"{len(nodeallocs)}allocs",
*[
"["
+ " ".join(
[
alloc["Name"],
taskname,
alloc["Namespace"],
f'{task["Memory"]["MemoryMB"]}MB',
f'{task["Cpu"]["CpuShares"]}MHz',
]
)
+ "]"
for alloc, taskname, task in alloc_taskname_tasks
],
]
)
tooutput.sort(key=lambda x: int(x[3].split("M")[0]))
for x in tooutput:
print(*x)

0 comments on commit 371b916

Please sign in to comment.