From d3af41949007e9b937250c33b36e02ba404e42f3 Mon Sep 17 00:00:00 2001 From: Razvan Deaconescu Date: Sat, 20 Nov 2010 00:50:04 +0200 Subject: [PATCH] ControlScripts: complete hook_monitor script --- ControlScripts/hook_monitor_pid.sh | 89 +++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 9 deletions(-) mode change 100644 => 100755 ControlScripts/hook_monitor_pid.sh diff --git a/ControlScripts/hook_monitor_pid.sh b/ControlScripts/hook_monitor_pid.sh old mode 100644 new mode 100755 index 14269d4..7bff3c6 --- a/ControlScripts/hook_monitor_pid.sh +++ b/ControlScripts/hook_monitor_pid.sh @@ -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 " 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 " 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 -- 2.20.1