I know I can get the real-time stats using top but have no idea what
to do with them
批处理模式可能很有用:
-b : Batch mode operation
Starts top in ’Batch mode’,which could be useful for sending output from top to other programs or
to a file. In this mode,top will not accept input and runs until the iterations limit you’ve set
with the ’-n’ command-line option or until killed.
例如:
$top -b -n 1 -u | awk 'NR > 7 { sum += $9 } END { print sum }'
cpu_per_user_gmetric.sh
#!/bin/bash
USERS="a b c"
for user in $USERS; do
/usr/bin/gmetric --name cpu_per_"$user"_user --value `top -b -n 1 -u $user | awk 'NR>7 { sum += $9; } END { print sum; }'` --type uint8 --unit Percent
done
crontab -l
* * * * * /path/to/cpu_per_user_gmetric.sh
这是结果: