4 # monitor process resources using systat (/proc), free, iptables and /sys
6 # 2010, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
9 # command line argument must be a process id
10 if test $# -ne 1; then
11 echo "Usage: $0 <pid>" 1>&2
18 kill -0 $pid > /dev/null 2>&1
19 if test $? -ne 0; then
21 echo -e "PID $pid does not exists.\nUsage: $0 <pid>" 1>&2
25 # use _DEBUG="off" to turn off debug printing
28 # Read the global configuration file
29 # Check if the global configuration file exists
30 if [ ! -e globalconfig ]; then
31 echo "Warning: The global config file globalconfig does not exist."
36 # cleanup code (run as signal handler)
39 kill -TERM $pidstat_pid
40 kill -TERM $iostat_pid
42 kill -TERM $timer_callback_pid
45 # function acting like a periodic callback
50 sudo iptables -t filter -L -n -v
51 cat /sys/class/net/eth0/statistics/rx_bytes /sys/class/net/eth0/statistics/tx_bytes
56 # run cleanup on signal receive
57 trap cleanup 0 1 2 3 15
59 # per-process processor (-u), disk (-d) and memory usage (-r)
60 pidstat -u -d -r -p $pid 1 &
63 # per-system (iostat and free)
69 # per-process network bandwidth usage (iptables)
70 # /sys/class/net/$iface/statistics/{tx,rx}_bytes (per system)
72 # remove old iptables rules/chains
73 sudo iptables -t filter -F udp_out > /dev/null 2>&1
74 sudo iptables -t filter -F udp_in > /dev/null 2>&1
75 sudo iptables -t filter -X udp_out > /dev/null 2>&1
76 sudo iptables -t filter -X udp_in > /dev/null 2>&1
77 sudo iptables -t filter -F OUTPUT > /dev/null 2>&1
80 sudo iptables -t filter -N udp_out
81 sudo iptables -t filter -N udp_in
82 sudo iptables -t filter -A OUTPUT -p udp -j udp_out
83 sudo iptables -t filter -A udp_out -j ACCEPT
84 sudo iptables -t filter -A OUTPUT -p udp -j udp_in
85 sudo iptables -t filter -A udp_in -j ACCEPT
90 # wait for child processes to end (shouldn't happen)