Advertisement
sandervanvugt

Untitled

Oct 14th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. /bin/bash
  2. # Script that monitors the top-active process. The script sends an email to the
  3. # user root if utilization of the top active process goed beyond 80%. Of course,
  4. # this script can be tuned to do anything else in such a case.
  5. #
  6. # Start the script, and it will run forever.
  7.  
  8. while true
  9. do
  10. # Check every 60 seconds if we have a process causing high CPU load
  11. sleep 6
  12. SAUSAGE=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1`
  13. USAGE=`echo ${SAUSAGE} | awk '{ print $1 }'`
  14. USAGE=${SAUSAGE%.*}
  15. PID=`echo ${SAUSAGE} | awk '{print $2 }'`
  16. PNAME=`echo ${SAUSAGE} | awk '{print $3 }'`
  17.  
  18. # Only if we have a high CPU load on one process, run a check within 7 seconds
  19. # In this check, we should monitor if the process is still that active
  20. # If that's the case, root gets a message
  21. if [ $USAGE -gt 80 ]
  22. then
  23. USAGE1=$USAGE
  24. PID1=$PID
  25. PNAME1=$PNAME
  26. sleep 7
  27. USAGE2=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{ print $1 } '`
  28. USAGE2=${USAGE2%.*}
  29. PID2=`echo ${USAGE2} | awk '{print $2 }'`
  30. PNAME2=`echo ${USAGE2} | awk '{print $3 }'`
  31.  
  32. # Now we have variables with the old process information and with the
  33. # new information
  34.  
  35. [ $USAGE2 -gt 80 ] && [ $PID1 = $PID2 ] && mail -s "CPU load of $PNAME is above 80%" root@blah.com < .
  36. fi
  37. done
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement