ControlScripts: complete hook_monitor script
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 19 Nov 2010 22:50:04 +0000 (00:50 +0200)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Fri, 19 Nov 2010 22:50:04 +0000 (00:50 +0200)
ControlScripts/hook_monitor_pid.sh [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 14269d4..7bff3c6
@@ -1,19 +1,90 @@
 #!/bin/bash
 
 #
-# monitor process characteristics using systat and iptables
+# monitor process resources using systat (/proc), free, iptables and /sys
 #
 # 2010, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
 #
 
-# per-process processor, disk and memory usage
-# pidstat
+# command line argument must be a process id
+if test $# -ne 1; then
+       echo "Usage: $0 <pid>" 1>&2
+       exit 1
+fi
 
-# per-system
-# iostat (processor & disk usage), free -s 1 (memory usage)
+pid=$1
 
-# per-process network bandwidth usage
-# iptables
+# check if pid exists
+kill -0 $pid > /dev/null 2>&1
+if test $? -ne 0; then
+       echo -e "PID $pid does not exists.\nUsage: $0 <pid>" 1>&2
+       exit 1
+fi
 
-# per-system
-# /sys/class/net/$iface/statistics/{tx,rx}_bytes (C app for periodic checks)
+# use _DEBUG="off" to turn off debug printing
+_DEBUG="off"
+
+# Read the global configuration file
+# Check if the global configuration file exists
+if [ ! -e globalconfig ]; then
+       echo "Warning: The global config file globalconfig does not exist."
+else
+       source globalconfig
+fi
+
+# cleanup code (run as signal handler)
+cleanup()
+{
+       kill -TERM $pidstat_pid
+       kill -TERM $iostat_pid
+       kill -TERM $free_pid
+       kill -TERM $timer_callback_pid
+}
+
+# function acting like a periodic callback
+timer_callback()
+{
+       while true; do
+               date +%F-%X
+               sudo iptables -t filter -L -n -v
+               cat /sys/class/net/eth0/statistics/rx_bytes /sys/class/net/eth0/statistics/tx_bytes
+               sleep 1
+       done
+}
+
+# run cleanup on signal receive
+trap cleanup 0 1 2 3 15
+
+# per-process processor (-u), disk (-d) and memory usage (-r)
+pidstat -u -d -r -p $pid 1 &
+pidstat_pid=$!
+
+# per-system (iostat and free)
+iostat -d -c 1 &
+iostat_pid=$!
+free -s 1 &
+free_pid=$!
+
+# per-process network bandwidth usage (iptables)
+# /sys/class/net/$iface/statistics/{tx,rx}_bytes (per system)
+
+# remove old iptables rules/chains
+sudo iptables -t filter -F udp_out > /dev/null 2>&1
+sudo iptables -t filter -F udp_in > /dev/null 2>&1
+sudo iptables -t filter -X udp_out > /dev/null 2>&1
+sudo iptables -t filter -X udp_in > /dev/null 2>&1
+sudo iptables -t filter -F OUTPUT > /dev/null 2>&1
+
+# add iptables rule
+sudo iptables -t filter -N udp_out
+sudo iptables -t filter -N udp_in
+sudo iptables -t filter -A OUTPUT -p udp -j udp_out
+sudo iptables -t filter -A udp_out -j ACCEPT
+sudo iptables -t filter -A OUTPUT -p udp -j udp_in
+sudo iptables -t filter -A udp_in -j ACCEPT
+
+timer_callback &
+timer_callback_pid=$!
+
+# wait for child processes to end (shouldn't happen)
+wait