Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create instant_per_process_cpu_mem_usage.sh #157

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions instant_per_process_cpu_mem_usage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

# Get CPU & Mem metrics, lightweight implementation
# Written (quickly) in 2023 by NetInvent
# SCRIPT_VERSION 2023110201


# -w [n] forces total column width to [n] so data won't be truncated
# -c forces full commandline, which we need in order to get command arguments
# -bn 1 makes top run once in batch mode

top -w 120 -cbn 1 | awk '{
# Skip headers
if (NR<8) { next };
# Get all command arguments
args=""; for(i = 13; i<= NF; i++) if ($i!="") {args=args" "$i};
# Sanitize arguments
gsub("{|}|\\\\|\"", "", args);
# Sanitize debian style floats in top
gsub(",", ".", $9);
gsub(",", ".", $10);
# Dont keep more than 30 chars for args, since we limited top -w size, we wont need this
#args=substr(args, 1, 30);
# Remove self process
if ($12=="top" && args=" -w 120 -cbn 1") { next };
# Do not keep not cpu hungry entries
if ($9!="0.0") {
if (cputype==0) { printf "# TYPE top_process_cpu_usage gauge\n# HELP top_process_cpu_usage ps gathered instant CPU usage per process\n"; cputype=1 };
printf "top_process_cpu_usage{pid=\""$1"\",process=\""$12"\",sanitized_args=\""args"\"} " $9z"\n"};
# Do not keep not memory hungry entries
if ($10!="0.0") {
if (memtype==0) { printf "# TYPE top_process_memory_usage gauge\n# HELP top_process_memory_usage ps gathered memory usage per process\n"; memtype=1 };
printf "top_process_memory_usage{pid=\""$1"\",process=\""$12"\",sanitized_args=\""args"\"} " $10z"\n"};
}'