#!/bin/bash MATCH_INIT=" " MATCH="$MATCH_INIT" SKIP_INIT="XOIXIXIXOOIX" SKIP="$SKIP_INIT" DOMAIN=. WIDTH=0 USAGE="$0 [[--match|-m] expr] [[--skip|-s] expr] [[--width|-w] n] [domain_name1 ... domain_nameN] --match: limit output to lines that match egrep regular expression --skip: limit output to lines that DO NOT match egrep regular expression --width: Limit output to N characters per line Examples: # Show all non-root commands running on virtual hosts $0 --skip root # Show all current virtual host ssh logins $0 --match 'bash|ssh' # Show all processes for 3 domains $0 wwd-hosting.net domain.com webwizarddesign.com " while [ "x$1" != "x" ] do case $1 in --match|-m) # Regex shift; MATCH="$1"; ;; --skip|-s) # Regex shift; SKIP="$1"; ;; --help|-h) echo "$USAGE" exit ;; --width|-w) shift; WIDTH="$1"; ;; --*) echo "Unknown option $1" echo "$USAGE" exit ;; *) if [ "$DOMAIN" = "." ] then DOMAIN=$1 else DOMAIN="$DOMAIN|$1" fi ;; esac shift done # Get an egrep compatible list of sites .. for # one or more domains get "|" separated list, # for all domains use regex that gets all sites if [ "$DOMAIN" != "." ] then site="$( egrep "($DOMAIN) =" /etc/virtualhosting/mappings/domainmap | \ awk '{printf $3"|"}' )" if [ "x$site" = "x" ] then echo 'No such domain `'$DOMAIN"': please check spelling and capitalization." exit 1 fi site="(${site}XOXOXOX)" else site='site[0-9]*' fi # Now pull the pids out for processes that have # been started under those domain filesystems # (chrooted) .. use uniq to make sure we # don't see the same PID twice in output PIDS=$( egrep "/home/virtual/$site/fst" /proc/*/maps | \ awk '$2 ~ /x/' | awk -F/ '{print $7" "$3}' | sort | uniq ) if [ "x$PIDS" = "x" ] then echo "No virtual host processes running." exit fi printf "%-12s %-6s %-32s %-6s %-4s %-4s %s\n" \ USER PID DOMAIN START %CPU %MEM COMMAND echo "$PIDS" | \ while read name pid do # Only redo name lookup if the domain name # changes .. save some time. if [ "$saved" != "$name" ] then domain=$(awk '$3 == "'"$name"'" {print $1;exit}' \ /etc/virtualhosting/mappings/domainmap) fi saved="$name" # Get fields from ps and format it, replacing # the numeric user id with the username # from the virtual host! ps --pid $pid -w -h -o uid,pid,%cpu,%mem,stime,args | \ while read id pid cpu mem start args do # Get username of user as they appear in virtual # host password file. user=$(echo $id | sed -e 's/#//'| \ awk -F: '$3 == "'"$id"'" {print $1;exit}' \ /home/virtual/$name/fst/etc/passwd) printf "%-12s %-6s %-32s %-6s %-4s %-4s %s\n" \ $user $pid $domain "$start" "$cpu" "$mem" "$args" done done | egrep "$MATCH" | egrep -v "$SKIP" | sort -k 3 | \ awk '{ if ('$WIDTH' > 0) { $0 = substr($0,0,'$WIDTH'-1); } total++; print; } END { if (total == 0) { print "No matching processes."; } }' # Filter output and sort by domain name!