#!/bin/ksh
# date_ps_ef.ksh
# Report on world.php
lastcur=""
while [ 0 -lt 1 ]; do
   cur="`ps -ef | grep '/world.php' | grep -v 'grep ' | wc -l | tr -d ' '`"
   echo "`date` $cur"
   if [ "$lastcur" != "$cur" ]; then
     if [ -z "$lastcur" ]; then
       echo "`ps -ef | grep '/world.php' | grep -v 'grep '`" > date_ps_ef_first.txt
       #cat date_ps_ef_first.txt | sendmail -f rmetcalfe15@gmail.com -s "First Date ps-ef world.php Report at `date`" rmetcalfe15@gmail.com
       #echo -e "Content-Type: text/plain\nFrom: rmetcalfe@rjmprogramming.com.au\nto:  rmetcalfe15@gmail.com\ncc: rmetcalfe@rjmprogramming.com.au\nSubject: First Date ps-ef world.php Report at `date`\n\n`cat date_ps_ef_first.txt`" | sendmail -t -f rmetcalfe15@gmail.com 
       echo -e "Content-Type: text/html\nFrom: rmetcalfe@rjmprogramming.com.au\nto:  rmetcalfe15@gmail.com\ncc: rmetcalfe@rjmprogramming.com.au\nSubject: First Date ps-ef world.php Report at `date`\n\n<html><head><style> tr:nth-child(even) {background-color: #CCC !important;} tr:nth-child(odd) {background-color: #FFF !important;} </style></head><body><h1>First Date ps-ef world.php Report at `date`</h1><table style='border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'><tr><th>UID</th><th>PID</th><th>PPID</th><th>C</th><th>STIME</th><th>TTY</th><th>TIME</th><th>CMD</th></tr>`cat date_ps_ef_first.txt | sed '/^/s//<tr><td>/g' | sed 's/ \{1,\}/<\/td><td>/g' | awk '{ t=0; while (getline \$0) { if (t == 0) { print \"<tr style=background-color:#e7e7e7 !important;\" substr(\$0,3);  } else { print \$0; } t=1-t; } }' | sed '/$/s//<\/td><\/tr>/g'`</table></body></html>" | sendmail -t -f rmetcalfe15@gmail.com 
     else 
       echo "`ps -ef | grep '/world.php' | grep -v 'grep '`" > date_ps_ef_next.txt
       #diff date_ps_ef_first.txt date_ps_ef_next.txt | sendmail -f rmetcalfe15@gmail.com -s "Changed Date ps-ef world.php Report at `date`" rmetcalfe15@gmail.com       
       echo -e "Content-Type: text/html\nFrom: rmetcalfe@rjmprogramming.com.au\nto:  rmetcalfe15@gmail.com\ncc: rmetcalfe@rjmprogramming.com.au\nSubject: Changed Date ps-ef world.php Report at `date`\n\n<html><body><h1>Changed Date ps-ef world.php Report at `date`</h1><textarea rows=${cur} style='background-color:#e7e7e7;border:5px dotted olive;width:70%;height:100%;margin: 5 5 5 5;font-family:Courier-New;'>`diff date_ps_ef_first.txt date_ps_ef_next.txt`</textarea></body></html>" | sendmail -t -f rmetcalfe15@gmail.com

       cat date_ps_ef_next.txt > date_ps_ef_first.txt
     fi
     lastcur="$cur"
   fi
   # ps -ef | grep '/world.php' | grep -v 'grep '
   sleep 30 
done
exit

