Skip to content

Commit

Permalink
Feat: Allow process filtering by container id (#4)
Browse files Browse the repository at this point in the history
- Part of #2110 in testsuite.

Signed-off-by: svteb <[email protected]>
  • Loading branch information
svteb authored Aug 2, 2024
1 parent 7f2fda7 commit b7e9d7a
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/kernel_introspection/k8s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ module KernelIntrospection
pids
end

def self.pids_by_container(container_id, node)
# Command explanation:
# 1. Get all process directories in /proc with find.
# 2. Exec grep <container_id> for every returned process cgroup file,
# redirect any errors (in case some temporary process disappears/find cannot access a directory)
# 3. Grep returns paths with desired cgroup in format /proc/<pid>/cgroup, this output gets trimmed
# by sed to only return a list of <pid>s
command = "/bin/sh -c \"find /proc -maxdepth 1 -regex '/proc/[0-9]+' -exec grep -l '#{container_id}' {}/cgroup \\; 2>/dev/null | sed -e 's,/proc/\\([0-9]*\\)/cgroup,\\1,'\""
result = ClusterTools.exec_by_node(command, node)
output = result["output"].strip

pids = output.split("\n")
pids
end

def self.all_statuses_by_pids(pids : Array(String), node) : Array(String)
Log.info { "all_statuses_by_pids" }
proc_statuses = pids.map do |pid|
Expand Down

0 comments on commit b7e9d7a

Please sign in to comment.