#!/bin/bash # # Watch processlist and notify differences to an admin. # # Usage: Just fire-off. # If there's no basefile, it'll generate it. After that, it runs quietly. # Only change the adminmail if needed. # # If you have some processes which are constantly changing, you can have them ingnored # by changing the baselist. Just change the counter to -1 and it will be ignored the # next run. So, "grep 2" will become "grep -1". # This feature might come in handy when checking i.e. a webserver. Your amount of # httpd processes will never be constant. # # Bugs: Yes. Sometimes it will send you mail about "bash" or "grep". # If it does this too often, remove the baselist, so it will re-initialize. # # Let it initialize by cron, as you're shell probably will not be there all the time. # # $Id: pcds.old,v 1.15 2002/12/22 16:35:27 bart Exp $ # $Revision: 1.15 $ # # Bart Somers # tempfile=~/outfile.txt baselist=~/baselist.txt currlist=~/currlist.txt logfile=~/logfile.txt adminmail=root@localhost verbose=0 exec 2> /dev/null mailcommand=`which mailx` if [[ $? != 0 ]]; then mailcommand="mail" else mailcommand="mailx" fi getprocs() { ps -eo comm | grep -v COMMAND | sort > $tempfile } makelist() { if [[ -e $currlist ]]; then rm $currlist fi for word in `cat $tempfile`; do if [[ $word == $oldword ]]; then let "count += 1" elif [[ $word != $oldword ]]; then echo -n $oldword $count "" >> $currlist unset count oldword=$word let "count += 1" fi done echo >> $currlist } difflist() { declare -a base curr read -a base < $baselist read -a curr < $currlist if [[ $verbose -eq "1" ]];then echo Base ${#base[@]} Curr ${#curr[@]} fi count=0 countcurr=0 match=0 flag=0 while [[ $countcurr -lt ${#curr[@]} ]];do while [[ $count -lt ${#base[@]} ]]; do if [[ ${base[$count]} == ${curr[$countcurr]} ]];then match=1 let intcount="$count + 1" let intcountcurr="$countcurr + 1" if [[ ${base[$intcount]} -eq "-1" ]];then # Proces is on ignore list if [[ $verbose -eq "1" ]]; then echo "${base[$count]} is on the ignore-list." fi elif [[ ${curr[$intcountcurr]} -ne ${base[$intcount]} ]];then echo "Oops ${curr[$intcountcurr]} ${curr[$countcurr]} processes running instead of ${base[$intcount]}" flag=1 elif [[ ${curr[$intcountcurr]} -eq ${base[$intcount]} && $verbose -eq "1" ]];then echo "Currently ${curr[$intcountcurr]} ${curr[$countcurr]} processes running. Fine." fi fi #echo "countcurr= $countcurr" #sleep 1 let count="$count + 2" done #echo "count= $count" #sleep 1 if [[ $match -eq "0" ]];then echo "${curr[$intcountcurr]} ${curr[$countcurr]} processes running. Not seen before." flag=1 fi let countcurr="$countcurr + 2" count=0 match=0 done } getprocs if [[ $1 == "-init" || ! -e $baselist ]]; then makelist cp $currlist $baselist echo "Initialization complete." >> $logfile cat $logfile | $mailcommand -s "PCDS logging from `hostname` " $adminmail exit 0 fi makelist difflist >> $logfile if [[ $verbose -eq "1" ]];then ls -l $logfile # cat $logfile fi if [[ -e $logfile && $flag -eq "1" ]]; then cat $logfile | $mailcommand -s "PCDS logging from `hostname` " $adminmail fi #Do some clean-up work if [[ -e $tempfile ]]; then rm $tempfile fi if [[ -e $currlist ]]; then rm $currlist fi if [[ -e $logfile ]]; then rm $logfile fi exit 0