procmon: pidstat redefined
Written by Eric Schwimmer
I love pidstat (part of the sysstat package). It's highly useful when you want to monitor the performance of a single process (or small subset of processes). However it's output is a little ... verbose, and not terribly configurable. So I wrote a small Bash wrapper for it that prints stats for one or more processes (as determined by the regular expression that you pass to the script) in a more compact format, ala dstat:
#!/bin/bash
fatal() { echo $1; exit 1; }
[[ $# == 1 ]] || fatal "Usage: procmon <proc name>"
[[ $(which pidstat) == "" ]] && fatal "Missing 'pidstat' binary"
pids=$(pgrep $1)
[[ $pids == "" ]] && fatal "No processes with that name found"
numNoHeaderLines=20
while [[ 1 ]]
do
i=0
printf '%8.8s|%5.5s|%6.6s|%10.10s|%6.6s|%10.10s|%10.10s\n' \
'Time' 'PID' '%CPU' 'CSW/s' '%Mem' 'Kb read/s' 'Kb write/s'
while [[ "$i" -lt "$numNoHeaderLines" ]]
do
pidstat -p $pids -d -r -u -h 5 1 | tail -1 | \
awk '{"date +%T -d @"$1 | getline date;
printf "%8.8s|%5.5s|%6.6s|%10.0f|%6.6s|%10.0f|%10.0f\n",
date, $3, $7, $17+$18, $13, $14, $15}' || exit
((++i))
done
done
I'm thinking about cranking out a script that would do most of what pidstat does, but add network traffic data, as well as the ability to print individual columns (e.g. %CPU utilization without having to print VSZ/RSS). Eh, much /proc wrangling would definitely be required.