Created
October 11, 2017 19:10
-
-
Save jhyland87/61e660fac98713eb6e8c4f30cc6deccf to your computer and use it in GitHub Desktop.
Awk script to total the data returned from ps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
# Examples | |
# $ ps -o pid,user,cpu,%cpu,%mem,nswap,sess,tty | ./ps_total.awk | |
# Count : 58 | |
# USER : 0 | |
# CPU : 0 | |
# %CPU : 1.8 | |
# %MEM : 2.5 | |
# NSWAP : 0 | |
# SESS : 0 | |
# TTY : 0 | |
# PID : 3272487 | |
# $ sudo ps -A -o pid,user,cpu,%cpu,%mem,nswap,sess,tty | ./ps_total.awk | |
# Count : 395 | |
# USER : 0 | |
# CPU : 0 | |
# %CPU : 47.4 | |
# %MEM : 98.2 | |
# NSWAP : 0 | |
# SESS : 0 | |
# TTY : 0 | |
# PID : 15177218 | |
# $ ps -A -o cpu,%cpu,%mem,nswap,p_ru,tsiz,vsz | ./ps_total.awk | |
# Count : 395 | |
# %CPU : 50.4 | |
# %MEM : 98.3 | |
# NSWAP : 0 | |
# P_RU : 0 | |
# TSIZ : 0 | |
# VSZ : 1062566720 | |
# CPU : 0 | |
# Todos | |
# - Accept a list of columns to limit processing to - EG: ps -A | ./ps_total.awk -v cols=cpu%,mem% | |
# - If the column header starts with %, then show it as a percentage | |
BEGIN { | |
headers["count"] = 0 | |
totals[""] = 0 | |
headerwid = 2 | |
} | |
{ | |
if ( NR == 1 ){ | |
for( i = 1; i <= NF; i++ ){ | |
headers[i] = $i | |
totals[i] = 0 | |
if ( length( i ) > headerwid ) | |
headerwid = length( i ) | |
} | |
next | |
} | |
headers["count"]++ | |
for(i = 1; i <= NF; i++) | |
totals[i] += $i | |
} | |
END { | |
headerwid += 3 | |
printf("%-"headerwid"s : %s\n", "Count", headers["count"]) | |
for ( t in totals ) { | |
if ( length(t) == 0 ) continue | |
printf("%-"headerwid"s : %s\n", headers[t], totals[t]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment